blob: e041b504af91a77d24246bd3249923f264494f22 [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"
Juergen Ributzka454d3742014-06-13 00:45:11 +000023#include "llvm/Analysis/BranchProbabilityInfo.h"
Dan Gohmand7b5ce32010-07-10 09:00:22 +000024#include "llvm/CodeGen/Analysis.h"
Evan Cheng24422d42008-09-03 00:03:49 +000025#include "llvm/CodeGen/FastISel.h"
Dan Gohman87fb4e82010-07-07 16:29:44 +000026#include "llvm/CodeGen/FunctionLoweringInfo.h"
Owen Anderson50288e32008-09-05 00:06:23 +000027#include "llvm/CodeGen/MachineConstantPool.h"
Evan Cheng6c8f55c2008-09-07 09:09:33 +000028#include "llvm/CodeGen/MachineFrameInfo.h"
Owen Anderson0673a8a2008-08-29 17:45:56 +000029#include "llvm/CodeGen/MachineRegisterInfo.h"
Chandler Carruth219b89b2014-03-04 11:01:28 +000030#include "llvm/IR/CallSite.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/CallingConv.h"
32#include "llvm/IR/DerivedTypes.h"
Chandler Carruth03eb0de2014-03-04 10:40:04 +000033#include "llvm/IR/GetElementPtrTypeIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/GlobalAlias.h"
35#include "llvm/IR/GlobalVariable.h"
36#include "llvm/IR/Instructions.h"
37#include "llvm/IR/IntrinsicInst.h"
38#include "llvm/IR/Operator.h"
Torok Edwin56d06592009-07-11 20:10:48 +000039#include "llvm/Support/ErrorHandling.h"
Evan Chengd10089a2010-01-27 00:00:57 +000040#include "llvm/Target/TargetOptions.h"
Evan Cheng24422d42008-09-03 00:03:49 +000041using namespace llvm;
42
Chris Lattnerd5ac9d82009-03-08 18:44:31 +000043namespace {
Wesley Peck527da1b2010-11-23 03:31:01 +000044
Craig Topper26696312014-03-18 07:27:13 +000045class X86FastISel final : public FastISel {
Evan Cheng24422d42008-09-03 00:03:49 +000046 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
47 /// make the right decision when generating code for different targets.
48 const X86Subtarget *Subtarget;
Evan Cheng6c8f55c2008-09-07 09:09:33 +000049
Wesley Peck527da1b2010-11-23 03:31:01 +000050 /// X86ScalarSSEf32, X86ScalarSSEf64 - Select between SSE or x87
Evan Cheng6c8f55c2008-09-07 09:09:33 +000051 /// floating point ops.
52 /// When SSE is available, use it for f32 operations.
53 /// When SSE2 is available, use it for f64 operations.
54 bool X86ScalarSSEf64;
55 bool X86ScalarSSEf32;
56
Evan Chenga41ee292008-09-03 06:44:39 +000057public:
Bob Wilson3e6fa462012-08-03 04:06:28 +000058 explicit X86FastISel(FunctionLoweringInfo &funcInfo,
59 const TargetLibraryInfo *libInfo)
60 : FastISel(funcInfo, libInfo) {
Evan Cheng8f23ec92008-09-03 01:04:47 +000061 Subtarget = &TM.getSubtarget<X86Subtarget>();
Craig Topperb0c0f722012-01-10 06:54:16 +000062 X86ScalarSSEf64 = Subtarget->hasSSE2();
63 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng8f23ec92008-09-03 01:04:47 +000064 }
Evan Cheng24422d42008-09-03 00:03:49 +000065
Craig Topper2d9361e2014-03-09 07:44:38 +000066 bool TargetSelectInstruction(const Instruction *I) override;
Evan Cheng24422d42008-09-03 00:03:49 +000067
Eli Bendersky90dd3e72013-04-19 22:29:18 +000068 /// \brief The specified machine instr operand is a vreg, and that
Chris Lattnereeba0c72010-09-05 02:18:34 +000069 /// vreg is being provided by the specified load instruction. If possible,
70 /// try to fold the load as an operand to the instruction, returning true if
71 /// possible.
Craig Topper2d9361e2014-03-09 07:44:38 +000072 bool tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
73 const LoadInst *LI) override;
Wesley Peck527da1b2010-11-23 03:31:01 +000074
Craig Topper2d9361e2014-03-09 07:44:38 +000075 bool FastLowerArguments() override;
Chad Rosiera92ef4b2013-02-25 21:59:35 +000076
Dan Gohmandaef7f42008-08-19 21:45:35 +000077#include "X86GenFastISel.inc"
Evan Chenga41ee292008-09-03 06:44:39 +000078
79private:
Dan Gohmanbcaf6812010-04-15 01:51:59 +000080 bool X86FastEmitCompare(const Value *LHS, const Value *RHS, EVT VT);
Wesley Peck527da1b2010-11-23 03:31:01 +000081
Juergen Ributzka349777d2014-06-12 23:27:57 +000082 bool X86FastEmitLoad(EVT VT, const X86AddressMode &AM, MachineMemOperand *MMO,
83 unsigned &ResultReg);
Evan Chengf5bc7e52008-09-05 21:00:03 +000084
Craig Topper4f55b0e2013-07-17 05:57:45 +000085 bool X86FastEmitStore(EVT VT, const Value *Val, const X86AddressMode &AM,
Juergen Ributzka349777d2014-06-12 23:27:57 +000086 MachineMemOperand *MMO = nullptr, bool Aligned = false);
87 bool X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
88 const X86AddressMode &AM,
89 MachineMemOperand *MMO = nullptr, bool Aligned = false);
Evan Cheng6500d172008-09-08 06:35:17 +000090
Owen Anderson53aa7a92009-08-10 22:56:29 +000091 bool X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT, unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +000092 unsigned &ResultReg);
Wesley Peck527da1b2010-11-23 03:31:01 +000093
Dan Gohmanbcaf6812010-04-15 01:51:59 +000094 bool X86SelectAddress(const Value *V, X86AddressMode &AM);
95 bool X86SelectCallAddress(const Value *V, X86AddressMode &AM);
Dan Gohman39d82f92008-09-10 20:11:02 +000096
Dan Gohmanbcaf6812010-04-15 01:51:59 +000097 bool X86SelectLoad(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +000098
Dan Gohmanbcaf6812010-04-15 01:51:59 +000099 bool X86SelectStore(const Instruction *I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +0000100
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000101 bool X86SelectRet(const Instruction *I);
102
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000103 bool X86SelectCmp(const Instruction *I);
Dan Gohmana5753b32008-09-05 01:06:14 +0000104
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000105 bool X86SelectZExt(const Instruction *I);
Dan Gohmana5753b32008-09-05 01:06:14 +0000106
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000107 bool X86SelectBranch(const Instruction *I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +0000108
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000109 bool X86SelectShift(const Instruction *I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +0000110
Eli Bendersky24a36eb2013-04-17 20:10:13 +0000111 bool X86SelectDivRem(const Instruction *I);
112
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000113 bool X86SelectSelect(const Instruction *I);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000114
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000115 bool X86SelectTrunc(const Instruction *I);
Wesley Peck527da1b2010-11-23 03:31:01 +0000116
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000117 bool X86SelectFPExt(const Instruction *I);
118 bool X86SelectFPTrunc(const Instruction *I);
Dan Gohmanbf646f22008-09-10 21:02:08 +0000119
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000120 bool X86VisitIntrinsicCall(const IntrinsicInst &I);
121 bool X86SelectCall(const Instruction *I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000122
Eli Friedmancd2124a2011-06-10 23:39:36 +0000123 bool DoSelectCall(const Instruction *I, const char *MemIntName);
124
Dan Gohman3691d502008-09-25 15:24:26 +0000125 const X86InstrInfo *getInstrInfo() const {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000126 return getTargetMachine()->getInstrInfo();
127 }
128 const X86TargetMachine *getTargetMachine() const {
129 return static_cast<const X86TargetMachine *>(&TM);
Dan Gohman3691d502008-09-25 15:24:26 +0000130 }
131
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000132 bool handleConstantAddresses(const Value *V, X86AddressMode &AM);
133
Craig Topper2d9361e2014-03-09 07:44:38 +0000134 unsigned TargetMaterializeConstant(const Constant *C) override;
Dan Gohman39d82f92008-09-10 20:11:02 +0000135
Craig Topper2d9361e2014-03-09 07:44:38 +0000136 unsigned TargetMaterializeAlloca(const AllocaInst *C) override;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000137
Craig Topper2d9361e2014-03-09 07:44:38 +0000138 unsigned TargetMaterializeFloatZero(const ConstantFP *CF) override;
Eli Friedman406c4712011-04-27 22:41:55 +0000139
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000140 /// isScalarFPTypeInSSEReg - Return true if the specified scalar FP type is
141 /// computed in an SSE register, not on the X87 floating point stack.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000142 bool isScalarFPTypeInSSEReg(EVT VT) const {
Owen Anderson9f944592009-08-11 20:47:22 +0000143 return (VT == MVT::f64 && X86ScalarSSEf64) || // f64 is when SSE2
144 (VT == MVT::f32 && X86ScalarSSEf32); // f32 is when SSE1
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000145 }
146
Chris Lattner229907c2011-07-18 04:54:35 +0000147 bool isTypeLegal(Type *Ty, MVT &VT, bool AllowI1 = false);
Eli Friedmanbcc69142011-04-27 01:45:07 +0000148
Eli Friedman60afcc22011-05-20 22:21:04 +0000149 bool IsMemcpySmall(uint64_t Len);
150
Eli Friedmanbcc69142011-04-27 01:45:07 +0000151 bool TryEmitSmallMemcpy(X86AddressMode DestAM,
152 X86AddressMode SrcAM, uint64_t Len);
Evan Cheng24422d42008-09-03 00:03:49 +0000153};
Wesley Peck527da1b2010-11-23 03:31:01 +0000154
Chris Lattnerd5ac9d82009-03-08 18:44:31 +0000155} // end anonymous namespace.
Dan Gohmand58f3e32008-08-28 23:21:34 +0000156
Chris Lattner229907c2011-07-18 04:54:35 +0000157bool X86FastISel::isTypeLegal(Type *Ty, MVT &VT, bool AllowI1) {
Duncan Sandsf5dda012010-11-03 11:35:31 +0000158 EVT evt = TLI.getValueType(Ty, /*HandleUnknown=*/true);
159 if (evt == MVT::Other || !evt.isSimple())
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000160 // Unhandled type. Halt "fast" selection and bail.
161 return false;
Duncan Sandsf5dda012010-11-03 11:35:31 +0000162
163 VT = evt.getSimpleVT();
Dan Gohman50331362008-09-30 00:48:39 +0000164 // For now, require SSE/SSE2 for performing floating-point operations,
165 // since x87 requires additional work.
Owen Anderson9f944592009-08-11 20:47:22 +0000166 if (VT == MVT::f64 && !X86ScalarSSEf64)
Craig Topper490c45c2012-08-11 17:53:00 +0000167 return false;
Owen Anderson9f944592009-08-11 20:47:22 +0000168 if (VT == MVT::f32 && !X86ScalarSSEf32)
Craig Topper490c45c2012-08-11 17:53:00 +0000169 return false;
Dan Gohman50331362008-09-30 00:48:39 +0000170 // Similarly, no f80 support yet.
Owen Anderson9f944592009-08-11 20:47:22 +0000171 if (VT == MVT::f80)
Dan Gohman50331362008-09-30 00:48:39 +0000172 return false;
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000173 // We only handle legal types. For example, on x86-32 the instruction
174 // selector contains all of the 64-bit instructions from x86-64,
175 // under the assumption that i64 won't be used if the target doesn't
176 // support it.
Owen Anderson9f944592009-08-11 20:47:22 +0000177 return (AllowI1 && VT == MVT::i1) || TLI.isTypeLegal(VT);
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000178}
179
180#include "X86GenCallingConv.inc"
181
Evan Chengf5bc7e52008-09-05 21:00:03 +0000182/// X86FastEmitLoad - Emit a machine instruction to load a value of type VT.
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000183/// The address is either pre-computed, i.e. Ptr, or a GlobalAddress, i.e. GV.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000184/// Return true and the result register by reference if it is possible.
Owen Anderson53aa7a92009-08-10 22:56:29 +0000185bool X86FastISel::X86FastEmitLoad(EVT VT, const X86AddressMode &AM,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000186 MachineMemOperand *MMO, unsigned &ResultReg) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000187 // Get opcode and regclass of the output for the given load instruction.
188 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000189 const TargetRegisterClass *RC = nullptr;
Owen Anderson9f944592009-08-11 20:47:22 +0000190 switch (VT.getSimpleVT().SimpleTy) {
Evan Chengf5bc7e52008-09-05 21:00:03 +0000191 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000192 case MVT::i1:
Owen Anderson9f944592009-08-11 20:47:22 +0000193 case MVT::i8:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000194 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +0000195 RC = &X86::GR8RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000196 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000197 case MVT::i16:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000198 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +0000199 RC = &X86::GR16RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000200 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000201 case MVT::i32:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000202 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +0000203 RC = &X86::GR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000204 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000205 case MVT::i64:
Evan Chengf5bc7e52008-09-05 21:00:03 +0000206 // Must be in x86-64 mode.
207 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +0000208 RC = &X86::GR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000209 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000210 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000211 if (X86ScalarSSEf32) {
212 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +0000213 RC = &X86::FR32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000214 } else {
215 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +0000216 RC = &X86::RFP32RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000217 }
218 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000219 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000220 if (X86ScalarSSEf64) {
221 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +0000222 RC = &X86::FR64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000223 } else {
224 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +0000225 RC = &X86::RFP64RegClass;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000226 }
227 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000228 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +0000229 // No f80 support yet.
230 return false;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000231 }
232
233 ResultReg = createResultReg(RC);
Juergen Ributzka349777d2014-06-12 23:27:57 +0000234 MachineInstrBuilder MIB =
235 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
236 addFullAddress(MIB, AM);
237 if (MMO)
238 MIB->addMemOperand(*FuncInfo.MF, MMO);
Evan Chengf5bc7e52008-09-05 21:00:03 +0000239 return true;
240}
241
Evan Cheng6c8f55c2008-09-07 09:09:33 +0000242/// X86FastEmitStore - Emit a machine instruction to store a value Val of
243/// type VT. The address is either pre-computed, consisted of a base ptr, Ptr
244/// and a displacement offset, or a GlobalAddress,
Evan Chengf5bc7e52008-09-05 21:00:03 +0000245/// i.e. V. Return true if it is possible.
Juergen Ributzka349777d2014-06-12 23:27:57 +0000246bool X86FastISel::X86FastEmitStore(EVT VT, unsigned ValReg, bool ValIsKill,
247 const X86AddressMode &AM,
248 MachineMemOperand *MMO, bool Aligned) {
Dan Gohman8f658ba2008-09-08 16:31:35 +0000249 // Get opcode and regclass of the output for the given store instruction.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000250 unsigned Opc = 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000251 switch (VT.getSimpleVT().SimpleTy) {
252 case MVT::f80: // No f80 support yet.
Evan Chengf5bc7e52008-09-05 21:00:03 +0000253 default: return false;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000254 case MVT::i1: {
255 // Mask out all but lowest bit.
Craig Topperabadc662012-04-20 06:31:50 +0000256 unsigned AndResult = createResultReg(&X86::GR8RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +0000257 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000258 TII.get(X86::AND8ri), AndResult)
259 .addReg(ValReg, getKillRegState(ValIsKill)).addImm(1);
Craig Topper4f55b0e2013-07-17 05:57:45 +0000260 ValReg = AndResult;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000261 }
262 // FALLTHROUGH, handling i1 as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000263 case MVT::i8: Opc = X86::MOV8mr; break;
264 case MVT::i16: Opc = X86::MOV16mr; break;
265 case MVT::i32: Opc = X86::MOV32mr; break;
266 case MVT::i64: Opc = X86::MOV64mr; break; // Must be in x86-64 mode.
267 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000268 Opc = X86ScalarSSEf32 ?
269 (Subtarget->hasAVX() ? X86::VMOVSSmr : X86::MOVSSmr) : X86::ST_Fp32m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000270 break;
Owen Anderson9f944592009-08-11 20:47:22 +0000271 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000272 Opc = X86ScalarSSEf64 ?
273 (Subtarget->hasAVX() ? X86::VMOVSDmr : X86::MOVSDmr) : X86::ST_Fp64m;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000274 break;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000275 case MVT::v4f32:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000276 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000277 Opc = Subtarget->hasAVX() ? X86::VMOVAPSmr : X86::MOVAPSmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000278 else
Craig Topper55475d42013-07-17 06:58:23 +0000279 Opc = Subtarget->hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000280 break;
281 case MVT::v2f64:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000282 if (Aligned)
Craig Topperad1fff92013-07-18 07:16:44 +0000283 Opc = Subtarget->hasAVX() ? X86::VMOVAPDmr : X86::MOVAPDmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000284 else
Craig Topperad1fff92013-07-18 07:16:44 +0000285 Opc = Subtarget->hasAVX() ? X86::VMOVUPDmr : X86::MOVUPDmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000286 break;
287 case MVT::v4i32:
288 case MVT::v2i64:
289 case MVT::v8i16:
290 case MVT::v16i8:
Craig Topper4f55b0e2013-07-17 05:57:45 +0000291 if (Aligned)
Craig Topper55475d42013-07-17 06:58:23 +0000292 Opc = Subtarget->hasAVX() ? X86::VMOVDQAmr : X86::MOVDQAmr;
Craig Topper4f55b0e2013-07-17 05:57:45 +0000293 else
Craig Topper55475d42013-07-17 06:58:23 +0000294 Opc = Subtarget->hasAVX() ? X86::VMOVDQUmr : X86::MOVDQUmr;
Lang Hames7d2f7b52011-10-18 22:11:33 +0000295 break;
Evan Chengf5bc7e52008-09-05 21:00:03 +0000296 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000297
Juergen Ributzka349777d2014-06-12 23:27:57 +0000298 MachineInstrBuilder MIB =
299 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
300 addFullAddress(MIB, AM).addReg(ValReg, getKillRegState(ValIsKill));
301 if (MMO)
302 MIB->addMemOperand(*FuncInfo.MF, MMO);
303
Evan Chengf5bc7e52008-09-05 21:00:03 +0000304 return true;
305}
306
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000307bool X86FastISel::X86FastEmitStore(EVT VT, const Value *Val,
Juergen Ributzka349777d2014-06-12 23:27:57 +0000308 const X86AddressMode &AM,
309 MachineMemOperand *MMO, bool Aligned) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000310 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000311 if (isa<ConstantPointerNull>(Val))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000312 Val = Constant::getNullValue(DL.getIntPtrType(Val->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000313
Chris Lattner3ba29352008-10-15 05:30:52 +0000314 // If this is a store of a simple constant, fold the constant into the store.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000315 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Val)) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000316 unsigned Opc = 0;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000317 bool Signed = true;
Owen Anderson9f944592009-08-11 20:47:22 +0000318 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner3ba29352008-10-15 05:30:52 +0000319 default: break;
Dan Gohman7f0ca9a2009-08-27 00:31:47 +0000320 case MVT::i1: Signed = false; // FALLTHROUGH to handle as i8.
Owen Anderson9f944592009-08-11 20:47:22 +0000321 case MVT::i8: Opc = X86::MOV8mi; break;
322 case MVT::i16: Opc = X86::MOV16mi; break;
323 case MVT::i32: Opc = X86::MOV32mi; break;
324 case MVT::i64:
Chris Lattner3ba29352008-10-15 05:30:52 +0000325 // Must be a 32-bit sign extended value.
Jakub Staszak11d1aee2012-11-15 19:05:23 +0000326 if (isInt<32>(CI->getSExtValue()))
Chris Lattner3ba29352008-10-15 05:30:52 +0000327 Opc = X86::MOV64mi32;
328 break;
329 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000330
Chris Lattner3ba29352008-10-15 05:30:52 +0000331 if (Opc) {
Juergen Ributzka349777d2014-06-12 23:27:57 +0000332 MachineInstrBuilder MIB =
333 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc));
334 addFullAddress(MIB, AM).addImm(Signed ? (uint64_t) CI->getSExtValue()
335 : CI->getZExtValue());
336 if (MMO)
337 MIB->addMemOperand(*FuncInfo.MF, MMO);
Chris Lattner3ba29352008-10-15 05:30:52 +0000338 return true;
339 }
340 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000341
Chris Lattner3ba29352008-10-15 05:30:52 +0000342 unsigned ValReg = getRegForValue(Val);
343 if (ValReg == 0)
Wesley Peck527da1b2010-11-23 03:31:01 +0000344 return false;
345
Juergen Ributzka349777d2014-06-12 23:27:57 +0000346 bool ValKill = hasTrivialKill(Val);
347 return X86FastEmitStore(VT, ValReg, ValKill, AM, MMO, Aligned);
Chris Lattner3ba29352008-10-15 05:30:52 +0000348}
349
Evan Cheng6500d172008-09-08 06:35:17 +0000350/// X86FastEmitExtend - Emit a machine instruction to extend a value Src of
351/// type SrcVT to type DstVT using the specified extension opcode Opc (e.g.
352/// ISD::SIGN_EXTEND).
Owen Anderson53aa7a92009-08-10 22:56:29 +0000353bool X86FastISel::X86FastEmitExtend(ISD::NodeType Opc, EVT DstVT,
354 unsigned Src, EVT SrcVT,
Evan Cheng6500d172008-09-08 06:35:17 +0000355 unsigned &ResultReg) {
Dan Gohman1a1b51f2010-05-11 23:54:07 +0000356 unsigned RR = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Opc,
357 Src, /*TODO: Kill=*/false);
Jakub Staszak701cc972013-02-14 21:50:09 +0000358 if (RR == 0)
Owen Anderson453564b2008-09-11 19:44:55 +0000359 return false;
Jakub Staszak701cc972013-02-14 21:50:09 +0000360
361 ResultReg = RR;
362 return true;
Evan Cheng6500d172008-09-08 06:35:17 +0000363}
364
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000365bool X86FastISel::handleConstantAddresses(const Value *V, X86AddressMode &AM) {
366 // Handle constant address.
367 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
368 // Can't handle alternate code models yet.
369 if (TM.getCodeModel() != CodeModel::Small)
370 return false;
371
372 // Can't handle TLS yet.
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000373 if (GV->isThreadLocal())
374 return false;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000375
376 // RIP-relative addresses can't have additional register operands, so if
377 // we've already folded stuff into the addressing mode, just force the
378 // global value into its own register, which we can use as the basereg.
379 if (!Subtarget->isPICStyleRIPRel() ||
380 (AM.Base.Reg == 0 && AM.IndexReg == 0)) {
381 // Okay, we've committed to selecting this global. Set up the address.
382 AM.GV = GV;
383
384 // Allow the subtarget to classify the global.
385 unsigned char GVFlags = Subtarget->ClassifyGlobalReference(GV, TM);
386
387 // If this reference is relative to the pic base, set it now.
388 if (isGlobalRelativeToPICBase(GVFlags)) {
389 // FIXME: How do we know Base.Reg is free??
390 AM.Base.Reg = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
391 }
392
393 // Unless the ABI requires an extra load, return a direct reference to
394 // the global.
395 if (!isGlobalStubReference(GVFlags)) {
396 if (Subtarget->isPICStyleRIPRel()) {
397 // Use rip-relative addressing if we can. Above we verified that the
398 // base and index registers are unused.
399 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
400 AM.Base.Reg = X86::RIP;
401 }
402 AM.GVOpFlags = GVFlags;
403 return true;
404 }
405
406 // Ok, we need to do a load from a stub. If we've already loaded from
407 // this stub, reuse the loaded pointer, otherwise emit the load now.
408 DenseMap<const Value*, unsigned>::iterator I = LocalValueMap.find(V);
409 unsigned LoadReg;
410 if (I != LocalValueMap.end() && I->second != 0) {
411 LoadReg = I->second;
412 } else {
413 // Issue load from stub.
414 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000415 const TargetRegisterClass *RC = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000416 X86AddressMode StubAM;
417 StubAM.Base.Reg = AM.Base.Reg;
418 StubAM.GV = GV;
419 StubAM.GVOpFlags = GVFlags;
420
421 // Prepare for inserting code in the local-value area.
422 SavePoint SaveInsertPt = enterLocalValueArea();
423
424 if (TLI.getPointerTy() == MVT::i64) {
425 Opc = X86::MOV64rm;
426 RC = &X86::GR64RegClass;
427
428 if (Subtarget->isPICStyleRIPRel())
429 StubAM.Base.Reg = X86::RIP;
430 } else {
431 Opc = X86::MOV32rm;
432 RC = &X86::GR32RegClass;
433 }
434
435 LoadReg = createResultReg(RC);
436 MachineInstrBuilder LoadMI =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000437 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), LoadReg);
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000438 addFullAddress(LoadMI, StubAM);
439
440 // Ok, back to normal mode.
441 leaveLocalValueArea(SaveInsertPt);
442
443 // Prevent loading GV stub multiple times in same MBB.
444 LocalValueMap[V] = LoadReg;
445 }
446
447 // Now construct the final address. Note that the Disp, Scale,
448 // and Index values may already be set here.
449 AM.Base.Reg = LoadReg;
Craig Topper062a2ba2014-04-25 05:30:21 +0000450 AM.GV = nullptr;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000451 return true;
452 }
453 }
454
455 // If all else fails, try to materialize the value in a register.
456 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
457 if (AM.Base.Reg == 0) {
458 AM.Base.Reg = getRegForValue(V);
459 return AM.Base.Reg != 0;
460 }
461 if (AM.IndexReg == 0) {
462 assert(AM.Scale == 1 && "Scale with no index!");
463 AM.IndexReg = getRegForValue(V);
464 return AM.IndexReg != 0;
465 }
466 }
467
468 return false;
469}
470
Dan Gohman39d82f92008-09-10 20:11:02 +0000471/// X86SelectAddress - Attempt to fill in an address from the given value.
472///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000473bool X86FastISel::X86SelectAddress(const Value *V, X86AddressMode &AM) {
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000474 SmallVector<const Value *, 32> GEPs;
Bill Wendling585a9012013-09-24 00:13:08 +0000475redo_gep:
Craig Topper062a2ba2014-04-25 05:30:21 +0000476 const User *U = nullptr;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000477 unsigned Opcode = Instruction::UserOp1;
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000478 if (const Instruction *I = dyn_cast<Instruction>(V)) {
Dan Gohmanaf4903d2010-06-18 20:44:47 +0000479 // Don't walk into other basic blocks; it's possible we haven't
480 // visited them yet, so the instructions may not yet be assigned
481 // virtual registers.
Dan Gohmanaeb5e662010-11-16 22:43:23 +0000482 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(V)) ||
483 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
484 Opcode = I->getOpcode();
485 U = I;
486 }
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000487 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Dan Gohman6e005fd2008-09-18 23:23:44 +0000488 Opcode = C->getOpcode();
489 U = C;
490 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000491
Chris Lattner229907c2011-07-18 04:54:35 +0000492 if (PointerType *Ty = dyn_cast<PointerType>(V->getType()))
Chris Lattner874c92b2010-06-15 19:08:40 +0000493 if (Ty->getAddressSpace() > 255)
Dan Gohmana46d6072010-06-18 20:45:41 +0000494 // Fast instruction selection doesn't support the special
495 // address spaces.
Chris Lattner874c92b2010-06-15 19:08:40 +0000496 return false;
497
Dan Gohman6e005fd2008-09-18 23:23:44 +0000498 switch (Opcode) {
499 default: break;
500 case Instruction::BitCast:
501 // Look past bitcasts.
Chris Lattner8212d372009-07-10 05:33:42 +0000502 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman6e005fd2008-09-18 23:23:44 +0000503
504 case Instruction::IntToPtr:
505 // Look past no-op inttoptrs.
506 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000507 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000508 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000509
510 case Instruction::PtrToInt:
511 // Look past no-op ptrtoints.
512 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000513 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohmanbc55c2a2008-12-08 23:50:06 +0000514 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000515
516 case Instruction::Alloca: {
517 // Do static allocas.
518 const AllocaInst *A = cast<AllocaInst>(V);
Dan Gohman87fb4e82010-07-07 16:29:44 +0000519 DenseMap<const AllocaInst*, int>::iterator SI =
520 FuncInfo.StaticAllocaMap.find(A);
521 if (SI != FuncInfo.StaticAllocaMap.end()) {
Dan Gohman007a6bb2008-09-26 19:15:30 +0000522 AM.BaseType = X86AddressMode::FrameIndexBase;
523 AM.Base.FrameIndex = SI->second;
524 return true;
525 }
526 break;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000527 }
528
529 case Instruction::Add: {
530 // Adds of constants are common and easy enough.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000531 if (const ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
Dan Gohman2564b902008-09-26 20:04:15 +0000532 uint64_t Disp = (int32_t)AM.Disp + (uint64_t)CI->getSExtValue();
533 // They have to fit in the 32-bit signed displacement field though.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000534 if (isInt<32>(Disp)) {
Dan Gohman2564b902008-09-26 20:04:15 +0000535 AM.Disp = (uint32_t)Disp;
Chris Lattner8212d372009-07-10 05:33:42 +0000536 return X86SelectAddress(U->getOperand(0), AM);
Dan Gohman2564b902008-09-26 20:04:15 +0000537 }
Dan Gohman39d82f92008-09-10 20:11:02 +0000538 }
Dan Gohman6e005fd2008-09-18 23:23:44 +0000539 break;
540 }
541
542 case Instruction::GetElementPtr: {
Chris Lattner795667b2010-03-04 19:54:45 +0000543 X86AddressMode SavedAM = AM;
544
Dan Gohman6e005fd2008-09-18 23:23:44 +0000545 // Pattern-match simple GEPs.
Dan Gohman2564b902008-09-26 20:04:15 +0000546 uint64_t Disp = (int32_t)AM.Disp;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000547 unsigned IndexReg = AM.IndexReg;
548 unsigned Scale = AM.Scale;
549 gep_type_iterator GTI = gep_type_begin(U);
Dan Gohman4c315242008-12-08 07:57:47 +0000550 // Iterate through the indices, folding what we can. Constants can be
551 // folded, and one dynamic index can be handled, if the scale is supported.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000552 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end();
Dan Gohman6e005fd2008-09-18 23:23:44 +0000553 i != e; ++i, ++GTI) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000554 const Value *Op = *i;
Chris Lattner229907c2011-07-18 04:54:35 +0000555 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000556 const StructLayout *SL = DL.getStructLayout(STy);
Chris Lattner4b026b92011-04-17 17:05:12 +0000557 Disp += SL->getElementOffset(cast<ConstantInt>(Op)->getZExtValue());
558 continue;
559 }
Eric Christopher0713a9d2011-06-08 23:55:35 +0000560
Chris Lattner4b026b92011-04-17 17:05:12 +0000561 // A array/variable index is always of the form i*S where S is the
562 // constant scale size. See if we can push the scale into immediates.
Rafael Espindolaea09c592014-02-18 22:05:46 +0000563 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
Chris Lattner4b026b92011-04-17 17:05:12 +0000564 for (;;) {
565 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
566 // Constant-offset addressing.
567 Disp += CI->getSExtValue() * S;
568 break;
Dan Gohmanc1783b32011-03-22 00:04:35 +0000569 }
Bob Wilson9f3e6b22013-11-15 19:09:27 +0000570 if (canFoldAddIntoGEP(U, Op)) {
571 // A compatible add with a constant operand. Fold the constant.
Chris Lattner4b026b92011-04-17 17:05:12 +0000572 ConstantInt *CI =
573 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
574 Disp += CI->getSExtValue() * S;
575 // Iterate on the other operand.
576 Op = cast<AddOperator>(Op)->getOperand(0);
577 continue;
578 }
579 if (IndexReg == 0 &&
580 (!AM.GV || !Subtarget->isPICStyleRIPRel()) &&
581 (S == 1 || S == 2 || S == 4 || S == 8)) {
582 // Scaled-index addressing.
583 Scale = S;
584 IndexReg = getRegForGEPIndex(Op).first;
585 if (IndexReg == 0)
586 return false;
587 break;
588 }
589 // Unsupported.
590 goto unsupported_gep;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000591 }
592 }
Bill Wendling585a9012013-09-24 00:13:08 +0000593
Dan Gohman2564b902008-09-26 20:04:15 +0000594 // Check for displacement overflow.
Benjamin Kramer2788f792010-03-29 21:13:41 +0000595 if (!isInt<32>(Disp))
Dan Gohman2564b902008-09-26 20:04:15 +0000596 break;
Bill Wendling585a9012013-09-24 00:13:08 +0000597
Dan Gohman6e005fd2008-09-18 23:23:44 +0000598 AM.IndexReg = IndexReg;
599 AM.Scale = Scale;
Dan Gohman2564b902008-09-26 20:04:15 +0000600 AM.Disp = (uint32_t)Disp;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000601 GEPs.push_back(V);
Bill Wendling585a9012013-09-24 00:13:08 +0000602
603 if (const GetElementPtrInst *GEP =
604 dyn_cast<GetElementPtrInst>(U->getOperand(0))) {
605 // Ok, the GEP indices were covered by constant-offset and scaled-index
606 // addressing. Update the address state and move on to examining the base.
607 V = GEP;
608 goto redo_gep;
609 } else if (X86SelectAddress(U->getOperand(0), AM)) {
Chris Lattner6ce8e242010-03-04 19:48:19 +0000610 return true;
Bill Wendling585a9012013-09-24 00:13:08 +0000611 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000612
Chris Lattner4b026b92011-04-17 17:05:12 +0000613 // If we couldn't merge the gep value into this addr mode, revert back to
Chris Lattner6ce8e242010-03-04 19:48:19 +0000614 // our address and just match the value instead of completely failing.
615 AM = SavedAM;
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000616
617 for (SmallVectorImpl<const Value *>::reverse_iterator
618 I = GEPs.rbegin(), E = GEPs.rend(); I != E; ++I)
619 if (handleConstantAddresses(*I, AM))
620 return true;
621
622 return false;
Dan Gohman6e005fd2008-09-18 23:23:44 +0000623 unsupported_gep:
624 // Ok, the GEP indices weren't all covered.
625 break;
626 }
627 }
628
Bill Wendlingc63c30c2013-09-24 07:19:30 +0000629 return handleConstantAddresses(V, AM);
Dan Gohman39d82f92008-09-10 20:11:02 +0000630}
631
Chris Lattner8212d372009-07-10 05:33:42 +0000632/// X86SelectCallAddress - Attempt to fill in an address from the given value.
633///
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000634bool X86FastISel::X86SelectCallAddress(const Value *V, X86AddressMode &AM) {
Craig Topper062a2ba2014-04-25 05:30:21 +0000635 const User *U = nullptr;
Chris Lattner8212d372009-07-10 05:33:42 +0000636 unsigned Opcode = Instruction::UserOp1;
Quentin Colombet778dba12013-10-14 22:32:09 +0000637 const Instruction *I = dyn_cast<Instruction>(V);
Quentin Colombetf34568b2013-10-22 21:29:08 +0000638 // Record if the value is defined in the same basic block.
639 //
640 // This information is crucial to know whether or not folding an
641 // operand is valid.
642 // Indeed, FastISel generates or reuses a virtual register for all
643 // operands of all instructions it selects. Obviously, the definition and
644 // its uses must use the same virtual register otherwise the produced
645 // code is incorrect.
646 // Before instruction selection, FunctionLoweringInfo::set sets the virtual
647 // registers for values that are alive across basic blocks. This ensures
648 // that the values are consistently set between across basic block, even
649 // if different instruction selection mechanisms are used (e.g., a mix of
650 // SDISel and FastISel).
651 // For values local to a basic block, the instruction selection process
652 // generates these virtual registers with whatever method is appropriate
653 // for its needs. In particular, FastISel and SDISel do not share the way
654 // local virtual registers are set.
655 // Therefore, this is impossible (or at least unsafe) to share values
656 // between basic blocks unless they use the same instruction selection
657 // method, which is not guarantee for X86.
658 // Moreover, things like hasOneUse could not be used accurately, if we
659 // allow to reference values across basic blocks whereas they are not
660 // alive across basic blocks initially.
Quentin Colombet778dba12013-10-14 22:32:09 +0000661 bool InMBB = true;
662 if (I) {
Chris Lattner8212d372009-07-10 05:33:42 +0000663 Opcode = I->getOpcode();
664 U = I;
Quentin Colombet778dba12013-10-14 22:32:09 +0000665 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000666 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000667 Opcode = C->getOpcode();
668 U = C;
669 }
670
671 switch (Opcode) {
672 default: break;
673 case Instruction::BitCast:
Quentin Colombet778dba12013-10-14 22:32:09 +0000674 // Look past bitcasts if its operand is in the same BB.
675 if (InMBB)
676 return X86SelectCallAddress(U->getOperand(0), AM);
677 break;
Chris Lattner8212d372009-07-10 05:33:42 +0000678
679 case Instruction::IntToPtr:
Quentin Colombet778dba12013-10-14 22:32:09 +0000680 // Look past no-op inttoptrs if its operand is in the same BB.
681 if (InMBB &&
682 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000683 return X86SelectCallAddress(U->getOperand(0), AM);
684 break;
685
686 case Instruction::PtrToInt:
Quentin Colombet778dba12013-10-14 22:32:09 +0000687 // Look past no-op ptrtoints if its operand is in the same BB.
688 if (InMBB &&
689 TLI.getValueType(U->getType()) == TLI.getPointerTy())
Chris Lattner8212d372009-07-10 05:33:42 +0000690 return X86SelectCallAddress(U->getOperand(0), AM);
691 break;
692 }
693
694 // Handle constant address.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000695 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
Chris Lattner8212d372009-07-10 05:33:42 +0000696 // Can't handle alternate code models yet.
Chris Lattner25e7f912009-07-10 21:03:06 +0000697 if (TM.getCodeModel() != CodeModel::Small)
Chris Lattner8212d372009-07-10 05:33:42 +0000698 return false;
699
700 // RIP-relative addresses can't have additional register operands.
701 if (Subtarget->isPICStyleRIPRel() &&
702 (AM.Base.Reg != 0 || AM.IndexReg != 0))
703 return false;
704
Rafael Espindolaea09c592014-02-18 22:05:46 +0000705 // Can't handle DbgLocLImport.
Nico Rieck7157bb72014-01-14 15:22:47 +0000706 if (GV->hasDLLImportStorageClass())
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000707 return false;
708
709 // Can't handle TLS.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000710 if (const GlobalVariable *GVar = dyn_cast<GlobalVariable>(GV))
NAKAMURA Takumi860abd02011-02-21 04:50:06 +0000711 if (GVar->isThreadLocal())
Chris Lattner8212d372009-07-10 05:33:42 +0000712 return false;
713
714 // Okay, we've committed to selecting this global. Set up the basic address.
715 AM.GV = GV;
Wesley Peck527da1b2010-11-23 03:31:01 +0000716
Chris Lattner7277a802009-07-10 05:45:15 +0000717 // No ABI requires an extra load for anything other than DLLImport, which
718 // we rejected above. Return a direct reference to the global.
Chris Lattner7277a802009-07-10 05:45:15 +0000719 if (Subtarget->isPICStyleRIPRel()) {
720 // Use rip-relative addressing if we can. Above we verified that the
721 // base and index registers are unused.
722 assert(AM.Base.Reg == 0 && AM.IndexReg == 0);
723 AM.Base.Reg = X86::RIP;
Chris Lattner21c29402009-07-10 21:00:45 +0000724 } else if (Subtarget->isPICStyleStubPIC()) {
Chris Lattner7277a802009-07-10 05:45:15 +0000725 AM.GVOpFlags = X86II::MO_PIC_BASE_OFFSET;
726 } else if (Subtarget->isPICStyleGOT()) {
727 AM.GVOpFlags = X86II::MO_GOTOFF;
Chris Lattner8212d372009-07-10 05:33:42 +0000728 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000729
Chris Lattner8212d372009-07-10 05:33:42 +0000730 return true;
731 }
732
733 // If all else fails, try to materialize the value in a register.
734 if (!AM.GV || !Subtarget->isPICStyleRIPRel()) {
735 if (AM.Base.Reg == 0) {
736 AM.Base.Reg = getRegForValue(V);
737 return AM.Base.Reg != 0;
738 }
739 if (AM.IndexReg == 0) {
740 assert(AM.Scale == 1 && "Scale with no index!");
741 AM.IndexReg = getRegForValue(V);
742 return AM.IndexReg != 0;
743 }
744 }
745
746 return false;
747}
748
749
Owen Anderson4f948bd2008-09-04 07:08:58 +0000750/// X86SelectStore - Select and emit code to implement store instructions.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000751bool X86FastISel::X86SelectStore(const Instruction *I) {
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000752 // Atomic stores need special handling.
Lang Hames7d2f7b52011-10-18 22:11:33 +0000753 const StoreInst *S = cast<StoreInst>(I);
754
755 if (S->isAtomic())
756 return false;
757
Juergen Ributzka349777d2014-06-12 23:27:57 +0000758 const Value *Val = S->getValueOperand();
759 const Value *Ptr = S->getPointerOperand();
Craig Topper4f55b0e2013-07-17 05:57:45 +0000760
Duncan Sandsf5dda012010-11-03 11:35:31 +0000761 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000762 if (!isTypeLegal(Val->getType(), VT, /*AllowI1=*/true))
Owen Anderson4f948bd2008-09-04 07:08:58 +0000763 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000764
Juergen Ributzka349777d2014-06-12 23:27:57 +0000765 unsigned Alignment = S->getAlignment();
766 unsigned ABIAlignment = DL.getABITypeAlignment(Val->getType());
767 if (Alignment == 0) // Ensure that codegen never sees alignment 0
768 Alignment = ABIAlignment;
769 bool Aligned = Alignment >= ABIAlignment;
770
Dan Gohman39d82f92008-09-10 20:11:02 +0000771 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000772 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000773 return false;
Owen Anderson4f948bd2008-09-04 07:08:58 +0000774
Juergen Ributzka349777d2014-06-12 23:27:57 +0000775 return X86FastEmitStore(VT, Val, AM, createMachineMemOperandFor(I), Aligned);
Owen Anderson4f948bd2008-09-04 07:08:58 +0000776}
777
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000778/// X86SelectRet - Select and emit code to implement ret instructions.
779bool X86FastISel::X86SelectRet(const Instruction *I) {
780 const ReturnInst *Ret = cast<ReturnInst>(I);
781 const Function &F = *I->getParent()->getParent();
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000782 const X86MachineFunctionInfo *X86MFInfo =
783 FuncInfo.MF->getInfo<X86MachineFunctionInfo>();
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000784
785 if (!FuncInfo.CanLowerReturn)
786 return false;
787
788 CallingConv::ID CC = F.getCallingConv();
789 if (CC != CallingConv::C &&
790 CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +0000791 CC != CallingConv::X86_FastCall &&
792 CC != CallingConv::X86_64_SysV)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000793 return false;
794
Charles Davise8f297c2013-07-12 06:02:35 +0000795 if (Subtarget->isCallingConvWin64(CC))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000796 return false;
797
798 // Don't handle popping bytes on return for now.
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000799 if (X86MFInfo->getBytesToPopOnReturn() != 0)
Jakub Staszak74010cd2013-02-17 18:35:25 +0000800 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000801
802 // fastcc with -tailcallopt is intended to provide a guaranteed
803 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +0000804 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000805 return false;
806
807 // Let SDISel handle vararg functions.
808 if (F.isVarArg())
809 return false;
810
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000811 // Build a list of return value registers.
812 SmallVector<unsigned, 4> RetRegs;
813
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000814 if (Ret->getNumOperands() > 0) {
815 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +0000816 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000817
818 // Analyze operands of the call, assigning locations to each operand.
819 SmallVector<CCValAssign, 16> ValLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +0000820 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +0000821 I->getContext());
Duncan Sandsfa7e6f22010-10-31 13:02:38 +0000822 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000823
824 const Value *RV = Ret->getOperand(0);
825 unsigned Reg = getRegForValue(RV);
826 if (Reg == 0)
827 return false;
828
829 // Only handle a single return value for now.
830 if (ValLocs.size() != 1)
831 return false;
832
833 CCValAssign &VA = ValLocs[0];
Wesley Peck527da1b2010-11-23 03:31:01 +0000834
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000835 // Don't bother handling odd stuff for now.
836 if (VA.getLocInfo() != CCValAssign::Full)
837 return false;
838 // Only handle register returns for now.
839 if (!VA.isRegLoc())
840 return false;
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000841
842 // The calling-convention tables for x87 returns don't tell
843 // the whole story.
844 if (VA.getLocReg() == X86::ST0 || VA.getLocReg() == X86::ST1)
845 return false;
846
Eli Friedman6fc94dd2011-05-18 23:13:10 +0000847 unsigned SrcReg = Reg + VA.getValNo();
Eli Friedman22da7992011-05-19 22:16:13 +0000848 EVT SrcVT = TLI.getValueType(RV->getType());
849 EVT DstVT = VA.getValVT();
850 // Special handling for extended integers.
851 if (SrcVT != DstVT) {
852 if (SrcVT != MVT::i1 && SrcVT != MVT::i8 && SrcVT != MVT::i16)
853 return false;
854
855 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
856 return false;
857
858 assert(DstVT == MVT::i32 && "X86 should always ext to i32");
859
860 if (SrcVT == MVT::i1) {
861 if (Outs[0].Flags.isSExt())
862 return false;
863 SrcReg = FastEmitZExtFromI1(MVT::i8, SrcReg, /*TODO: Kill=*/false);
864 SrcVT = MVT::i8;
865 }
866 unsigned Op = Outs[0].Flags.isZExt() ? ISD::ZERO_EXTEND :
867 ISD::SIGN_EXTEND;
868 SrcReg = FastEmit_r(SrcVT.getSimpleVT(), DstVT.getSimpleVT(), Op,
869 SrcReg, /*TODO: Kill=*/false);
870 }
871
872 // Make the copy.
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000873 unsigned DstReg = VA.getLocReg();
874 const TargetRegisterClass* SrcRC = MRI.getRegClass(SrcReg);
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000875 // Avoid a cross-class copy. This is very unlikely.
876 if (!SrcRC->contains(DstReg))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000877 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000878 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen89696572010-07-11 05:17:02 +0000879 DstReg).addReg(SrcReg);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000880
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000881 // Add register to return instruction.
882 RetRegs.push_back(VA.getLocReg());
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000883 }
884
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000885 // The x86-64 ABI for returning structs by value requires that we copy
886 // the sret argument into %rax for the return. We saved the argument into
887 // a virtual register in the entry block, so now we copy the value out
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000888 // and into %rax. We also do the same with %eax for Win32.
889 if (F.hasStructRetAttr() &&
Yaron Keren136fe7d2014-04-01 18:15:34 +0000890 (Subtarget->is64Bit() || Subtarget->isTargetKnownWindowsMSVC())) {
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000891 unsigned Reg = X86MFInfo->getSRetReturnReg();
892 assert(Reg &&
893 "SRetReturnReg should have been set in LowerFormalArguments()!");
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000894 unsigned RetReg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000895 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Timur Iskhodzhanova2fd5fd2013-03-28 21:30:04 +0000896 RetReg).addReg(Reg);
897 RetRegs.push_back(RetReg);
Nick Lewyckyf8fc8922012-10-02 22:45:06 +0000898 }
899
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000900 // Now emit the RET.
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000901 MachineInstrBuilder MIB =
Rafael Espindolaea09c592014-02-18 22:05:46 +0000902 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Subtarget->is64Bit() ? X86::RETQ : X86::RETL));
Jakob Stoklund Olesendc69f6f2013-02-05 17:59:48 +0000903 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
904 MIB.addReg(RetRegs[i], RegState::Implicit);
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000905 return true;
906}
907
Evan Chenga41ee292008-09-03 06:44:39 +0000908/// X86SelectLoad - Select and emit code to implement load instructions.
909///
Juergen Ributzka349777d2014-06-12 23:27:57 +0000910bool X86FastISel::X86SelectLoad(const Instruction *I) {
911 const LoadInst *LI = cast<LoadInst>(I);
912
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000913 // Atomic loads need special handling.
Juergen Ributzka349777d2014-06-12 23:27:57 +0000914 if (LI->isAtomic())
Eli Friedmanf3dd6da2011-09-02 22:33:24 +0000915 return false;
916
Duncan Sandsf5dda012010-11-03 11:35:31 +0000917 MVT VT;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000918 if (!isTypeLegal(LI->getType(), VT, /*AllowI1=*/true))
Evan Chenga41ee292008-09-03 06:44:39 +0000919 return false;
920
Juergen Ributzka349777d2014-06-12 23:27:57 +0000921 const Value *Ptr = LI->getPointerOperand();
922
Dan Gohman39d82f92008-09-10 20:11:02 +0000923 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000924 if (!X86SelectAddress(Ptr, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +0000925 return false;
Evan Chenga41ee292008-09-03 06:44:39 +0000926
Evan Chengf5bc7e52008-09-05 21:00:03 +0000927 unsigned ResultReg = 0;
Juergen Ributzka349777d2014-06-12 23:27:57 +0000928 if (!X86FastEmitLoad(VT, AM, createMachineMemOperandFor(LI), ResultReg))
929 return false;
930
931 UpdateValueMap(I, ResultReg);
932 return true;
Evan Chenga41ee292008-09-03 06:44:39 +0000933}
934
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000935static unsigned X86ChooseCmpOpcode(EVT VT, const X86Subtarget *Subtarget) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000936 bool HasAVX = Subtarget->hasAVX();
Craig Topperb0c0f722012-01-10 06:54:16 +0000937 bool X86ScalarSSEf32 = Subtarget->hasSSE1();
938 bool X86ScalarSSEf64 = Subtarget->hasSSE2();
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000939
Owen Anderson9f944592009-08-11 20:47:22 +0000940 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner74e01282008-10-15 04:32:45 +0000941 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000942 case MVT::i8: return X86::CMP8rr;
943 case MVT::i16: return X86::CMP16rr;
944 case MVT::i32: return X86::CMP32rr;
945 case MVT::i64: return X86::CMP64rr;
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +0000946 case MVT::f32:
947 return X86ScalarSSEf32 ? (HasAVX ? X86::VUCOMISSrr : X86::UCOMISSrr) : 0;
948 case MVT::f64:
949 return X86ScalarSSEf64 ? (HasAVX ? X86::VUCOMISDrr : X86::UCOMISDrr) : 0;
Dan Gohman1ab1d312008-10-02 22:15:21 +0000950 }
Dan Gohman1ab1d312008-10-02 22:15:21 +0000951}
952
Chris Lattner88f47542008-10-15 04:13:29 +0000953/// X86ChooseCmpImmediateOpcode - If we have a comparison with RHS as the RHS
954/// of the comparison, return an opcode that works for the compare (e.g.
955/// CMP32ri) otherwise return 0.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000956static unsigned X86ChooseCmpImmediateOpcode(EVT VT, const ConstantInt *RHSC) {
Owen Anderson9f944592009-08-11 20:47:22 +0000957 switch (VT.getSimpleVT().SimpleTy) {
Chris Lattner88f47542008-10-15 04:13:29 +0000958 // Otherwise, we can't fold the immediate into this comparison.
Chris Lattner74e01282008-10-15 04:32:45 +0000959 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +0000960 case MVT::i8: return X86::CMP8ri;
961 case MVT::i16: return X86::CMP16ri;
962 case MVT::i32: return X86::CMP32ri;
963 case MVT::i64:
Chris Lattner74e01282008-10-15 04:32:45 +0000964 // 64-bit comparisons are only valid if the immediate fits in a 32-bit sext
965 // field.
Chris Lattner3ba29352008-10-15 05:30:52 +0000966 if ((int)RHSC->getSExtValue() == RHSC->getSExtValue())
Chris Lattner74e01282008-10-15 04:32:45 +0000967 return X86::CMP64ri32;
968 return 0;
969 }
Chris Lattner88f47542008-10-15 04:13:29 +0000970}
971
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000972bool X86FastISel::X86FastEmitCompare(const Value *Op0, const Value *Op1,
973 EVT VT) {
Chris Lattnerd46b9512008-10-15 04:26:38 +0000974 unsigned Op0Reg = getRegForValue(Op0);
975 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000976
Chris Lattnere388725a2008-10-15 05:18:04 +0000977 // Handle 'null' like i32/i64 0.
Chandler Carruth7ec50852012-11-01 08:07:29 +0000978 if (isa<ConstantPointerNull>(Op1))
Rafael Espindolaea09c592014-02-18 22:05:46 +0000979 Op1 = Constant::getNullValue(DL.getIntPtrType(Op0->getContext()));
Wesley Peck527da1b2010-11-23 03:31:01 +0000980
Chris Lattnerd46b9512008-10-15 04:26:38 +0000981 // We have two options: compare with register or immediate. If the RHS of
982 // the compare is an immediate that we can fold into this compare, use
983 // CMPri, otherwise use CMPrr.
Dan Gohmanbcaf6812010-04-15 01:51:59 +0000984 if (const ConstantInt *Op1C = dyn_cast<ConstantInt>(Op1)) {
Chris Lattner74e01282008-10-15 04:32:45 +0000985 if (unsigned CompareImmOpc = X86ChooseCmpImmediateOpcode(VT, Op1C)) {
Rafael Espindolaea09c592014-02-18 22:05:46 +0000986 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareImmOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000987 .addReg(Op0Reg)
988 .addImm(Op1C->getSExtValue());
Chris Lattnerd46b9512008-10-15 04:26:38 +0000989 return true;
990 }
991 }
Wesley Peck527da1b2010-11-23 03:31:01 +0000992
Jakob Stoklund Olesen48068482010-07-11 16:22:13 +0000993 unsigned CompareOpc = X86ChooseCmpOpcode(VT, Subtarget);
Chris Lattnerd46b9512008-10-15 04:26:38 +0000994 if (CompareOpc == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +0000995
Chris Lattnerd46b9512008-10-15 04:26:38 +0000996 unsigned Op1Reg = getRegForValue(Op1);
997 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +0000998 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CompareOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +0000999 .addReg(Op0Reg)
1000 .addReg(Op1Reg);
Wesley Peck527da1b2010-11-23 03:31:01 +00001001
Chris Lattnerd46b9512008-10-15 04:26:38 +00001002 return true;
1003}
1004
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001005bool X86FastISel::X86SelectCmp(const Instruction *I) {
1006 const CmpInst *CI = cast<CmpInst>(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001007
Duncan Sandsf5dda012010-11-03 11:35:31 +00001008 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00001009 if (!isTypeLegal(I->getOperand(0)->getType(), VT))
Dan Gohman09faf812008-09-05 01:33:56 +00001010 return false;
1011
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001012 unsigned ResultReg = createResultReg(&X86::GR8RegClass);
Chris Lattnera3596db2008-10-15 03:47:17 +00001013 unsigned SetCCOpc;
Chris Lattnerf32ce222008-10-15 03:52:54 +00001014 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001015 switch (CI->getPredicate()) {
1016 case CmpInst::FCMP_OEQ: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001017 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
1018 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001019
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001020 unsigned EReg = createResultReg(&X86::GR8RegClass);
1021 unsigned NPReg = createResultReg(&X86::GR8RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001022 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SETEr), EReg);
1023 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001024 TII.get(X86::SETNPr), NPReg);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001025 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dale Johannesen9bba9022009-02-13 02:33:27 +00001026 TII.get(X86::AND8rr), ResultReg).addReg(NPReg).addReg(EReg);
Chris Lattnera3596db2008-10-15 03:47:17 +00001027 UpdateValueMap(I, ResultReg);
1028 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001029 }
1030 case CmpInst::FCMP_UNE: {
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001031 if (!X86FastEmitCompare(CI->getOperand(0), CI->getOperand(1), VT))
1032 return false;
1033
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001034 unsigned NEReg = createResultReg(&X86::GR8RegClass);
1035 unsigned PReg = createResultReg(&X86::GR8RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001036 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SETNEr), NEReg);
1037 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SETPr), PReg);
1038 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::OR8rr),ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001039 .addReg(PReg).addReg(NEReg);
Chris Lattnera3596db2008-10-15 03:47:17 +00001040 UpdateValueMap(I, ResultReg);
1041 return true;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001042 }
Chris Lattnerf32ce222008-10-15 03:52:54 +00001043 case CmpInst::FCMP_OGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1044 case CmpInst::FCMP_OGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1045 case CmpInst::FCMP_OLT: SwapArgs = true; SetCCOpc = X86::SETAr; break;
1046 case CmpInst::FCMP_OLE: SwapArgs = true; SetCCOpc = X86::SETAEr; break;
1047 case CmpInst::FCMP_ONE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1048 case CmpInst::FCMP_ORD: SwapArgs = false; SetCCOpc = X86::SETNPr; break;
1049 case CmpInst::FCMP_UNO: SwapArgs = false; SetCCOpc = X86::SETPr; break;
1050 case CmpInst::FCMP_UEQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1051 case CmpInst::FCMP_UGT: SwapArgs = true; SetCCOpc = X86::SETBr; break;
1052 case CmpInst::FCMP_UGE: SwapArgs = true; SetCCOpc = X86::SETBEr; break;
1053 case CmpInst::FCMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1054 case CmpInst::FCMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
Wesley Peck527da1b2010-11-23 03:31:01 +00001055
Chris Lattnerf32ce222008-10-15 03:52:54 +00001056 case CmpInst::ICMP_EQ: SwapArgs = false; SetCCOpc = X86::SETEr; break;
1057 case CmpInst::ICMP_NE: SwapArgs = false; SetCCOpc = X86::SETNEr; break;
1058 case CmpInst::ICMP_UGT: SwapArgs = false; SetCCOpc = X86::SETAr; break;
1059 case CmpInst::ICMP_UGE: SwapArgs = false; SetCCOpc = X86::SETAEr; break;
1060 case CmpInst::ICMP_ULT: SwapArgs = false; SetCCOpc = X86::SETBr; break;
1061 case CmpInst::ICMP_ULE: SwapArgs = false; SetCCOpc = X86::SETBEr; break;
1062 case CmpInst::ICMP_SGT: SwapArgs = false; SetCCOpc = X86::SETGr; break;
1063 case CmpInst::ICMP_SGE: SwapArgs = false; SetCCOpc = X86::SETGEr; break;
1064 case CmpInst::ICMP_SLT: SwapArgs = false; SetCCOpc = X86::SETLr; break;
1065 case CmpInst::ICMP_SLE: SwapArgs = false; SetCCOpc = X86::SETLEr; break;
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001066 default:
1067 return false;
1068 }
1069
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001070 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001071 if (SwapArgs)
Chris Lattnerd46b9512008-10-15 04:26:38 +00001072 std::swap(Op0, Op1);
Chris Lattnerf32ce222008-10-15 03:52:54 +00001073
Chris Lattnerd46b9512008-10-15 04:26:38 +00001074 // Emit a compare of Op0/Op1.
Chris Lattnerdc1c3802008-10-15 04:29:23 +00001075 if (!X86FastEmitCompare(Op0, Op1, VT))
1076 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001077
Rafael Espindolaea09c592014-02-18 22:05:46 +00001078 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SetCCOpc), ResultReg);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00001079 UpdateValueMap(I, ResultReg);
1080 return true;
1081}
Evan Chenga41ee292008-09-03 06:44:39 +00001082
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001083bool X86FastISel::X86SelectZExt(const Instruction *I) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001084 EVT DstVT = TLI.getValueType(I->getType());
1085 if (!TLI.isTypeLegal(DstVT))
1086 return false;
1087
1088 unsigned ResultReg = getRegForValue(I->getOperand(0));
1089 if (ResultReg == 0)
1090 return false;
1091
Tim Northover04eb4232013-05-30 10:43:18 +00001092 // Handle zero-extension from i1 to i8, which is common.
Craig Topper56710102013-08-15 02:33:50 +00001093 MVT SrcVT = TLI.getSimpleValueType(I->getOperand(0)->getType());
Tim Northover04eb4232013-05-30 10:43:18 +00001094 if (SrcVT.SimpleTy == MVT::i1) {
1095 // Set the high bits to zero.
1096 ResultReg = FastEmitZExtFromI1(MVT::i8, ResultReg, /*TODO: Kill=*/false);
1097 SrcVT = MVT::i8;
Eli Friedmanc7035512011-05-25 23:49:02 +00001098
Tim Northover04eb4232013-05-30 10:43:18 +00001099 if (ResultReg == 0)
1100 return false;
1101 }
1102
1103 if (DstVT == MVT::i64) {
1104 // Handle extension to 64-bits via sub-register shenanigans.
1105 unsigned MovInst;
1106
1107 switch (SrcVT.SimpleTy) {
1108 case MVT::i8: MovInst = X86::MOVZX32rr8; break;
1109 case MVT::i16: MovInst = X86::MOVZX32rr16; break;
1110 case MVT::i32: MovInst = X86::MOV32rr; break;
1111 default: llvm_unreachable("Unexpected zext to i64 source type");
1112 }
1113
1114 unsigned Result32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001115 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MovInst), Result32)
Tim Northover04eb4232013-05-30 10:43:18 +00001116 .addReg(ResultReg);
1117
1118 ResultReg = createResultReg(&X86::GR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001119 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::SUBREG_TO_REG),
Tim Northover04eb4232013-05-30 10:43:18 +00001120 ResultReg)
1121 .addImm(0).addReg(Result32).addImm(X86::sub_32bit);
1122 } else if (DstVT != MVT::i8) {
Eli Friedmanc7035512011-05-25 23:49:02 +00001123 ResultReg = FastEmit_r(MVT::i8, DstVT.getSimpleVT(), ISD::ZERO_EXTEND,
1124 ResultReg, /*Kill=*/true);
1125 if (ResultReg == 0)
1126 return false;
Dan Gohmana5753b32008-09-05 01:06:14 +00001127 }
1128
Eli Friedmanc7035512011-05-25 23:49:02 +00001129 UpdateValueMap(I, ResultReg);
1130 return true;
Dan Gohmana5753b32008-09-05 01:06:14 +00001131}
1132
Chris Lattnerd46b9512008-10-15 04:26:38 +00001133
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001134bool X86FastISel::X86SelectBranch(const Instruction *I) {
Dan Gohmana5753b32008-09-05 01:06:14 +00001135 // Unconditional branches are selected by tablegen-generated code.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001136 // Handle a conditional branch.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001137 const BranchInst *BI = cast<BranchInst>(I);
Dan Gohman87fb4e82010-07-07 16:29:44 +00001138 MachineBasicBlock *TrueMBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1139 MachineBasicBlock *FalseMBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
Dan Gohmana5753b32008-09-05 01:06:14 +00001140
Dan Gohman42ef6692010-08-21 02:32:36 +00001141 // Fold the common case of a conditional branch with a comparison
1142 // in the same block (values defined on other blocks may not have
1143 // initialized registers).
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001144 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
Dan Gohman42ef6692010-08-21 02:32:36 +00001145 if (CI->hasOneUse() && CI->getParent() == I->getParent()) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001146 EVT VT = TLI.getValueType(CI->getOperand(0)->getType());
Dan Gohmana5753b32008-09-05 01:06:14 +00001147
Dan Gohman1ab1d312008-10-02 22:15:21 +00001148 // Try to take advantage of fallthrough opportunities.
1149 CmpInst::Predicate Predicate = CI->getPredicate();
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001150 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
Dan Gohman1ab1d312008-10-02 22:15:21 +00001151 std::swap(TrueMBB, FalseMBB);
1152 Predicate = CmpInst::getInversePredicate(Predicate);
1153 }
1154
Chris Lattner0ce717a2008-10-15 03:58:05 +00001155 bool SwapArgs; // false -> compare Op0, Op1. true -> compare Op1, Op0.
1156 unsigned BranchOpc; // Opcode to jump on, e.g. "X86::JA"
1157
Dan Gohman1ab1d312008-10-02 22:15:21 +00001158 switch (Predicate) {
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001159 case CmpInst::FCMP_OEQ:
1160 std::swap(TrueMBB, FalseMBB);
1161 Predicate = CmpInst::FCMP_UNE;
1162 // FALL THROUGH
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001163 case CmpInst::FCMP_UNE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1164 case CmpInst::FCMP_OGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1165 case CmpInst::FCMP_OGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1166 case CmpInst::FCMP_OLT: SwapArgs = true; BranchOpc = X86::JA_4; break;
1167 case CmpInst::FCMP_OLE: SwapArgs = true; BranchOpc = X86::JAE_4; break;
1168 case CmpInst::FCMP_ONE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1169 case CmpInst::FCMP_ORD: SwapArgs = false; BranchOpc = X86::JNP_4; break;
1170 case CmpInst::FCMP_UNO: SwapArgs = false; BranchOpc = X86::JP_4; break;
1171 case CmpInst::FCMP_UEQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1172 case CmpInst::FCMP_UGT: SwapArgs = true; BranchOpc = X86::JB_4; break;
1173 case CmpInst::FCMP_UGE: SwapArgs = true; BranchOpc = X86::JBE_4; break;
1174 case CmpInst::FCMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1175 case CmpInst::FCMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
Wesley Peck527da1b2010-11-23 03:31:01 +00001176
Chris Lattner2b0a7a22010-02-11 19:25:55 +00001177 case CmpInst::ICMP_EQ: SwapArgs = false; BranchOpc = X86::JE_4; break;
1178 case CmpInst::ICMP_NE: SwapArgs = false; BranchOpc = X86::JNE_4; break;
1179 case CmpInst::ICMP_UGT: SwapArgs = false; BranchOpc = X86::JA_4; break;
1180 case CmpInst::ICMP_UGE: SwapArgs = false; BranchOpc = X86::JAE_4; break;
1181 case CmpInst::ICMP_ULT: SwapArgs = false; BranchOpc = X86::JB_4; break;
1182 case CmpInst::ICMP_ULE: SwapArgs = false; BranchOpc = X86::JBE_4; break;
1183 case CmpInst::ICMP_SGT: SwapArgs = false; BranchOpc = X86::JG_4; break;
1184 case CmpInst::ICMP_SGE: SwapArgs = false; BranchOpc = X86::JGE_4; break;
1185 case CmpInst::ICMP_SLT: SwapArgs = false; BranchOpc = X86::JL_4; break;
1186 case CmpInst::ICMP_SLE: SwapArgs = false; BranchOpc = X86::JLE_4; break;
Dan Gohman1ab1d312008-10-02 22:15:21 +00001187 default:
1188 return false;
1189 }
Wesley Peck527da1b2010-11-23 03:31:01 +00001190
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001191 const Value *Op0 = CI->getOperand(0), *Op1 = CI->getOperand(1);
Chris Lattner47bef252008-10-15 04:02:26 +00001192 if (SwapArgs)
1193 std::swap(Op0, Op1);
1194
Chris Lattnerd46b9512008-10-15 04:26:38 +00001195 // Emit a compare of the LHS and RHS, setting the flags.
1196 if (!X86FastEmitCompare(Op0, Op1, VT))
1197 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001198
Rafael Espindolaea09c592014-02-18 22:05:46 +00001199 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(BranchOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001200 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001201
1202 if (Predicate == CmpInst::FCMP_UNE) {
1203 // X86 requires a second branch to handle UNE (and OEQ,
1204 // which is mapped to UNE above).
Rafael Espindolaea09c592014-02-18 22:05:46 +00001205 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JP_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001206 .addMBB(TrueMBB);
Dan Gohman4ddf7a42008-10-21 18:24:51 +00001207 }
1208
Rafael Espindolaea09c592014-02-18 22:05:46 +00001209 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001210 uint32_t BranchWeight = 0;
1211 if (FuncInfo.BPI)
1212 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1213 TrueMBB->getBasicBlock());
1214 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Dan Gohman1ab1d312008-10-02 22:15:21 +00001215 return true;
1216 }
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001217 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1218 // Handle things like "%cond = trunc i32 %X to i1 / br i1 %cond", which
1219 // typically happen for _Bool and C++ bools.
1220 MVT SourceVT;
1221 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1222 isTypeLegal(TI->getOperand(0)->getType(), SourceVT)) {
1223 unsigned TestOpc = 0;
1224 switch (SourceVT.SimpleTy) {
1225 default: break;
1226 case MVT::i8: TestOpc = X86::TEST8ri; break;
1227 case MVT::i16: TestOpc = X86::TEST16ri; break;
1228 case MVT::i32: TestOpc = X86::TEST32ri; break;
1229 case MVT::i64: TestOpc = X86::TEST64ri32; break;
1230 }
1231 if (TestOpc) {
1232 unsigned OpReg = getRegForValue(TI->getOperand(0));
1233 if (OpReg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001234 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TestOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001235 .addReg(OpReg).addImm(1);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001236
Chris Lattnerc59290a2011-04-19 04:26:32 +00001237 unsigned JmpOpc = X86::JNE_4;
1238 if (FuncInfo.MBB->isLayoutSuccessor(TrueMBB)) {
1239 std::swap(TrueMBB, FalseMBB);
1240 JmpOpc = X86::JE_4;
1241 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001242
Rafael Espindolaea09c592014-02-18 22:05:46 +00001243 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(JmpOpc))
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001244 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001245 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001246 uint32_t BranchWeight = 0;
1247 if (FuncInfo.BPI)
1248 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1249 TrueMBB->getBasicBlock());
1250 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Chris Lattner2c8a4c32011-04-19 04:22:17 +00001251 return true;
1252 }
1253 }
Dan Gohman1ab1d312008-10-02 22:15:21 +00001254 }
1255
1256 // Otherwise do a clumsy setcc and re-test it.
Eli Friedman0eea0292011-04-27 01:34:27 +00001257 // Note that i1 essentially gets ANY_EXTEND'ed to i8 where it isn't used
1258 // in an explicit cast, so make sure to handle that correctly.
Dan Gohman1ab1d312008-10-02 22:15:21 +00001259 unsigned OpReg = getRegForValue(BI->getCondition());
1260 if (OpReg == 0) return false;
1261
Rafael Espindolaea09c592014-02-18 22:05:46 +00001262 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Eli Friedman0eea0292011-04-27 01:34:27 +00001263 .addReg(OpReg).addImm(1);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001264 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::JNE_4))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001265 .addMBB(TrueMBB);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001266 FastEmitBranch(FalseMBB, DbgLoc);
Juergen Ributzka454d3742014-06-13 00:45:11 +00001267 uint32_t BranchWeight = 0;
1268 if (FuncInfo.BPI)
1269 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1270 TrueMBB->getBasicBlock());
1271 FuncInfo.MBB->addSuccessor(TrueMBB, BranchWeight);
Dan Gohmana5753b32008-09-05 01:06:14 +00001272 return true;
1273}
1274
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001275bool X86FastISel::X86SelectShift(const Instruction *I) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001276 unsigned CReg = 0, OpReg = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001277 const TargetRegisterClass *RC = nullptr;
Duncan Sands9dff9be2010-02-15 16:12:20 +00001278 if (I->getType()->isIntegerTy(8)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001279 CReg = X86::CL;
1280 RC = &X86::GR8RegClass;
1281 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001282 case Instruction::LShr: OpReg = X86::SHR8rCL; break;
1283 case Instruction::AShr: OpReg = X86::SAR8rCL; break;
1284 case Instruction::Shl: OpReg = X86::SHL8rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001285 default: return false;
1286 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001287 } else if (I->getType()->isIntegerTy(16)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001288 CReg = X86::CX;
1289 RC = &X86::GR16RegClass;
1290 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001291 case Instruction::LShr: OpReg = X86::SHR16rCL; break;
1292 case Instruction::AShr: OpReg = X86::SAR16rCL; break;
1293 case Instruction::Shl: OpReg = X86::SHL16rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001294 default: return false;
1295 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001296 } else if (I->getType()->isIntegerTy(32)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001297 CReg = X86::ECX;
1298 RC = &X86::GR32RegClass;
1299 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001300 case Instruction::LShr: OpReg = X86::SHR32rCL; break;
1301 case Instruction::AShr: OpReg = X86::SAR32rCL; break;
1302 case Instruction::Shl: OpReg = X86::SHL32rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001303 default: return false;
1304 }
Duncan Sands9dff9be2010-02-15 16:12:20 +00001305 } else if (I->getType()->isIntegerTy(64)) {
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001306 CReg = X86::RCX;
1307 RC = &X86::GR64RegClass;
1308 switch (I->getOpcode()) {
Chris Lattnerb53ccb82011-04-17 20:23:29 +00001309 case Instruction::LShr: OpReg = X86::SHR64rCL; break;
1310 case Instruction::AShr: OpReg = X86::SAR64rCL; break;
1311 case Instruction::Shl: OpReg = X86::SHL64rCL; break;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001312 default: return false;
1313 }
1314 } else {
1315 return false;
1316 }
1317
Duncan Sandsf5dda012010-11-03 11:35:31 +00001318 MVT VT;
1319 if (!isTypeLegal(I->getType(), VT))
Dan Gohmandb06a992008-09-05 21:27:34 +00001320 return false;
1321
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001322 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1323 if (Op0Reg == 0) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001324
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001325 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1326 if (Op1Reg == 0) return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00001327 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen3bb12672010-07-11 03:31:00 +00001328 CReg).addReg(Op1Reg);
Dan Gohmand3917152008-10-07 21:50:36 +00001329
1330 // The shift instruction uses X86::CL. If we defined a super-register
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001331 // of X86::CL, emit a subreg KILL to precisely describe what we're doing here.
Dan Gohmand3917152008-10-07 21:50:36 +00001332 if (CReg != X86::CL)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001333 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001334 TII.get(TargetOpcode::KILL), X86::CL)
Jakob Stoklund Olesen00264622010-07-08 16:40:22 +00001335 .addReg(CReg, RegState::Kill);
Dan Gohmand3917152008-10-07 21:50:36 +00001336
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001337 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001338 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(OpReg), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001339 .addReg(Op0Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001340 UpdateValueMap(I, ResultReg);
1341 return true;
1342}
1343
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001344bool X86FastISel::X86SelectDivRem(const Instruction *I) {
1345 const static unsigned NumTypes = 4; // i8, i16, i32, i64
1346 const static unsigned NumOps = 4; // SDiv, SRem, UDiv, URem
1347 const static bool S = true; // IsSigned
1348 const static bool U = false; // !IsSigned
1349 const static unsigned Copy = TargetOpcode::COPY;
1350 // For the X86 DIV/IDIV instruction, in most cases the dividend
1351 // (numerator) must be in a specific register pair highreg:lowreg,
1352 // producing the quotient in lowreg and the remainder in highreg.
1353 // For most data types, to set up the instruction, the dividend is
1354 // copied into lowreg, and lowreg is sign-extended or zero-extended
1355 // into highreg. The exception is i8, where the dividend is defined
1356 // as a single register rather than a register pair, and we
1357 // therefore directly sign-extend or zero-extend the dividend into
1358 // lowreg, instead of copying, and ignore the highreg.
1359 const static struct DivRemEntry {
1360 // The following portion depends only on the data type.
1361 const TargetRegisterClass *RC;
1362 unsigned LowInReg; // low part of the register pair
1363 unsigned HighInReg; // high part of the register pair
1364 // The following portion depends on both the data type and the operation.
1365 struct DivRemResult {
1366 unsigned OpDivRem; // The specific DIV/IDIV opcode to use.
1367 unsigned OpSignExtend; // Opcode for sign-extending lowreg into
1368 // highreg, or copying a zero into highreg.
1369 unsigned OpCopy; // Opcode for copying dividend into lowreg, or
1370 // zero/sign-extending into lowreg for i8.
1371 unsigned DivRemResultReg; // Register containing the desired result.
1372 bool IsOpSigned; // Whether to use signed or unsigned form.
1373 } ResultTable[NumOps];
1374 } OpTable[NumTypes] = {
1375 { &X86::GR8RegClass, X86::AX, 0, {
1376 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AL, S }, // SDiv
1377 { X86::IDIV8r, 0, X86::MOVSX16rr8, X86::AH, S }, // SRem
1378 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AL, U }, // UDiv
1379 { X86::DIV8r, 0, X86::MOVZX16rr8, X86::AH, U }, // URem
1380 }
1381 }, // i8
1382 { &X86::GR16RegClass, X86::AX, X86::DX, {
1383 { X86::IDIV16r, X86::CWD, Copy, X86::AX, S }, // SDiv
1384 { X86::IDIV16r, X86::CWD, Copy, X86::DX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001385 { X86::DIV16r, X86::MOV32r0, Copy, X86::AX, U }, // UDiv
1386 { X86::DIV16r, X86::MOV32r0, Copy, X86::DX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001387 }
1388 }, // i16
1389 { &X86::GR32RegClass, X86::EAX, X86::EDX, {
1390 { X86::IDIV32r, X86::CDQ, Copy, X86::EAX, S }, // SDiv
1391 { X86::IDIV32r, X86::CDQ, Copy, X86::EDX, S }, // SRem
1392 { X86::DIV32r, X86::MOV32r0, Copy, X86::EAX, U }, // UDiv
1393 { X86::DIV32r, X86::MOV32r0, Copy, X86::EDX, U }, // URem
1394 }
1395 }, // i32
1396 { &X86::GR64RegClass, X86::RAX, X86::RDX, {
1397 { X86::IDIV64r, X86::CQO, Copy, X86::RAX, S }, // SDiv
1398 { X86::IDIV64r, X86::CQO, Copy, X86::RDX, S }, // SRem
Tim Northover64ec0ff2013-05-30 13:19:42 +00001399 { X86::DIV64r, X86::MOV32r0, Copy, X86::RAX, U }, // UDiv
1400 { X86::DIV64r, X86::MOV32r0, Copy, X86::RDX, U }, // URem
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001401 }
1402 }, // i64
1403 };
1404
1405 MVT VT;
1406 if (!isTypeLegal(I->getType(), VT))
1407 return false;
1408
1409 unsigned TypeIndex, OpIndex;
1410 switch (VT.SimpleTy) {
1411 default: return false;
1412 case MVT::i8: TypeIndex = 0; break;
1413 case MVT::i16: TypeIndex = 1; break;
1414 case MVT::i32: TypeIndex = 2; break;
1415 case MVT::i64: TypeIndex = 3;
1416 if (!Subtarget->is64Bit())
1417 return false;
1418 break;
1419 }
1420
1421 switch (I->getOpcode()) {
1422 default: llvm_unreachable("Unexpected div/rem opcode");
1423 case Instruction::SDiv: OpIndex = 0; break;
1424 case Instruction::SRem: OpIndex = 1; break;
1425 case Instruction::UDiv: OpIndex = 2; break;
1426 case Instruction::URem: OpIndex = 3; break;
1427 }
1428
1429 const DivRemEntry &TypeEntry = OpTable[TypeIndex];
1430 const DivRemEntry::DivRemResult &OpEntry = TypeEntry.ResultTable[OpIndex];
1431 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1432 if (Op0Reg == 0)
1433 return false;
1434 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1435 if (Op1Reg == 0)
1436 return false;
1437
1438 // Move op0 into low-order input register.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001439 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001440 TII.get(OpEntry.OpCopy), TypeEntry.LowInReg).addReg(Op0Reg);
1441 // Zero-extend or sign-extend into high-order input register.
1442 if (OpEntry.OpSignExtend) {
1443 if (OpEntry.IsOpSigned)
Rafael Espindolaea09c592014-02-18 22:05:46 +00001444 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001445 TII.get(OpEntry.OpSignExtend));
Tim Northover64ec0ff2013-05-30 13:19:42 +00001446 else {
1447 unsigned Zero32 = createResultReg(&X86::GR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001448 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001449 TII.get(X86::MOV32r0), Zero32);
1450
1451 // Copy the zero into the appropriate sub/super/identical physical
1452 // register. Unfortunately the operations needed are not uniform enough to
1453 // fit neatly into the table above.
1454 if (VT.SimpleTy == MVT::i16) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001455 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001456 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001457 .addReg(Zero32, 0, X86::sub_16bit);
1458 } else if (VT.SimpleTy == MVT::i32) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001459 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eric Christopher8f6a0832013-06-11 23:41:41 +00001460 TII.get(Copy), TypeEntry.HighInReg)
Tim Northover64ec0ff2013-05-30 13:19:42 +00001461 .addReg(Zero32);
1462 } else if (VT.SimpleTy == MVT::i64) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001463 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Tim Northover64ec0ff2013-05-30 13:19:42 +00001464 TII.get(TargetOpcode::SUBREG_TO_REG), TypeEntry.HighInReg)
1465 .addImm(0).addReg(Zero32).addImm(X86::sub_32bit);
1466 }
1467 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001468 }
1469 // Generate the DIV/IDIV instruction.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001470 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001471 TII.get(OpEntry.OpDivRem)).addReg(Op1Reg);
Jim Grosbachc35388f2013-07-09 02:07:25 +00001472 // For i8 remainder, we can't reference AH directly, as we'll end
1473 // up with bogus copies like %R9B = COPY %AH. Reference AX
1474 // instead to prevent AH references in a REX instruction.
1475 //
1476 // The current assumption of the fast register allocator is that isel
1477 // won't generate explicit references to the GPR8_NOREX registers. If
1478 // the allocator and/or the backend get enhanced to be more robust in
1479 // that regard, this can be, and should be, removed.
1480 unsigned ResultReg = 0;
1481 if ((I->getOpcode() == Instruction::SRem ||
1482 I->getOpcode() == Instruction::URem) &&
1483 OpEntry.DivRemResultReg == X86::AH && Subtarget->is64Bit()) {
1484 unsigned SourceSuperReg = createResultReg(&X86::GR16RegClass);
1485 unsigned ResultSuperReg = createResultReg(&X86::GR16RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001486 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Jim Grosbachc35388f2013-07-09 02:07:25 +00001487 TII.get(Copy), SourceSuperReg).addReg(X86::AX);
1488
1489 // Shift AX right by 8 bits instead of using AH.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001490 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::SHR16ri),
Jim Grosbachc35388f2013-07-09 02:07:25 +00001491 ResultSuperReg).addReg(SourceSuperReg).addImm(8);
1492
1493 // Now reference the 8-bit subreg of the result.
1494 ResultReg = FastEmitInst_extractsubreg(MVT::i8, ResultSuperReg,
1495 /*Kill=*/true, X86::sub_8bit);
1496 }
1497 // Copy the result out of the physreg if we haven't already.
1498 if (!ResultReg) {
1499 ResultReg = createResultReg(TypeEntry.RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001500 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Copy), ResultReg)
Jim Grosbachc35388f2013-07-09 02:07:25 +00001501 .addReg(OpEntry.DivRemResultReg);
1502 }
Eli Bendersky24a36eb2013-04-17 20:10:13 +00001503 UpdateValueMap(I, ResultReg);
1504
1505 return true;
1506}
1507
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001508bool X86FastISel::X86SelectSelect(const Instruction *I) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00001509 MVT VT;
1510 if (!isTypeLegal(I->getType(), VT))
Chris Lattnera0f9d492008-10-15 05:07:36 +00001511 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001512
Eric Christopher0574cc52010-09-29 23:00:29 +00001513 // We only use cmov here, if we don't have a cmov instruction bail.
1514 if (!Subtarget->hasCMov()) return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00001515
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001516 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00001517 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001518 if (VT == MVT::i16) {
Dan Gohmane5560182008-09-05 21:13:04 +00001519 Opc = X86::CMOVE16rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001520 RC = &X86::GR16RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001521 } else if (VT == MVT::i32) {
Dan Gohmane5560182008-09-05 21:13:04 +00001522 Opc = X86::CMOVE32rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001523 RC = &X86::GR32RegClass;
Duncan Sandsf5dda012010-11-03 11:35:31 +00001524 } else if (VT == MVT::i64) {
Dan Gohmane5560182008-09-05 21:13:04 +00001525 Opc = X86::CMOVE64rr;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001526 RC = &X86::GR64RegClass;
1527 } else {
Wesley Peck527da1b2010-11-23 03:31:01 +00001528 return false;
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001529 }
1530
1531 unsigned Op0Reg = getRegForValue(I->getOperand(0));
1532 if (Op0Reg == 0) return false;
1533 unsigned Op1Reg = getRegForValue(I->getOperand(1));
1534 if (Op1Reg == 0) return false;
1535 unsigned Op2Reg = getRegForValue(I->getOperand(2));
1536 if (Op2Reg == 0) return false;
1537
Quentin Colombet90a646e2013-12-19 18:32:04 +00001538 // Selects operate on i1, however, Op0Reg is 8 bits width and may contain
1539 // garbage. Indeed, only the less significant bit is supposed to be accurate.
1540 // If we read more than the lsb, we may see non-zero values whereas lsb
1541 // is zero. Therefore, we have to truncate Op0Reg to i1 for the select.
Alp Tokercb402912014-01-24 17:20:08 +00001542 // This is achieved by performing TEST against 1.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001543 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TEST8ri))
Quentin Colombet90a646e2013-12-19 18:32:04 +00001544 .addReg(Op0Reg).addImm(1);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001545 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001546 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001547 .addReg(Op1Reg).addReg(Op2Reg);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00001548 UpdateValueMap(I, ResultReg);
1549 return true;
1550}
1551
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001552bool X86FastISel::X86SelectFPExt(const Instruction *I) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001553 // fpext from float to double.
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001554 if (X86ScalarSSEf64 &&
Chris Lattnerfdd87902009-10-05 05:54:46 +00001555 I->getType()->isDoubleTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001556 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001557 if (V->getType()->isFloatTy()) {
Chris Lattnera0f9d492008-10-15 05:07:36 +00001558 unsigned OpReg = getRegForValue(V);
1559 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001560 unsigned ResultReg = createResultReg(&X86::FR64RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001561 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001562 TII.get(X86::CVTSS2SDrr), ResultReg)
1563 .addReg(OpReg);
Chris Lattnera0f9d492008-10-15 05:07:36 +00001564 UpdateValueMap(I, ResultReg);
1565 return true;
Dan Gohmanbf646f22008-09-10 21:02:08 +00001566 }
1567 }
1568
1569 return false;
1570}
1571
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001572bool X86FastISel::X86SelectFPTrunc(const Instruction *I) {
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00001573 if (X86ScalarSSEf64) {
Chris Lattnerfdd87902009-10-05 05:54:46 +00001574 if (I->getType()->isFloatTy()) {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001575 const Value *V = I->getOperand(0);
Chris Lattnerfdd87902009-10-05 05:54:46 +00001576 if (V->getType()->isDoubleTy()) {
Dan Gohmanbf646f22008-09-10 21:02:08 +00001577 unsigned OpReg = getRegForValue(V);
1578 if (OpReg == 0) return false;
Craig Topperabadc662012-04-20 06:31:50 +00001579 unsigned ResultReg = createResultReg(&X86::FR32RegClass);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001580 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001581 TII.get(X86::CVTSD2SSrr), ResultReg)
1582 .addReg(OpReg);
Dan Gohmanbf646f22008-09-10 21:02:08 +00001583 UpdateValueMap(I, ResultReg);
1584 return true;
1585 }
1586 }
1587 }
1588
1589 return false;
1590}
1591
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001592bool X86FastISel::X86SelectTrunc(const Instruction *I) {
Owen Anderson53aa7a92009-08-10 22:56:29 +00001593 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
1594 EVT DstVT = TLI.getValueType(I->getType());
Wesley Peck527da1b2010-11-23 03:31:01 +00001595
Eli Friedmanc7035512011-05-25 23:49:02 +00001596 // This code only handles truncation to byte.
Owen Anderson9f944592009-08-11 20:47:22 +00001597 if (DstVT != MVT::i8 && DstVT != MVT::i1)
Evan Chengb9286692008-09-07 08:47:42 +00001598 return false;
Eli Friedmanc7035512011-05-25 23:49:02 +00001599 if (!TLI.isTypeLegal(SrcVT))
Evan Chengb9286692008-09-07 08:47:42 +00001600 return false;
1601
1602 unsigned InputReg = getRegForValue(I->getOperand(0));
1603 if (!InputReg)
1604 // Unhandled operand. Halt "fast" selection and bail.
1605 return false;
1606
Eli Friedmanc7035512011-05-25 23:49:02 +00001607 if (SrcVT == MVT::i8) {
1608 // Truncate from i8 to i1; no code needed.
1609 UpdateValueMap(I, InputReg);
1610 return true;
1611 }
Evan Chengb9286692008-09-07 08:47:42 +00001612
Eli Friedmanc7035512011-05-25 23:49:02 +00001613 if (!Subtarget->is64Bit()) {
1614 // If we're on x86-32; we can't extract an i8 from a general register.
1615 // First issue a copy to GR16_ABCD or GR32_ABCD.
Craig Topperabadc662012-04-20 06:31:50 +00001616 const TargetRegisterClass *CopyRC = (SrcVT == MVT::i16) ?
1617 (const TargetRegisterClass*)&X86::GR16_ABCDRegClass :
1618 (const TargetRegisterClass*)&X86::GR32_ABCDRegClass;
Eli Friedmanc7035512011-05-25 23:49:02 +00001619 unsigned CopyReg = createResultReg(CopyRC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00001620 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
Eli Friedmanc7035512011-05-25 23:49:02 +00001621 CopyReg).addReg(InputReg);
1622 InputReg = CopyReg;
1623 }
1624
1625 // Issue an extract_subreg.
Owen Anderson9f944592009-08-11 20:47:22 +00001626 unsigned ResultReg = FastEmitInst_extractsubreg(MVT::i8,
Eli Friedmanc7035512011-05-25 23:49:02 +00001627 InputReg, /*Kill=*/true,
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00001628 X86::sub_8bit);
Evan Chengb9286692008-09-07 08:47:42 +00001629 if (!ResultReg)
1630 return false;
1631
1632 UpdateValueMap(I, ResultReg);
1633 return true;
1634}
1635
Eli Friedman60afcc22011-05-20 22:21:04 +00001636bool X86FastISel::IsMemcpySmall(uint64_t Len) {
1637 return Len <= (Subtarget->is64Bit() ? 32 : 16);
1638}
1639
Eli Friedmanbcc69142011-04-27 01:45:07 +00001640bool X86FastISel::TryEmitSmallMemcpy(X86AddressMode DestAM,
1641 X86AddressMode SrcAM, uint64_t Len) {
Eli Friedman60afcc22011-05-20 22:21:04 +00001642
Eli Friedmanbcc69142011-04-27 01:45:07 +00001643 // Make sure we don't bloat code by inlining very large memcpy's.
Eli Friedman60afcc22011-05-20 22:21:04 +00001644 if (!IsMemcpySmall(Len))
1645 return false;
1646
1647 bool i64Legal = Subtarget->is64Bit();
Eli Friedmanbcc69142011-04-27 01:45:07 +00001648
1649 // We don't care about alignment here since we just emit integer accesses.
1650 while (Len) {
1651 MVT VT;
1652 if (Len >= 8 && i64Legal)
1653 VT = MVT::i64;
1654 else if (Len >= 4)
1655 VT = MVT::i32;
1656 else if (Len >= 2)
1657 VT = MVT::i16;
1658 else {
Eli Friedmanbcc69142011-04-27 01:45:07 +00001659 VT = MVT::i8;
1660 }
1661
1662 unsigned Reg;
Juergen Ributzka349777d2014-06-12 23:27:57 +00001663 bool RV = X86FastEmitLoad(VT, SrcAM, nullptr, Reg);
1664 RV &= X86FastEmitStore(VT, Reg, /*Kill=*/true, DestAM);
Eli Friedmanbcc69142011-04-27 01:45:07 +00001665 assert(RV && "Failed to emit load or store??");
1666
1667 unsigned Size = VT.getSizeInBits()/8;
1668 Len -= Size;
1669 DestAM.Disp += Size;
1670 SrcAM.Disp += Size;
1671 }
1672
1673 return true;
1674}
1675
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001676static bool isCommutativeIntrinsic(IntrinsicInst const &I) {
1677 switch (I.getIntrinsicID()) {
1678 case Intrinsic::sadd_with_overflow:
1679 case Intrinsic::uadd_with_overflow:
1680 case Intrinsic::smul_with_overflow:
1681 case Intrinsic::umul_with_overflow:
1682 return true;
1683 default:
1684 return false;
1685 }
1686}
1687
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001688bool X86FastISel::X86VisitIntrinsicCall(const IntrinsicInst &I) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001689 // FIXME: Handle more intrinsics.
Chris Lattner99a8cb62009-04-12 07:36:01 +00001690 switch (I.getIntrinsicID()) {
Bill Wendling80b34b32008-12-09 02:42:50 +00001691 default: return false;
Juergen Ributzka4dc95872014-06-11 21:44:44 +00001692 case Intrinsic::frameaddress: {
1693 Type *RetTy = I.getCalledFunction()->getReturnType();
1694
1695 MVT VT;
1696 if (!isTypeLegal(RetTy, VT))
1697 return false;
1698
1699 unsigned Opc;
1700 const TargetRegisterClass *RC = nullptr;
1701
1702 switch (VT.SimpleTy) {
1703 default: llvm_unreachable("Invalid result type for frameaddress.");
1704 case MVT::i32: Opc = X86::MOV32rm; RC = &X86::GR32RegClass; break;
1705 case MVT::i64: Opc = X86::MOV64rm; RC = &X86::GR64RegClass; break;
1706 }
1707
1708 // This needs to be set before we call getFrameRegister, otherwise we get
1709 // the wrong frame register.
1710 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
1711 MFI->setFrameAddressIsTaken(true);
1712
1713 const X86RegisterInfo *RegInfo =
1714 static_cast<const X86RegisterInfo*>(TM.getRegisterInfo());
1715 unsigned FrameReg = RegInfo->getFrameRegister(*(FuncInfo.MF));
1716 assert(((FrameReg == X86::RBP && VT == MVT::i64) ||
1717 (FrameReg == X86::EBP && VT == MVT::i32)) &&
1718 "Invalid Frame Register!");
1719
1720 // Always make a copy of the frame register to to a vreg first, so that we
1721 // never directly reference the frame register (the TwoAddressInstruction-
1722 // Pass doesn't like that).
1723 unsigned SrcReg = createResultReg(RC);
1724 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1725 TII.get(TargetOpcode::COPY), SrcReg).addReg(FrameReg);
1726
1727 // Now recursively load from the frame address.
1728 // movq (%rbp), %rax
1729 // movq (%rax), %rax
1730 // movq (%rax), %rax
1731 // ...
1732 unsigned DestReg;
1733 unsigned Depth = cast<ConstantInt>(I.getOperand(0))->getZExtValue();
1734 while (Depth--) {
1735 DestReg = createResultReg(RC);
1736 addDirectMem(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1737 TII.get(Opc), DestReg), SrcReg);
1738 SrcReg = DestReg;
1739 }
1740
1741 UpdateValueMap(&I, SrcReg);
1742 return true;
1743 }
Chris Lattner91328b32011-04-19 05:52:03 +00001744 case Intrinsic::memcpy: {
1745 const MemCpyInst &MCI = cast<MemCpyInst>(I);
1746 // Don't handle volatile or variable length memcpys.
Eli Friedmancd2124a2011-06-10 23:39:36 +00001747 if (MCI.isVolatile())
Chris Lattner91328b32011-04-19 05:52:03 +00001748 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001749
Eli Friedmancd2124a2011-06-10 23:39:36 +00001750 if (isa<ConstantInt>(MCI.getLength())) {
1751 // Small memcpy's are common enough that we want to do them
1752 // without a call if possible.
1753 uint64_t Len = cast<ConstantInt>(MCI.getLength())->getZExtValue();
1754 if (IsMemcpySmall(Len)) {
1755 X86AddressMode DestAM, SrcAM;
1756 if (!X86SelectAddress(MCI.getRawDest(), DestAM) ||
1757 !X86SelectAddress(MCI.getRawSource(), SrcAM))
1758 return false;
1759 TryEmitSmallMemcpy(DestAM, SrcAM, Len);
1760 return true;
1761 }
1762 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00001763
Eli Friedmancd2124a2011-06-10 23:39:36 +00001764 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1765 if (!MCI.getLength()->getType()->isIntegerTy(SizeWidth))
Chris Lattner91328b32011-04-19 05:52:03 +00001766 return false;
Eli Friedmanbcc69142011-04-27 01:45:07 +00001767
Eli Friedmancd2124a2011-06-10 23:39:36 +00001768 if (MCI.getSourceAddressSpace() > 255 || MCI.getDestAddressSpace() > 255)
1769 return false;
1770
1771 return DoSelectCall(&I, "memcpy");
Chris Lattner91328b32011-04-19 05:52:03 +00001772 }
Eli Friedmancd2124a2011-06-10 23:39:36 +00001773 case Intrinsic::memset: {
1774 const MemSetInst &MSI = cast<MemSetInst>(I);
Eric Christopher0713a9d2011-06-08 23:55:35 +00001775
Nick Lewyckya530a4d2011-08-02 00:40:16 +00001776 if (MSI.isVolatile())
1777 return false;
1778
Eli Friedmancd2124a2011-06-10 23:39:36 +00001779 unsigned SizeWidth = Subtarget->is64Bit() ? 64 : 32;
1780 if (!MSI.getLength()->getType()->isIntegerTy(SizeWidth))
1781 return false;
1782
1783 if (MSI.getDestAddressSpace() > 255)
1784 return false;
1785
1786 return DoSelectCall(&I, "memset");
1787 }
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001788 case Intrinsic::stackprotector: {
Chad Rosier06e34d92012-05-11 19:43:29 +00001789 // Emit code to store the stack guard onto the stack.
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001790 EVT PtrTy = TLI.getPointerTy();
1791
Gabor Greif83205af2010-06-26 11:51:52 +00001792 const Value *Op1 = I.getArgOperand(0); // The guard's value.
1793 const AllocaInst *Slot = cast<AllocaInst>(I.getArgOperand(1));
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001794
Josh Magee22b8ba22013-12-19 03:17:11 +00001795 MFI.setStackProtectorIndex(FuncInfo.StaticAllocaMap[Slot]);
1796
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001797 // Grab the frame index.
1798 X86AddressMode AM;
1799 if (!X86SelectAddress(Slot, AM)) return false;
Eric Christopher5e95aee2010-03-18 21:58:33 +00001800 if (!X86FastEmitStore(PtrTy, Op1, AM)) return false;
Eric Christopher52ecfdf2010-03-18 20:27:26 +00001801 return true;
1802 }
Dale Johannesend5575f22010-01-26 00:09:58 +00001803 case Intrinsic::dbg_declare: {
Dan Gohmanbcaf6812010-04-15 01:51:59 +00001804 const DbgDeclareInst *DI = cast<DbgDeclareInst>(&I);
Dale Johannesend5575f22010-01-26 00:09:58 +00001805 X86AddressMode AM;
Dale Johannesenad00f032010-01-29 21:21:28 +00001806 assert(DI->getAddress() && "Null address should be checked earlier!");
Dale Johannesend5575f22010-01-26 00:09:58 +00001807 if (!X86SelectAddress(DI->getAddress(), AM))
1808 return false;
Evan Cheng6cc775f2011-06-28 19:10:37 +00001809 const MCInstrDesc &II = TII.get(TargetOpcode::DBG_VALUE);
Dale Johannesen654528e2010-02-18 18:51:15 +00001810 // FIXME may need to add RegState::Debug to any registers produced,
1811 // although ESP/EBP should be the only ones at the moment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00001812 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II), AM).
Dan Gohmand7b5ce32010-07-10 09:00:22 +00001813 addImm(0).addMetadata(DI->getVariable());
Dale Johannesend5575f22010-01-26 00:09:58 +00001814 return true;
1815 }
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001816 case Intrinsic::trap: {
Rafael Espindolaea09c592014-02-18 22:05:46 +00001817 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::TRAP));
Eric Christopher7eb6e0f2010-01-18 22:11:29 +00001818 return true;
1819 }
Juergen Ributzka272b5702014-06-11 23:11:02 +00001820 case Intrinsic::sqrt: {
1821 if (!Subtarget->hasSSE1())
1822 return false;
1823
1824 Type *RetTy = I.getCalledFunction()->getReturnType();
1825
1826 MVT VT;
1827 if (!isTypeLegal(RetTy, VT))
1828 return false;
1829
1830 // Unfortunatelly we can't use FastEmit_r, because the AVX version of FSQRT
1831 // is not generated by FastISel yet.
1832 // FIXME: Update this code once tablegen can handle it.
1833 static const unsigned SqrtOpc[2][2] = {
1834 {X86::SQRTSSr, X86::VSQRTSSr},
1835 {X86::SQRTSDr, X86::VSQRTSDr}
1836 };
1837 bool HasAVX = Subtarget->hasAVX();
1838 unsigned Opc;
1839 const TargetRegisterClass *RC;
1840 switch (VT.SimpleTy) {
1841 default: return false;
1842 case MVT::f32: Opc = SqrtOpc[0][HasAVX]; RC = &X86::FR32RegClass; break;
1843 case MVT::f64: Opc = SqrtOpc[1][HasAVX]; RC = &X86::FR64RegClass; break;
1844 }
1845
1846 const Value *SrcVal = I.getArgOperand(0);
1847 unsigned SrcReg = getRegForValue(SrcVal);
1848
1849 if (SrcReg == 0)
1850 return false;
1851
1852 unsigned ImplicitDefReg = 0;
1853 if (HasAVX) {
1854 ImplicitDefReg = createResultReg(RC);
1855 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1856 TII.get(TargetOpcode::IMPLICIT_DEF), ImplicitDefReg);
1857 }
1858
1859 unsigned ResultReg = createResultReg(RC);
1860 MachineInstrBuilder MIB;
1861 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1862 ResultReg);
1863
1864 if (ImplicitDefReg)
1865 MIB.addReg(ImplicitDefReg);
1866
1867 MIB.addReg(SrcReg);
1868
1869 UpdateValueMap(&I, ResultReg);
1870 return true;
1871 }
Bill Wendling80b34b32008-12-09 02:42:50 +00001872 case Intrinsic::sadd_with_overflow:
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001873 case Intrinsic::uadd_with_overflow:
1874 case Intrinsic::ssub_with_overflow:
1875 case Intrinsic::usub_with_overflow:
1876 case Intrinsic::smul_with_overflow:
1877 case Intrinsic::umul_with_overflow: {
1878 // This implements the basic lowering of the xalu with overflow intrinsics
1879 // into add/sub/mul folowed by either seto or setb.
Bill Wendling80b34b32008-12-09 02:42:50 +00001880 const Function *Callee = I.getCalledFunction();
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001881 auto *Ty = cast<StructType>(Callee->getReturnType());
1882 Type *RetTy = Ty->getTypeAtIndex(0U);
1883 Type *CondTy = Ty->getTypeAtIndex(1);
Bill Wendling80b34b32008-12-09 02:42:50 +00001884
Duncan Sandsf5dda012010-11-03 11:35:31 +00001885 MVT VT;
Bill Wendling80b34b32008-12-09 02:42:50 +00001886 if (!isTypeLegal(RetTy, VT))
1887 return false;
1888
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001889 if (VT < MVT::i8 || VT > MVT::i64)
Bill Wendling80b34b32008-12-09 02:42:50 +00001890 return false;
1891
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001892 const Value *LHS = I.getArgOperand(0);
1893 const Value *RHS = I.getArgOperand(1);
1894
1895 // Canonicalize immediates to the RHS.
1896 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
1897 isCommutativeIntrinsic(I))
1898 std::swap(LHS, RHS);
1899
1900 unsigned BaseOpc, CondOpc;
1901 switch (I.getIntrinsicID()) {
1902 default: llvm_unreachable("Unexpected intrinsic!");
1903 case Intrinsic::sadd_with_overflow:
1904 BaseOpc = ISD::ADD; CondOpc = X86::SETOr; break;
1905 case Intrinsic::uadd_with_overflow:
1906 BaseOpc = ISD::ADD; CondOpc = X86::SETBr; break;
1907 case Intrinsic::ssub_with_overflow:
1908 BaseOpc = ISD::SUB; CondOpc = X86::SETOr; break;
1909 case Intrinsic::usub_with_overflow:
1910 BaseOpc = ISD::SUB; CondOpc = X86::SETBr; break;
1911 case Intrinsic::smul_with_overflow:
1912 BaseOpc = ISD::MUL; CondOpc = X86::SETOr; break;
1913 case Intrinsic::umul_with_overflow:
1914 BaseOpc = X86ISD::UMUL; CondOpc = X86::SETOr; break;
1915 }
1916
1917 unsigned LHSReg = getRegForValue(LHS);
1918 if (LHSReg == 0)
1919 return false;
1920 bool LHSIsKill = hasTrivialKill(LHS);
1921
1922 unsigned ResultReg = 0;
1923 // Check if we have an immediate version.
1924 if (auto const *C = dyn_cast<ConstantInt>(RHS)) {
1925 ResultReg = FastEmit_ri(VT, VT, BaseOpc, LHSReg, LHSIsKill,
1926 C->getZExtValue());
1927 }
1928
1929 unsigned RHSReg;
1930 bool RHSIsKill;
1931 if (!ResultReg) {
1932 RHSReg = getRegForValue(RHS);
1933 if (RHSReg == 0)
1934 return false;
1935 RHSIsKill = hasTrivialKill(RHS);
1936 ResultReg = FastEmit_rr(VT, VT, BaseOpc, LHSReg, LHSIsKill, RHSReg,
1937 RHSIsKill);
1938 }
1939
1940 // FastISel doesn't have a pattern for X86::MUL*r. Emit it manually.
1941 if (BaseOpc == X86ISD::UMUL && !ResultReg) {
1942 static const unsigned MULOpc[] =
1943 { X86::MUL8r, X86::MUL16r, X86::MUL32r, X86::MUL64r };
1944 static const unsigned Reg[] = { X86::AL, X86::AX, X86::EAX, X86::RAX };
1945 // First copy the first operand into RAX, which is an implicit input to
1946 // the X86::MUL*r instruction.
1947 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1948 TII.get(TargetOpcode::COPY), Reg[VT.SimpleTy-MVT::i8])
1949 .addReg(LHSReg, getKillRegState(LHSIsKill));
1950 ResultReg = FastEmitInst_r(MULOpc[VT.SimpleTy-MVT::i8],
1951 TLI.getRegClassFor(VT), RHSReg, RHSIsKill);
1952 }
1953
1954 if (!ResultReg)
Bill Wendling80b34b32008-12-09 02:42:50 +00001955 return false;
1956
Juergen Ributzka2dace6e2014-06-10 23:52:44 +00001957 unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy);
1958 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
1959 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CondOpc),
1960 ResultReg2);
Eli Friedmana4d4a012011-05-16 21:06:17 +00001961
1962 UpdateValueMap(&I, ResultReg, 2);
Bill Wendling80b34b32008-12-09 02:42:50 +00001963 return true;
1964 }
1965 }
1966}
1967
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001968bool X86FastISel::FastLowerArguments() {
1969 if (!FuncInfo.CanLowerReturn)
1970 return false;
1971
1972 const Function *F = FuncInfo.Fn;
1973 if (F->isVarArg())
1974 return false;
1975
1976 CallingConv::ID CC = F->getCallingConv();
1977 if (CC != CallingConv::C)
1978 return false;
Charles Davise8f297c2013-07-12 06:02:35 +00001979
1980 if (Subtarget->isCallingConvWin64(CC))
1981 return false;
1982
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001983 if (!Subtarget->is64Bit())
1984 return false;
1985
1986 // Only handle simple cases. i.e. Up to 6 i32/i64 scalar arguments.
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00001987 unsigned GPRCnt = 0;
1988 unsigned FPRCnt = 0;
1989 unsigned Idx = 0;
1990 for (auto const &Arg : F->args()) {
1991 // The first argument is at index 1.
1992 ++Idx;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00001993 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1994 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1995 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1996 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1997 return false;
1998
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00001999 Type *ArgTy = Arg.getType();
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002000 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
2001 return false;
2002
2003 EVT ArgVT = TLI.getValueType(ArgTy);
Chad Rosier1b33e8d2013-02-26 01:05:31 +00002004 if (!ArgVT.isSimple()) return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002005 switch (ArgVT.getSimpleVT().SimpleTy) {
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002006 default: return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002007 case MVT::i32:
2008 case MVT::i64:
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002009 ++GPRCnt;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002010 break;
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002011 case MVT::f32:
2012 case MVT::f64:
2013 if (!Subtarget->hasSSE1())
2014 return false;
2015 ++FPRCnt;
2016 break;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002017 }
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002018
2019 if (GPRCnt > 6)
2020 return false;
2021
2022 if (FPRCnt > 8)
2023 return false;
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002024 }
2025
Craig Topper840beec2014-04-04 05:16:06 +00002026 static const MCPhysReg GPR32ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002027 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
2028 };
Craig Topper840beec2014-04-04 05:16:06 +00002029 static const MCPhysReg GPR64ArgRegs[] = {
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002030 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8 , X86::R9
2031 };
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002032 static const MCPhysReg XMMArgRegs[] = {
2033 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2034 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2035 };
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002036
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002037 unsigned GPRIdx = 0;
2038 unsigned FPRIdx = 0;
2039 for (auto const &Arg : F->args()) {
2040 MVT VT = TLI.getSimpleValueType(Arg.getType());
2041 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
2042 unsigned SrcReg;
2043 switch (VT.SimpleTy) {
2044 default: llvm_unreachable("Unexpected value type.");
2045 case MVT::i32: SrcReg = GPR32ArgRegs[GPRIdx++]; break;
2046 case MVT::i64: SrcReg = GPR64ArgRegs[GPRIdx++]; break;
2047 case MVT::f32: // fall-through
2048 case MVT::f64: SrcReg = XMMArgRegs[FPRIdx++]; break;
2049 }
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002050 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2051 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2052 // Without this, EmitLiveInCopies may eliminate the livein if its only
2053 // use is a bitcast (which isn't turned into an instruction).
2054 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002055 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzkaa13cab52014-06-12 20:12:34 +00002056 TII.get(TargetOpcode::COPY), ResultReg)
2057 .addReg(DstReg, getKillRegState(true));
2058 UpdateValueMap(&Arg, ResultReg);
Chad Rosiera92ef4b2013-02-25 21:59:35 +00002059 }
2060 return true;
2061}
2062
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002063bool X86FastISel::X86SelectCall(const Instruction *I) {
2064 const CallInst *CI = cast<CallInst>(I);
Gabor Greif83205af2010-06-26 11:51:52 +00002065 const Value *Callee = CI->getCalledValue();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002066
2067 // Can't handle inline asm yet.
2068 if (isa<InlineAsm>(Callee))
2069 return false;
2070
Bill Wendling80b34b32008-12-09 02:42:50 +00002071 // Handle intrinsic calls.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002072 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI))
Chris Lattner99a8cb62009-04-12 07:36:01 +00002073 return X86VisitIntrinsicCall(*II);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002074
Chad Rosierdf42cf32012-12-11 00:18:02 +00002075 // Allow SelectionDAG isel to handle tail calls.
2076 if (cast<CallInst>(I)->isTailCall())
2077 return false;
2078
Craig Topper062a2ba2014-04-25 05:30:21 +00002079 return DoSelectCall(I, nullptr);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002080}
2081
Rafael Espindola73173c52012-07-25 15:42:45 +00002082static unsigned computeBytesPoppedByCallee(const X86Subtarget &Subtarget,
2083 const ImmutableCallSite &CS) {
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002084 if (Subtarget.is64Bit())
2085 return 0;
Rafael Espindola32cb5ac2013-12-12 16:06:58 +00002086 if (Subtarget.getTargetTriple().isOSMSVCRT())
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002087 return 0;
2088 CallingConv::ID CC = CS.getCallingConv();
2089 if (CC == CallingConv::Fast || CC == CallingConv::GHC)
2090 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002091 if (!CS.paramHasAttr(1, Attribute::StructRet))
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002092 return 0;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002093 if (CS.paramHasAttr(1, Attribute::InReg))
Rafael Espindola11c38b92012-07-25 13:41:10 +00002094 return 0;
Rafael Espindola2caee7f2012-07-25 13:35:45 +00002095 return 4;
2096}
2097
Eli Friedmancd2124a2011-06-10 23:39:36 +00002098// Select either a call, or an llvm.memcpy/memmove/memset intrinsic
2099bool X86FastISel::DoSelectCall(const Instruction *I, const char *MemIntName) {
2100 const CallInst *CI = cast<CallInst>(I);
2101 const Value *Callee = CI->getCalledValue();
2102
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002103 // Handle only C and fastcc calling conventions for now.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002104 ImmutableCallSite CS(CI);
Sandeep Patel68c5f472009-09-02 08:44:58 +00002105 CallingConv::ID CC = CS.getCallingConv();
Charles Davise8f297c2013-07-12 06:02:35 +00002106 bool isWin64 = Subtarget->isCallingConvWin64(CC);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002107 if (CC != CallingConv::C && CC != CallingConv::Fast &&
Charles Davise8f297c2013-07-12 06:02:35 +00002108 CC != CallingConv::X86_FastCall && CC != CallingConv::X86_64_Win64 &&
2109 CC != CallingConv::X86_64_SysV)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002110 return false;
2111
Evan Chengd10089a2010-01-27 00:00:57 +00002112 // fastcc with -tailcallopt is intended to provide a guaranteed
2113 // tail call optimization. Fastisel doesn't know how to do that.
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002114 if (CC == CallingConv::Fast && TM.Options.GuaranteedTailCallOpt)
Evan Chengd10089a2010-01-27 00:00:57 +00002115 return false;
2116
Chris Lattner229907c2011-07-18 04:54:35 +00002117 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
2118 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002119 bool isVarArg = FTy->isVarArg();
2120
2121 // Don't know how to handle Win64 varargs yet. Nothing special needed for
2122 // x86-32. Special handling for x86-64 is implemented.
Charles Davise8f297c2013-07-12 06:02:35 +00002123 if (isVarArg && isWin64)
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002124 return false;
2125
Reid Klecknerf5b76512014-01-31 23:50:57 +00002126 // Don't know about inalloca yet.
2127 if (CS.hasInAllocaArgument())
2128 return false;
2129
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002130 // Fast-isel doesn't know about callee-pop yet.
Evan Cheng3a0c5e52011-06-23 17:54:54 +00002131 if (X86::isCalleePop(CC, Subtarget->is64Bit(), isVarArg,
Nick Lewycky50f02cb2011-12-02 22:16:29 +00002132 TM.Options.GuaranteedTailCallOpt))
Dan Gohmandc53f1c2010-05-27 18:43:40 +00002133 return false;
2134
Eli Friedman7b279422011-05-17 18:29:03 +00002135 // Check whether the function can return without sret-demotion.
2136 SmallVector<ISD::OutputArg, 4> Outs;
Bill Wendling74dba872012-12-30 13:01:51 +00002137 GetReturnInfo(I->getType(), CS.getAttributes(), Outs, TLI);
Eli Friedman7b279422011-05-17 18:29:03 +00002138 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Bill Wendlingea6397f2012-07-19 00:11:40 +00002139 *FuncInfo.MF, FTy->isVarArg(),
2140 Outs, FTy->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002141 if (!CanLowerReturn)
Eli Friedman7335e8a2011-05-17 02:36:59 +00002142 return false;
2143
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002144 // Materialize callee address in a register. FIXME: GV address can be
2145 // handled with a CALLpcrel32 instead.
Dan Gohman9801ba42008-09-19 22:16:54 +00002146 X86AddressMode CalleeAM;
Chris Lattner8212d372009-07-10 05:33:42 +00002147 if (!X86SelectCallAddress(Callee, CalleeAM))
Dan Gohman9801ba42008-09-19 22:16:54 +00002148 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002149 unsigned CalleeOp = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002150 const GlobalValue *GV = nullptr;
2151 if (CalleeAM.GV != nullptr) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002152 GV = CalleeAM.GV;
Chris Lattnerd17366a2009-06-27 04:50:14 +00002153 } else if (CalleeAM.Base.Reg != 0) {
2154 CalleeOp = CalleeAM.Base.Reg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002155 } else
2156 return false;
Dan Gohmanaf13bf12008-09-17 21:18:49 +00002157
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002158 // Deal with call operands first.
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002159 SmallVector<const Value *, 8> ArgVals;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002160 SmallVector<unsigned, 8> Args;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002161 SmallVector<MVT, 8> ArgVTs;
Chris Lattnerddb17ce2008-10-15 05:38:32 +00002162 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
Chad Rosierf0687632012-02-15 00:36:26 +00002163 unsigned arg_size = CS.arg_size();
2164 Args.reserve(arg_size);
2165 ArgVals.reserve(arg_size);
2166 ArgVTs.reserve(arg_size);
2167 ArgFlags.reserve(arg_size);
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002168 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002169 i != e; ++i) {
Eli Friedmancd2124a2011-06-10 23:39:36 +00002170 // If we're lowering a mem intrinsic instead of a regular call, skip the
2171 // last two arguments, which should not passed to the underlying functions.
2172 if (MemIntName && e-i <= 2)
2173 break;
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002174 Value *ArgVal = *i;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002175 ISD::ArgFlagsTy Flags;
2176 unsigned AttrInd = i - CS.arg_begin() + 1;
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002177 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002178 Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002179 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002180 Flags.setZExt();
2181
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002182 if (CS.paramHasAttr(AttrInd, Attribute::ByVal)) {
Chris Lattner229907c2011-07-18 04:54:35 +00002183 PointerType *Ty = cast<PointerType>(ArgVal->getType());
2184 Type *ElementTy = Ty->getElementType();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002185 unsigned FrameSize = DL.getTypeAllocSize(ElementTy);
Eli Friedman60afcc22011-05-20 22:21:04 +00002186 unsigned FrameAlign = CS.getParamAlignment(AttrInd);
2187 if (!FrameAlign)
2188 FrameAlign = TLI.getByValTypeAlignment(ElementTy);
2189 Flags.setByVal();
2190 Flags.setByValSize(FrameSize);
2191 Flags.setByValAlign(FrameAlign);
2192 if (!IsMemcpySmall(FrameSize))
2193 return false;
2194 }
2195
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002196 if (CS.paramHasAttr(AttrInd, Attribute::InReg))
Eli Friedman60afcc22011-05-20 22:21:04 +00002197 Flags.setInReg();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002198 if (CS.paramHasAttr(AttrInd, Attribute::Nest))
Eli Friedman60afcc22011-05-20 22:21:04 +00002199 Flags.setNest();
2200
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002201 // If this is an i1/i8/i16 argument, promote to i32 to avoid an extra
2202 // instruction. This is safe because it is common to all fastisel supported
2203 // calling conventions on x86.
2204 if (ConstantInt *CI = dyn_cast<ConstantInt>(ArgVal)) {
2205 if (CI->getBitWidth() == 1 || CI->getBitWidth() == 8 ||
2206 CI->getBitWidth() == 16) {
2207 if (Flags.isSExt())
2208 ArgVal = ConstantExpr::getSExt(CI,Type::getInt32Ty(CI->getContext()));
2209 else
2210 ArgVal = ConstantExpr::getZExt(CI,Type::getInt32Ty(CI->getContext()));
2211 }
2212 }
Eric Christopher0713a9d2011-06-08 23:55:35 +00002213
Chris Lattner5f4b7832011-04-19 05:09:50 +00002214 unsigned ArgReg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002215
Chris Lattner34a08c22011-04-19 05:15:59 +00002216 // Passing bools around ends up doing a trunc to i1 and passing it.
2217 // Codegen this as an argument + "and 1".
Chris Lattner5f4b7832011-04-19 05:09:50 +00002218 if (ArgVal->getType()->isIntegerTy(1) && isa<TruncInst>(ArgVal) &&
2219 cast<TruncInst>(ArgVal)->getParent() == I->getParent() &&
2220 ArgVal->hasOneUse()) {
Chris Lattner5f4b7832011-04-19 05:09:50 +00002221 ArgVal = cast<TruncInst>(ArgVal)->getOperand(0);
2222 ArgReg = getRegForValue(ArgVal);
2223 if (ArgReg == 0) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002224
Chris Lattner5f4b7832011-04-19 05:09:50 +00002225 MVT ArgVT;
2226 if (!isTypeLegal(ArgVal->getType(), ArgVT)) return false;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002227
Chris Lattner5f4b7832011-04-19 05:09:50 +00002228 ArgReg = FastEmit_ri(ArgVT, ArgVT, ISD::AND, ArgReg,
2229 ArgVal->hasOneUse(), 1);
2230 } else {
2231 ArgReg = getRegForValue(ArgVal);
Chris Lattner5f4b7832011-04-19 05:09:50 +00002232 }
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002233
Chris Lattner34a08c22011-04-19 05:15:59 +00002234 if (ArgReg == 0) return false;
2235
Chris Lattner229907c2011-07-18 04:54:35 +00002236 Type *ArgTy = ArgVal->getType();
Duncan Sandsf5dda012010-11-03 11:35:31 +00002237 MVT ArgVT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002238 if (!isTypeLegal(ArgTy, ArgVT))
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002239 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002240 if (ArgVT == MVT::x86mmx)
2241 return false;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002242 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002243 Flags.setOrigAlign(OriginalAlignment);
2244
Chris Lattner5f4b7832011-04-19 05:09:50 +00002245 Args.push_back(ArgReg);
Chris Lattnerd7f7c932011-04-19 04:42:38 +00002246 ArgVals.push_back(ArgVal);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002247 ArgVTs.push_back(ArgVT);
2248 ArgFlags.push_back(Flags);
2249 }
2250
2251 // Analyze operands of the call, assigning locations to each operand.
2252 SmallVector<CCValAssign, 16> ArgLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002253 CCState CCInfo(CC, isVarArg, *FuncInfo.MF, TM, ArgLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002254 I->getParent()->getContext());
Wesley Peck527da1b2010-11-23 03:31:01 +00002255
Dan Gohman47a07242010-06-01 21:09:47 +00002256 // Allocate shadow area for Win64
Charles Davise8f297c2013-07-12 06:02:35 +00002257 if (isWin64)
Wesley Peck527da1b2010-11-23 03:31:01 +00002258 CCInfo.AllocateStack(32, 8);
Dan Gohman47a07242010-06-01 21:09:47 +00002259
Duncan Sandsfb0a48e2010-10-31 13:21:44 +00002260 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CC_X86);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002261
2262 // Get a count of how many bytes are to be pushed on the stack.
2263 unsigned NumBytes = CCInfo.getNextStackOffset();
2264
2265 // Issue CALLSEQ_START
Evan Cheng194c3dc2011-06-28 21:14:33 +00002266 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
Rafael Espindolaea09c592014-02-18 22:05:46 +00002267 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002268 .addImm(NumBytes);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002269
Chris Lattner3ba29352008-10-15 05:30:52 +00002270 // Process argument: walk the register/memloc assignments, inserting
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002271 // copies / loads.
2272 SmallVector<unsigned, 4> RegArgs;
2273 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2274 CCValAssign &VA = ArgLocs[i];
2275 unsigned Arg = Args[VA.getValNo()];
Owen Anderson53aa7a92009-08-10 22:56:29 +00002276 EVT ArgVT = ArgVTs[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002277
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002278 // Promote the value if needed.
2279 switch (VA.getLocInfo()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002280 case CCValAssign::Full: break;
Evan Cheng6500d172008-09-08 06:35:17 +00002281 case CCValAssign::SExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002282 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2283 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002284 bool Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2285 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002286 assert(Emitted && "Failed to emit a sext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002287 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002288 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002289 }
2290 case CCValAssign::ZExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002291 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2292 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002293 bool Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
2294 Arg, ArgVT, Arg);
Chris Lattner2d7df022011-01-05 22:26:52 +00002295 assert(Emitted && "Failed to emit a zext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002296 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002297 break;
Evan Cheng6500d172008-09-08 06:35:17 +00002298 }
2299 case CCValAssign::AExt: {
Eli Friedman60afcc22011-05-20 22:21:04 +00002300 assert(VA.getLocVT().isInteger() && !VA.getLocVT().isVector() &&
2301 "Unexpected extend");
Evan Cheng6500d172008-09-08 06:35:17 +00002302 bool Emitted = X86FastEmitExtend(ISD::ANY_EXTEND, VA.getLocVT(),
2303 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002304 if (!Emitted)
2305 Emitted = X86FastEmitExtend(ISD::ZERO_EXTEND, VA.getLocVT(),
Chris Lattnera0f9d492008-10-15 05:07:36 +00002306 Arg, ArgVT, Arg);
Owen Anderson41baf8b2008-09-11 02:41:37 +00002307 if (!Emitted)
2308 Emitted = X86FastEmitExtend(ISD::SIGN_EXTEND, VA.getLocVT(),
2309 Arg, ArgVT, Arg);
Wesley Peck527da1b2010-11-23 03:31:01 +00002310
Chris Lattner2d7df022011-01-05 22:26:52 +00002311 assert(Emitted && "Failed to emit a aext!"); (void)Emitted;
Evan Cheng6500d172008-09-08 06:35:17 +00002312 ArgVT = VA.getLocVT();
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002313 break;
2314 }
Dan Gohman8c795692009-08-05 05:33:42 +00002315 case CCValAssign::BCvt: {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002316 unsigned BC = FastEmit_r(ArgVT.getSimpleVT(), VA.getLocVT(),
Wesley Peck527da1b2010-11-23 03:31:01 +00002317 ISD::BITCAST, Arg, /*TODO: Kill=*/false);
Dan Gohman8c795692009-08-05 05:33:42 +00002318 assert(BC != 0 && "Failed to emit a bitcast!");
2319 Arg = BC;
2320 ArgVT = VA.getLocVT();
2321 break;
2322 }
Chad Rosier8446ede2012-07-11 19:58:38 +00002323 case CCValAssign::VExt:
2324 // VExt has not been implemented, so this should be impossible to reach
2325 // for now. However, fallback to Selection DAG isel once implemented.
2326 return false;
2327 case CCValAssign::Indirect:
2328 // FIXME: Indirect doesn't need extending, but fast-isel doesn't fully
2329 // support this.
2330 return false;
Lang Hames06234ec2014-01-14 19:56:36 +00002331 case CCValAssign::FPExt:
2332 llvm_unreachable("Unexpected loc info!");
Evan Cheng6500d172008-09-08 06:35:17 +00002333 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002334
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002335 if (VA.isRegLoc()) {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002336 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2337 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002338 RegArgs.push_back(VA.getLocReg());
2339 } else {
2340 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman39d82f92008-09-10 20:11:02 +00002341 X86AddressMode AM;
Bill Wendling8f268402013-06-07 21:00:34 +00002342 const X86RegisterInfo *RegInfo = static_cast<const X86RegisterInfo*>(
2343 getTargetMachine()->getRegisterInfo());
Michael Liao70a99c82012-11-01 03:47:50 +00002344 AM.Base.Reg = RegInfo->getStackRegister();
Dan Gohman39d82f92008-09-10 20:11:02 +00002345 AM.Disp = LocMemOffset;
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002346 const Value *ArgVal = ArgVals[VA.getValNo()];
Eli Friedman60afcc22011-05-20 22:21:04 +00002347 ISD::ArgFlagsTy Flags = ArgFlags[VA.getValNo()];
Wesley Peck527da1b2010-11-23 03:31:01 +00002348
Eli Friedman60afcc22011-05-20 22:21:04 +00002349 if (Flags.isByVal()) {
2350 X86AddressMode SrcAM;
2351 SrcAM.Base.Reg = Arg;
2352 bool Res = TryEmitSmallMemcpy(AM, SrcAM, Flags.getByValSize());
2353 assert(Res && "memcpy length already checked!"); (void)Res;
2354 } else if (isa<ConstantInt>(ArgVal) || isa<ConstantPointerNull>(ArgVal)) {
2355 // If this is a really simple value, emit this with the Value* version
Nick Lewycky064c1c02011-10-12 00:14:12 +00002356 // of X86FastEmitStore. If it isn't simple, we don't want to do this,
Eli Friedman60afcc22011-05-20 22:21:04 +00002357 // as it can cause us to reevaluate the argument.
Lang Hames7d2f7b52011-10-18 22:11:33 +00002358 if (!X86FastEmitStore(ArgVT, ArgVal, AM))
2359 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002360 } else {
Juergen Ributzka349777d2014-06-12 23:27:57 +00002361 if (!X86FastEmitStore(ArgVT, Arg, /*ValIsKill=*/false, AM))
Lang Hames7d2f7b52011-10-18 22:11:33 +00002362 return false;
Eli Friedman60afcc22011-05-20 22:21:04 +00002363 }
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002364 }
2365 }
2366
Dan Gohman3691d502008-09-25 15:24:26 +00002367 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Wesley Peck527da1b2010-11-23 03:31:01 +00002368 // GOT pointer.
Chris Lattnerfef11d62009-07-09 04:39:06 +00002369 if (Subtarget->isPICStyleGOT()) {
Dan Gohman87fb4e82010-07-07 16:29:44 +00002370 unsigned Base = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002371 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2372 TII.get(TargetOpcode::COPY), X86::EBX).addReg(Base);
Dan Gohman3691d502008-09-25 15:24:26 +00002373 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002374
Charles Davise8f297c2013-07-12 06:02:35 +00002375 if (Subtarget->is64Bit() && isVarArg && !isWin64) {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002376 // Count the number of XMM registers allocated.
Craig Topper840beec2014-04-04 05:16:06 +00002377 static const MCPhysReg XMMArgRegs[] = {
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002378 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2379 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
2380 };
2381 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002382 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(X86::MOV8ri),
Eli Friedmanee92a6b2011-04-19 17:22:22 +00002383 X86::AL).addImm(NumXMMRegs);
2384 }
2385
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002386 // Issue the call.
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002387 MachineInstrBuilder MIB;
2388 if (CalleeOp) {
2389 // Register-indirect call.
Nate Begeman68a069a2010-07-22 00:09:39 +00002390 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002391 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002392 CallOpc = X86::CALL64r;
2393 else
2394 CallOpc = X86::CALL32r;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002395 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc))
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002396 .addReg(CalleeOp);
Wesley Peck527da1b2010-11-23 03:31:01 +00002397
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002398 } else {
2399 // Direct call.
2400 assert(GV && "Not a direct call");
Nate Begeman68a069a2010-07-22 00:09:39 +00002401 unsigned CallOpc;
Jakob Stoklund Olesen97e31152012-02-16 17:56:02 +00002402 if (Subtarget->is64Bit())
Nate Begeman68a069a2010-07-22 00:09:39 +00002403 CallOpc = X86::CALL64pcrel32;
2404 else
2405 CallOpc = X86::CALLpcrel32;
Wesley Peck527da1b2010-11-23 03:31:01 +00002406
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002407 // See if we need any target-specific flags on the GV operand.
2408 unsigned char OpFlags = 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002409
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002410 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
2411 // external symbols most go through the PLT in PIC mode. If the symbol
2412 // has hidden or protected visibility, or if it is static or local, then
2413 // we don't need to use the PLT - we can directly call it.
2414 if (Subtarget->isTargetELF() &&
2415 TM.getRelocationModel() == Reloc::PIC_ &&
2416 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
2417 OpFlags = X86II::MO_PLT;
Chris Lattnere2f524f2009-07-10 20:47:30 +00002418 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002419 (GV->isDeclaration() || GV->isWeakForLinker()) &&
Daniel Dunbarcd01ed52011-04-20 00:14:25 +00002420 (!Subtarget->getTargetTriple().isMacOSX() ||
2421 Subtarget->getTargetTriple().isMacOSXVersionLT(10, 5))) {
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002422 // PC-relative references to external symbols should go through $stub,
2423 // unless we're building with the leopard linker or later, which
2424 // automatically synthesizes these stubs.
2425 OpFlags = X86II::MO_DARWIN_STUB;
2426 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002427
2428
Rafael Espindolaea09c592014-02-18 22:05:46 +00002429 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
Eli Friedmancd2124a2011-06-10 23:39:36 +00002430 if (MemIntName)
Eli Friedman1735b292011-06-11 01:55:07 +00002431 MIB.addExternalSymbol(MemIntName, OpFlags);
Eli Friedmancd2124a2011-06-10 23:39:36 +00002432 else
2433 MIB.addGlobalAddress(GV, 0, OpFlags);
Chris Lattnerc58f1fb2009-07-09 06:34:26 +00002434 }
Dan Gohman3691d502008-09-25 15:24:26 +00002435
Jakob Stoklund Olesen8a450cb2012-02-16 00:02:50 +00002436 // Add a register mask with the call-preserved registers.
2437 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2438 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
2439
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002440 // Add an implicit use GOT pointer in EBX.
2441 if (Subtarget->isPICStyleGOT())
2442 MIB.addReg(X86::EBX, RegState::Implicit);
2443
Charles Davise8f297c2013-07-12 06:02:35 +00002444 if (Subtarget->is64Bit() && isVarArg && !isWin64)
Jakob Stoklund Olesend14101e2012-07-04 23:53:27 +00002445 MIB.addReg(X86::AL, RegState::Implicit);
2446
2447 // Add implicit physical register uses to the call.
2448 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
2449 MIB.addReg(RegArgs[i], RegState::Implicit);
2450
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002451 // Issue CALLSEQ_END
Evan Cheng194c3dc2011-06-28 21:14:33 +00002452 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
Rafael Espindola73173c52012-07-25 15:42:45 +00002453 const unsigned NumBytesCallee = computeBytesPoppedByCallee(*Subtarget, CS);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002454 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
Eli Friedman7cd51012011-04-28 20:19:12 +00002455 .addImm(NumBytes).addImm(NumBytesCallee);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002456
Eli Friedman7b279422011-05-17 18:29:03 +00002457 // Build info for return calling conv lowering code.
2458 // FIXME: This is practically a copy-paste from TargetLowering::LowerCallTo.
2459 SmallVector<ISD::InputArg, 32> Ins;
2460 SmallVector<EVT, 4> RetTys;
2461 ComputeValueVTs(TLI, I->getType(), RetTys);
2462 for (unsigned i = 0, e = RetTys.size(); i != e; ++i) {
2463 EVT VT = RetTys[i];
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002464 MVT RegisterVT = TLI.getRegisterType(I->getParent()->getContext(), VT);
Eli Friedman7b279422011-05-17 18:29:03 +00002465 unsigned NumRegs = TLI.getNumRegisters(I->getParent()->getContext(), VT);
2466 for (unsigned j = 0; j != NumRegs; ++j) {
2467 ISD::InputArg MyFlags;
Patrik Hagglundbad545c2012-12-19 11:48:16 +00002468 MyFlags.VT = RegisterVT;
Eli Friedman7b279422011-05-17 18:29:03 +00002469 MyFlags.Used = !CS.getInstruction()->use_empty();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002470 if (CS.paramHasAttr(0, Attribute::SExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002471 MyFlags.Flags.setSExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002472 if (CS.paramHasAttr(0, Attribute::ZExt))
Eli Friedman7b279422011-05-17 18:29:03 +00002473 MyFlags.Flags.setZExt();
Bill Wendling3d7b0b82012-12-19 07:18:57 +00002474 if (CS.paramHasAttr(0, Attribute::InReg))
Eli Friedman7b279422011-05-17 18:29:03 +00002475 MyFlags.Flags.setInReg();
2476 Ins.push_back(MyFlags);
2477 }
2478 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002479
Eli Friedman7b279422011-05-17 18:29:03 +00002480 // Now handle call return values.
2481 SmallVector<unsigned, 4> UsedRegs;
2482 SmallVector<CCValAssign, 16> RVLocs;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002483 CCState CCRetInfo(CC, false, *FuncInfo.MF, TM, RVLocs,
Bill Wendlingea6397f2012-07-19 00:11:40 +00002484 I->getParent()->getContext());
Eli Friedman7b279422011-05-17 18:29:03 +00002485 unsigned ResultReg = FuncInfo.CreateRegs(I->getType());
2486 CCRetInfo.AnalyzeCallResult(Ins, RetCC_X86);
2487 for (unsigned i = 0; i != RVLocs.size(); ++i) {
2488 EVT CopyVT = RVLocs[i].getValVT();
2489 unsigned CopyReg = ResultReg + i;
Wesley Peck527da1b2010-11-23 03:31:01 +00002490
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002491 // If this is a call to a function that returns an fp value on the x87 fp
2492 // stack, but where we prefer to use the value in xmm registers, copy it
2493 // out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Eli Friedman7b279422011-05-17 18:29:03 +00002494 if ((RVLocs[i].getLocReg() == X86::ST0 ||
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002495 RVLocs[i].getLocReg() == X86::ST1)) {
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002496 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002497 CopyVT = MVT::f80;
Craig Topperabadc662012-04-20 06:31:50 +00002498 CopyReg = createResultReg(&X86::RFP80RegClass);
Jakob Stoklund Olesend0e23522011-06-30 23:42:18 +00002499 }
Rafael Espindolaea09c592014-02-18 22:05:46 +00002500 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2501 TII.get(X86::FpPOP_RETVAL), CopyReg);
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002502 } else {
Rafael Espindolaea09c592014-02-18 22:05:46 +00002503 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2504 TII.get(TargetOpcode::COPY),
Jakob Stoklund Olesen7297e7e2011-06-28 18:32:28 +00002505 CopyReg).addReg(RVLocs[i].getLocReg());
2506 UsedRegs.push_back(RVLocs[i].getLocReg());
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002507 }
2508
Eli Friedman7b279422011-05-17 18:29:03 +00002509 if (CopyVT != RVLocs[i].getValVT()) {
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002510 // Round the F80 the right size, which also moves to the appropriate xmm
2511 // register. This is accomplished by storing the F80 value in memory and
2512 // then loading it back. Ewww...
Eli Friedman7b279422011-05-17 18:29:03 +00002513 EVT ResVT = RVLocs[i].getValVT();
Owen Anderson9f944592009-08-11 20:47:22 +00002514 unsigned Opc = ResVT == MVT::f32 ? X86::ST_Fp80m32 : X86::ST_Fp80m64;
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002515 unsigned MemSize = ResVT.getSizeInBits()/8;
David Greene1fbe0542009-11-12 20:49:22 +00002516 int FI = MFI.CreateStackObject(MemSize, MemSize, false);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002517 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002518 TII.get(Opc)), FI)
Eli Friedman7b279422011-05-17 18:29:03 +00002519 .addReg(CopyReg);
Owen Anderson9f944592009-08-11 20:47:22 +00002520 Opc = ResVT == MVT::f32 ? X86::MOVSSrm : X86::MOVSDrm;
Rafael Espindolaea09c592014-02-18 22:05:46 +00002521 addFrameReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Eli Friedman7b279422011-05-17 18:29:03 +00002522 TII.get(Opc), ResultReg + i), FI);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002523 }
Eli Friedman7335e8a2011-05-17 02:36:59 +00002524 }
Eli Friedman83ba1502011-05-17 00:13:47 +00002525
Eli Friedman7b279422011-05-17 18:29:03 +00002526 if (RVLocs.size())
2527 UpdateValueMap(I, ResultReg, RVLocs.size());
2528
Dan Gohman86936502010-06-18 23:28:01 +00002529 // Set all unused physreg defs as dead.
2530 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
2531
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002532 return true;
2533}
2534
2535
Dan Gohmand58f3e32008-08-28 23:21:34 +00002536bool
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002537X86FastISel::TargetSelectInstruction(const Instruction *I) {
Dan Gohmand58f3e32008-08-28 23:21:34 +00002538 switch (I->getOpcode()) {
2539 default: break;
Evan Chenga41ee292008-09-03 06:44:39 +00002540 case Instruction::Load:
Dan Gohman7bda51f2008-09-03 23:12:08 +00002541 return X86SelectLoad(I);
Owen Andersonb8c7ba22008-09-04 16:48:33 +00002542 case Instruction::Store:
2543 return X86SelectStore(I);
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002544 case Instruction::Ret:
2545 return X86SelectRet(I);
Dan Gohman09fdbcf2008-09-04 23:26:51 +00002546 case Instruction::ICmp:
2547 case Instruction::FCmp:
2548 return X86SelectCmp(I);
Dan Gohmana5753b32008-09-05 01:06:14 +00002549 case Instruction::ZExt:
2550 return X86SelectZExt(I);
2551 case Instruction::Br:
2552 return X86SelectBranch(I);
Evan Cheng6c8f55c2008-09-07 09:09:33 +00002553 case Instruction::Call:
2554 return X86SelectCall(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002555 case Instruction::LShr:
2556 case Instruction::AShr:
2557 case Instruction::Shl:
2558 return X86SelectShift(I);
Eli Bendersky24a36eb2013-04-17 20:10:13 +00002559 case Instruction::SDiv:
2560 case Instruction::UDiv:
2561 case Instruction::SRem:
2562 case Instruction::URem:
2563 return X86SelectDivRem(I);
Dan Gohman7d7a26df2008-09-05 18:30:08 +00002564 case Instruction::Select:
2565 return X86SelectSelect(I);
Evan Chengb9286692008-09-07 08:47:42 +00002566 case Instruction::Trunc:
2567 return X86SelectTrunc(I);
Dan Gohmanbf646f22008-09-10 21:02:08 +00002568 case Instruction::FPExt:
2569 return X86SelectFPExt(I);
2570 case Instruction::FPTrunc:
2571 return X86SelectFPTrunc(I);
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002572 case Instruction::IntToPtr: // Deliberate fall-through.
2573 case Instruction::PtrToInt: {
Owen Anderson53aa7a92009-08-10 22:56:29 +00002574 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType());
2575 EVT DstVT = TLI.getValueType(I->getType());
Dan Gohmana62e4ab2009-03-13 23:53:06 +00002576 if (DstVT.bitsGT(SrcVT))
2577 return X86SelectZExt(I);
2578 if (DstVT.bitsLT(SrcVT))
2579 return X86SelectTrunc(I);
2580 unsigned Reg = getRegForValue(I->getOperand(0));
2581 if (Reg == 0) return false;
2582 UpdateValueMap(I, Reg);
2583 return true;
2584 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002585 }
2586
2587 return false;
2588}
2589
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002590unsigned X86FastISel::TargetMaterializeConstant(const Constant *C) {
Duncan Sandsf5dda012010-11-03 11:35:31 +00002591 MVT VT;
Chris Lattnera0f9d492008-10-15 05:07:36 +00002592 if (!isTypeLegal(C->getType(), VT))
Michael Liao3c898062012-08-30 00:30:16 +00002593 return 0;
2594
2595 // Can't handle alternate code models yet.
2596 if (TM.getCodeModel() != CodeModel::Small)
2597 return 0;
Wesley Peck527da1b2010-11-23 03:31:01 +00002598
Owen Anderson50288e32008-09-05 00:06:23 +00002599 // Get opcode and regclass of the output for the given load instruction.
2600 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002601 const TargetRegisterClass *RC = nullptr;
Duncan Sandsf5dda012010-11-03 11:35:31 +00002602 switch (VT.SimpleTy) {
Michael Liao3c898062012-08-30 00:30:16 +00002603 default: return 0;
Owen Anderson9f944592009-08-11 20:47:22 +00002604 case MVT::i8:
Owen Anderson50288e32008-09-05 00:06:23 +00002605 Opc = X86::MOV8rm;
Craig Topperabadc662012-04-20 06:31:50 +00002606 RC = &X86::GR8RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002607 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002608 case MVT::i16:
Owen Anderson50288e32008-09-05 00:06:23 +00002609 Opc = X86::MOV16rm;
Craig Topperabadc662012-04-20 06:31:50 +00002610 RC = &X86::GR16RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002611 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002612 case MVT::i32:
Owen Anderson50288e32008-09-05 00:06:23 +00002613 Opc = X86::MOV32rm;
Craig Topperabadc662012-04-20 06:31:50 +00002614 RC = &X86::GR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002615 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002616 case MVT::i64:
Owen Anderson50288e32008-09-05 00:06:23 +00002617 // Must be in x86-64 mode.
2618 Opc = X86::MOV64rm;
Craig Topperabadc662012-04-20 06:31:50 +00002619 RC = &X86::GR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002620 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002621 case MVT::f32:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002622 if (X86ScalarSSEf32) {
2623 Opc = Subtarget->hasAVX() ? X86::VMOVSSrm : X86::MOVSSrm;
Craig Topperabadc662012-04-20 06:31:50 +00002624 RC = &X86::FR32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002625 } else {
2626 Opc = X86::LD_Fp32m;
Craig Topperabadc662012-04-20 06:31:50 +00002627 RC = &X86::RFP32RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002628 }
2629 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002630 case MVT::f64:
Bruno Cardoso Lopesd893fc92011-09-03 00:46:42 +00002631 if (X86ScalarSSEf64) {
2632 Opc = Subtarget->hasAVX() ? X86::VMOVSDrm : X86::MOVSDrm;
Craig Topperabadc662012-04-20 06:31:50 +00002633 RC = &X86::FR64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002634 } else {
2635 Opc = X86::LD_Fp64m;
Craig Topperabadc662012-04-20 06:31:50 +00002636 RC = &X86::RFP64RegClass;
Owen Anderson50288e32008-09-05 00:06:23 +00002637 }
2638 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002639 case MVT::f80:
Dan Gohman839105d2008-09-26 01:39:32 +00002640 // No f80 support yet.
Michael Liao3c898062012-08-30 00:30:16 +00002641 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002642 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002643
Dan Gohman9801ba42008-09-19 22:16:54 +00002644 // Materialize addresses with LEA instructions.
Owen Anderson50288e32008-09-05 00:06:23 +00002645 if (isa<GlobalValue>(C)) {
Dan Gohman9801ba42008-09-19 22:16:54 +00002646 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002647 if (X86SelectAddress(C, AM)) {
Chris Lattner48326602011-04-17 17:12:08 +00002648 // If the expression is just a basereg, then we're done, otherwise we need
2649 // to emit an LEA.
2650 if (AM.BaseType == X86AddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +00002651 AM.IndexReg == 0 && AM.Disp == 0 && AM.GV == nullptr)
Chris Lattner48326602011-04-17 17:12:08 +00002652 return AM.Base.Reg;
Eric Christopher0713a9d2011-06-08 23:55:35 +00002653
Chris Lattner48326602011-04-17 17:12:08 +00002654 Opc = TLI.getPointerTy() == MVT::i32 ? X86::LEA32r : X86::LEA64r;
Dan Gohman9801ba42008-09-19 22:16:54 +00002655 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002656 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002657 TII.get(Opc), ResultReg), AM);
Owen Anderson50288e32008-09-05 00:06:23 +00002658 return ResultReg;
Dan Gohman9801ba42008-09-19 22:16:54 +00002659 }
Evan Chengf5bc7e52008-09-05 21:00:03 +00002660 return 0;
Owen Anderson50288e32008-09-05 00:06:23 +00002661 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002662
Owen Andersond41c7162008-09-06 01:11:01 +00002663 // MachineConstantPool wants an explicit alignment.
Rafael Espindolaea09c592014-02-18 22:05:46 +00002664 unsigned Align = DL.getPrefTypeAlignment(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002665 if (Align == 0) {
2666 // Alignment of vector types. FIXME!
Rafael Espindolaea09c592014-02-18 22:05:46 +00002667 Align = DL.getTypeAllocSize(C->getType());
Owen Andersond41c7162008-09-06 01:11:01 +00002668 }
Wesley Peck527da1b2010-11-23 03:31:01 +00002669
Dan Gohman8392f0c2008-09-30 01:21:32 +00002670 // x86-32 PIC requires a PIC base register for constant pools.
2671 unsigned PICBase = 0;
Chris Lattnera3260c02009-06-27 01:31:51 +00002672 unsigned char OpFlag = 0;
Chris Lattner21c29402009-07-10 21:00:45 +00002673 if (Subtarget->isPICStyleStubPIC()) { // Not dynamic-no-pic
Chris Lattnerfef11d62009-07-09 04:39:06 +00002674 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002675 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002676 } else if (Subtarget->isPICStyleGOT()) {
2677 OpFlag = X86II::MO_GOTOFF;
Dan Gohman87fb4e82010-07-07 16:29:44 +00002678 PICBase = getInstrInfo()->getGlobalBaseReg(FuncInfo.MF);
Chris Lattnerfef11d62009-07-09 04:39:06 +00002679 } else if (Subtarget->isPICStyleRIPRel() &&
2680 TM.getCodeModel() == CodeModel::Small) {
2681 PICBase = X86::RIP;
Chris Lattnera3260c02009-06-27 01:31:51 +00002682 }
Dan Gohman8392f0c2008-09-30 01:21:32 +00002683
2684 // Create the load from the constant pool.
Dan Gohman39d82f92008-09-10 20:11:02 +00002685 unsigned MCPOffset = MCP.getConstantPoolIndex(C, Align);
Dan Gohman9801ba42008-09-19 22:16:54 +00002686 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002687 addConstantPoolReference(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002688 TII.get(Opc), ResultReg),
Chris Lattnera3260c02009-06-27 01:31:51 +00002689 MCPOffset, PICBase, OpFlag);
Dan Gohman8392f0c2008-09-30 01:21:32 +00002690
Owen Anderson50288e32008-09-05 00:06:23 +00002691 return ResultReg;
2692}
2693
Dan Gohmanbcaf6812010-04-15 01:51:59 +00002694unsigned X86FastISel::TargetMaterializeAlloca(const AllocaInst *C) {
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002695 // Fail on dynamic allocas. At this point, getRegForValue has already
2696 // checked its CSE maps, so if we're here trying to handle a dynamic
2697 // alloca, we're not going to succeed. X86SelectAddress has a
2698 // check for dynamic allocas, because it's called directly from
2699 // various places, but TargetMaterializeAlloca also needs a check
2700 // in order to avoid recursion between getRegForValue,
2701 // X86SelectAddrss, and TargetMaterializeAlloca.
Dan Gohman87fb4e82010-07-07 16:29:44 +00002702 if (!FuncInfo.StaticAllocaMap.count(C))
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002703 return 0;
Reid Klecknerdfbed592014-01-31 23:45:12 +00002704 assert(C->isStaticAlloca() && "dynamic alloca in the static alloca map?");
Dan Gohmanb01a9c92008-10-03 01:27:49 +00002705
Dan Gohman39d82f92008-09-10 20:11:02 +00002706 X86AddressMode AM;
Chris Lattner8212d372009-07-10 05:33:42 +00002707 if (!X86SelectAddress(C, AM))
Dan Gohman39d82f92008-09-10 20:11:02 +00002708 return 0;
2709 unsigned Opc = Subtarget->is64Bit() ? X86::LEA64r : X86::LEA32r;
Craig Topper760b1342012-02-22 05:59:10 +00002710 const TargetRegisterClass* RC = TLI.getRegClassFor(TLI.getPointerTy());
Dan Gohman39d82f92008-09-10 20:11:02 +00002711 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002712 addFullAddress(BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Dan Gohmand7b5ce32010-07-10 09:00:22 +00002713 TII.get(Opc), ResultReg), AM);
Dan Gohman39d82f92008-09-10 20:11:02 +00002714 return ResultReg;
2715}
2716
Eli Friedman406c4712011-04-27 22:41:55 +00002717unsigned X86FastISel::TargetMaterializeFloatZero(const ConstantFP *CF) {
2718 MVT VT;
2719 if (!isTypeLegal(CF->getType(), VT))
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002720 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002721
2722 // Get opcode and regclass for the given zero.
2723 unsigned Opc = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +00002724 const TargetRegisterClass *RC = nullptr;
Eli Friedman406c4712011-04-27 22:41:55 +00002725 switch (VT.SimpleTy) {
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002726 default: return 0;
Craig Topper490c45c2012-08-11 17:53:00 +00002727 case MVT::f32:
2728 if (X86ScalarSSEf32) {
2729 Opc = X86::FsFLD0SS;
2730 RC = &X86::FR32RegClass;
2731 } else {
2732 Opc = X86::LD_Fp032;
2733 RC = &X86::RFP32RegClass;
2734 }
2735 break;
2736 case MVT::f64:
2737 if (X86ScalarSSEf64) {
2738 Opc = X86::FsFLD0SD;
2739 RC = &X86::FR64RegClass;
2740 } else {
2741 Opc = X86::LD_Fp064;
2742 RC = &X86::RFP64RegClass;
2743 }
2744 break;
2745 case MVT::f80:
2746 // No f80 support yet.
Jakub Staszakf34e4fa2012-11-15 19:40:29 +00002747 return 0;
Eli Friedman406c4712011-04-27 22:41:55 +00002748 }
2749
2750 unsigned ResultReg = createResultReg(RC);
Rafael Espindolaea09c592014-02-18 22:05:46 +00002751 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg);
Eli Friedman406c4712011-04-27 22:41:55 +00002752 return ResultReg;
2753}
2754
2755
Eli Bendersky90dd3e72013-04-19 22:29:18 +00002756bool X86FastISel::tryToFoldLoadIntoMI(MachineInstr *MI, unsigned OpNo,
2757 const LoadInst *LI) {
Juergen Ributzka349777d2014-06-12 23:27:57 +00002758 const Value *Ptr = LI->getPointerOperand();
Chris Lattnereeba0c72010-09-05 02:18:34 +00002759 X86AddressMode AM;
Juergen Ributzka349777d2014-06-12 23:27:57 +00002760 if (!X86SelectAddress(Ptr, AM))
Chris Lattnereeba0c72010-09-05 02:18:34 +00002761 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002762
Craig Topper55406d92012-08-11 17:46:16 +00002763 const X86InstrInfo &XII = (const X86InstrInfo&)TII;
Wesley Peck527da1b2010-11-23 03:31:01 +00002764
Rafael Espindolaea09c592014-02-18 22:05:46 +00002765 unsigned Size = DL.getTypeAllocSize(LI->getType());
Chris Lattnereeba0c72010-09-05 02:18:34 +00002766 unsigned Alignment = LI->getAlignment();
2767
Juergen Ributzka349777d2014-06-12 23:27:57 +00002768 if (Alignment == 0) // Ensure that codegen never sees alignment 0
2769 Alignment = DL.getABITypeAlignment(LI->getType());
2770
Chris Lattnereeba0c72010-09-05 02:18:34 +00002771 SmallVector<MachineOperand, 8> AddrOps;
2772 AM.getFullAddress(AddrOps);
Wesley Peck527da1b2010-11-23 03:31:01 +00002773
Chris Lattnereeba0c72010-09-05 02:18:34 +00002774 MachineInstr *Result =
2775 XII.foldMemoryOperandImpl(*FuncInfo.MF, MI, OpNo, AddrOps, Size, Alignment);
Juergen Ributzka349777d2014-06-12 23:27:57 +00002776 if (!Result)
2777 return false;
Wesley Peck527da1b2010-11-23 03:31:01 +00002778
Juergen Ributzka349777d2014-06-12 23:27:57 +00002779 Result->addMemOperand(*FuncInfo.MF, createMachineMemOperandFor(LI));
Chris Lattner2d186572011-01-16 02:27:38 +00002780 FuncInfo.MBB->insert(FuncInfo.InsertPt, Result);
Chris Lattnereeba0c72010-09-05 02:18:34 +00002781 MI->eraseFromParent();
2782 return true;
2783}
2784
2785
Evan Cheng24422d42008-09-03 00:03:49 +00002786namespace llvm {
Bob Wilson3e6fa462012-08-03 04:06:28 +00002787 FastISel *X86::createFastISel(FunctionLoweringInfo &funcInfo,
2788 const TargetLibraryInfo *libInfo) {
2789 return new X86FastISel(funcInfo, libInfo);
Evan Cheng24422d42008-09-03 00:03:49 +00002790 }
Dan Gohmand58f3e32008-08-28 23:21:34 +00002791}