blob: 669108f2563ce32e4a018c15553cdf931b719180 [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
Wesley Peckbf17cfa2010-11-23 03:31:01 +000048 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Chengf3d4efe2008-09-07 09:09:33 +000049 /// floating point ops.
50 /// When SSE is available, use it for f32 operations.
51 /// When SSE2 is available, use it for f64 operations.
52 bool X86ScalarSSEf64;
53 bool X86ScalarSSEf32;
54
Evan Cheng8b19e562008-09-03 06:44:39 +000055public:
Bob Wilsond49edb72012-08-03 04:06:28 +000056 explicit X86FastISel(FunctionLoweringInfo &funcInfo,
57 const TargetLibraryInfo *libInfo)
58 : FastISel(funcInfo, libInfo) {
Evan Cheng88e30412008-09-03 01:04:47 +000059 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topper1accb7e2012-01-10 06:54:16 +000060 X86ScalarSSEf64 = Subtarget->hasSSE2();
61 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng88e30412008-09-03 01:04:47 +000062 }
Evan Chengc3f44b02008-09-03 00:03:49 +000063
Dan Gohman46510a72010-04-15 01:51:59 +000064 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Chengc3f44b02008-09-03 00:03:49 +000065
Eli Bendersky75299e32013-04-19 22:29:18 +000066 /// \brief The specified machine instr operand is a vreg, and that
Chris Lattnerbeac75d2010-09-05 02:18:34 +000067 /// vreg is being provided by the specified load instruction. If possible,
68 /// try to fold the load as an operand to the instruction, returning true if
69 /// possible.
Eli Bendersky75299e32013-04-19 22:29:18 +000070 virtual bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
71 const LoadInst *LI);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000072
Chad Rosierfd3417d2013-02-25 21:59:35 +000073 virtual bool FastLowerArguments();
74
Dan Gohman1adf1b02008-08-19 21:45:35 +000075#include "X86GenFastISel.inc"
Evan Cheng8b19e562008-09-03 06:44:39 +000076
77private:
Dan Gohman46510a72010-04-15 01:51:59 +000078 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000079
Owen Andersone50ed302009-08-10 22:56:29 +000080 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Cheng0de588f2008-09-05 21:00:03 +000081
Chris Lattnerb44101c2011-04-19 05:09:50 +000082 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM);
83 bool X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM);
Evan Cheng24e3a902008-09-08 06:35:17 +000084
Owen Andersone50ed302009-08-10 22:56:29 +000085 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000086 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000087
Dan Gohman46510a72010-04-15 01:51:59 +000088 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
89 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000090
Dan Gohman46510a72010-04-15 01:51:59 +000091 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000092
Dan Gohman46510a72010-04-15 01:51:59 +000093 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000094
Dan Gohman84023e02010-07-10 09:00:22 +000095 bool X86SelectRet(const Instruction *I);
96
Dan Gohman46510a72010-04-15 01:51:59 +000097 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +000098
Dan Gohman46510a72010-04-15 01:51:59 +000099 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000100
Dan Gohman46510a72010-04-15 01:51:59 +0000101 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000102
Dan Gohman46510a72010-04-15 01:51:59 +0000103 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000104
Eli Bendersky50125482013-04-17 20:10:13 +0000105 bool X86SelectDivRem(const Instruction *I);
106
Dan Gohman46510a72010-04-15 01:51:59 +0000107 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000108
Dan Gohman46510a72010-04-15 01:51:59 +0000109 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000110
Dan Gohman46510a72010-04-15 01:51:59 +0000111 bool X86SelectFPExt(const Instruction *I);
112 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000113
Dan Gohman46510a72010-04-15 01:51:59 +0000114 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
115 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000116
Eli Friedman25255cb2011-06-10 23:39:36 +0000117 bool DoSelectCall(const Instruction *I, const char *MemIntName);
118
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000119 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000120 return getTargetMachine()->getInstrInfo();
121 }
122 const X86TargetMachine *getTargetMachine() const {
123 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000124 }
125
Dan Gohman46510a72010-04-15 01:51:59 +0000126 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000127
Dan Gohman46510a72010-04-15 01:51:59 +0000128 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000129
Eli Friedman2790ba82011-04-27 22:41:55 +0000130 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
131
Evan Chengf3d4efe2008-09-07 09:09:33 +0000132 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
133 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000134 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
136 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000137 }
138
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000139 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000140
Eli Friedmanc0883452011-05-20 22:21:04 +0000141 bool IsMemcpySmall(uint64_t Len);
142
Eli Friedmand5089a92011-04-27 01:45:07 +0000143 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
144 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000145};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000146
Chris Lattner087fcf32009-03-08 18:44:31 +0000147} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000148
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000149bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000150 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
151 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000152 // Unhandled type. Halt "fast" selection and bail.
153 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000154
155 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000156 // For now, require SSE/SSE2 for performing floating-point operations,
157 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000158 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topperf4cfc442012-08-11 17:53:00 +0000159 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topperf4cfc442012-08-11 17:53:00 +0000161 return false;
Dan Gohman9b66d732008-09-30 00:48:39 +0000162 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000163 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000164 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000165 // We only handle legal types. For example, on x86-32 the instruction
166 // selector contains all of the 64-bit instructions from x86-64,
167 // under the assumption that i64 won't be used if the target doesn't
168 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000169 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000170}
171
172#include "X86GenCallingConv.inc"
173
Evan Cheng0de588f2008-09-05 21:00:03 +0000174/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000175/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000176/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000177bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000178 unsigned &ResultReg) {
179 // Get opcode and regclass of the output for the given load instruction.
180 unsigned Opc = 0;
181 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000182 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000183 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000184 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000185 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000186 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +0000187 RC = &X86::GR8RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000188 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000189 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000190 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +0000191 RC = &X86::GR16RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000192 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000193 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000194 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000195 RC = &X86::GR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000196 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000197 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000198 // Must be in x86-64 mode.
199 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000200 RC = &X86::GR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000201 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000202 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000203 if (X86ScalarSSEf32) {
204 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +0000205 RC = &X86::FR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000206 } else {
207 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +0000208 RC = &X86::RFP32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000209 }
210 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000211 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000212 if (X86ScalarSSEf64) {
213 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +0000214 RC = &X86::FR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000215 } else {
216 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +0000217 RC = &X86::RFP64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000218 }
219 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000220 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000221 // No f80 support yet.
222 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000223 }
224
225 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000226 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
227 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000228 return true;
229}
230
Evan Chengf3d4efe2008-09-07 09:09:33 +0000231/// X86FastEmitStore - Emit a machine instruction to store a value Val of
232/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
233/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000234/// i.e. V. Return true if it is possible.
235bool
Chris Lattnerb44101c2011-04-19 05:09:50 +0000236X86FastISel::X86FastEmitStore(EVT VT, unsigned Val, const X86AddressMode &AM) {
Dan Gohman863890e2008-09-08 16:31:35 +0000237 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000238 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000239 switch (VT.getSimpleVT().SimpleTy) {
240 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000241 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000242 case MVT::i1: {
243 // Mask out all but lowest bit.
Craig Topperc9099502012-04-20 06:31:50 +0000244 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000245 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000246 TII.get(X86::AND8ri), AndResult).addReg(Val).addImm(1);
247 Val = AndResult;
248 }
249 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000250 case MVT::i8: Opc = X86::MOV8mr; break;
251 case MVT::i16: Opc = X86::MOV16mr; break;
252 case MVT::i32: Opc = X86::MOV32mr; break;
253 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
254 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000255 Opc = X86ScalarSSEf32 ?
256 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000257 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000258 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000259 Opc = X86ScalarSSEf64 ?
260 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000261 break;
Lang Hamese4824712011-10-18 22:11:33 +0000262 case MVT::v4f32:
263 Opc = X86::MOVAPSmr;
264 break;
265 case MVT::v2f64:
266 Opc = X86::MOVAPDmr;
267 break;
268 case MVT::v4i32:
269 case MVT::v2i64:
270 case MVT::v8i16:
271 case MVT::v16i8:
272 Opc = X86::MOVDQAmr;
273 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000274 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000275
Dan Gohman84023e02010-07-10 09:00:22 +0000276 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
277 DL, TII.get(Opc)), AM).addReg(Val);
Evan Cheng0de588f2008-09-05 21:00:03 +0000278 return true;
279}
280
Dan Gohman46510a72010-04-15 01:51:59 +0000281bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Chris Lattner438949a2008-10-15 05:30:52 +0000282 const X86AddressMode &AM) {
283 // Handle 'null' like i32/i64 0.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000284 if (isa<ConstantPointerNull>(Val))
285 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000286
Chris Lattner438949a2008-10-15 05:30:52 +0000287 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000288 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000289 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000290 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000291 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000292 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000293 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000294 case MVT::i8: Opc = X86::MOV8mi; break;
295 case MVT::i16: Opc = X86::MOV16mi; break;
296 case MVT::i32: Opc = X86::MOV32mi; break;
297 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000298 // Must be a 32-bit sign extended value.
Jakub Staszakeaf77252012-11-15 19:05:23 +0000299 if (isInt<32>(CI->getSExtValue()))
Chris Lattner438949a2008-10-15 05:30:52 +0000300 Opc = X86::MOV64mi32;
301 break;
302 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000303
Chris Lattner438949a2008-10-15 05:30:52 +0000304 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000305 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
306 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000307 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000308 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000309 return true;
310 }
311 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000312
Chris Lattner438949a2008-10-15 05:30:52 +0000313 unsigned ValReg = getRegForValue(Val);
314 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000315 return false;
316
Chris Lattner438949a2008-10-15 05:30:52 +0000317 return X86FastEmitStore(VT, ValReg, AM);
318}
319
Evan Cheng24e3a902008-09-08 06:35:17 +0000320/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
321/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
322/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000323bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
324 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000325 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000326 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
327 Src, /*TODO: Kill=*/false);
Jakub Staszakfe9b5a42013-02-14 21:50:09 +0000328 if (RR == 0)
Owen Andersonac34a002008-09-11 19:44:55 +0000329 return false;
Jakub Staszakfe9b5a42013-02-14 21:50:09 +0000330
331 ResultReg = RR;
332 return true;
Evan Cheng24e3a902008-09-08 06:35:17 +0000333}
334
Dan Gohman0586d912008-09-10 20:11:02 +0000335/// X86SelectAddress - Attempt to fill in an address from the given value.
336///
Dan Gohman46510a72010-04-15 01:51:59 +0000337bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
338 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000339 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000340 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000341 // Don't walk into other basic blocks; it's possible we haven't
342 // visited them yet, so the instructions may not yet be assigned
343 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000344 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
345 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
346 Opcode = I->getOpcode();
347 U = I;
348 }
Dan Gohman46510a72010-04-15 01:51:59 +0000349 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000350 Opcode = C->getOpcode();
351 U = C;
352 }
Dan Gohman0586d912008-09-10 20:11:02 +0000353
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000354 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner868ee942010-06-15 19:08:40 +0000355 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000356 // Fast instruction selection doesn't support the special
357 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000358 return false;
359
Dan Gohman35893082008-09-18 23:23:44 +0000360 switch (Opcode) {
361 default: break;
362 case Instruction::BitCast:
363 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000364 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000365
366 case Instruction::IntToPtr:
367 // Look past no-op inttoptrs.
368 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000369 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000370 break;
Dan Gohman35893082008-09-18 23:23:44 +0000371
372 case Instruction::PtrToInt:
373 // Look past no-op ptrtoints.
374 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000375 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000376 break;
Dan Gohman35893082008-09-18 23:23:44 +0000377
378 case Instruction::Alloca: {
379 // Do static allocas.
380 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000381 DenseMap<const AllocaInst*, int>::iterator SI =
382 FuncInfo.StaticAllocaMap.find(A);
383 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000384 AM.BaseType = X86AddressMode::FrameIndexBase;
385 AM.Base.FrameIndex = SI->second;
386 return true;
387 }
388 break;
Dan Gohman35893082008-09-18 23:23:44 +0000389 }
390
391 case Instruction::Add: {
392 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000393 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000394 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
395 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000396 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000397 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000398 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000399 }
Dan Gohman0586d912008-09-10 20:11:02 +0000400 }
Dan Gohman35893082008-09-18 23:23:44 +0000401 break;
402 }
403
404 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000405 X86AddressMode SavedAM = AM;
406
Dan Gohman35893082008-09-18 23:23:44 +0000407 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000408 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000409 unsigned IndexReg = AM.IndexReg;
410 unsigned Scale = AM.Scale;
411 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000412 // Iterate through the indices, folding what we can. Constants can be
413 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000414 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000415 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000416 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000417 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman35893082008-09-18 23:23:44 +0000418 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000419 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
420 continue;
421 }
Eric Christopher471e4222011-06-08 23:55:35 +0000422
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000423 // A array/variable index is always of the form i*S where S is the
424 // constant scale size. See if we can push the scale into immediates.
425 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
426 for (;;) {
427 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
428 // Constant-offset addressing.
429 Disp += CI->getSExtValue() * S;
430 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000431 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000432 if (isa<AddOperator>(Op) &&
433 (!isa<Instruction>(Op) ||
434 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
435 == FuncInfo.MBB) &&
436 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
437 // An add (in the same block) with a constant operand. Fold the
438 // constant.
439 ConstantInt *CI =
440 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
441 Disp += CI->getSExtValue() * S;
442 // Iterate on the other operand.
443 Op = cast<AddOperator>(Op)->getOperand(0);
444 continue;
445 }
446 if (IndexReg == 0 &&
447 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
448 (S == 1 || S == 2 || S == 4 || S == 8)) {
449 // Scaled-index addressing.
450 Scale = S;
451 IndexReg = getRegForGEPIndex(Op).first;
452 if (IndexReg == 0)
453 return false;
454 break;
455 }
456 // Unsupported.
457 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000458 }
459 }
Dan Gohman09aae462008-09-26 20:04:15 +0000460 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000461 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000462 break;
Dan Gohman35893082008-09-18 23:23:44 +0000463 // Ok, the GEP indices were covered by constant-offset and scaled-index
464 // addressing. Update the address state and move on to examining the base.
465 AM.IndexReg = IndexReg;
466 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000467 AM.Disp = (uint32_t)Disp;
Chris Lattner225d4ca2010-03-04 19:48:19 +0000468 if (X86SelectAddress(U->getOperand(0), AM))
469 return true;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000470
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000471 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000472 // our address and just match the value instead of completely failing.
473 AM = SavedAM;
474 break;
Dan Gohman35893082008-09-18 23:23:44 +0000475 unsupported_gep:
476 // Ok, the GEP indices weren't all covered.
477 break;
478 }
479 }
480
481 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000482 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Eli Friedmana6176ad2011-09-22 23:41:28 +0000483 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000484 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000485 return false;
486
Eli Friedmana6176ad2011-09-22 23:41:28 +0000487 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000488 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000489 if (GVar->isThreadLocal())
490 return false;
Eric Christopher471e4222011-06-08 23:55:35 +0000491
Eli Friedmana6176ad2011-09-22 23:41:28 +0000492 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
493 // it works...).
494 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
495 if (const GlobalVariable *GVar =
496 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
497 if (GVar->isThreadLocal())
498 return false;
499
Chris Lattner0a1c9972011-04-17 17:47:38 +0000500 // RIP-relative addresses can't have additional register operands, so if
501 // we've already folded stuff into the addressing mode, just force the
502 // global value into its own register, which we can use as the basereg.
503 if (!Subtarget->isPICStyleRIPRel() ||
504 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
505 // Okay, we've committed to selecting this global. Set up the address.
506 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000507
Chris Lattner0a1c9972011-04-17 17:47:38 +0000508 // Allow the subtarget to classify the global.
509 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000510
Chris Lattner0a1c9972011-04-17 17:47:38 +0000511 // If this reference is relative to the pic base, set it now.
512 if (isGlobalRelativeToPICBase(GVFlags)) {
513 // FIXME: How do we know Base.Reg is free??
514 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000515 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000516
517 // Unless the ABI requires an extra load, return a direct reference to
518 // the global.
519 if (!isGlobalStubReference(GVFlags)) {
520 if (Subtarget->isPICStyleRIPRel()) {
521 // Use rip-relative addressing if we can. Above we verified that the
522 // base and index registers are unused.
523 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
524 AM.Base.Reg = X86::RIP;
525 }
526 AM.GVOpFlags = GVFlags;
527 return true;
528 }
529
530 // Ok, we need to do a load from a stub. If we've already loaded from
531 // this stub, reuse the loaded pointer, otherwise emit the load now.
532 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
533 unsigned LoadReg;
534 if (I != LocalValueMap.end() && I->second != 0) {
535 LoadReg = I->second;
536 } else {
537 // Issue load from stub.
538 unsigned Opc = 0;
539 const TargetRegisterClass *RC = NULL;
540 X86AddressMode StubAM;
541 StubAM.Base.Reg = AM.Base.Reg;
542 StubAM.GV = GV;
543 StubAM.GVOpFlags = GVFlags;
544
545 // Prepare for inserting code in the local-value area.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000546 SavePoint SaveInsertPt = enterLocalValueArea();
Chris Lattner0a1c9972011-04-17 17:47:38 +0000547
548 if (TLI.getPointerTy() == MVT::i64) {
549 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000550 RC = &X86::GR64RegClass;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000551
552 if (Subtarget->isPICStyleRIPRel())
553 StubAM.Base.Reg = X86::RIP;
554 } else {
555 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000556 RC = &X86::GR32RegClass;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000557 }
558
559 LoadReg = createResultReg(RC);
560 MachineInstrBuilder LoadMI =
561 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
562 addFullAddress(LoadMI, StubAM);
563
564 // Ok, back to normal mode.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000565 leaveLocalValueArea(SaveInsertPt);
Chris Lattner0a1c9972011-04-17 17:47:38 +0000566
567 // Prevent loading GV stub multiple times in same MBB.
568 LocalValueMap[V] = LoadReg;
569 }
570
571 // Now construct the final address. Note that the Disp, Scale,
572 // and Index values may already be set here.
573 AM.Base.Reg = LoadReg;
574 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000575 return true;
576 }
Dan Gohman0586d912008-09-10 20:11:02 +0000577 }
578
Dan Gohman97135e12008-09-26 19:15:30 +0000579 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000580 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000581 if (AM.Base.Reg == 0) {
582 AM.Base.Reg = getRegForValue(V);
583 return AM.Base.Reg != 0;
584 }
585 if (AM.IndexReg == 0) {
586 assert(AM.Scale == 1 && "Scale with no index!");
587 AM.IndexReg = getRegForValue(V);
588 return AM.IndexReg != 0;
589 }
590 }
591
592 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000593}
594
Chris Lattner0aa43de2009-07-10 05:33:42 +0000595/// X86SelectCallAddress - Attempt to fill in an address from the given value.
596///
Dan Gohman46510a72010-04-15 01:51:59 +0000597bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
598 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000599 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000600 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000601 Opcode = I->getOpcode();
602 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000603 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000604 Opcode = C->getOpcode();
605 U = C;
606 }
607
608 switch (Opcode) {
609 default: break;
610 case Instruction::BitCast:
611 // Look past bitcasts.
612 return X86SelectCallAddress(U->getOperand(0), AM);
613
614 case Instruction::IntToPtr:
615 // Look past no-op inttoptrs.
616 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
617 return X86SelectCallAddress(U->getOperand(0), AM);
618 break;
619
620 case Instruction::PtrToInt:
621 // Look past no-op ptrtoints.
622 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
623 return X86SelectCallAddress(U->getOperand(0), AM);
624 break;
625 }
626
627 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000628 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000629 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000630 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000631 return false;
632
633 // RIP-relative addresses can't have additional register operands.
634 if (Subtarget->isPICStyleRIPRel() &&
635 (AM.Base.Reg != 0 || AM.IndexReg != 0))
636 return false;
637
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000638 // Can't handle DLLImport.
639 if (GV->hasDLLImportLinkage())
640 return false;
641
642 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000643 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000644 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000645 return false;
646
647 // Okay, we've committed to selecting this global. Set up the basic address.
648 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000649
Chris Lattnere6c07b52009-07-10 05:45:15 +0000650 // No ABI requires an extra load for anything other than DLLImport, which
651 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000652 if (Subtarget->isPICStyleRIPRel()) {
653 // Use rip-relative addressing if we can. Above we verified that the
654 // base and index registers are unused.
655 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
656 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000657 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000658 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
659 } else if (Subtarget->isPICStyleGOT()) {
660 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000661 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000662
Chris Lattner0aa43de2009-07-10 05:33:42 +0000663 return true;
664 }
665
666 // If all else fails, try to materialize the value in a register.
667 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
668 if (AM.Base.Reg == 0) {
669 AM.Base.Reg = getRegForValue(V);
670 return AM.Base.Reg != 0;
671 }
672 if (AM.IndexReg == 0) {
673 assert(AM.Scale == 1 && "Scale with no index!");
674 AM.IndexReg = getRegForValue(V);
675 return AM.IndexReg != 0;
676 }
677 }
678
679 return false;
680}
681
682
Owen Andersona3971df2008-09-04 07:08:58 +0000683/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000684bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000685 // Atomic stores need special handling.
Lang Hamese4824712011-10-18 22:11:33 +0000686 const StoreInst *S = cast<StoreInst>(I);
687
688 if (S->isAtomic())
689 return false;
690
Duncan Sands1440e8b2010-11-03 11:35:31 +0000691 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000692 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000693 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000694
Dan Gohman0586d912008-09-10 20:11:02 +0000695 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000696 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000697 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000698
Chris Lattner438949a2008-10-15 05:30:52 +0000699 return X86FastEmitStore(VT, I->getOperand(0), AM);
Owen Andersona3971df2008-09-04 07:08:58 +0000700}
701
Dan Gohman84023e02010-07-10 09:00:22 +0000702/// X86SelectRet - Select and emit code to implement ret instructions.
703bool X86FastISel::X86SelectRet(const Instruction *I) {
704 const ReturnInst *Ret = cast<ReturnInst>(I);
705 const Function &F = *I->getParent()->getParent();
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000706 const X86MachineFunctionInfo *X86MFInfo =
707 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohman84023e02010-07-10 09:00:22 +0000708
709 if (!FuncInfo.CanLowerReturn)
710 return false;
711
712 CallingConv::ID CC = F.getCallingConv();
713 if (CC != CallingConv::C &&
714 CC != CallingConv::Fast &&
715 CC != CallingConv::X86_FastCall)
716 return false;
717
718 if (Subtarget->isTargetWin64())
719 return false;
720
721 // Don't handle popping bytes on return for now.
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000722 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszakd61932b2013-02-17 18:35:25 +0000723 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000724
725 // fastcc with -tailcallopt is intended to provide a guaranteed
726 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000727 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohman84023e02010-07-10 09:00:22 +0000728 return false;
729
730 // Let SDISel handle vararg functions.
731 if (F.isVarArg())
732 return false;
733
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000734 // Build a list of return value registers.
735 SmallVector<unsigned, 4> RetRegs;
736
Dan Gohman84023e02010-07-10 09:00:22 +0000737 if (Ret->getNumOperands() > 0) {
738 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +0000739 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohman84023e02010-07-10 09:00:22 +0000740
741 // Analyze operands of the call, assigning locations to each operand.
742 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000743 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +0000744 I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000745 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000746
747 const Value *RV = Ret->getOperand(0);
748 unsigned Reg = getRegForValue(RV);
749 if (Reg == 0)
750 return false;
751
752 // Only handle a single return value for now.
753 if (ValLocs.size() != 1)
754 return false;
755
756 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000757
Dan Gohman84023e02010-07-10 09:00:22 +0000758 // Don't bother handling odd stuff for now.
759 if (VA.getLocInfo() != CCValAssign::Full)
760 return false;
761 // Only handle register returns for now.
762 if (!VA.isRegLoc())
763 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000764
765 // The calling-convention tables for x87 returns don't tell
766 // the whole story.
767 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
768 return false;
769
Eli Friedman22486c92011-05-18 23:13:10 +0000770 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000771 EVT SrcVT = TLI.getValueType(RV->getType());
772 EVT DstVT = VA.getValVT();
773 // Special handling for extended integers.
774 if (SrcVT != DstVT) {
775 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
776 return false;
777
778 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
779 return false;
780
781 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
782
783 if (SrcVT == MVT::i1) {
784 if (Outs[0].Flags.isSExt())
785 return false;
786 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
787 SrcVT = MVT::i8;
788 }
789 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
790 ISD::SIGN_EXTEND;
791 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
792 SrcReg, /*TODO: Kill=*/false);
793 }
794
795 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000796 unsigned DstReg = VA.getLocReg();
797 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000798 // Avoid a cross-class copy. This is very unlikely.
799 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000800 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000801 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
802 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000803
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000804 // Add register to return instruction.
805 RetRegs.push_back(VA.getLocReg());
Dan Gohman84023e02010-07-10 09:00:22 +0000806 }
807
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000808 // The x86-64 ABI for returning structs by value requires that we copy
809 // the sret argument into %rax for the return. We saved the argument into
810 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000811 // and into %rax. We also do the same with %eax for Win32.
812 if (F.hasStructRetAttr() &&
813 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000814 unsigned Reg = X86MFInfo->getSRetReturnReg();
815 assert(Reg &&
816 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000817 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000818 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000819 RetReg).addReg(Reg);
820 RetRegs.push_back(RetReg);
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000821 }
822
Dan Gohman84023e02010-07-10 09:00:22 +0000823 // Now emit the RET.
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000824 MachineInstrBuilder MIB =
825 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
826 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
827 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohman84023e02010-07-10 09:00:22 +0000828 return true;
829}
830
Evan Cheng8b19e562008-09-03 06:44:39 +0000831/// X86SelectLoad - Select and emit code to implement load instructions.
832///
Dan Gohman46510a72010-04-15 01:51:59 +0000833bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000834 // Atomic loads need special handling.
835 if (cast<LoadInst>(I)->isAtomic())
836 return false;
837
Duncan Sands1440e8b2010-11-03 11:35:31 +0000838 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000839 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000840 return false;
841
Dan Gohman0586d912008-09-10 20:11:02 +0000842 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000843 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000844 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000845
Evan Cheng0de588f2008-09-05 21:00:03 +0000846 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000847 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000848 UpdateValueMap(I, ResultReg);
849 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000850 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000851 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000852}
853
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000854static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000855 bool HasAVX = Subtarget->hasAVX();
Craig Topper1accb7e2012-01-10 06:54:16 +0000856 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
857 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000858
Owen Anderson825b72b2009-08-11 20:47:22 +0000859 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000860 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000861 case MVT::i8: return X86::CMP8rr;
862 case MVT::i16: return X86::CMP16rr;
863 case MVT::i32: return X86::CMP32rr;
864 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000865 case MVT::f32:
866 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
867 case MVT::f64:
868 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000869 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000870}
871
Chris Lattner0e13c782008-10-15 04:13:29 +0000872/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
873/// of the comparison, return an opcode that works for the compare (e.g.
874/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000875static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000876 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000877 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000878 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000879 case MVT::i8: return X86::CMP8ri;
880 case MVT::i16: return X86::CMP16ri;
881 case MVT::i32: return X86::CMP32ri;
882 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000883 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
884 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000885 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000886 return X86::CMP64ri32;
887 return 0;
888 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000889}
890
Dan Gohman46510a72010-04-15 01:51:59 +0000891bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
892 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000893 unsigned Op0Reg = getRegForValue(Op0);
894 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000895
Chris Lattnerd53886b2008-10-15 05:18:04 +0000896 // Handle 'null' like i32/i64 0.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000897 if (isa<ConstantPointerNull>(Op1))
898 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000899
Chris Lattner9a08a612008-10-15 04:26:38 +0000900 // We have two options: compare with register or immediate. If the RHS of
901 // the compare is an immediate that we can fold into this compare, use
902 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000903 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000904 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000905 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
906 .addReg(Op0Reg)
907 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000908 return true;
909 }
910 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000911
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000912 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000913 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000914
Chris Lattner9a08a612008-10-15 04:26:38 +0000915 unsigned Op1Reg = getRegForValue(Op1);
916 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000917 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
918 .addReg(Op0Reg)
919 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000920
Chris Lattner9a08a612008-10-15 04:26:38 +0000921 return true;
922}
923
Dan Gohman46510a72010-04-15 01:51:59 +0000924bool X86FastISel::X86SelectCmp(const Instruction *I) {
925 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000926
Duncan Sands1440e8b2010-11-03 11:35:31 +0000927 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000928 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000929 return false;
930
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000931 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000932 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000933 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000934 switch (CI->getPredicate()) {
935 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000936 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
937 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000938
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000939 unsigned EReg = createResultReg(&X86::GR8RegClass);
940 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000941 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
942 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
943 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000944 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000945 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000946 UpdateValueMap(I, ResultReg);
947 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000948 }
949 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000950 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
951 return false;
952
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000953 unsigned NEReg = createResultReg(&X86::GR8RegClass);
954 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000955 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
956 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
957 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000958 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000959 UpdateValueMap(I, ResultReg);
960 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000961 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000962 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
963 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
964 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
965 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
966 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
967 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
968 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
969 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
970 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
971 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
972 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
973 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000974
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000975 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
976 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
977 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
978 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
979 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
980 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
981 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
982 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
983 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
984 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000985 default:
986 return false;
987 }
988
Dan Gohman46510a72010-04-15 01:51:59 +0000989 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000990 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +0000991 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000992
Chris Lattner9a08a612008-10-15 04:26:38 +0000993 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000994 if (!X86FastEmitCompare(Op0, Op1, VT))
995 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000996
Dan Gohman84023e02010-07-10 09:00:22 +0000997 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000998 UpdateValueMap(I, ResultReg);
999 return true;
1000}
Evan Cheng8b19e562008-09-03 06:44:39 +00001001
Dan Gohman46510a72010-04-15 01:51:59 +00001002bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedman76927d732011-05-25 23:49:02 +00001003 EVT DstVT = TLI.getValueType(I->getType());
1004 if (!TLI.isTypeLegal(DstVT))
1005 return false;
1006
1007 unsigned ResultReg = getRegForValue(I->getOperand(0));
1008 if (ResultReg == 0)
1009 return false;
1010
Tim Northoverda0416b2013-05-30 10:43:18 +00001011 // Handle zero-extension from i1 to i8, which is common.
1012 MVT SrcVT = TLI.getValueType(I->getOperand(0)->getType()).getSimpleVT();
1013 if (SrcVT.SimpleTy == MVT::i1) {
1014 // Set the high bits to zero.
1015 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1016 SrcVT = MVT::i8;
Eli Friedman76927d732011-05-25 23:49:02 +00001017
Tim Northoverda0416b2013-05-30 10:43:18 +00001018 if (ResultReg == 0)
1019 return false;
1020 }
1021
1022 if (DstVT == MVT::i64) {
1023 // Handle extension to 64-bits via sub-register shenanigans.
1024 unsigned MovInst;
1025
1026 switch (SrcVT.SimpleTy) {
1027 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1028 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1029 case MVT::i32: MovInst = X86::MOV32rr; break;
1030 default: llvm_unreachable("Unexpected zext to i64 source type");
1031 }
1032
1033 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1034 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovInst), Result32)
1035 .addReg(ResultReg);
1036
1037 ResultReg = createResultReg(&X86::GR64RegClass);
1038 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::SUBREG_TO_REG),
1039 ResultReg)
1040 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1041 } else if (DstVT != MVT::i8) {
Eli Friedman76927d732011-05-25 23:49:02 +00001042 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1043 ResultReg, /*Kill=*/true);
1044 if (ResultReg == 0)
1045 return false;
Dan Gohmand89ae992008-09-05 01:06:14 +00001046 }
1047
Eli Friedman76927d732011-05-25 23:49:02 +00001048 UpdateValueMap(I, ResultReg);
1049 return true;
Dan Gohmand89ae992008-09-05 01:06:14 +00001050}
1051
Chris Lattner9a08a612008-10-15 04:26:38 +00001052
Dan Gohman46510a72010-04-15 01:51:59 +00001053bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +00001054 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +00001055 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001056 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +00001057 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1058 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +00001059
Dan Gohman8bef7442010-08-21 02:32:36 +00001060 // Fold the common case of a conditional branch with a comparison
1061 // in the same block (values defined on other blocks may not have
1062 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +00001063 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +00001064 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001065 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +00001066
Dan Gohmand98d6202008-10-02 22:15:21 +00001067 // Try to take advantage of fallthrough opportunities.
1068 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +00001069 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +00001070 std::swap(TrueMBB, FalseMBB);
1071 Predicate = CmpInst::getInversePredicate(Predicate);
1072 }
1073
Chris Lattner871d2462008-10-15 03:58:05 +00001074 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1075 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1076
Dan Gohmand98d6202008-10-02 22:15:21 +00001077 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +00001078 case CmpInst::FCMP_OEQ:
1079 std::swap(TrueMBB, FalseMBB);
1080 Predicate = CmpInst::FCMP_UNE;
1081 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001082 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1083 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1084 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1085 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1086 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1087 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1088 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1089 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1090 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1091 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1092 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1093 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1094 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001095
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001096 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1097 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1098 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1099 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1100 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1101 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1102 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1103 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1104 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1105 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001106 default:
1107 return false;
1108 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001109
Dan Gohman46510a72010-04-15 01:51:59 +00001110 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001111 if (SwapArgs)
1112 std::swap(Op0, Op1);
1113
Chris Lattner9a08a612008-10-15 04:26:38 +00001114 // Emit a compare of the LHS and RHS, setting the flags.
1115 if (!X86FastEmitCompare(Op0, Op1, VT))
1116 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001117
Dan Gohman84023e02010-07-10 09:00:22 +00001118 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1119 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001120
1121 if (Predicate == CmpInst::FCMP_UNE) {
1122 // X86 requires a second branch to handle UNE (and OEQ,
1123 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001124 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1125 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001126 }
1127
Stuart Hastings3bf91252010-06-17 22:43:56 +00001128 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001129 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001130 return true;
1131 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001132 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1133 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1134 // typically happen for _Bool and C++ bools.
1135 MVT SourceVT;
1136 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1137 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1138 unsigned TestOpc = 0;
1139 switch (SourceVT.SimpleTy) {
1140 default: break;
1141 case MVT::i8: TestOpc = X86::TEST8ri; break;
1142 case MVT::i16: TestOpc = X86::TEST16ri; break;
1143 case MVT::i32: TestOpc = X86::TEST32ri; break;
1144 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1145 }
1146 if (TestOpc) {
1147 unsigned OpReg = getRegForValue(TI->getOperand(0));
1148 if (OpReg == 0) return false;
1149 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1150 .addReg(OpReg).addImm(1);
Eric Christopher471e4222011-06-08 23:55:35 +00001151
Chris Lattnerc76d1212011-04-19 04:26:32 +00001152 unsigned JmpOpc = X86::JNE_4;
1153 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1154 std::swap(TrueMBB, FalseMBB);
1155 JmpOpc = X86::JE_4;
1156 }
Eric Christopher471e4222011-06-08 23:55:35 +00001157
Chris Lattnerc76d1212011-04-19 04:26:32 +00001158 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001159 .addMBB(TrueMBB);
1160 FastEmitBranch(FalseMBB, DL);
1161 FuncInfo.MBB->addSuccessor(TrueMBB);
1162 return true;
1163 }
1164 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001165 }
1166
1167 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001168 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1169 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001170 unsigned OpReg = getRegForValue(BI->getCondition());
1171 if (OpReg == 0) return false;
1172
Eli Friedman547eb4f2011-04-27 01:34:27 +00001173 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1174 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001175 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1176 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001177 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001178 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001179 return true;
1180}
1181
Dan Gohman46510a72010-04-15 01:51:59 +00001182bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001183 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001184 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001185 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001186 CReg = X86::CL;
1187 RC = &X86::GR8RegClass;
1188 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001189 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1190 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1191 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001192 default: return false;
1193 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001194 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001195 CReg = X86::CX;
1196 RC = &X86::GR16RegClass;
1197 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001198 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1199 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1200 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001201 default: return false;
1202 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001203 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001204 CReg = X86::ECX;
1205 RC = &X86::GR32RegClass;
1206 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001207 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1208 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1209 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001210 default: return false;
1211 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001212 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001213 CReg = X86::RCX;
1214 RC = &X86::GR64RegClass;
1215 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001216 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1217 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1218 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001219 default: return false;
1220 }
1221 } else {
1222 return false;
1223 }
1224
Duncan Sands1440e8b2010-11-03 11:35:31 +00001225 MVT VT;
1226 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001227 return false;
1228
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001229 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1230 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001231
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001232 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1233 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001234 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1235 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001236
1237 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001238 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001239 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001240 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1241 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001242 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001243
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001244 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001245 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1246 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001247 UpdateValueMap(I, ResultReg);
1248 return true;
1249}
1250
Eli Bendersky50125482013-04-17 20:10:13 +00001251bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1252 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1253 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1254 const static bool S = true; // IsSigned
1255 const static bool U = false; // !IsSigned
1256 const static unsigned Copy = TargetOpcode::COPY;
1257 // For the X86 DIV/IDIV instruction, in most cases the dividend
1258 // (numerator) must be in a specific register pair highreg:lowreg,
1259 // producing the quotient in lowreg and the remainder in highreg.
1260 // For most data types, to set up the instruction, the dividend is
1261 // copied into lowreg, and lowreg is sign-extended or zero-extended
1262 // into highreg. The exception is i8, where the dividend is defined
1263 // as a single register rather than a register pair, and we
1264 // therefore directly sign-extend or zero-extend the dividend into
1265 // lowreg, instead of copying, and ignore the highreg.
1266 const static struct DivRemEntry {
1267 // The following portion depends only on the data type.
1268 const TargetRegisterClass *RC;
1269 unsigned LowInReg; // low part of the register pair
1270 unsigned HighInReg; // high part of the register pair
1271 // The following portion depends on both the data type and the operation.
1272 struct DivRemResult {
1273 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1274 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1275 // highreg, or copying a zero into highreg.
1276 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1277 // zero/sign-extending into lowreg for i8.
1278 unsigned DivRemResultReg; // Register containing the desired result.
1279 bool IsOpSigned; // Whether to use signed or unsigned form.
1280 } ResultTable[NumOps];
1281 } OpTable[NumTypes] = {
1282 { &X86::GR8RegClass, X86::AX, 0, {
1283 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1284 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1285 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1286 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1287 }
1288 }, // i8
1289 { &X86::GR16RegClass, X86::AX, X86::DX, {
1290 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1291 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover15983b82013-05-30 13:19:42 +00001292 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1293 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky50125482013-04-17 20:10:13 +00001294 }
1295 }, // i16
1296 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1297 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1298 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1299 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1300 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1301 }
1302 }, // i32
1303 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1304 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1305 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover15983b82013-05-30 13:19:42 +00001306 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1307 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky50125482013-04-17 20:10:13 +00001308 }
1309 }, // i64
1310 };
1311
1312 MVT VT;
1313 if (!isTypeLegal(I->getType(), VT))
1314 return false;
1315
1316 unsigned TypeIndex, OpIndex;
1317 switch (VT.SimpleTy) {
1318 default: return false;
1319 case MVT::i8: TypeIndex = 0; break;
1320 case MVT::i16: TypeIndex = 1; break;
1321 case MVT::i32: TypeIndex = 2; break;
1322 case MVT::i64: TypeIndex = 3;
1323 if (!Subtarget->is64Bit())
1324 return false;
1325 break;
1326 }
1327
1328 switch (I->getOpcode()) {
1329 default: llvm_unreachable("Unexpected div/rem opcode");
1330 case Instruction::SDiv: OpIndex = 0; break;
1331 case Instruction::SRem: OpIndex = 1; break;
1332 case Instruction::UDiv: OpIndex = 2; break;
1333 case Instruction::URem: OpIndex = 3; break;
1334 }
1335
1336 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1337 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1338 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1339 if (Op0Reg == 0)
1340 return false;
1341 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1342 if (Op1Reg == 0)
1343 return false;
1344
1345 // Move op0 into low-order input register.
1346 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1347 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1348 // Zero-extend or sign-extend into high-order input register.
1349 if (OpEntry.OpSignExtend) {
1350 if (OpEntry.IsOpSigned)
1351 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1352 TII.get(OpEntry.OpSignExtend));
Tim Northover15983b82013-05-30 13:19:42 +00001353 else {
1354 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Eli Bendersky50125482013-04-17 20:10:13 +00001355 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Tim Northover15983b82013-05-30 13:19:42 +00001356 TII.get(X86::MOV32r0), Zero32);
1357
1358 // Copy the zero into the appropriate sub/super/identical physical
1359 // register. Unfortunately the operations needed are not uniform enough to
1360 // fit neatly into the table above.
1361 if (VT.SimpleTy == MVT::i16) {
1362 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher3ce2a982013-06-11 23:41:41 +00001363 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover15983b82013-05-30 13:19:42 +00001364 .addReg(Zero32, 0, X86::sub_16bit);
1365 } else if (VT.SimpleTy == MVT::i32) {
1366 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher3ce2a982013-06-11 23:41:41 +00001367 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover15983b82013-05-30 13:19:42 +00001368 .addReg(Zero32);
1369 } else if (VT.SimpleTy == MVT::i64) {
1370 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1371 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1372 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1373 }
1374 }
Eli Bendersky50125482013-04-17 20:10:13 +00001375 }
1376 // Generate the DIV/IDIV instruction.
1377 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1378 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
1379 // Copy output register into result register.
1380 unsigned ResultReg = createResultReg(TypeEntry.RC);
1381 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1382 TII.get(Copy), ResultReg).addReg(OpEntry.DivRemResultReg);
1383 UpdateValueMap(I, ResultReg);
1384
1385 return true;
1386}
1387
Dan Gohman46510a72010-04-15 01:51:59 +00001388bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001389 MVT VT;
1390 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001391 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001392
Eric Christophere487b012010-09-29 23:00:29 +00001393 // We only use cmov here, if we don't have a cmov instruction bail.
1394 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001395
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001396 unsigned Opc = 0;
1397 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001398 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001399 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001400 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001401 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001402 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001403 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001404 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001405 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001406 RC = &X86::GR64RegClass;
1407 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001408 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001409 }
1410
1411 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1412 if (Op0Reg == 0) return false;
1413 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1414 if (Op1Reg == 0) return false;
1415 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1416 if (Op2Reg == 0) return false;
1417
Dan Gohman84023e02010-07-10 09:00:22 +00001418 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1419 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001420 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001421 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1422 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001423 UpdateValueMap(I, ResultReg);
1424 return true;
1425}
1426
Dan Gohman46510a72010-04-15 01:51:59 +00001427bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001428 // fpext from float to double.
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001429 if (X86ScalarSSEf64 &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001430 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001431 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001432 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001433 unsigned OpReg = getRegForValue(V);
1434 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001435 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001436 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1437 TII.get(X86::CVTSS2SDrr), ResultReg)
1438 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001439 UpdateValueMap(I, ResultReg);
1440 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001441 }
1442 }
1443
1444 return false;
1445}
1446
Dan Gohman46510a72010-04-15 01:51:59 +00001447bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001448 if (X86ScalarSSEf64) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001449 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001450 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001451 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001452 unsigned OpReg = getRegForValue(V);
1453 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001454 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001455 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1456 TII.get(X86::CVTSD2SSrr), ResultReg)
1457 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001458 UpdateValueMap(I, ResultReg);
1459 return true;
1460 }
1461 }
1462 }
1463
1464 return false;
1465}
1466
Dan Gohman46510a72010-04-15 01:51:59 +00001467bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001468 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1469 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001470
Eli Friedman76927d732011-05-25 23:49:02 +00001471 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001472 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001473 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001474 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001475 return false;
1476
1477 unsigned InputReg = getRegForValue(I->getOperand(0));
1478 if (!InputReg)
1479 // Unhandled operand. Halt "fast" selection and bail.
1480 return false;
1481
Eli Friedman76927d732011-05-25 23:49:02 +00001482 if (SrcVT == MVT::i8) {
1483 // Truncate from i8 to i1; no code needed.
1484 UpdateValueMap(I, InputReg);
1485 return true;
1486 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001487
Eli Friedman76927d732011-05-25 23:49:02 +00001488 if (!Subtarget->is64Bit()) {
1489 // If we're on x86-32; we can't extract an i8 from a general register.
1490 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperc9099502012-04-20 06:31:50 +00001491 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1492 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1493 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedman76927d732011-05-25 23:49:02 +00001494 unsigned CopyReg = createResultReg(CopyRC);
1495 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1496 CopyReg).addReg(InputReg);
1497 InputReg = CopyReg;
1498 }
1499
1500 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001501 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001502 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001503 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001504 if (!ResultReg)
1505 return false;
1506
1507 UpdateValueMap(I, ResultReg);
1508 return true;
1509}
1510
Eli Friedmanc0883452011-05-20 22:21:04 +00001511bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1512 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1513}
1514
Eli Friedmand5089a92011-04-27 01:45:07 +00001515bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1516 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001517
Eli Friedmand5089a92011-04-27 01:45:07 +00001518 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001519 if (!IsMemcpySmall(Len))
1520 return false;
1521
1522 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001523
1524 // We don't care about alignment here since we just emit integer accesses.
1525 while (Len) {
1526 MVT VT;
1527 if (Len >= 8 && i64Legal)
1528 VT = MVT::i64;
1529 else if (Len >= 4)
1530 VT = MVT::i32;
1531 else if (Len >= 2)
1532 VT = MVT::i16;
1533 else {
Eli Friedmand5089a92011-04-27 01:45:07 +00001534 VT = MVT::i8;
1535 }
1536
1537 unsigned Reg;
1538 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1539 RV &= X86FastEmitStore(VT, Reg, DestAM);
1540 assert(RV && "Failed to emit load or store??");
1541
1542 unsigned Size = VT.getSizeInBits()/8;
1543 Len -= Size;
1544 DestAM.Disp += Size;
1545 SrcAM.Disp += Size;
1546 }
1547
1548 return true;
1549}
1550
Dan Gohman46510a72010-04-15 01:51:59 +00001551bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001552 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001553 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001554 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001555 case Intrinsic::memcpy: {
1556 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1557 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001558 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001559 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001560
Eli Friedman25255cb2011-06-10 23:39:36 +00001561 if (isa<ConstantInt>(MCI.getLength())) {
1562 // Small memcpy's are common enough that we want to do them
1563 // without a call if possible.
1564 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1565 if (IsMemcpySmall(Len)) {
1566 X86AddressMode DestAM, SrcAM;
1567 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1568 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1569 return false;
1570 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1571 return true;
1572 }
1573 }
Eric Christopher471e4222011-06-08 23:55:35 +00001574
Eli Friedman25255cb2011-06-10 23:39:36 +00001575 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1576 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001577 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001578
Eli Friedman25255cb2011-06-10 23:39:36 +00001579 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1580 return false;
1581
1582 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001583 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001584 case Intrinsic::memset: {
1585 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001586
Nick Lewycky3207c9a2011-08-02 00:40:16 +00001587 if (MSI.isVolatile())
1588 return false;
1589
Eli Friedman25255cb2011-06-10 23:39:36 +00001590 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1591 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1592 return false;
1593
1594 if (MSI.getDestAddressSpace() > 255)
1595 return false;
1596
1597 return DoSelectCall(&I, "memset");
1598 }
Eric Christopher07754c22010-03-18 20:27:26 +00001599 case Intrinsic::stackprotector: {
Chad Rosiere1093e52012-05-11 19:43:29 +00001600 // Emit code to store the stack guard onto the stack.
Eric Christopher07754c22010-03-18 20:27:26 +00001601 EVT PtrTy = TLI.getPointerTy();
1602
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001603 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1604 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001605
1606 // Grab the frame index.
1607 X86AddressMode AM;
1608 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001609 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001610 return true;
1611 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001612 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001613 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001614 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001615 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001616 if (!X86SelectAddress(DI->getAddress(), AM))
1617 return false;
Evan Chenge837dea2011-06-28 19:10:37 +00001618 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001619 // FIXME may need to add RegState::Debug to any registers produced,
1620 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001621 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1622 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001623 return true;
1624 }
Eric Christopher77f79892010-01-18 22:11:29 +00001625 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001626 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001627 return true;
1628 }
Bill Wendling52370a12008-12-09 02:42:50 +00001629 case Intrinsic::sadd_with_overflow:
1630 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001631 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001632
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001633 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001634 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001635 const Function *Callee = I.getCalledFunction();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001636 Type *RetTy =
Bill Wendling52370a12008-12-09 02:42:50 +00001637 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1638
Duncan Sands1440e8b2010-11-03 11:35:31 +00001639 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001640 if (!isTypeLegal(RetTy, VT))
1641 return false;
1642
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001643 const Value *Op1 = I.getArgOperand(0);
1644 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001645 unsigned Reg1 = getRegForValue(Op1);
1646 unsigned Reg2 = getRegForValue(Op2);
1647
1648 if (Reg1 == 0 || Reg2 == 0)
1649 // FIXME: Handle values *not* in registers.
1650 return false;
1651
1652 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001653 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001654 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001655 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001656 OpC = X86::ADD64rr;
1657 else
1658 return false;
1659
Eli Friedman482feb32011-05-16 21:06:17 +00001660 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001661 // both the returned values.
Eli Friedman482feb32011-05-16 21:06:17 +00001662 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001663 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1664 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001665
Chris Lattnera9a42252009-04-12 07:36:01 +00001666 unsigned Opc = X86::SETBr;
1667 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1668 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001669 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1670
1671 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001672 return true;
1673 }
1674 }
1675}
1676
Chad Rosierfd3417d2013-02-25 21:59:35 +00001677bool X86FastISel::FastLowerArguments() {
1678 if (!FuncInfo.CanLowerReturn)
1679 return false;
1680
Chad Rosier146b8c22013-04-02 16:31:41 +00001681 if (Subtarget->isTargetWin64())
Chad Rosierd9b306a2013-03-14 21:25:04 +00001682 return false;
1683
Chad Rosierfd3417d2013-02-25 21:59:35 +00001684 const Function *F = FuncInfo.Fn;
1685 if (F->isVarArg())
1686 return false;
1687
1688 CallingConv::ID CC = F->getCallingConv();
1689 if (CC != CallingConv::C)
1690 return false;
1691
1692 if (!Subtarget->is64Bit())
1693 return false;
1694
1695 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
1696 unsigned Idx = 1;
1697 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1698 I != E; ++I, ++Idx) {
1699 if (Idx > 6)
1700 return false;
1701
1702 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1703 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1704 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1705 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1706 return false;
1707
1708 Type *ArgTy = I->getType();
1709 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1710 return false;
1711
1712 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosierfe88aa02013-02-26 01:05:31 +00001713 if (!ArgVT.isSimple()) return false;
Chad Rosierfd3417d2013-02-25 21:59:35 +00001714 switch (ArgVT.getSimpleVT().SimpleTy) {
1715 case MVT::i32:
1716 case MVT::i64:
1717 break;
1718 default:
1719 return false;
1720 }
1721 }
1722
1723 static const uint16_t GPR32ArgRegs[] = {
1724 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1725 };
1726 static const uint16_t GPR64ArgRegs[] = {
1727 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
1728 };
1729
1730 Idx = 0;
1731 const TargetRegisterClass *RC32 = TLI.getRegClassFor(MVT::i32);
1732 const TargetRegisterClass *RC64 = TLI.getRegClassFor(MVT::i64);
1733 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1734 I != E; ++I, ++Idx) {
Chad Rosierfd3417d2013-02-25 21:59:35 +00001735 bool is32Bit = TLI.getValueType(I->getType()) == MVT::i32;
1736 const TargetRegisterClass *RC = is32Bit ? RC32 : RC64;
1737 unsigned SrcReg = is32Bit ? GPR32ArgRegs[Idx] : GPR64ArgRegs[Idx];
1738 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1739 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1740 // Without this, EmitLiveInCopies may eliminate the livein if its only
1741 // use is a bitcast (which isn't turned into an instruction).
1742 unsigned ResultReg = createResultReg(RC);
1743 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1744 ResultReg).addReg(DstReg, getKillRegState(true));
1745 UpdateValueMap(I, ResultReg);
1746 }
1747 return true;
1748}
1749
Dan Gohman46510a72010-04-15 01:51:59 +00001750bool X86FastISel::X86SelectCall(const Instruction *I) {
1751 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001752 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001753
1754 // Can't handle inline asm yet.
1755 if (isa<InlineAsm>(Callee))
1756 return false;
1757
Bill Wendling52370a12008-12-09 02:42:50 +00001758 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001759 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001760 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001761
Chad Rosier425e9512012-12-11 00:18:02 +00001762 // Allow SelectionDAG isel to handle tail calls.
1763 if (cast<CallInst>(I)->isTailCall())
1764 return false;
1765
Eli Friedman25255cb2011-06-10 23:39:36 +00001766 return DoSelectCall(I, 0);
1767}
1768
Rafael Espindolac338fe02012-07-25 15:42:45 +00001769static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1770 const ImmutableCallSite &CS) {
Rafael Espindola742f2c92012-07-25 13:35:45 +00001771 if (Subtarget.is64Bit())
1772 return 0;
1773 if (Subtarget.isTargetWindows())
1774 return 0;
1775 CallingConv::ID CC = CS.getCallingConv();
1776 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1777 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001778 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola742f2c92012-07-25 13:35:45 +00001779 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001780 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola1cee7102012-07-25 13:41:10 +00001781 return 0;
Rafael Espindola742f2c92012-07-25 13:35:45 +00001782 return 4;
1783}
1784
Eli Friedman25255cb2011-06-10 23:39:36 +00001785// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1786bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1787 const CallInst *CI = cast<CallInst>(I);
1788 const Value *Callee = CI->getCalledValue();
1789
Evan Chengf3d4efe2008-09-07 09:09:33 +00001790 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001791 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001792 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001793 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001794 CC != CallingConv::X86_FastCall)
1795 return false;
1796
Evan Cheng381993f2010-01-27 00:00:57 +00001797 // fastcc with -tailcallopt is intended to provide a guaranteed
1798 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001799 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001800 return false;
1801
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001802 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1803 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001804 bool isVarArg = FTy->isVarArg();
1805
1806 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1807 // x86-32. Special handling for x86-64 is implemented.
1808 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001809 return false;
1810
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001811 // Fast-isel doesn't know about callee-pop yet.
Evan Chengef41ff62011-06-23 17:54:54 +00001812 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001813 TM.Options.GuaranteedTailCallOpt))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001814 return false;
1815
Eli Friedman19515b42011-05-17 18:29:03 +00001816 // Check whether the function can return without sret-demotion.
1817 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001818 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman19515b42011-05-17 18:29:03 +00001819 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendling56cb2292012-07-19 00:11:40 +00001820 *FuncInfo.MF, FTy->isVarArg(),
1821 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001822 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001823 return false;
1824
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001825 // Materialize callee address in a register. FIXME: GV address can be
1826 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001827 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001828 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001829 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001830 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001831 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001832 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001833 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001834 } else if (CalleeAM.Base.Reg != 0) {
1835 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001836 } else
1837 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001838
Evan Chengf3d4efe2008-09-07 09:09:33 +00001839 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001840 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001841 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001842 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001843 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosier15b44972012-02-15 00:36:26 +00001844 unsigned arg_size = CS.arg_size();
1845 Args.reserve(arg_size);
1846 ArgVals.reserve(arg_size);
1847 ArgVTs.reserve(arg_size);
1848 ArgFlags.reserve(arg_size);
Dan Gohman46510a72010-04-15 01:51:59 +00001849 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001850 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001851 // If we're lowering a mem intrinsic instead of a regular call, skip the
1852 // last two arguments, which should not passed to the underlying functions.
1853 if (MemIntName && e-i <= 2)
1854 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001855 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001856 ISD::ArgFlagsTy Flags;
1857 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling034b94b2012-12-19 07:18:57 +00001858 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001859 Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00001860 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001861 Flags.setZExt();
1862
Bill Wendling034b94b2012-12-19 07:18:57 +00001863 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001864 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1865 Type *ElementTy = Ty->getElementType();
Eli Friedmanc0883452011-05-20 22:21:04 +00001866 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1867 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1868 if (!FrameAlign)
1869 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1870 Flags.setByVal();
1871 Flags.setByValSize(FrameSize);
1872 Flags.setByValAlign(FrameAlign);
1873 if (!IsMemcpySmall(FrameSize))
1874 return false;
1875 }
1876
Bill Wendling034b94b2012-12-19 07:18:57 +00001877 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedmanc0883452011-05-20 22:21:04 +00001878 Flags.setInReg();
Bill Wendling034b94b2012-12-19 07:18:57 +00001879 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedmanc0883452011-05-20 22:21:04 +00001880 Flags.setNest();
1881
Chris Lattnere03b8d32011-04-19 04:42:38 +00001882 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1883 // instruction. This is safe because it is common to all fastisel supported
1884 // calling conventions on x86.
1885 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1886 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1887 CI->getBitWidth() == 16) {
1888 if (Flags.isSExt())
1889 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1890 else
1891 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1892 }
1893 }
Eric Christopher471e4222011-06-08 23:55:35 +00001894
Chris Lattnerb44101c2011-04-19 05:09:50 +00001895 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001896
Chris Lattnerff009ad2011-04-19 05:15:59 +00001897 // Passing bools around ends up doing a trunc to i1 and passing it.
1898 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001899 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1900 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1901 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001902 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1903 ArgReg = getRegForValue(ArgVal);
1904 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001905
Chris Lattnerb44101c2011-04-19 05:09:50 +00001906 MVT ArgVT;
1907 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001908
Chris Lattnerb44101c2011-04-19 05:09:50 +00001909 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1910 ArgVal->hasOneUse(), 1);
1911 } else {
1912 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001913 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001914
Chris Lattnerff009ad2011-04-19 05:15:59 +00001915 if (ArgReg == 0) return false;
1916
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001917 Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001918 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001919 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001920 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001921 if (ArgVT == MVT::x86mmx)
1922 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001923 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1924 Flags.setOrigAlign(OriginalAlignment);
1925
Chris Lattnerb44101c2011-04-19 05:09:50 +00001926 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001927 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001928 ArgVTs.push_back(ArgVT);
1929 ArgFlags.push_back(Flags);
1930 }
1931
1932 // Analyze operands of the call, assigning locations to each operand.
1933 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001934 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00001935 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001936
Dan Gohmand8acddd2010-06-01 21:09:47 +00001937 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001938 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001939 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001940
Duncan Sands45907662010-10-31 13:21:44 +00001941 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001942
1943 // Get a count of how many bytes are to be pushed on the stack.
1944 unsigned NumBytes = CCInfo.getNextStackOffset();
1945
1946 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001947 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001948 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1949 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001950
Chris Lattner438949a2008-10-15 05:30:52 +00001951 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001952 // copies / loads.
1953 SmallVector<unsigned, 4> RegArgs;
1954 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1955 CCValAssign &VA = ArgLocs[i];
1956 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001957 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001958
Evan Chengf3d4efe2008-09-07 09:09:33 +00001959 // Promote the value if needed.
1960 switch (VA.getLocInfo()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001961 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001962 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001963 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1964 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001965 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1966 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001967 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001968 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001969 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001970 }
1971 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001972 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1973 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001974 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
1975 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001976 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001977 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001978 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001979 }
1980 case CCValAssign::AExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001981 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1982 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001983 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
1984 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001985 if (!Emitted)
1986 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00001987 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00001988 if (!Emitted)
1989 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1990 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001991
Chris Lattnerc46ec642011-01-05 22:26:52 +00001992 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001993 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001994 break;
1995 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001996 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001997 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001998 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00001999 assert(BC != 0 && "Failed to emit a bitcast!");
2000 Arg = BC;
2001 ArgVT = VA.getLocVT();
2002 break;
2003 }
Chad Rosier36ec0ca2012-07-11 19:58:38 +00002004 case CCValAssign::VExt:
2005 // VExt has not been implemented, so this should be impossible to reach
2006 // for now. However, fallback to Selection DAG isel once implemented.
2007 return false;
2008 case CCValAssign::Indirect:
2009 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2010 // support this.
2011 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00002012 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002013
Evan Chengf3d4efe2008-09-07 09:09:33 +00002014 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00002015 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2016 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002017 RegArgs.push_back(VA.getLocReg());
2018 } else {
2019 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00002020 X86AddressMode AM;
Bill Wendlinga5e5ba62013-06-07 21:00:34 +00002021 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2022 getTargetMachine()->getRegisterInfo());
Michael Liaof0e06e82012-11-01 03:47:50 +00002023 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman0586d912008-09-10 20:11:02 +00002024 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00002025 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00002026 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002027
Eli Friedmanc0883452011-05-20 22:21:04 +00002028 if (Flags.isByVal()) {
2029 X86AddressMode SrcAM;
2030 SrcAM.Base.Reg = Arg;
2031 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2032 assert(Res && "memcpy length already checked!"); (void)Res;
2033 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2034 // If this is a really simple value, emit this with the Value* version
Nick Lewycky1f9c6862011-10-12 00:14:12 +00002035 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedmanc0883452011-05-20 22:21:04 +00002036 // as it can cause us to reevaluate the argument.
Lang Hamese4824712011-10-18 22:11:33 +00002037 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2038 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00002039 } else {
Lang Hamese4824712011-10-18 22:11:33 +00002040 if (!X86FastEmitStore(ArgVT, Arg, AM))
2041 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00002042 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00002043 }
2044 }
2045
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002046 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002047 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00002048 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00002049 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00002050 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2051 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002052 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002053
Eli Friedman37620462011-04-19 17:22:22 +00002054 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
2055 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002056 static const uint16_t XMMArgRegs[] = {
Eli Friedman37620462011-04-19 17:22:22 +00002057 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2058 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2059 };
2060 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
2061 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
2062 X86::AL).addImm(NumXMMRegs);
2063 }
2064
Evan Chengf3d4efe2008-09-07 09:09:33 +00002065 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00002066 MachineInstrBuilder MIB;
2067 if (CalleeOp) {
2068 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00002069 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00002070 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00002071 CallOpc = X86::CALL64r;
2072 else
2073 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00002074 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
2075 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002076
Chris Lattner51e8eab2009-07-09 06:34:26 +00002077 } else {
2078 // Direct call.
2079 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00002080 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00002081 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00002082 CallOpc = X86::CALL64pcrel32;
2083 else
2084 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002085
Chris Lattner51e8eab2009-07-09 06:34:26 +00002086 // See if we need any target-specific flags on the GV operand.
2087 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002088
Chris Lattner51e8eab2009-07-09 06:34:26 +00002089 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2090 // external symbols most go through the PLT in PIC mode. If the symbol
2091 // has hidden or protected visibility, or if it is static or local, then
2092 // we don't need to use the PLT - we can directly call it.
2093 if (Subtarget->isTargetELF() &&
2094 TM.getRelocationModel() == Reloc::PIC_ &&
2095 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2096 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002097 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00002098 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002099 (!Subtarget->getTargetTriple().isMacOSX() ||
2100 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00002101 // PC-relative references to external symbols should go through $stub,
2102 // unless we're building with the leopard linker or later, which
2103 // automatically synthesizes these stubs.
2104 OpFlags = X86II::MO_DARWIN_STUB;
2105 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002106
2107
Eli Friedman25255cb2011-06-10 23:39:36 +00002108 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
2109 if (MemIntName)
Eli Friedman8a37aba2011-06-11 01:55:07 +00002110 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedman25255cb2011-06-10 23:39:36 +00002111 else
2112 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00002113 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002114
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002115 // Add a register mask with the call-preserved registers.
2116 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2117 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2118
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +00002119 // Add an implicit use GOT pointer in EBX.
2120 if (Subtarget->isPICStyleGOT())
2121 MIB.addReg(X86::EBX, RegState::Implicit);
2122
2123 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
2124 MIB.addReg(X86::AL, RegState::Implicit);
2125
2126 // Add implicit physical register uses to the call.
2127 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2128 MIB.addReg(RegArgs[i], RegState::Implicit);
2129
Evan Chengf3d4efe2008-09-07 09:09:33 +00002130 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00002131 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolac338fe02012-07-25 15:42:45 +00002132 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohman84023e02010-07-10 09:00:22 +00002133 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00002134 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002135
Eli Friedman19515b42011-05-17 18:29:03 +00002136 // Build info for return calling conv lowering code.
2137 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2138 SmallVector<ISD::InputArg, 32> Ins;
2139 SmallVector<EVT, 4> RetTys;
2140 ComputeValueVTs(TLI, I->getType(), RetTys);
2141 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2142 EVT VT = RetTys[i];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00002143 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman19515b42011-05-17 18:29:03 +00002144 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2145 for (unsigned j = 0; j != NumRegs; ++j) {
2146 ISD::InputArg MyFlags;
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00002147 MyFlags.VT = RegisterVT;
Eli Friedman19515b42011-05-17 18:29:03 +00002148 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling034b94b2012-12-19 07:18:57 +00002149 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman19515b42011-05-17 18:29:03 +00002150 MyFlags.Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002151 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman19515b42011-05-17 18:29:03 +00002152 MyFlags.Flags.setZExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002153 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman19515b42011-05-17 18:29:03 +00002154 MyFlags.Flags.setInReg();
2155 Ins.push_back(MyFlags);
2156 }
2157 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00002158
Eli Friedman19515b42011-05-17 18:29:03 +00002159 // Now handle call return values.
2160 SmallVector<unsigned, 4> UsedRegs;
2161 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002162 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00002163 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00002164 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2165 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2166 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2167 EVT CopyVT = RVLocs[i].getValVT();
2168 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002169
Evan Chengf3d4efe2008-09-07 09:09:33 +00002170 // If this is a call to a function that returns an fp value on the x87 fp
2171 // stack, but where we prefer to use the value in xmm registers, copy it
2172 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00002173 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002174 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00002175 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002176 CopyVT = MVT::f80;
Craig Topperc9099502012-04-20 06:31:50 +00002177 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00002178 }
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002179 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
2180 CopyReg);
2181 } else {
2182 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2183 CopyReg).addReg(RVLocs[i].getLocReg());
2184 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Chengf3d4efe2008-09-07 09:09:33 +00002185 }
2186
Eli Friedman19515b42011-05-17 18:29:03 +00002187 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00002188 // Round the F80 the right size, which also moves to the appropriate xmm
2189 // register. This is accomplished by storing the F80 value in memory and
2190 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00002191 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00002192 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00002193 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00002194 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00002195 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2196 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00002197 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002198 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00002199 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00002200 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002201 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00002202 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00002203
Eli Friedman19515b42011-05-17 18:29:03 +00002204 if (RVLocs.size())
2205 UpdateValueMap(I, ResultReg, RVLocs.size());
2206
Dan Gohmandb497122010-06-18 23:28:01 +00002207 // Set all unused physreg defs as dead.
2208 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2209
Evan Chengf3d4efe2008-09-07 09:09:33 +00002210 return true;
2211}
2212
2213
Dan Gohman99b21822008-08-28 23:21:34 +00002214bool
Dan Gohman46510a72010-04-15 01:51:59 +00002215X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00002216 switch (I->getOpcode()) {
2217 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00002218 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00002219 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00002220 case Instruction::Store:
2221 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00002222 case Instruction::Ret:
2223 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00002224 case Instruction::ICmp:
2225 case Instruction::FCmp:
2226 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00002227 case Instruction::ZExt:
2228 return X86SelectZExt(I);
2229 case Instruction::Br:
2230 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002231 case Instruction::Call:
2232 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00002233 case Instruction::LShr:
2234 case Instruction::AShr:
2235 case Instruction::Shl:
2236 return X86SelectShift(I);
Eli Bendersky50125482013-04-17 20:10:13 +00002237 case Instruction::SDiv:
2238 case Instruction::UDiv:
2239 case Instruction::SRem:
2240 case Instruction::URem:
2241 return X86SelectDivRem(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00002242 case Instruction::Select:
2243 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00002244 case Instruction::Trunc:
2245 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00002246 case Instruction::FPExt:
2247 return X86SelectFPExt(I);
2248 case Instruction::FPTrunc:
2249 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00002250 case Instruction::IntToPtr: // Deliberate fall-through.
2251 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00002252 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2253 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00002254 if (DstVT.bitsGT(SrcVT))
2255 return X86SelectZExt(I);
2256 if (DstVT.bitsLT(SrcVT))
2257 return X86SelectTrunc(I);
2258 unsigned Reg = getRegForValue(I->getOperand(0));
2259 if (Reg == 0) return false;
2260 UpdateValueMap(I, Reg);
2261 return true;
2262 }
Dan Gohman99b21822008-08-28 23:21:34 +00002263 }
2264
2265 return false;
2266}
2267
Dan Gohman46510a72010-04-15 01:51:59 +00002268unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002269 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00002270 if (!isTypeLegal(C->getType(), VT))
Michael Liaofaa11592012-08-30 00:30:16 +00002271 return 0;
2272
2273 // Can't handle alternate code models yet.
2274 if (TM.getCodeModel() != CodeModel::Small)
2275 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002276
Owen Anderson95267a12008-09-05 00:06:23 +00002277 // Get opcode and regclass of the output for the given load instruction.
2278 unsigned Opc = 0;
2279 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002280 switch (VT.SimpleTy) {
Michael Liaofaa11592012-08-30 00:30:16 +00002281 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00002282 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00002283 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +00002284 RC = &X86::GR8RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002285 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002286 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00002287 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +00002288 RC = &X86::GR16RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002289 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002290 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00002291 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +00002292 RC = &X86::GR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002293 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002294 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00002295 // Must be in x86-64 mode.
2296 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +00002297 RC = &X86::GR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002298 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002299 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002300 if (X86ScalarSSEf32) {
2301 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +00002302 RC = &X86::FR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002303 } else {
2304 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +00002305 RC = &X86::RFP32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002306 }
2307 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002308 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002309 if (X86ScalarSSEf64) {
2310 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +00002311 RC = &X86::FR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002312 } else {
2313 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +00002314 RC = &X86::RFP64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002315 }
2316 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002317 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00002318 // No f80 support yet.
Michael Liaofaa11592012-08-30 00:30:16 +00002319 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002320 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002321
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002322 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00002323 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002324 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002325 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00002326 // If the expression is just a basereg, then we're done, otherwise we need
2327 // to emit an LEA.
2328 if (AM.BaseType == X86AddressMode::RegBase &&
2329 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2330 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00002331
Chris Lattner685090f2011-04-17 17:12:08 +00002332 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002333 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002334 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2335 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00002336 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002337 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002338 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002339 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002340
Owen Anderson3b217c62008-09-06 01:11:01 +00002341 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002342 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002343 if (Align == 0) {
2344 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002345 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002346 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002347
Dan Gohman5396c992008-09-30 01:21:32 +00002348 // x86-32 PIC requires a PIC base register for constant pools.
2349 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002350 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002351 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002352 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002353 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002354 } else if (Subtarget->isPICStyleGOT()) {
2355 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002356 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002357 } else if (Subtarget->isPICStyleRIPRel() &&
2358 TM.getCodeModel() == CodeModel::Small) {
2359 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002360 }
Dan Gohman5396c992008-09-30 01:21:32 +00002361
2362 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002363 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002364 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002365 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2366 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002367 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002368
Owen Anderson95267a12008-09-05 00:06:23 +00002369 return ResultReg;
2370}
2371
Dan Gohman46510a72010-04-15 01:51:59 +00002372unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002373 // Fail on dynamic allocas. At this point, getRegForValue has already
2374 // checked its CSE maps, so if we're here trying to handle a dynamic
2375 // alloca, we're not going to succeed. X86SelectAddress has a
2376 // check for dynamic allocas, because it's called directly from
2377 // various places, but TargetMaterializeAlloca also needs a check
2378 // in order to avoid recursion between getRegForValue,
2379 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002380 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002381 return 0;
2382
Dan Gohman0586d912008-09-10 20:11:02 +00002383 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002384 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002385 return 0;
2386 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper44d23822012-02-22 05:59:10 +00002387 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman0586d912008-09-10 20:11:02 +00002388 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002389 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2390 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002391 return ResultReg;
2392}
2393
Eli Friedman2790ba82011-04-27 22:41:55 +00002394unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2395 MVT VT;
2396 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002397 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002398
2399 // Get opcode and regclass for the given zero.
2400 unsigned Opc = 0;
2401 const TargetRegisterClass *RC = NULL;
2402 switch (VT.SimpleTy) {
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002403 default: return 0;
Craig Topperf4cfc442012-08-11 17:53:00 +00002404 case MVT::f32:
2405 if (X86ScalarSSEf32) {
2406 Opc = X86::FsFLD0SS;
2407 RC = &X86::FR32RegClass;
2408 } else {
2409 Opc = X86::LD_Fp032;
2410 RC = &X86::RFP32RegClass;
2411 }
2412 break;
2413 case MVT::f64:
2414 if (X86ScalarSSEf64) {
2415 Opc = X86::FsFLD0SD;
2416 RC = &X86::FR64RegClass;
2417 } else {
2418 Opc = X86::LD_Fp064;
2419 RC = &X86::RFP64RegClass;
2420 }
2421 break;
2422 case MVT::f80:
2423 // No f80 support yet.
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002424 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002425 }
2426
2427 unsigned ResultReg = createResultReg(RC);
2428 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2429 return ResultReg;
2430}
2431
2432
Eli Bendersky75299e32013-04-19 22:29:18 +00002433bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2434 const LoadInst *LI) {
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002435 X86AddressMode AM;
2436 if (!X86SelectAddress(LI->getOperand(0), AM))
2437 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002438
Craig Topperdca72542012-08-11 17:46:16 +00002439 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002440
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002441 unsigned Size = TD.getTypeAllocSize(LI->getType());
2442 unsigned Alignment = LI->getAlignment();
2443
2444 SmallVector<MachineOperand, 8> AddrOps;
2445 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002446
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002447 MachineInstr *Result =
2448 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2449 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002450
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002451 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002452 MI->eraseFromParent();
2453 return true;
2454}
2455
2456
Evan Chengc3f44b02008-09-03 00:03:49 +00002457namespace llvm {
Bob Wilsond49edb72012-08-03 04:06:28 +00002458 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2459 const TargetLibraryInfo *libInfo) {
2460 return new X86FastISel(funcInfo, libInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002461 }
Dan Gohman99b21822008-08-28 23:21:34 +00002462}