blob: 5afc002b0fb579f8d37c692095deca15733334bd [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
Craig Toppere0364b62013-07-17 05:57:45 +000082 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM,
83 bool Aligned = false);
84 bool X86FastEmitStore(EVT VT, unsigned ValReg, const X86AddressMode &AM,
85 bool Aligned = false);
Evan Cheng24e3a902008-09-08 06:35:17 +000086
Owen Andersone50ed302009-08-10 22:56:29 +000087 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +000088 unsigned &ResultReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000089
Dan Gohman46510a72010-04-15 01:51:59 +000090 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
91 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman0586d912008-09-10 20:11:02 +000092
Dan Gohman46510a72010-04-15 01:51:59 +000093 bool X86SelectLoad(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +000094
Dan Gohman46510a72010-04-15 01:51:59 +000095 bool X86SelectStore(const Instruction *I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +000096
Dan Gohman84023e02010-07-10 09:00:22 +000097 bool X86SelectRet(const Instruction *I);
98
Dan Gohman46510a72010-04-15 01:51:59 +000099 bool X86SelectCmp(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000100
Dan Gohman46510a72010-04-15 01:51:59 +0000101 bool X86SelectZExt(const Instruction *I);
Dan Gohmand89ae992008-09-05 01:06:14 +0000102
Dan Gohman46510a72010-04-15 01:51:59 +0000103 bool X86SelectBranch(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000104
Dan Gohman46510a72010-04-15 01:51:59 +0000105 bool X86SelectShift(const Instruction *I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +0000106
Eli Bendersky50125482013-04-17 20:10:13 +0000107 bool X86SelectDivRem(const Instruction *I);
108
Dan Gohman46510a72010-04-15 01:51:59 +0000109 bool X86SelectSelect(const Instruction *I);
Evan Cheng0de588f2008-09-05 21:00:03 +0000110
Dan Gohman46510a72010-04-15 01:51:59 +0000111 bool X86SelectTrunc(const Instruction *I);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000112
Dan Gohman46510a72010-04-15 01:51:59 +0000113 bool X86SelectFPExt(const Instruction *I);
114 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohman78efce62008-09-10 21:02:08 +0000115
Dan Gohman46510a72010-04-15 01:51:59 +0000116 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
117 bool X86SelectCall(const Instruction *I);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000118
Eli Friedman25255cb2011-06-10 23:39:36 +0000119 bool DoSelectCall(const Instruction *I, const char *MemIntName);
120
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000121 const X86InstrInfo *getInstrInfo() const {
Dan Gohman97135e12008-09-26 19:15:30 +0000122 return getTargetMachine()->getInstrInfo();
123 }
124 const X86TargetMachine *getTargetMachine() const {
125 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000126 }
127
Dan Gohman46510a72010-04-15 01:51:59 +0000128 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman0586d912008-09-10 20:11:02 +0000129
Dan Gohman46510a72010-04-15 01:51:59 +0000130 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000131
Eli Friedman2790ba82011-04-27 22:41:55 +0000132 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
133
Evan Chengf3d4efe2008-09-07 09:09:33 +0000134 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
135 /// computed in an SSE register, not on the X87 floating point stack.
Owen Andersone50ed302009-08-10 22:56:29 +0000136 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson825b72b2009-08-11 20:47:22 +0000137 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
138 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Chengf3d4efe2008-09-07 09:09:33 +0000139 }
140
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000141 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmand5089a92011-04-27 01:45:07 +0000142
Eli Friedmanc0883452011-05-20 22:21:04 +0000143 bool IsMemcpySmall(uint64_t Len);
144
Eli Friedmand5089a92011-04-27 01:45:07 +0000145 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
146 X86AddressMode SrcAM, uint64_t Len);
Evan Chengc3f44b02008-09-03 00:03:49 +0000147};
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000148
Chris Lattner087fcf32009-03-08 18:44:31 +0000149} // end anonymous namespace.
Dan Gohman99b21822008-08-28 23:21:34 +0000150
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000151bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sands1440e8b2010-11-03 11:35:31 +0000152 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
153 if (evt == MVT::Other || !evt.isSimple())
Evan Chengf3d4efe2008-09-07 09:09:33 +0000154 // Unhandled type. Halt "fast" selection and bail.
155 return false;
Duncan Sands1440e8b2010-11-03 11:35:31 +0000156
157 VT = evt.getSimpleVT();
Dan Gohman9b66d732008-09-30 00:48:39 +0000158 // For now, require SSE/SSE2 for performing floating-point operations,
159 // since x87 requires additional work.
Owen Anderson825b72b2009-08-11 20:47:22 +0000160 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topperf4cfc442012-08-11 17:53:00 +0000161 return false;
Owen Anderson825b72b2009-08-11 20:47:22 +0000162 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topperf4cfc442012-08-11 17:53:00 +0000163 return false;
Dan Gohman9b66d732008-09-30 00:48:39 +0000164 // Similarly, no f80 support yet.
Owen Anderson825b72b2009-08-11 20:47:22 +0000165 if (VT == MVT::f80)
Dan Gohman9b66d732008-09-30 00:48:39 +0000166 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +0000167 // We only handle legal types. For example, on x86-32 the instruction
168 // selector contains all of the 64-bit instructions from x86-64,
169 // under the assumption that i64 won't be used if the target doesn't
170 // support it.
Owen Anderson825b72b2009-08-11 20:47:22 +0000171 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Chengf3d4efe2008-09-07 09:09:33 +0000172}
173
174#include "X86GenCallingConv.inc"
175
Evan Cheng0de588f2008-09-05 21:00:03 +0000176/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Chengf3d4efe2008-09-07 09:09:33 +0000177/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Cheng0de588f2008-09-05 21:00:03 +0000178/// Return true and the result register by reference if it is possible.
Owen Andersone50ed302009-08-10 22:56:29 +0000179bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Cheng0de588f2008-09-05 21:00:03 +0000180 unsigned &ResultReg) {
181 // Get opcode and regclass of the output for the given load instruction.
182 unsigned Opc = 0;
183 const TargetRegisterClass *RC = NULL;
Owen Anderson825b72b2009-08-11 20:47:22 +0000184 switch (VT.getSimpleVT().SimpleTy) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000185 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000186 case MVT::i1:
Owen Anderson825b72b2009-08-11 20:47:22 +0000187 case MVT::i8:
Evan Cheng0de588f2008-09-05 21:00:03 +0000188 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +0000189 RC = &X86::GR8RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000190 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000191 case MVT::i16:
Evan Cheng0de588f2008-09-05 21:00:03 +0000192 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +0000193 RC = &X86::GR16RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000194 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000195 case MVT::i32:
Evan Cheng0de588f2008-09-05 21:00:03 +0000196 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000197 RC = &X86::GR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000198 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000199 case MVT::i64:
Evan Cheng0de588f2008-09-05 21:00:03 +0000200 // Must be in x86-64 mode.
201 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000202 RC = &X86::GR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000203 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000204 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000205 if (X86ScalarSSEf32) {
206 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +0000207 RC = &X86::FR32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000208 } else {
209 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +0000210 RC = &X86::RFP32RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000211 }
212 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000213 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000214 if (X86ScalarSSEf64) {
215 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +0000216 RC = &X86::FR64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000217 } else {
218 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +0000219 RC = &X86::RFP64RegClass;
Evan Cheng0de588f2008-09-05 21:00:03 +0000220 }
221 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000222 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +0000223 // No f80 support yet.
224 return false;
Evan Cheng0de588f2008-09-05 21:00:03 +0000225 }
226
227 ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +0000228 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
229 DL, TII.get(Opc), ResultReg), AM);
Evan Cheng0de588f2008-09-05 21:00:03 +0000230 return true;
231}
232
Evan Chengf3d4efe2008-09-07 09:09:33 +0000233/// X86FastEmitStore - Emit a machine instruction to store a value Val of
234/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
235/// and a displacement offset, or a GlobalAddress,
Evan Cheng0de588f2008-09-05 21:00:03 +0000236/// i.e. V. Return true if it is possible.
237bool
Craig Toppere0364b62013-07-17 05:57:45 +0000238X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg,
239 const X86AddressMode &AM, bool Aligned) {
Dan Gohman863890e2008-09-08 16:31:35 +0000240 // Get opcode and regclass of the output for the given store instruction.
Evan Cheng0de588f2008-09-05 21:00:03 +0000241 unsigned Opc = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000242 switch (VT.getSimpleVT().SimpleTy) {
243 case MVT::f80: // No f80 support yet.
Evan Cheng0de588f2008-09-05 21:00:03 +0000244 default: return false;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000245 case MVT::i1: {
246 // Mask out all but lowest bit.
Craig Topperc9099502012-04-20 06:31:50 +0000247 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000248 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Craig Toppere0364b62013-07-17 05:57:45 +0000249 TII.get(X86::AND8ri), AndResult).addReg(ValReg).addImm(1);
250 ValReg = AndResult;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000251 }
252 // FALLTHROUGH, handling i1 as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000253 case MVT::i8: Opc = X86::MOV8mr; break;
254 case MVT::i16: Opc = X86::MOV16mr; break;
255 case MVT::i32: Opc = X86::MOV32mr; break;
256 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
257 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000258 Opc = X86ScalarSSEf32 ?
259 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000260 break;
Owen Anderson825b72b2009-08-11 20:47:22 +0000261 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000262 Opc = X86ScalarSSEf64 ?
263 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Cheng0de588f2008-09-05 21:00:03 +0000264 break;
Lang Hamese4824712011-10-18 22:11:33 +0000265 case MVT::v4f32:
Craig Toppere0364b62013-07-17 05:57:45 +0000266 if (Aligned)
Craig Topper77c95b62013-07-17 06:58:23 +0000267 Opc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Craig Toppere0364b62013-07-17 05:57:45 +0000268 else
Craig Topper77c95b62013-07-17 06:58:23 +0000269 Opc = Subtarget->hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
Lang Hamese4824712011-10-18 22:11:33 +0000270 break;
271 case MVT::v2f64:
Craig Toppere0364b62013-07-17 05:57:45 +0000272 if (Aligned)
Craig Topperfe754512013-07-18 07:16:44 +0000273 Opc = Subtarget->hasAVX() ? X86::VMOVAPDmr : X86::MOVAPDmr;
Craig Toppere0364b62013-07-17 05:57:45 +0000274 else
Craig Topperfe754512013-07-18 07:16:44 +0000275 Opc = Subtarget->hasAVX() ? X86::VMOVUPDmr : X86::MOVUPDmr;
Lang Hamese4824712011-10-18 22:11:33 +0000276 break;
277 case MVT::v4i32:
278 case MVT::v2i64:
279 case MVT::v8i16:
280 case MVT::v16i8:
Craig Toppere0364b62013-07-17 05:57:45 +0000281 if (Aligned)
Craig Topper77c95b62013-07-17 06:58:23 +0000282 Opc = Subtarget->hasAVX() ? X86::VMOVDQAmr : X86::MOVDQAmr;
Craig Toppere0364b62013-07-17 05:57:45 +0000283 else
Craig Topper77c95b62013-07-17 06:58:23 +0000284 Opc = Subtarget->hasAVX() ? X86::VMOVDQUmr : X86::MOVDQUmr;
Lang Hamese4824712011-10-18 22:11:33 +0000285 break;
Evan Cheng0de588f2008-09-05 21:00:03 +0000286 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000287
Dan Gohman84023e02010-07-10 09:00:22 +0000288 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Craig Toppere0364b62013-07-17 05:57:45 +0000289 DL, TII.get(Opc)), AM).addReg(ValReg);
Evan Cheng0de588f2008-09-05 21:00:03 +0000290 return true;
291}
292
Dan Gohman46510a72010-04-15 01:51:59 +0000293bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Craig Toppere0364b62013-07-17 05:57:45 +0000294 const X86AddressMode &AM, bool Aligned) {
Chris Lattner438949a2008-10-15 05:30:52 +0000295 // Handle 'null' like i32/i64 0.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000296 if (isa<ConstantPointerNull>(Val))
297 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000298
Chris Lattner438949a2008-10-15 05:30:52 +0000299 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohman46510a72010-04-15 01:51:59 +0000300 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner438949a2008-10-15 05:30:52 +0000301 unsigned Opc = 0;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000302 bool Signed = true;
Owen Anderson825b72b2009-08-11 20:47:22 +0000303 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner438949a2008-10-15 05:30:52 +0000304 default: break;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000305 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson825b72b2009-08-11 20:47:22 +0000306 case MVT::i8: Opc = X86::MOV8mi; break;
307 case MVT::i16: Opc = X86::MOV16mi; break;
308 case MVT::i32: Opc = X86::MOV32mi; break;
309 case MVT::i64:
Chris Lattner438949a2008-10-15 05:30:52 +0000310 // Must be a 32-bit sign extended value.
Jakub Staszakeaf77252012-11-15 19:05:23 +0000311 if (isInt<32>(CI->getSExtValue()))
Chris Lattner438949a2008-10-15 05:30:52 +0000312 Opc = X86::MOV64mi32;
313 break;
314 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000315
Chris Lattner438949a2008-10-15 05:30:52 +0000316 if (Opc) {
Dan Gohman84023e02010-07-10 09:00:22 +0000317 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
318 DL, TII.get(Opc)), AM)
John McCall795ee9d2010-04-06 23:35:53 +0000319 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000320 CI->getZExtValue());
Chris Lattner438949a2008-10-15 05:30:52 +0000321 return true;
322 }
323 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000324
Chris Lattner438949a2008-10-15 05:30:52 +0000325 unsigned ValReg = getRegForValue(Val);
326 if (ValReg == 0)
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000327 return false;
328
Craig Toppere0364b62013-07-17 05:57:45 +0000329 return X86FastEmitStore(VT, ValReg, AM, Aligned);
Chris Lattner438949a2008-10-15 05:30:52 +0000330}
331
Evan Cheng24e3a902008-09-08 06:35:17 +0000332/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
333/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
334/// ISD::SIGN_EXTEND).
Owen Andersone50ed302009-08-10 22:56:29 +0000335bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
336 unsigned Src, EVT SrcVT,
Evan Cheng24e3a902008-09-08 06:35:17 +0000337 unsigned &ResultReg) {
Dan Gohmana6cb6412010-05-11 23:54:07 +0000338 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
339 Src, /*TODO: Kill=*/false);
Jakub Staszakfe9b5a42013-02-14 21:50:09 +0000340 if (RR == 0)
Owen Andersonac34a002008-09-11 19:44:55 +0000341 return false;
Jakub Staszakfe9b5a42013-02-14 21:50:09 +0000342
343 ResultReg = RR;
344 return true;
Evan Cheng24e3a902008-09-08 06:35:17 +0000345}
346
Dan Gohman0586d912008-09-10 20:11:02 +0000347/// X86SelectAddress - Attempt to fill in an address from the given value.
348///
Dan Gohman46510a72010-04-15 01:51:59 +0000349bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendlingcb3023a2013-09-24 00:13:08 +0000350redo_gep:
Dan Gohman46510a72010-04-15 01:51:59 +0000351 const User *U = NULL;
Dan Gohman35893082008-09-18 23:23:44 +0000352 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000353 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanea9f1512010-06-18 20:44:47 +0000354 // Don't walk into other basic blocks; it's possible we haven't
355 // visited them yet, so the instructions may not yet be assigned
356 // virtual registers.
Dan Gohman742bf872010-11-16 22:43:23 +0000357 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
358 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
359 Opcode = I->getOpcode();
360 U = I;
361 }
Dan Gohman46510a72010-04-15 01:51:59 +0000362 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman35893082008-09-18 23:23:44 +0000363 Opcode = C->getOpcode();
364 U = C;
365 }
Dan Gohman0586d912008-09-10 20:11:02 +0000366
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000367 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner868ee942010-06-15 19:08:40 +0000368 if (Ty->getAddressSpace() > 255)
Dan Gohman1415a602010-06-18 20:45:41 +0000369 // Fast instruction selection doesn't support the special
370 // address spaces.
Chris Lattner868ee942010-06-15 19:08:40 +0000371 return false;
372
Dan Gohman35893082008-09-18 23:23:44 +0000373 switch (Opcode) {
374 default: break;
375 case Instruction::BitCast:
376 // Look past bitcasts.
Chris Lattner0aa43de2009-07-10 05:33:42 +0000377 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman35893082008-09-18 23:23:44 +0000378
379 case Instruction::IntToPtr:
380 // Look past no-op inttoptrs.
381 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000382 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000383 break;
Dan Gohman35893082008-09-18 23:23:44 +0000384
385 case Instruction::PtrToInt:
386 // Look past no-op ptrtoints.
387 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000388 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman55fdaec2008-12-08 23:50:06 +0000389 break;
Dan Gohman35893082008-09-18 23:23:44 +0000390
391 case Instruction::Alloca: {
392 // Do static allocas.
393 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohmana4160c32010-07-07 16:29:44 +0000394 DenseMap<const AllocaInst*, int>::iterator SI =
395 FuncInfo.StaticAllocaMap.find(A);
396 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000397 AM.BaseType = X86AddressMode::FrameIndexBase;
398 AM.Base.FrameIndex = SI->second;
399 return true;
400 }
401 break;
Dan Gohman35893082008-09-18 23:23:44 +0000402 }
403
404 case Instruction::Add: {
405 // Adds of constants are common and easy enough.
Dan Gohman46510a72010-04-15 01:51:59 +0000406 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman09aae462008-09-26 20:04:15 +0000407 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
408 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000409 if (isInt<32>(Disp)) {
Dan Gohman09aae462008-09-26 20:04:15 +0000410 AM.Disp = (uint32_t)Disp;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000411 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman09aae462008-09-26 20:04:15 +0000412 }
Dan Gohman0586d912008-09-10 20:11:02 +0000413 }
Dan Gohman35893082008-09-18 23:23:44 +0000414 break;
415 }
416
417 case Instruction::GetElementPtr: {
Chris Lattnerbfcc8e02010-03-04 19:54:45 +0000418 X86AddressMode SavedAM = AM;
419
Dan Gohman35893082008-09-18 23:23:44 +0000420 // Pattern-match simple GEPs.
Dan Gohman09aae462008-09-26 20:04:15 +0000421 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman35893082008-09-18 23:23:44 +0000422 unsigned IndexReg = AM.IndexReg;
423 unsigned Scale = AM.Scale;
424 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohmanc8a1a3c2008-12-08 07:57:47 +0000425 // Iterate through the indices, folding what we can. Constants can be
426 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohman46510a72010-04-15 01:51:59 +0000427 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman35893082008-09-18 23:23:44 +0000428 i != e; ++i, ++GTI) {
Dan Gohman46510a72010-04-15 01:51:59 +0000429 const Value *Op = *i;
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000430 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman35893082008-09-18 23:23:44 +0000431 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000432 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
433 continue;
434 }
Eric Christopher471e4222011-06-08 23:55:35 +0000435
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000436 // A array/variable index is always of the form i*S where S is the
437 // constant scale size. See if we can push the scale into immediates.
438 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
439 for (;;) {
440 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
441 // Constant-offset addressing.
442 Disp += CI->getSExtValue() * S;
443 break;
Dan Gohmanb55d6b62011-03-22 00:04:35 +0000444 }
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000445 if (isa<AddOperator>(Op) &&
446 (!isa<Instruction>(Op) ||
447 FuncInfo.MBBMap[cast<Instruction>(Op)->getParent()]
448 == FuncInfo.MBB) &&
449 isa<ConstantInt>(cast<AddOperator>(Op)->getOperand(1))) {
450 // An add (in the same block) with a constant operand. Fold the
451 // constant.
452 ConstantInt *CI =
453 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
454 Disp += CI->getSExtValue() * S;
455 // Iterate on the other operand.
456 Op = cast<AddOperator>(Op)->getOperand(0);
457 continue;
458 }
459 if (IndexReg == 0 &&
460 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
461 (S == 1 || S == 2 || S == 4 || S == 8)) {
462 // Scaled-index addressing.
463 Scale = S;
464 IndexReg = getRegForGEPIndex(Op).first;
465 if (IndexReg == 0)
466 return false;
467 break;
468 }
469 // Unsupported.
470 goto unsupported_gep;
Dan Gohman35893082008-09-18 23:23:44 +0000471 }
472 }
Bill Wendlingcb3023a2013-09-24 00:13:08 +0000473
Dan Gohman09aae462008-09-26 20:04:15 +0000474 // Check for displacement overflow.
Benjamin Kramer34247a02010-03-29 21:13:41 +0000475 if (!isInt<32>(Disp))
Dan Gohman09aae462008-09-26 20:04:15 +0000476 break;
Bill Wendlingcb3023a2013-09-24 00:13:08 +0000477
Dan Gohman35893082008-09-18 23:23:44 +0000478 AM.IndexReg = IndexReg;
479 AM.Scale = Scale;
Dan Gohman09aae462008-09-26 20:04:15 +0000480 AM.Disp = (uint32_t)Disp;
Bill Wendlingcb3023a2013-09-24 00:13:08 +0000481
482 if (const GetElementPtrInst *GEP =
483 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
484 // Ok, the GEP indices were covered by constant-offset and scaled-index
485 // addressing. Update the address state and move on to examining the base.
486 V = GEP;
487 goto redo_gep;
488 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner225d4ca2010-03-04 19:48:19 +0000489 return true;
Bill Wendlingcb3023a2013-09-24 00:13:08 +0000490 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000491
Chris Lattnerdceb52a2011-04-17 17:05:12 +0000492 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner225d4ca2010-03-04 19:48:19 +0000493 // our address and just match the value instead of completely failing.
494 AM = SavedAM;
495 break;
Dan Gohman35893082008-09-18 23:23:44 +0000496 unsupported_gep:
497 // Ok, the GEP indices weren't all covered.
498 break;
499 }
500 }
501
502 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000503 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Eli Friedmana6176ad2011-09-22 23:41:28 +0000504 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000505 if (TM.getCodeModel() != CodeModel::Small)
Dan Gohman2cc3aa42008-09-25 15:24:26 +0000506 return false;
507
Eli Friedmana6176ad2011-09-22 23:41:28 +0000508 // Can't handle TLS yet.
Dan Gohman46510a72010-04-15 01:51:59 +0000509 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
Dan Gohmane9865942009-02-23 22:03:08 +0000510 if (GVar->isThreadLocal())
511 return false;
Eric Christopher471e4222011-06-08 23:55:35 +0000512
Eli Friedmana6176ad2011-09-22 23:41:28 +0000513 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
514 // it works...).
515 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
516 if (const GlobalVariable *GVar =
517 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
518 if (GVar->isThreadLocal())
519 return false;
520
Chris Lattner0a1c9972011-04-17 17:47:38 +0000521 // RIP-relative addresses can't have additional register operands, so if
522 // we've already folded stuff into the addressing mode, just force the
523 // global value into its own register, which we can use as the basereg.
524 if (!Subtarget->isPICStyleRIPRel() ||
525 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
526 // Okay, we've committed to selecting this global. Set up the address.
527 AM.GV = GV;
Dan Gohmane9865942009-02-23 22:03:08 +0000528
Chris Lattner0a1c9972011-04-17 17:47:38 +0000529 // Allow the subtarget to classify the global.
530 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000531
Chris Lattner0a1c9972011-04-17 17:47:38 +0000532 // If this reference is relative to the pic base, set it now.
533 if (isGlobalRelativeToPICBase(GVFlags)) {
534 // FIXME: How do we know Base.Reg is free??
535 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Dan Gohman7e8ef602008-09-19 23:42:04 +0000536 }
Chris Lattner0a1c9972011-04-17 17:47:38 +0000537
538 // Unless the ABI requires an extra load, return a direct reference to
539 // the global.
540 if (!isGlobalStubReference(GVFlags)) {
541 if (Subtarget->isPICStyleRIPRel()) {
542 // Use rip-relative addressing if we can. Above we verified that the
543 // base and index registers are unused.
544 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
545 AM.Base.Reg = X86::RIP;
546 }
547 AM.GVOpFlags = GVFlags;
548 return true;
549 }
550
551 // Ok, we need to do a load from a stub. If we've already loaded from
552 // this stub, reuse the loaded pointer, otherwise emit the load now.
553 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
554 unsigned LoadReg;
555 if (I != LocalValueMap.end() && I->second != 0) {
556 LoadReg = I->second;
557 } else {
558 // Issue load from stub.
559 unsigned Opc = 0;
560 const TargetRegisterClass *RC = NULL;
561 X86AddressMode StubAM;
562 StubAM.Base.Reg = AM.Base.Reg;
563 StubAM.GV = GV;
564 StubAM.GVOpFlags = GVFlags;
565
566 // Prepare for inserting code in the local-value area.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000567 SavePoint SaveInsertPt = enterLocalValueArea();
Chris Lattner0a1c9972011-04-17 17:47:38 +0000568
569 if (TLI.getPointerTy() == MVT::i64) {
570 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +0000571 RC = &X86::GR64RegClass;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000572
573 if (Subtarget->isPICStyleRIPRel())
574 StubAM.Base.Reg = X86::RIP;
575 } else {
576 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +0000577 RC = &X86::GR32RegClass;
Chris Lattner0a1c9972011-04-17 17:47:38 +0000578 }
579
580 LoadReg = createResultReg(RC);
581 MachineInstrBuilder LoadMI =
582 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
583 addFullAddress(LoadMI, StubAM);
584
585 // Ok, back to normal mode.
Eric Christopher76ad43c2012-10-03 08:10:01 +0000586 leaveLocalValueArea(SaveInsertPt);
Chris Lattner0a1c9972011-04-17 17:47:38 +0000587
588 // Prevent loading GV stub multiple times in same MBB.
589 LocalValueMap[V] = LoadReg;
590 }
591
592 // Now construct the final address. Note that the Disp, Scale,
593 // and Index values may already be set here.
594 AM.Base.Reg = LoadReg;
595 AM.GV = 0;
Chris Lattnerff7727f2009-07-09 06:41:35 +0000596 return true;
597 }
Dan Gohman0586d912008-09-10 20:11:02 +0000598 }
599
Dan Gohman97135e12008-09-26 19:15:30 +0000600 // If all else fails, try to materialize the value in a register.
Chris Lattner4c1b6062009-06-27 05:24:12 +0000601 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
Dan Gohman97135e12008-09-26 19:15:30 +0000602 if (AM.Base.Reg == 0) {
603 AM.Base.Reg = getRegForValue(V);
604 return AM.Base.Reg != 0;
605 }
606 if (AM.IndexReg == 0) {
607 assert(AM.Scale == 1 && "Scale with no index!");
608 AM.IndexReg = getRegForValue(V);
609 return AM.IndexReg != 0;
610 }
611 }
612
613 return false;
Dan Gohman0586d912008-09-10 20:11:02 +0000614}
615
Chris Lattner0aa43de2009-07-10 05:33:42 +0000616/// X86SelectCallAddress - Attempt to fill in an address from the given value.
617///
Dan Gohman46510a72010-04-15 01:51:59 +0000618bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
619 const User *U = NULL;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000620 unsigned Opcode = Instruction::UserOp1;
Dan Gohman46510a72010-04-15 01:51:59 +0000621 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000622 Opcode = I->getOpcode();
623 U = I;
Dan Gohman46510a72010-04-15 01:51:59 +0000624 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000625 Opcode = C->getOpcode();
626 U = C;
627 }
628
629 switch (Opcode) {
630 default: break;
631 case Instruction::BitCast:
632 // Look past bitcasts.
633 return X86SelectCallAddress(U->getOperand(0), AM);
634
635 case Instruction::IntToPtr:
636 // Look past no-op inttoptrs.
637 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
638 return X86SelectCallAddress(U->getOperand(0), AM);
639 break;
640
641 case Instruction::PtrToInt:
642 // Look past no-op ptrtoints.
643 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
644 return X86SelectCallAddress(U->getOperand(0), AM);
645 break;
646 }
647
648 // Handle constant address.
Dan Gohman46510a72010-04-15 01:51:59 +0000649 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner0aa43de2009-07-10 05:33:42 +0000650 // Can't handle alternate code models yet.
Chris Lattnerf1d6bd52009-07-10 21:03:06 +0000651 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner0aa43de2009-07-10 05:33:42 +0000652 return false;
653
654 // RIP-relative addresses can't have additional register operands.
655 if (Subtarget->isPICStyleRIPRel() &&
656 (AM.Base.Reg != 0 || AM.IndexReg != 0))
657 return false;
658
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000659 // Can't handle DLLImport.
660 if (GV->hasDLLImportLinkage())
661 return false;
662
663 // Can't handle TLS.
Dan Gohman46510a72010-04-15 01:51:59 +0000664 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumid64cfe12011-02-21 04:50:06 +0000665 if (GVar->isThreadLocal())
Chris Lattner0aa43de2009-07-10 05:33:42 +0000666 return false;
667
668 // Okay, we've committed to selecting this global. Set up the basic address.
669 AM.GV = GV;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000670
Chris Lattnere6c07b52009-07-10 05:45:15 +0000671 // No ABI requires an extra load for anything other than DLLImport, which
672 // we rejected above. Return a direct reference to the global.
Chris Lattnere6c07b52009-07-10 05:45:15 +0000673 if (Subtarget->isPICStyleRIPRel()) {
674 // Use rip-relative addressing if we can. Above we verified that the
675 // base and index registers are unused.
676 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
677 AM.Base.Reg = X86::RIP;
Chris Lattnere2c92082009-07-10 21:00:45 +0000678 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattnere6c07b52009-07-10 05:45:15 +0000679 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
680 } else if (Subtarget->isPICStyleGOT()) {
681 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000682 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000683
Chris Lattner0aa43de2009-07-10 05:33:42 +0000684 return true;
685 }
686
687 // If all else fails, try to materialize the value in a register.
688 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
689 if (AM.Base.Reg == 0) {
690 AM.Base.Reg = getRegForValue(V);
691 return AM.Base.Reg != 0;
692 }
693 if (AM.IndexReg == 0) {
694 assert(AM.Scale == 1 && "Scale with no index!");
695 AM.IndexReg = getRegForValue(V);
696 return AM.IndexReg != 0;
697 }
698 }
699
700 return false;
701}
702
703
Owen Andersona3971df2008-09-04 07:08:58 +0000704/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohman46510a72010-04-15 01:51:59 +0000705bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000706 // Atomic stores need special handling.
Lang Hamese4824712011-10-18 22:11:33 +0000707 const StoreInst *S = cast<StoreInst>(I);
708
709 if (S->isAtomic())
710 return false;
711
Craig Toppere0364b62013-07-17 05:57:45 +0000712 unsigned SABIAlignment =
713 TD.getABITypeAlignment(S->getValueOperand()->getType());
714 bool Aligned = S->getAlignment() == 0 || S->getAlignment() >= SABIAlignment;
715
Duncan Sands1440e8b2010-11-03 11:35:31 +0000716 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000717 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Andersona3971df2008-09-04 07:08:58 +0000718 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000719
Dan Gohman0586d912008-09-10 20:11:02 +0000720 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000721 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000722 return false;
Owen Andersona3971df2008-09-04 07:08:58 +0000723
Craig Toppere0364b62013-07-17 05:57:45 +0000724 return X86FastEmitStore(VT, I->getOperand(0), AM, Aligned);
Owen Andersona3971df2008-09-04 07:08:58 +0000725}
726
Dan Gohman84023e02010-07-10 09:00:22 +0000727/// X86SelectRet - Select and emit code to implement ret instructions.
728bool X86FastISel::X86SelectRet(const Instruction *I) {
729 const ReturnInst *Ret = cast<ReturnInst>(I);
730 const Function &F = *I->getParent()->getParent();
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000731 const X86MachineFunctionInfo *X86MFInfo =
732 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohman84023e02010-07-10 09:00:22 +0000733
734 if (!FuncInfo.CanLowerReturn)
735 return false;
736
737 CallingConv::ID CC = F.getCallingConv();
738 if (CC != CallingConv::C &&
739 CC != CallingConv::Fast &&
Charles Davisac226bb2013-07-12 06:02:35 +0000740 CC != CallingConv::X86_FastCall &&
741 CC != CallingConv::X86_64_SysV)
Dan Gohman84023e02010-07-10 09:00:22 +0000742 return false;
743
Charles Davisac226bb2013-07-12 06:02:35 +0000744 if (Subtarget->isCallingConvWin64(CC))
Dan Gohman84023e02010-07-10 09:00:22 +0000745 return false;
746
747 // Don't handle popping bytes on return for now.
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000748 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszakd61932b2013-02-17 18:35:25 +0000749 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000750
751 // fastcc with -tailcallopt is intended to provide a guaranteed
752 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000753 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohman84023e02010-07-10 09:00:22 +0000754 return false;
755
756 // Let SDISel handle vararg functions.
757 if (F.isVarArg())
758 return false;
759
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000760 // Build a list of return value registers.
761 SmallVector<unsigned, 4> RetRegs;
762
Dan Gohman84023e02010-07-10 09:00:22 +0000763 if (Ret->getNumOperands() > 0) {
764 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +0000765 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohman84023e02010-07-10 09:00:22 +0000766
767 // Analyze operands of the call, assigning locations to each operand.
768 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher471e4222011-06-08 23:55:35 +0000769 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +0000770 I->getContext());
Duncan Sandse26032d2010-10-31 13:02:38 +0000771 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohman84023e02010-07-10 09:00:22 +0000772
773 const Value *RV = Ret->getOperand(0);
774 unsigned Reg = getRegForValue(RV);
775 if (Reg == 0)
776 return false;
777
778 // Only handle a single return value for now.
779 if (ValLocs.size() != 1)
780 return false;
781
782 CCValAssign &VA = ValLocs[0];
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000783
Dan Gohman84023e02010-07-10 09:00:22 +0000784 // Don't bother handling odd stuff for now.
785 if (VA.getLocInfo() != CCValAssign::Full)
786 return false;
787 // Only handle register returns for now.
788 if (!VA.isRegLoc())
789 return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000790
791 // The calling-convention tables for x87 returns don't tell
792 // the whole story.
793 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
794 return false;
795
Eli Friedman22486c92011-05-18 23:13:10 +0000796 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedmandc515752011-05-19 22:16:13 +0000797 EVT SrcVT = TLI.getValueType(RV->getType());
798 EVT DstVT = VA.getValVT();
799 // Special handling for extended integers.
800 if (SrcVT != DstVT) {
801 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
802 return false;
803
804 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
805 return false;
806
807 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
808
809 if (SrcVT == MVT::i1) {
810 if (Outs[0].Flags.isSExt())
811 return false;
812 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
813 SrcVT = MVT::i8;
814 }
815 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
816 ISD::SIGN_EXTEND;
817 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
818 SrcReg, /*TODO: Kill=*/false);
819 }
820
821 // Make the copy.
Dan Gohman84023e02010-07-10 09:00:22 +0000822 unsigned DstReg = VA.getLocReg();
823 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000824 // Avoid a cross-class copy. This is very unlikely.
825 if (!SrcRC->contains(DstReg))
Dan Gohman84023e02010-07-10 09:00:22 +0000826 return false;
Jakob Stoklund Olesen1ba31892010-07-11 05:17:02 +0000827 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
828 DstReg).addReg(SrcReg);
Dan Gohman84023e02010-07-10 09:00:22 +0000829
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000830 // Add register to return instruction.
831 RetRegs.push_back(VA.getLocReg());
Dan Gohman84023e02010-07-10 09:00:22 +0000832 }
833
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000834 // The x86-64 ABI for returning structs by value requires that we copy
835 // the sret argument into %rax for the return. We saved the argument into
836 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000837 // and into %rax. We also do the same with %eax for Win32.
838 if (F.hasStructRetAttr() &&
839 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000840 unsigned Reg = X86MFInfo->getSRetReturnReg();
841 assert(Reg &&
842 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000843 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000844 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova46f82d2013-03-28 21:30:04 +0000845 RetReg).addReg(Reg);
846 RetRegs.push_back(RetReg);
Nick Lewyckyb09649b2012-10-02 22:45:06 +0000847 }
848
Dan Gohman84023e02010-07-10 09:00:22 +0000849 // Now emit the RET.
Jakob Stoklund Olesenc3afc762013-02-05 17:59:48 +0000850 MachineInstrBuilder MIB =
851 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::RET));
852 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
853 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohman84023e02010-07-10 09:00:22 +0000854 return true;
855}
856
Evan Cheng8b19e562008-09-03 06:44:39 +0000857/// X86SelectLoad - Select and emit code to implement load instructions.
858///
Dan Gohman46510a72010-04-15 01:51:59 +0000859bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedman4136d232011-09-02 22:33:24 +0000860 // Atomic loads need special handling.
861 if (cast<LoadInst>(I)->isAtomic())
862 return false;
863
Duncan Sands1440e8b2010-11-03 11:35:31 +0000864 MVT VT;
Dan Gohman7e7f06e2009-08-27 00:31:47 +0000865 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Cheng8b19e562008-09-03 06:44:39 +0000866 return false;
867
Dan Gohman0586d912008-09-10 20:11:02 +0000868 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +0000869 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman0586d912008-09-10 20:11:02 +0000870 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000871
Evan Cheng0de588f2008-09-05 21:00:03 +0000872 unsigned ResultReg = 0;
Dan Gohman0586d912008-09-10 20:11:02 +0000873 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Cheng0de588f2008-09-05 21:00:03 +0000874 UpdateValueMap(I, ResultReg);
875 return true;
Evan Cheng8b19e562008-09-03 06:44:39 +0000876 }
Evan Cheng0de588f2008-09-05 21:00:03 +0000877 return false;
Evan Cheng8b19e562008-09-03 06:44:39 +0000878}
879
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000880static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000881 bool HasAVX = Subtarget->hasAVX();
Craig Topper1accb7e2012-01-10 06:54:16 +0000882 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
883 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000884
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000886 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000887 case MVT::i8: return X86::CMP8rr;
888 case MVT::i16: return X86::CMP16rr;
889 case MVT::i32: return X86::CMP32rr;
890 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +0000891 case MVT::f32:
892 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
893 case MVT::f64:
894 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohmand98d6202008-10-02 22:15:21 +0000895 }
Dan Gohmand98d6202008-10-02 22:15:21 +0000896}
897
Chris Lattner0e13c782008-10-15 04:13:29 +0000898/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
899/// of the comparison, return an opcode that works for the compare (e.g.
900/// CMP32ri) otherwise return 0.
Dan Gohman46510a72010-04-15 01:51:59 +0000901static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson825b72b2009-08-11 20:47:22 +0000902 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner0e13c782008-10-15 04:13:29 +0000903 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner45ac17f2008-10-15 04:32:45 +0000904 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +0000905 case MVT::i8: return X86::CMP8ri;
906 case MVT::i16: return X86::CMP16ri;
907 case MVT::i32: return X86::CMP32ri;
908 case MVT::i64:
Chris Lattner45ac17f2008-10-15 04:32:45 +0000909 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
910 // field.
Chris Lattner438949a2008-10-15 05:30:52 +0000911 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner45ac17f2008-10-15 04:32:45 +0000912 return X86::CMP64ri32;
913 return 0;
914 }
Chris Lattner0e13c782008-10-15 04:13:29 +0000915}
916
Dan Gohman46510a72010-04-15 01:51:59 +0000917bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
918 EVT VT) {
Chris Lattner9a08a612008-10-15 04:26:38 +0000919 unsigned Op0Reg = getRegForValue(Op0);
920 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000921
Chris Lattnerd53886b2008-10-15 05:18:04 +0000922 // Handle 'null' like i32/i64 0.
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000923 if (isa<ConstantPointerNull>(Op1))
924 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000925
Chris Lattner9a08a612008-10-15 04:26:38 +0000926 // We have two options: compare with register or immediate. If the RHS of
927 // the compare is an immediate that we can fold into this compare, use
928 // CMPri, otherwise use CMPrr.
Dan Gohman46510a72010-04-15 01:51:59 +0000929 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner45ac17f2008-10-15 04:32:45 +0000930 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohman84023e02010-07-10 09:00:22 +0000931 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
932 .addReg(Op0Reg)
933 .addImm(Op1C->getSExtValue());
Chris Lattner9a08a612008-10-15 04:26:38 +0000934 return true;
935 }
936 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000937
Jakob Stoklund Olesen75be45c2010-07-11 16:22:13 +0000938 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattner9a08a612008-10-15 04:26:38 +0000939 if (CompareOpc == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000940
Chris Lattner9a08a612008-10-15 04:26:38 +0000941 unsigned Op1Reg = getRegForValue(Op1);
942 if (Op1Reg == 0) return false;
Dan Gohman84023e02010-07-10 09:00:22 +0000943 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
944 .addReg(Op0Reg)
945 .addReg(Op1Reg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000946
Chris Lattner9a08a612008-10-15 04:26:38 +0000947 return true;
948}
949
Dan Gohman46510a72010-04-15 01:51:59 +0000950bool X86FastISel::X86SelectCmp(const Instruction *I) {
951 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000952
Duncan Sands1440e8b2010-11-03 11:35:31 +0000953 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +0000954 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman4f22bb02008-09-05 01:33:56 +0000955 return false;
956
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000957 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattner54aebde2008-10-15 03:47:17 +0000958 unsigned SetCCOpc;
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000959 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000960 switch (CI->getPredicate()) {
961 case CmpInst::FCMP_OEQ: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000962 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
963 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000964
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000965 unsigned EReg = createResultReg(&X86::GR8RegClass);
966 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +0000967 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
968 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
969 TII.get(X86::SETNPr), NPReg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +0000970 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen8d13f8f2009-02-13 02:33:27 +0000971 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000972 UpdateValueMap(I, ResultReg);
973 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000974 }
975 case CmpInst::FCMP_UNE: {
Chris Lattner51ccb3d2008-10-15 04:29:23 +0000976 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
977 return false;
978
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000979 unsigned NEReg = createResultReg(&X86::GR8RegClass);
980 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner90cb88a2011-04-19 04:22:17 +0000981 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
982 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
983 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohman84023e02010-07-10 09:00:22 +0000984 .addReg(PReg).addReg(NEReg);
Chris Lattner54aebde2008-10-15 03:47:17 +0000985 UpdateValueMap(I, ResultReg);
986 return true;
Dan Gohman6e3f05f2008-09-04 23:26:51 +0000987 }
Chris Lattner8aeeeb92008-10-15 03:52:54 +0000988 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
989 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
990 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
991 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
992 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
993 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
994 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
995 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
996 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
997 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
998 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
999 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001000
Chris Lattner8aeeeb92008-10-15 03:52:54 +00001001 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1002 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1003 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1004 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1005 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1006 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
1007 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
1008 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
1009 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
1010 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001011 default:
1012 return false;
1013 }
1014
Dan Gohman46510a72010-04-15 01:51:59 +00001015 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +00001016 if (SwapArgs)
Chris Lattner9a08a612008-10-15 04:26:38 +00001017 std::swap(Op0, Op1);
Chris Lattner8aeeeb92008-10-15 03:52:54 +00001018
Chris Lattner9a08a612008-10-15 04:26:38 +00001019 // Emit a compare of Op0/Op1.
Chris Lattner51ccb3d2008-10-15 04:29:23 +00001020 if (!X86FastEmitCompare(Op0, Op1, VT))
1021 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001022
Dan Gohman84023e02010-07-10 09:00:22 +00001023 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00001024 UpdateValueMap(I, ResultReg);
1025 return true;
1026}
Evan Cheng8b19e562008-09-03 06:44:39 +00001027
Dan Gohman46510a72010-04-15 01:51:59 +00001028bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedman76927d732011-05-25 23:49:02 +00001029 EVT DstVT = TLI.getValueType(I->getType());
1030 if (!TLI.isTypeLegal(DstVT))
1031 return false;
1032
1033 unsigned ResultReg = getRegForValue(I->getOperand(0));
1034 if (ResultReg == 0)
1035 return false;
1036
Tim Northoverda0416b2013-05-30 10:43:18 +00001037 // Handle zero-extension from i1 to i8, which is common.
Craig Topper5a0910b2013-08-15 02:33:50 +00001038 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northoverda0416b2013-05-30 10:43:18 +00001039 if (SrcVT.SimpleTy == MVT::i1) {
1040 // Set the high bits to zero.
1041 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1042 SrcVT = MVT::i8;
Eli Friedman76927d732011-05-25 23:49:02 +00001043
Tim Northoverda0416b2013-05-30 10:43:18 +00001044 if (ResultReg == 0)
1045 return false;
1046 }
1047
1048 if (DstVT == MVT::i64) {
1049 // Handle extension to 64-bits via sub-register shenanigans.
1050 unsigned MovInst;
1051
1052 switch (SrcVT.SimpleTy) {
1053 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1054 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1055 case MVT::i32: MovInst = X86::MOV32rr; break;
1056 default: llvm_unreachable("Unexpected zext to i64 source type");
1057 }
1058
1059 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1060 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovInst), Result32)
1061 .addReg(ResultReg);
1062
1063 ResultReg = createResultReg(&X86::GR64RegClass);
1064 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::SUBREG_TO_REG),
1065 ResultReg)
1066 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1067 } else if (DstVT != MVT::i8) {
Eli Friedman76927d732011-05-25 23:49:02 +00001068 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1069 ResultReg, /*Kill=*/true);
1070 if (ResultReg == 0)
1071 return false;
Dan Gohmand89ae992008-09-05 01:06:14 +00001072 }
1073
Eli Friedman76927d732011-05-25 23:49:02 +00001074 UpdateValueMap(I, ResultReg);
1075 return true;
Dan Gohmand89ae992008-09-05 01:06:14 +00001076}
1077
Chris Lattner9a08a612008-10-15 04:26:38 +00001078
Dan Gohman46510a72010-04-15 01:51:59 +00001079bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmand89ae992008-09-05 01:06:14 +00001080 // Unconditional branches are selected by tablegen-generated code.
Dan Gohmand98d6202008-10-02 22:15:21 +00001081 // Handle a conditional branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001082 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohmana4160c32010-07-07 16:29:44 +00001083 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1084 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmand89ae992008-09-05 01:06:14 +00001085
Dan Gohman8bef7442010-08-21 02:32:36 +00001086 // Fold the common case of a conditional branch with a comparison
1087 // in the same block (values defined on other blocks may not have
1088 // initialized registers).
Dan Gohman46510a72010-04-15 01:51:59 +00001089 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman8bef7442010-08-21 02:32:36 +00001090 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Andersone50ed302009-08-10 22:56:29 +00001091 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmand89ae992008-09-05 01:06:14 +00001092
Dan Gohmand98d6202008-10-02 22:15:21 +00001093 // Try to take advantage of fallthrough opportunities.
1094 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohman84023e02010-07-10 09:00:22 +00001095 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohmand98d6202008-10-02 22:15:21 +00001096 std::swap(TrueMBB, FalseMBB);
1097 Predicate = CmpInst::getInversePredicate(Predicate);
1098 }
1099
Chris Lattner871d2462008-10-15 03:58:05 +00001100 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1101 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1102
Dan Gohmand98d6202008-10-02 22:15:21 +00001103 switch (Predicate) {
Dan Gohman7b66e042008-10-21 18:24:51 +00001104 case CmpInst::FCMP_OEQ:
1105 std::swap(TrueMBB, FalseMBB);
1106 Predicate = CmpInst::FCMP_UNE;
1107 // FALL THROUGH
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001108 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1109 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1110 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1111 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1112 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1113 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1114 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1115 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1116 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1117 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1118 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1119 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1120 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001121
Chris Lattnerbd13fb62010-02-11 19:25:55 +00001122 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1123 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1124 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1125 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1126 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1127 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1128 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1129 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1130 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1131 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohmand98d6202008-10-02 22:15:21 +00001132 default:
1133 return false;
1134 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001135
Dan Gohman46510a72010-04-15 01:51:59 +00001136 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner709d8292008-10-15 04:02:26 +00001137 if (SwapArgs)
1138 std::swap(Op0, Op1);
1139
Chris Lattner9a08a612008-10-15 04:26:38 +00001140 // Emit a compare of the LHS and RHS, setting the flags.
1141 if (!X86FastEmitCompare(Op0, Op1, VT))
1142 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001143
Dan Gohman84023e02010-07-10 09:00:22 +00001144 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1145 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001146
1147 if (Predicate == CmpInst::FCMP_UNE) {
1148 // X86 requires a second branch to handle UNE (and OEQ,
1149 // which is mapped to UNE above).
Dan Gohman84023e02010-07-10 09:00:22 +00001150 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1151 .addMBB(TrueMBB);
Dan Gohman7b66e042008-10-21 18:24:51 +00001152 }
1153
Stuart Hastings3bf91252010-06-17 22:43:56 +00001154 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001155 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand98d6202008-10-02 22:15:21 +00001156 return true;
1157 }
Chris Lattner90cb88a2011-04-19 04:22:17 +00001158 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1159 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1160 // typically happen for _Bool and C++ bools.
1161 MVT SourceVT;
1162 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1163 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1164 unsigned TestOpc = 0;
1165 switch (SourceVT.SimpleTy) {
1166 default: break;
1167 case MVT::i8: TestOpc = X86::TEST8ri; break;
1168 case MVT::i16: TestOpc = X86::TEST16ri; break;
1169 case MVT::i32: TestOpc = X86::TEST32ri; break;
1170 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1171 }
1172 if (TestOpc) {
1173 unsigned OpReg = getRegForValue(TI->getOperand(0));
1174 if (OpReg == 0) return false;
1175 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1176 .addReg(OpReg).addImm(1);
Eric Christopher471e4222011-06-08 23:55:35 +00001177
Chris Lattnerc76d1212011-04-19 04:26:32 +00001178 unsigned JmpOpc = X86::JNE_4;
1179 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1180 std::swap(TrueMBB, FalseMBB);
1181 JmpOpc = X86::JE_4;
1182 }
Eric Christopher471e4222011-06-08 23:55:35 +00001183
Chris Lattnerc76d1212011-04-19 04:26:32 +00001184 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner90cb88a2011-04-19 04:22:17 +00001185 .addMBB(TrueMBB);
1186 FastEmitBranch(FalseMBB, DL);
1187 FuncInfo.MBB->addSuccessor(TrueMBB);
1188 return true;
1189 }
1190 }
Dan Gohmand98d6202008-10-02 22:15:21 +00001191 }
1192
1193 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman547eb4f2011-04-27 01:34:27 +00001194 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1195 // in an explicit cast, so make sure to handle that correctly.
Dan Gohmand98d6202008-10-02 22:15:21 +00001196 unsigned OpReg = getRegForValue(BI->getCondition());
1197 if (OpReg == 0) return false;
1198
Eli Friedman547eb4f2011-04-27 01:34:27 +00001199 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1200 .addReg(OpReg).addImm(1);
Dan Gohman84023e02010-07-10 09:00:22 +00001201 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1202 .addMBB(TrueMBB);
Stuart Hastings3bf91252010-06-17 22:43:56 +00001203 FastEmitBranch(FalseMBB, DL);
Dan Gohman84023e02010-07-10 09:00:22 +00001204 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmand89ae992008-09-05 01:06:14 +00001205 return true;
1206}
1207
Dan Gohman46510a72010-04-15 01:51:59 +00001208bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattner602fc062011-04-17 20:23:29 +00001209 unsigned CReg = 0, OpReg = 0;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001210 const TargetRegisterClass *RC = NULL;
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001211 if (I->getType()->isIntegerTy(8)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001212 CReg = X86::CL;
1213 RC = &X86::GR8RegClass;
1214 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001215 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1216 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1217 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001218 default: return false;
1219 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001220 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001221 CReg = X86::CX;
1222 RC = &X86::GR16RegClass;
1223 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001224 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1225 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1226 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001227 default: return false;
1228 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001229 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001230 CReg = X86::ECX;
1231 RC = &X86::GR32RegClass;
1232 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001233 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1234 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1235 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001236 default: return false;
1237 }
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001238 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001239 CReg = X86::RCX;
1240 RC = &X86::GR64RegClass;
1241 switch (I->getOpcode()) {
Chris Lattner602fc062011-04-17 20:23:29 +00001242 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1243 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1244 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001245 default: return false;
1246 }
1247 } else {
1248 return false;
1249 }
1250
Duncan Sands1440e8b2010-11-03 11:35:31 +00001251 MVT VT;
1252 if (!isTypeLegal(I->getType(), VT))
Dan Gohmanf58cb6d2008-09-05 21:27:34 +00001253 return false;
1254
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001255 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1256 if (Op0Reg == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001257
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001258 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1259 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00001260 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1261 CReg).addReg(Op1Reg);
Dan Gohman145b8282008-10-07 21:50:36 +00001262
1263 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001264 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohman145b8282008-10-07 21:50:36 +00001265 if (CReg != X86::CL)
Dan Gohman84023e02010-07-10 09:00:22 +00001266 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1267 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen0bc25f42010-07-08 16:40:22 +00001268 .addReg(CReg, RegState::Kill);
Dan Gohman145b8282008-10-07 21:50:36 +00001269
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001270 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001271 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1272 .addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001273 UpdateValueMap(I, ResultReg);
1274 return true;
1275}
1276
Eli Bendersky50125482013-04-17 20:10:13 +00001277bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1278 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1279 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1280 const static bool S = true; // IsSigned
1281 const static bool U = false; // !IsSigned
1282 const static unsigned Copy = TargetOpcode::COPY;
1283 // For the X86 DIV/IDIV instruction, in most cases the dividend
1284 // (numerator) must be in a specific register pair highreg:lowreg,
1285 // producing the quotient in lowreg and the remainder in highreg.
1286 // For most data types, to set up the instruction, the dividend is
1287 // copied into lowreg, and lowreg is sign-extended or zero-extended
1288 // into highreg. The exception is i8, where the dividend is defined
1289 // as a single register rather than a register pair, and we
1290 // therefore directly sign-extend or zero-extend the dividend into
1291 // lowreg, instead of copying, and ignore the highreg.
1292 const static struct DivRemEntry {
1293 // The following portion depends only on the data type.
1294 const TargetRegisterClass *RC;
1295 unsigned LowInReg; // low part of the register pair
1296 unsigned HighInReg; // high part of the register pair
1297 // The following portion depends on both the data type and the operation.
1298 struct DivRemResult {
1299 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1300 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1301 // highreg, or copying a zero into highreg.
1302 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1303 // zero/sign-extending into lowreg for i8.
1304 unsigned DivRemResultReg; // Register containing the desired result.
1305 bool IsOpSigned; // Whether to use signed or unsigned form.
1306 } ResultTable[NumOps];
1307 } OpTable[NumTypes] = {
1308 { &X86::GR8RegClass, X86::AX, 0, {
1309 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1310 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1311 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1312 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1313 }
1314 }, // i8
1315 { &X86::GR16RegClass, X86::AX, X86::DX, {
1316 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1317 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover15983b82013-05-30 13:19:42 +00001318 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1319 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky50125482013-04-17 20:10:13 +00001320 }
1321 }, // i16
1322 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1323 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1324 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1325 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1326 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1327 }
1328 }, // i32
1329 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1330 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1331 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover15983b82013-05-30 13:19:42 +00001332 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1333 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky50125482013-04-17 20:10:13 +00001334 }
1335 }, // i64
1336 };
1337
1338 MVT VT;
1339 if (!isTypeLegal(I->getType(), VT))
1340 return false;
1341
1342 unsigned TypeIndex, OpIndex;
1343 switch (VT.SimpleTy) {
1344 default: return false;
1345 case MVT::i8: TypeIndex = 0; break;
1346 case MVT::i16: TypeIndex = 1; break;
1347 case MVT::i32: TypeIndex = 2; break;
1348 case MVT::i64: TypeIndex = 3;
1349 if (!Subtarget->is64Bit())
1350 return false;
1351 break;
1352 }
1353
1354 switch (I->getOpcode()) {
1355 default: llvm_unreachable("Unexpected div/rem opcode");
1356 case Instruction::SDiv: OpIndex = 0; break;
1357 case Instruction::SRem: OpIndex = 1; break;
1358 case Instruction::UDiv: OpIndex = 2; break;
1359 case Instruction::URem: OpIndex = 3; break;
1360 }
1361
1362 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1363 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1364 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1365 if (Op0Reg == 0)
1366 return false;
1367 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1368 if (Op1Reg == 0)
1369 return false;
1370
1371 // Move op0 into low-order input register.
1372 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1373 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1374 // Zero-extend or sign-extend into high-order input register.
1375 if (OpEntry.OpSignExtend) {
1376 if (OpEntry.IsOpSigned)
1377 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1378 TII.get(OpEntry.OpSignExtend));
Tim Northover15983b82013-05-30 13:19:42 +00001379 else {
1380 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Eli Bendersky50125482013-04-17 20:10:13 +00001381 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Tim Northover15983b82013-05-30 13:19:42 +00001382 TII.get(X86::MOV32r0), Zero32);
1383
1384 // Copy the zero into the appropriate sub/super/identical physical
1385 // register. Unfortunately the operations needed are not uniform enough to
1386 // fit neatly into the table above.
1387 if (VT.SimpleTy == MVT::i16) {
1388 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher3ce2a982013-06-11 23:41:41 +00001389 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover15983b82013-05-30 13:19:42 +00001390 .addReg(Zero32, 0, X86::sub_16bit);
1391 } else if (VT.SimpleTy == MVT::i32) {
1392 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher3ce2a982013-06-11 23:41:41 +00001393 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover15983b82013-05-30 13:19:42 +00001394 .addReg(Zero32);
1395 } else if (VT.SimpleTy == MVT::i64) {
1396 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1397 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1398 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1399 }
1400 }
Eli Bendersky50125482013-04-17 20:10:13 +00001401 }
1402 // Generate the DIV/IDIV instruction.
1403 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1404 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachcc64dc62013-07-09 02:07:25 +00001405 // For i8 remainder, we can't reference AH directly, as we'll end
1406 // up with bogus copies like %R9B = COPY %AH. Reference AX
1407 // instead to prevent AH references in a REX instruction.
1408 //
1409 // The current assumption of the fast register allocator is that isel
1410 // won't generate explicit references to the GPR8_NOREX registers. If
1411 // the allocator and/or the backend get enhanced to be more robust in
1412 // that regard, this can be, and should be, removed.
1413 unsigned ResultReg = 0;
1414 if ((I->getOpcode() == Instruction::SRem ||
1415 I->getOpcode() == Instruction::URem) &&
1416 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1417 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1418 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
1419 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1420 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1421
1422 // Shift AX right by 8 bits instead of using AH.
1423 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SHR16ri),
1424 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1425
1426 // Now reference the 8-bit subreg of the result.
1427 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1428 /*Kill=*/true, X86::sub_8bit);
1429 }
1430 // Copy the result out of the physreg if we haven't already.
1431 if (!ResultReg) {
1432 ResultReg = createResultReg(TypeEntry.RC);
1433 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Copy), ResultReg)
1434 .addReg(OpEntry.DivRemResultReg);
1435 }
Eli Bendersky50125482013-04-17 20:10:13 +00001436 UpdateValueMap(I, ResultReg);
1437
1438 return true;
1439}
1440
Dan Gohman46510a72010-04-15 01:51:59 +00001441bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00001442 MVT VT;
1443 if (!isTypeLegal(I->getType(), VT))
Chris Lattner160f6cc2008-10-15 05:07:36 +00001444 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001445
Eric Christophere487b012010-09-29 23:00:29 +00001446 // We only use cmov here, if we don't have a cmov instruction bail.
1447 if (!Subtarget->hasCMov()) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001448
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001449 unsigned Opc = 0;
1450 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001451 if (VT == MVT::i16) {
Dan Gohman31d26912008-09-05 21:13:04 +00001452 Opc = X86::CMOVE16rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001453 RC = &X86::GR16RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001454 } else if (VT == MVT::i32) {
Dan Gohman31d26912008-09-05 21:13:04 +00001455 Opc = X86::CMOVE32rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001456 RC = &X86::GR32RegClass;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001457 } else if (VT == MVT::i64) {
Dan Gohman31d26912008-09-05 21:13:04 +00001458 Opc = X86::CMOVE64rr;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001459 RC = &X86::GR64RegClass;
1460 } else {
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001461 return false;
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001462 }
1463
1464 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1465 if (Op0Reg == 0) return false;
1466 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1467 if (Op1Reg == 0) return false;
1468 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1469 if (Op2Reg == 0) return false;
1470
Dan Gohman84023e02010-07-10 09:00:22 +00001471 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8rr))
1472 .addReg(Op0Reg).addReg(Op0Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001473 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00001474 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1475 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00001476 UpdateValueMap(I, ResultReg);
1477 return true;
1478}
1479
Dan Gohman46510a72010-04-15 01:51:59 +00001480bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001481 // fpext from float to double.
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001482 if (X86ScalarSSEf64 &&
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001483 I->getType()->isDoubleTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001484 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001485 if (V->getType()->isFloatTy()) {
Chris Lattner160f6cc2008-10-15 05:07:36 +00001486 unsigned OpReg = getRegForValue(V);
1487 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001488 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001489 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1490 TII.get(X86::CVTSS2SDrr), ResultReg)
1491 .addReg(OpReg);
Chris Lattner160f6cc2008-10-15 05:07:36 +00001492 UpdateValueMap(I, ResultReg);
1493 return true;
Dan Gohman78efce62008-09-10 21:02:08 +00001494 }
1495 }
1496
1497 return false;
1498}
1499
Dan Gohman46510a72010-04-15 01:51:59 +00001500bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00001501 if (X86ScalarSSEf64) {
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001502 if (I->getType()->isFloatTy()) {
Dan Gohman46510a72010-04-15 01:51:59 +00001503 const Value *V = I->getOperand(0);
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001504 if (V->getType()->isDoubleTy()) {
Dan Gohman78efce62008-09-10 21:02:08 +00001505 unsigned OpReg = getRegForValue(V);
1506 if (OpReg == 0) return false;
Craig Topperc9099502012-04-20 06:31:50 +00001507 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohman84023e02010-07-10 09:00:22 +00001508 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1509 TII.get(X86::CVTSD2SSrr), ResultReg)
1510 .addReg(OpReg);
Dan Gohman78efce62008-09-10 21:02:08 +00001511 UpdateValueMap(I, ResultReg);
1512 return true;
1513 }
1514 }
1515 }
1516
1517 return false;
1518}
1519
Dan Gohman46510a72010-04-15 01:51:59 +00001520bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Andersone50ed302009-08-10 22:56:29 +00001521 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1522 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001523
Eli Friedman76927d732011-05-25 23:49:02 +00001524 // This code only handles truncation to byte.
Owen Anderson825b72b2009-08-11 20:47:22 +00001525 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001526 return false;
Eli Friedman76927d732011-05-25 23:49:02 +00001527 if (!TLI.isTypeLegal(SrcVT))
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001528 return false;
1529
1530 unsigned InputReg = getRegForValue(I->getOperand(0));
1531 if (!InputReg)
1532 // Unhandled operand. Halt "fast" selection and bail.
1533 return false;
1534
Eli Friedman76927d732011-05-25 23:49:02 +00001535 if (SrcVT == MVT::i8) {
1536 // Truncate from i8 to i1; no code needed.
1537 UpdateValueMap(I, InputReg);
1538 return true;
1539 }
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001540
Eli Friedman76927d732011-05-25 23:49:02 +00001541 if (!Subtarget->is64Bit()) {
1542 // If we're on x86-32; we can't extract an i8 from a general register.
1543 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperc9099502012-04-20 06:31:50 +00001544 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1545 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1546 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedman76927d732011-05-25 23:49:02 +00001547 unsigned CopyReg = createResultReg(CopyRC);
1548 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1549 CopyReg).addReg(InputReg);
1550 InputReg = CopyReg;
1551 }
1552
1553 // Issue an extract_subreg.
Owen Anderson825b72b2009-08-11 20:47:22 +00001554 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedman76927d732011-05-25 23:49:02 +00001555 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen3458e9e2010-05-24 14:48:17 +00001556 X86::sub_8bit);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00001557 if (!ResultReg)
1558 return false;
1559
1560 UpdateValueMap(I, ResultReg);
1561 return true;
1562}
1563
Eli Friedmanc0883452011-05-20 22:21:04 +00001564bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1565 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1566}
1567
Eli Friedmand5089a92011-04-27 01:45:07 +00001568bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1569 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedmanc0883452011-05-20 22:21:04 +00001570
Eli Friedmand5089a92011-04-27 01:45:07 +00001571 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedmanc0883452011-05-20 22:21:04 +00001572 if (!IsMemcpySmall(Len))
1573 return false;
1574
1575 bool i64Legal = Subtarget->is64Bit();
Eli Friedmand5089a92011-04-27 01:45:07 +00001576
1577 // We don't care about alignment here since we just emit integer accesses.
1578 while (Len) {
1579 MVT VT;
1580 if (Len >= 8 && i64Legal)
1581 VT = MVT::i64;
1582 else if (Len >= 4)
1583 VT = MVT::i32;
1584 else if (Len >= 2)
1585 VT = MVT::i16;
1586 else {
Eli Friedmand5089a92011-04-27 01:45:07 +00001587 VT = MVT::i8;
1588 }
1589
1590 unsigned Reg;
1591 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1592 RV &= X86FastEmitStore(VT, Reg, DestAM);
1593 assert(RV && "Failed to emit load or store??");
1594
1595 unsigned Size = VT.getSizeInBits()/8;
1596 Len -= Size;
1597 DestAM.Disp += Size;
1598 SrcAM.Disp += Size;
1599 }
1600
1601 return true;
1602}
1603
Dan Gohman46510a72010-04-15 01:51:59 +00001604bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling52370a12008-12-09 02:42:50 +00001605 // FIXME: Handle more intrinsics.
Chris Lattnera9a42252009-04-12 07:36:01 +00001606 switch (I.getIntrinsicID()) {
Bill Wendling52370a12008-12-09 02:42:50 +00001607 default: return false;
Chris Lattner832e4942011-04-19 05:52:03 +00001608 case Intrinsic::memcpy: {
1609 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1610 // Don't handle volatile or variable length memcpys.
Eli Friedman25255cb2011-06-10 23:39:36 +00001611 if (MCI.isVolatile())
Chris Lattner832e4942011-04-19 05:52:03 +00001612 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001613
Eli Friedman25255cb2011-06-10 23:39:36 +00001614 if (isa<ConstantInt>(MCI.getLength())) {
1615 // Small memcpy's are common enough that we want to do them
1616 // without a call if possible.
1617 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1618 if (IsMemcpySmall(Len)) {
1619 X86AddressMode DestAM, SrcAM;
1620 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1621 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1622 return false;
1623 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1624 return true;
1625 }
1626 }
Eric Christopher471e4222011-06-08 23:55:35 +00001627
Eli Friedman25255cb2011-06-10 23:39:36 +00001628 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1629 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner832e4942011-04-19 05:52:03 +00001630 return false;
Eli Friedmand5089a92011-04-27 01:45:07 +00001631
Eli Friedman25255cb2011-06-10 23:39:36 +00001632 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1633 return false;
1634
1635 return DoSelectCall(&I, "memcpy");
Chris Lattner832e4942011-04-19 05:52:03 +00001636 }
Eli Friedman25255cb2011-06-10 23:39:36 +00001637 case Intrinsic::memset: {
1638 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher471e4222011-06-08 23:55:35 +00001639
Nick Lewycky3207c9a2011-08-02 00:40:16 +00001640 if (MSI.isVolatile())
1641 return false;
1642
Eli Friedman25255cb2011-06-10 23:39:36 +00001643 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1644 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1645 return false;
1646
1647 if (MSI.getDestAddressSpace() > 255)
1648 return false;
1649
1650 return DoSelectCall(&I, "memset");
1651 }
Eric Christopher07754c22010-03-18 20:27:26 +00001652 case Intrinsic::stackprotector: {
Chad Rosiere1093e52012-05-11 19:43:29 +00001653 // Emit code to store the stack guard onto the stack.
Eric Christopher07754c22010-03-18 20:27:26 +00001654 EVT PtrTy = TLI.getPointerTy();
1655
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001656 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1657 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher07754c22010-03-18 20:27:26 +00001658
1659 // Grab the frame index.
1660 X86AddressMode AM;
1661 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher88dee302010-03-18 21:58:33 +00001662 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher07754c22010-03-18 20:27:26 +00001663 return true;
1664 }
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001665 case Intrinsic::dbg_declare: {
Dan Gohman46510a72010-04-15 01:51:59 +00001666 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001667 X86AddressMode AM;
Dale Johannesen973f4672010-01-29 21:21:28 +00001668 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001669 if (!X86SelectAddress(DI->getAddress(), AM))
1670 return false;
Evan Chenge837dea2011-06-28 19:10:37 +00001671 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen116b7992010-02-18 18:51:15 +00001672 // FIXME may need to add RegState::Debug to any registers produced,
1673 // although ESP/EBP should be the only ones at the moment.
Dan Gohman84023e02010-07-10 09:00:22 +00001674 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1675 addImm(0).addMetadata(DI->getVariable());
Dale Johannesen5ed17ae2010-01-26 00:09:58 +00001676 return true;
1677 }
Eric Christopher77f79892010-01-18 22:11:29 +00001678 case Intrinsic::trap: {
Dan Gohman84023e02010-07-10 09:00:22 +00001679 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher77f79892010-01-18 22:11:29 +00001680 return true;
1681 }
Bill Wendling52370a12008-12-09 02:42:50 +00001682 case Intrinsic::sadd_with_overflow:
1683 case Intrinsic::uadd_with_overflow: {
Chris Lattner832e4942011-04-19 05:52:03 +00001684 // FIXME: Should fold immediates.
Eric Christopher471e4222011-06-08 23:55:35 +00001685
Bill Wendlingc065b3f2008-12-09 07:55:31 +00001686 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedman482feb32011-05-16 21:06:17 +00001687 // by a seto/setc instruction.
Bill Wendling52370a12008-12-09 02:42:50 +00001688 const Function *Callee = I.getCalledFunction();
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001689 Type *RetTy =
Bill Wendling52370a12008-12-09 02:42:50 +00001690 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1691
Duncan Sands1440e8b2010-11-03 11:35:31 +00001692 MVT VT;
Bill Wendling52370a12008-12-09 02:42:50 +00001693 if (!isTypeLegal(RetTy, VT))
1694 return false;
1695
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001696 const Value *Op1 = I.getArgOperand(0);
1697 const Value *Op2 = I.getArgOperand(1);
Bill Wendling52370a12008-12-09 02:42:50 +00001698 unsigned Reg1 = getRegForValue(Op1);
1699 unsigned Reg2 = getRegForValue(Op2);
1700
1701 if (Reg1 == 0 || Reg2 == 0)
1702 // FIXME: Handle values *not* in registers.
1703 return false;
1704
1705 unsigned OpC = 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00001706 if (VT == MVT::i32)
Bill Wendling52370a12008-12-09 02:42:50 +00001707 OpC = X86::ADD32rr;
Owen Anderson825b72b2009-08-11 20:47:22 +00001708 else if (VT == MVT::i64)
Bill Wendling52370a12008-12-09 02:42:50 +00001709 OpC = X86::ADD64rr;
1710 else
1711 return false;
1712
Eli Friedman482feb32011-05-16 21:06:17 +00001713 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledruc8e41c52012-07-23 08:51:15 +00001714 // both the returned values.
Eli Friedman482feb32011-05-16 21:06:17 +00001715 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohman84023e02010-07-10 09:00:22 +00001716 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1717 .addReg(Reg1).addReg(Reg2);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001718
Chris Lattnera9a42252009-04-12 07:36:01 +00001719 unsigned Opc = X86::SETBr;
1720 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1721 Opc = X86::SETOr;
Eli Friedman482feb32011-05-16 21:06:17 +00001722 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1723
1724 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling52370a12008-12-09 02:42:50 +00001725 return true;
1726 }
1727 }
1728}
1729
Chad Rosierfd3417d2013-02-25 21:59:35 +00001730bool X86FastISel::FastLowerArguments() {
1731 if (!FuncInfo.CanLowerReturn)
1732 return false;
1733
1734 const Function *F = FuncInfo.Fn;
1735 if (F->isVarArg())
1736 return false;
1737
1738 CallingConv::ID CC = F->getCallingConv();
1739 if (CC != CallingConv::C)
1740 return false;
Charles Davisac226bb2013-07-12 06:02:35 +00001741
1742 if (Subtarget->isCallingConvWin64(CC))
1743 return false;
1744
Chad Rosierfd3417d2013-02-25 21:59:35 +00001745 if (!Subtarget->is64Bit())
1746 return false;
1747
1748 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
1749 unsigned Idx = 1;
1750 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1751 I != E; ++I, ++Idx) {
1752 if (Idx > 6)
1753 return false;
1754
1755 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1756 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1757 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1758 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1759 return false;
1760
1761 Type *ArgTy = I->getType();
1762 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1763 return false;
1764
1765 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosierfe88aa02013-02-26 01:05:31 +00001766 if (!ArgVT.isSimple()) return false;
Chad Rosierfd3417d2013-02-25 21:59:35 +00001767 switch (ArgVT.getSimpleVT().SimpleTy) {
1768 case MVT::i32:
1769 case MVT::i64:
1770 break;
1771 default:
1772 return false;
1773 }
1774 }
1775
1776 static const uint16_t GPR32ArgRegs[] = {
1777 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1778 };
1779 static const uint16_t GPR64ArgRegs[] = {
1780 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
1781 };
1782
1783 Idx = 0;
1784 const TargetRegisterClass *RC32 = TLI.getRegClassFor(MVT::i32);
1785 const TargetRegisterClass *RC64 = TLI.getRegClassFor(MVT::i64);
1786 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1787 I != E; ++I, ++Idx) {
Chad Rosierfd3417d2013-02-25 21:59:35 +00001788 bool is32Bit = TLI.getValueType(I->getType()) == MVT::i32;
1789 const TargetRegisterClass *RC = is32Bit ? RC32 : RC64;
1790 unsigned SrcReg = is32Bit ? GPR32ArgRegs[Idx] : GPR64ArgRegs[Idx];
1791 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1792 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1793 // Without this, EmitLiveInCopies may eliminate the livein if its only
1794 // use is a bitcast (which isn't turned into an instruction).
1795 unsigned ResultReg = createResultReg(RC);
1796 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1797 ResultReg).addReg(DstReg, getKillRegState(true));
1798 UpdateValueMap(I, ResultReg);
1799 }
1800 return true;
1801}
1802
Dan Gohman46510a72010-04-15 01:51:59 +00001803bool X86FastISel::X86SelectCall(const Instruction *I) {
1804 const CallInst *CI = cast<CallInst>(I);
Gabor Greif1cfe44a2010-06-26 11:51:52 +00001805 const Value *Callee = CI->getCalledValue();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001806
1807 // Can't handle inline asm yet.
1808 if (isa<InlineAsm>(Callee))
1809 return false;
1810
Bill Wendling52370a12008-12-09 02:42:50 +00001811 // Handle intrinsic calls.
Dan Gohman46510a72010-04-15 01:51:59 +00001812 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattnera9a42252009-04-12 07:36:01 +00001813 return X86VisitIntrinsicCall(*II);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001814
Chad Rosier425e9512012-12-11 00:18:02 +00001815 // Allow SelectionDAG isel to handle tail calls.
1816 if (cast<CallInst>(I)->isTailCall())
1817 return false;
1818
Eli Friedman25255cb2011-06-10 23:39:36 +00001819 return DoSelectCall(I, 0);
1820}
1821
Rafael Espindolac338fe02012-07-25 15:42:45 +00001822static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1823 const ImmutableCallSite &CS) {
Rafael Espindola742f2c92012-07-25 13:35:45 +00001824 if (Subtarget.is64Bit())
1825 return 0;
1826 if (Subtarget.isTargetWindows())
1827 return 0;
1828 CallingConv::ID CC = CS.getCallingConv();
1829 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1830 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001831 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola742f2c92012-07-25 13:35:45 +00001832 return 0;
Bill Wendling034b94b2012-12-19 07:18:57 +00001833 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola1cee7102012-07-25 13:41:10 +00001834 return 0;
Rafael Espindola742f2c92012-07-25 13:35:45 +00001835 return 4;
1836}
1837
Eli Friedman25255cb2011-06-10 23:39:36 +00001838// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1839bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1840 const CallInst *CI = cast<CallInst>(I);
1841 const Value *Callee = CI->getCalledValue();
1842
Evan Chengf3d4efe2008-09-07 09:09:33 +00001843 // Handle only C and fastcc calling conventions for now.
Dan Gohman46510a72010-04-15 01:51:59 +00001844 ImmutableCallSite CS(CI);
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001845 CallingConv::ID CC = CS.getCallingConv();
Charles Davisac226bb2013-07-12 06:02:35 +00001846 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001847 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davisac226bb2013-07-12 06:02:35 +00001848 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
1849 CC != CallingConv::X86_64_SysV)
Evan Chengf3d4efe2008-09-07 09:09:33 +00001850 return false;
1851
Evan Cheng381993f2010-01-27 00:00:57 +00001852 // fastcc with -tailcallopt is intended to provide a guaranteed
1853 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001854 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Cheng381993f2010-01-27 00:00:57 +00001855 return false;
1856
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001857 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1858 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedman37620462011-04-19 17:22:22 +00001859 bool isVarArg = FTy->isVarArg();
1860
1861 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1862 // x86-32. Special handling for x86-64 is implemented.
Charles Davisac226bb2013-07-12 06:02:35 +00001863 if (isVarArg && isWin64)
Evan Chengf3d4efe2008-09-07 09:09:33 +00001864 return false;
1865
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001866 // Fast-isel doesn't know about callee-pop yet.
Evan Chengef41ff62011-06-23 17:54:54 +00001867 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky8a8d4792011-12-02 22:16:29 +00001868 TM.Options.GuaranteedTailCallOpt))
Dan Gohman4d3d6e12010-05-27 18:43:40 +00001869 return false;
1870
Eli Friedman19515b42011-05-17 18:29:03 +00001871 // Check whether the function can return without sret-demotion.
1872 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling8b62abd2012-12-30 13:01:51 +00001873 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman19515b42011-05-17 18:29:03 +00001874 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendling56cb2292012-07-19 00:11:40 +00001875 *FuncInfo.MF, FTy->isVarArg(),
1876 Outs, FTy->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00001877 if (!CanLowerReturn)
Eli Friedmanc93943b2011-05-17 02:36:59 +00001878 return false;
1879
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001880 // Materialize callee address in a register. FIXME: GV address can be
1881 // handled with a CALLpcrel32 instead.
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001882 X86AddressMode CalleeAM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00001883 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001884 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001885 unsigned CalleeOp = 0;
Dan Gohman46510a72010-04-15 01:51:59 +00001886 const GlobalValue *GV = 0;
Chris Lattner553e5712009-06-27 04:50:14 +00001887 if (CalleeAM.GV != 0) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001888 GV = CalleeAM.GV;
Chris Lattner553e5712009-06-27 04:50:14 +00001889 } else if (CalleeAM.Base.Reg != 0) {
1890 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00001891 } else
1892 return false;
Dan Gohmanb5b6ec62008-09-17 21:18:49 +00001893
Evan Chengf3d4efe2008-09-07 09:09:33 +00001894 // Deal with call operands first.
Dan Gohman46510a72010-04-15 01:51:59 +00001895 SmallVector<const Value *, 8> ArgVals;
Chris Lattner241ab472008-10-15 05:38:32 +00001896 SmallVector<unsigned, 8> Args;
Duncan Sands1440e8b2010-11-03 11:35:31 +00001897 SmallVector<MVT, 8> ArgVTs;
Chris Lattner241ab472008-10-15 05:38:32 +00001898 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosier15b44972012-02-15 00:36:26 +00001899 unsigned arg_size = CS.arg_size();
1900 Args.reserve(arg_size);
1901 ArgVals.reserve(arg_size);
1902 ArgVTs.reserve(arg_size);
1903 ArgFlags.reserve(arg_size);
Dan Gohman46510a72010-04-15 01:51:59 +00001904 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Chengf3d4efe2008-09-07 09:09:33 +00001905 i != e; ++i) {
Eli Friedman25255cb2011-06-10 23:39:36 +00001906 // If we're lowering a mem intrinsic instead of a regular call, skip the
1907 // last two arguments, which should not passed to the underlying functions.
1908 if (MemIntName && e-i <= 2)
1909 break;
Chris Lattnere03b8d32011-04-19 04:42:38 +00001910 Value *ArgVal = *i;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001911 ISD::ArgFlagsTy Flags;
1912 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling034b94b2012-12-19 07:18:57 +00001913 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001914 Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00001915 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001916 Flags.setZExt();
1917
Bill Wendling034b94b2012-12-19 07:18:57 +00001918 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001919 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1920 Type *ElementTy = Ty->getElementType();
Eli Friedmanc0883452011-05-20 22:21:04 +00001921 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1922 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1923 if (!FrameAlign)
1924 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1925 Flags.setByVal();
1926 Flags.setByValSize(FrameSize);
1927 Flags.setByValAlign(FrameAlign);
1928 if (!IsMemcpySmall(FrameSize))
1929 return false;
1930 }
1931
Bill Wendling034b94b2012-12-19 07:18:57 +00001932 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedmanc0883452011-05-20 22:21:04 +00001933 Flags.setInReg();
Bill Wendling034b94b2012-12-19 07:18:57 +00001934 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedmanc0883452011-05-20 22:21:04 +00001935 Flags.setNest();
1936
Chris Lattnere03b8d32011-04-19 04:42:38 +00001937 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1938 // instruction. This is safe because it is common to all fastisel supported
1939 // calling conventions on x86.
1940 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1941 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1942 CI->getBitWidth() == 16) {
1943 if (Flags.isSExt())
1944 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1945 else
1946 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1947 }
1948 }
Eric Christopher471e4222011-06-08 23:55:35 +00001949
Chris Lattnerb44101c2011-04-19 05:09:50 +00001950 unsigned ArgReg;
Eric Christopher471e4222011-06-08 23:55:35 +00001951
Chris Lattnerff009ad2011-04-19 05:15:59 +00001952 // Passing bools around ends up doing a trunc to i1 and passing it.
1953 // Codegen this as an argument + "and 1".
Chris Lattnerb44101c2011-04-19 05:09:50 +00001954 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
1955 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
1956 ArgVal->hasOneUse()) {
Chris Lattnerb44101c2011-04-19 05:09:50 +00001957 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
1958 ArgReg = getRegForValue(ArgVal);
1959 if (ArgReg == 0) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001960
Chris Lattnerb44101c2011-04-19 05:09:50 +00001961 MVT ArgVT;
1962 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher471e4222011-06-08 23:55:35 +00001963
Chris Lattnerb44101c2011-04-19 05:09:50 +00001964 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
1965 ArgVal->hasOneUse(), 1);
1966 } else {
1967 ArgReg = getRegForValue(ArgVal);
Chris Lattnerb44101c2011-04-19 05:09:50 +00001968 }
Chris Lattnere03b8d32011-04-19 04:42:38 +00001969
Chris Lattnerff009ad2011-04-19 05:15:59 +00001970 if (ArgReg == 0) return false;
1971
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001972 Type *ArgTy = ArgVal->getType();
Duncan Sands1440e8b2010-11-03 11:35:31 +00001973 MVT ArgVT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00001974 if (!isTypeLegal(ArgTy, ArgVT))
Evan Chengf3d4efe2008-09-07 09:09:33 +00001975 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00001976 if (ArgVT == MVT::x86mmx)
1977 return false;
Evan Chengf3d4efe2008-09-07 09:09:33 +00001978 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
1979 Flags.setOrigAlign(OriginalAlignment);
1980
Chris Lattnerb44101c2011-04-19 05:09:50 +00001981 Args.push_back(ArgReg);
Chris Lattnere03b8d32011-04-19 04:42:38 +00001982 ArgVals.push_back(ArgVal);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001983 ArgVTs.push_back(ArgVT);
1984 ArgFlags.push_back(Flags);
1985 }
1986
1987 // Analyze operands of the call, assigning locations to each operand.
1988 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00001989 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00001990 I->getParent()->getContext());
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001991
Dan Gohmand8acddd2010-06-01 21:09:47 +00001992 // Allocate shadow area for Win64
Charles Davisac226bb2013-07-12 06:02:35 +00001993 if (isWin64)
Wesley Peckbf17cfa2010-11-23 03:31:01 +00001994 CCInfo.AllocateStack(32, 8);
Dan Gohmand8acddd2010-06-01 21:09:47 +00001995
Duncan Sands45907662010-10-31 13:21:44 +00001996 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Chengf3d4efe2008-09-07 09:09:33 +00001997
1998 // Get a count of how many bytes are to be pushed on the stack.
1999 unsigned NumBytes = CCInfo.getNextStackOffset();
2000
2001 // Issue CALLSEQ_START
Evan Chengd5b03f22011-06-28 21:14:33 +00002002 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohman84023e02010-07-10 09:00:22 +00002003 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
2004 .addImm(NumBytes);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002005
Chris Lattner438949a2008-10-15 05:30:52 +00002006 // Process argument: walk the register/memloc assignments, inserting
Evan Chengf3d4efe2008-09-07 09:09:33 +00002007 // copies / loads.
2008 SmallVector<unsigned, 4> RegArgs;
2009 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2010 CCValAssign &VA = ArgLocs[i];
2011 unsigned Arg = Args[VA.getValNo()];
Owen Andersone50ed302009-08-10 22:56:29 +00002012 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002013
Evan Chengf3d4efe2008-09-07 09:09:33 +00002014 // Promote the value if needed.
2015 switch (VA.getLocInfo()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00002016 case CCValAssign::Full: break;
Evan Cheng24e3a902008-09-08 06:35:17 +00002017 case CCValAssign::SExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00002018 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2019 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00002020 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2021 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00002022 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00002023 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00002024 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00002025 }
2026 case CCValAssign::ZExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00002027 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2028 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00002029 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2030 Arg, ArgVT, Arg);
Chris Lattnerc46ec642011-01-05 22:26:52 +00002031 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00002032 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00002033 break;
Evan Cheng24e3a902008-09-08 06:35:17 +00002034 }
2035 case CCValAssign::AExt: {
Eli Friedmanc0883452011-05-20 22:21:04 +00002036 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2037 "Unexpected extend");
Evan Cheng24e3a902008-09-08 06:35:17 +00002038 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2039 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00002040 if (!Emitted)
2041 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattner160f6cc2008-10-15 05:07:36 +00002042 Arg, ArgVT, Arg);
Owen Andersonb6369132008-09-11 02:41:37 +00002043 if (!Emitted)
2044 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2045 Arg, ArgVT, Arg);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002046
Chris Lattnerc46ec642011-01-05 22:26:52 +00002047 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng24e3a902008-09-08 06:35:17 +00002048 ArgVT = VA.getLocVT();
Evan Chengf3d4efe2008-09-07 09:09:33 +00002049 break;
2050 }
Dan Gohmanc3c9c482009-08-05 05:33:42 +00002051 case CCValAssign::BCvt: {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002052 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002053 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohmanc3c9c482009-08-05 05:33:42 +00002054 assert(BC != 0 && "Failed to emit a bitcast!");
2055 Arg = BC;
2056 ArgVT = VA.getLocVT();
2057 break;
2058 }
Chad Rosier36ec0ca2012-07-11 19:58:38 +00002059 case CCValAssign::VExt:
2060 // VExt has not been implemented, so this should be impossible to reach
2061 // for now. However, fallback to Selection DAG isel once implemented.
2062 return false;
2063 case CCValAssign::Indirect:
2064 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2065 // support this.
2066 return false;
Evan Cheng24e3a902008-09-08 06:35:17 +00002067 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002068
Evan Chengf3d4efe2008-09-07 09:09:33 +00002069 if (VA.isRegLoc()) {
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00002070 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2071 VA.getLocReg()).addReg(Arg);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002072 RegArgs.push_back(VA.getLocReg());
2073 } else {
2074 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman0586d912008-09-10 20:11:02 +00002075 X86AddressMode AM;
Bill Wendlinga5e5ba62013-06-07 21:00:34 +00002076 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2077 getTargetMachine()->getRegisterInfo());
Michael Liaof0e06e82012-11-01 03:47:50 +00002078 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman0586d912008-09-10 20:11:02 +00002079 AM.Disp = LocMemOffset;
Dan Gohman46510a72010-04-15 01:51:59 +00002080 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedmanc0883452011-05-20 22:21:04 +00002081 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002082
Eli Friedmanc0883452011-05-20 22:21:04 +00002083 if (Flags.isByVal()) {
2084 X86AddressMode SrcAM;
2085 SrcAM.Base.Reg = Arg;
2086 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2087 assert(Res && "memcpy length already checked!"); (void)Res;
2088 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2089 // If this is a really simple value, emit this with the Value* version
Nick Lewycky1f9c6862011-10-12 00:14:12 +00002090 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedmanc0883452011-05-20 22:21:04 +00002091 // as it can cause us to reevaluate the argument.
Lang Hamese4824712011-10-18 22:11:33 +00002092 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2093 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00002094 } else {
Lang Hamese4824712011-10-18 22:11:33 +00002095 if (!X86FastEmitStore(ArgVT, Arg, AM))
2096 return false;
Eli Friedmanc0883452011-05-20 22:21:04 +00002097 }
Evan Chengf3d4efe2008-09-07 09:09:33 +00002098 }
2099 }
2100
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002101 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002102 // GOT pointer.
Chris Lattner15a380a2009-07-09 04:39:06 +00002103 if (Subtarget->isPICStyleGOT()) {
Dan Gohmana4160c32010-07-07 16:29:44 +00002104 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen5127f792010-07-11 03:31:00 +00002105 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2106 X86::EBX).addReg(Base);
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002107 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002108
Charles Davisac226bb2013-07-12 06:02:35 +00002109 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedman37620462011-04-19 17:22:22 +00002110 // Count the number of XMM registers allocated.
Craig Topperc5eaae42012-03-11 07:57:25 +00002111 static const uint16_t XMMArgRegs[] = {
Eli Friedman37620462011-04-19 17:22:22 +00002112 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2113 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2114 };
2115 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
2116 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
2117 X86::AL).addImm(NumXMMRegs);
2118 }
2119
Evan Chengf3d4efe2008-09-07 09:09:33 +00002120 // Issue the call.
Chris Lattner51e8eab2009-07-09 06:34:26 +00002121 MachineInstrBuilder MIB;
2122 if (CalleeOp) {
2123 // Register-indirect call.
Nate Begeman0c07b642010-07-22 00:09:39 +00002124 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00002125 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00002126 CallOpc = X86::CALL64r;
2127 else
2128 CallOpc = X86::CALL32r;
Dan Gohman84023e02010-07-10 09:00:22 +00002129 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
2130 .addReg(CalleeOp);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002131
Chris Lattner51e8eab2009-07-09 06:34:26 +00002132 } else {
2133 // Direct call.
2134 assert(GV && "Not a direct call");
Nate Begeman0c07b642010-07-22 00:09:39 +00002135 unsigned CallOpc;
Jakob Stoklund Olesen527a08b2012-02-16 17:56:02 +00002136 if (Subtarget->is64Bit())
Nate Begeman0c07b642010-07-22 00:09:39 +00002137 CallOpc = X86::CALL64pcrel32;
2138 else
2139 CallOpc = X86::CALLpcrel32;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002140
Chris Lattner51e8eab2009-07-09 06:34:26 +00002141 // See if we need any target-specific flags on the GV operand.
2142 unsigned char OpFlags = 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002143
Chris Lattner51e8eab2009-07-09 06:34:26 +00002144 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2145 // external symbols most go through the PLT in PIC mode. If the symbol
2146 // has hidden or protected visibility, or if it is static or local, then
2147 // we don't need to use the PLT - we can directly call it.
2148 if (Subtarget->isTargetELF() &&
2149 TM.getRelocationModel() == Reloc::PIC_ &&
2150 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2151 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00002152 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner51e8eab2009-07-09 06:34:26 +00002153 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbar558692f2011-04-20 00:14:25 +00002154 (!Subtarget->getTargetTriple().isMacOSX() ||
2155 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattner51e8eab2009-07-09 06:34:26 +00002156 // PC-relative references to external symbols should go through $stub,
2157 // unless we're building with the leopard linker or later, which
2158 // automatically synthesizes these stubs.
2159 OpFlags = X86II::MO_DARWIN_STUB;
2160 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002161
2162
Eli Friedman25255cb2011-06-10 23:39:36 +00002163 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
2164 if (MemIntName)
Eli Friedman8a37aba2011-06-11 01:55:07 +00002165 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedman25255cb2011-06-10 23:39:36 +00002166 else
2167 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattner51e8eab2009-07-09 06:34:26 +00002168 }
Dan Gohman2cc3aa42008-09-25 15:24:26 +00002169
Jakob Stoklund Olesen8bcde2a2012-02-16 00:02:50 +00002170 // Add a register mask with the call-preserved registers.
2171 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2172 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2173
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +00002174 // Add an implicit use GOT pointer in EBX.
2175 if (Subtarget->isPICStyleGOT())
2176 MIB.addReg(X86::EBX, RegState::Implicit);
2177
Charles Davisac226bb2013-07-12 06:02:35 +00002178 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesen85dccf12012-07-04 23:53:27 +00002179 MIB.addReg(X86::AL, RegState::Implicit);
2180
2181 // Add implicit physical register uses to the call.
2182 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2183 MIB.addReg(RegArgs[i], RegState::Implicit);
2184
Evan Chengf3d4efe2008-09-07 09:09:33 +00002185 // Issue CALLSEQ_END
Evan Chengd5b03f22011-06-28 21:14:33 +00002186 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindolac338fe02012-07-25 15:42:45 +00002187 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohman84023e02010-07-10 09:00:22 +00002188 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedmand227eed2011-04-28 20:19:12 +00002189 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002190
Eli Friedman19515b42011-05-17 18:29:03 +00002191 // Build info for return calling conv lowering code.
2192 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2193 SmallVector<ISD::InputArg, 32> Ins;
2194 SmallVector<EVT, 4> RetTys;
2195 ComputeValueVTs(TLI, I->getType(), RetTys);
2196 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2197 EVT VT = RetTys[i];
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00002198 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman19515b42011-05-17 18:29:03 +00002199 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2200 for (unsigned j = 0; j != NumRegs; ++j) {
2201 ISD::InputArg MyFlags;
Patrik Hagglunddfcf33a2012-12-19 11:48:16 +00002202 MyFlags.VT = RegisterVT;
Eli Friedman19515b42011-05-17 18:29:03 +00002203 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling034b94b2012-12-19 07:18:57 +00002204 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman19515b42011-05-17 18:29:03 +00002205 MyFlags.Flags.setSExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002206 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman19515b42011-05-17 18:29:03 +00002207 MyFlags.Flags.setZExt();
Bill Wendling034b94b2012-12-19 07:18:57 +00002208 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman19515b42011-05-17 18:29:03 +00002209 MyFlags.Flags.setInReg();
2210 Ins.push_back(MyFlags);
2211 }
2212 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00002213
Eli Friedman19515b42011-05-17 18:29:03 +00002214 // Now handle call return values.
2215 SmallVector<unsigned, 4> UsedRegs;
2216 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher471e4222011-06-08 23:55:35 +00002217 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendling56cb2292012-07-19 00:11:40 +00002218 I->getParent()->getContext());
Eli Friedman19515b42011-05-17 18:29:03 +00002219 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2220 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2221 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2222 EVT CopyVT = RVLocs[i].getValVT();
2223 unsigned CopyReg = ResultReg + i;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002224
Evan Chengf3d4efe2008-09-07 09:09:33 +00002225 // If this is a call to a function that returns an fp value on the x87 fp
2226 // stack, but where we prefer to use the value in xmm registers, copy it
2227 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman19515b42011-05-17 18:29:03 +00002228 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002229 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00002230 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002231 CopyVT = MVT::f80;
Craig Topperc9099502012-04-20 06:31:50 +00002232 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesen098c7ac2011-06-30 23:42:18 +00002233 }
Jakob Stoklund Olesen9bbe4d62011-06-28 18:32:28 +00002234 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
2235 CopyReg);
2236 } else {
2237 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2238 CopyReg).addReg(RVLocs[i].getLocReg());
2239 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Chengf3d4efe2008-09-07 09:09:33 +00002240 }
2241
Eli Friedman19515b42011-05-17 18:29:03 +00002242 if (CopyVT != RVLocs[i].getValVT()) {
Evan Chengf3d4efe2008-09-07 09:09:33 +00002243 // Round the F80 the right size, which also moves to the appropriate xmm
2244 // register. This is accomplished by storing the F80 value in memory and
2245 // then loading it back. Ewww...
Eli Friedman19515b42011-05-17 18:29:03 +00002246 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson825b72b2009-08-11 20:47:22 +00002247 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Chengf3d4efe2008-09-07 09:09:33 +00002248 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene3f2bf852009-11-12 20:49:22 +00002249 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohman84023e02010-07-10 09:00:22 +00002250 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2251 TII.get(Opc)), FI)
Eli Friedman19515b42011-05-17 18:29:03 +00002252 .addReg(CopyReg);
Owen Anderson825b72b2009-08-11 20:47:22 +00002253 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohman84023e02010-07-10 09:00:22 +00002254 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman19515b42011-05-17 18:29:03 +00002255 TII.get(Opc), ResultReg + i), FI);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002256 }
Eli Friedmanc93943b2011-05-17 02:36:59 +00002257 }
Eli Friedmancdc9a202011-05-17 00:13:47 +00002258
Eli Friedman19515b42011-05-17 18:29:03 +00002259 if (RVLocs.size())
2260 UpdateValueMap(I, ResultReg, RVLocs.size());
2261
Dan Gohmandb497122010-06-18 23:28:01 +00002262 // Set all unused physreg defs as dead.
2263 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2264
Evan Chengf3d4efe2008-09-07 09:09:33 +00002265 return true;
2266}
2267
2268
Dan Gohman99b21822008-08-28 23:21:34 +00002269bool
Dan Gohman46510a72010-04-15 01:51:59 +00002270X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohman99b21822008-08-28 23:21:34 +00002271 switch (I->getOpcode()) {
2272 default: break;
Evan Cheng8b19e562008-09-03 06:44:39 +00002273 case Instruction::Load:
Dan Gohman3df24e62008-09-03 23:12:08 +00002274 return X86SelectLoad(I);
Owen Anderson79924eb2008-09-04 16:48:33 +00002275 case Instruction::Store:
2276 return X86SelectStore(I);
Dan Gohman84023e02010-07-10 09:00:22 +00002277 case Instruction::Ret:
2278 return X86SelectRet(I);
Dan Gohman6e3f05f2008-09-04 23:26:51 +00002279 case Instruction::ICmp:
2280 case Instruction::FCmp:
2281 return X86SelectCmp(I);
Dan Gohmand89ae992008-09-05 01:06:14 +00002282 case Instruction::ZExt:
2283 return X86SelectZExt(I);
2284 case Instruction::Br:
2285 return X86SelectBranch(I);
Evan Chengf3d4efe2008-09-07 09:09:33 +00002286 case Instruction::Call:
2287 return X86SelectCall(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00002288 case Instruction::LShr:
2289 case Instruction::AShr:
2290 case Instruction::Shl:
2291 return X86SelectShift(I);
Eli Bendersky50125482013-04-17 20:10:13 +00002292 case Instruction::SDiv:
2293 case Instruction::UDiv:
2294 case Instruction::SRem:
2295 case Instruction::URem:
2296 return X86SelectDivRem(I);
Dan Gohmanc39f4db2008-09-05 18:30:08 +00002297 case Instruction::Select:
2298 return X86SelectSelect(I);
Evan Cheng10a8d9c2008-09-07 08:47:42 +00002299 case Instruction::Trunc:
2300 return X86SelectTrunc(I);
Dan Gohman78efce62008-09-10 21:02:08 +00002301 case Instruction::FPExt:
2302 return X86SelectFPExt(I);
2303 case Instruction::FPTrunc:
2304 return X86SelectFPTrunc(I);
Dan Gohman474d3b32009-03-13 23:53:06 +00002305 case Instruction::IntToPtr: // Deliberate fall-through.
2306 case Instruction::PtrToInt: {
Owen Andersone50ed302009-08-10 22:56:29 +00002307 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2308 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohman474d3b32009-03-13 23:53:06 +00002309 if (DstVT.bitsGT(SrcVT))
2310 return X86SelectZExt(I);
2311 if (DstVT.bitsLT(SrcVT))
2312 return X86SelectTrunc(I);
2313 unsigned Reg = getRegForValue(I->getOperand(0));
2314 if (Reg == 0) return false;
2315 UpdateValueMap(I, Reg);
2316 return true;
2317 }
Dan Gohman99b21822008-08-28 23:21:34 +00002318 }
2319
2320 return false;
2321}
2322
Dan Gohman46510a72010-04-15 01:51:59 +00002323unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sands1440e8b2010-11-03 11:35:31 +00002324 MVT VT;
Chris Lattner160f6cc2008-10-15 05:07:36 +00002325 if (!isTypeLegal(C->getType(), VT))
Michael Liaofaa11592012-08-30 00:30:16 +00002326 return 0;
2327
2328 // Can't handle alternate code models yet.
2329 if (TM.getCodeModel() != CodeModel::Small)
2330 return 0;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002331
Owen Anderson95267a12008-09-05 00:06:23 +00002332 // Get opcode and regclass of the output for the given load instruction.
2333 unsigned Opc = 0;
2334 const TargetRegisterClass *RC = NULL;
Duncan Sands1440e8b2010-11-03 11:35:31 +00002335 switch (VT.SimpleTy) {
Michael Liaofaa11592012-08-30 00:30:16 +00002336 default: return 0;
Owen Anderson825b72b2009-08-11 20:47:22 +00002337 case MVT::i8:
Owen Anderson95267a12008-09-05 00:06:23 +00002338 Opc = X86::MOV8rm;
Craig Topperc9099502012-04-20 06:31:50 +00002339 RC = &X86::GR8RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002340 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002341 case MVT::i16:
Owen Anderson95267a12008-09-05 00:06:23 +00002342 Opc = X86::MOV16rm;
Craig Topperc9099502012-04-20 06:31:50 +00002343 RC = &X86::GR16RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002344 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002345 case MVT::i32:
Owen Anderson95267a12008-09-05 00:06:23 +00002346 Opc = X86::MOV32rm;
Craig Topperc9099502012-04-20 06:31:50 +00002347 RC = &X86::GR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002348 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002349 case MVT::i64:
Owen Anderson95267a12008-09-05 00:06:23 +00002350 // Must be in x86-64 mode.
2351 Opc = X86::MOV64rm;
Craig Topperc9099502012-04-20 06:31:50 +00002352 RC = &X86::GR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002353 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002354 case MVT::f32:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002355 if (X86ScalarSSEf32) {
2356 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperc9099502012-04-20 06:31:50 +00002357 RC = &X86::FR32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002358 } else {
2359 Opc = X86::LD_Fp32m;
Craig Topperc9099502012-04-20 06:31:50 +00002360 RC = &X86::RFP32RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002361 }
2362 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002363 case MVT::f64:
Bruno Cardoso Lopes645b8be2011-09-03 00:46:42 +00002364 if (X86ScalarSSEf64) {
2365 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperc9099502012-04-20 06:31:50 +00002366 RC = &X86::FR64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002367 } else {
2368 Opc = X86::LD_Fp64m;
Craig Topperc9099502012-04-20 06:31:50 +00002369 RC = &X86::RFP64RegClass;
Owen Anderson95267a12008-09-05 00:06:23 +00002370 }
2371 break;
Owen Anderson825b72b2009-08-11 20:47:22 +00002372 case MVT::f80:
Dan Gohman5af29c22008-09-26 01:39:32 +00002373 // No f80 support yet.
Michael Liaofaa11592012-08-30 00:30:16 +00002374 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002375 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002376
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002377 // Materialize addresses with LEA instructions.
Owen Anderson95267a12008-09-05 00:06:23 +00002378 if (isa<GlobalValue>(C)) {
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002379 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002380 if (X86SelectAddress(C, AM)) {
Chris Lattner685090f2011-04-17 17:12:08 +00002381 // If the expression is just a basereg, then we're done, otherwise we need
2382 // to emit an LEA.
2383 if (AM.BaseType == X86AddressMode::RegBase &&
2384 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2385 return AM.Base.Reg;
Eric Christopher471e4222011-06-08 23:55:35 +00002386
Chris Lattner685090f2011-04-17 17:12:08 +00002387 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002388 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002389 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2390 TII.get(Opc), ResultReg), AM);
Owen Anderson95267a12008-09-05 00:06:23 +00002391 return ResultReg;
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002392 }
Evan Cheng0de588f2008-09-05 21:00:03 +00002393 return 0;
Owen Anderson95267a12008-09-05 00:06:23 +00002394 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002395
Owen Anderson3b217c62008-09-06 01:11:01 +00002396 // MachineConstantPool wants an explicit alignment.
Evan Cheng1606e8e2009-03-13 07:51:59 +00002397 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002398 if (Align == 0) {
2399 // Alignment of vector types. FIXME!
Duncan Sands777d2302009-05-09 07:06:46 +00002400 Align = TD.getTypeAllocSize(C->getType());
Owen Anderson3b217c62008-09-06 01:11:01 +00002401 }
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002402
Dan Gohman5396c992008-09-30 01:21:32 +00002403 // x86-32 PIC requires a PIC base register for constant pools.
2404 unsigned PICBase = 0;
Chris Lattner89da6992009-06-27 01:31:51 +00002405 unsigned char OpFlag = 0;
Chris Lattnere2c92082009-07-10 21:00:45 +00002406 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattner15a380a2009-07-09 04:39:06 +00002407 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohmana4160c32010-07-07 16:29:44 +00002408 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002409 } else if (Subtarget->isPICStyleGOT()) {
2410 OpFlag = X86II::MO_GOTOFF;
Dan Gohmana4160c32010-07-07 16:29:44 +00002411 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattner15a380a2009-07-09 04:39:06 +00002412 } else if (Subtarget->isPICStyleRIPRel() &&
2413 TM.getCodeModel() == CodeModel::Small) {
2414 PICBase = X86::RIP;
Chris Lattner89da6992009-06-27 01:31:51 +00002415 }
Dan Gohman5396c992008-09-30 01:21:32 +00002416
2417 // Create the load from the constant pool.
Dan Gohman0586d912008-09-10 20:11:02 +00002418 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman2ff7fd12008-09-19 22:16:54 +00002419 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002420 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2421 TII.get(Opc), ResultReg),
Chris Lattner89da6992009-06-27 01:31:51 +00002422 MCPOffset, PICBase, OpFlag);
Dan Gohman5396c992008-09-30 01:21:32 +00002423
Owen Anderson95267a12008-09-05 00:06:23 +00002424 return ResultReg;
2425}
2426
Dan Gohman46510a72010-04-15 01:51:59 +00002427unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002428 // Fail on dynamic allocas. At this point, getRegForValue has already
2429 // checked its CSE maps, so if we're here trying to handle a dynamic
2430 // alloca, we're not going to succeed. X86SelectAddress has a
2431 // check for dynamic allocas, because it's called directly from
2432 // various places, but TargetMaterializeAlloca also needs a check
2433 // in order to avoid recursion between getRegForValue,
2434 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohmana4160c32010-07-07 16:29:44 +00002435 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohman4e6ed5e2008-10-03 01:27:49 +00002436 return 0;
2437
Dan Gohman0586d912008-09-10 20:11:02 +00002438 X86AddressMode AM;
Chris Lattner0aa43de2009-07-10 05:33:42 +00002439 if (!X86SelectAddress(C, AM))
Dan Gohman0586d912008-09-10 20:11:02 +00002440 return 0;
2441 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper44d23822012-02-22 05:59:10 +00002442 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman0586d912008-09-10 20:11:02 +00002443 unsigned ResultReg = createResultReg(RC);
Dan Gohman84023e02010-07-10 09:00:22 +00002444 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2445 TII.get(Opc), ResultReg), AM);
Dan Gohman0586d912008-09-10 20:11:02 +00002446 return ResultReg;
2447}
2448
Eli Friedman2790ba82011-04-27 22:41:55 +00002449unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2450 MVT VT;
2451 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002452 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002453
2454 // Get opcode and regclass for the given zero.
2455 unsigned Opc = 0;
2456 const TargetRegisterClass *RC = NULL;
2457 switch (VT.SimpleTy) {
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002458 default: return 0;
Craig Topperf4cfc442012-08-11 17:53:00 +00002459 case MVT::f32:
2460 if (X86ScalarSSEf32) {
2461 Opc = X86::FsFLD0SS;
2462 RC = &X86::FR32RegClass;
2463 } else {
2464 Opc = X86::LD_Fp032;
2465 RC = &X86::RFP32RegClass;
2466 }
2467 break;
2468 case MVT::f64:
2469 if (X86ScalarSSEf64) {
2470 Opc = X86::FsFLD0SD;
2471 RC = &X86::FR64RegClass;
2472 } else {
2473 Opc = X86::LD_Fp064;
2474 RC = &X86::RFP64RegClass;
2475 }
2476 break;
2477 case MVT::f80:
2478 // No f80 support yet.
Jakub Staszak1c1c4932012-11-15 19:40:29 +00002479 return 0;
Eli Friedman2790ba82011-04-27 22:41:55 +00002480 }
2481
2482 unsigned ResultReg = createResultReg(RC);
2483 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2484 return ResultReg;
2485}
2486
2487
Eli Bendersky75299e32013-04-19 22:29:18 +00002488bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2489 const LoadInst *LI) {
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002490 X86AddressMode AM;
2491 if (!X86SelectAddress(LI->getOperand(0), AM))
2492 return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002493
Craig Topperdca72542012-08-11 17:46:16 +00002494 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002495
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002496 unsigned Size = TD.getTypeAllocSize(LI->getType());
2497 unsigned Alignment = LI->getAlignment();
2498
2499 SmallVector<MachineOperand, 8> AddrOps;
2500 AM.getFullAddress(AddrOps);
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002501
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002502 MachineInstr *Result =
2503 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2504 if (Result == 0) return false;
Wesley Peckbf17cfa2010-11-23 03:31:01 +00002505
Chris Lattnerb99fdee2011-01-16 02:27:38 +00002506 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnerbeac75d2010-09-05 02:18:34 +00002507 MI->eraseFromParent();
2508 return true;
2509}
2510
2511
Evan Chengc3f44b02008-09-03 00:03:49 +00002512namespace llvm {
Bob Wilsond49edb72012-08-03 04:06:28 +00002513 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2514 const TargetLibraryInfo *libInfo) {
2515 return new X86FastISel(funcInfo, libInfo);
Evan Chengc3f44b02008-09-03 00:03:49 +00002516 }
Dan Gohman99b21822008-08-28 23:21:34 +00002517}