blob: d5423cec22fd0e11e288286fc3cdc047ee1bc8aa [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
Eli Bendersky75299e32013-04-19 22:29:18 +000071 /// \brief The specified machine instr operand is a vreg, and that
Chris Lattnerbeac75d2010-09-05 02:18:34 +000072 /// 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.
Eli Bendersky75299e32013-04-19 22:29:18 +000075 virtual bool tryToFoldLoadIntoMI(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
Eli Bendersky50125482013-04-17 20:10:13 +0000110 bool X86SelectDivRem(const Instruction *I);
111
Dan Gohman46510a72010-04-15 01:51:59 +0000112 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000113
Dan Gohman46510a72010-04-15 01:51:59 +0000114 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000115
Dan Gohman46510a72010-04-15 01:51:59 +0000116 bool X86SelectFPExt(const Instruction *I);
117 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000118
Dan Gohman46510a72010-04-15 01:51:59 +0000119 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
120 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000121
Eli Friedman25255cb2011-06-10 23:39:36 +0000122 bool DoSelectCall(const Instruction *I, const char *MemIntName);
123
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000124 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000125 return getTargetMachine()->getInstrInfo();
126 }
127 const X86TargetMachine *getTargetMachine() const {
128 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000129 }
130
Dan Gohman46510a72010-04-15 01:51:59 +0000131 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000132
Dan Gohman46510a72010-04-15 01:51:59 +0000133 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000134
Eli Friedman2790ba82011-04-27 22:41:55 +0000135 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
136
Evan Chengf3d4efe2008-09-07 09:09:33 +0000137 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
138 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000139 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000140 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
141 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000142 }
143
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000144 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000145
Eli Friedmanc0883452011-05-20 22:21:04 +0000146 bool IsMemcpySmall(uint64_t Len);
147
Eli Friedmand5089a92011-04-27 01:45:07 +0000148 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
149 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000150};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000151
Chris Lattner087fcf32009-03-08 18:44:31 +0000152} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000153
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000154bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000155 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
156 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000157 // Unhandled type. Halt "fast" selection and bail.
158 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000159
160 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000161 // For now, require SSE/SSE2 for performing floating-point operations,
162 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topperf4cfc442012-08-11 17:53:00 +0000164 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topperf4cfc442012-08-11 17:53:00 +0000166 return false;
Dan Gohman9b66d732008-09-30 00:48:39 +0000167 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000168 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000169 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000170 // We only handle legal types. For example, on x86-32 the instruction
171 // selector contains all of the 64-bit instructions from x86-64,
172 // under the assumption that i64 won't be used if the target doesn't
173 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000174 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000175}
176
177#include "X86GenCallingConv.inc"
178
Evan Cheng0de588f2008-09-05 21:00:03 +0000179/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000180/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000181/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000182bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000183 unsigned &ResultReg) {
184 // Get opcode and regclass of the output for the given load instruction.
185 unsigned Opc = 0;
186 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000188 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000189 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000190 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000191 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +0000192 RC = &X86::GR8RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000193 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000194 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000195 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +0000196 RC = &X86::GR16RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000197 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000198 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000199 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000200 RC = &X86::GR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000201 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000203 // Must be in x86-64 mode.
204 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000205 RC = &X86::GR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000206 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000207 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000208 if (X86ScalarSSEf32) {
209 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +0000210 RC = &X86::FR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000211 } else {
212 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +0000213 RC = &X86::RFP32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000214 }
215 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000216 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000217 if (X86ScalarSSEf64) {
218 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +0000219 RC = &X86::FR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000220 } else {
221 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +0000222 RC = &X86::RFP64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000223 }
224 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000225 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000226 // No f80 support yet.
227 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000228 }
229
230 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000231 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
232 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000233 return true;
234}
235
Evan Chengf3d4efe2008-09-07 09:09:33 +0000236/// X86FastEmitStore - Emit a machine instruction to store a value Val of
237/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
238/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000239/// i.e. V. Return true if it is possible.
240bool
Chris Lattnerb44101c2011-04-19 05:09:50 +0000241X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000242 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000243 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000244 switch (VT.getSimpleVT().SimpleTy) {
245 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000246 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000247 case MVT::i1: {
248 // Mask out all but lowest bit.
Craig Topperc9099502012-04-20 06:31:50 +0000249 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000250 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000251 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
252 Val = AndResult;
253 }
254 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000255 case MVT::i8: Opc = X86::MOV8mr; break;
256 case MVT::i16: Opc = X86::MOV16mr; break;
257 case MVT::i32: Opc = X86::MOV32mr; break;
258 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
259 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000260 Opc = X86ScalarSSEf32 ?
261 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000262 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000263 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000264 Opc = X86ScalarSSEf64 ?
265 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000266 break;
Lang Hamese4824712011-10-18 22:11:33 +0000267 case MVT::v4f32:
268 Opc = X86::MOVAPSmr;
269 break;
270 case MVT::v2f64:
271 Opc = X86::MOVAPDmr;
272 break;
273 case MVT::v4i32:
274 case MVT::v2i64:
275 case MVT::v8i16:
276 case MVT::v16i8:
277 Opc = X86::MOVDQAmr;
278 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000279 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000280
Dan Gohman84023e02010-07-10 09:00:22 +0000281 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
282 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000283 return true;
284}
285
Dan Gohman46510a72010-04-15 01:51:59 +0000286bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000287 const X86AddressMode &AM) {
288 // Handle 'null' like i32/i64 0.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000289 if (isa<ConstantPointerNull>(Val))
290 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000291
Chris Lattner438949a2008-10-15 05:30:52 +0000292 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000293 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000294 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000295 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000296 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000297 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000298 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000299 case MVT::i8: Opc = X86::MOV8mi; break;
300 case MVT::i16: Opc = X86::MOV16mi; break;
301 case MVT::i32: Opc = X86::MOV32mi; break;
302 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000303 // Must be a 32-bit sign extended value.
Jakub Staszakeaf77252012-11-15 19:05:23 +0000304 if (isInt<32>(CI->getSExtValue()))
Chris Lattner438949a2008-10-15 05:30:52 +0000305 Opc = X86::MOV64mi32;
306 break;
307 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000308
Chris Lattner438949a2008-10-15 05:30:52 +0000309 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000310 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
311 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000312 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000313 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000314 return true;
315 }
316 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000317
Chris Lattner438949a2008-10-15 05:30:52 +0000318 unsigned ValReg = getRegForValue(Val);
319 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000320 return false;
321
Chris Lattner438949a2008-10-15 05:30:52 +0000322 return X86FastEmitStore(VT, ValReg, AM);
323}
324
Evan Cheng24e3a902008-09-08 06:35:17 +0000325/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
326/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
327/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000328bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
329 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000330 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000331 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
332 Src, /*TODO: Kill=*/false);
Jakub Staszakfe9b5a42013-02-14 21:50:09 +0000333 if (RR == 0)
Owen Andersonac34a002008-09-11 19:44:55 +0000334 return false;
Jakub Staszakfe9b5a42013-02-14 21:50:09 +0000335
336 ResultReg = RR;
337 return true;
Evan Cheng24e3a902008-09-08 06:35:17 +0000338}
339
Dan Gohman0586d912008-09-10 20:11:02 +0000340/// X86SelectAddress - Attempt to fill in an address from the given value.
341///
Dan Gohman46510a72010-04-15 01:51:59 +0000342bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
343 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000344 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000345 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000346 // Don't walk into other basic blocks; it's possible we haven't
347 // visited them yet, so the instructions may not yet be assigned
348 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000349 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
350 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
351 Opcode = I->getOpcode();
352 U = I;
353 }
Dan Gohman46510a72010-04-15 01:51:59 +0000354 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000355 Opcode = C->getOpcode();
356 U = C;
357 }
Dan Gohman0586d912008-09-10 20:11:02 +0000358
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000359 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner868ee942010-06-15 19:08:40 +0000360 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000361 // Fast instruction selection doesn't support the special
362 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000363 return false;
364
Dan Gohman35893082008-09-18 23:23:44 +0000365 switch (Opcode) {
366 default: break;
367 case Instruction::BitCast:
368 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000369 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000370
371 case Instruction::IntToPtr:
372 // Look past no-op inttoptrs.
373 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000374 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000375 break;
Dan Gohman35893082008-09-18 23:23:44 +0000376
377 case Instruction::PtrToInt:
378 // Look past no-op ptrtoints.
379 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000380 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000381 break;
Dan Gohman35893082008-09-18 23:23:44 +0000382
383 case Instruction::Alloca: {
384 // Do static allocas.
385 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000386 DenseMap<const AllocaInst*, int>::iterator SI =
387 FuncInfo.StaticAllocaMap.find(A);
388 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000389 AM.BaseType = X86AddressMode::FrameIndexBase;
390 AM.Base.FrameIndex = SI->second;
391 return true;
392 }
393 break;
Dan Gohman35893082008-09-18 23:23:44 +0000394 }
395
396 case Instruction::Add: {
397 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000398 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000399 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
400 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000401 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000402 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000403 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000404 }
Dan Gohman0586d912008-09-10 20:11:02 +0000405 }
Dan Gohman35893082008-09-18 23:23:44 +0000406 break;
407 }
408
409 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000410 X86AddressMode SavedAM = AM;
411
Dan Gohman35893082008-09-18 23:23:44 +0000412 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000413 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000414 unsigned IndexReg = AM.IndexReg;
415 unsigned Scale = AM.Scale;
416 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000417 // Iterate through the indices, folding what we can. Constants can be
418 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000419 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000420 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000421 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000422 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman35893082008-09-18 23:23:44 +0000423 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000424 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
425 continue;
426 }
Eric Christopher471e4222011-06-08 23:55:35 +0000427
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000428 // A array/variable index is always of the form i*S where S is the
429 // constant scale size. See if we can push the scale into immediates.
430 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
431 for (;;) {
432 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
433 // Constant-offset addressing.
434 Disp += CI->getSExtValue() * S;
435 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000436 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000437 if (isa<AddOperator>(Op) &&
438 (!isa<Instruction>(Op) ||
439 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
440 == FuncInfo.MBB) &&
441 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
442 // An add (in the same block) with a constant operand. Fold the
443 // constant.
444 ConstantInt *CI =
445 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
446 Disp += CI->getSExtValue() * S;
447 // Iterate on the other operand.
448 Op = cast<AddOperator>(Op)->getOperand(0);
449 continue;
450 }
451 if (IndexReg == 0 &&
452 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
453 (S == 1 || S == 2 || S == 4 || S == 8)) {
454 // Scaled-index addressing.
455 Scale = S;
456 IndexReg = getRegForGEPIndex(Op).first;
457 if (IndexReg == 0)
458 return false;
459 break;
460 }
461 // Unsupported.
462 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000463 }
464 }
Dan Gohman09aae462008-09-26 20:04:15 +0000465 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000466 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000467 break;
Dan Gohman35893082008-09-18 23:23:44 +0000468 // Ok, the GEP indices were covered by constant-offset and scaled-index
469 // addressing. Update the address state and move on to examining the base.
470 AM.IndexReg = IndexReg;
471 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000472 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000473 if (X86SelectAddress(U->getOperand(0), AM))
474 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000475
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000476 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000477 // our address and just match the value instead of completely failing.
478 AM = SavedAM;
479 break;
Dan Gohman35893082008-09-18 23:23:44 +0000480 unsupported_gep:
481 // Ok, the GEP indices weren't all covered.
482 break;
483 }
484 }
485
486 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000487 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Eli Friedmana6176ad2011-09-22 23:41:28 +0000488 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000489 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000490 return false;
491
Eli Friedmana6176ad2011-09-22 23:41:28 +0000492 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000493 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000494 if (GVar->isThreadLocal())
495 return false;
Eric Christopher471e4222011-06-08 23:55:35 +0000496
Eli Friedmana6176ad2011-09-22 23:41:28 +0000497 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
498 // it works...).
499 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
500 if (const GlobalVariable *GVar =
501 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
502 if (GVar->isThreadLocal())
503 return false;
504
Chris Lattner0a1c9972011-04-17 17:47:38 +0000505 // RIP-relative addresses can't have additional register operands, so if
506 // we've already folded stuff into the addressing mode, just force the
507 // global value into its own register, which we can use as the basereg.
508 if (!Subtarget->isPICStyleRIPRel() ||
509 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
510 // Okay, we've committed to selecting this global. Set up the address.
511 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000512
Chris Lattner0a1c9972011-04-17 17:47:38 +0000513 // Allow the subtarget to classify the global.
514 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000515
Chris Lattner0a1c9972011-04-17 17:47:38 +0000516 // If this reference is relative to the pic base, set it now.
517 if (isGlobalRelativeToPICBase(GVFlags)) {
518 // FIXME: How do we know Base.Reg is free??
519 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000520 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000521
522 // Unless the ABI requires an extra load, return a direct reference to
523 // the global.
524 if (!isGlobalStubReference(GVFlags)) {
525 if (Subtarget->isPICStyleRIPRel()) {
526 // Use rip-relative addressing if we can. Above we verified that the
527 // base and index registers are unused.
528 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
529 AM.Base.Reg = X86::RIP;
530 }
531 AM.GVOpFlags = GVFlags;
532 return true;
533 }
534
535 // Ok, we need to do a load from a stub. If we've already loaded from
536 // this stub, reuse the loaded pointer, otherwise emit the load now.
537 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
538 unsigned LoadReg;
539 if (I != LocalValueMap.end() && I->second != 0) {
540 LoadReg = I->second;
541 } else {
542 // Issue load from stub.
543 unsigned Opc = 0;
544 const TargetRegisterClass *RC = NULL;
545 X86AddressMode StubAM;
546 StubAM.Base.Reg = AM.Base.Reg;
547 StubAM.GV = GV;
548 StubAM.GVOpFlags = GVFlags;
549
550 // Prepare for inserting code in the local-value area.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000551 SavePoint SaveInsertPt = enterLocalValueArea();
Chris Lattner0a1c9972011-04-17 17:47:38 +0000552
553 if (TLI.getPointerTy() == MVT::i64) {
554 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000555 RC = &X86::GR64RegClass;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000556
557 if (Subtarget->isPICStyleRIPRel())
558 StubAM.Base.Reg = X86::RIP;
559 } else {
560 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000561 RC = &X86::GR32RegClass;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000562 }
563
564 LoadReg = createResultReg(RC);
565 MachineInstrBuilder LoadMI =
566 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
567 addFullAddress(LoadMI, StubAM);
568
569 // Ok, back to normal mode.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000570 leaveLocalValueArea(SaveInsertPt);
Chris Lattner0a1c9972011-04-17 17:47:38 +0000571
572 // Prevent loading GV stub multiple times in same MBB.
573 LocalValueMap[V] = LoadReg;
574 }
575
576 // Now construct the final address. Note that the Disp, Scale,
577 // and Index values may already be set here.
578 AM.Base.Reg = LoadReg;
579 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000580 return true;
581 }
Dan Gohman0586d912008-09-10 20:11:02 +0000582 }
583
Dan Gohman97135e12008-09-26 19:15:30 +0000584 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000585 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000586 if (AM.Base.Reg == 0) {
587 AM.Base.Reg = getRegForValue(V);
588 return AM.Base.Reg != 0;
589 }
590 if (AM.IndexReg == 0) {
591 assert(AM.Scale == 1 && "Scale with no index!");
592 AM.IndexReg = getRegForValue(V);
593 return AM.IndexReg != 0;
594 }
595 }
596
597 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000598}
599
Chris Lattner0aa43de2009-07-10 05:33:42 +0000600/// X86SelectCallAddress - Attempt to fill in an address from the given value.
601///
Dan Gohman46510a72010-04-15 01:51:59 +0000602bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
603 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000604 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000605 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000606 Opcode = I->getOpcode();
607 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000608 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000609 Opcode = C->getOpcode();
610 U = C;
611 }
612
613 switch (Opcode) {
614 default: break;
615 case Instruction::BitCast:
616 // Look past bitcasts.
617 return X86SelectCallAddress(U->getOperand(0), AM);
618
619 case Instruction::IntToPtr:
620 // Look past no-op inttoptrs.
621 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
622 return X86SelectCallAddress(U->getOperand(0), AM);
623 break;
624
625 case Instruction::PtrToInt:
626 // Look past no-op ptrtoints.
627 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
628 return X86SelectCallAddress(U->getOperand(0), AM);
629 break;
630 }
631
632 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000633 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000634 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000635 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000636 return false;
637
638 // RIP-relative addresses can't have additional register operands.
639 if (Subtarget->isPICStyleRIPRel() &&
640 (AM.Base.Reg != 0 || AM.IndexReg != 0))
641 return false;
642
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000643 // Can't handle DLLImport.
644 if (GV->hasDLLImportLinkage())
645 return false;
646
647 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000648 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000649 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000650 return false;
651
652 // Okay, we've committed to selecting this global. Set up the basic address.
653 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000654
Chris Lattnere6c07b52009-07-10 05:45:15 +0000655 // No ABI requires an extra load for anything other than DLLImport, which
656 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000657 if (Subtarget->isPICStyleRIPRel()) {
658 // Use rip-relative addressing if we can. Above we verified that the
659 // base and index registers are unused.
660 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
661 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000662 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000663 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
664 } else if (Subtarget->isPICStyleGOT()) {
665 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000666 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000667
Chris Lattner0aa43de2009-07-10 05:33:42 +0000668 return true;
669 }
670
671 // If all else fails, try to materialize the value in a register.
672 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
673 if (AM.Base.Reg == 0) {
674 AM.Base.Reg = getRegForValue(V);
675 return AM.Base.Reg != 0;
676 }
677 if (AM.IndexReg == 0) {
678 assert(AM.Scale == 1 && "Scale with no index!");
679 AM.IndexReg = getRegForValue(V);
680 return AM.IndexReg != 0;
681 }
682 }
683
684 return false;
685}
686
687
Owen Andersona3971df2008-09-04 07:08:58 +0000688/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000689bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000690 // Atomic stores need special handling.
Lang Hamese4824712011-10-18 22:11:33 +0000691 const StoreInst *S = cast<StoreInst>(I);
692
693 if (S->isAtomic())
694 return false;
695
Duncan Sands1440e8b2010-11-03 11:35:31 +0000696 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000697 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000698 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000699
Dan Gohman0586d912008-09-10 20:11:02 +0000700 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000701 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000702 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000703
Chris Lattner438949a2008-10-15 05:30:52 +0000704 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000705}
706
Dan Gohman84023e02010-07-10 09:00:22 +0000707/// X86SelectRet - Select and emit code to implement ret instructions.
708bool X86FastISel::X86SelectRet(const Instruction *I) {
709 const ReturnInst *Ret = cast<ReturnInst>(I);
710 const Function &F = *I->getParent()->getParent();
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000711 const X86MachineFunctionInfo *X86MFInfo =
712 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohman84023e02010-07-10 09:00:22 +0000713
714 if (!FuncInfo.CanLowerReturn)
715 return false;
716
717 CallingConv::ID CC = F.getCallingConv();
718 if (CC != CallingConv::C &&
719 CC != CallingConv::Fast &&
720 CC != CallingConv::X86_FastCall)
721 return false;
722
723 if (Subtarget->isTargetWin64())
724 return false;
725
726 // Don't handle popping bytes on return for now.
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000727 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszakd61932b2013-02-17 18:35:25 +0000728 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000729
730 // fastcc with -tailcallopt is intended to provide a guaranteed
731 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000732 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohman84023e02010-07-10 09:00:22 +0000733 return false;
734
735 // Let SDISel handle vararg functions.
736 if (F.isVarArg())
737 return false;
738
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000739 // Build a list of return value registers.
740 SmallVector<unsigned, 4> RetRegs;
741
Dan Gohman84023e02010-07-10 09:00:22 +0000742 if (Ret->getNumOperands() > 0) {
743 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +0000744 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohman84023e02010-07-10 09:00:22 +0000745
746 // Analyze operands of the call, assigning locations to each operand.
747 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000748 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +0000749 I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000750 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000751
752 const Value *RV = Ret->getOperand(0);
753 unsigned Reg = getRegForValue(RV);
754 if (Reg == 0)
755 return false;
756
757 // Only handle a single return value for now.
758 if (ValLocs.size() != 1)
759 return false;
760
761 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000762
Dan Gohman84023e02010-07-10 09:00:22 +0000763 // Don't bother handling odd stuff for now.
764 if (VA.getLocInfo() != CCValAssign::Full)
765 return false;
766 // Only handle register returns for now.
767 if (!VA.isRegLoc())
768 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000769
770 // The calling-convention tables for x87 returns don't tell
771 // the whole story.
772 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
773 return false;
774
Eli Friedman22486c92011-05-18 23:13:10 +0000775 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000776 EVT SrcVT = TLI.getValueType(RV->getType());
777 EVT DstVT = VA.getValVT();
778 // Special handling for extended integers.
779 if (SrcVT != DstVT) {
780 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
781 return false;
782
783 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
784 return false;
785
786 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
787
788 if (SrcVT == MVT::i1) {
789 if (Outs[0].Flags.isSExt())
790 return false;
791 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
792 SrcVT = MVT::i8;
793 }
794 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
795 ISD::SIGN_EXTEND;
796 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
797 SrcReg, /*TODO: Kill=*/false);
798 }
799
800 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000801 unsigned DstReg = VA.getLocReg();
802 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000803 // Avoid a cross-class copy. This is very unlikely.
804 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000805 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000806 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
807 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000808
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000809 // Add register to return instruction.
810 RetRegs.push_back(VA.getLocReg());
Dan Gohman84023e02010-07-10 09:00:22 +0000811 }
812
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000813 // The x86-64 ABI for returning structs by value requires that we copy
814 // the sret argument into %rax for the return. We saved the argument into
815 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000816 // and into %rax. We also do the same with %eax for Win32.
817 if (F.hasStructRetAttr() &&
818 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000819 unsigned Reg = X86MFInfo->getSRetReturnReg();
820 assert(Reg &&
821 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000822 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000823 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000824 RetReg).addReg(Reg);
825 RetRegs.push_back(RetReg);
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000826 }
827
Dan Gohman84023e02010-07-10 09:00:22 +0000828 // Now emit the RET.
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000829 MachineInstrBuilder MIB =
830 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
831 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
832 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohman84023e02010-07-10 09:00:22 +0000833 return true;
834}
835
Evan Cheng8b19e562008-09-03 06:44:39 +0000836/// X86SelectLoad - Select and emit code to implement load instructions.
837///
Dan Gohman46510a72010-04-15 01:51:59 +0000838bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000839 // Atomic loads need special handling.
840 if (cast<LoadInst>(I)->isAtomic())
841 return false;
842
Duncan Sands1440e8b2010-11-03 11:35:31 +0000843 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000844 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000845 return false;
846
Dan Gohman0586d912008-09-10 20:11:02 +0000847 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000848 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000849 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000850
Evan Cheng0de588f2008-09-05 21:00:03 +0000851 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000852 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000853 UpdateValueMap(I, ResultReg);
854 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000855 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000856 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000857}
858
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000859static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000860 bool HasAVX = Subtarget->hasAVX();
Craig Topper1accb7e2012-01-10 06:54:16 +0000861 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
862 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000863
Owen Anderson825b72b2009-08-11 20:47:22 +0000864 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000865 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000866 case MVT::i8: return X86::CMP8rr;
867 case MVT::i16: return X86::CMP16rr;
868 case MVT::i32: return X86::CMP32rr;
869 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000870 case MVT::f32:
871 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
872 case MVT::f64:
873 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000874 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000875}
876
Chris Lattner0e13c782008-10-15 04:13:29 +0000877/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
878/// of the comparison, return an opcode that works for the compare (e.g.
879/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000880static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000881 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000882 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000883 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000884 case MVT::i8: return X86::CMP8ri;
885 case MVT::i16: return X86::CMP16ri;
886 case MVT::i32: return X86::CMP32ri;
887 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000888 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
889 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000890 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000891 return X86::CMP64ri32;
892 return 0;
893 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000894}
895
Dan Gohman46510a72010-04-15 01:51:59 +0000896bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
897 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000898 unsigned Op0Reg = getRegForValue(Op0);
899 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000900
Chris Lattnerd53886b2008-10-15 05:18:04 +0000901 // Handle 'null' like i32/i64 0.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000902 if (isa<ConstantPointerNull>(Op1))
903 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000904
Chris Lattner9a08a612008-10-15 04:26:38 +0000905 // We have two options: compare with register or immediate. If the RHS of
906 // the compare is an immediate that we can fold into this compare, use
907 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000908 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000909 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000910 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
911 .addReg(Op0Reg)
912 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000913 return true;
914 }
915 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000916
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000917 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000918 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000919
Chris Lattner9a08a612008-10-15 04:26:38 +0000920 unsigned Op1Reg = getRegForValue(Op1);
921 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000922 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
923 .addReg(Op0Reg)
924 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000925
Chris Lattner9a08a612008-10-15 04:26:38 +0000926 return true;
927}
928
Dan Gohman46510a72010-04-15 01:51:59 +0000929bool X86FastISel::X86SelectCmp(const Instruction *I) {
930 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000931
Duncan Sands1440e8b2010-11-03 11:35:31 +0000932 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000933 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000934 return false;
935
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000936 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000937 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000938 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000939 switch (CI->getPredicate()) {
940 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000941 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
942 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000943
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000944 unsigned EReg = createResultReg(&X86::GR8RegClass);
945 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000946 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
947 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
948 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000949 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000950 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000951 UpdateValueMap(I, ResultReg);
952 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000953 }
954 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000955 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
956 return false;
957
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000958 unsigned NEReg = createResultReg(&X86::GR8RegClass);
959 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000960 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
962 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000963 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000964 UpdateValueMap(I, ResultReg);
965 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000966 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000967 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
968 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
969 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
970 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
971 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
972 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
973 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
974 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
975 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
976 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
977 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
978 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000979
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000980 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
981 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
982 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
983 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
984 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
985 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
986 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
987 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
988 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
989 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000990 default:
991 return false;
992 }
993
Dan Gohman46510a72010-04-15 01:51:59 +0000994 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000995 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000996 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000997
Chris Lattner9a08a612008-10-15 04:26:38 +0000998 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000999 if (!X86FastEmitCompare(Op0, Op1, VT))
1000 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001001
Dan Gohman84023e02010-07-10 09:00:22 +00001002 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001003 UpdateValueMap(I, ResultReg);
1004 return true;
1005}
Evan Cheng8b19e562008-09-03 06:44:39 +00001006
Dan Gohman46510a72010-04-15 01:51:59 +00001007bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedman76927d732011-05-25 23:49:02 +00001008 EVT DstVT = TLI.getValueType(I->getType());
1009 if (!TLI.isTypeLegal(DstVT))
1010 return false;
1011
1012 unsigned ResultReg = getRegForValue(I->getOperand(0));
1013 if (ResultReg == 0)
1014 return false;
1015
Tim Northoverda0416b2013-05-30 10:43:18 +00001016 // Handle zero-extension from i1 to i8, which is common.
1017 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()).getSimpleVT();
1018 if (SrcVT.SimpleTy == MVT::i1) {
1019 // Set the high bits to zero.
1020 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1021 SrcVT = MVT::i8;
Eli Friedman76927d732011-05-25 23:49:02 +00001022
Tim Northoverda0416b2013-05-30 10:43:18 +00001023 if (ResultReg == 0)
1024 return false;
1025 }
1026
1027 if (DstVT == MVT::i64) {
1028 // Handle extension to 64-bits via sub-register shenanigans.
1029 unsigned MovInst;
1030
1031 switch (SrcVT.SimpleTy) {
1032 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1033 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1034 case MVT::i32: MovInst = X86::MOV32rr; break;
1035 default: llvm_unreachable("Unexpected zext to i64 source type");
1036 }
1037
1038 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1039 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovInst), Result32)
1040 .addReg(ResultReg);
1041
1042 ResultReg = createResultReg(&X86::GR64RegClass);
1043 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::SUBREG_TO_REG),
1044 ResultReg)
1045 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1046 } else if (DstVT != MVT::i8) {
Eli Friedman76927d732011-05-25 23:49:02 +00001047 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1048 ResultReg, /*Kill=*/true);
1049 if (ResultReg == 0)
1050 return false;
Dan Gohmand89ae992008-09-05 01:06:14 +00001051 }
1052
Eli Friedman76927d732011-05-25 23:49:02 +00001053 UpdateValueMap(I, ResultReg);
1054 return true;
Dan Gohmand89ae992008-09-05 01:06:14 +00001055}
1056
Chris Lattner9a08a612008-10-15 04:26:38 +00001057
Dan Gohman46510a72010-04-15 01:51:59 +00001058bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +00001059 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +00001060 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001061 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +00001062 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1063 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +00001064
Dan Gohman8bef7442010-08-21 02:32:36 +00001065 // Fold the common case of a conditional branch with a comparison
1066 // in the same block (values defined on other blocks may not have
1067 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +00001068 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +00001069 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001070 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +00001071
Dan Gohmand98d6202008-10-02 22:15:21 +00001072 // Try to take advantage of fallthrough opportunities.
1073 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +00001074 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +00001075 std::swap(TrueMBB, FalseMBB);
1076 Predicate = CmpInst::getInversePredicate(Predicate);
1077 }
1078
Chris Lattner871d2462008-10-15 03:58:05 +00001079 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1080 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1081
Dan Gohmand98d6202008-10-02 22:15:21 +00001082 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +00001083 case CmpInst::FCMP_OEQ:
1084 std::swap(TrueMBB, FalseMBB);
1085 Predicate = CmpInst::FCMP_UNE;
1086 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001087 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1088 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1089 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1090 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1091 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1092 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1093 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1094 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1095 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1096 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1097 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1098 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1099 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001100
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001101 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1102 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1103 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1104 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1105 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1106 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1107 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1108 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1109 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1110 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001111 default:
1112 return false;
1113 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001114
Dan Gohman46510a72010-04-15 01:51:59 +00001115 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001116 if (SwapArgs)
1117 std::swap(Op0, Op1);
1118
Chris Lattner9a08a612008-10-15 04:26:38 +00001119 // Emit a compare of the LHS and RHS, setting the flags.
1120 if (!X86FastEmitCompare(Op0, Op1, VT))
1121 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001122
Dan Gohman84023e02010-07-10 09:00:22 +00001123 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1124 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001125
1126 if (Predicate == CmpInst::FCMP_UNE) {
1127 // X86 requires a second branch to handle UNE (and OEQ,
1128 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001129 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1130 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001131 }
1132
Stuart Hastings3bf91252010-06-17 22:43:56 +00001133 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001134 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001135 return true;
1136 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001137 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1138 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1139 // typically happen for _Bool and C++ bools.
1140 MVT SourceVT;
1141 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1142 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1143 unsigned TestOpc = 0;
1144 switch (SourceVT.SimpleTy) {
1145 default: break;
1146 case MVT::i8: TestOpc = X86::TEST8ri; break;
1147 case MVT::i16: TestOpc = X86::TEST16ri; break;
1148 case MVT::i32: TestOpc = X86::TEST32ri; break;
1149 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1150 }
1151 if (TestOpc) {
1152 unsigned OpReg = getRegForValue(TI->getOperand(0));
1153 if (OpReg == 0) return false;
1154 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1155 .addReg(OpReg).addImm(1);
Eric Christopher471e4222011-06-08 23:55:35 +00001156
Chris Lattnerc76d1212011-04-19 04:26:32 +00001157 unsigned JmpOpc = X86::JNE_4;
1158 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1159 std::swap(TrueMBB, FalseMBB);
1160 JmpOpc = X86::JE_4;
1161 }
Eric Christopher471e4222011-06-08 23:55:35 +00001162
Chris Lattnerc76d1212011-04-19 04:26:32 +00001163 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001164 .addMBB(TrueMBB);
1165 FastEmitBranch(FalseMBB, DL);
1166 FuncInfo.MBB->addSuccessor(TrueMBB);
1167 return true;
1168 }
1169 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001170 }
1171
1172 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001173 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1174 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001175 unsigned OpReg = getRegForValue(BI->getCondition());
1176 if (OpReg == 0) return false;
1177
Eli Friedman547eb4f2011-04-27 01:34:27 +00001178 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1179 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001180 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1181 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001182 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001183 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001184 return true;
1185}
1186
Dan Gohman46510a72010-04-15 01:51:59 +00001187bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001188 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001189 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001190 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001191 CReg = X86::CL;
1192 RC = &X86::GR8RegClass;
1193 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001194 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1195 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1196 case Instruction::Shl: OpReg = X86::SHL8rCL; 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(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001200 CReg = X86::CX;
1201 RC = &X86::GR16RegClass;
1202 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001203 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1204 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1205 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001206 default: return false;
1207 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001208 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001209 CReg = X86::ECX;
1210 RC = &X86::GR32RegClass;
1211 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001212 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1213 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1214 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001215 default: return false;
1216 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001217 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001218 CReg = X86::RCX;
1219 RC = &X86::GR64RegClass;
1220 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001221 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1222 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1223 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001224 default: return false;
1225 }
1226 } else {
1227 return false;
1228 }
1229
Duncan Sands1440e8b2010-11-03 11:35:31 +00001230 MVT VT;
1231 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001232 return false;
1233
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001234 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1235 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001236
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001237 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1238 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001239 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1240 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001241
1242 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001243 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001244 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001245 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1246 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001247 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001248
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001249 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001250 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1251 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001252 UpdateValueMap(I, ResultReg);
1253 return true;
1254}
1255
Eli Bendersky50125482013-04-17 20:10:13 +00001256bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1257 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1258 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1259 const static bool S = true; // IsSigned
1260 const static bool U = false; // !IsSigned
1261 const static unsigned Copy = TargetOpcode::COPY;
1262 // For the X86 DIV/IDIV instruction, in most cases the dividend
1263 // (numerator) must be in a specific register pair highreg:lowreg,
1264 // producing the quotient in lowreg and the remainder in highreg.
1265 // For most data types, to set up the instruction, the dividend is
1266 // copied into lowreg, and lowreg is sign-extended or zero-extended
1267 // into highreg. The exception is i8, where the dividend is defined
1268 // as a single register rather than a register pair, and we
1269 // therefore directly sign-extend or zero-extend the dividend into
1270 // lowreg, instead of copying, and ignore the highreg.
1271 const static struct DivRemEntry {
1272 // The following portion depends only on the data type.
1273 const TargetRegisterClass *RC;
1274 unsigned LowInReg; // low part of the register pair
1275 unsigned HighInReg; // high part of the register pair
1276 // The following portion depends on both the data type and the operation.
1277 struct DivRemResult {
1278 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1279 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1280 // highreg, or copying a zero into highreg.
1281 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1282 // zero/sign-extending into lowreg for i8.
1283 unsigned DivRemResultReg; // Register containing the desired result.
1284 bool IsOpSigned; // Whether to use signed or unsigned form.
1285 } ResultTable[NumOps];
1286 } OpTable[NumTypes] = {
1287 { &X86::GR8RegClass, X86::AX, 0, {
1288 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1289 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1290 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1291 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1292 }
1293 }, // i8
1294 { &X86::GR16RegClass, X86::AX, X86::DX, {
1295 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1296 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover15983b82013-05-30 13:19:42 +00001297 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1298 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky50125482013-04-17 20:10:13 +00001299 }
1300 }, // i16
1301 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1302 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1303 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1304 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1305 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1306 }
1307 }, // i32
1308 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1309 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1310 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover15983b82013-05-30 13:19:42 +00001311 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1312 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky50125482013-04-17 20:10:13 +00001313 }
1314 }, // i64
1315 };
1316
1317 MVT VT;
1318 if (!isTypeLegal(I->getType(), VT))
1319 return false;
1320
1321 unsigned TypeIndex, OpIndex;
1322 switch (VT.SimpleTy) {
1323 default: return false;
1324 case MVT::i8: TypeIndex = 0; break;
1325 case MVT::i16: TypeIndex = 1; break;
1326 case MVT::i32: TypeIndex = 2; break;
1327 case MVT::i64: TypeIndex = 3;
1328 if (!Subtarget->is64Bit())
1329 return false;
1330 break;
1331 }
1332
1333 switch (I->getOpcode()) {
1334 default: llvm_unreachable("Unexpected div/rem opcode");
1335 case Instruction::SDiv: OpIndex = 0; break;
1336 case Instruction::SRem: OpIndex = 1; break;
1337 case Instruction::UDiv: OpIndex = 2; break;
1338 case Instruction::URem: OpIndex = 3; break;
1339 }
1340
1341 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1342 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1343 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1344 if (Op0Reg == 0)
1345 return false;
1346 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1347 if (Op1Reg == 0)
1348 return false;
1349
1350 // Move op0 into low-order input register.
1351 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1352 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1353 // Zero-extend or sign-extend into high-order input register.
1354 if (OpEntry.OpSignExtend) {
1355 if (OpEntry.IsOpSigned)
1356 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1357 TII.get(OpEntry.OpSignExtend));
Tim Northover15983b82013-05-30 13:19:42 +00001358 else {
1359 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Eli Bendersky50125482013-04-17 20:10:13 +00001360 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Tim Northover15983b82013-05-30 13:19:42 +00001361 TII.get(X86::MOV32r0), Zero32);
1362
1363 // Copy the zero into the appropriate sub/super/identical physical
1364 // register. Unfortunately the operations needed are not uniform enough to
1365 // fit neatly into the table above.
1366 if (VT.SimpleTy == MVT::i16) {
1367 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1368 TII.get(TargetOpcode::COPY), TypeEntry.HighInReg)
1369 .addReg(Zero32, 0, X86::sub_16bit);
1370 } else if (VT.SimpleTy == MVT::i32) {
1371 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1372 TII.get(TargetOpcode::COPY), TypeEntry.HighInReg)
1373 .addReg(Zero32);
1374 } else if (VT.SimpleTy == MVT::i64) {
1375 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1376 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1377 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1378 }
1379 }
Eli Bendersky50125482013-04-17 20:10:13 +00001380 }
1381 // Generate the DIV/IDIV instruction.
1382 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1383 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
1384 // Copy output register into result register.
1385 unsigned ResultReg = createResultReg(TypeEntry.RC);
1386 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1387 TII.get(Copy), ResultReg).addReg(OpEntry.DivRemResultReg);
1388 UpdateValueMap(I, ResultReg);
1389
1390 return true;
1391}
1392
Dan Gohman46510a72010-04-15 01:51:59 +00001393bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001394 MVT VT;
1395 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001396 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001397
Eric Christophere487b012010-09-29 23:00:29 +00001398 // We only use cmov here, if we don't have a cmov instruction bail.
1399 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001400
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001401 unsigned Opc = 0;
1402 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001403 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001404 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001405 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001406 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001407 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001408 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001409 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001410 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001411 RC = &X86::GR64RegClass;
1412 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001413 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001414 }
1415
1416 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1417 if (Op0Reg == 0) return false;
1418 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1419 if (Op1Reg == 0) return false;
1420 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1421 if (Op2Reg == 0) return false;
1422
Dan Gohman84023e02010-07-10 09:00:22 +00001423 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1424 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001425 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001426 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1427 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001428 UpdateValueMap(I, ResultReg);
1429 return true;
1430}
1431
Dan Gohman46510a72010-04-15 01:51:59 +00001432bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001433 // fpext from float to double.
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001434 if (X86ScalarSSEf64 &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001435 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001436 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001437 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001438 unsigned OpReg = getRegForValue(V);
1439 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001440 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001441 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1442 TII.get(X86::CVTSS2SDrr), ResultReg)
1443 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001444 UpdateValueMap(I, ResultReg);
1445 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001446 }
1447 }
1448
1449 return false;
1450}
1451
Dan Gohman46510a72010-04-15 01:51:59 +00001452bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001453 if (X86ScalarSSEf64) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001454 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001455 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001456 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001457 unsigned OpReg = getRegForValue(V);
1458 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001459 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001460 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1461 TII.get(X86::CVTSD2SSrr), ResultReg)
1462 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001463 UpdateValueMap(I, ResultReg);
1464 return true;
1465 }
1466 }
1467 }
1468
1469 return false;
1470}
1471
Dan Gohman46510a72010-04-15 01:51:59 +00001472bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001473 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1474 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001475
Eli Friedman76927d732011-05-25 23:49:02 +00001476 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001477 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001478 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001479 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001480 return false;
1481
1482 unsigned InputReg = getRegForValue(I->getOperand(0));
1483 if (!InputReg)
1484 // Unhandled operand. Halt "fast" selection and bail.
1485 return false;
1486
Eli Friedman76927d732011-05-25 23:49:02 +00001487 if (SrcVT == MVT::i8) {
1488 // Truncate from i8 to i1; no code needed.
1489 UpdateValueMap(I, InputReg);
1490 return true;
1491 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001492
Eli Friedman76927d732011-05-25 23:49:02 +00001493 if (!Subtarget->is64Bit()) {
1494 // If we're on x86-32; we can't extract an i8 from a general register.
1495 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperc9099502012-04-20 06:31:50 +00001496 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1497 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1498 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedman76927d732011-05-25 23:49:02 +00001499 unsigned CopyReg = createResultReg(CopyRC);
1500 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1501 CopyReg).addReg(InputReg);
1502 InputReg = CopyReg;
1503 }
1504
1505 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001506 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001507 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001508 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001509 if (!ResultReg)
1510 return false;
1511
1512 UpdateValueMap(I, ResultReg);
1513 return true;
1514}
1515
Eli Friedmanc0883452011-05-20 22:21:04 +00001516bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1517 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1518}
1519
Eli Friedmand5089a92011-04-27 01:45:07 +00001520bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1521 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001522
Eli Friedmand5089a92011-04-27 01:45:07 +00001523 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001524 if (!IsMemcpySmall(Len))
1525 return false;
1526
1527 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001528
1529 // We don't care about alignment here since we just emit integer accesses.
1530 while (Len) {
1531 MVT VT;
1532 if (Len >= 8 && i64Legal)
1533 VT = MVT::i64;
1534 else if (Len >= 4)
1535 VT = MVT::i32;
1536 else if (Len >= 2)
1537 VT = MVT::i16;
1538 else {
Eli Friedmand5089a92011-04-27 01:45:07 +00001539 VT = MVT::i8;
1540 }
1541
1542 unsigned Reg;
1543 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1544 RV &= X86FastEmitStore(VT, Reg, DestAM);
1545 assert(RV && "Failed to emit load or store??");
1546
1547 unsigned Size = VT.getSizeInBits()/8;
1548 Len -= Size;
1549 DestAM.Disp += Size;
1550 SrcAM.Disp += Size;
1551 }
1552
1553 return true;
1554}
1555
Dan Gohman46510a72010-04-15 01:51:59 +00001556bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001557 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001558 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001559 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001560 case Intrinsic::memcpy: {
1561 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1562 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001563 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001564 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001565
Eli Friedman25255cb2011-06-10 23:39:36 +00001566 if (isa<ConstantInt>(MCI.getLength())) {
1567 // Small memcpy's are common enough that we want to do them
1568 // without a call if possible.
1569 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1570 if (IsMemcpySmall(Len)) {
1571 X86AddressMode DestAM, SrcAM;
1572 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1573 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1574 return false;
1575 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1576 return true;
1577 }
1578 }
Eric Christopher471e4222011-06-08 23:55:35 +00001579
Eli Friedman25255cb2011-06-10 23:39:36 +00001580 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1581 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001582 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001583
Eli Friedman25255cb2011-06-10 23:39:36 +00001584 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1585 return false;
1586
1587 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001588 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001589 case Intrinsic::memset: {
1590 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001591
Nick Lewycky3207c9a2011-08-02 00:40:16 +00001592 if (MSI.isVolatile())
1593 return false;
1594
Eli Friedman25255cb2011-06-10 23:39:36 +00001595 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1596 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1597 return false;
1598
1599 if (MSI.getDestAddressSpace() > 255)
1600 return false;
1601
1602 return DoSelectCall(&I, "memset");
1603 }
Eric Christopher07754c22010-03-18 20:27:26 +00001604 case Intrinsic::stackprotector: {
Chad Rosiere1093e52012-05-11 19:43:29 +00001605 // Emit code to store the stack guard onto the stack.
Eric Christopher07754c22010-03-18 20:27:26 +00001606 EVT PtrTy = TLI.getPointerTy();
1607
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001608 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1609 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001610
1611 // Grab the frame index.
1612 X86AddressMode AM;
1613 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001614 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001615 return true;
1616 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001617 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001618 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001619 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001620 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001621 if (!X86SelectAddress(DI->getAddress(), AM))
1622 return false;
Evan Chenge837dea2011-06-28 19:10:37 +00001623 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001624 // FIXME may need to add RegState::Debug to any registers produced,
1625 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001626 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1627 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001628 return true;
1629 }
Eric Christopher77f79892010-01-18 22:11:29 +00001630 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001631 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001632 return true;
1633 }
Bill Wendling52370a12008-12-09 02:42:50 +00001634 case Intrinsic::sadd_with_overflow:
1635 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001636 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001637
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001638 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001639 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001640 const Function *Callee = I.getCalledFunction();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001641 Type *RetTy =
Bill Wendling52370a12008-12-09 02:42:50 +00001642 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1643
Duncan Sands1440e8b2010-11-03 11:35:31 +00001644 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001645 if (!isTypeLegal(RetTy, VT))
1646 return false;
1647
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001648 const Value *Op1 = I.getArgOperand(0);
1649 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001650 unsigned Reg1 = getRegForValue(Op1);
1651 unsigned Reg2 = getRegForValue(Op2);
1652
1653 if (Reg1 == 0 || Reg2 == 0)
1654 // FIXME: Handle values *not* in registers.
1655 return false;
1656
1657 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001658 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001659 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001660 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001661 OpC = X86::ADD64rr;
1662 else
1663 return false;
1664
Eli Friedman482feb32011-05-16 21:06:17 +00001665 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001666 // both the returned values.
Eli Friedman482feb32011-05-16 21:06:17 +00001667 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001668 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1669 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001670
Chris Lattnera9a42252009-04-12 07:36:01 +00001671 unsigned Opc = X86::SETBr;
1672 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1673 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001674 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1675
1676 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001677 return true;
1678 }
1679 }
1680}
1681
Chad Rosierfd3417d2013-02-25 21:59:35 +00001682bool X86FastISel::FastLowerArguments() {
1683 if (!FuncInfo.CanLowerReturn)
1684 return false;
1685
Chad Rosier146b8c22013-04-02 16:31:41 +00001686 if (Subtarget->isTargetWin64())
Chad Rosierd9b306a2013-03-14 21:25:04 +00001687 return false;
1688
Chad Rosierfd3417d2013-02-25 21:59:35 +00001689 const Function *F = FuncInfo.Fn;
1690 if (F->isVarArg())
1691 return false;
1692
1693 CallingConv::ID CC = F->getCallingConv();
1694 if (CC != CallingConv::C)
1695 return false;
1696
1697 if (!Subtarget->is64Bit())
1698 return false;
1699
1700 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
1701 unsigned Idx = 1;
1702 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1703 I != E; ++I, ++Idx) {
1704 if (Idx > 6)
1705 return false;
1706
1707 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1708 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1709 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1710 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1711 return false;
1712
1713 Type *ArgTy = I->getType();
1714 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1715 return false;
1716
1717 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosierfe88aa02013-02-26 01:05:31 +00001718 if (!ArgVT.isSimple()) return false;
Chad Rosierfd3417d2013-02-25 21:59:35 +00001719 switch (ArgVT.getSimpleVT().SimpleTy) {
1720 case MVT::i32:
1721 case MVT::i64:
1722 break;
1723 default:
1724 return false;
1725 }
1726 }
1727
1728 static const uint16_t GPR32ArgRegs[] = {
1729 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1730 };
1731 static const uint16_t GPR64ArgRegs[] = {
1732 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
1733 };
1734
1735 Idx = 0;
1736 const TargetRegisterClass *RC32 = TLI.getRegClassFor(MVT::i32);
1737 const TargetRegisterClass *RC64 = TLI.getRegClassFor(MVT::i64);
1738 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1739 I != E; ++I, ++Idx) {
1740 if (I->use_empty())
1741 continue;
1742 bool is32Bit = TLI.getValueType(I->getType()) == MVT::i32;
1743 const TargetRegisterClass *RC = is32Bit ? RC32 : RC64;
1744 unsigned SrcReg = is32Bit ? GPR32ArgRegs[Idx] : GPR64ArgRegs[Idx];
1745 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1746 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1747 // Without this, EmitLiveInCopies may eliminate the livein if its only
1748 // use is a bitcast (which isn't turned into an instruction).
1749 unsigned ResultReg = createResultReg(RC);
1750 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1751 ResultReg).addReg(DstReg, getKillRegState(true));
1752 UpdateValueMap(I, ResultReg);
1753 }
1754 return true;
1755}
1756
Dan Gohman46510a72010-04-15 01:51:59 +00001757bool X86FastISel::X86SelectCall(const Instruction *I) {
1758 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001759 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001760
1761 // Can't handle inline asm yet.
1762 if (isa<InlineAsm>(Callee))
1763 return false;
1764
Bill Wendling52370a12008-12-09 02:42:50 +00001765 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001766 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001767 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001768
Chad Rosier425e9512012-12-11 00:18:02 +00001769 // Allow SelectionDAG isel to handle tail calls.
1770 if (cast<CallInst>(I)->isTailCall())
1771 return false;
1772
Eli Friedman25255cb2011-06-10 23:39:36 +00001773 return DoSelectCall(I, 0);
1774}
1775
Rafael Espindolac338fe02012-07-25 15:42:45 +00001776static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1777 const ImmutableCallSite &CS) {
Rafael Espindola742f2c92012-07-25 13:35:45 +00001778 if (Subtarget.is64Bit())
1779 return 0;
1780 if (Subtarget.isTargetWindows())
1781 return 0;
1782 CallingConv::ID CC = CS.getCallingConv();
1783 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1784 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001785 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola742f2c92012-07-25 13:35:45 +00001786 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001787 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola1cee7102012-07-25 13:41:10 +00001788 return 0;
Rafael Espindola742f2c92012-07-25 13:35:45 +00001789 return 4;
1790}
1791
Eli Friedman25255cb2011-06-10 23:39:36 +00001792// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1793bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1794 const CallInst *CI = cast<CallInst>(I);
1795 const Value *Callee = CI->getCalledValue();
1796
Evan Chengf3d4efe2008-09-07 09:09:33 +00001797 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001798 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001799 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001800 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001801 CC != CallingConv::X86_FastCall)
1802 return false;
1803
Evan Cheng381993f2010-01-27 00:00:57 +00001804 // fastcc with -tailcallopt is intended to provide a guaranteed
1805 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001806 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001807 return false;
1808
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001809 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1810 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001811 bool isVarArg = FTy->isVarArg();
1812
1813 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1814 // x86-32. Special handling for x86-64 is implemented.
1815 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001816 return false;
1817
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001818 // Fast-isel doesn't know about callee-pop yet.
Evan Chengef41ff62011-06-23 17:54:54 +00001819 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001820 TM.Options.GuaranteedTailCallOpt))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001821 return false;
1822
Eli Friedman19515b42011-05-17 18:29:03 +00001823 // Check whether the function can return without sret-demotion.
1824 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001825 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman19515b42011-05-17 18:29:03 +00001826 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendling56cb2292012-07-19 00:11:40 +00001827 *FuncInfo.MF, FTy->isVarArg(),
1828 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001829 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001830 return false;
1831
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001832 // Materialize callee address in a register. FIXME: GV address can be
1833 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001834 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001835 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001836 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001837 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001838 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001839 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001840 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001841 } else if (CalleeAM.Base.Reg != 0) {
1842 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001843 } else
1844 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001845
Evan Chengf3d4efe2008-09-07 09:09:33 +00001846 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001847 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001848 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001849 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001850 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosier15b44972012-02-15 00:36:26 +00001851 unsigned arg_size = CS.arg_size();
1852 Args.reserve(arg_size);
1853 ArgVals.reserve(arg_size);
1854 ArgVTs.reserve(arg_size);
1855 ArgFlags.reserve(arg_size);
Dan Gohman46510a72010-04-15 01:51:59 +00001856 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001857 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001858 // If we're lowering a mem intrinsic instead of a regular call, skip the
1859 // last two arguments, which should not passed to the underlying functions.
1860 if (MemIntName && e-i <= 2)
1861 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001862 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001863 ISD::ArgFlagsTy Flags;
1864 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling034b94b2012-12-19 07:18:57 +00001865 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001866 Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00001867 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001868 Flags.setZExt();
1869
Bill Wendling034b94b2012-12-19 07:18:57 +00001870 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001871 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1872 Type *ElementTy = Ty->getElementType();
Eli Friedmanc0883452011-05-20 22:21:04 +00001873 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1874 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1875 if (!FrameAlign)
1876 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1877 Flags.setByVal();
1878 Flags.setByValSize(FrameSize);
1879 Flags.setByValAlign(FrameAlign);
1880 if (!IsMemcpySmall(FrameSize))
1881 return false;
1882 }
1883
Bill Wendling034b94b2012-12-19 07:18:57 +00001884 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedmanc0883452011-05-20 22:21:04 +00001885 Flags.setInReg();
Bill Wendling034b94b2012-12-19 07:18:57 +00001886 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedmanc0883452011-05-20 22:21:04 +00001887 Flags.setNest();
1888
Chris Lattnere03b8d32011-04-19 04:42:38 +00001889 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1890 // instruction. This is safe because it is common to all fastisel supported
1891 // calling conventions on x86.
1892 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1893 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1894 CI->getBitWidth() == 16) {
1895 if (Flags.isSExt())
1896 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1897 else
1898 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1899 }
1900 }
Eric Christopher471e4222011-06-08 23:55:35 +00001901
Chris Lattnerb44101c2011-04-19 05:09:50 +00001902 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001903
Chris Lattnerff009ad2011-04-19 05:15:59 +00001904 // Passing bools around ends up doing a trunc to i1 and passing it.
1905 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001906 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1907 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1908 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001909 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1910 ArgReg = getRegForValue(ArgVal);
1911 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001912
Chris Lattnerb44101c2011-04-19 05:09:50 +00001913 MVT ArgVT;
1914 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001915
Chris Lattnerb44101c2011-04-19 05:09:50 +00001916 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1917 ArgVal->hasOneUse(), 1);
1918 } else {
1919 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001920 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001921
Chris Lattnerff009ad2011-04-19 05:15:59 +00001922 if (ArgReg == 0) return false;
1923
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001924 Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001925 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001926 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001927 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001928 if (ArgVT == MVT::x86mmx)
1929 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001930 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1931 Flags.setOrigAlign(OriginalAlignment);
1932
Chris Lattnerb44101c2011-04-19 05:09:50 +00001933 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001934 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001935 ArgVTs.push_back(ArgVT);
1936 ArgFlags.push_back(Flags);
1937 }
1938
1939 // Analyze operands of the call, assigning locations to each operand.
1940 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001941 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00001942 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001943
Dan Gohmand8acddd2010-06-01 21:09:47 +00001944 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001945 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001946 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001947
Duncan Sands45907662010-10-31 13:21:44 +00001948 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001949
1950 // Get a count of how many bytes are to be pushed on the stack.
1951 unsigned NumBytes = CCInfo.getNextStackOffset();
1952
1953 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001954 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001955 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1956 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001957
Chris Lattner438949a2008-10-15 05:30:52 +00001958 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001959 // copies / loads.
1960 SmallVector<unsigned, 4> RegArgs;
1961 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1962 CCValAssign &VA = ArgLocs[i];
1963 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001964 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001965
Evan Chengf3d4efe2008-09-07 09:09:33 +00001966 // Promote the value if needed.
1967 switch (VA.getLocInfo()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001968 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001969 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001970 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1971 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001972 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1973 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001974 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001975 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001976 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001977 }
1978 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001979 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1980 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001981 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1982 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001983 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001984 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001985 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001986 }
1987 case CCValAssign::AExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001988 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1989 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001990 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1991 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001992 if (!Emitted)
1993 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001994 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001995 if (!Emitted)
1996 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1997 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001998
Chris Lattnerc46ec642011-01-05 22:26:52 +00001999 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00002000 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00002001 break;
2002 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00002003 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002004 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002005 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00002006 assert(BC != 0 && "Failed to emit a bitcast!");
2007 Arg = BC;
2008 ArgVT = VA.getLocVT();
2009 break;
2010 }
Chad Rosier36ec0ca2012-07-11 19:58:38 +00002011 case CCValAssign::VExt:
2012 // VExt has not been implemented, so this should be impossible to reach
2013 // for now. However, fallback to Selection DAG isel once implemented.
2014 return false;
2015 case CCValAssign::Indirect:
2016 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2017 // support this.
2018 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00002019 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002020
Evan Chengf3d4efe2008-09-07 09:09:33 +00002021 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00002022 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2023 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002024 RegArgs.push_back(VA.getLocReg());
2025 } else {
2026 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00002027 X86AddressMode AM;
Michael Liaof0e06e82012-11-01 03:47:50 +00002028 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman0586d912008-09-10 20:11:02 +00002029 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00002030 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00002031 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002032
Eli Friedmanc0883452011-05-20 22:21:04 +00002033 if (Flags.isByVal()) {
2034 X86AddressMode SrcAM;
2035 SrcAM.Base.Reg = Arg;
2036 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2037 assert(Res && "memcpy length already checked!"); (void)Res;
2038 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2039 // If this is a really simple value, emit this with the Value* version
Nick Lewycky1f9c6862011-10-12 00:14:12 +00002040 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedmanc0883452011-05-20 22:21:04 +00002041 // as it can cause us to reevaluate the argument.
Lang Hamese4824712011-10-18 22:11:33 +00002042 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2043 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00002044 } else {
Lang Hamese4824712011-10-18 22:11:33 +00002045 if (!X86FastEmitStore(ArgVT, Arg, AM))
2046 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00002047 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00002048 }
2049 }
2050
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002051 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002052 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00002053 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00002054 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00002055 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2056 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002057 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002058
Eli Friedman37620462011-04-19 17:22:22 +00002059 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
2060 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002061 static const uint16_t XMMArgRegs[] = {
Eli Friedman37620462011-04-19 17:22:22 +00002062 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2063 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2064 };
2065 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
2066 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
2067 X86::AL).addImm(NumXMMRegs);
2068 }
2069
Evan Chengf3d4efe2008-09-07 09:09:33 +00002070 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00002071 MachineInstrBuilder MIB;
2072 if (CalleeOp) {
2073 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00002074 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00002075 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00002076 CallOpc = X86::CALL64r;
2077 else
2078 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00002079 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
2080 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002081
Chris Lattner51e8eab2009-07-09 06:34:26 +00002082 } else {
2083 // Direct call.
2084 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00002085 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00002086 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00002087 CallOpc = X86::CALL64pcrel32;
2088 else
2089 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002090
Chris Lattner51e8eab2009-07-09 06:34:26 +00002091 // See if we need any target-specific flags on the GV operand.
2092 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002093
Chris Lattner51e8eab2009-07-09 06:34:26 +00002094 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2095 // external symbols most go through the PLT in PIC mode. If the symbol
2096 // has hidden or protected visibility, or if it is static or local, then
2097 // we don't need to use the PLT - we can directly call it.
2098 if (Subtarget->isTargetELF() &&
2099 TM.getRelocationModel() == Reloc::PIC_ &&
2100 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2101 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002102 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00002103 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002104 (!Subtarget->getTargetTriple().isMacOSX() ||
2105 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00002106 // PC-relative references to external symbols should go through $stub,
2107 // unless we're building with the leopard linker or later, which
2108 // automatically synthesizes these stubs.
2109 OpFlags = X86II::MO_DARWIN_STUB;
2110 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002111
2112
Eli Friedman25255cb2011-06-10 23:39:36 +00002113 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
2114 if (MemIntName)
Eli Friedman8a37aba2011-06-11 01:55:07 +00002115 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedman25255cb2011-06-10 23:39:36 +00002116 else
2117 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00002118 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002119
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002120 // Add a register mask with the call-preserved registers.
2121 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2122 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2123
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +00002124 // Add an implicit use GOT pointer in EBX.
2125 if (Subtarget->isPICStyleGOT())
2126 MIB.addReg(X86::EBX, RegState::Implicit);
2127
2128 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
2129 MIB.addReg(X86::AL, RegState::Implicit);
2130
2131 // Add implicit physical register uses to the call.
2132 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2133 MIB.addReg(RegArgs[i], RegState::Implicit);
2134
Evan Chengf3d4efe2008-09-07 09:09:33 +00002135 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00002136 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolac338fe02012-07-25 15:42:45 +00002137 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohman84023e02010-07-10 09:00:22 +00002138 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00002139 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002140
Eli Friedman19515b42011-05-17 18:29:03 +00002141 // Build info for return calling conv lowering code.
2142 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2143 SmallVector<ISD::InputArg, 32> Ins;
2144 SmallVector<EVT, 4> RetTys;
2145 ComputeValueVTs(TLI, I->getType(), RetTys);
2146 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2147 EVT VT = RetTys[i];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00002148 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman19515b42011-05-17 18:29:03 +00002149 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2150 for (unsigned j = 0; j != NumRegs; ++j) {
2151 ISD::InputArg MyFlags;
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00002152 MyFlags.VT = RegisterVT;
Eli Friedman19515b42011-05-17 18:29:03 +00002153 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling034b94b2012-12-19 07:18:57 +00002154 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman19515b42011-05-17 18:29:03 +00002155 MyFlags.Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002156 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman19515b42011-05-17 18:29:03 +00002157 MyFlags.Flags.setZExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002158 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman19515b42011-05-17 18:29:03 +00002159 MyFlags.Flags.setInReg();
2160 Ins.push_back(MyFlags);
2161 }
2162 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00002163
Eli Friedman19515b42011-05-17 18:29:03 +00002164 // Now handle call return values.
2165 SmallVector<unsigned, 4> UsedRegs;
2166 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002167 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00002168 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00002169 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2170 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2171 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2172 EVT CopyVT = RVLocs[i].getValVT();
2173 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002174
Evan Chengf3d4efe2008-09-07 09:09:33 +00002175 // If this is a call to a function that returns an fp value on the x87 fp
2176 // stack, but where we prefer to use the value in xmm registers, copy it
2177 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00002178 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002179 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00002180 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002181 CopyVT = MVT::f80;
Craig Topperc9099502012-04-20 06:31:50 +00002182 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00002183 }
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002184 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
2185 CopyReg);
2186 } else {
2187 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2188 CopyReg).addReg(RVLocs[i].getLocReg());
2189 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Chengf3d4efe2008-09-07 09:09:33 +00002190 }
2191
Eli Friedman19515b42011-05-17 18:29:03 +00002192 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00002193 // Round the F80 the right size, which also moves to the appropriate xmm
2194 // register. This is accomplished by storing the F80 value in memory and
2195 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00002196 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00002197 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00002198 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00002199 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00002200 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2201 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00002202 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002203 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00002204 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00002205 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002206 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00002207 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00002208
Eli Friedman19515b42011-05-17 18:29:03 +00002209 if (RVLocs.size())
2210 UpdateValueMap(I, ResultReg, RVLocs.size());
2211
Dan Gohmandb497122010-06-18 23:28:01 +00002212 // Set all unused physreg defs as dead.
2213 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2214
Evan Chengf3d4efe2008-09-07 09:09:33 +00002215 return true;
2216}
2217
2218
Dan Gohman99b21822008-08-28 23:21:34 +00002219bool
Dan Gohman46510a72010-04-15 01:51:59 +00002220X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00002221 switch (I->getOpcode()) {
2222 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00002223 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00002224 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00002225 case Instruction::Store:
2226 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00002227 case Instruction::Ret:
2228 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00002229 case Instruction::ICmp:
2230 case Instruction::FCmp:
2231 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00002232 case Instruction::ZExt:
2233 return X86SelectZExt(I);
2234 case Instruction::Br:
2235 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002236 case Instruction::Call:
2237 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00002238 case Instruction::LShr:
2239 case Instruction::AShr:
2240 case Instruction::Shl:
2241 return X86SelectShift(I);
Eli Bendersky50125482013-04-17 20:10:13 +00002242 case Instruction::SDiv:
2243 case Instruction::UDiv:
2244 case Instruction::SRem:
2245 case Instruction::URem:
2246 return X86SelectDivRem(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00002247 case Instruction::Select:
2248 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00002249 case Instruction::Trunc:
2250 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00002251 case Instruction::FPExt:
2252 return X86SelectFPExt(I);
2253 case Instruction::FPTrunc:
2254 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00002255 case Instruction::IntToPtr: // Deliberate fall-through.
2256 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00002257 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2258 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00002259 if (DstVT.bitsGT(SrcVT))
2260 return X86SelectZExt(I);
2261 if (DstVT.bitsLT(SrcVT))
2262 return X86SelectTrunc(I);
2263 unsigned Reg = getRegForValue(I->getOperand(0));
2264 if (Reg == 0) return false;
2265 UpdateValueMap(I, Reg);
2266 return true;
2267 }
Dan Gohman99b21822008-08-28 23:21:34 +00002268 }
2269
2270 return false;
2271}
2272
Dan Gohman46510a72010-04-15 01:51:59 +00002273unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002274 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00002275 if (!isTypeLegal(C->getType(), VT))
Michael Liaofaa11592012-08-30 00:30:16 +00002276 return 0;
2277
2278 // Can't handle alternate code models yet.
2279 if (TM.getCodeModel() != CodeModel::Small)
2280 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002281
Owen Anderson95267a12008-09-05 00:06:23 +00002282 // Get opcode and regclass of the output for the given load instruction.
2283 unsigned Opc = 0;
2284 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002285 switch (VT.SimpleTy) {
Michael Liaofaa11592012-08-30 00:30:16 +00002286 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00002287 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00002288 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +00002289 RC = &X86::GR8RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002290 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002291 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00002292 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +00002293 RC = &X86::GR16RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002294 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002295 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00002296 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +00002297 RC = &X86::GR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002298 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002299 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00002300 // Must be in x86-64 mode.
2301 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +00002302 RC = &X86::GR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002303 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002304 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002305 if (X86ScalarSSEf32) {
2306 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +00002307 RC = &X86::FR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002308 } else {
2309 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +00002310 RC = &X86::RFP32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002311 }
2312 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002313 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002314 if (X86ScalarSSEf64) {
2315 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +00002316 RC = &X86::FR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002317 } else {
2318 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +00002319 RC = &X86::RFP64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002320 }
2321 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002322 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00002323 // No f80 support yet.
Michael Liaofaa11592012-08-30 00:30:16 +00002324 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002325 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002326
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002327 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00002328 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002329 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002330 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00002331 // If the expression is just a basereg, then we're done, otherwise we need
2332 // to emit an LEA.
2333 if (AM.BaseType == X86AddressMode::RegBase &&
2334 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2335 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00002336
Chris Lattner685090f2011-04-17 17:12:08 +00002337 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002338 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002339 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2340 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00002341 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002342 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002343 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002344 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002345
Owen Anderson3b217c62008-09-06 01:11:01 +00002346 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002347 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002348 if (Align == 0) {
2349 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002350 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002351 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002352
Dan Gohman5396c992008-09-30 01:21:32 +00002353 // x86-32 PIC requires a PIC base register for constant pools.
2354 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002355 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002356 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002357 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002358 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002359 } else if (Subtarget->isPICStyleGOT()) {
2360 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002361 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002362 } else if (Subtarget->isPICStyleRIPRel() &&
2363 TM.getCodeModel() == CodeModel::Small) {
2364 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002365 }
Dan Gohman5396c992008-09-30 01:21:32 +00002366
2367 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002368 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002369 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002370 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2371 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002372 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002373
Owen Anderson95267a12008-09-05 00:06:23 +00002374 return ResultReg;
2375}
2376
Dan Gohman46510a72010-04-15 01:51:59 +00002377unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002378 // Fail on dynamic allocas. At this point, getRegForValue has already
2379 // checked its CSE maps, so if we're here trying to handle a dynamic
2380 // alloca, we're not going to succeed. X86SelectAddress has a
2381 // check for dynamic allocas, because it's called directly from
2382 // various places, but TargetMaterializeAlloca also needs a check
2383 // in order to avoid recursion between getRegForValue,
2384 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002385 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002386 return 0;
2387
Dan Gohman0586d912008-09-10 20:11:02 +00002388 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002389 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002390 return 0;
2391 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper44d23822012-02-22 05:59:10 +00002392 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman0586d912008-09-10 20:11:02 +00002393 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002394 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2395 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002396 return ResultReg;
2397}
2398
Eli Friedman2790ba82011-04-27 22:41:55 +00002399unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2400 MVT VT;
2401 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002402 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002403
2404 // Get opcode and regclass for the given zero.
2405 unsigned Opc = 0;
2406 const TargetRegisterClass *RC = NULL;
2407 switch (VT.SimpleTy) {
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002408 default: return 0;
Craig Topperf4cfc442012-08-11 17:53:00 +00002409 case MVT::f32:
2410 if (X86ScalarSSEf32) {
2411 Opc = X86::FsFLD0SS;
2412 RC = &X86::FR32RegClass;
2413 } else {
2414 Opc = X86::LD_Fp032;
2415 RC = &X86::RFP32RegClass;
2416 }
2417 break;
2418 case MVT::f64:
2419 if (X86ScalarSSEf64) {
2420 Opc = X86::FsFLD0SD;
2421 RC = &X86::FR64RegClass;
2422 } else {
2423 Opc = X86::LD_Fp064;
2424 RC = &X86::RFP64RegClass;
2425 }
2426 break;
2427 case MVT::f80:
2428 // No f80 support yet.
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002429 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002430 }
2431
2432 unsigned ResultReg = createResultReg(RC);
2433 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2434 return ResultReg;
2435}
2436
2437
Eli Bendersky75299e32013-04-19 22:29:18 +00002438bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2439 const LoadInst *LI) {
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002440 X86AddressMode AM;
2441 if (!X86SelectAddress(LI->getOperand(0), AM))
2442 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002443
Craig Topperdca72542012-08-11 17:46:16 +00002444 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002445
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002446 unsigned Size = TD.getTypeAllocSize(LI->getType());
2447 unsigned Alignment = LI->getAlignment();
2448
2449 SmallVector<MachineOperand, 8> AddrOps;
2450 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002451
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002452 MachineInstr *Result =
2453 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2454 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002455
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002456 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002457 MI->eraseFromParent();
2458 return true;
2459}
2460
2461
Evan Chengc3f44b02008-09-03 00:03:49 +00002462namespace llvm {
Bob Wilsond49edb72012-08-03 04:06:28 +00002463 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2464 const TargetLibraryInfo *libInfo) {
2465 return new X86FastISel(funcInfo, libInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002466 }
Dan Gohman99b21822008-08-28 23:21:34 +00002467}