blob: 2391984964869757a0c0e318f67f8d1b6da122d2 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "X86InstrBuilder.h"
Craig Topperc6d4efa2014-03-19 06:53:25 +000019#include "X86MachineFunctionInfo.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 Carruth219b89b2014-03-04 11:01:28 +000029#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/CallingConv.h"
31#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000032#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/GlobalAlias.h"
34#include "llvm/IR/GlobalVariable.h"
35#include "llvm/IR/Instructions.h"
36#include "llvm/IR/IntrinsicInst.h"
37#include "llvm/IR/Operator.h"
Torok Edwin56d06592009-07-11 20:10:48 +000038#include "llvm/Support/ErrorHandling.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
Craig Topper26696312014-03-18 07:27:13 +000044class X86FastISel final : public FastISel {
Evan Cheng24422d42008-09-03 00:03:49 +000045 /// 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
Craig Topper2d9361e2014-03-09 07:44:38 +000065 bool TargetSelectInstruction(const Instruction *I) override;
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.
Craig Topper2d9361e2014-03-09 07:44:38 +000071 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
72 const LoadInst *LI) override;
Wesley Peck527da1b2010-11-23 03:31:01 +000073
Craig Topper2d9361e2014-03-09 07:44:38 +000074 bool FastLowerArguments() override;
Chad Rosiera92ef4b2013-02-25 21:59:35 +000075
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
Craig Topper2d9361e2014-03-09 07:44:38 +0000131 unsigned TargetMaterializeConstant(const Constant *C) override;
Dan Gohman39d82f92008-09-10 20:11:02 +0000132
Craig Topper2d9361e2014-03-09 07:44:38 +0000133 unsigned TargetMaterializeAlloca(const AllocaInst *C) override;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000134
Craig Topper2d9361e2014-03-09 07:44:38 +0000135 unsigned TargetMaterializeFloatZero(const ConstantFP *CF) override;
Eli Friedman406c4712011-04-27 22:41:55 +0000136
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;
Craig Topper062a2ba2014-04-25 05:30:21 +0000186 const TargetRegisterClass *RC = nullptr;
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,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000232 DbgLoc, 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);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000251 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
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,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000292 DbgLoc, 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))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000300 Val = Constant::getNullValue(DL.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,
Rafael Espindolaea09c592014-02-18 22:05:46 +0000321 DbgLoc, 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.
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000358 if (GV->isThreadLocal())
359 return false;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000360
361 // RIP-relative addresses can't have additional register operands, so if
362 // we've already folded stuff into the addressing mode, just force the
363 // global value into its own register, which we can use as the basereg.
364 if (!Subtarget->isPICStyleRIPRel() ||
365 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
366 // Okay, we've committed to selecting this global. Set up the address.
367 AM.GV = GV;
368
369 // Allow the subtarget to classify the global.
370 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
371
372 // If this reference is relative to the pic base, set it now.
373 if (isGlobalRelativeToPICBase(GVFlags)) {
374 // FIXME: How do we know Base.Reg is free??
375 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
376 }
377
378 // Unless the ABI requires an extra load, return a direct reference to
379 // the global.
380 if (!isGlobalStubReference(GVFlags)) {
381 if (Subtarget->isPICStyleRIPRel()) {
382 // Use rip-relative addressing if we can. Above we verified that the
383 // base and index registers are unused.
384 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
385 AM.Base.Reg = X86::RIP;
386 }
387 AM.GVOpFlags = GVFlags;
388 return true;
389 }
390
391 // Ok, we need to do a load from a stub. If we've already loaded from
392 // this stub, reuse the loaded pointer, otherwise emit the load now.
393 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
394 unsigned LoadReg;
395 if (I != LocalValueMap.end() && I->second != 0) {
396 LoadReg = I->second;
397 } else {
398 // Issue load from stub.
399 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000400 const TargetRegisterClass *RC = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000401 X86AddressMode StubAM;
402 StubAM.Base.Reg = AM.Base.Reg;
403 StubAM.GV = GV;
404 StubAM.GVOpFlags = GVFlags;
405
406 // Prepare for inserting code in the local-value area.
407 SavePoint SaveInsertPt = enterLocalValueArea();
408
409 if (TLI.getPointerTy() == MVT::i64) {
410 Opc = X86::MOV64rm;
411 RC = &X86::GR64RegClass;
412
413 if (Subtarget->isPICStyleRIPRel())
414 StubAM.Base.Reg = X86::RIP;
415 } else {
416 Opc = X86::MOV32rm;
417 RC = &X86::GR32RegClass;
418 }
419
420 LoadReg = createResultReg(RC);
421 MachineInstrBuilder LoadMI =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000422 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg);
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000423 addFullAddress(LoadMI, StubAM);
424
425 // Ok, back to normal mode.
426 leaveLocalValueArea(SaveInsertPt);
427
428 // Prevent loading GV stub multiple times in same MBB.
429 LocalValueMap[V] = LoadReg;
430 }
431
432 // Now construct the final address. Note that the Disp, Scale,
433 // and Index values may already be set here.
434 AM.Base.Reg = LoadReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000435 AM.GV = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000436 return true;
437 }
438 }
439
440 // If all else fails, try to materialize the value in a register.
441 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
442 if (AM.Base.Reg == 0) {
443 AM.Base.Reg = getRegForValue(V);
444 return AM.Base.Reg != 0;
445 }
446 if (AM.IndexReg == 0) {
447 assert(AM.Scale == 1 && "Scale with no index!");
448 AM.IndexReg = getRegForValue(V);
449 return AM.IndexReg != 0;
450 }
451 }
452
453 return false;
454}
455
Dan Gohman39d82f92008-09-10 20:11:02 +0000456/// X86SelectAddress - Attempt to fill in an address from the given value.
457///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000458bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000459 SmallVector<const Value *, 32> GEPs;
Bill Wendling585a9012013-09-24 00:13:08 +0000460redo_gep:
Craig Topper062a2ba2014-04-25 05:30:21 +0000461 const User *U = nullptr;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000462 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000463 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000464 // Don't walk into other basic blocks; it's possible we haven't
465 // visited them yet, so the instructions may not yet be assigned
466 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000467 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
468 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
469 Opcode = I->getOpcode();
470 U = I;
471 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000472 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000473 Opcode = C->getOpcode();
474 U = C;
475 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000476
Chris Lattner229907c2011-07-18 04:54:35 +0000477 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000478 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000479 // Fast instruction selection doesn't support the special
480 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000481 return false;
482
Dan Gohman6e005fd2008-09-18 23:23:44 +0000483 switch (Opcode) {
484 default: break;
485 case Instruction::BitCast:
486 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000487 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000488
489 case Instruction::IntToPtr:
490 // Look past no-op inttoptrs.
491 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000492 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000493 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000494
495 case Instruction::PtrToInt:
496 // Look past no-op ptrtoints.
497 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000498 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000499 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000500
501 case Instruction::Alloca: {
502 // Do static allocas.
503 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000504 DenseMap<const AllocaInst*, int>::iterator SI =
505 FuncInfo.StaticAllocaMap.find(A);
506 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000507 AM.BaseType = X86AddressMode::FrameIndexBase;
508 AM.Base.FrameIndex = SI->second;
509 return true;
510 }
511 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000512 }
513
514 case Instruction::Add: {
515 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000516 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000517 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
518 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000519 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000520 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000521 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000522 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000523 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000524 break;
525 }
526
527 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000528 X86AddressMode SavedAM = AM;
529
Dan Gohman6e005fd2008-09-18 23:23:44 +0000530 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000531 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000532 unsigned IndexReg = AM.IndexReg;
533 unsigned Scale = AM.Scale;
534 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000535 // Iterate through the indices, folding what we can. Constants can be
536 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000537 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000538 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000539 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000540 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000541 const StructLayout *SL = DL.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000542 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
543 continue;
544 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000545
Chris Lattner4b026b92011-04-17 17:05:12 +0000546 // A array/variable index is always of the form i*S where S is the
547 // constant scale size. See if we can push the scale into immediates.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000548 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Chris Lattner4b026b92011-04-17 17:05:12 +0000549 for (;;) {
550 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
551 // Constant-offset addressing.
552 Disp += CI->getSExtValue() * S;
553 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000554 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000555 if (canFoldAddIntoGEP(U, Op)) {
556 // A compatible add with a constant operand. Fold the constant.
Chris Lattner4b026b92011-04-17 17:05:12 +0000557 ConstantInt *CI =
558 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
559 Disp += CI->getSExtValue() * S;
560 // Iterate on the other operand.
561 Op = cast<AddOperator>(Op)->getOperand(0);
562 continue;
563 }
564 if (IndexReg == 0 &&
565 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
566 (S == 1 || S == 2 || S == 4 || S == 8)) {
567 // Scaled-index addressing.
568 Scale = S;
569 IndexReg = getRegForGEPIndex(Op).first;
570 if (IndexReg == 0)
571 return false;
572 break;
573 }
574 // Unsupported.
575 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000576 }
577 }
Bill Wendling585a9012013-09-24 00:13:08 +0000578
Dan Gohman2564b902008-09-26 20:04:15 +0000579 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000580 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000581 break;
Bill Wendling585a9012013-09-24 00:13:08 +0000582
Dan Gohman6e005fd2008-09-18 23:23:44 +0000583 AM.IndexReg = IndexReg;
584 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000585 AM.Disp = (uint32_t)Disp;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000586 GEPs.push_back(V);
Bill Wendling585a9012013-09-24 00:13:08 +0000587
588 if (const GetElementPtrInst *GEP =
589 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
590 // Ok, the GEP indices were covered by constant-offset and scaled-index
591 // addressing. Update the address state and move on to examining the base.
592 V = GEP;
593 goto redo_gep;
594 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner6ce8e242010-03-04 19:48:19 +0000595 return true;
Bill Wendling585a9012013-09-24 00:13:08 +0000596 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000597
Chris Lattner4b026b92011-04-17 17:05:12 +0000598 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000599 // our address and just match the value instead of completely failing.
600 AM = SavedAM;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000601
602 for (SmallVectorImpl<const Value *>::reverse_iterator
603 I = GEPs.rbegin(), E = GEPs.rend(); I != E; ++I)
604 if (handleConstantAddresses(*I, AM))
605 return true;
606
607 return false;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000608 unsupported_gep:
609 // Ok, the GEP indices weren't all covered.
610 break;
611 }
612 }
613
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000614 return handleConstantAddresses(V, AM);
Dan Gohman39d82f92008-09-10 20:11:02 +0000615}
616
Chris Lattner8212d372009-07-10 05:33:42 +0000617/// X86SelectCallAddress - Attempt to fill in an address from the given value.
618///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000619bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000620 const User *U = nullptr;
Chris Lattner8212d372009-07-10 05:33:42 +0000621 unsigned Opcode = Instruction::UserOp1;
Quentin Colombet778dba12013-10-14 22:32:09 +0000622 const Instruction *I = dyn_cast<Instruction>(V);
Quentin Colombetf34568b2013-10-22 21:29:08 +0000623 // Record if the value is defined in the same basic block.
624 //
625 // This information is crucial to know whether or not folding an
626 // operand is valid.
627 // Indeed, FastISel generates or reuses a virtual register for all
628 // operands of all instructions it selects. Obviously, the definition and
629 // its uses must use the same virtual register otherwise the produced
630 // code is incorrect.
631 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
632 // registers for values that are alive across basic blocks. This ensures
633 // that the values are consistently set between across basic block, even
634 // if different instruction selection mechanisms are used (e.g., a mix of
635 // SDISel and FastISel).
636 // For values local to a basic block, the instruction selection process
637 // generates these virtual registers with whatever method is appropriate
638 // for its needs. In particular, FastISel and SDISel do not share the way
639 // local virtual registers are set.
640 // Therefore, this is impossible (or at least unsafe) to share values
641 // between basic blocks unless they use the same instruction selection
642 // method, which is not guarantee for X86.
643 // Moreover, things like hasOneUse could not be used accurately, if we
644 // allow to reference values across basic blocks whereas they are not
645 // alive across basic blocks initially.
Quentin Colombet778dba12013-10-14 22:32:09 +0000646 bool InMBB = true;
647 if (I) {
Chris Lattner8212d372009-07-10 05:33:42 +0000648 Opcode = I->getOpcode();
649 U = I;
Quentin Colombet778dba12013-10-14 22:32:09 +0000650 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000651 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000652 Opcode = C->getOpcode();
653 U = C;
654 }
655
656 switch (Opcode) {
657 default: break;
658 case Instruction::BitCast:
Quentin Colombet778dba12013-10-14 22:32:09 +0000659 // Look past bitcasts if its operand is in the same BB.
660 if (InMBB)
661 return X86SelectCallAddress(U->getOperand(0), AM);
662 break;
Chris Lattner8212d372009-07-10 05:33:42 +0000663
664 case Instruction::IntToPtr:
Quentin Colombet778dba12013-10-14 22:32:09 +0000665 // Look past no-op inttoptrs if its operand is in the same BB.
666 if (InMBB &&
667 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000668 return X86SelectCallAddress(U->getOperand(0), AM);
669 break;
670
671 case Instruction::PtrToInt:
Quentin Colombet778dba12013-10-14 22:32:09 +0000672 // Look past no-op ptrtoints if its operand is in the same BB.
673 if (InMBB &&
674 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000675 return X86SelectCallAddress(U->getOperand(0), AM);
676 break;
677 }
678
679 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000680 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000681 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000682 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000683 return false;
684
685 // RIP-relative addresses can't have additional register operands.
686 if (Subtarget->isPICStyleRIPRel() &&
687 (AM.Base.Reg != 0 || AM.IndexReg != 0))
688 return false;
689
Rafael Espindolaea09c592014-02-18 22:05:46 +0000690 // Can't handle DbgLocLImport.
Nico Rieck7157bb72014-01-14 15:22:47 +0000691 if (GV->hasDLLImportStorageClass())
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000692 return false;
693
694 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000695 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000696 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000697 return false;
698
699 // Okay, we've committed to selecting this global. Set up the basic address.
700 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000701
Chris Lattner7277a802009-07-10 05:45:15 +0000702 // No ABI requires an extra load for anything other than DLLImport, which
703 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000704 if (Subtarget->isPICStyleRIPRel()) {
705 // Use rip-relative addressing if we can. Above we verified that the
706 // base and index registers are unused.
707 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
708 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000709 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000710 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
711 } else if (Subtarget->isPICStyleGOT()) {
712 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000713 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000714
Chris Lattner8212d372009-07-10 05:33:42 +0000715 return true;
716 }
717
718 // If all else fails, try to materialize the value in a register.
719 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
720 if (AM.Base.Reg == 0) {
721 AM.Base.Reg = getRegForValue(V);
722 return AM.Base.Reg != 0;
723 }
724 if (AM.IndexReg == 0) {
725 assert(AM.Scale == 1 && "Scale with no index!");
726 AM.IndexReg = getRegForValue(V);
727 return AM.IndexReg != 0;
728 }
729 }
730
731 return false;
732}
733
734
Owen Anderson4f948bd2008-09-04 07:08:58 +0000735/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000736bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000737 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000738 const StoreInst *S = cast<StoreInst>(I);
739
740 if (S->isAtomic())
741 return false;
742
Craig Topper4f55b0e2013-07-17 05:57:45 +0000743 unsigned SABIAlignment =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000744 DL.getABITypeAlignment(S->getValueOperand()->getType());
Craig Topper4f55b0e2013-07-17 05:57:45 +0000745 bool Aligned = S->getAlignment() == 0 || S->getAlignment() >= SABIAlignment;
746
Duncan Sandsf5dda012010-11-03 11:35:31 +0000747 MVT VT;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000748 if (!isTypeLegal(I->getOperand(0)->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000749 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000750
Dan Gohman39d82f92008-09-10 20:11:02 +0000751 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +0000752 if (!X86SelectAddress(I->getOperand(1), AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000753 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000754
Craig Topper4f55b0e2013-07-17 05:57:45 +0000755 return X86FastEmitStore(VT, I->getOperand(0), AM, Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000756}
757
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000758/// X86SelectRet - Select and emit code to implement ret instructions.
759bool X86FastISel::X86SelectRet(const Instruction *I) {
760 const ReturnInst *Ret = cast<ReturnInst>(I);
761 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000762 const X86MachineFunctionInfo *X86MFInfo =
763 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000764
765 if (!FuncInfo.CanLowerReturn)
766 return false;
767
768 CallingConv::ID CC = F.getCallingConv();
769 if (CC != CallingConv::C &&
770 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000771 CC != CallingConv::X86_FastCall &&
772 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000773 return false;
774
Charles Davise8f297c2013-07-12 06:02:35 +0000775 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000776 return false;
777
778 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000779 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000780 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000781
782 // fastcc with -tailcallopt is intended to provide a guaranteed
783 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000784 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000785 return false;
786
787 // Let SDISel handle vararg functions.
788 if (F.isVarArg())
789 return false;
790
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000791 // Build a list of return value registers.
792 SmallVector<unsigned, 4> RetRegs;
793
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000794 if (Ret->getNumOperands() > 0) {
795 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000796 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000797
798 // Analyze operands of the call, assigning locations to each operand.
799 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000800 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +0000801 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000802 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000803
804 const Value *RV = Ret->getOperand(0);
805 unsigned Reg = getRegForValue(RV);
806 if (Reg == 0)
807 return false;
808
809 // Only handle a single return value for now.
810 if (ValLocs.size() != 1)
811 return false;
812
813 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +0000814
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000815 // Don't bother handling odd stuff for now.
816 if (VA.getLocInfo() != CCValAssign::Full)
817 return false;
818 // Only handle register returns for now.
819 if (!VA.isRegLoc())
820 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000821
822 // The calling-convention tables for x87 returns don't tell
823 // the whole story.
824 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
825 return false;
826
Eli Friedman6fc94dd2011-05-18 23:13:10 +0000827 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +0000828 EVT SrcVT = TLI.getValueType(RV->getType());
829 EVT DstVT = VA.getValVT();
830 // Special handling for extended integers.
831 if (SrcVT != DstVT) {
832 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
833 return false;
834
835 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
836 return false;
837
838 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
839
840 if (SrcVT == MVT::i1) {
841 if (Outs[0].Flags.isSExt())
842 return false;
843 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
844 SrcVT = MVT::i8;
845 }
846 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
847 ISD::SIGN_EXTEND;
848 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
849 SrcReg, /*TODO: Kill=*/false);
850 }
851
852 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000853 unsigned DstReg = VA.getLocReg();
854 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000855 // Avoid a cross-class copy. This is very unlikely.
856 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000857 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000858 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000859 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000860
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000861 // Add register to return instruction.
862 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000863 }
864
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000865 // The x86-64 ABI for returning structs by value requires that we copy
866 // the sret argument into %rax for the return. We saved the argument into
867 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000868 // and into %rax. We also do the same with %eax for Win32.
869 if (F.hasStructRetAttr() &&
Yaron Keren136fe7d2014-04-01 18:15:34 +0000870 (Subtarget->is64Bit() || Subtarget->isTargetKnownWindowsMSVC())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000871 unsigned Reg = X86MFInfo->getSRetReturnReg();
872 assert(Reg &&
873 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000874 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000875 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000876 RetReg).addReg(Reg);
877 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000878 }
879
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000880 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000881 MachineInstrBuilder MIB =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000882 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000883 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
884 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000885 return true;
886}
887
Evan Chenga41ee292008-09-03 06:44:39 +0000888/// X86SelectLoad - Select and emit code to implement load instructions.
889///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000890bool X86FastISel::X86SelectLoad(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000891 // Atomic loads need special handling.
892 if (cast<LoadInst>(I)->isAtomic())
893 return false;
894
Duncan Sandsf5dda012010-11-03 11:35:31 +0000895 MVT VT;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000896 if (!isTypeLegal(I->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +0000897 return false;
898
Dan Gohman39d82f92008-09-10 20:11:02 +0000899 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +0000900 if (!X86SelectAddress(I->getOperand(0), AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000901 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000902
Evan Chengf5bc7e52008-09-05 21:00:03 +0000903 unsigned ResultReg = 0;
Dan Gohman39d82f92008-09-10 20:11:02 +0000904 if (X86FastEmitLoad(VT, AM, ResultReg)) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000905 UpdateValueMap(I, ResultReg);
906 return true;
Evan Chenga41ee292008-09-03 06:44:39 +0000907 }
Evan Chengf5bc7e52008-09-05 21:00:03 +0000908 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000909}
910
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000911static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000912 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +0000913 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
914 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000915
Owen Anderson9f944592009-08-11 20:47:22 +0000916 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +0000917 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000918 case MVT::i8: return X86::CMP8rr;
919 case MVT::i16: return X86::CMP16rr;
920 case MVT::i32: return X86::CMP32rr;
921 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000922 case MVT::f32:
923 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
924 case MVT::f64:
925 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +0000926 }
Dan Gohman1ab1d312008-10-02 22:15:21 +0000927}
928
Chris Lattner88f47542008-10-15 04:13:29 +0000929/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
930/// of the comparison, return an opcode that works for the compare (e.g.
931/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000932static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +0000933 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +0000934 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +0000935 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000936 case MVT::i8: return X86::CMP8ri;
937 case MVT::i16: return X86::CMP16ri;
938 case MVT::i32: return X86::CMP32ri;
939 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +0000940 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
941 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +0000942 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +0000943 return X86::CMP64ri32;
944 return 0;
945 }
Chris Lattner88f47542008-10-15 04:13:29 +0000946}
947
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000948bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
949 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +0000950 unsigned Op0Reg = getRegForValue(Op0);
951 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000952
Chris Lattnere388725a2008-10-15 05:18:04 +0000953 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000954 if (isa<ConstantPointerNull>(Op1))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000955 Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000956
Chris Lattnerd46b9512008-10-15 04:26:38 +0000957 // We have two options: compare with register or immediate. If the RHS of
958 // the compare is an immediate that we can fold into this compare, use
959 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000960 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +0000961 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000962 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareImmOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000963 .addReg(Op0Reg)
964 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +0000965 return true;
966 }
967 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000968
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000969 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +0000970 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000971
Chris Lattnerd46b9512008-10-15 04:26:38 +0000972 unsigned Op1Reg = getRegForValue(Op1);
973 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000974 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000975 .addReg(Op0Reg)
976 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +0000977
Chris Lattnerd46b9512008-10-15 04:26:38 +0000978 return true;
979}
980
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000981bool X86FastISel::X86SelectCmp(const Instruction *I) {
982 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000983
Duncan Sandsf5dda012010-11-03 11:35:31 +0000984 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +0000985 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +0000986 return false;
987
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000988 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattnera3596db2008-10-15 03:47:17 +0000989 unsigned SetCCOpc;
Chris Lattnerf32ce222008-10-15 03:52:54 +0000990 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000991 switch (CI->getPredicate()) {
992 case CmpInst::FCMP_OEQ: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +0000993 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
994 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000995
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000996 unsigned EReg = createResultReg(&X86::GR8RegClass);
997 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000998 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SETEr), EReg);
999 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001000 TII.get(X86::SETNPr), NPReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001001 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dale Johannesen9bba9022009-02-13 02:33:27 +00001002 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattnera3596db2008-10-15 03:47:17 +00001003 UpdateValueMap(I, ResultReg);
1004 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001005 }
1006 case CmpInst::FCMP_UNE: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001007 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
1008 return false;
1009
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001010 unsigned NEReg = createResultReg(&X86::GR8RegClass);
1011 unsigned PReg = createResultReg(&X86::GR8RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001012 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SETNEr), NEReg);
1013 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SETPr), PReg);
1014 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::OR8rr),ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001015 .addReg(PReg).addReg(NEReg);
Chris Lattnera3596db2008-10-15 03:47:17 +00001016 UpdateValueMap(I, ResultReg);
1017 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001018 }
Chris Lattnerf32ce222008-10-15 03:52:54 +00001019 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1020 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1021 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
1022 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
1023 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1024 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
1025 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
1026 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1027 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
1028 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
1029 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1030 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peck527da1b2010-11-23 03:31:01 +00001031
Chris Lattnerf32ce222008-10-15 03:52:54 +00001032 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1033 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1034 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1035 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1036 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1037 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
1038 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
1039 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
1040 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
1041 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001042 default:
1043 return false;
1044 }
1045
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001046 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001047 if (SwapArgs)
Chris Lattnerd46b9512008-10-15 04:26:38 +00001048 std::swap(Op0, Op1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001049
Chris Lattnerd46b9512008-10-15 04:26:38 +00001050 // Emit a compare of Op0/Op1.
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001051 if (!X86FastEmitCompare(Op0, Op1, VT))
1052 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001053
Rafael Espindolaea09c592014-02-18 22:05:46 +00001054 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SetCCOpc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001055 UpdateValueMap(I, ResultReg);
1056 return true;
1057}
Evan Chenga41ee292008-09-03 06:44:39 +00001058
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001059bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001060 EVT DstVT = TLI.getValueType(I->getType());
1061 if (!TLI.isTypeLegal(DstVT))
1062 return false;
1063
1064 unsigned ResultReg = getRegForValue(I->getOperand(0));
1065 if (ResultReg == 0)
1066 return false;
1067
Tim Northover04eb4232013-05-30 10:43:18 +00001068 // Handle zero-extension from i1 to i8, which is common.
Craig Topper56710102013-08-15 02:33:50 +00001069 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northover04eb4232013-05-30 10:43:18 +00001070 if (SrcVT.SimpleTy == MVT::i1) {
1071 // Set the high bits to zero.
1072 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1073 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001074
Tim Northover04eb4232013-05-30 10:43:18 +00001075 if (ResultReg == 0)
1076 return false;
1077 }
1078
1079 if (DstVT == MVT::i64) {
1080 // Handle extension to 64-bits via sub-register shenanigans.
1081 unsigned MovInst;
1082
1083 switch (SrcVT.SimpleTy) {
1084 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1085 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1086 case MVT::i32: MovInst = X86::MOV32rr; break;
1087 default: llvm_unreachable("Unexpected zext to i64 source type");
1088 }
1089
1090 unsigned Result32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001091 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32)
Tim Northover04eb4232013-05-30 10:43:18 +00001092 .addReg(ResultReg);
1093
1094 ResultReg = createResultReg(&X86::GR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001095 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG),
Tim Northover04eb4232013-05-30 10:43:18 +00001096 ResultReg)
1097 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1098 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001099 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1100 ResultReg, /*Kill=*/true);
1101 if (ResultReg == 0)
1102 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001103 }
1104
Eli Friedmanc7035512011-05-25 23:49:02 +00001105 UpdateValueMap(I, ResultReg);
1106 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001107}
1108
Chris Lattnerd46b9512008-10-15 04:26:38 +00001109
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001110bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001111 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001112 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001113 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001114 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1115 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001116
Dan Gohman42ef6692010-08-21 02:32:36 +00001117 // Fold the common case of a conditional branch with a comparison
1118 // in the same block (values defined on other blocks may not have
1119 // initialized registers).
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001120 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001121 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001122 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001123
Dan Gohman1ab1d312008-10-02 22:15:21 +00001124 // Try to take advantage of fallthrough opportunities.
1125 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001126 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001127 std::swap(TrueMBB, FalseMBB);
1128 Predicate = CmpInst::getInversePredicate(Predicate);
1129 }
1130
Chris Lattner0ce717a2008-10-15 03:58:05 +00001131 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1132 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1133
Dan Gohman1ab1d312008-10-02 22:15:21 +00001134 switch (Predicate) {
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001135 case CmpInst::FCMP_OEQ:
1136 std::swap(TrueMBB, FalseMBB);
1137 Predicate = CmpInst::FCMP_UNE;
1138 // FALL THROUGH
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001139 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1140 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1141 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1142 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1143 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1144 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1145 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1146 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1147 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1148 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1149 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1150 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1151 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peck527da1b2010-11-23 03:31:01 +00001152
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001153 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1154 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1155 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1156 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1157 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1158 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1159 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1160 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1161 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1162 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001163 default:
1164 return false;
1165 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001166
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001167 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner47bef252008-10-15 04:02:26 +00001168 if (SwapArgs)
1169 std::swap(Op0, Op1);
1170
Chris Lattnerd46b9512008-10-15 04:26:38 +00001171 // Emit a compare of the LHS and RHS, setting the flags.
1172 if (!X86FastEmitCompare(Op0, Op1, VT))
1173 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001174
Rafael Espindolaea09c592014-02-18 22:05:46 +00001175 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001176 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001177
1178 if (Predicate == CmpInst::FCMP_UNE) {
1179 // X86 requires a second branch to handle UNE (and OEQ,
1180 // which is mapped to UNE above).
Rafael Espindolaea09c592014-02-18 22:05:46 +00001181 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JP_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001182 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001183 }
1184
Rafael Espindolaea09c592014-02-18 22:05:46 +00001185 FastEmitBranch(FalseMBB, DbgLoc);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001186 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohman1ab1d312008-10-02 22:15:21 +00001187 return true;
1188 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001189 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1190 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1191 // typically happen for _Bool and C++ bools.
1192 MVT SourceVT;
1193 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1194 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1195 unsigned TestOpc = 0;
1196 switch (SourceVT.SimpleTy) {
1197 default: break;
1198 case MVT::i8: TestOpc = X86::TEST8ri; break;
1199 case MVT::i16: TestOpc = X86::TEST16ri; break;
1200 case MVT::i32: TestOpc = X86::TEST32ri; break;
1201 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1202 }
1203 if (TestOpc) {
1204 unsigned OpReg = getRegForValue(TI->getOperand(0));
1205 if (OpReg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001206 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001207 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001208
Chris Lattnerc59290a2011-04-19 04:26:32 +00001209 unsigned JmpOpc = X86::JNE_4;
1210 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1211 std::swap(TrueMBB, FalseMBB);
1212 JmpOpc = X86::JE_4;
1213 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001214
Rafael Espindolaea09c592014-02-18 22:05:46 +00001215 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001216 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001217 FastEmitBranch(FalseMBB, DbgLoc);
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001218 FuncInfo.MBB->addSuccessor(TrueMBB);
1219 return true;
1220 }
1221 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001222 }
1223
1224 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001225 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1226 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001227 unsigned OpReg = getRegForValue(BI->getCondition());
1228 if (OpReg == 0) return false;
1229
Rafael Espindolaea09c592014-02-18 22:05:46 +00001230 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Eli Friedman0eea0292011-04-27 01:34:27 +00001231 .addReg(OpReg).addImm(1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001232 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JNE_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001233 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001234 FastEmitBranch(FalseMBB, DbgLoc);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001235 FuncInfo.MBB->addSuccessor(TrueMBB);
Dan Gohmana5753b32008-09-05 01:06:14 +00001236 return true;
1237}
1238
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001239bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001240 unsigned CReg = 0, OpReg = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001241 const TargetRegisterClass *RC = nullptr;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001242 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001243 CReg = X86::CL;
1244 RC = &X86::GR8RegClass;
1245 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001246 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1247 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1248 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001249 default: return false;
1250 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001251 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001252 CReg = X86::CX;
1253 RC = &X86::GR16RegClass;
1254 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001255 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1256 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1257 case Instruction::Shl: OpReg = X86::SHL16rCL; 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(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001261 CReg = X86::ECX;
1262 RC = &X86::GR32RegClass;
1263 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001264 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1265 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1266 case Instruction::Shl: OpReg = X86::SHL32rCL; 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(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001270 CReg = X86::RCX;
1271 RC = &X86::GR64RegClass;
1272 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001273 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1274 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1275 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001276 default: return false;
1277 }
1278 } else {
1279 return false;
1280 }
1281
Duncan Sandsf5dda012010-11-03 11:35:31 +00001282 MVT VT;
1283 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001284 return false;
1285
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001286 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1287 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001288
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001289 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1290 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001291 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001292 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001293
1294 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001295 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001296 if (CReg != X86::CL)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001297 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001298 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001299 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001300
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001301 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001302 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001303 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001304 UpdateValueMap(I, ResultReg);
1305 return true;
1306}
1307
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001308bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1309 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1310 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1311 const static bool S = true; // IsSigned
1312 const static bool U = false; // !IsSigned
1313 const static unsigned Copy = TargetOpcode::COPY;
1314 // For the X86 DIV/IDIV instruction, in most cases the dividend
1315 // (numerator) must be in a specific register pair highreg:lowreg,
1316 // producing the quotient in lowreg and the remainder in highreg.
1317 // For most data types, to set up the instruction, the dividend is
1318 // copied into lowreg, and lowreg is sign-extended or zero-extended
1319 // into highreg. The exception is i8, where the dividend is defined
1320 // as a single register rather than a register pair, and we
1321 // therefore directly sign-extend or zero-extend the dividend into
1322 // lowreg, instead of copying, and ignore the highreg.
1323 const static struct DivRemEntry {
1324 // The following portion depends only on the data type.
1325 const TargetRegisterClass *RC;
1326 unsigned LowInReg; // low part of the register pair
1327 unsigned HighInReg; // high part of the register pair
1328 // The following portion depends on both the data type and the operation.
1329 struct DivRemResult {
1330 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1331 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1332 // highreg, or copying a zero into highreg.
1333 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1334 // zero/sign-extending into lowreg for i8.
1335 unsigned DivRemResultReg; // Register containing the desired result.
1336 bool IsOpSigned; // Whether to use signed or unsigned form.
1337 } ResultTable[NumOps];
1338 } OpTable[NumTypes] = {
1339 { &X86::GR8RegClass, X86::AX, 0, {
1340 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1341 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1342 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1343 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1344 }
1345 }, // i8
1346 { &X86::GR16RegClass, X86::AX, X86::DX, {
1347 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1348 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001349 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1350 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001351 }
1352 }, // i16
1353 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1354 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1355 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1356 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1357 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1358 }
1359 }, // i32
1360 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1361 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1362 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001363 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1364 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001365 }
1366 }, // i64
1367 };
1368
1369 MVT VT;
1370 if (!isTypeLegal(I->getType(), VT))
1371 return false;
1372
1373 unsigned TypeIndex, OpIndex;
1374 switch (VT.SimpleTy) {
1375 default: return false;
1376 case MVT::i8: TypeIndex = 0; break;
1377 case MVT::i16: TypeIndex = 1; break;
1378 case MVT::i32: TypeIndex = 2; break;
1379 case MVT::i64: TypeIndex = 3;
1380 if (!Subtarget->is64Bit())
1381 return false;
1382 break;
1383 }
1384
1385 switch (I->getOpcode()) {
1386 default: llvm_unreachable("Unexpected div/rem opcode");
1387 case Instruction::SDiv: OpIndex = 0; break;
1388 case Instruction::SRem: OpIndex = 1; break;
1389 case Instruction::UDiv: OpIndex = 2; break;
1390 case Instruction::URem: OpIndex = 3; break;
1391 }
1392
1393 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1394 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1395 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1396 if (Op0Reg == 0)
1397 return false;
1398 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1399 if (Op1Reg == 0)
1400 return false;
1401
1402 // Move op0 into low-order input register.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001403 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001404 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1405 // Zero-extend or sign-extend into high-order input register.
1406 if (OpEntry.OpSignExtend) {
1407 if (OpEntry.IsOpSigned)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001408 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001409 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001410 else {
1411 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001412 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001413 TII.get(X86::MOV32r0), Zero32);
1414
1415 // Copy the zero into the appropriate sub/super/identical physical
1416 // register. Unfortunately the operations needed are not uniform enough to
1417 // fit neatly into the table above.
1418 if (VT.SimpleTy == MVT::i16) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001419 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001420 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001421 .addReg(Zero32, 0, X86::sub_16bit);
1422 } else if (VT.SimpleTy == MVT::i32) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001423 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001424 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001425 .addReg(Zero32);
1426 } else if (VT.SimpleTy == MVT::i64) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001427 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001428 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1429 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1430 }
1431 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001432 }
1433 // Generate the DIV/IDIV instruction.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001434 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001435 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001436 // For i8 remainder, we can't reference AH directly, as we'll end
1437 // up with bogus copies like %R9B = COPY %AH. Reference AX
1438 // instead to prevent AH references in a REX instruction.
1439 //
1440 // The current assumption of the fast register allocator is that isel
1441 // won't generate explicit references to the GPR8_NOREX registers. If
1442 // the allocator and/or the backend get enhanced to be more robust in
1443 // that regard, this can be, and should be, removed.
1444 unsigned ResultReg = 0;
1445 if ((I->getOpcode() == Instruction::SRem ||
1446 I->getOpcode() == Instruction::URem) &&
1447 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1448 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1449 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001450 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbachc35388f2013-07-09 02:07:25 +00001451 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1452
1453 // Shift AX right by 8 bits instead of using AH.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001454 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri),
Jim Grosbachc35388f2013-07-09 02:07:25 +00001455 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1456
1457 // Now reference the 8-bit subreg of the result.
1458 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1459 /*Kill=*/true, X86::sub_8bit);
1460 }
1461 // Copy the result out of the physreg if we haven't already.
1462 if (!ResultReg) {
1463 ResultReg = createResultReg(TypeEntry.RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001464 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg)
Jim Grosbachc35388f2013-07-09 02:07:25 +00001465 .addReg(OpEntry.DivRemResultReg);
1466 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001467 UpdateValueMap(I, ResultReg);
1468
1469 return true;
1470}
1471
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001472bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001473 MVT VT;
1474 if (!isTypeLegal(I->getType(), VT))
Chris Lattnera0f9d492008-10-15 05:07:36 +00001475 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001476
Eric Christopher0574cc52010-09-29 23:00:29 +00001477 // We only use cmov here, if we don't have a cmov instruction bail.
1478 if (!Subtarget->hasCMov()) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001479
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001480 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001481 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001482 if (VT == MVT::i16) {
Dan Gohmane5560182008-09-05 21:13:04 +00001483 Opc = X86::CMOVE16rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001484 RC = &X86::GR16RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001485 } else if (VT == MVT::i32) {
Dan Gohmane5560182008-09-05 21:13:04 +00001486 Opc = X86::CMOVE32rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001487 RC = &X86::GR32RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001488 } else if (VT == MVT::i64) {
Dan Gohmane5560182008-09-05 21:13:04 +00001489 Opc = X86::CMOVE64rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001490 RC = &X86::GR64RegClass;
1491 } else {
Wesley Peck527da1b2010-11-23 03:31:01 +00001492 return false;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001493 }
1494
1495 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1496 if (Op0Reg == 0) return false;
1497 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1498 if (Op1Reg == 0) return false;
1499 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1500 if (Op2Reg == 0) return false;
1501
Quentin Colombet90a646e2013-12-19 18:32:04 +00001502 // Selects operate on i1, however, Op0Reg is 8 bits width and may contain
1503 // garbage. Indeed, only the less significant bit is supposed to be accurate.
1504 // If we read more than the lsb, we may see non-zero values whereas lsb
1505 // is zero. Therefore, we have to truncate Op0Reg to i1 for the select.
Alp Tokercb402912014-01-24 17:20:08 +00001506 // This is achieved by performing TEST against 1.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001507 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Quentin Colombet90a646e2013-12-19 18:32:04 +00001508 .addReg(Op0Reg).addImm(1);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001509 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001510 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001511 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001512 UpdateValueMap(I, ResultReg);
1513 return true;
1514}
1515
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001516bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001517 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001518 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00001519 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001520 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001521 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001522 unsigned OpReg = getRegForValue(V);
1523 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001524 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001525 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001526 TII.get(X86::CVTSS2SDrr), ResultReg)
1527 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00001528 UpdateValueMap(I, ResultReg);
1529 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00001530 }
1531 }
1532
1533 return false;
1534}
1535
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001536bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001537 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00001538 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001539 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001540 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00001541 unsigned OpReg = getRegForValue(V);
1542 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001543 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001544 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001545 TII.get(X86::CVTSD2SSrr), ResultReg)
1546 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00001547 UpdateValueMap(I, ResultReg);
1548 return true;
1549 }
1550 }
1551 }
1552
1553 return false;
1554}
1555
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001556bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001557 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1558 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00001559
Eli Friedmanc7035512011-05-25 23:49:02 +00001560 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00001561 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00001562 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00001563 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00001564 return false;
1565
1566 unsigned InputReg = getRegForValue(I->getOperand(0));
1567 if (!InputReg)
1568 // Unhandled operand. Halt "fast" selection and bail.
1569 return false;
1570
Eli Friedmanc7035512011-05-25 23:49:02 +00001571 if (SrcVT == MVT::i8) {
1572 // Truncate from i8 to i1; no code needed.
1573 UpdateValueMap(I, InputReg);
1574 return true;
1575 }
Evan Chengb9286692008-09-07 08:47:42 +00001576
Eli Friedmanc7035512011-05-25 23:49:02 +00001577 if (!Subtarget->is64Bit()) {
1578 // If we're on x86-32; we can't extract an i8 from a general register.
1579 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00001580 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1581 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1582 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00001583 unsigned CopyReg = createResultReg(CopyRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001584 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Eli Friedmanc7035512011-05-25 23:49:02 +00001585 CopyReg).addReg(InputReg);
1586 InputReg = CopyReg;
1587 }
1588
1589 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00001590 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00001591 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00001592 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00001593 if (!ResultReg)
1594 return false;
1595
1596 UpdateValueMap(I, ResultReg);
1597 return true;
1598}
1599
Eli Friedman60afcc22011-05-20 22:21:04 +00001600bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1601 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1602}
1603
Eli Friedmanbcc69142011-04-27 01:45:07 +00001604bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1605 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00001606
Eli Friedmanbcc69142011-04-27 01:45:07 +00001607 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00001608 if (!IsMemcpySmall(Len))
1609 return false;
1610
1611 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00001612
1613 // We don't care about alignment here since we just emit integer accesses.
1614 while (Len) {
1615 MVT VT;
1616 if (Len >= 8 && i64Legal)
1617 VT = MVT::i64;
1618 else if (Len >= 4)
1619 VT = MVT::i32;
1620 else if (Len >= 2)
1621 VT = MVT::i16;
1622 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00001623 VT = MVT::i8;
1624 }
1625
1626 unsigned Reg;
1627 bool RV = X86FastEmitLoad(VT, SrcAM, Reg);
1628 RV &= X86FastEmitStore(VT, Reg, DestAM);
1629 assert(RV && "Failed to emit load or store??");
1630
1631 unsigned Size = VT.getSizeInBits()/8;
1632 Len -= Size;
1633 DestAM.Disp += Size;
1634 SrcAM.Disp += Size;
1635 }
1636
1637 return true;
1638}
1639
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001640static bool isCommutativeIntrinsic(IntrinsicInst const &I) {
1641 switch (I.getIntrinsicID()) {
1642 case Intrinsic::sadd_with_overflow:
1643 case Intrinsic::uadd_with_overflow:
1644 case Intrinsic::smul_with_overflow:
1645 case Intrinsic::umul_with_overflow:
1646 return true;
1647 default:
1648 return false;
1649 }
1650}
1651
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001652bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001653 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00001654 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001655 default: return false;
Juergen Ributzka4dc95872014-06-11 21:44:44 +00001656 case Intrinsic::frameaddress: {
1657 Type *RetTy = I.getCalledFunction()->getReturnType();
1658
1659 MVT VT;
1660 if (!isTypeLegal(RetTy, VT))
1661 return false;
1662
1663 unsigned Opc;
1664 const TargetRegisterClass *RC = nullptr;
1665
1666 switch (VT.SimpleTy) {
1667 default: llvm_unreachable("Invalid result type for frameaddress.");
1668 case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break;
1669 case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break;
1670 }
1671
1672 // This needs to be set before we call getFrameRegister, otherwise we get
1673 // the wrong frame register.
1674 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
1675 MFI->setFrameAddressIsTaken(true);
1676
1677 const X86RegisterInfo *RegInfo =
1678 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
1679 unsigned FrameReg = RegInfo->getFrameRegister(*(FuncInfo.MF));
1680 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
1681 (FrameReg == X86::EBP && VT == MVT::i32)) &&
1682 "Invalid Frame Register!");
1683
1684 // Always make a copy of the frame register to to a vreg first, so that we
1685 // never directly reference the frame register (the TwoAddressInstruction-
1686 // Pass doesn't like that).
1687 unsigned SrcReg = createResultReg(RC);
1688 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1689 TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg);
1690
1691 // Now recursively load from the frame address.
1692 // movq (%rbp), %rax
1693 // movq (%rax), %rax
1694 // movq (%rax), %rax
1695 // ...
1696 unsigned DestReg;
1697 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
1698 while (Depth--) {
1699 DestReg = createResultReg(RC);
1700 addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1701 TII.get(Opc), DestReg), SrcReg);
1702 SrcReg = DestReg;
1703 }
1704
1705 UpdateValueMap(&I, SrcReg);
1706 return true;
1707 }
Chris Lattner91328b32011-04-19 05:52:03 +00001708 case Intrinsic::memcpy: {
1709 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1710 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00001711 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00001712 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001713
Eli Friedmancd2124a2011-06-10 23:39:36 +00001714 if (isa<ConstantInt>(MCI.getLength())) {
1715 // Small memcpy's are common enough that we want to do them
1716 // without a call if possible.
1717 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1718 if (IsMemcpySmall(Len)) {
1719 X86AddressMode DestAM, SrcAM;
1720 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1721 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1722 return false;
1723 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1724 return true;
1725 }
1726 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001727
Eli Friedmancd2124a2011-06-10 23:39:36 +00001728 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1729 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00001730 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001731
Eli Friedmancd2124a2011-06-10 23:39:36 +00001732 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1733 return false;
1734
1735 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00001736 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00001737 case Intrinsic::memset: {
1738 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001739
Nick Lewyckya530a4d2011-08-02 00:40:16 +00001740 if (MSI.isVolatile())
1741 return false;
1742
Eli Friedmancd2124a2011-06-10 23:39:36 +00001743 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1744 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1745 return false;
1746
1747 if (MSI.getDestAddressSpace() > 255)
1748 return false;
1749
1750 return DoSelectCall(&I, "memset");
1751 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001752 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00001753 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001754 EVT PtrTy = TLI.getPointerTy();
1755
Gabor Greif83205af2010-06-26 11:51:52 +00001756 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1757 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001758
Josh Magee22b8ba22013-12-19 03:17:11 +00001759 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
1760
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001761 // Grab the frame index.
1762 X86AddressMode AM;
1763 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00001764 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001765 return true;
1766 }
Dale Johannesend5575f22010-01-26 00:09:58 +00001767 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001768 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00001769 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00001770 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00001771 if (!X86SelectAddress(DI->getAddress(), AM))
1772 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001773 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00001774 // FIXME may need to add RegState::Debug to any registers produced,
1775 // although ESP/EBP should be the only ones at the moment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001776 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001777 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00001778 return true;
1779 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001780 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001781 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001782 return true;
1783 }
Bill Wendling80b34b32008-12-09 02:42:50 +00001784 case Intrinsic::sadd_with_overflow:
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001785 case Intrinsic::uadd_with_overflow:
1786 case Intrinsic::ssub_with_overflow:
1787 case Intrinsic::usub_with_overflow:
1788 case Intrinsic::smul_with_overflow:
1789 case Intrinsic::umul_with_overflow: {
1790 // This implements the basic lowering of the xalu with overflow intrinsics
1791 // into add/sub/mul folowed by either seto or setb.
Bill Wendling80b34b32008-12-09 02:42:50 +00001792 const Function *Callee = I.getCalledFunction();
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001793 auto *Ty = cast<StructType>(Callee->getReturnType());
1794 Type *RetTy = Ty->getTypeAtIndex(0U);
1795 Type *CondTy = Ty->getTypeAtIndex(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00001796
Duncan Sandsf5dda012010-11-03 11:35:31 +00001797 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00001798 if (!isTypeLegal(RetTy, VT))
1799 return false;
1800
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001801 if (VT < MVT::i8 || VT > MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00001802 return false;
1803
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001804 const Value *LHS = I.getArgOperand(0);
1805 const Value *RHS = I.getArgOperand(1);
1806
1807 // Canonicalize immediates to the RHS.
1808 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
1809 isCommutativeIntrinsic(I))
1810 std::swap(LHS, RHS);
1811
1812 unsigned BaseOpc, CondOpc;
1813 switch (I.getIntrinsicID()) {
1814 default: llvm_unreachable("Unexpected intrinsic!");
1815 case Intrinsic::sadd_with_overflow:
1816 BaseOpc = ISD::ADD; CondOpc = X86::SETOr; break;
1817 case Intrinsic::uadd_with_overflow:
1818 BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break;
1819 case Intrinsic::ssub_with_overflow:
1820 BaseOpc = ISD::SUB; CondOpc = X86::SETOr; break;
1821 case Intrinsic::usub_with_overflow:
1822 BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break;
1823 case Intrinsic::smul_with_overflow:
1824 BaseOpc = ISD::MUL; CondOpc = X86::SETOr; break;
1825 case Intrinsic::umul_with_overflow:
1826 BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break;
1827 }
1828
1829 unsigned LHSReg = getRegForValue(LHS);
1830 if (LHSReg == 0)
1831 return false;
1832 bool LHSIsKill = hasTrivialKill(LHS);
1833
1834 unsigned ResultReg = 0;
1835 // Check if we have an immediate version.
1836 if (auto const *C = dyn_cast<ConstantInt>(RHS)) {
1837 ResultReg = FastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
1838 C->getZExtValue());
1839 }
1840
1841 unsigned RHSReg;
1842 bool RHSIsKill;
1843 if (!ResultReg) {
1844 RHSReg = getRegForValue(RHS);
1845 if (RHSReg == 0)
1846 return false;
1847 RHSIsKill = hasTrivialKill(RHS);
1848 ResultReg = FastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
1849 RHSIsKill);
1850 }
1851
1852 // FastISel doesn't have a pattern for X86::MUL*r. Emit it manually.
1853 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
1854 static const unsigned MULOpc[] =
1855 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
1856 static const unsigned Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
1857 // First copy the first operand into RAX, which is an implicit input to
1858 // the X86::MUL*r instruction.
1859 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1860 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
1861 .addReg(LHSReg, getKillRegState(LHSIsKill));
1862 ResultReg = FastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
1863 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
1864 }
1865
1866 if (!ResultReg)
Bill Wendling80b34b32008-12-09 02:42:50 +00001867 return false;
1868
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001869 unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy);
1870 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
1871 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc),
1872 ResultReg2);
Eli Friedmana4d4a012011-05-16 21:06:17 +00001873
1874 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00001875 return true;
1876 }
1877 }
1878}
1879
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001880bool X86FastISel::FastLowerArguments() {
1881 if (!FuncInfo.CanLowerReturn)
1882 return false;
1883
1884 const Function *F = FuncInfo.Fn;
1885 if (F->isVarArg())
1886 return false;
1887
1888 CallingConv::ID CC = F->getCallingConv();
1889 if (CC != CallingConv::C)
1890 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00001891
1892 if (Subtarget->isCallingConvWin64(CC))
1893 return false;
1894
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001895 if (!Subtarget->is64Bit())
1896 return false;
1897
1898 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
Juergen Ributzkafbaa3db2014-06-11 23:10:58 +00001899 unsigned GPRCnt = 0;
1900 unsigned FPRCnt = 0;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001901 unsigned Idx = 1;
Juergen Ributzkafbaa3db2014-06-11 23:10:58 +00001902 for (auto const &Arg : F->args()) {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001903 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1904 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1905 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1906 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1907 return false;
1908
Juergen Ributzkafbaa3db2014-06-11 23:10:58 +00001909 Type *ArgTy = Arg.getType();
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001910 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1911 return false;
1912
1913 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00001914 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001915 switch (ArgVT.getSimpleVT().SimpleTy) {
Juergen Ributzkafbaa3db2014-06-11 23:10:58 +00001916 default: return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001917 case MVT::i32:
1918 case MVT::i64:
Juergen Ributzkafbaa3db2014-06-11 23:10:58 +00001919 ++GPRCnt;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001920 break;
Juergen Ributzkafbaa3db2014-06-11 23:10:58 +00001921 case MVT::f32:
1922 case MVT::f64:
1923 ++FPRCnt;
1924 break;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001925 }
Juergen Ributzkafbaa3db2014-06-11 23:10:58 +00001926
1927 if (GPRCnt > 6)
1928 return false;
1929
1930 if (FPRCnt > 8)
1931 return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001932 }
1933
Craig Topper840beec2014-04-04 05:16:06 +00001934 static const MCPhysReg GPR32ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001935 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1936 };
Craig Topper840beec2014-04-04 05:16:06 +00001937 static const MCPhysReg GPR64ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001938 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
1939 };
Juergen Ributzkafbaa3db2014-06-11 23:10:58 +00001940 static const MCPhysReg XMMArgRegs[] = {
1941 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1942 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1943 };
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001944
Juergen Ributzkafbaa3db2014-06-11 23:10:58 +00001945 unsigned GPRIdx = 0;
1946 unsigned FPRIdx = 0;
1947 for (auto const &Arg : F->args()) {
1948 MVT VT = TLI.getSimpleValueType(Arg.getType());
1949 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1950 unsigned SrcReg;
1951 switch (VT.SimpleTy) {
1952 default: llvm_unreachable("Unexpected value type.");
1953 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
1954 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
1955 case MVT::f32: // fall-through
1956 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
1957 }
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001958 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1959 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1960 // Without this, EmitLiveInCopies may eliminate the livein if its only
1961 // use is a bitcast (which isn't turned into an instruction).
1962 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001963 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzkafbaa3db2014-06-11 23:10:58 +00001964 TII.get(TargetOpcode::COPY), ResultReg)
1965 .addReg(DstReg, getKillRegState(true));
1966 UpdateValueMap(&Arg, ResultReg);
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001967 }
1968 return true;
1969}
1970
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001971bool X86FastISel::X86SelectCall(const Instruction *I) {
1972 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00001973 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001974
1975 // Can't handle inline asm yet.
1976 if (isa<InlineAsm>(Callee))
1977 return false;
1978
Bill Wendling80b34b32008-12-09 02:42:50 +00001979 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001980 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00001981 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00001982
Chad Rosierdf42cf32012-12-11 00:18:02 +00001983 // Allow SelectionDAG isel to handle tail calls.
1984 if (cast<CallInst>(I)->isTailCall())
1985 return false;
1986
Craig Topper062a2ba2014-04-25 05:30:21 +00001987 return DoSelectCall(I, nullptr);
Eli Friedmancd2124a2011-06-10 23:39:36 +00001988}
1989
Rafael Espindola73173c52012-07-25 15:42:45 +00001990static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
1991 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001992 if (Subtarget.is64Bit())
1993 return 0;
Rafael Espindola32cb5ac2013-12-12 16:06:58 +00001994 if (Subtarget.getTargetTriple().isOSMSVCRT())
Rafael Espindola2caee7f2012-07-25 13:35:45 +00001995 return 0;
1996 CallingConv::ID CC = CS.getCallingConv();
1997 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
1998 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00001999 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002000 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002001 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00002002 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002003 return 4;
2004}
2005
Eli Friedmancd2124a2011-06-10 23:39:36 +00002006// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
2007bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
2008 const CallInst *CI = cast<CallInst>(I);
2009 const Value *Callee = CI->getCalledValue();
2010
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002011 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002012 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002013 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00002014 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002015 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00002016 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
2017 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002018 return false;
2019
Evan Chengd10089a2010-01-27 00:00:57 +00002020 // fastcc with -tailcallopt is intended to provide a guaranteed
2021 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002022 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00002023 return false;
2024
Chris Lattner229907c2011-07-18 04:54:35 +00002025 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2026 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002027 bool isVarArg = FTy->isVarArg();
2028
2029 // Don't know how to handle Win64 varargs yet. Nothing special needed for
2030 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00002031 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002032 return false;
2033
Reid Klecknerf5b76512014-01-31 23:50:57 +00002034 // Don't know about inalloca yet.
2035 if (CS.hasInAllocaArgument())
2036 return false;
2037
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002038 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00002039 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002040 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002041 return false;
2042
Eli Friedman7b279422011-05-17 18:29:03 +00002043 // Check whether the function can return without sret-demotion.
2044 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002045 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00002046 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00002047 *FuncInfo.MF, FTy->isVarArg(),
2048 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002049 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00002050 return false;
2051
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002052 // Materialize callee address in a register. FIXME: GV address can be
2053 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00002054 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00002055 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00002056 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002057 unsigned CalleeOp = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002058 const GlobalValue *GV = nullptr;
2059 if (CalleeAM.GV != nullptr) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002060 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00002061 } else if (CalleeAM.Base.Reg != 0) {
2062 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002063 } else
2064 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002065
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002066 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002067 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002068 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002069 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002070 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00002071 unsigned arg_size = CS.arg_size();
2072 Args.reserve(arg_size);
2073 ArgVals.reserve(arg_size);
2074 ArgVTs.reserve(arg_size);
2075 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002076 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002077 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00002078 // If we're lowering a mem intrinsic instead of a regular call, skip the
2079 // last two arguments, which should not passed to the underlying functions.
2080 if (MemIntName && e-i <= 2)
2081 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002082 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002083 ISD::ArgFlagsTy Flags;
2084 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002085 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002086 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002087 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002088 Flags.setZExt();
2089
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002090 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00002091 PointerType *Ty = cast<PointerType>(ArgVal->getType());
2092 Type *ElementTy = Ty->getElementType();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002093 unsigned FrameSize = DL.getTypeAllocSize(ElementTy);
Eli Friedman60afcc22011-05-20 22:21:04 +00002094 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
2095 if (!FrameAlign)
2096 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
2097 Flags.setByVal();
2098 Flags.setByValSize(FrameSize);
2099 Flags.setByValAlign(FrameAlign);
2100 if (!IsMemcpySmall(FrameSize))
2101 return false;
2102 }
2103
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002104 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00002105 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002106 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00002107 Flags.setNest();
2108
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002109 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
2110 // instruction. This is safe because it is common to all fastisel supported
2111 // calling conventions on x86.
2112 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
2113 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
2114 CI->getBitWidth() == 16) {
2115 if (Flags.isSExt())
2116 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
2117 else
2118 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
2119 }
2120 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002121
Chris Lattner5f4b7832011-04-19 05:09:50 +00002122 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002123
Chris Lattner34a08c22011-04-19 05:15:59 +00002124 // Passing bools around ends up doing a trunc to i1 and passing it.
2125 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00002126 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
2127 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
2128 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00002129 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
2130 ArgReg = getRegForValue(ArgVal);
2131 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002132
Chris Lattner5f4b7832011-04-19 05:09:50 +00002133 MVT ArgVT;
2134 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002135
Chris Lattner5f4b7832011-04-19 05:09:50 +00002136 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
2137 ArgVal->hasOneUse(), 1);
2138 } else {
2139 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00002140 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002141
Chris Lattner34a08c22011-04-19 05:15:59 +00002142 if (ArgReg == 0) return false;
2143
Chris Lattner229907c2011-07-18 04:54:35 +00002144 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002145 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002146 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002147 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002148 if (ArgVT == MVT::x86mmx)
2149 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002150 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002151 Flags.setOrigAlign(OriginalAlignment);
2152
Chris Lattner5f4b7832011-04-19 05:09:50 +00002153 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002154 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002155 ArgVTs.push_back(ArgVT);
2156 ArgFlags.push_back(Flags);
2157 }
2158
2159 // Analyze operands of the call, assigning locations to each operand.
2160 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002161 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002162 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00002163
Dan Gohman47a07242010-06-01 21:09:47 +00002164 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00002165 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00002166 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00002167
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00002168 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002169
2170 // Get a count of how many bytes are to be pushed on the stack.
2171 unsigned NumBytes = CCInfo.getNextStackOffset();
2172
2173 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002174 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002175 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002176 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002177
Chris Lattner3ba29352008-10-15 05:30:52 +00002178 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002179 // copies / loads.
2180 SmallVector<unsigned, 4> RegArgs;
2181 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2182 CCValAssign &VA = ArgLocs[i];
2183 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00002184 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002185
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002186 // Promote the value if needed.
2187 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002188 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00002189 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002190 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2191 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002192 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2193 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002194 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002195 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002196 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002197 }
2198 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002199 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2200 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002201 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2202 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002203 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002204 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002205 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002206 }
2207 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002208 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2209 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002210 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2211 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002212 if (!Emitted)
2213 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002214 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002215 if (!Emitted)
2216 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2217 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002218
Chris Lattner2d7df022011-01-05 22:26:52 +00002219 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002220 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002221 break;
2222 }
Dan Gohman8c795692009-08-05 05:33:42 +00002223 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002224 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002225 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002226 assert(BC != 0 && "Failed to emit a bitcast!");
2227 Arg = BC;
2228 ArgVT = VA.getLocVT();
2229 break;
2230 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002231 case CCValAssign::VExt:
2232 // VExt has not been implemented, so this should be impossible to reach
2233 // for now. However, fallback to Selection DAG isel once implemented.
2234 return false;
2235 case CCValAssign::Indirect:
2236 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2237 // support this.
2238 return false;
Lang Hames06234ec2014-01-14 19:56:36 +00002239 case CCValAssign::FPExt:
2240 llvm_unreachable("Unexpected loc info!");
Evan Cheng6500d172008-09-08 06:35:17 +00002241 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002242
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002243 if (VA.isRegLoc()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002244 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2245 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002246 RegArgs.push_back(VA.getLocReg());
2247 } else {
2248 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002249 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002250 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2251 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002252 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002253 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002254 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002255 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002256
Eli Friedman60afcc22011-05-20 22:21:04 +00002257 if (Flags.isByVal()) {
2258 X86AddressMode SrcAM;
2259 SrcAM.Base.Reg = Arg;
2260 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2261 assert(Res && "memcpy length already checked!"); (void)Res;
2262 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2263 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002264 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002265 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002266 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2267 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002268 } else {
Lang Hames7d2f7b52011-10-18 22:11:33 +00002269 if (!X86FastEmitStore(ArgVT, Arg, AM))
2270 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002271 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002272 }
2273 }
2274
Dan Gohman3691d502008-09-25 15:24:26 +00002275 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00002276 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00002277 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00002278 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002279 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2280 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00002281 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002282
Charles Davise8f297c2013-07-12 06:02:35 +00002283 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002284 // Count the number of XMM registers allocated.
Craig Topper840beec2014-04-04 05:16:06 +00002285 static const MCPhysReg XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002286 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2287 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2288 };
2289 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002290 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002291 X86::AL).addImm(NumXMMRegs);
2292 }
2293
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002294 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002295 MachineInstrBuilder MIB;
2296 if (CalleeOp) {
2297 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00002298 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002299 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002300 CallOpc = X86::CALL64r;
2301 else
2302 CallOpc = X86::CALL32r;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002303 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002304 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00002305
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002306 } else {
2307 // Direct call.
2308 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00002309 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002310 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002311 CallOpc = X86::CALL64pcrel32;
2312 else
2313 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00002314
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002315 // See if we need any target-specific flags on the GV operand.
2316 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002317
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002318 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2319 // external symbols most go through the PLT in PIC mode. If the symbol
2320 // has hidden or protected visibility, or if it is static or local, then
2321 // we don't need to use the PLT - we can directly call it.
2322 if (Subtarget->isTargetELF() &&
2323 TM.getRelocationModel() == Reloc::PIC_ &&
2324 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2325 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00002326 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002327 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00002328 (!Subtarget->getTargetTriple().isMacOSX() ||
2329 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002330 // PC-relative references to external symbols should go through $stub,
2331 // unless we're building with the leopard linker or later, which
2332 // automatically synthesizes these stubs.
2333 OpFlags = X86II::MO_DARWIN_STUB;
2334 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002335
2336
Rafael Espindolaea09c592014-02-18 22:05:46 +00002337 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
Eli Friedmancd2124a2011-06-10 23:39:36 +00002338 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00002339 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002340 else
2341 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002342 }
Dan Gohman3691d502008-09-25 15:24:26 +00002343
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00002344 // Add a register mask with the call-preserved registers.
2345 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2346 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2347
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002348 // Add an implicit use GOT pointer in EBX.
2349 if (Subtarget->isPICStyleGOT())
2350 MIB.addReg(X86::EBX, RegState::Implicit);
2351
Charles Davise8f297c2013-07-12 06:02:35 +00002352 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002353 MIB.addReg(X86::AL, RegState::Implicit);
2354
2355 // Add implicit physical register uses to the call.
2356 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2357 MIB.addReg(RegArgs[i], RegState::Implicit);
2358
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002359 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002360 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00002361 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002362 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00002363 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002364
Eli Friedman7b279422011-05-17 18:29:03 +00002365 // Build info for return calling conv lowering code.
2366 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2367 SmallVector<ISD::InputArg, 32> Ins;
2368 SmallVector<EVT, 4> RetTys;
2369 ComputeValueVTs(TLI, I->getType(), RetTys);
2370 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2371 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002372 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00002373 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2374 for (unsigned j = 0; j != NumRegs; ++j) {
2375 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002376 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00002377 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002378 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002379 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002380 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002381 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002382 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00002383 MyFlags.Flags.setInReg();
2384 Ins.push_back(MyFlags);
2385 }
2386 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002387
Eli Friedman7b279422011-05-17 18:29:03 +00002388 // Now handle call return values.
2389 SmallVector<unsigned, 4> UsedRegs;
2390 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002391 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002392 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002393 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2394 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2395 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2396 EVT CopyVT = RVLocs[i].getValVT();
2397 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00002398
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002399 // If this is a call to a function that returns an fp value on the x87 fp
2400 // stack, but where we prefer to use the value in xmm registers, copy it
2401 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00002402 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002403 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002404 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002405 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00002406 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002407 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00002408 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2409 TII.get(X86::FpPOP_RETVAL), CopyReg);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002410 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002411 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2412 TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002413 CopyReg).addReg(RVLocs[i].getLocReg());
2414 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002415 }
2416
Eli Friedman7b279422011-05-17 18:29:03 +00002417 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002418 // Round the F80 the right size, which also moves to the appropriate xmm
2419 // register. This is accomplished by storing the F80 value in memory and
2420 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00002421 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00002422 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002423 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00002424 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002425 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002426 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00002427 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00002428 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002429 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Friedman7b279422011-05-17 18:29:03 +00002430 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002431 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002432 }
Eli Friedman83ba1502011-05-17 00:13:47 +00002433
Eli Friedman7b279422011-05-17 18:29:03 +00002434 if (RVLocs.size())
2435 UpdateValueMap(I, ResultReg, RVLocs.size());
2436
Dan Gohman86936502010-06-18 23:28:01 +00002437 // Set all unused physreg defs as dead.
2438 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2439
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002440 return true;
2441}
2442
2443
Dan Gohmand58f3e32008-08-28 23:21:34 +00002444bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002445X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00002446 switch (I->getOpcode()) {
2447 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00002448 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00002449 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00002450 case Instruction::Store:
2451 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002452 case Instruction::Ret:
2453 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00002454 case Instruction::ICmp:
2455 case Instruction::FCmp:
2456 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00002457 case Instruction::ZExt:
2458 return X86SelectZExt(I);
2459 case Instruction::Br:
2460 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002461 case Instruction::Call:
2462 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002463 case Instruction::LShr:
2464 case Instruction::AShr:
2465 case Instruction::Shl:
2466 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00002467 case Instruction::SDiv:
2468 case Instruction::UDiv:
2469 case Instruction::SRem:
2470 case Instruction::URem:
2471 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002472 case Instruction::Select:
2473 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00002474 case Instruction::Trunc:
2475 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002476 case Instruction::FPExt:
2477 return X86SelectFPExt(I);
2478 case Instruction::FPTrunc:
2479 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002480 case Instruction::IntToPtr: // Deliberate fall-through.
2481 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002482 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2483 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002484 if (DstVT.bitsGT(SrcVT))
2485 return X86SelectZExt(I);
2486 if (DstVT.bitsLT(SrcVT))
2487 return X86SelectTrunc(I);
2488 unsigned Reg = getRegForValue(I->getOperand(0));
2489 if (Reg == 0) return false;
2490 UpdateValueMap(I, Reg);
2491 return true;
2492 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002493 }
2494
2495 return false;
2496}
2497
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002498unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002499 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002500 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00002501 return 0;
2502
2503 // Can't handle alternate code models yet.
2504 if (TM.getCodeModel() != CodeModel::Small)
2505 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002506
Owen Anderson50288e32008-09-05 00:06:23 +00002507 // Get opcode and regclass of the output for the given load instruction.
2508 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002509 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002510 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00002511 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00002512 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00002513 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00002514 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002515 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002516 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00002517 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00002518 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002519 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002520 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00002521 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00002522 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002523 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002524 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00002525 // Must be in x86-64 mode.
2526 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00002527 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002528 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002529 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002530 if (X86ScalarSSEf32) {
2531 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00002532 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002533 } else {
2534 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00002535 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002536 }
2537 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002538 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002539 if (X86ScalarSSEf64) {
2540 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00002541 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002542 } else {
2543 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00002544 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002545 }
2546 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002547 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00002548 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00002549 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002550 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002551
Dan Gohman9801ba42008-09-19 22:16:54 +00002552 // Materialize addresses with LEA instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00002553 if (isa<GlobalValue>(C)) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002554 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002555 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00002556 // If the expression is just a basereg, then we're done, otherwise we need
2557 // to emit an LEA.
2558 if (AM.BaseType == X86AddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +00002559 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
Chris Lattner48326602011-04-17 17:12:08 +00002560 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002561
Chris Lattner48326602011-04-17 17:12:08 +00002562 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman9801ba42008-09-19 22:16:54 +00002563 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002564 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002565 TII.get(Opc), ResultReg), AM);
Owen Anderson50288e32008-09-05 00:06:23 +00002566 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002567 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00002568 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002569 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002570
Owen Andersond41c7162008-09-06 01:11:01 +00002571 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002572 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002573 if (Align == 0) {
2574 // Alignment of vector types. FIXME!
Rafael Espindolaea09c592014-02-18 22:05:46 +00002575 Align = DL.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002576 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002577
Dan Gohman8392f0c2008-09-30 01:21:32 +00002578 // x86-32 PIC requires a PIC base register for constant pools.
2579 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00002580 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00002581 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00002582 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002583 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002584 } else if (Subtarget->isPICStyleGOT()) {
2585 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002586 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002587 } else if (Subtarget->isPICStyleRIPRel() &&
2588 TM.getCodeModel() == CodeModel::Small) {
2589 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00002590 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00002591
2592 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00002593 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00002594 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002595 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002596 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00002597 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00002598
Owen Anderson50288e32008-09-05 00:06:23 +00002599 return ResultReg;
2600}
2601
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002602unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002603 // Fail on dynamic allocas. At this point, getRegForValue has already
2604 // checked its CSE maps, so if we're here trying to handle a dynamic
2605 // alloca, we're not going to succeed. X86SelectAddress has a
2606 // check for dynamic allocas, because it's called directly from
2607 // various places, but TargetMaterializeAlloca also needs a check
2608 // in order to avoid recursion between getRegForValue,
2609 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00002610 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002611 return 0;
Reid Klecknerdfbed592014-01-31 23:45:12 +00002612 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002613
Dan Gohman39d82f92008-09-10 20:11:02 +00002614 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002615 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00002616 return 0;
2617 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00002618 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00002619 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002620 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002621 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00002622 return ResultReg;
2623}
2624
Eli Friedman406c4712011-04-27 22:41:55 +00002625unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2626 MVT VT;
2627 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002628 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002629
2630 // Get opcode and regclass for the given zero.
2631 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002632 const TargetRegisterClass *RC = nullptr;
Eli Friedman406c4712011-04-27 22:41:55 +00002633 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002634 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00002635 case MVT::f32:
2636 if (X86ScalarSSEf32) {
2637 Opc = X86::FsFLD0SS;
2638 RC = &X86::FR32RegClass;
2639 } else {
2640 Opc = X86::LD_Fp032;
2641 RC = &X86::RFP32RegClass;
2642 }
2643 break;
2644 case MVT::f64:
2645 if (X86ScalarSSEf64) {
2646 Opc = X86::FsFLD0SD;
2647 RC = &X86::FR64RegClass;
2648 } else {
2649 Opc = X86::LD_Fp064;
2650 RC = &X86::RFP64RegClass;
2651 }
2652 break;
2653 case MVT::f80:
2654 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002655 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002656 }
2657
2658 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002659 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Eli Friedman406c4712011-04-27 22:41:55 +00002660 return ResultReg;
2661}
2662
2663
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002664bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2665 const LoadInst *LI) {
Chris Lattnereeba0c72010-09-05 02:18:34 +00002666 X86AddressMode AM;
2667 if (!X86SelectAddress(LI->getOperand(0), AM))
2668 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002669
Craig Topper55406d92012-08-11 17:46:16 +00002670 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00002671
Rafael Espindolaea09c592014-02-18 22:05:46 +00002672 unsigned Size = DL.getTypeAllocSize(LI->getType());
Chris Lattnereeba0c72010-09-05 02:18:34 +00002673 unsigned Alignment = LI->getAlignment();
2674
2675 SmallVector<MachineOperand, 8> AddrOps;
2676 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00002677
Chris Lattnereeba0c72010-09-05 02:18:34 +00002678 MachineInstr *Result =
2679 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
Craig Topper062a2ba2014-04-25 05:30:21 +00002680 if (!Result) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002681
Chris Lattner2d186572011-01-16 02:27:38 +00002682 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00002683 MI->eraseFromParent();
2684 return true;
2685}
2686
2687
Evan Cheng24422d42008-09-03 00:03:49 +00002688namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00002689 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2690 const TargetLibraryInfo *libInfo) {
2691 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00002692 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002693}