blob: f8f06f60164dff12c060e887f26a182d4482e620 [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);
Jim Grosbachcc64dc62013-07-09 02:07:25 +00001379 // For i8 remainder, we can't reference AH directly, as we'll end
1380 // up with bogus copies like %R9B = COPY %AH. Reference AX
1381 // instead to prevent AH references in a REX instruction.
1382 //
1383 // The current assumption of the fast register allocator is that isel
1384 // won't generate explicit references to the GPR8_NOREX registers. If
1385 // the allocator and/or the backend get enhanced to be more robust in
1386 // that regard, this can be, and should be, removed.
1387 unsigned ResultReg = 0;
1388 if ((I->getOpcode() == Instruction::SRem ||
1389 I->getOpcode() == Instruction::URem) &&
1390 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1391 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1392 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
1393 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1394 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1395
1396 // Shift AX right by 8 bits instead of using AH.
1397 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SHR16ri),
1398 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1399
1400 // Now reference the 8-bit subreg of the result.
1401 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1402 /*Kill=*/true, X86::sub_8bit);
1403 }
1404 // Copy the result out of the physreg if we haven't already.
1405 if (!ResultReg) {
1406 ResultReg = createResultReg(TypeEntry.RC);
1407 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Copy), ResultReg)
1408 .addReg(OpEntry.DivRemResultReg);
1409 }
Eli Bendersky50125482013-04-17 20:10:13 +00001410 UpdateValueMap(I, ResultReg);
1411
1412 return true;
1413}
1414
Dan Gohman46510a72010-04-15 01:51:59 +00001415bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001416 MVT VT;
1417 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001418 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001419
Eric Christophere487b012010-09-29 23:00:29 +00001420 // We only use cmov here, if we don't have a cmov instruction bail.
1421 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001422
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001423 unsigned Opc = 0;
1424 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001425 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001426 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001427 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001428 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001429 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001430 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001431 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001432 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001433 RC = &X86::GR64RegClass;
1434 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001435 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001436 }
1437
1438 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1439 if (Op0Reg == 0) return false;
1440 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1441 if (Op1Reg == 0) return false;
1442 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1443 if (Op2Reg == 0) return false;
1444
Dan Gohman84023e02010-07-10 09:00:22 +00001445 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1446 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001447 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001448 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1449 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001450 UpdateValueMap(I, ResultReg);
1451 return true;
1452}
1453
Dan Gohman46510a72010-04-15 01:51:59 +00001454bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001455 // fpext from float to double.
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001456 if (X86ScalarSSEf64 &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001457 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001458 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001459 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001460 unsigned OpReg = getRegForValue(V);
1461 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001462 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001463 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1464 TII.get(X86::CVTSS2SDrr), ResultReg)
1465 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001466 UpdateValueMap(I, ResultReg);
1467 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001468 }
1469 }
1470
1471 return false;
1472}
1473
Dan Gohman46510a72010-04-15 01:51:59 +00001474bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001475 if (X86ScalarSSEf64) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001476 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001477 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001478 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001479 unsigned OpReg = getRegForValue(V);
1480 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001481 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001482 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1483 TII.get(X86::CVTSD2SSrr), ResultReg)
1484 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001485 UpdateValueMap(I, ResultReg);
1486 return true;
1487 }
1488 }
1489 }
1490
1491 return false;
1492}
1493
Dan Gohman46510a72010-04-15 01:51:59 +00001494bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001495 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1496 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001497
Eli Friedman76927d732011-05-25 23:49:02 +00001498 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001499 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001500 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001501 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001502 return false;
1503
1504 unsigned InputReg = getRegForValue(I->getOperand(0));
1505 if (!InputReg)
1506 // Unhandled operand. Halt "fast" selection and bail.
1507 return false;
1508
Eli Friedman76927d732011-05-25 23:49:02 +00001509 if (SrcVT == MVT::i8) {
1510 // Truncate from i8 to i1; no code needed.
1511 UpdateValueMap(I, InputReg);
1512 return true;
1513 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001514
Eli Friedman76927d732011-05-25 23:49:02 +00001515 if (!Subtarget->is64Bit()) {
1516 // If we're on x86-32; we can't extract an i8 from a general register.
1517 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperc9099502012-04-20 06:31:50 +00001518 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1519 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1520 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedman76927d732011-05-25 23:49:02 +00001521 unsigned CopyReg = createResultReg(CopyRC);
1522 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1523 CopyReg).addReg(InputReg);
1524 InputReg = CopyReg;
1525 }
1526
1527 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001529 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001530 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001531 if (!ResultReg)
1532 return false;
1533
1534 UpdateValueMap(I, ResultReg);
1535 return true;
1536}
1537
Eli Friedmanc0883452011-05-20 22:21:04 +00001538bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1539 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1540}
1541
Eli Friedmand5089a92011-04-27 01:45:07 +00001542bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1543 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001544
Eli Friedmand5089a92011-04-27 01:45:07 +00001545 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001546 if (!IsMemcpySmall(Len))
1547 return false;
1548
1549 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001550
1551 // We don't care about alignment here since we just emit integer accesses.
1552 while (Len) {
1553 MVT VT;
1554 if (Len >= 8 && i64Legal)
1555 VT = MVT::i64;
1556 else if (Len >= 4)
1557 VT = MVT::i32;
1558 else if (Len >= 2)
1559 VT = MVT::i16;
1560 else {
Eli Friedmand5089a92011-04-27 01:45:07 +00001561 VT = MVT::i8;
1562 }
1563
1564 unsigned Reg;
1565 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1566 RV &= X86FastEmitStore(VT, Reg, DestAM);
1567 assert(RV && "Failed to emit load or store??");
1568
1569 unsigned Size = VT.getSizeInBits()/8;
1570 Len -= Size;
1571 DestAM.Disp += Size;
1572 SrcAM.Disp += Size;
1573 }
1574
1575 return true;
1576}
1577
Dan Gohman46510a72010-04-15 01:51:59 +00001578bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001579 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001580 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001581 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001582 case Intrinsic::memcpy: {
1583 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1584 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001585 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001586 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001587
Eli Friedman25255cb2011-06-10 23:39:36 +00001588 if (isa<ConstantInt>(MCI.getLength())) {
1589 // Small memcpy's are common enough that we want to do them
1590 // without a call if possible.
1591 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1592 if (IsMemcpySmall(Len)) {
1593 X86AddressMode DestAM, SrcAM;
1594 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1595 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1596 return false;
1597 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1598 return true;
1599 }
1600 }
Eric Christopher471e4222011-06-08 23:55:35 +00001601
Eli Friedman25255cb2011-06-10 23:39:36 +00001602 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1603 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001604 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001605
Eli Friedman25255cb2011-06-10 23:39:36 +00001606 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1607 return false;
1608
1609 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001610 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001611 case Intrinsic::memset: {
1612 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001613
Nick Lewycky3207c9a2011-08-02 00:40:16 +00001614 if (MSI.isVolatile())
1615 return false;
1616
Eli Friedman25255cb2011-06-10 23:39:36 +00001617 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1618 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1619 return false;
1620
1621 if (MSI.getDestAddressSpace() > 255)
1622 return false;
1623
1624 return DoSelectCall(&I, "memset");
1625 }
Eric Christopher07754c22010-03-18 20:27:26 +00001626 case Intrinsic::stackprotector: {
Chad Rosiere1093e52012-05-11 19:43:29 +00001627 // Emit code to store the stack guard onto the stack.
Eric Christopher07754c22010-03-18 20:27:26 +00001628 EVT PtrTy = TLI.getPointerTy();
1629
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001630 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1631 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001632
1633 // Grab the frame index.
1634 X86AddressMode AM;
1635 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001636 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001637 return true;
1638 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001639 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001640 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001641 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001642 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001643 if (!X86SelectAddress(DI->getAddress(), AM))
1644 return false;
Evan Chenge837dea2011-06-28 19:10:37 +00001645 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001646 // FIXME may need to add RegState::Debug to any registers produced,
1647 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001648 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1649 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001650 return true;
1651 }
Eric Christopher77f79892010-01-18 22:11:29 +00001652 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001653 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001654 return true;
1655 }
Bill Wendling52370a12008-12-09 02:42:50 +00001656 case Intrinsic::sadd_with_overflow:
1657 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001658 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001659
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001660 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001661 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001662 const Function *Callee = I.getCalledFunction();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001663 Type *RetTy =
Bill Wendling52370a12008-12-09 02:42:50 +00001664 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1665
Duncan Sands1440e8b2010-11-03 11:35:31 +00001666 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001667 if (!isTypeLegal(RetTy, VT))
1668 return false;
1669
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001670 const Value *Op1 = I.getArgOperand(0);
1671 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001672 unsigned Reg1 = getRegForValue(Op1);
1673 unsigned Reg2 = getRegForValue(Op2);
1674
1675 if (Reg1 == 0 || Reg2 == 0)
1676 // FIXME: Handle values *not* in registers.
1677 return false;
1678
1679 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001680 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001681 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001682 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001683 OpC = X86::ADD64rr;
1684 else
1685 return false;
1686
Eli Friedman482feb32011-05-16 21:06:17 +00001687 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001688 // both the returned values.
Eli Friedman482feb32011-05-16 21:06:17 +00001689 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001690 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1691 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001692
Chris Lattnera9a42252009-04-12 07:36:01 +00001693 unsigned Opc = X86::SETBr;
1694 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1695 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001696 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1697
1698 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001699 return true;
1700 }
1701 }
1702}
1703
Chad Rosierfd3417d2013-02-25 21:59:35 +00001704bool X86FastISel::FastLowerArguments() {
1705 if (!FuncInfo.CanLowerReturn)
1706 return false;
1707
Chad Rosier146b8c22013-04-02 16:31:41 +00001708 if (Subtarget->isTargetWin64())
Chad Rosierd9b306a2013-03-14 21:25:04 +00001709 return false;
1710
Chad Rosierfd3417d2013-02-25 21:59:35 +00001711 const Function *F = FuncInfo.Fn;
1712 if (F->isVarArg())
1713 return false;
1714
1715 CallingConv::ID CC = F->getCallingConv();
1716 if (CC != CallingConv::C)
1717 return false;
1718
1719 if (!Subtarget->is64Bit())
1720 return false;
1721
1722 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
1723 unsigned Idx = 1;
1724 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1725 I != E; ++I, ++Idx) {
1726 if (Idx > 6)
1727 return false;
1728
1729 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1730 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1731 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1732 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1733 return false;
1734
1735 Type *ArgTy = I->getType();
1736 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1737 return false;
1738
1739 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosierfe88aa02013-02-26 01:05:31 +00001740 if (!ArgVT.isSimple()) return false;
Chad Rosierfd3417d2013-02-25 21:59:35 +00001741 switch (ArgVT.getSimpleVT().SimpleTy) {
1742 case MVT::i32:
1743 case MVT::i64:
1744 break;
1745 default:
1746 return false;
1747 }
1748 }
1749
1750 static const uint16_t GPR32ArgRegs[] = {
1751 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1752 };
1753 static const uint16_t GPR64ArgRegs[] = {
1754 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
1755 };
1756
1757 Idx = 0;
1758 const TargetRegisterClass *RC32 = TLI.getRegClassFor(MVT::i32);
1759 const TargetRegisterClass *RC64 = TLI.getRegClassFor(MVT::i64);
1760 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1761 I != E; ++I, ++Idx) {
Chad Rosierfd3417d2013-02-25 21:59:35 +00001762 bool is32Bit = TLI.getValueType(I->getType()) == MVT::i32;
1763 const TargetRegisterClass *RC = is32Bit ? RC32 : RC64;
1764 unsigned SrcReg = is32Bit ? GPR32ArgRegs[Idx] : GPR64ArgRegs[Idx];
1765 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1766 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1767 // Without this, EmitLiveInCopies may eliminate the livein if its only
1768 // use is a bitcast (which isn't turned into an instruction).
1769 unsigned ResultReg = createResultReg(RC);
1770 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1771 ResultReg).addReg(DstReg, getKillRegState(true));
1772 UpdateValueMap(I, ResultReg);
1773 }
1774 return true;
1775}
1776
Dan Gohman46510a72010-04-15 01:51:59 +00001777bool X86FastISel::X86SelectCall(const Instruction *I) {
1778 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001779 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001780
1781 // Can't handle inline asm yet.
1782 if (isa<InlineAsm>(Callee))
1783 return false;
1784
Bill Wendling52370a12008-12-09 02:42:50 +00001785 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001786 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001787 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001788
Chad Rosier425e9512012-12-11 00:18:02 +00001789 // Allow SelectionDAG isel to handle tail calls.
1790 if (cast<CallInst>(I)->isTailCall())
1791 return false;
1792
Eli Friedman25255cb2011-06-10 23:39:36 +00001793 return DoSelectCall(I, 0);
1794}
1795
Rafael Espindolac338fe02012-07-25 15:42:45 +00001796static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1797 const ImmutableCallSite &CS) {
Rafael Espindola742f2c92012-07-25 13:35:45 +00001798 if (Subtarget.is64Bit())
1799 return 0;
1800 if (Subtarget.isTargetWindows())
1801 return 0;
1802 CallingConv::ID CC = CS.getCallingConv();
1803 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1804 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001805 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola742f2c92012-07-25 13:35:45 +00001806 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001807 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola1cee7102012-07-25 13:41:10 +00001808 return 0;
Rafael Espindola742f2c92012-07-25 13:35:45 +00001809 return 4;
1810}
1811
Eli Friedman25255cb2011-06-10 23:39:36 +00001812// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1813bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1814 const CallInst *CI = cast<CallInst>(I);
1815 const Value *Callee = CI->getCalledValue();
1816
Evan Chengf3d4efe2008-09-07 09:09:33 +00001817 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001818 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001819 CallingConv::ID CC = CS.getCallingConv();
Chris Lattnere03b8d32011-04-19 04:42:38 +00001820 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Evan Chengf3d4efe2008-09-07 09:09:33 +00001821 CC != CallingConv::X86_FastCall)
1822 return false;
1823
Evan Cheng381993f2010-01-27 00:00:57 +00001824 // fastcc with -tailcallopt is intended to provide a guaranteed
1825 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001826 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001827 return false;
1828
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001829 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1830 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001831 bool isVarArg = FTy->isVarArg();
1832
1833 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1834 // x86-32. Special handling for x86-64 is implemented.
1835 if (isVarArg && Subtarget->isTargetWin64())
Evan Chengf3d4efe2008-09-07 09:09:33 +00001836 return false;
1837
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001838 // Fast-isel doesn't know about callee-pop yet.
Evan Chengef41ff62011-06-23 17:54:54 +00001839 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001840 TM.Options.GuaranteedTailCallOpt))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001841 return false;
1842
Eli Friedman19515b42011-05-17 18:29:03 +00001843 // Check whether the function can return without sret-demotion.
1844 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001845 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman19515b42011-05-17 18:29:03 +00001846 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendling56cb2292012-07-19 00:11:40 +00001847 *FuncInfo.MF, FTy->isVarArg(),
1848 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001849 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001850 return false;
1851
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001852 // Materialize callee address in a register. FIXME: GV address can be
1853 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001854 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001855 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001856 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001857 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001858 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001859 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001860 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001861 } else if (CalleeAM.Base.Reg != 0) {
1862 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001863 } else
1864 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001865
Evan Chengf3d4efe2008-09-07 09:09:33 +00001866 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001867 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001868 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001869 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001870 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosier15b44972012-02-15 00:36:26 +00001871 unsigned arg_size = CS.arg_size();
1872 Args.reserve(arg_size);
1873 ArgVals.reserve(arg_size);
1874 ArgVTs.reserve(arg_size);
1875 ArgFlags.reserve(arg_size);
Dan Gohman46510a72010-04-15 01:51:59 +00001876 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001877 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001878 // If we're lowering a mem intrinsic instead of a regular call, skip the
1879 // last two arguments, which should not passed to the underlying functions.
1880 if (MemIntName && e-i <= 2)
1881 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001882 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001883 ISD::ArgFlagsTy Flags;
1884 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling034b94b2012-12-19 07:18:57 +00001885 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001886 Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00001887 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001888 Flags.setZExt();
1889
Bill Wendling034b94b2012-12-19 07:18:57 +00001890 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001891 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1892 Type *ElementTy = Ty->getElementType();
Eli Friedmanc0883452011-05-20 22:21:04 +00001893 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1894 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1895 if (!FrameAlign)
1896 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1897 Flags.setByVal();
1898 Flags.setByValSize(FrameSize);
1899 Flags.setByValAlign(FrameAlign);
1900 if (!IsMemcpySmall(FrameSize))
1901 return false;
1902 }
1903
Bill Wendling034b94b2012-12-19 07:18:57 +00001904 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedmanc0883452011-05-20 22:21:04 +00001905 Flags.setInReg();
Bill Wendling034b94b2012-12-19 07:18:57 +00001906 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedmanc0883452011-05-20 22:21:04 +00001907 Flags.setNest();
1908
Chris Lattnere03b8d32011-04-19 04:42:38 +00001909 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1910 // instruction. This is safe because it is common to all fastisel supported
1911 // calling conventions on x86.
1912 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1913 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1914 CI->getBitWidth() == 16) {
1915 if (Flags.isSExt())
1916 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1917 else
1918 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1919 }
1920 }
Eric Christopher471e4222011-06-08 23:55:35 +00001921
Chris Lattnerb44101c2011-04-19 05:09:50 +00001922 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001923
Chris Lattnerff009ad2011-04-19 05:15:59 +00001924 // Passing bools around ends up doing a trunc to i1 and passing it.
1925 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001926 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1927 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1928 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001929 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1930 ArgReg = getRegForValue(ArgVal);
1931 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001932
Chris Lattnerb44101c2011-04-19 05:09:50 +00001933 MVT ArgVT;
1934 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001935
Chris Lattnerb44101c2011-04-19 05:09:50 +00001936 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1937 ArgVal->hasOneUse(), 1);
1938 } else {
1939 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001940 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001941
Chris Lattnerff009ad2011-04-19 05:15:59 +00001942 if (ArgReg == 0) return false;
1943
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001944 Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001945 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001946 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001947 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001948 if (ArgVT == MVT::x86mmx)
1949 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001950 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1951 Flags.setOrigAlign(OriginalAlignment);
1952
Chris Lattnerb44101c2011-04-19 05:09:50 +00001953 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001954 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001955 ArgVTs.push_back(ArgVT);
1956 ArgFlags.push_back(Flags);
1957 }
1958
1959 // Analyze operands of the call, assigning locations to each operand.
1960 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001961 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00001962 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001963
Dan Gohmand8acddd2010-06-01 21:09:47 +00001964 // Allocate shadow area for Win64
Chris Lattnere03b8d32011-04-19 04:42:38 +00001965 if (Subtarget->isTargetWin64())
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001966 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001967
Duncan Sands45907662010-10-31 13:21:44 +00001968 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001969
1970 // Get a count of how many bytes are to be pushed on the stack.
1971 unsigned NumBytes = CCInfo.getNextStackOffset();
1972
1973 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00001974 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00001975 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
1976 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001977
Chris Lattner438949a2008-10-15 05:30:52 +00001978 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00001979 // copies / loads.
1980 SmallVector<unsigned, 4> RegArgs;
1981 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1982 CCValAssign &VA = ArgLocs[i];
1983 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00001984 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001985
Evan Chengf3d4efe2008-09-07 09:09:33 +00001986 // Promote the value if needed.
1987 switch (VA.getLocInfo()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00001988 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001989 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001990 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
1991 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00001992 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
1993 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00001994 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00001995 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001996 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00001997 }
1998 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00001999 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2000 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00002001 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2002 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00002003 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00002004 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00002005 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00002006 }
2007 case CCValAssign::AExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00002008 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2009 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00002010 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2011 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00002012 if (!Emitted)
2013 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00002014 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00002015 if (!Emitted)
2016 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2017 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002018
Chris Lattnerc46ec642011-01-05 22:26:52 +00002019 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00002020 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00002021 break;
2022 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00002023 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002024 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002025 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00002026 assert(BC != 0 && "Failed to emit a bitcast!");
2027 Arg = BC;
2028 ArgVT = VA.getLocVT();
2029 break;
2030 }
Chad Rosier36ec0ca2012-07-11 19:58:38 +00002031 case CCValAssign::VExt:
2032 // VExt has not been implemented, so this should be impossible to reach
2033 // for now. However, fallback to Selection DAG isel once implemented.
2034 return false;
2035 case CCValAssign::Indirect:
2036 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2037 // support this.
2038 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00002039 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002040
Evan Chengf3d4efe2008-09-07 09:09:33 +00002041 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00002042 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2043 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002044 RegArgs.push_back(VA.getLocReg());
2045 } else {
2046 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00002047 X86AddressMode AM;
Bill Wendlinga5e5ba62013-06-07 21:00:34 +00002048 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2049 getTargetMachine()->getRegisterInfo());
Michael Liaof0e06e82012-11-01 03:47:50 +00002050 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman0586d912008-09-10 20:11:02 +00002051 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00002052 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00002053 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002054
Eli Friedmanc0883452011-05-20 22:21:04 +00002055 if (Flags.isByVal()) {
2056 X86AddressMode SrcAM;
2057 SrcAM.Base.Reg = Arg;
2058 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2059 assert(Res && "memcpy length already checked!"); (void)Res;
2060 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2061 // If this is a really simple value, emit this with the Value* version
Nick Lewycky1f9c6862011-10-12 00:14:12 +00002062 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedmanc0883452011-05-20 22:21:04 +00002063 // as it can cause us to reevaluate the argument.
Lang Hamese4824712011-10-18 22:11:33 +00002064 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2065 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00002066 } else {
Lang Hamese4824712011-10-18 22:11:33 +00002067 if (!X86FastEmitStore(ArgVT, Arg, AM))
2068 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00002069 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00002070 }
2071 }
2072
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002073 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002074 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00002075 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00002076 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00002077 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2078 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002079 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002080
Eli Friedman37620462011-04-19 17:22:22 +00002081 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64()) {
2082 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002083 static const uint16_t XMMArgRegs[] = {
Eli Friedman37620462011-04-19 17:22:22 +00002084 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2085 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2086 };
2087 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
2088 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
2089 X86::AL).addImm(NumXMMRegs);
2090 }
2091
Evan Chengf3d4efe2008-09-07 09:09:33 +00002092 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00002093 MachineInstrBuilder MIB;
2094 if (CalleeOp) {
2095 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00002096 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00002097 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00002098 CallOpc = X86::CALL64r;
2099 else
2100 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00002101 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
2102 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002103
Chris Lattner51e8eab2009-07-09 06:34:26 +00002104 } else {
2105 // Direct call.
2106 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00002107 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00002108 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00002109 CallOpc = X86::CALL64pcrel32;
2110 else
2111 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002112
Chris Lattner51e8eab2009-07-09 06:34:26 +00002113 // See if we need any target-specific flags on the GV operand.
2114 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002115
Chris Lattner51e8eab2009-07-09 06:34:26 +00002116 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2117 // external symbols most go through the PLT in PIC mode. If the symbol
2118 // has hidden or protected visibility, or if it is static or local, then
2119 // we don't need to use the PLT - we can directly call it.
2120 if (Subtarget->isTargetELF() &&
2121 TM.getRelocationModel() == Reloc::PIC_ &&
2122 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2123 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002124 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00002125 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002126 (!Subtarget->getTargetTriple().isMacOSX() ||
2127 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00002128 // PC-relative references to external symbols should go through $stub,
2129 // unless we're building with the leopard linker or later, which
2130 // automatically synthesizes these stubs.
2131 OpFlags = X86II::MO_DARWIN_STUB;
2132 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002133
2134
Eli Friedman25255cb2011-06-10 23:39:36 +00002135 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
2136 if (MemIntName)
Eli Friedman8a37aba2011-06-11 01:55:07 +00002137 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedman25255cb2011-06-10 23:39:36 +00002138 else
2139 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00002140 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002141
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002142 // Add a register mask with the call-preserved registers.
2143 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2144 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2145
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +00002146 // Add an implicit use GOT pointer in EBX.
2147 if (Subtarget->isPICStyleGOT())
2148 MIB.addReg(X86::EBX, RegState::Implicit);
2149
2150 if (Subtarget->is64Bit() && isVarArg && !Subtarget->isTargetWin64())
2151 MIB.addReg(X86::AL, RegState::Implicit);
2152
2153 // Add implicit physical register uses to the call.
2154 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2155 MIB.addReg(RegArgs[i], RegState::Implicit);
2156
Evan Chengf3d4efe2008-09-07 09:09:33 +00002157 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00002158 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolac338fe02012-07-25 15:42:45 +00002159 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohman84023e02010-07-10 09:00:22 +00002160 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00002161 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002162
Eli Friedman19515b42011-05-17 18:29:03 +00002163 // Build info for return calling conv lowering code.
2164 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2165 SmallVector<ISD::InputArg, 32> Ins;
2166 SmallVector<EVT, 4> RetTys;
2167 ComputeValueVTs(TLI, I->getType(), RetTys);
2168 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2169 EVT VT = RetTys[i];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00002170 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman19515b42011-05-17 18:29:03 +00002171 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2172 for (unsigned j = 0; j != NumRegs; ++j) {
2173 ISD::InputArg MyFlags;
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00002174 MyFlags.VT = RegisterVT;
Eli Friedman19515b42011-05-17 18:29:03 +00002175 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling034b94b2012-12-19 07:18:57 +00002176 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman19515b42011-05-17 18:29:03 +00002177 MyFlags.Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002178 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman19515b42011-05-17 18:29:03 +00002179 MyFlags.Flags.setZExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002180 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman19515b42011-05-17 18:29:03 +00002181 MyFlags.Flags.setInReg();
2182 Ins.push_back(MyFlags);
2183 }
2184 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00002185
Eli Friedman19515b42011-05-17 18:29:03 +00002186 // Now handle call return values.
2187 SmallVector<unsigned, 4> UsedRegs;
2188 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002189 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00002190 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00002191 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2192 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2193 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2194 EVT CopyVT = RVLocs[i].getValVT();
2195 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002196
Evan Chengf3d4efe2008-09-07 09:09:33 +00002197 // If this is a call to a function that returns an fp value on the x87 fp
2198 // stack, but where we prefer to use the value in xmm registers, copy it
2199 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00002200 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002201 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00002202 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002203 CopyVT = MVT::f80;
Craig Topperc9099502012-04-20 06:31:50 +00002204 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00002205 }
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002206 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
2207 CopyReg);
2208 } else {
2209 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2210 CopyReg).addReg(RVLocs[i].getLocReg());
2211 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Chengf3d4efe2008-09-07 09:09:33 +00002212 }
2213
Eli Friedman19515b42011-05-17 18:29:03 +00002214 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00002215 // Round the F80 the right size, which also moves to the appropriate xmm
2216 // register. This is accomplished by storing the F80 value in memory and
2217 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00002218 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00002219 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00002220 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00002221 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00002222 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2223 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00002224 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002225 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00002226 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00002227 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002228 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00002229 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00002230
Eli Friedman19515b42011-05-17 18:29:03 +00002231 if (RVLocs.size())
2232 UpdateValueMap(I, ResultReg, RVLocs.size());
2233
Dan Gohmandb497122010-06-18 23:28:01 +00002234 // Set all unused physreg defs as dead.
2235 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2236
Evan Chengf3d4efe2008-09-07 09:09:33 +00002237 return true;
2238}
2239
2240
Dan Gohman99b21822008-08-28 23:21:34 +00002241bool
Dan Gohman46510a72010-04-15 01:51:59 +00002242X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00002243 switch (I->getOpcode()) {
2244 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00002245 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00002246 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00002247 case Instruction::Store:
2248 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00002249 case Instruction::Ret:
2250 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00002251 case Instruction::ICmp:
2252 case Instruction::FCmp:
2253 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00002254 case Instruction::ZExt:
2255 return X86SelectZExt(I);
2256 case Instruction::Br:
2257 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002258 case Instruction::Call:
2259 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00002260 case Instruction::LShr:
2261 case Instruction::AShr:
2262 case Instruction::Shl:
2263 return X86SelectShift(I);
Eli Bendersky50125482013-04-17 20:10:13 +00002264 case Instruction::SDiv:
2265 case Instruction::UDiv:
2266 case Instruction::SRem:
2267 case Instruction::URem:
2268 return X86SelectDivRem(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00002269 case Instruction::Select:
2270 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00002271 case Instruction::Trunc:
2272 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00002273 case Instruction::FPExt:
2274 return X86SelectFPExt(I);
2275 case Instruction::FPTrunc:
2276 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00002277 case Instruction::IntToPtr: // Deliberate fall-through.
2278 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00002279 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2280 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00002281 if (DstVT.bitsGT(SrcVT))
2282 return X86SelectZExt(I);
2283 if (DstVT.bitsLT(SrcVT))
2284 return X86SelectTrunc(I);
2285 unsigned Reg = getRegForValue(I->getOperand(0));
2286 if (Reg == 0) return false;
2287 UpdateValueMap(I, Reg);
2288 return true;
2289 }
Dan Gohman99b21822008-08-28 23:21:34 +00002290 }
2291
2292 return false;
2293}
2294
Dan Gohman46510a72010-04-15 01:51:59 +00002295unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002296 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00002297 if (!isTypeLegal(C->getType(), VT))
Michael Liaofaa11592012-08-30 00:30:16 +00002298 return 0;
2299
2300 // Can't handle alternate code models yet.
2301 if (TM.getCodeModel() != CodeModel::Small)
2302 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002303
Owen Anderson95267a12008-09-05 00:06:23 +00002304 // Get opcode and regclass of the output for the given load instruction.
2305 unsigned Opc = 0;
2306 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002307 switch (VT.SimpleTy) {
Michael Liaofaa11592012-08-30 00:30:16 +00002308 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00002309 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00002310 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +00002311 RC = &X86::GR8RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002312 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002313 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00002314 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +00002315 RC = &X86::GR16RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002316 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002317 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00002318 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +00002319 RC = &X86::GR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002320 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002321 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00002322 // Must be in x86-64 mode.
2323 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +00002324 RC = &X86::GR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002325 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002326 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002327 if (X86ScalarSSEf32) {
2328 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +00002329 RC = &X86::FR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002330 } else {
2331 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +00002332 RC = &X86::RFP32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002333 }
2334 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002335 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002336 if (X86ScalarSSEf64) {
2337 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +00002338 RC = &X86::FR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002339 } else {
2340 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +00002341 RC = &X86::RFP64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002342 }
2343 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002344 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00002345 // No f80 support yet.
Michael Liaofaa11592012-08-30 00:30:16 +00002346 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002347 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002348
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002349 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00002350 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002351 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002352 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00002353 // If the expression is just a basereg, then we're done, otherwise we need
2354 // to emit an LEA.
2355 if (AM.BaseType == X86AddressMode::RegBase &&
2356 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2357 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00002358
Chris Lattner685090f2011-04-17 17:12:08 +00002359 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002360 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002361 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2362 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00002363 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002364 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002365 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002366 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002367
Owen Anderson3b217c62008-09-06 01:11:01 +00002368 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002369 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002370 if (Align == 0) {
2371 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002372 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002373 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002374
Dan Gohman5396c992008-09-30 01:21:32 +00002375 // x86-32 PIC requires a PIC base register for constant pools.
2376 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002377 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002378 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002379 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002380 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002381 } else if (Subtarget->isPICStyleGOT()) {
2382 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002383 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002384 } else if (Subtarget->isPICStyleRIPRel() &&
2385 TM.getCodeModel() == CodeModel::Small) {
2386 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002387 }
Dan Gohman5396c992008-09-30 01:21:32 +00002388
2389 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002390 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002391 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002392 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2393 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002394 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002395
Owen Anderson95267a12008-09-05 00:06:23 +00002396 return ResultReg;
2397}
2398
Dan Gohman46510a72010-04-15 01:51:59 +00002399unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002400 // Fail on dynamic allocas. At this point, getRegForValue has already
2401 // checked its CSE maps, so if we're here trying to handle a dynamic
2402 // alloca, we're not going to succeed. X86SelectAddress has a
2403 // check for dynamic allocas, because it's called directly from
2404 // various places, but TargetMaterializeAlloca also needs a check
2405 // in order to avoid recursion between getRegForValue,
2406 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002407 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002408 return 0;
2409
Dan Gohman0586d912008-09-10 20:11:02 +00002410 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002411 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002412 return 0;
2413 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper44d23822012-02-22 05:59:10 +00002414 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman0586d912008-09-10 20:11:02 +00002415 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002416 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2417 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002418 return ResultReg;
2419}
2420
Eli Friedman2790ba82011-04-27 22:41:55 +00002421unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2422 MVT VT;
2423 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002424 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002425
2426 // Get opcode and regclass for the given zero.
2427 unsigned Opc = 0;
2428 const TargetRegisterClass *RC = NULL;
2429 switch (VT.SimpleTy) {
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002430 default: return 0;
Craig Topperf4cfc442012-08-11 17:53:00 +00002431 case MVT::f32:
2432 if (X86ScalarSSEf32) {
2433 Opc = X86::FsFLD0SS;
2434 RC = &X86::FR32RegClass;
2435 } else {
2436 Opc = X86::LD_Fp032;
2437 RC = &X86::RFP32RegClass;
2438 }
2439 break;
2440 case MVT::f64:
2441 if (X86ScalarSSEf64) {
2442 Opc = X86::FsFLD0SD;
2443 RC = &X86::FR64RegClass;
2444 } else {
2445 Opc = X86::LD_Fp064;
2446 RC = &X86::RFP64RegClass;
2447 }
2448 break;
2449 case MVT::f80:
2450 // No f80 support yet.
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002451 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002452 }
2453
2454 unsigned ResultReg = createResultReg(RC);
2455 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2456 return ResultReg;
2457}
2458
2459
Eli Bendersky75299e32013-04-19 22:29:18 +00002460bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2461 const LoadInst *LI) {
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002462 X86AddressMode AM;
2463 if (!X86SelectAddress(LI->getOperand(0), AM))
2464 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002465
Craig Topperdca72542012-08-11 17:46:16 +00002466 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002467
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002468 unsigned Size = TD.getTypeAllocSize(LI->getType());
2469 unsigned Alignment = LI->getAlignment();
2470
2471 SmallVector<MachineOperand, 8> AddrOps;
2472 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002473
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002474 MachineInstr *Result =
2475 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2476 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002477
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002478 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002479 MI->eraseFromParent();
2480 return true;
2481}
2482
2483
Evan Chengc3f44b02008-09-03 00:03:49 +00002484namespace llvm {
Bob Wilsond49edb72012-08-03 04:06:28 +00002485 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2486 const TargetLibraryInfo *libInfo) {
2487 return new X86FastISel(funcInfo, libInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002488 }
Dan Gohman99b21822008-08-28 23:21:34 +00002489}