blob: 9b842b74e4cb9111d9a64a78120e38a84390124b [file] [log] [blame]
Dan Gohmandaef7f42008-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"
Juergen Ributzka9969d3e2013-11-08 23:28:16 +000017#include "X86CallingConv.h"
Evan Cheng3a0c5e52011-06-23 17:54:54 +000018#include "X86ISelLowering.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "X86InstrBuilder.h"
Evan Cheng8f23ec92008-09-03 01:04:47 +000020#include "X86RegisterInfo.h"
21#include "X86Subtarget.h"
Dan Gohman49e19e92008-08-22 00:20:26 +000022#include "X86TargetMachine.h"
Dan Gohmand7b5ce32010-07-10 09:00:22 +000023#include "llvm/CodeGen/Analysis.h"
Evan Cheng24422d42008-09-03 00:03:49 +000024#include "llvm/CodeGen/FastISel.h"
Dan Gohman87fb4e82010-07-07 16:29:44 +000025#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson50288e32008-09-05 00:06:23 +000026#include "llvm/CodeGen/MachineConstantPool.h"
Evan Cheng6c8f55c2008-09-07 09:09:33 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson0673a8a2008-08-29 17:45:56 +000028#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000029#include "llvm/IR/CallingConv.h"
30#include "llvm/IR/DerivedTypes.h"
31#include "llvm/IR/GlobalAlias.h"
32#include "llvm/IR/GlobalVariable.h"
33#include "llvm/IR/Instructions.h"
34#include "llvm/IR/IntrinsicInst.h"
35#include "llvm/IR/Operator.h"
Evan Cheng6c8f55c2008-09-07 09:09:33 +000036#include "llvm/Support/CallSite.h"
Torok Edwin56d06592009-07-11 20:10:48 +000037#include "llvm/Support/ErrorHandling.h"
Dan Gohman6e005fd2008-09-18 23:23:44 +000038#include "llvm/Support/GetElementPtrTypeIterator.h"
Evan Chengd10089a2010-01-27 00:00:57 +000039#include "llvm/Target/TargetOptions.h"
Evan Cheng24422d42008-09-03 00:03:49 +000040using namespace llvm;
41
Chris Lattnerd5ac9d82009-03-08 18:44:31 +000042namespace {
Wesley Peck527da1b2010-11-23 03:31:01 +000043
Evan Cheng24422d42008-09-03 00:03:49 +000044class X86FastISel : public FastISel {
45 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
46 /// make the right decision when generating code for different targets.
47 const X86Subtarget *Subtarget;
Evan Cheng6c8f55c2008-09-07 09:09:33 +000048
Wesley Peck527da1b2010-11-23 03:31:01 +000049 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Cheng6c8f55c2008-09-07 09:09:33 +000050 /// floating point ops.
51 /// When SSE is available, use it for f32 operations.
52 /// When SSE2 is available, use it for f64 operations.
53 bool X86ScalarSSEf64;
54 bool X86ScalarSSEf32;
55
Evan Chenga41ee292008-09-03 06:44:39 +000056public:
Bob Wilson3e6fa462012-08-03 04:06:28 +000057 explicit X86FastISel(FunctionLoweringInfo &funcInfo,
58 const TargetLibraryInfo *libInfo)
59 : FastISel(funcInfo, libInfo) {
Evan Cheng8f23ec92008-09-03 01:04:47 +000060 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topperb0c0f722012-01-10 06:54:16 +000061 X86ScalarSSEf64 = Subtarget->hasSSE2();
62 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng8f23ec92008-09-03 01:04:47 +000063 }
Evan Cheng24422d42008-09-03 00:03:49 +000064
Dan Gohmanbcaf6812010-04-15 01:51:59 +000065 virtual bool TargetSelectInstruction(const Instruction *I);
Evan Cheng24422d42008-09-03 00:03:49 +000066
Eli Bendersky90dd3e72013-04-19 22:29:18 +000067 /// \brief The specified machine instr operand is a vreg, and that
Chris Lattnereeba0c72010-09-05 02:18:34 +000068 /// vreg is being provided by the specified load instruction. If possible,
69 /// try to fold the load as an operand to the instruction, returning true if
70 /// possible.
Eli Bendersky90dd3e72013-04-19 22:29:18 +000071 virtual bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
72 const LoadInst *LI);
Wesley Peck527da1b2010-11-23 03:31:01 +000073
Chad Rosiera92ef4b2013-02-25 21:59:35 +000074 virtual bool FastLowerArguments();
75
Dan Gohmandaef7f42008-08-19 21:45:35 +000076#include "X86GenFastISel.inc"
Evan Chenga41ee292008-09-03 06:44:39 +000077
78private:
Dan Gohmanbcaf6812010-04-15 01:51:59 +000079 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peck527da1b2010-11-23 03:31:01 +000080
Owen Anderson53aa7a92009-08-10 22:56:29 +000081 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, unsigned &RR);
Evan Chengf5bc7e52008-09-05 21:00:03 +000082
Craig Topper4f55b0e2013-07-17 05:57:45 +000083 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM,
84 bool Aligned = false);
85 bool X86FastEmitStore(EVT VT, unsigned ValReg, const X86AddressMode &AM,
86 bool Aligned = false);
Evan Cheng6500d172008-09-08 06:35:17 +000087
Owen Anderson53aa7a92009-08-10 22:56:29 +000088 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +000089 unsigned &ResultReg);
Wesley Peck527da1b2010-11-23 03:31:01 +000090
Dan Gohmanbcaf6812010-04-15 01:51:59 +000091 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
92 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman39d82f92008-09-10 20:11:02 +000093
Dan Gohmanbcaf6812010-04-15 01:51:59 +000094 bool X86SelectLoad(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +000095
Dan Gohmanbcaf6812010-04-15 01:51:59 +000096 bool X86SelectStore(const Instruction *I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +000097
Dan Gohmand7b5ce32010-07-10 09:00:22 +000098 bool X86SelectRet(const Instruction *I);
99
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000100 bool X86SelectCmp(const Instruction *I);
Dan Gohmana5753b32008-09-05 01:06:14 +0000101
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000102 bool X86SelectZExt(const Instruction *I);
Dan Gohmana5753b32008-09-05 01:06:14 +0000103
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000104 bool X86SelectBranch(const Instruction *I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +0000105
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000106 bool X86SelectShift(const Instruction *I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +0000107
Eli Bendersky24a36eb2013-04-17 20:10:13 +0000108 bool X86SelectDivRem(const Instruction *I);
109
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000110 bool X86SelectSelect(const Instruction *I);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000111
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000112 bool X86SelectTrunc(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +0000113
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000114 bool X86SelectFPExt(const Instruction *I);
115 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohmanbf646f22008-09-10 21:02:08 +0000116
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000117 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
118 bool X86SelectCall(const Instruction *I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000119
Eli Friedmancd2124a2011-06-10 23:39:36 +0000120 bool DoSelectCall(const Instruction *I, const char *MemIntName);
121
Dan Gohman3691d502008-09-25 15:24:26 +0000122 const X86InstrInfo *getInstrInfo() const {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000123 return getTargetMachine()->getInstrInfo();
124 }
125 const X86TargetMachine *getTargetMachine() const {
126 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman3691d502008-09-25 15:24:26 +0000127 }
128
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000129 bool handleConstantAddresses(const Value *V, X86AddressMode &AM);
130
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000131 unsigned TargetMaterializeConstant(const Constant *C);
Dan Gohman39d82f92008-09-10 20:11:02 +0000132
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000133 unsigned TargetMaterializeAlloca(const AllocaInst *C);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000134
Eli Friedman406c4712011-04-27 22:41:55 +0000135 unsigned TargetMaterializeFloatZero(const ConstantFP *CF);
136
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000137 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
138 /// computed in an SSE register, not on the X87 floating point stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000139 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson9f944592009-08-11 20:47:22 +0000140 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
141 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000142 }
143
Chris Lattner229907c2011-07-18 04:54:35 +0000144 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmanbcc69142011-04-27 01:45:07 +0000145
Eli Friedman60afcc22011-05-20 22:21:04 +0000146 bool IsMemcpySmall(uint64_t Len);
147
Eli Friedmanbcc69142011-04-27 01:45:07 +0000148 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
149 X86AddressMode SrcAM, uint64_t Len);
Evan Cheng24422d42008-09-03 00:03:49 +0000150};
Wesley Peck527da1b2010-11-23 03:31:01 +0000151
Chris Lattnerd5ac9d82009-03-08 18:44:31 +0000152} // end anonymous namespace.
Dan Gohmand58f3e32008-08-28 23:21:34 +0000153
Chris Lattner229907c2011-07-18 04:54:35 +0000154bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000155 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
156 if (evt == MVT::Other || !evt.isSimple())
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000157 // Unhandled type. Halt "fast" selection and bail.
158 return false;
Duncan Sandsf5dda012010-11-03 11:35:31 +0000159
160 VT = evt.getSimpleVT();
Dan Gohman50331362008-09-30 00:48:39 +0000161 // For now, require SSE/SSE2 for performing floating-point operations,
162 // since x87 requires additional work.
Owen Anderson9f944592009-08-11 20:47:22 +0000163 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topper490c45c2012-08-11 17:53:00 +0000164 return false;
Owen Anderson9f944592009-08-11 20:47:22 +0000165 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topper490c45c2012-08-11 17:53:00 +0000166 return false;
Dan Gohman50331362008-09-30 00:48:39 +0000167 // Similarly, no f80 support yet.
Owen Anderson9f944592009-08-11 20:47:22 +0000168 if (VT == MVT::f80)
Dan Gohman50331362008-09-30 00:48:39 +0000169 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000170 // We only handle legal types. For example, on x86-32 the instruction
171 // selector contains all of the 64-bit instructions from x86-64,
172 // under the assumption that i64 won't be used if the target doesn't
173 // support it.
Owen Anderson9f944592009-08-11 20:47:22 +0000174 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000175}
176
177#include "X86GenCallingConv.inc"
178
Evan Chengf5bc7e52008-09-05 21:00:03 +0000179/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000180/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000181/// Return true and the result register by reference if it is possible.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000182bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000183 unsigned &ResultReg) {
184 // Get opcode and regclass of the output for the given load instruction.
185 unsigned Opc = 0;
186 const TargetRegisterClass *RC = NULL;
Owen Anderson9f944592009-08-11 20:47:22 +0000187 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000188 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000189 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +0000190 case MVT::i8:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000191 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +0000192 RC = &X86::GR8RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000193 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000194 case MVT::i16:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000195 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +0000196 RC = &X86::GR16RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000197 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000198 case MVT::i32:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000199 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000200 RC = &X86::GR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000201 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000202 case MVT::i64:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000203 // Must be in x86-64 mode.
204 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000205 RC = &X86::GR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000206 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000207 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000208 if (X86ScalarSSEf32) {
209 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +0000210 RC = &X86::FR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000211 } else {
212 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +0000213 RC = &X86::RFP32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000214 }
215 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000216 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000217 if (X86ScalarSSEf64) {
218 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +0000219 RC = &X86::FR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000220 } else {
221 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +0000222 RC = &X86::RFP64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000223 }
224 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000225 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +0000226 // No f80 support yet.
227 return false;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000228 }
229
230 ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000231 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
232 DL, TII.get(Opc), ResultReg), AM);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000233 return true;
234}
235
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000236/// X86FastEmitStore - Emit a machine instruction to store a value Val of
237/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
238/// and a displacement offset, or a GlobalAddress,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000239/// i.e. V. Return true if it is possible.
240bool
Craig Topper4f55b0e2013-07-17 05:57:45 +0000241X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg,
242 const X86AddressMode &AM, bool Aligned) {
Dan Gohman8f658ba2008-09-08 16:31:35 +0000243 // Get opcode and regclass of the output for the given store instruction.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000244 unsigned Opc = 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000245 switch (VT.getSimpleVT().SimpleTy) {
246 case MVT::f80: // No f80 support yet.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000247 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000248 case MVT::i1: {
249 // Mask out all but lowest bit.
Craig Topperabadc662012-04-20 06:31:50 +0000250 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000251 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Craig Topper4f55b0e2013-07-17 05:57:45 +0000252 TII.get(X86::AND8ri), AndResult).addReg(ValReg).addImm(1);
253 ValReg = AndResult;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000254 }
255 // FALLTHROUGH, handling i1 as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000256 case MVT::i8: Opc = X86::MOV8mr; break;
257 case MVT::i16: Opc = X86::MOV16mr; break;
258 case MVT::i32: Opc = X86::MOV32mr; break;
259 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
260 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000261 Opc = X86ScalarSSEf32 ?
262 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000263 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000264 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000265 Opc = X86ScalarSSEf64 ?
266 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000267 break;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000268 case MVT::v4f32:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000269 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000270 Opc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000271 else
Craig Topper55475d42013-07-17 06:58:23 +0000272 Opc = Subtarget->hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000273 break;
274 case MVT::v2f64:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000275 if (Aligned)
Craig Topperad1fff92013-07-18 07:16:44 +0000276 Opc = Subtarget->hasAVX() ? X86::VMOVAPDmr : X86::MOVAPDmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000277 else
Craig Topperad1fff92013-07-18 07:16:44 +0000278 Opc = Subtarget->hasAVX() ? X86::VMOVUPDmr : X86::MOVUPDmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000279 break;
280 case MVT::v4i32:
281 case MVT::v2i64:
282 case MVT::v8i16:
283 case MVT::v16i8:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000284 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000285 Opc = Subtarget->hasAVX() ? X86::VMOVDQAmr : X86::MOVDQAmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000286 else
Craig Topper55475d42013-07-17 06:58:23 +0000287 Opc = Subtarget->hasAVX() ? X86::VMOVDQUmr : X86::MOVDQUmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000288 break;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000289 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000290
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000291 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
Craig Topper4f55b0e2013-07-17 05:57:45 +0000292 DL, TII.get(Opc)), AM).addReg(ValReg);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000293 return true;
294}
295
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000296bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Craig Topper4f55b0e2013-07-17 05:57:45 +0000297 const X86AddressMode &AM, bool Aligned) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000298 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000299 if (isa<ConstantPointerNull>(Val))
300 Val = Constant::getNullValue(TD.getIntPtrType(Val->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000301
Chris Lattner3ba29352008-10-15 05:30:52 +0000302 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000303 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000304 unsigned Opc = 0;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000305 bool Signed = true;
Owen Anderson9f944592009-08-11 20:47:22 +0000306 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000307 default: break;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000308 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000309 case MVT::i8: Opc = X86::MOV8mi; break;
310 case MVT::i16: Opc = X86::MOV16mi; break;
311 case MVT::i32: Opc = X86::MOV32mi; break;
312 case MVT::i64:
Chris Lattner3ba29352008-10-15 05:30:52 +0000313 // Must be a 32-bit sign extended value.
Jakub Staszak11d1aee2012-11-15 19:05:23 +0000314 if (isInt<32>(CI->getSExtValue()))
Chris Lattner3ba29352008-10-15 05:30:52 +0000315 Opc = X86::MOV64mi32;
316 break;
317 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000318
Chris Lattner3ba29352008-10-15 05:30:52 +0000319 if (Opc) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000320 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt,
321 DL, TII.get(Opc)), AM)
John McCall796583e2010-04-06 23:35:53 +0000322 .addImm(Signed ? (uint64_t) CI->getSExtValue() :
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000323 CI->getZExtValue());
Chris Lattner3ba29352008-10-15 05:30:52 +0000324 return true;
325 }
326 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000327
Chris Lattner3ba29352008-10-15 05:30:52 +0000328 unsigned ValReg = getRegForValue(Val);
329 if (ValReg == 0)
Wesley Peck527da1b2010-11-23 03:31:01 +0000330 return false;
331
Craig Topper4f55b0e2013-07-17 05:57:45 +0000332 return X86FastEmitStore(VT, ValReg, AM, Aligned);
Chris Lattner3ba29352008-10-15 05:30:52 +0000333}
334
Evan Cheng6500d172008-09-08 06:35:17 +0000335/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
336/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
337/// ISD::SIGN_EXTEND).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000338bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
339 unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +0000340 unsigned &ResultReg) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000341 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
342 Src, /*TODO: Kill=*/false);
Jakub Staszak701cc972013-02-14 21:50:09 +0000343 if (RR == 0)
Owen Anderson453564b2008-09-11 19:44:55 +0000344 return false;
Jakub Staszak701cc972013-02-14 21:50:09 +0000345
346 ResultReg = RR;
347 return true;
Evan Cheng6500d172008-09-08 06:35:17 +0000348}
349
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000350bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
351 // Handle constant address.
352 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
353 // Can't handle alternate code models yet.
354 if (TM.getCodeModel() != CodeModel::Small)
355 return false;
356
357 // Can't handle TLS yet.
358 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
359 if (GVar->isThreadLocal())
360 return false;
361
362 // Can't handle TLS yet, part 2 (this is slightly crazy, but this is how
363 // it works...).
364 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
365 if (const GlobalVariable *GVar =
366 dyn_cast_or_null<GlobalVariable>(GA->resolveAliasedGlobal(false)))
367 if (GVar->isThreadLocal())
368 return false;
369
370 // RIP-relative addresses can't have additional register operands, so if
371 // we've already folded stuff into the addressing mode, just force the
372 // global value into its own register, which we can use as the basereg.
373 if (!Subtarget->isPICStyleRIPRel() ||
374 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
375 // Okay, we've committed to selecting this global. Set up the address.
376 AM.GV = GV;
377
378 // Allow the subtarget to classify the global.
379 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
380
381 // If this reference is relative to the pic base, set it now.
382 if (isGlobalRelativeToPICBase(GVFlags)) {
383 // FIXME: How do we know Base.Reg is free??
384 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
385 }
386
387 // Unless the ABI requires an extra load, return a direct reference to
388 // the global.
389 if (!isGlobalStubReference(GVFlags)) {
390 if (Subtarget->isPICStyleRIPRel()) {
391 // Use rip-relative addressing if we can. Above we verified that the
392 // base and index registers are unused.
393 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
394 AM.Base.Reg = X86::RIP;
395 }
396 AM.GVOpFlags = GVFlags;
397 return true;
398 }
399
400 // Ok, we need to do a load from a stub. If we've already loaded from
401 // this stub, reuse the loaded pointer, otherwise emit the load now.
402 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
403 unsigned LoadReg;
404 if (I != LocalValueMap.end() && I->second != 0) {
405 LoadReg = I->second;
406 } else {
407 // Issue load from stub.
408 unsigned Opc = 0;
409 const TargetRegisterClass *RC = NULL;
410 X86AddressMode StubAM;
411 StubAM.Base.Reg = AM.Base.Reg;
412 StubAM.GV = GV;
413 StubAM.GVOpFlags = GVFlags;
414
415 // Prepare for inserting code in the local-value area.
416 SavePoint SaveInsertPt = enterLocalValueArea();
417
418 if (TLI.getPointerTy() == MVT::i64) {
419 Opc = X86::MOV64rm;
420 RC = &X86::GR64RegClass;
421
422 if (Subtarget->isPICStyleRIPRel())
423 StubAM.Base.Reg = X86::RIP;
424 } else {
425 Opc = X86::MOV32rm;
426 RC = &X86::GR32RegClass;
427 }
428
429 LoadReg = createResultReg(RC);
430 MachineInstrBuilder LoadMI =
431 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), LoadReg);
432 addFullAddress(LoadMI, StubAM);
433
434 // Ok, back to normal mode.
435 leaveLocalValueArea(SaveInsertPt);
436
437 // Prevent loading GV stub multiple times in same MBB.
438 LocalValueMap[V] = LoadReg;
439 }
440
441 // Now construct the final address. Note that the Disp, Scale,
442 // and Index values may already be set here.
443 AM.Base.Reg = LoadReg;
444 AM.GV = 0;
445 return true;
446 }
447 }
448
449 // If all else fails, try to materialize the value in a register.
450 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
451 if (AM.Base.Reg == 0) {
452 AM.Base.Reg = getRegForValue(V);
453 return AM.Base.Reg != 0;
454 }
455 if (AM.IndexReg == 0) {
456 assert(AM.Scale == 1 && "Scale with no index!");
457 AM.IndexReg = getRegForValue(V);
458 return AM.IndexReg != 0;
459 }
460 }
461
462 return false;
463}
464
Dan Gohman39d82f92008-09-10 20:11:02 +0000465/// X86SelectAddress - Attempt to fill in an address from the given value.
466///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000467bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000468 SmallVector<const Value *, 32> GEPs;
Bill Wendling585a9012013-09-24 00:13:08 +0000469redo_gep:
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000470 const User *U = NULL;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000471 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000472 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000473 // Don't walk into other basic blocks; it's possible we haven't
474 // visited them yet, so the instructions may not yet be assigned
475 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000476 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
477 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
478 Opcode = I->getOpcode();
479 U = I;
480 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000481 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000482 Opcode = C->getOpcode();
483 U = C;
484 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000485
Chris Lattner229907c2011-07-18 04:54:35 +0000486 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000487 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000488 // Fast instruction selection doesn't support the special
489 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000490 return false;
491
Dan Gohman6e005fd2008-09-18 23:23:44 +0000492 switch (Opcode) {
493 default: break;
494 case Instruction::BitCast:
495 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000496 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000497
498 case Instruction::IntToPtr:
499 // Look past no-op inttoptrs.
500 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000501 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000502 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000503
504 case Instruction::PtrToInt:
505 // Look past no-op ptrtoints.
506 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000507 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000508 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000509
510 case Instruction::Alloca: {
511 // Do static allocas.
512 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000513 DenseMap<const AllocaInst*, int>::iterator SI =
514 FuncInfo.StaticAllocaMap.find(A);
515 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000516 AM.BaseType = X86AddressMode::FrameIndexBase;
517 AM.Base.FrameIndex = SI->second;
518 return true;
519 }
520 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000521 }
522
523 case Instruction::Add: {
524 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000525 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000526 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
527 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000528 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000529 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000530 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000531 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000532 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000533 break;
534 }
535
536 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000537 X86AddressMode SavedAM = AM;
538
Dan Gohman6e005fd2008-09-18 23:23:44 +0000539 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000540 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000541 unsigned IndexReg = AM.IndexReg;
542 unsigned Scale = AM.Scale;
543 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000544 // Iterate through the indices, folding what we can. Constants can be
545 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000546 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000547 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000548 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000549 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000550 const StructLayout *SL = TD.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000551 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
552 continue;
553 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000554
Chris Lattner4b026b92011-04-17 17:05:12 +0000555 // A array/variable index is always of the form i*S where S is the
556 // constant scale size. See if we can push the scale into immediates.
557 uint64_t S = TD.getTypeAllocSize(GTI.getIndexedType());
558 for (;;) {
559 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
560 // Constant-offset addressing.
561 Disp += CI->getSExtValue() * S;
562 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000563 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000564 if (canFoldAddIntoGEP(U, Op)) {
565 // A compatible add with a constant operand. Fold the constant.
Chris Lattner4b026b92011-04-17 17:05:12 +0000566 ConstantInt *CI =
567 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
568 Disp += CI->getSExtValue() * S;
569 // Iterate on the other operand.
570 Op = cast<AddOperator>(Op)->getOperand(0);
571 continue;
572 }
573 if (IndexReg == 0 &&
574 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
575 (S == 1 || S == 2 || S == 4 || S == 8)) {
576 // Scaled-index addressing.
577 Scale = S;
578 IndexReg = getRegForGEPIndex(Op).first;
579 if (IndexReg == 0)
580 return false;
581 break;
582 }
583 // Unsupported.
584 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000585 }
586 }
Bill Wendling585a9012013-09-24 00:13:08 +0000587
Dan Gohman2564b902008-09-26 20:04:15 +0000588 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000589 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000590 break;
Bill Wendling585a9012013-09-24 00:13:08 +0000591
Dan Gohman6e005fd2008-09-18 23:23:44 +0000592 AM.IndexReg = IndexReg;
593 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000594 AM.Disp = (uint32_t)Disp;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000595 GEPs.push_back(V);
Bill Wendling585a9012013-09-24 00:13:08 +0000596
597 if (const GetElementPtrInst *GEP =
598 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
599 // Ok, the GEP indices were covered by constant-offset and scaled-index
600 // addressing. Update the address state and move on to examining the base.
601 V = GEP;
602 goto redo_gep;
603 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner6ce8e242010-03-04 19:48:19 +0000604 return true;
Bill Wendling585a9012013-09-24 00:13:08 +0000605 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000606
Chris Lattner4b026b92011-04-17 17:05:12 +0000607 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000608 // our address and just match the value instead of completely failing.
609 AM = SavedAM;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000610
611 for (SmallVectorImpl<const Value *>::reverse_iterator
612 I = GEPs.rbegin(), E = GEPs.rend(); I != E; ++I)
613 if (handleConstantAddresses(*I, AM))
614 return true;
615
616 return false;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000617 unsupported_gep:
618 // Ok, the GEP indices weren't all covered.
619 break;
620 }
621 }
622
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000623 return handleConstantAddresses(V, AM);
Dan Gohman39d82f92008-09-10 20:11:02 +0000624}
625
Chris Lattner8212d372009-07-10 05:33:42 +0000626/// X86SelectCallAddress - Attempt to fill in an address from the given value.
627///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000628bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
629 const User *U = NULL;
Chris Lattner8212d372009-07-10 05:33:42 +0000630 unsigned Opcode = Instruction::UserOp1;
Quentin Colombet778dba12013-10-14 22:32:09 +0000631 const Instruction *I = dyn_cast<Instruction>(V);
Quentin Colombetf34568b2013-10-22 21:29:08 +0000632 // Record if the value is defined in the same basic block.
633 //
634 // This information is crucial to know whether or not folding an
635 // operand is valid.
636 // Indeed, FastISel generates or reuses a virtual register for all
637 // operands of all instructions it selects. Obviously, the definition and
638 // its uses must use the same virtual register otherwise the produced
639 // code is incorrect.
640 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
641 // registers for values that are alive across basic blocks. This ensures
642 // that the values are consistently set between across basic block, even
643 // if different instruction selection mechanisms are used (e.g., a mix of
644 // SDISel and FastISel).
645 // For values local to a basic block, the instruction selection process
646 // generates these virtual registers with whatever method is appropriate
647 // for its needs. In particular, FastISel and SDISel do not share the way
648 // local virtual registers are set.
649 // Therefore, this is impossible (or at least unsafe) to share values
650 // between basic blocks unless they use the same instruction selection
651 // method, which is not guarantee for X86.
652 // Moreover, things like hasOneUse could not be used accurately, if we
653 // allow to reference values across basic blocks whereas they are not
654 // alive across basic blocks initially.
Quentin Colombet778dba12013-10-14 22:32:09 +0000655 bool InMBB = true;
656 if (I) {
Chris Lattner8212d372009-07-10 05:33:42 +0000657 Opcode = I->getOpcode();
658 U = I;
Quentin Colombet778dba12013-10-14 22:32:09 +0000659 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000660 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000661 Opcode = C->getOpcode();
662 U = C;
663 }
664
665 switch (Opcode) {
666 default: break;
667 case Instruction::BitCast:
Quentin Colombet778dba12013-10-14 22:32:09 +0000668 // Look past bitcasts if its operand is in the same BB.
669 if (InMBB)
670 return X86SelectCallAddress(U->getOperand(0), AM);
671 break;
Chris Lattner8212d372009-07-10 05:33:42 +0000672
673 case Instruction::IntToPtr:
Quentin Colombet778dba12013-10-14 22:32:09 +0000674 // Look past no-op inttoptrs if its operand is in the same BB.
675 if (InMBB &&
676 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000677 return X86SelectCallAddress(U->getOperand(0), AM);
678 break;
679
680 case Instruction::PtrToInt:
Quentin Colombet778dba12013-10-14 22:32:09 +0000681 // Look past no-op ptrtoints if its operand is in the same BB.
682 if (InMBB &&
683 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000684 return X86SelectCallAddress(U->getOperand(0), AM);
685 break;
686 }
687
688 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000689 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000690 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000691 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000692 return false;
693
694 // RIP-relative addresses can't have additional register operands.
695 if (Subtarget->isPICStyleRIPRel() &&
696 (AM.Base.Reg != 0 || AM.IndexReg != 0))
697 return false;
698
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000699 // Can't handle DLLImport.
Nico Rieck7157bb72014-01-14 15:22:47 +0000700 if (GV->hasDLLImportStorageClass())
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000701 return false;
702
703 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000704 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000705 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000706 return false;
707
708 // Okay, we've committed to selecting this global. Set up the basic address.
709 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000710
Chris Lattner7277a802009-07-10 05:45:15 +0000711 // No ABI requires an extra load for anything other than DLLImport, which
712 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000713 if (Subtarget->isPICStyleRIPRel()) {
714 // Use rip-relative addressing if we can. Above we verified that the
715 // base and index registers are unused.
716 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
717 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000718 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000719 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
720 } else if (Subtarget->isPICStyleGOT()) {
721 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000722 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000723
Chris Lattner8212d372009-07-10 05:33:42 +0000724 return true;
725 }
726
727 // If all else fails, try to materialize the value in a register.
728 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
729 if (AM.Base.Reg == 0) {
730 AM.Base.Reg = getRegForValue(V);
731 return AM.Base.Reg != 0;
732 }
733 if (AM.IndexReg == 0) {
734 assert(AM.Scale == 1 && "Scale with no index!");
735 AM.IndexReg = getRegForValue(V);
736 return AM.IndexReg != 0;
737 }
738 }
739
740 return false;
741}
742
743
Owen Anderson4f948bd2008-09-04 07:08:58 +0000744/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000745bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000746 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000747 const StoreInst *S = cast<StoreInst>(I);
748
749 if (S->isAtomic())
750 return false;
751
Craig Topper4f55b0e2013-07-17 05:57:45 +0000752 unsigned SABIAlignment =
753 TD.getABITypeAlignment(S->getValueOperand()->getType());
754 bool Aligned = S->getAlignment() == 0 || S->getAlignment() >= SABIAlignment;
755
Duncan Sandsf5dda012010-11-03 11:35:31 +0000756 MVT VT;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000757 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000758 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000759
Dan Gohman39d82f92008-09-10 20:11:02 +0000760 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +0000761 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000762 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000763
Craig Topper4f55b0e2013-07-17 05:57:45 +0000764 return X86FastEmitStore(VT, I->getOperand(0), AM, Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000765}
766
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000767/// X86SelectRet - Select and emit code to implement ret instructions.
768bool X86FastISel::X86SelectRet(const Instruction *I) {
769 const ReturnInst *Ret = cast<ReturnInst>(I);
770 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000771 const X86MachineFunctionInfo *X86MFInfo =
772 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000773
774 if (!FuncInfo.CanLowerReturn)
775 return false;
776
777 CallingConv::ID CC = F.getCallingConv();
778 if (CC != CallingConv::C &&
779 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000780 CC != CallingConv::X86_FastCall &&
781 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000782 return false;
783
Charles Davise8f297c2013-07-12 06:02:35 +0000784 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000785 return false;
786
787 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000788 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000789 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000790
791 // fastcc with -tailcallopt is intended to provide a guaranteed
792 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000793 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000794 return false;
795
796 // Let SDISel handle vararg functions.
797 if (F.isVarArg())
798 return false;
799
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000800 // Build a list of return value registers.
801 SmallVector<unsigned, 4> RetRegs;
802
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000803 if (Ret->getNumOperands() > 0) {
804 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000805 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000806
807 // Analyze operands of the call, assigning locations to each operand.
808 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000809 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +0000810 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000811 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000812
813 const Value *RV = Ret->getOperand(0);
814 unsigned Reg = getRegForValue(RV);
815 if (Reg == 0)
816 return false;
817
818 // Only handle a single return value for now.
819 if (ValLocs.size() != 1)
820 return false;
821
822 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +0000823
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000824 // Don't bother handling odd stuff for now.
825 if (VA.getLocInfo() != CCValAssign::Full)
826 return false;
827 // Only handle register returns for now.
828 if (!VA.isRegLoc())
829 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000830
831 // The calling-convention tables for x87 returns don't tell
832 // the whole story.
833 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
834 return false;
835
Eli Friedman6fc94dd2011-05-18 23:13:10 +0000836 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +0000837 EVT SrcVT = TLI.getValueType(RV->getType());
838 EVT DstVT = VA.getValVT();
839 // Special handling for extended integers.
840 if (SrcVT != DstVT) {
841 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
842 return false;
843
844 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
845 return false;
846
847 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
848
849 if (SrcVT == MVT::i1) {
850 if (Outs[0].Flags.isSExt())
851 return false;
852 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
853 SrcVT = MVT::i8;
854 }
855 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
856 ISD::SIGN_EXTEND;
857 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
858 SrcReg, /*TODO: Kill=*/false);
859 }
860
861 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000862 unsigned DstReg = VA.getLocReg();
863 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000864 // Avoid a cross-class copy. This is very unlikely.
865 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000866 return false;
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000867 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
868 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000869
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000870 // Add register to return instruction.
871 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000872 }
873
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000874 // The x86-64 ABI for returning structs by value requires that we copy
875 // the sret argument into %rax for the return. We saved the argument into
876 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000877 // and into %rax. We also do the same with %eax for Win32.
878 if (F.hasStructRetAttr() &&
879 (Subtarget->is64Bit() || Subtarget->isTargetWindows())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000880 unsigned Reg = X86MFInfo->getSRetReturnReg();
881 assert(Reg &&
882 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000883 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000884 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000885 RetReg).addReg(Reg);
886 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000887 }
888
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000889 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000890 MachineInstrBuilder MIB =
David Woodhouse79dd5052014-01-08 12:58:07 +0000891 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000892 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
893 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000894 return true;
895}
896
Evan Chenga41ee292008-09-03 06:44:39 +0000897/// X86SelectLoad - Select and emit code to implement load instructions.
898///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000899bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000900 // Atomic loads need special handling.
901 if (cast<LoadInst>(I)->isAtomic())
902 return false;
903
Duncan Sandsf5dda012010-11-03 11:35:31 +0000904 MVT VT;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000905 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +0000906 return false;
907
Dan Gohman39d82f92008-09-10 20:11:02 +0000908 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +0000909 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000910 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000911
Evan Chengf5bc7e52008-09-05 21:00:03 +0000912 unsigned ResultReg = 0;
Dan Gohman39d82f92008-09-10 20:11:02 +0000913 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000914 UpdateValueMap(I, ResultReg);
915 return true;
Evan Chenga41ee292008-09-03 06:44:39 +0000916 }
Evan Chengf5bc7e52008-09-05 21:00:03 +0000917 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000918}
919
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000920static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000921 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +0000922 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
923 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000924
Owen Anderson9f944592009-08-11 20:47:22 +0000925 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +0000926 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000927 case MVT::i8: return X86::CMP8rr;
928 case MVT::i16: return X86::CMP16rr;
929 case MVT::i32: return X86::CMP32rr;
930 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000931 case MVT::f32:
932 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
933 case MVT::f64:
934 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +0000935 }
Dan Gohman1ab1d312008-10-02 22:15:21 +0000936}
937
Chris Lattner88f47542008-10-15 04:13:29 +0000938/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
939/// of the comparison, return an opcode that works for the compare (e.g.
940/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000941static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +0000942 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +0000943 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +0000944 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000945 case MVT::i8: return X86::CMP8ri;
946 case MVT::i16: return X86::CMP16ri;
947 case MVT::i32: return X86::CMP32ri;
948 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +0000949 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
950 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +0000951 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +0000952 return X86::CMP64ri32;
953 return 0;
954 }
Chris Lattner88f47542008-10-15 04:13:29 +0000955}
956
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000957bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
958 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +0000959 unsigned Op0Reg = getRegForValue(Op0);
960 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000961
Chris Lattnere388725a2008-10-15 05:18:04 +0000962 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000963 if (isa<ConstantPointerNull>(Op1))
964 Op1 = Constant::getNullValue(TD.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000965
Chris Lattnerd46b9512008-10-15 04:26:38 +0000966 // We have two options: compare with register or immediate. If the RHS of
967 // the compare is an immediate that we can fold into this compare, use
968 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000969 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +0000970 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000971 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareImmOpc))
972 .addReg(Op0Reg)
973 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +0000974 return true;
975 }
976 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000977
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000978 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +0000979 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000980
Chris Lattnerd46b9512008-10-15 04:26:38 +0000981 unsigned Op1Reg = getRegForValue(Op1);
982 if (Op1Reg == 0) return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000983 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CompareOpc))
984 .addReg(Op0Reg)
985 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +0000986
Chris Lattnerd46b9512008-10-15 04:26:38 +0000987 return true;
988}
989
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000990bool X86FastISel::X86SelectCmp(const Instruction *I) {
991 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000992
Duncan Sandsf5dda012010-11-03 11:35:31 +0000993 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +0000994 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +0000995 return false;
996
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000997 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattnera3596db2008-10-15 03:47:17 +0000998 unsigned SetCCOpc;
Chris Lattnerf32ce222008-10-15 03:52:54 +0000999 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001000 switch (CI->getPredicate()) {
1001 case CmpInst::FCMP_OEQ: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001002 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
1003 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001004
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001005 unsigned EReg = createResultReg(&X86::GR8RegClass);
1006 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001007 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETEr), EReg);
1008 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1009 TII.get(X86::SETNPr), NPReg);
Wesley Peck527da1b2010-11-23 03:31:01 +00001010 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Dale Johannesen9bba9022009-02-13 02:33:27 +00001011 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattnera3596db2008-10-15 03:47:17 +00001012 UpdateValueMap(I, ResultReg);
1013 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001014 }
1015 case CmpInst::FCMP_UNE: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001016 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
1017 return false;
1018
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001019 unsigned NEReg = createResultReg(&X86::GR8RegClass);
1020 unsigned PReg = createResultReg(&X86::GR8RegClass);
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001021 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETNEr), NEReg);
1022 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SETPr), PReg);
1023 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::OR8rr),ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001024 .addReg(PReg).addReg(NEReg);
Chris Lattnera3596db2008-10-15 03:47:17 +00001025 UpdateValueMap(I, ResultReg);
1026 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001027 }
Chris Lattnerf32ce222008-10-15 03:52:54 +00001028 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1029 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1030 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
1031 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
1032 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1033 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
1034 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
1035 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1036 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
1037 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
1038 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1039 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peck527da1b2010-11-23 03:31:01 +00001040
Chris Lattnerf32ce222008-10-15 03:52:54 +00001041 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1042 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1043 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1044 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1045 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1046 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
1047 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
1048 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
1049 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
1050 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001051 default:
1052 return false;
1053 }
1054
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001055 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001056 if (SwapArgs)
Chris Lattnerd46b9512008-10-15 04:26:38 +00001057 std::swap(Op0, Op1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001058
Chris Lattnerd46b9512008-10-15 04:26:38 +00001059 // Emit a compare of Op0/Op1.
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001060 if (!X86FastEmitCompare(Op0, Op1, VT))
1061 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001062
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001063 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(SetCCOpc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001064 UpdateValueMap(I, ResultReg);
1065 return true;
1066}
Evan Chenga41ee292008-09-03 06:44:39 +00001067
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001068bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001069 EVT DstVT = TLI.getValueType(I->getType());
1070 if (!TLI.isTypeLegal(DstVT))
1071 return false;
1072
1073 unsigned ResultReg = getRegForValue(I->getOperand(0));
1074 if (ResultReg == 0)
1075 return false;
1076
Tim Northover04eb4232013-05-30 10:43:18 +00001077 // Handle zero-extension from i1 to i8, which is common.
Craig Topper56710102013-08-15 02:33:50 +00001078 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northover04eb4232013-05-30 10:43:18 +00001079 if (SrcVT.SimpleTy == MVT::i1) {
1080 // Set the high bits to zero.
1081 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1082 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001083
Tim Northover04eb4232013-05-30 10:43:18 +00001084 if (ResultReg == 0)
1085 return false;
1086 }
1087
1088 if (DstVT == MVT::i64) {
1089 // Handle extension to 64-bits via sub-register shenanigans.
1090 unsigned MovInst;
1091
1092 switch (SrcVT.SimpleTy) {
1093 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1094 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1095 case MVT::i32: MovInst = X86::MOV32rr; break;
1096 default: llvm_unreachable("Unexpected zext to i64 source type");
1097 }
1098
1099 unsigned Result32 = createResultReg(&X86::GR32RegClass);
1100 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(MovInst), Result32)
1101 .addReg(ResultReg);
1102
1103 ResultReg = createResultReg(&X86::GR64RegClass);
1104 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::SUBREG_TO_REG),
1105 ResultReg)
1106 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1107 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001108 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1109 ResultReg, /*Kill=*/true);
1110 if (ResultReg == 0)
1111 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001112 }
1113
Eli Friedmanc7035512011-05-25 23:49:02 +00001114 UpdateValueMap(I, ResultReg);
1115 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001116}
1117
Chris Lattnerd46b9512008-10-15 04:26:38 +00001118
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001119bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001120 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001121 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001122 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001123 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1124 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001125
Dan Gohman42ef6692010-08-21 02:32:36 +00001126 // Fold the common case of a conditional branch with a comparison
1127 // in the same block (values defined on other blocks may not have
1128 // initialized registers).
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001129 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001130 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001131 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001132
Dan Gohman1ab1d312008-10-02 22:15:21 +00001133 // Try to take advantage of fallthrough opportunities.
1134 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001135 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001136 std::swap(TrueMBB, FalseMBB);
1137 Predicate = CmpInst::getInversePredicate(Predicate);
1138 }
1139
Chris Lattner0ce717a2008-10-15 03:58:05 +00001140 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1141 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1142
Dan Gohman1ab1d312008-10-02 22:15:21 +00001143 switch (Predicate) {
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001144 case CmpInst::FCMP_OEQ:
1145 std::swap(TrueMBB, FalseMBB);
1146 Predicate = CmpInst::FCMP_UNE;
1147 // FALL THROUGH
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001148 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1149 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1150 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1151 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1152 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1153 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1154 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1155 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1156 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1157 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1158 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1159 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1160 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peck527da1b2010-11-23 03:31:01 +00001161
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001162 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1163 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1164 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1165 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1166 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1167 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1168 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1169 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1170 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1171 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001172 default:
1173 return false;
1174 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001175
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001176 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner47bef252008-10-15 04:02:26 +00001177 if (SwapArgs)
1178 std::swap(Op0, Op1);
1179
Chris Lattnerd46b9512008-10-15 04:26:38 +00001180 // Emit a compare of the LHS and RHS, setting the flags.
1181 if (!X86FastEmitCompare(Op0, Op1, VT))
1182 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001183
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001184 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(BranchOpc))
1185 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001186
1187 if (Predicate == CmpInst::FCMP_UNE) {
1188 // X86 requires a second branch to handle UNE (and OEQ,
1189 // which is mapped to UNE above).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001190 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JP_4))
1191 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001192 }
1193
Stuart Hastings0125b642010-06-17 22:43:56 +00001194 FastEmitBranch(FalseMBB, DL);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001195 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohman1ab1d312008-10-02 22:15:21 +00001196 return true;
1197 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001198 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1199 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1200 // typically happen for _Bool and C++ bools.
1201 MVT SourceVT;
1202 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1203 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1204 unsigned TestOpc = 0;
1205 switch (SourceVT.SimpleTy) {
1206 default: break;
1207 case MVT::i8: TestOpc = X86::TEST8ri; break;
1208 case MVT::i16: TestOpc = X86::TEST16ri; break;
1209 case MVT::i32: TestOpc = X86::TEST32ri; break;
1210 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1211 }
1212 if (TestOpc) {
1213 unsigned OpReg = getRegForValue(TI->getOperand(0));
1214 if (OpReg == 0) return false;
1215 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TestOpc))
1216 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001217
Chris Lattnerc59290a2011-04-19 04:26:32 +00001218 unsigned JmpOpc = X86::JNE_4;
1219 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1220 std::swap(TrueMBB, FalseMBB);
1221 JmpOpc = X86::JE_4;
1222 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001223
Chris Lattnerc59290a2011-04-19 04:26:32 +00001224 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001225 .addMBB(TrueMBB);
1226 FastEmitBranch(FalseMBB, DL);
1227 FuncInfo.MBB->addSuccessor(TrueMBB);
1228 return true;
1229 }
1230 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001231 }
1232
1233 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001234 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1235 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001236 unsigned OpReg = getRegForValue(BI->getCondition());
1237 if (OpReg == 0) return false;
1238
Eli Friedman0eea0292011-04-27 01:34:27 +00001239 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1240 .addReg(OpReg).addImm(1);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001241 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::JNE_4))
1242 .addMBB(TrueMBB);
Stuart Hastings0125b642010-06-17 22:43:56 +00001243 FastEmitBranch(FalseMBB, DL);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001244 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmana5753b32008-09-05 01:06:14 +00001245 return true;
1246}
1247
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001248bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001249 unsigned CReg = 0, OpReg = 0;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001250 const TargetRegisterClass *RC = NULL;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001251 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001252 CReg = X86::CL;
1253 RC = &X86::GR8RegClass;
1254 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001255 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1256 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1257 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001258 default: return false;
1259 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001260 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001261 CReg = X86::CX;
1262 RC = &X86::GR16RegClass;
1263 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001264 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1265 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1266 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001267 default: return false;
1268 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001269 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001270 CReg = X86::ECX;
1271 RC = &X86::GR32RegClass;
1272 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001273 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1274 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1275 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001276 default: return false;
1277 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001278 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001279 CReg = X86::RCX;
1280 RC = &X86::GR64RegClass;
1281 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001282 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1283 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1284 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001285 default: return false;
1286 }
1287 } else {
1288 return false;
1289 }
1290
Duncan Sandsf5dda012010-11-03 11:35:31 +00001291 MVT VT;
1292 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001293 return false;
1294
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001295 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1296 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001297
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001298 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1299 if (Op1Reg == 0) return false;
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001300 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1301 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001302
1303 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001304 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001305 if (CReg != X86::CL)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001306 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1307 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001308 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001309
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001310 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001311 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpReg), ResultReg)
1312 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001313 UpdateValueMap(I, ResultReg);
1314 return true;
1315}
1316
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001317bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1318 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1319 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1320 const static bool S = true; // IsSigned
1321 const static bool U = false; // !IsSigned
1322 const static unsigned Copy = TargetOpcode::COPY;
1323 // For the X86 DIV/IDIV instruction, in most cases the dividend
1324 // (numerator) must be in a specific register pair highreg:lowreg,
1325 // producing the quotient in lowreg and the remainder in highreg.
1326 // For most data types, to set up the instruction, the dividend is
1327 // copied into lowreg, and lowreg is sign-extended or zero-extended
1328 // into highreg. The exception is i8, where the dividend is defined
1329 // as a single register rather than a register pair, and we
1330 // therefore directly sign-extend or zero-extend the dividend into
1331 // lowreg, instead of copying, and ignore the highreg.
1332 const static struct DivRemEntry {
1333 // The following portion depends only on the data type.
1334 const TargetRegisterClass *RC;
1335 unsigned LowInReg; // low part of the register pair
1336 unsigned HighInReg; // high part of the register pair
1337 // The following portion depends on both the data type and the operation.
1338 struct DivRemResult {
1339 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1340 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1341 // highreg, or copying a zero into highreg.
1342 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1343 // zero/sign-extending into lowreg for i8.
1344 unsigned DivRemResultReg; // Register containing the desired result.
1345 bool IsOpSigned; // Whether to use signed or unsigned form.
1346 } ResultTable[NumOps];
1347 } OpTable[NumTypes] = {
1348 { &X86::GR8RegClass, X86::AX, 0, {
1349 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1350 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1351 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1352 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1353 }
1354 }, // i8
1355 { &X86::GR16RegClass, X86::AX, X86::DX, {
1356 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1357 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001358 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1359 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001360 }
1361 }, // i16
1362 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1363 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1364 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1365 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1366 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1367 }
1368 }, // i32
1369 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1370 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1371 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001372 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1373 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001374 }
1375 }, // i64
1376 };
1377
1378 MVT VT;
1379 if (!isTypeLegal(I->getType(), VT))
1380 return false;
1381
1382 unsigned TypeIndex, OpIndex;
1383 switch (VT.SimpleTy) {
1384 default: return false;
1385 case MVT::i8: TypeIndex = 0; break;
1386 case MVT::i16: TypeIndex = 1; break;
1387 case MVT::i32: TypeIndex = 2; break;
1388 case MVT::i64: TypeIndex = 3;
1389 if (!Subtarget->is64Bit())
1390 return false;
1391 break;
1392 }
1393
1394 switch (I->getOpcode()) {
1395 default: llvm_unreachable("Unexpected div/rem opcode");
1396 case Instruction::SDiv: OpIndex = 0; break;
1397 case Instruction::SRem: OpIndex = 1; break;
1398 case Instruction::UDiv: OpIndex = 2; break;
1399 case Instruction::URem: OpIndex = 3; break;
1400 }
1401
1402 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1403 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1404 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1405 if (Op0Reg == 0)
1406 return false;
1407 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1408 if (Op1Reg == 0)
1409 return false;
1410
1411 // Move op0 into low-order input register.
1412 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1413 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1414 // Zero-extend or sign-extend into high-order input register.
1415 if (OpEntry.OpSignExtend) {
1416 if (OpEntry.IsOpSigned)
1417 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1418 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001419 else {
1420 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001421 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001422 TII.get(X86::MOV32r0), Zero32);
1423
1424 // Copy the zero into the appropriate sub/super/identical physical
1425 // register. Unfortunately the operations needed are not uniform enough to
1426 // fit neatly into the table above.
1427 if (VT.SimpleTy == MVT::i16) {
1428 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001429 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001430 .addReg(Zero32, 0, X86::sub_16bit);
1431 } else if (VT.SimpleTy == MVT::i32) {
1432 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001433 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001434 .addReg(Zero32);
1435 } else if (VT.SimpleTy == MVT::i64) {
1436 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1437 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1438 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1439 }
1440 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001441 }
1442 // Generate the DIV/IDIV instruction.
1443 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1444 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001445 // For i8 remainder, we can't reference AH directly, as we'll end
1446 // up with bogus copies like %R9B = COPY %AH. Reference AX
1447 // instead to prevent AH references in a REX instruction.
1448 //
1449 // The current assumption of the fast register allocator is that isel
1450 // won't generate explicit references to the GPR8_NOREX registers. If
1451 // the allocator and/or the backend get enhanced to be more robust in
1452 // that regard, this can be, and should be, removed.
1453 unsigned ResultReg = 0;
1454 if ((I->getOpcode() == Instruction::SRem ||
1455 I->getOpcode() == Instruction::URem) &&
1456 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1457 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1458 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
1459 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1460 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1461
1462 // Shift AX right by 8 bits instead of using AH.
1463 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::SHR16ri),
1464 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1465
1466 // Now reference the 8-bit subreg of the result.
1467 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1468 /*Kill=*/true, X86::sub_8bit);
1469 }
1470 // Copy the result out of the physreg if we haven't already.
1471 if (!ResultReg) {
1472 ResultReg = createResultReg(TypeEntry.RC);
1473 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Copy), ResultReg)
1474 .addReg(OpEntry.DivRemResultReg);
1475 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001476 UpdateValueMap(I, ResultReg);
1477
1478 return true;
1479}
1480
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001481bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001482 MVT VT;
1483 if (!isTypeLegal(I->getType(), VT))
Chris Lattnera0f9d492008-10-15 05:07:36 +00001484 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001485
Eric Christopher0574cc52010-09-29 23:00:29 +00001486 // We only use cmov here, if we don't have a cmov instruction bail.
1487 if (!Subtarget->hasCMov()) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001488
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001489 unsigned Opc = 0;
1490 const TargetRegisterClass *RC = NULL;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001491 if (VT == MVT::i16) {
Dan Gohmane5560182008-09-05 21:13:04 +00001492 Opc = X86::CMOVE16rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001493 RC = &X86::GR16RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001494 } else if (VT == MVT::i32) {
Dan Gohmane5560182008-09-05 21:13:04 +00001495 Opc = X86::CMOVE32rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001496 RC = &X86::GR32RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001497 } else if (VT == MVT::i64) {
Dan Gohmane5560182008-09-05 21:13:04 +00001498 Opc = X86::CMOVE64rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001499 RC = &X86::GR64RegClass;
1500 } else {
Wesley Peck527da1b2010-11-23 03:31:01 +00001501 return false;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001502 }
1503
1504 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1505 if (Op0Reg == 0) return false;
1506 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1507 if (Op1Reg == 0) return false;
1508 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1509 if (Op2Reg == 0) return false;
1510
Quentin Colombet90a646e2013-12-19 18:32:04 +00001511 // Selects operate on i1, however, Op0Reg is 8 bits width and may contain
1512 // garbage. Indeed, only the less significant bit is supposed to be accurate.
1513 // If we read more than the lsb, we may see non-zero values whereas lsb
1514 // is zero. Therefore, we have to truncate Op0Reg to i1 for the select.
Alp Tokercb402912014-01-24 17:20:08 +00001515 // This is achieved by performing TEST against 1.
Quentin Colombet90a646e2013-12-19 18:32:04 +00001516 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TEST8ri))
1517 .addReg(Op0Reg).addImm(1);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001518 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001519 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg)
1520 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001521 UpdateValueMap(I, ResultReg);
1522 return true;
1523}
1524
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001525bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001526 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001527 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00001528 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001529 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001530 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001531 unsigned OpReg = getRegForValue(V);
1532 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001533 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001534 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1535 TII.get(X86::CVTSS2SDrr), ResultReg)
1536 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00001537 UpdateValueMap(I, ResultReg);
1538 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00001539 }
1540 }
1541
1542 return false;
1543}
1544
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001545bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001546 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00001547 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001548 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001549 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00001550 unsigned OpReg = getRegForValue(V);
1551 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001552 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001553 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
1554 TII.get(X86::CVTSD2SSrr), ResultReg)
1555 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00001556 UpdateValueMap(I, ResultReg);
1557 return true;
1558 }
1559 }
1560 }
1561
1562 return false;
1563}
1564
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001565bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001566 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1567 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00001568
Eli Friedmanc7035512011-05-25 23:49:02 +00001569 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00001570 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00001571 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00001572 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00001573 return false;
1574
1575 unsigned InputReg = getRegForValue(I->getOperand(0));
1576 if (!InputReg)
1577 // Unhandled operand. Halt "fast" selection and bail.
1578 return false;
1579
Eli Friedmanc7035512011-05-25 23:49:02 +00001580 if (SrcVT == MVT::i8) {
1581 // Truncate from i8 to i1; no code needed.
1582 UpdateValueMap(I, InputReg);
1583 return true;
1584 }
Evan Chengb9286692008-09-07 08:47:42 +00001585
Eli Friedmanc7035512011-05-25 23:49:02 +00001586 if (!Subtarget->is64Bit()) {
1587 // If we're on x86-32; we can't extract an i8 from a general register.
1588 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00001589 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1590 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1591 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00001592 unsigned CopyReg = createResultReg(CopyRC);
1593 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1594 CopyReg).addReg(InputReg);
1595 InputReg = CopyReg;
1596 }
1597
1598 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00001599 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00001600 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00001601 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00001602 if (!ResultReg)
1603 return false;
1604
1605 UpdateValueMap(I, ResultReg);
1606 return true;
1607}
1608
Eli Friedman60afcc22011-05-20 22:21:04 +00001609bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1610 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1611}
1612
Eli Friedmanbcc69142011-04-27 01:45:07 +00001613bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1614 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00001615
Eli Friedmanbcc69142011-04-27 01:45:07 +00001616 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00001617 if (!IsMemcpySmall(Len))
1618 return false;
1619
1620 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00001621
1622 // We don't care about alignment here since we just emit integer accesses.
1623 while (Len) {
1624 MVT VT;
1625 if (Len >= 8 && i64Legal)
1626 VT = MVT::i64;
1627 else if (Len >= 4)
1628 VT = MVT::i32;
1629 else if (Len >= 2)
1630 VT = MVT::i16;
1631 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00001632 VT = MVT::i8;
1633 }
1634
1635 unsigned Reg;
1636 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1637 RV &= X86FastEmitStore(VT, Reg, DestAM);
1638 assert(RV && "Failed to emit load or store??");
1639
1640 unsigned Size = VT.getSizeInBits()/8;
1641 Len -= Size;
1642 DestAM.Disp += Size;
1643 SrcAM.Disp += Size;
1644 }
1645
1646 return true;
1647}
1648
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001649bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001650 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00001651 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001652 default: return false;
Chris Lattner91328b32011-04-19 05:52:03 +00001653 case Intrinsic::memcpy: {
1654 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1655 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00001656 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00001657 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001658
Eli Friedmancd2124a2011-06-10 23:39:36 +00001659 if (isa<ConstantInt>(MCI.getLength())) {
1660 // Small memcpy's are common enough that we want to do them
1661 // without a call if possible.
1662 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1663 if (IsMemcpySmall(Len)) {
1664 X86AddressMode DestAM, SrcAM;
1665 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1666 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1667 return false;
1668 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1669 return true;
1670 }
1671 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001672
Eli Friedmancd2124a2011-06-10 23:39:36 +00001673 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1674 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00001675 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001676
Eli Friedmancd2124a2011-06-10 23:39:36 +00001677 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1678 return false;
1679
1680 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00001681 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00001682 case Intrinsic::memset: {
1683 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001684
Nick Lewyckya530a4d2011-08-02 00:40:16 +00001685 if (MSI.isVolatile())
1686 return false;
1687
Eli Friedmancd2124a2011-06-10 23:39:36 +00001688 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1689 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1690 return false;
1691
1692 if (MSI.getDestAddressSpace() > 255)
1693 return false;
1694
1695 return DoSelectCall(&I, "memset");
1696 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001697 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00001698 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001699 EVT PtrTy = TLI.getPointerTy();
1700
Gabor Greif83205af2010-06-26 11:51:52 +00001701 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1702 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001703
Josh Magee22b8ba22013-12-19 03:17:11 +00001704 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
1705
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001706 // Grab the frame index.
1707 X86AddressMode AM;
1708 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00001709 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001710 return true;
1711 }
Dale Johannesend5575f22010-01-26 00:09:58 +00001712 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001713 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00001714 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00001715 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00001716 if (!X86SelectAddress(DI->getAddress(), AM))
1717 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001718 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00001719 // FIXME may need to add RegState::Debug to any registers produced,
1720 // although ESP/EBP should be the only ones at the moment.
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001721 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, II), AM).
1722 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00001723 return true;
1724 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001725 case Intrinsic::trap: {
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001726 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001727 return true;
1728 }
Bill Wendling80b34b32008-12-09 02:42:50 +00001729 case Intrinsic::sadd_with_overflow:
1730 case Intrinsic::uadd_with_overflow: {
Chris Lattner91328b32011-04-19 05:52:03 +00001731 // FIXME: Should fold immediates.
Eric Christopher0713a9d2011-06-08 23:55:35 +00001732
Bill Wendlinge25d3412008-12-09 07:55:31 +00001733 // Replace "add with overflow" intrinsics with an "add" instruction followed
Eli Friedmana4d4a012011-05-16 21:06:17 +00001734 // by a seto/setc instruction.
Bill Wendling80b34b32008-12-09 02:42:50 +00001735 const Function *Callee = I.getCalledFunction();
Chris Lattner229907c2011-07-18 04:54:35 +00001736 Type *RetTy =
Bill Wendling80b34b32008-12-09 02:42:50 +00001737 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(unsigned(0));
1738
Duncan Sandsf5dda012010-11-03 11:35:31 +00001739 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00001740 if (!isTypeLegal(RetTy, VT))
1741 return false;
1742
Gabor Greif83205af2010-06-26 11:51:52 +00001743 const Value *Op1 = I.getArgOperand(0);
1744 const Value *Op2 = I.getArgOperand(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00001745 unsigned Reg1 = getRegForValue(Op1);
1746 unsigned Reg2 = getRegForValue(Op2);
1747
1748 if (Reg1 == 0 || Reg2 == 0)
1749 // FIXME: Handle values *not* in registers.
1750 return false;
1751
1752 unsigned OpC = 0;
Owen Anderson9f944592009-08-11 20:47:22 +00001753 if (VT == MVT::i32)
Bill Wendling80b34b32008-12-09 02:42:50 +00001754 OpC = X86::ADD32rr;
Owen Anderson9f944592009-08-11 20:47:22 +00001755 else if (VT == MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00001756 OpC = X86::ADD64rr;
1757 else
1758 return false;
1759
Eli Friedmana4d4a012011-05-16 21:06:17 +00001760 // The call to CreateRegs builds two sequential registers, to store the
Sylvestre Ledru35521e22012-07-23 08:51:15 +00001761 // both the returned values.
Eli Friedmana4d4a012011-05-16 21:06:17 +00001762 unsigned ResultReg = FuncInfo.CreateRegs(I.getType());
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001763 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(OpC), ResultReg)
1764 .addReg(Reg1).addReg(Reg2);
Wesley Peck527da1b2010-11-23 03:31:01 +00001765
Chris Lattner99a8cb62009-04-12 07:36:01 +00001766 unsigned Opc = X86::SETBr;
1767 if (I.getIntrinsicID() == Intrinsic::sadd_with_overflow)
1768 Opc = X86::SETOr;
Eli Friedmana4d4a012011-05-16 21:06:17 +00001769 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg+1);
1770
1771 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00001772 return true;
1773 }
1774 }
1775}
1776
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001777bool X86FastISel::FastLowerArguments() {
1778 if (!FuncInfo.CanLowerReturn)
1779 return false;
1780
1781 const Function *F = FuncInfo.Fn;
1782 if (F->isVarArg())
1783 return false;
1784
1785 CallingConv::ID CC = F->getCallingConv();
1786 if (CC != CallingConv::C)
1787 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00001788
1789 if (Subtarget->isCallingConvWin64(CC))
1790 return false;
1791
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001792 if (!Subtarget->is64Bit())
1793 return false;
1794
1795 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
1796 unsigned Idx = 1;
1797 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1798 I != E; ++I, ++Idx) {
1799 if (Idx > 6)
1800 return false;
1801
1802 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1803 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1804 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1805 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1806 return false;
1807
1808 Type *ArgTy = I->getType();
1809 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1810 return false;
1811
1812 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00001813 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001814 switch (ArgVT.getSimpleVT().SimpleTy) {
1815 case MVT::i32:
1816 case MVT::i64:
1817 break;
1818 default:
1819 return false;
1820 }
1821 }
1822
1823 static const uint16_t GPR32ArgRegs[] = {
1824 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1825 };
1826 static const uint16_t GPR64ArgRegs[] = {
1827 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
1828 };
1829
1830 Idx = 0;
1831 const TargetRegisterClass *RC32 = TLI.getRegClassFor(MVT::i32);
1832 const TargetRegisterClass *RC64 = TLI.getRegClassFor(MVT::i64);
1833 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
1834 I != E; ++I, ++Idx) {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001835 bool is32Bit = TLI.getValueType(I->getType()) == MVT::i32;
1836 const TargetRegisterClass *RC = is32Bit ? RC32 : RC64;
1837 unsigned SrcReg = is32Bit ? GPR32ArgRegs[Idx] : GPR64ArgRegs[Idx];
1838 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1839 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1840 // Without this, EmitLiveInCopies may eliminate the livein if its only
1841 // use is a bitcast (which isn't turned into an instruction).
1842 unsigned ResultReg = createResultReg(RC);
1843 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
1844 ResultReg).addReg(DstReg, getKillRegState(true));
1845 UpdateValueMap(I, ResultReg);
1846 }
1847 return true;
1848}
1849
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001850bool X86FastISel::X86SelectCall(const Instruction *I) {
1851 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00001852 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001853
1854 // Can't handle inline asm yet.
1855 if (isa<InlineAsm>(Callee))
1856 return false;
1857
Bill Wendling80b34b32008-12-09 02:42:50 +00001858 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001859 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00001860 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001861
Chad Rosierdf42cf32012-12-11 00:18:02 +00001862 // Allow SelectionDAG isel to handle tail calls.
1863 if (cast<CallInst>(I)->isTailCall())
1864 return false;
1865
Eli Friedmancd2124a2011-06-10 23:39:36 +00001866 return DoSelectCall(I, 0);
1867}
1868
Rafael Espindola73173c52012-07-25 15:42:45 +00001869static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1870 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001871 if (Subtarget.is64Bit())
1872 return 0;
Rafael Espindola32cb5ac2013-12-12 16:06:58 +00001873 if (Subtarget.getTargetTriple().isOSMSVCRT())
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001874 return 0;
1875 CallingConv::ID CC = CS.getCallingConv();
1876 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1877 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001878 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001879 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001880 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00001881 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001882 return 4;
1883}
1884
Eli Friedmancd2124a2011-06-10 23:39:36 +00001885// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
1886bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
1887 const CallInst *CI = cast<CallInst>(I);
1888 const Value *Callee = CI->getCalledValue();
1889
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001890 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001891 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00001892 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00001893 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001894 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00001895 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
1896 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001897 return false;
1898
Evan Chengd10089a2010-01-27 00:00:57 +00001899 // fastcc with -tailcallopt is intended to provide a guaranteed
1900 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001901 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00001902 return false;
1903
Chris Lattner229907c2011-07-18 04:54:35 +00001904 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1905 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00001906 bool isVarArg = FTy->isVarArg();
1907
1908 // Don't know how to handle Win64 varargs yet. Nothing special needed for
1909 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00001910 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001911 return false;
1912
Reid Klecknerf5b76512014-01-31 23:50:57 +00001913 // Don't know about inalloca yet.
1914 if (CS.hasInAllocaArgument())
1915 return false;
1916
Dan Gohmandc53f1c2010-05-27 18:43:40 +00001917 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00001918 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00001919 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00001920 return false;
1921
Eli Friedman7b279422011-05-17 18:29:03 +00001922 // Check whether the function can return without sret-demotion.
1923 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00001924 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00001925 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00001926 *FuncInfo.MF, FTy->isVarArg(),
1927 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00001928 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00001929 return false;
1930
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001931 // Materialize callee address in a register. FIXME: GV address can be
1932 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00001933 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00001934 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00001935 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001936 unsigned CalleeOp = 0;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001937 const GlobalValue *GV = 0;
Chris Lattnerd17366a2009-06-27 04:50:14 +00001938 if (CalleeAM.GV != 0) {
Dan Gohman9801ba42008-09-19 22:16:54 +00001939 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00001940 } else if (CalleeAM.Base.Reg != 0) {
1941 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00001942 } else
1943 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00001944
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001945 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001946 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00001947 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001948 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00001949 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00001950 unsigned arg_size = CS.arg_size();
1951 Args.reserve(arg_size);
1952 ArgVals.reserve(arg_size);
1953 ArgVTs.reserve(arg_size);
1954 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001955 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001956 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00001957 // If we're lowering a mem intrinsic instead of a regular call, skip the
1958 // last two arguments, which should not passed to the underlying functions.
1959 if (MemIntName && e-i <= 2)
1960 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001961 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001962 ISD::ArgFlagsTy Flags;
1963 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001964 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001965 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001966 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001967 Flags.setZExt();
1968
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001969 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00001970 PointerType *Ty = cast<PointerType>(ArgVal->getType());
1971 Type *ElementTy = Ty->getElementType();
Eli Friedman60afcc22011-05-20 22:21:04 +00001972 unsigned FrameSize = TD.getTypeAllocSize(ElementTy);
1973 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
1974 if (!FrameAlign)
1975 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
1976 Flags.setByVal();
1977 Flags.setByValSize(FrameSize);
1978 Flags.setByValAlign(FrameAlign);
1979 if (!IsMemcpySmall(FrameSize))
1980 return false;
1981 }
1982
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001983 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00001984 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001985 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00001986 Flags.setNest();
1987
Chris Lattnerd7f7c932011-04-19 04:42:38 +00001988 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
1989 // instruction. This is safe because it is common to all fastisel supported
1990 // calling conventions on x86.
1991 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
1992 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
1993 CI->getBitWidth() == 16) {
1994 if (Flags.isSExt())
1995 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
1996 else
1997 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
1998 }
1999 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002000
Chris Lattner5f4b7832011-04-19 05:09:50 +00002001 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002002
Chris Lattner34a08c22011-04-19 05:15:59 +00002003 // Passing bools around ends up doing a trunc to i1 and passing it.
2004 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00002005 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
2006 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
2007 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00002008 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
2009 ArgReg = getRegForValue(ArgVal);
2010 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002011
Chris Lattner5f4b7832011-04-19 05:09:50 +00002012 MVT ArgVT;
2013 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002014
Chris Lattner5f4b7832011-04-19 05:09:50 +00002015 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
2016 ArgVal->hasOneUse(), 1);
2017 } else {
2018 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00002019 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002020
Chris Lattner34a08c22011-04-19 05:15:59 +00002021 if (ArgReg == 0) return false;
2022
Chris Lattner229907c2011-07-18 04:54:35 +00002023 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002024 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002025 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002026 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002027 if (ArgVT == MVT::x86mmx)
2028 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002029 unsigned OriginalAlignment = TD.getABITypeAlignment(ArgTy);
2030 Flags.setOrigAlign(OriginalAlignment);
2031
Chris Lattner5f4b7832011-04-19 05:09:50 +00002032 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002033 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002034 ArgVTs.push_back(ArgVT);
2035 ArgFlags.push_back(Flags);
2036 }
2037
2038 // Analyze operands of the call, assigning locations to each operand.
2039 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002040 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002041 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00002042
Dan Gohman47a07242010-06-01 21:09:47 +00002043 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00002044 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00002045 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00002046
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00002047 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002048
2049 // Get a count of how many bytes are to be pushed on the stack.
2050 unsigned NumBytes = CCInfo.getNextStackOffset();
2051
2052 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002053 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002054 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackDown))
2055 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002056
Chris Lattner3ba29352008-10-15 05:30:52 +00002057 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002058 // copies / loads.
2059 SmallVector<unsigned, 4> RegArgs;
2060 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2061 CCValAssign &VA = ArgLocs[i];
2062 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00002063 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002064
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002065 // Promote the value if needed.
2066 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002067 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00002068 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002069 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2070 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002071 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2072 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002073 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002074 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002075 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002076 }
2077 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002078 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2079 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002080 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2081 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002082 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002083 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002084 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002085 }
2086 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002087 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2088 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002089 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2090 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002091 if (!Emitted)
2092 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002093 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002094 if (!Emitted)
2095 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2096 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002097
Chris Lattner2d7df022011-01-05 22:26:52 +00002098 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002099 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002100 break;
2101 }
Dan Gohman8c795692009-08-05 05:33:42 +00002102 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002103 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002104 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002105 assert(BC != 0 && "Failed to emit a bitcast!");
2106 Arg = BC;
2107 ArgVT = VA.getLocVT();
2108 break;
2109 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002110 case CCValAssign::VExt:
2111 // VExt has not been implemented, so this should be impossible to reach
2112 // for now. However, fallback to Selection DAG isel once implemented.
2113 return false;
2114 case CCValAssign::Indirect:
2115 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2116 // support this.
2117 return false;
Lang Hames06234ec2014-01-14 19:56:36 +00002118 case CCValAssign::FPExt:
2119 llvm_unreachable("Unexpected loc info!");
Evan Cheng6500d172008-09-08 06:35:17 +00002120 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002121
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002122 if (VA.isRegLoc()) {
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00002123 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2124 VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002125 RegArgs.push_back(VA.getLocReg());
2126 } else {
2127 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002128 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002129 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2130 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002131 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002132 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002133 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002134 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002135
Eli Friedman60afcc22011-05-20 22:21:04 +00002136 if (Flags.isByVal()) {
2137 X86AddressMode SrcAM;
2138 SrcAM.Base.Reg = Arg;
2139 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2140 assert(Res && "memcpy length already checked!"); (void)Res;
2141 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2142 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002143 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002144 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002145 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2146 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002147 } else {
Lang Hames7d2f7b52011-10-18 22:11:33 +00002148 if (!X86FastEmitStore(ArgVT, Arg, AM))
2149 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002150 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002151 }
2152 }
2153
Dan Gohman3691d502008-09-25 15:24:26 +00002154 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00002155 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00002156 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00002157 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00002158 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2159 X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00002160 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002161
Charles Davise8f297c2013-07-12 06:02:35 +00002162 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002163 // Count the number of XMM registers allocated.
Craig Topperbef78fc2012-03-11 07:57:25 +00002164 static const uint16_t XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002165 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2166 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2167 };
2168 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
2169 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::MOV8ri),
2170 X86::AL).addImm(NumXMMRegs);
2171 }
2172
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002173 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002174 MachineInstrBuilder MIB;
2175 if (CalleeOp) {
2176 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00002177 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002178 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002179 CallOpc = X86::CALL64r;
2180 else
2181 CallOpc = X86::CALL32r;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002182 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc))
2183 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00002184
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002185 } else {
2186 // Direct call.
2187 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00002188 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002189 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002190 CallOpc = X86::CALL64pcrel32;
2191 else
2192 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00002193
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002194 // See if we need any target-specific flags on the GV operand.
2195 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002196
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002197 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2198 // external symbols most go through the PLT in PIC mode. If the symbol
2199 // has hidden or protected visibility, or if it is static or local, then
2200 // we don't need to use the PLT - we can directly call it.
2201 if (Subtarget->isTargetELF() &&
2202 TM.getRelocationModel() == Reloc::PIC_ &&
2203 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2204 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00002205 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002206 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00002207 (!Subtarget->getTargetTriple().isMacOSX() ||
2208 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002209 // PC-relative references to external symbols should go through $stub,
2210 // unless we're building with the leopard linker or later, which
2211 // automatically synthesizes these stubs.
2212 OpFlags = X86II::MO_DARWIN_STUB;
2213 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002214
2215
Eli Friedmancd2124a2011-06-10 23:39:36 +00002216 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(CallOpc));
2217 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00002218 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002219 else
2220 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002221 }
Dan Gohman3691d502008-09-25 15:24:26 +00002222
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00002223 // Add a register mask with the call-preserved registers.
2224 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2225 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2226
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002227 // Add an implicit use GOT pointer in EBX.
2228 if (Subtarget->isPICStyleGOT())
2229 MIB.addReg(X86::EBX, RegState::Implicit);
2230
Charles Davise8f297c2013-07-12 06:02:35 +00002231 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002232 MIB.addReg(X86::AL, RegState::Implicit);
2233
2234 // Add implicit physical register uses to the call.
2235 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2236 MIB.addReg(RegArgs[i], RegState::Implicit);
2237
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002238 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002239 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00002240 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002241 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00002242 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002243
Eli Friedman7b279422011-05-17 18:29:03 +00002244 // Build info for return calling conv lowering code.
2245 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2246 SmallVector<ISD::InputArg, 32> Ins;
2247 SmallVector<EVT, 4> RetTys;
2248 ComputeValueVTs(TLI, I->getType(), RetTys);
2249 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2250 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002251 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00002252 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2253 for (unsigned j = 0; j != NumRegs; ++j) {
2254 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002255 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00002256 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002257 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002258 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002259 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002260 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002261 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00002262 MyFlags.Flags.setInReg();
2263 Ins.push_back(MyFlags);
2264 }
2265 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002266
Eli Friedman7b279422011-05-17 18:29:03 +00002267 // Now handle call return values.
2268 SmallVector<unsigned, 4> UsedRegs;
2269 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002270 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002271 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002272 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2273 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2274 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2275 EVT CopyVT = RVLocs[i].getValVT();
2276 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00002277
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002278 // If this is a call to a function that returns an fp value on the x87 fp
2279 // stack, but where we prefer to use the value in xmm registers, copy it
2280 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00002281 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002282 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002283 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002284 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00002285 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002286 }
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002287 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(X86::FpPOP_RETVAL),
2288 CopyReg);
2289 } else {
2290 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(TargetOpcode::COPY),
2291 CopyReg).addReg(RVLocs[i].getLocReg());
2292 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002293 }
2294
Eli Friedman7b279422011-05-17 18:29:03 +00002295 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002296 // Round the F80 the right size, which also moves to the appropriate xmm
2297 // register. This is accomplished by storing the F80 value in memory and
2298 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00002299 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00002300 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002301 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00002302 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002303 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2304 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00002305 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00002306 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002307 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
Eli Friedman7b279422011-05-17 18:29:03 +00002308 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002309 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002310 }
Eli Friedman83ba1502011-05-17 00:13:47 +00002311
Eli Friedman7b279422011-05-17 18:29:03 +00002312 if (RVLocs.size())
2313 UpdateValueMap(I, ResultReg, RVLocs.size());
2314
Dan Gohman86936502010-06-18 23:28:01 +00002315 // Set all unused physreg defs as dead.
2316 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2317
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002318 return true;
2319}
2320
2321
Dan Gohmand58f3e32008-08-28 23:21:34 +00002322bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002323X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00002324 switch (I->getOpcode()) {
2325 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00002326 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00002327 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00002328 case Instruction::Store:
2329 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002330 case Instruction::Ret:
2331 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00002332 case Instruction::ICmp:
2333 case Instruction::FCmp:
2334 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00002335 case Instruction::ZExt:
2336 return X86SelectZExt(I);
2337 case Instruction::Br:
2338 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002339 case Instruction::Call:
2340 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002341 case Instruction::LShr:
2342 case Instruction::AShr:
2343 case Instruction::Shl:
2344 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00002345 case Instruction::SDiv:
2346 case Instruction::UDiv:
2347 case Instruction::SRem:
2348 case Instruction::URem:
2349 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002350 case Instruction::Select:
2351 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00002352 case Instruction::Trunc:
2353 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002354 case Instruction::FPExt:
2355 return X86SelectFPExt(I);
2356 case Instruction::FPTrunc:
2357 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002358 case Instruction::IntToPtr: // Deliberate fall-through.
2359 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002360 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2361 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002362 if (DstVT.bitsGT(SrcVT))
2363 return X86SelectZExt(I);
2364 if (DstVT.bitsLT(SrcVT))
2365 return X86SelectTrunc(I);
2366 unsigned Reg = getRegForValue(I->getOperand(0));
2367 if (Reg == 0) return false;
2368 UpdateValueMap(I, Reg);
2369 return true;
2370 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002371 }
2372
2373 return false;
2374}
2375
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002376unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002377 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002378 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00002379 return 0;
2380
2381 // Can't handle alternate code models yet.
2382 if (TM.getCodeModel() != CodeModel::Small)
2383 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002384
Owen Anderson50288e32008-09-05 00:06:23 +00002385 // Get opcode and regclass of the output for the given load instruction.
2386 unsigned Opc = 0;
2387 const TargetRegisterClass *RC = NULL;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002388 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00002389 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00002390 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00002391 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00002392 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002393 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002394 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00002395 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00002396 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002397 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002398 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00002399 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00002400 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002401 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002402 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00002403 // Must be in x86-64 mode.
2404 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00002405 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002406 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002407 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002408 if (X86ScalarSSEf32) {
2409 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00002410 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002411 } else {
2412 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00002413 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002414 }
2415 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002416 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002417 if (X86ScalarSSEf64) {
2418 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00002419 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002420 } else {
2421 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00002422 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002423 }
2424 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002425 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00002426 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00002427 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002428 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002429
Dan Gohman9801ba42008-09-19 22:16:54 +00002430 // Materialize addresses with LEA instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00002431 if (isa<GlobalValue>(C)) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002432 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002433 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00002434 // If the expression is just a basereg, then we're done, otherwise we need
2435 // to emit an LEA.
2436 if (AM.BaseType == X86AddressMode::RegBase &&
2437 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == 0)
2438 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002439
Chris Lattner48326602011-04-17 17:12:08 +00002440 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman9801ba42008-09-19 22:16:54 +00002441 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002442 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2443 TII.get(Opc), ResultReg), AM);
Owen Anderson50288e32008-09-05 00:06:23 +00002444 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002445 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00002446 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002447 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002448
Owen Andersond41c7162008-09-06 01:11:01 +00002449 // MachineConstantPool wants an explicit alignment.
Evan Cheng1fb8aed2009-03-13 07:51:59 +00002450 unsigned Align = TD.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002451 if (Align == 0) {
2452 // Alignment of vector types. FIXME!
Duncan Sandsaf9eaa82009-05-09 07:06:46 +00002453 Align = TD.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002454 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002455
Dan Gohman8392f0c2008-09-30 01:21:32 +00002456 // x86-32 PIC requires a PIC base register for constant pools.
2457 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00002458 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00002459 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00002460 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002461 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002462 } else if (Subtarget->isPICStyleGOT()) {
2463 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002464 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002465 } else if (Subtarget->isPICStyleRIPRel() &&
2466 TM.getCodeModel() == CodeModel::Small) {
2467 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00002468 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00002469
2470 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00002471 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00002472 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002473 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2474 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00002475 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00002476
Owen Anderson50288e32008-09-05 00:06:23 +00002477 return ResultReg;
2478}
2479
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002480unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002481 // Fail on dynamic allocas. At this point, getRegForValue has already
2482 // checked its CSE maps, so if we're here trying to handle a dynamic
2483 // alloca, we're not going to succeed. X86SelectAddress has a
2484 // check for dynamic allocas, because it's called directly from
2485 // various places, but TargetMaterializeAlloca also needs a check
2486 // in order to avoid recursion between getRegForValue,
2487 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00002488 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002489 return 0;
Reid Klecknerdfbed592014-01-31 23:45:12 +00002490 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002491
Dan Gohman39d82f92008-09-10 20:11:02 +00002492 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002493 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00002494 return 0;
2495 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00002496 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00002497 unsigned ResultReg = createResultReg(RC);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002498 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL,
2499 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00002500 return ResultReg;
2501}
2502
Eli Friedman406c4712011-04-27 22:41:55 +00002503unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2504 MVT VT;
2505 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002506 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002507
2508 // Get opcode and regclass for the given zero.
2509 unsigned Opc = 0;
2510 const TargetRegisterClass *RC = NULL;
2511 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002512 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00002513 case MVT::f32:
2514 if (X86ScalarSSEf32) {
2515 Opc = X86::FsFLD0SS;
2516 RC = &X86::FR32RegClass;
2517 } else {
2518 Opc = X86::LD_Fp032;
2519 RC = &X86::RFP32RegClass;
2520 }
2521 break;
2522 case MVT::f64:
2523 if (X86ScalarSSEf64) {
2524 Opc = X86::FsFLD0SD;
2525 RC = &X86::FR64RegClass;
2526 } else {
2527 Opc = X86::LD_Fp064;
2528 RC = &X86::RFP64RegClass;
2529 }
2530 break;
2531 case MVT::f80:
2532 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002533 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002534 }
2535
2536 unsigned ResultReg = createResultReg(RC);
2537 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DL, TII.get(Opc), ResultReg);
2538 return ResultReg;
2539}
2540
2541
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002542bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2543 const LoadInst *LI) {
Chris Lattnereeba0c72010-09-05 02:18:34 +00002544 X86AddressMode AM;
2545 if (!X86SelectAddress(LI->getOperand(0), AM))
2546 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002547
Craig Topper55406d92012-08-11 17:46:16 +00002548 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00002549
Chris Lattnereeba0c72010-09-05 02:18:34 +00002550 unsigned Size = TD.getTypeAllocSize(LI->getType());
2551 unsigned Alignment = LI->getAlignment();
2552
2553 SmallVector<MachineOperand, 8> AddrOps;
2554 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00002555
Chris Lattnereeba0c72010-09-05 02:18:34 +00002556 MachineInstr *Result =
2557 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
2558 if (Result == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002559
Chris Lattner2d186572011-01-16 02:27:38 +00002560 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00002561 MI->eraseFromParent();
2562 return true;
2563}
2564
2565
Evan Cheng24422d42008-09-03 00:03:49 +00002566namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00002567 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2568 const TargetLibraryInfo *libInfo) {
2569 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00002570 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002571}