blob: d5351d25d6edf1d5f5cbbff71507bbe550b99f3a [file] [log] [blame]
Chris Lattner5930d3d2005-11-16 22:59:19 +00001//===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
Chris Lattner655e7df2005-11-16 01:54:32 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner655e7df2005-11-16 01:54:32 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a DAG pattern matching instruction selector for X86,
11// converting from a legalized dag to a X86 dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Chengbc7a0f442006-01-11 06:09:51 +000016#include "X86InstrBuilder.h"
Evan Chengf55b7382008-01-05 00:41:47 +000017#include "X86MachineFunctionInfo.h"
Chris Lattner7c551262006-01-11 01:15:34 +000018#include "X86RegisterInfo.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000019#include "X86Subtarget.h"
Evan Cheng2dd2c652006-03-13 23:20:37 +000020#include "X86TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000021#include "llvm/ADT/Statistic.h"
Evan Cheng73a1ad92006-01-10 20:26:56 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000023#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner7c551262006-01-11 01:15:34 +000024#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnera10fff52007-12-31 04:13:23 +000025#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattner655e7df2005-11-16 01:54:32 +000026#include "llvm/CodeGen/SelectionDAGISel.h"
Eric Christopher79cc1e32014-09-02 22:28:02 +000027#include "llvm/IR/Function.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/Instructions.h"
29#include "llvm/IR/Intrinsics.h"
30#include "llvm/IR/Type.h"
Evan Cheng11b0a5d2006-09-08 06:48:29 +000031#include "llvm/Support/Debug.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000032#include "llvm/Support/ErrorHandling.h"
Evan Cheng11b0a5d2006-09-08 06:48:29 +000033#include "llvm/Support/MathExtras.h"
Torok Edwinfb8d6d52009-07-08 20:53:28 +000034#include "llvm/Support/raw_ostream.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Target/TargetMachine.h"
36#include "llvm/Target/TargetOptions.h"
Robin Morisset880580b2014-10-07 23:53:57 +000037#include <stdint.h>
Chris Lattner655e7df2005-11-16 01:54:32 +000038using namespace llvm;
39
Chandler Carruth84e68b22014-04-22 02:41:26 +000040#define DEBUG_TYPE "x86-isel"
41
Chris Lattner1ef9cd42006-12-19 22:59:26 +000042STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
43
Chris Lattner655e7df2005-11-16 01:54:32 +000044//===----------------------------------------------------------------------===//
45// Pattern Matcher Implementation
46//===----------------------------------------------------------------------===//
47
48namespace {
Chris Lattner3f0f71b2005-11-19 02:11:08 +000049 /// X86ISelAddressMode - This corresponds to X86AddressMode, but uses
Dan Gohman2ce6f2a2008-07-27 21:46:04 +000050 /// SDValue's instead of register numbers for the leaves of the matched
Chris Lattner3f0f71b2005-11-19 02:11:08 +000051 /// tree.
52 struct X86ISelAddressMode {
53 enum {
54 RegBase,
Chris Lattneraa2372562006-05-24 17:04:05 +000055 FrameIndexBase
Chris Lattner3f0f71b2005-11-19 02:11:08 +000056 } BaseType;
57
Dan Gohman0fd54fb2010-04-29 23:30:41 +000058 // This is really a union, discriminated by BaseType!
59 SDValue Base_Reg;
60 int Base_FrameIndex;
Chris Lattner3f0f71b2005-11-19 02:11:08 +000061
62 unsigned Scale;
Chad Rosier24c19d22012-08-01 18:39:17 +000063 SDValue IndexReg;
Dan Gohman059c4fa2008-11-11 15:52:29 +000064 int32_t Disp;
Rafael Espindola3b2df102009-04-08 21:14:34 +000065 SDValue Segment;
Dan Gohmanbcaf6812010-04-15 01:51:59 +000066 const GlobalValue *GV;
67 const Constant *CP;
68 const BlockAddress *BlockAddr;
Evan Cheng11b0a5d2006-09-08 06:48:29 +000069 const char *ES;
Rafael Espindola36b718f2015-06-22 17:46:53 +000070 MCSymbol *MCSym;
Evan Cheng11b0a5d2006-09-08 06:48:29 +000071 int JT;
Evan Cheng77d86ff2006-02-25 10:09:08 +000072 unsigned Align; // CP alignment.
Chris Lattnerbd7e26d2009-06-26 05:51:45 +000073 unsigned char SymbolFlags; // X86II::MO_*
Chris Lattner3f0f71b2005-11-19 02:11:08 +000074
75 X86ISelAddressMode()
Rafael Espindola36b718f2015-06-22 17:46:53 +000076 : BaseType(RegBase), Base_FrameIndex(0), Scale(1), IndexReg(), Disp(0),
77 Segment(), GV(nullptr), CP(nullptr), BlockAddr(nullptr), ES(nullptr),
78 MCSym(nullptr), JT(-1), Align(0), SymbolFlags(X86II::MO_NO_FLAG) {}
Dan Gohman4e3e3de2009-02-07 00:43:41 +000079
80 bool hasSymbolicDisplacement() const {
Craig Topper062a2ba2014-04-25 05:30:21 +000081 return GV != nullptr || CP != nullptr || ES != nullptr ||
Rafael Espindola36b718f2015-06-22 17:46:53 +000082 MCSym != nullptr || JT != -1 || BlockAddr != nullptr;
Dan Gohman4e3e3de2009-02-07 00:43:41 +000083 }
Chad Rosier24c19d22012-08-01 18:39:17 +000084
Chris Lattnerfea81da2009-06-27 04:16:01 +000085 bool hasBaseOrIndexReg() const {
Tim Northover97347a82013-09-19 11:33:53 +000086 return BaseType == FrameIndexBase ||
Craig Topper062a2ba2014-04-25 05:30:21 +000087 IndexReg.getNode() != nullptr || Base_Reg.getNode() != nullptr;
Chris Lattnerfea81da2009-06-27 04:16:01 +000088 }
Chad Rosier24c19d22012-08-01 18:39:17 +000089
Chris Lattnerfea81da2009-06-27 04:16:01 +000090 /// isRIPRelative - Return true if this addressing mode is already RIP
91 /// relative.
92 bool isRIPRelative() const {
93 if (BaseType != RegBase) return false;
94 if (RegisterSDNode *RegNode =
Dan Gohman0fd54fb2010-04-29 23:30:41 +000095 dyn_cast_or_null<RegisterSDNode>(Base_Reg.getNode()))
Chris Lattnerfea81da2009-06-27 04:16:01 +000096 return RegNode->getReg() == X86::RIP;
97 return false;
98 }
Chad Rosier24c19d22012-08-01 18:39:17 +000099
Chris Lattnerfea81da2009-06-27 04:16:01 +0000100 void setBaseReg(SDValue Reg) {
101 BaseType = RegBase;
Dan Gohman0fd54fb2010-04-29 23:30:41 +0000102 Base_Reg = Reg;
Chris Lattnerfea81da2009-06-27 04:16:01 +0000103 }
Dan Gohman4e3e3de2009-02-07 00:43:41 +0000104
Manman Ren19f49ac2012-09-11 22:23:19 +0000105#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Dale Johannesendafdbf72008-08-11 23:46:25 +0000106 void dump() {
David Greenedbdb1b22010-01-05 01:29:08 +0000107 dbgs() << "X86ISelAddressMode " << this << '\n';
Dan Gohman0fd54fb2010-04-29 23:30:41 +0000108 dbgs() << "Base_Reg ";
Craig Toppere73658d2014-04-28 04:05:08 +0000109 if (Base_Reg.getNode())
Chad Rosier24c19d22012-08-01 18:39:17 +0000110 Base_Reg.getNode()->dump();
Bill Wendlingfe3bdb42009-08-07 21:33:25 +0000111 else
David Greenedbdb1b22010-01-05 01:29:08 +0000112 dbgs() << "nul";
Dan Gohman0fd54fb2010-04-29 23:30:41 +0000113 dbgs() << " Base.FrameIndex " << Base_FrameIndex << '\n'
Benjamin Kramer940fbb02009-08-23 11:52:17 +0000114 << " Scale" << Scale << '\n'
115 << "IndexReg ";
Craig Toppere73658d2014-04-28 04:05:08 +0000116 if (IndexReg.getNode())
Bill Wendlingfe3bdb42009-08-07 21:33:25 +0000117 IndexReg.getNode()->dump();
118 else
Chad Rosier24c19d22012-08-01 18:39:17 +0000119 dbgs() << "nul";
David Greenedbdb1b22010-01-05 01:29:08 +0000120 dbgs() << " Disp " << Disp << '\n'
Benjamin Kramer940fbb02009-08-23 11:52:17 +0000121 << "GV ";
Bill Wendlingfe3bdb42009-08-07 21:33:25 +0000122 if (GV)
123 GV->dump();
124 else
David Greenedbdb1b22010-01-05 01:29:08 +0000125 dbgs() << "nul";
126 dbgs() << " CP ";
Bill Wendlingfe3bdb42009-08-07 21:33:25 +0000127 if (CP)
128 CP->dump();
129 else
David Greenedbdb1b22010-01-05 01:29:08 +0000130 dbgs() << "nul";
131 dbgs() << '\n'
Benjamin Kramer940fbb02009-08-23 11:52:17 +0000132 << "ES ";
Bill Wendlingfe3bdb42009-08-07 21:33:25 +0000133 if (ES)
David Greenedbdb1b22010-01-05 01:29:08 +0000134 dbgs() << ES;
Bill Wendlingfe3bdb42009-08-07 21:33:25 +0000135 else
David Greenedbdb1b22010-01-05 01:29:08 +0000136 dbgs() << "nul";
Rafael Espindola36b718f2015-06-22 17:46:53 +0000137 dbgs() << " MCSym ";
138 if (MCSym)
139 dbgs() << MCSym;
140 else
141 dbgs() << "nul";
David Greenedbdb1b22010-01-05 01:29:08 +0000142 dbgs() << " JT" << JT << " Align" << Align << '\n';
Dale Johannesendafdbf72008-08-11 23:46:25 +0000143 }
Manman Ren742534c2012-09-06 19:06:06 +0000144#endif
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000145 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000146}
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000147
148namespace {
Chris Lattner655e7df2005-11-16 01:54:32 +0000149 //===--------------------------------------------------------------------===//
150 /// ISel - X86 specific code to select X86 machine instructions for
151 /// SelectionDAG operations.
152 ///
Craig Topper26eec092014-03-31 06:22:15 +0000153 class X86DAGToDAGISel final : public SelectionDAGISel {
Chris Lattner655e7df2005-11-16 01:54:32 +0000154 /// Subtarget - Keep a pointer to the X86Subtarget around so that we can
155 /// make the right decision when generating code for different targets.
156 const X86Subtarget *Subtarget;
Evan Cheng5588de92006-02-18 00:15:05 +0000157
Evan Cheng7d6fa972008-09-26 23:41:32 +0000158 /// OptForSize - If true, selector should try to optimize for code size
159 /// instead of performance.
160 bool OptForSize;
161
Chris Lattner655e7df2005-11-16 01:54:32 +0000162 public:
Bill Wendling026e5d72009-04-29 23:29:43 +0000163 explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
Eric Christopher05b81972015-02-02 17:38:43 +0000164 : SelectionDAGISel(tm, OptLevel), OptForSize(false) {}
Chris Lattner655e7df2005-11-16 01:54:32 +0000165
Craig Topper2d9361e2014-03-09 07:44:38 +0000166 const char *getPassName() const override {
Chris Lattner655e7df2005-11-16 01:54:32 +0000167 return "X86 DAG->DAG Instruction Selection";
168 }
169
Eric Christopher4f09c592014-05-22 01:53:26 +0000170 bool runOnMachineFunction(MachineFunction &MF) override {
171 // Reset the subtarget each time through.
Eric Christopher05b81972015-02-02 17:38:43 +0000172 Subtarget = &MF.getSubtarget<X86Subtarget>();
Eric Christopher4f09c592014-05-22 01:53:26 +0000173 SelectionDAGISel::runOnMachineFunction(MF);
174 return true;
175 }
176
Craig Topper2d9361e2014-03-09 07:44:38 +0000177 void EmitFunctionEntryCode() override;
Anton Korobeynikov90910742007-09-25 21:52:30 +0000178
Craig Topper2d9361e2014-03-09 07:44:38 +0000179 bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const override;
Evan Cheng5e73ff22010-02-15 19:41:07 +0000180
Craig Topper2d9361e2014-03-09 07:44:38 +0000181 void PreprocessISelDAG() override;
Chris Lattnerf98f1242010-03-02 06:34:30 +0000182
Jakob Stoklund Olesen08aede22010-09-03 00:35:18 +0000183 inline bool immSext8(SDNode *N) const {
184 return isInt<8>(cast<ConstantSDNode>(N)->getSExtValue());
185 }
186
187 // i64immSExt32 predicate - True if the 64-bit immediate fits in a 32-bit
188 // sign extended field.
189 inline bool i64immSExt32(SDNode *N) const {
190 uint64_t v = cast<ConstantSDNode>(N)->getZExtValue();
191 return (int64_t)v == (int32_t)v;
192 }
193
Chris Lattner655e7df2005-11-16 01:54:32 +0000194// Include the pieces autogenerated from the target description.
195#include "X86GenDAGISel.inc"
196
197 private:
Craig Topper2d9361e2014-03-09 07:44:38 +0000198 SDNode *Select(SDNode *N) override;
Manman Rena0982042012-06-26 19:47:59 +0000199 SDNode *SelectGather(SDNode *N, unsigned Opc);
Craig Topper83e042a2013-08-15 05:57:07 +0000200 SDNode *SelectAtomicLoadArith(SDNode *Node, MVT NVT);
Chris Lattner655e7df2005-11-16 01:54:32 +0000201
Eli Friedmanef67e7d2011-07-13 20:44:23 +0000202 bool FoldOffsetIntoAddress(uint64_t Offset, X86ISelAddressMode &AM);
Chris Lattner8a236b62010-09-22 04:39:11 +0000203 bool MatchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM);
Rafael Espindola6688b0a2009-04-12 21:55:03 +0000204 bool MatchWrapper(SDValue N, X86ISelAddressMode &AM);
Dan Gohman824ab402009-07-22 23:26:55 +0000205 bool MatchAddress(SDValue N, X86ISelAddressMode &AM);
206 bool MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
207 unsigned Depth);
Rafael Espindola92773792009-03-31 16:16:57 +0000208 bool MatchAddressBase(SDValue N, X86ISelAddressMode &AM);
Chris Lattnerd58d7c12010-09-21 22:07:31 +0000209 bool SelectAddr(SDNode *Parent, SDValue N, SDValue &Base,
Rafael Espindola3b2df102009-04-08 21:14:34 +0000210 SDValue &Scale, SDValue &Index, SDValue &Disp,
211 SDValue &Segment);
Elena Demikhovskye1eda8a2015-04-30 08:38:48 +0000212 bool SelectVectorAddr(SDNode *Parent, SDValue N, SDValue &Base,
213 SDValue &Scale, SDValue &Index, SDValue &Disp,
214 SDValue &Segment);
Tim Northover3a1fd4c2013-06-01 09:55:14 +0000215 bool SelectMOV64Imm32(SDValue N, SDValue &Imm);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000216 bool SelectLEAAddr(SDValue N, SDValue &Base,
Chris Lattnerf4693072010-07-08 23:46:44 +0000217 SDValue &Scale, SDValue &Index, SDValue &Disp,
218 SDValue &Segment);
Tim Northover6833e3f2013-06-10 20:43:49 +0000219 bool SelectLEA64_32Addr(SDValue N, SDValue &Base,
220 SDValue &Scale, SDValue &Index, SDValue &Disp,
221 SDValue &Segment);
Chris Lattner0e023ea2010-09-21 20:31:19 +0000222 bool SelectTLSADDRAddr(SDValue N, SDValue &Base,
Chris Lattnerf4693072010-07-08 23:46:44 +0000223 SDValue &Scale, SDValue &Index, SDValue &Disp,
224 SDValue &Segment);
Chris Lattnerbd6e1932010-03-01 22:51:11 +0000225 bool SelectScalarSSELoad(SDNode *Root, SDValue N,
Chris Lattnerafac7dad2010-02-16 22:35:06 +0000226 SDValue &Base, SDValue &Scale,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000227 SDValue &Index, SDValue &Disp,
Rafael Espindola3b2df102009-04-08 21:14:34 +0000228 SDValue &Segment,
Chris Lattner18a32ce2010-02-21 03:17:59 +0000229 SDValue &NodeWithChain);
Chad Rosier24c19d22012-08-01 18:39:17 +0000230
Dan Gohmanea6f91f2010-01-05 01:24:18 +0000231 bool TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +0000232 SDValue &Base, SDValue &Scale,
Rafael Espindola3b2df102009-04-08 21:14:34 +0000233 SDValue &Index, SDValue &Disp,
234 SDValue &Segment);
Chad Rosier24c19d22012-08-01 18:39:17 +0000235
Chris Lattnerba1ed582006-06-08 18:03:49 +0000236 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
237 /// inline asm expressions.
Craig Topper2d9361e2014-03-09 07:44:38 +0000238 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +0000239 unsigned ConstraintID,
Craig Topper2d9361e2014-03-09 07:44:38 +0000240 std::vector<SDValue> &OutOps) override;
Chad Rosier24c19d22012-08-01 18:39:17 +0000241
David Majnemerd5ab35f2015-02-21 05:49:45 +0000242 void EmitSpecialCodeForMain();
Anton Korobeynikov90910742007-09-25 21:52:30 +0000243
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000244 inline void getAddressOperands(X86ISelAddressMode &AM, SDLoc DL,
245 SDValue &Base, SDValue &Scale,
246 SDValue &Index, SDValue &Disp,
247 SDValue &Segment) {
Eric Christopherb17140d2014-10-08 07:32:17 +0000248 Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
Mehdi Amini44ede332015-07-09 02:09:04 +0000249 ? CurDAG->getTargetFrameIndex(
250 AM.Base_FrameIndex,
251 TLI->getPointerTy(CurDAG->getDataLayout()))
Eric Christopherb17140d2014-10-08 07:32:17 +0000252 : AM.Base_Reg;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000253 Scale = getI8Imm(AM.Scale, DL);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000254 Index = AM.IndexReg;
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000255 // These are 32-bit even in 64-bit mode since RIP relative offset
256 // is 32-bit.
257 if (AM.GV)
Andrew Trickef9de2a2013-05-25 02:42:55 +0000258 Disp = CurDAG->getTargetGlobalAddress(AM.GV, SDLoc(),
Devang Patela3ca21b2010-07-06 22:08:15 +0000259 MVT::i32, AM.Disp,
Chris Lattnerbd7e26d2009-06-26 05:51:45 +0000260 AM.SymbolFlags);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000261 else if (AM.CP)
Owen Anderson9f944592009-08-11 20:47:22 +0000262 Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
Chris Lattnerbd7e26d2009-06-26 05:51:45 +0000263 AM.Align, AM.Disp, AM.SymbolFlags);
Michael Liaoabb87d42012-09-12 21:43:09 +0000264 else if (AM.ES) {
265 assert(!AM.Disp && "Non-zero displacement is ignored with ES.");
Owen Anderson9f944592009-08-11 20:47:22 +0000266 Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
Rafael Espindola36b718f2015-06-22 17:46:53 +0000267 } else if (AM.MCSym) {
268 assert(!AM.Disp && "Non-zero displacement is ignored with MCSym.");
269 assert(AM.SymbolFlags == 0 && "oo");
270 Disp = CurDAG->getMCSymbol(AM.MCSym, MVT::i32);
Michael Liaoabb87d42012-09-12 21:43:09 +0000271 } else if (AM.JT != -1) {
272 assert(!AM.Disp && "Non-zero displacement is ignored with JT.");
Owen Anderson9f944592009-08-11 20:47:22 +0000273 Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
Michael Liaoabb87d42012-09-12 21:43:09 +0000274 } else if (AM.BlockAddr)
275 Disp = CurDAG->getTargetBlockAddress(AM.BlockAddr, MVT::i32, AM.Disp,
276 AM.SymbolFlags);
Evan Cheng11b0a5d2006-09-08 06:48:29 +0000277 else
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000278 Disp = CurDAG->getTargetConstant(AM.Disp, DL, MVT::i32);
Rafael Espindola3b2df102009-04-08 21:14:34 +0000279
280 if (AM.Segment.getNode())
281 Segment = AM.Segment;
282 else
Owen Anderson9f944592009-08-11 20:47:22 +0000283 Segment = CurDAG->getRegister(0, MVT::i32);
Evan Cheng67ed58e2005-12-12 21:49:40 +0000284 }
285
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000286 /// getI8Imm - Return a target constant with the specified value, of type
287 /// i8.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000288 inline SDValue getI8Imm(unsigned Imm, SDLoc DL) {
289 return CurDAG->getTargetConstant(Imm, DL, MVT::i8);
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000290 }
291
Chris Lattner655e7df2005-11-16 01:54:32 +0000292 /// getI32Imm - Return a target constant with the specified value, of type
293 /// i32.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000294 inline SDValue getI32Imm(unsigned Imm, SDLoc DL) {
295 return CurDAG->getTargetConstant(Imm, DL, MVT::i32);
Chris Lattner655e7df2005-11-16 01:54:32 +0000296 }
Evan Chengd49cc362006-02-10 22:24:32 +0000297
Dan Gohman24300732008-09-23 18:22:58 +0000298 /// getGlobalBaseReg - Return an SDNode that returns the value of
299 /// the global base register. Output instructions required to
300 /// initialize the global base register, if necessary.
301 ///
Evan Cheng61413a32006-08-26 05:34:46 +0000302 SDNode *getGlobalBaseReg();
Evan Cheng5588de92006-02-18 00:15:05 +0000303
Dan Gohman4751bb92009-06-03 20:20:00 +0000304 /// getTargetMachine - Return a reference to the TargetMachine, casted
305 /// to the target-specific type.
Jakub Staszake167cf52013-02-19 21:54:59 +0000306 const X86TargetMachine &getTargetMachine() const {
Dan Gohman4751bb92009-06-03 20:20:00 +0000307 return static_cast<const X86TargetMachine &>(TM);
308 }
309
310 /// getInstrInfo - Return a reference to the TargetInstrInfo, casted
311 /// to the target-specific type.
Jakub Staszake167cf52013-02-19 21:54:59 +0000312 const X86InstrInfo *getInstrInfo() const {
Eric Christopher05b81972015-02-02 17:38:43 +0000313 return Subtarget->getInstrInfo();
Dan Gohman4751bb92009-06-03 20:20:00 +0000314 }
Adam Nemetff63a2d2014-10-03 20:00:34 +0000315
316 /// \brief Address-mode matching performs shift-of-and to and-of-shift
317 /// reassociation in order to expose more scaled addressing
318 /// opportunities.
319 bool ComplexPatternFuncMutatesDAG() const override {
320 return true;
321 }
Chris Lattner655e7df2005-11-16 01:54:32 +0000322 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000323}
Chris Lattner655e7df2005-11-16 01:54:32 +0000324
Evan Cheng72bb66a2006-08-08 00:31:00 +0000325
Evan Cheng5e73ff22010-02-15 19:41:07 +0000326bool
327X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const {
Bill Wendling026e5d72009-04-29 23:29:43 +0000328 if (OptLevel == CodeGenOpt::None) return false;
Evan Chengb86375c2006-10-14 08:33:25 +0000329
Evan Cheng5e73ff22010-02-15 19:41:07 +0000330 if (!N.hasOneUse())
331 return false;
332
333 if (N.getOpcode() != ISD::LOAD)
334 return true;
335
336 // If N is a load, do additional profitability checks.
337 if (U == Root) {
Evan Cheng83bdb382008-11-27 00:49:46 +0000338 switch (U->getOpcode()) {
339 default: break;
Dan Gohman85d4fdf2010-01-04 20:51:50 +0000340 case X86ISD::ADD:
341 case X86ISD::SUB:
342 case X86ISD::AND:
343 case X86ISD::XOR:
344 case X86ISD::OR:
Evan Cheng83bdb382008-11-27 00:49:46 +0000345 case ISD::ADD:
346 case ISD::ADDC:
347 case ISD::ADDE:
348 case ISD::AND:
349 case ISD::OR:
350 case ISD::XOR: {
Rafael Espindolabb834f02009-04-10 10:09:34 +0000351 SDValue Op1 = U->getOperand(1);
352
Evan Cheng83bdb382008-11-27 00:49:46 +0000353 // If the other operand is a 8-bit immediate we should fold the immediate
354 // instead. This reduces code size.
355 // e.g.
356 // movl 4(%esp), %eax
357 // addl $4, %eax
358 // vs.
359 // movl $4, %eax
360 // addl 4(%esp), %eax
361 // The former is 2 bytes shorter. In case where the increment is 1, then
362 // the saving can be 4 bytes (by using incl %eax).
Rafael Espindolabb834f02009-04-10 10:09:34 +0000363 if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
Dan Gohman2293eb62009-03-14 02:07:16 +0000364 if (Imm->getAPIntValue().isSignedIntN(8))
365 return false;
Rafael Espindolabb834f02009-04-10 10:09:34 +0000366
367 // If the other operand is a TLS address, we should fold it instead.
368 // This produces
369 // movl %gs:0, %eax
370 // leal i@NTPOFF(%eax), %eax
371 // instead of
372 // movl $i@NTPOFF, %eax
373 // addl %gs:0, %eax
374 // if the block also has an access to a second TLS address this will save
375 // a load.
Alp Tokerf907b892013-12-05 05:44:44 +0000376 // FIXME: This is probably also true for non-TLS addresses.
Rafael Espindolabb834f02009-04-10 10:09:34 +0000377 if (Op1.getOpcode() == X86ISD::Wrapper) {
378 SDValue Val = Op1.getOperand(0);
379 if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
380 return false;
381 }
Evan Cheng83bdb382008-11-27 00:49:46 +0000382 }
383 }
Evan Cheng5e73ff22010-02-15 19:41:07 +0000384 }
385
386 return true;
387}
388
Evan Chengd703df62010-03-14 03:48:46 +0000389/// MoveBelowCallOrigChain - Replace the original chain operand of the call with
390/// load's chain operand and move load below the call's chain operand.
391static void MoveBelowOrigChain(SelectionDAG *CurDAG, SDValue Load,
Evan Cheng214156c2012-10-02 23:49:13 +0000392 SDValue Call, SDValue OrigChain) {
Evan Chengf00f1e52008-08-25 21:27:18 +0000393 SmallVector<SDValue, 8> Ops;
Evan Chengd703df62010-03-14 03:48:46 +0000394 SDValue Chain = OrigChain.getOperand(0);
Evan Cheng6c7e8512009-01-26 18:43:34 +0000395 if (Chain.getNode() == Load.getNode())
396 Ops.push_back(Load.getOperand(0));
397 else {
398 assert(Chain.getOpcode() == ISD::TokenFactor &&
Evan Chengd703df62010-03-14 03:48:46 +0000399 "Unexpected chain operand");
Evan Cheng6c7e8512009-01-26 18:43:34 +0000400 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
401 if (Chain.getOperand(i).getNode() == Load.getNode())
402 Ops.push_back(Load.getOperand(0));
403 else
404 Ops.push_back(Chain.getOperand(i));
405 SDValue NewChain =
Craig Topper48d114b2014-04-26 18:35:24 +0000406 CurDAG->getNode(ISD::TokenFactor, SDLoc(Load), MVT::Other, Ops);
Evan Cheng6c7e8512009-01-26 18:43:34 +0000407 Ops.clear();
408 Ops.push_back(NewChain);
409 }
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000410 Ops.append(OrigChain->op_begin() + 1, OrigChain->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +0000411 CurDAG->UpdateNodeOperands(OrigChain.getNode(), Ops);
Dan Gohman92c11ac2010-06-18 15:30:29 +0000412 CurDAG->UpdateNodeOperands(Load.getNode(), Call.getOperand(0),
Evan Chengf00f1e52008-08-25 21:27:18 +0000413 Load.getOperand(1), Load.getOperand(2));
Evan Cheng214156c2012-10-02 23:49:13 +0000414
Evan Chengf00f1e52008-08-25 21:27:18 +0000415 Ops.clear();
Gabor Greiff304a7a2008-08-28 21:40:38 +0000416 Ops.push_back(SDValue(Load.getNode(), 1));
Benjamin Kramer6cd780f2015-02-17 15:29:18 +0000417 Ops.append(Call->op_begin() + 1, Call->op_end());
Craig Topper8c0b4d02014-04-28 05:57:50 +0000418 CurDAG->UpdateNodeOperands(Call.getNode(), Ops);
Evan Chengf00f1e52008-08-25 21:27:18 +0000419}
420
421/// isCalleeLoad - Return true if call address is a load and it can be
422/// moved below CALLSEQ_START and the chains leading up to the call.
423/// Return the CALLSEQ_START by reference as a second output.
Evan Chengd703df62010-03-14 03:48:46 +0000424/// In the case of a tail call, there isn't a callseq node between the call
425/// chain and the load.
426static bool isCalleeLoad(SDValue Callee, SDValue &Chain, bool HasCallSeq) {
Evan Cheng847ad442012-10-05 01:48:22 +0000427 // The transformation is somewhat dangerous if the call's chain was glued to
428 // the call. After MoveBelowOrigChain the load is moved between the call and
429 // the chain, this can create a cycle if the load is not folded. So it is
430 // *really* important that we are sure the load will be folded.
Gabor Greiff304a7a2008-08-28 21:40:38 +0000431 if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
Evan Chengf00f1e52008-08-25 21:27:18 +0000432 return false;
Gabor Greiff304a7a2008-08-28 21:40:38 +0000433 LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
Evan Chengf00f1e52008-08-25 21:27:18 +0000434 if (!LD ||
435 LD->isVolatile() ||
436 LD->getAddressingMode() != ISD::UNINDEXED ||
437 LD->getExtensionType() != ISD::NON_EXTLOAD)
438 return false;
439
440 // Now let's find the callseq_start.
Evan Chengd703df62010-03-14 03:48:46 +0000441 while (HasCallSeq && Chain.getOpcode() != ISD::CALLSEQ_START) {
Evan Chengf00f1e52008-08-25 21:27:18 +0000442 if (!Chain.hasOneUse())
443 return false;
444 Chain = Chain.getOperand(0);
445 }
Evan Chengd703df62010-03-14 03:48:46 +0000446
447 if (!Chain.getNumOperands())
448 return false;
Evan Cheng3fb03e22013-01-06 19:00:15 +0000449 // Since we are not checking for AA here, conservatively abort if the chain
450 // writes to memory. It's not safe to move the callee (a load) across a store.
451 if (isa<MemSDNode>(Chain.getNode()) &&
452 cast<MemSDNode>(Chain.getNode())->writeMem())
453 return false;
Evan Cheng6c7e8512009-01-26 18:43:34 +0000454 if (Chain.getOperand(0).getNode() == Callee.getNode())
455 return true;
456 if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
Dan Gohman520a6852009-09-15 01:22:01 +0000457 Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
458 Callee.getValue(1).hasOneUse())
Evan Cheng6c7e8512009-01-26 18:43:34 +0000459 return true;
460 return false;
Evan Chengf00f1e52008-08-25 21:27:18 +0000461}
462
Chris Lattner8d637042010-03-02 23:12:51 +0000463void X86DAGToDAGISel::PreprocessISelDAG() {
Chris Lattner82cc5332010-03-04 01:43:43 +0000464 // OptForSize is used in pattern predicates that isel is matching.
Duncan P. N. Exon Smith5975a702015-02-14 01:59:52 +0000465 OptForSize = MF->getFunction()->hasFnAttribute(Attribute::OptimizeForSize);
Chad Rosier24c19d22012-08-01 18:39:17 +0000466
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000467 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
468 E = CurDAG->allnodes_end(); I != E; ) {
Chris Lattnera91f77e2008-01-24 08:07:48 +0000469 SDNode *N = I++; // Preincrement iterator to avoid invalidation issues.
Chris Lattner8d637042010-03-02 23:12:51 +0000470
Evan Chengd703df62010-03-14 03:48:46 +0000471 if (OptLevel != CodeGenOpt::None &&
Michael Liao96b42602013-03-28 23:13:21 +0000472 // Only does this when target favors doesn't favor register indirect
473 // call.
474 ((N->getOpcode() == X86ISD::CALL && !Subtarget->callRegIndirect()) ||
Evan Cheng847ad442012-10-05 01:48:22 +0000475 (N->getOpcode() == X86ISD::TC_RETURN &&
Nick Lewyckyf41a80e2013-01-13 19:03:55 +0000476 // Only does this if load can be folded into TC_RETURN.
Evan Cheng847ad442012-10-05 01:48:22 +0000477 (Subtarget->is64Bit() ||
478 getTargetMachine().getRelocationModel() != Reloc::PIC_)))) {
Chris Lattner8d637042010-03-02 23:12:51 +0000479 /// Also try moving call address load from outside callseq_start to just
480 /// before the call to allow it to be folded.
481 ///
482 /// [Load chain]
483 /// ^
484 /// |
485 /// [Load]
486 /// ^ ^
487 /// | |
488 /// / \--
489 /// / |
490 ///[CALLSEQ_START] |
491 /// ^ |
492 /// | |
493 /// [LOAD/C2Reg] |
494 /// | |
495 /// \ /
496 /// \ /
497 /// [CALL]
Evan Chengd703df62010-03-14 03:48:46 +0000498 bool HasCallSeq = N->getOpcode() == X86ISD::CALL;
Chris Lattner8d637042010-03-02 23:12:51 +0000499 SDValue Chain = N->getOperand(0);
500 SDValue Load = N->getOperand(1);
Evan Chengd703df62010-03-14 03:48:46 +0000501 if (!isCalleeLoad(Load, Chain, HasCallSeq))
Chris Lattner8d637042010-03-02 23:12:51 +0000502 continue;
Evan Chengd703df62010-03-14 03:48:46 +0000503 MoveBelowOrigChain(CurDAG, Load, SDValue(N, 0), Chain);
Chris Lattner8d637042010-03-02 23:12:51 +0000504 ++NumLoadMoved;
505 continue;
506 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000507
Chris Lattner8d637042010-03-02 23:12:51 +0000508 // Lower fpround and fpextend nodes that target the FP stack to be store and
509 // load to the stack. This is a gross hack. We would like to simply mark
510 // these as being illegal, but when we do that, legalize produces these when
511 // it expands calls, then expands these in the same legalize pass. We would
512 // like dag combine to be able to hack on these between the call expansion
513 // and the node legalization. As such this pass basically does "really
514 // late" legalization of these inline with the X86 isel pass.
515 // FIXME: This should only happen when not compiled with -O0.
Chris Lattnera91f77e2008-01-24 08:07:48 +0000516 if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
517 continue;
Chad Rosier24c19d22012-08-01 18:39:17 +0000518
Craig Topper83e042a2013-08-15 05:57:07 +0000519 MVT SrcVT = N->getOperand(0).getSimpleValueType();
520 MVT DstVT = N->getSimpleValueType(0);
Bruno Cardoso Lopes616fe602011-08-01 21:54:05 +0000521
522 // If any of the sources are vectors, no fp stack involved.
523 if (SrcVT.isVector() || DstVT.isVector())
524 continue;
525
526 // If the source and destination are SSE registers, then this is a legal
527 // conversion that should not be lowered.
Benjamin Kramer02ff1cd2013-06-27 11:07:42 +0000528 const X86TargetLowering *X86Lowering =
Eric Christopherb17140d2014-10-08 07:32:17 +0000529 static_cast<const X86TargetLowering *>(TLI);
Bill Wendlinga3cd3502013-06-19 21:36:55 +0000530 bool SrcIsSSE = X86Lowering->isScalarFPTypeInSSEReg(SrcVT);
531 bool DstIsSSE = X86Lowering->isScalarFPTypeInSSEReg(DstVT);
Chris Lattnera91f77e2008-01-24 08:07:48 +0000532 if (SrcIsSSE && DstIsSSE)
533 continue;
534
Chris Lattnerd587e582008-03-09 07:05:32 +0000535 if (!SrcIsSSE && !DstIsSSE) {
536 // If this is an FPStack extension, it is a noop.
537 if (N->getOpcode() == ISD::FP_EXTEND)
538 continue;
539 // If this is a value-preserving FPStack truncation, it is a noop.
540 if (N->getConstantOperandVal(1))
541 continue;
542 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000543
Chris Lattnera91f77e2008-01-24 08:07:48 +0000544 // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
545 // FPStack has extload and truncstore. SSE can fold direct loads into other
546 // operations. Based on this, decide what we want to do.
Craig Topper83e042a2013-08-15 05:57:07 +0000547 MVT MemVT;
Chris Lattnera91f77e2008-01-24 08:07:48 +0000548 if (N->getOpcode() == ISD::FP_ROUND)
549 MemVT = DstVT; // FP_ROUND must use DstVT, we can't do a 'trunc load'.
550 else
551 MemVT = SrcIsSSE ? SrcVT : DstVT;
Chad Rosier24c19d22012-08-01 18:39:17 +0000552
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000553 SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
Andrew Trickef9de2a2013-05-25 02:42:55 +0000554 SDLoc dl(N);
Chad Rosier24c19d22012-08-01 18:39:17 +0000555
Chris Lattnera91f77e2008-01-24 08:07:48 +0000556 // FIXME: optimize the case where the src/dest is a load or store?
Dale Johannesen14f2d9d2009-02-03 21:48:12 +0000557 SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000558 N->getOperand(0),
Chris Lattner3d178ed2010-09-21 17:04:51 +0000559 MemTmp, MachinePointerInfo(), MemVT,
David Greenecbd39c52010-02-15 16:57:43 +0000560 false, false, 0);
Stuart Hastings81c43062011-02-16 16:23:55 +0000561 SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
Chris Lattner3d178ed2010-09-21 17:04:51 +0000562 MachinePointerInfo(),
Louis Gerbarg67474e32014-07-31 21:45:05 +0000563 MemVT, false, false, false, 0);
Chris Lattnera91f77e2008-01-24 08:07:48 +0000564
565 // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
566 // extload we created. This will cause general havok on the dag because
567 // anything below the conversion could be folded into other existing nodes.
568 // To avoid invalidating 'I', back it up to the convert node.
569 --I;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000570 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
Chad Rosier24c19d22012-08-01 18:39:17 +0000571
Chris Lattnera91f77e2008-01-24 08:07:48 +0000572 // Now that we did that, the node is dead. Increment the iterator to the
573 // next node to process, then delete N.
574 ++I;
Dan Gohmaneb0cee92008-08-23 02:25:05 +0000575 CurDAG->DeleteNode(N);
Chad Rosier24c19d22012-08-01 18:39:17 +0000576 }
Chris Lattnera91f77e2008-01-24 08:07:48 +0000577}
578
Chris Lattner655e7df2005-11-16 01:54:32 +0000579
Anton Korobeynikov90910742007-09-25 21:52:30 +0000580/// EmitSpecialCodeForMain - Emit any code that needs to be executed only in
581/// the main function.
David Majnemerd5ab35f2015-02-21 05:49:45 +0000582void X86DAGToDAGISel::EmitSpecialCodeForMain() {
Bill Wendling81d40712011-01-06 00:47:10 +0000583 if (Subtarget->isTargetCygMing()) {
David Majnemerd5ab35f2015-02-21 05:49:45 +0000584 TargetLowering::ArgListTy Args;
Mehdi Amini44ede332015-07-09 02:09:04 +0000585 auto &DL = CurDAG->getDataLayout();
David Majnemerd5ab35f2015-02-21 05:49:45 +0000586
587 TargetLowering::CallLoweringInfo CLI(*CurDAG);
588 CLI.setChain(CurDAG->getRoot())
589 .setCallee(CallingConv::C, Type::getVoidTy(*CurDAG->getContext()),
Mehdi Amini44ede332015-07-09 02:09:04 +0000590 CurDAG->getExternalSymbol("__main", TLI->getPointerTy(DL)),
David Majnemerd5ab35f2015-02-21 05:49:45 +0000591 std::move(Args), 0);
592 const TargetLowering &TLI = CurDAG->getTargetLoweringInfo();
593 std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
594 CurDAG->setRoot(Result.second);
Bill Wendling81d40712011-01-06 00:47:10 +0000595 }
Anton Korobeynikov90910742007-09-25 21:52:30 +0000596}
597
Dan Gohmanc87b74d2010-04-14 20:17:22 +0000598void X86DAGToDAGISel::EmitFunctionEntryCode() {
Anton Korobeynikov90910742007-09-25 21:52:30 +0000599 // If this is main, emit special code for main.
Dan Gohmanc87b74d2010-04-14 20:17:22 +0000600 if (const Function *Fn = MF->getFunction())
601 if (Fn->hasExternalLinkage() && Fn->getName() == "main")
David Majnemerd5ab35f2015-02-21 05:49:45 +0000602 EmitSpecialCodeForMain();
Anton Korobeynikov90910742007-09-25 21:52:30 +0000603}
604
Eli Friedman344ec792011-07-13 21:29:53 +0000605static bool isDispSafeForFrameIndex(int64_t Val) {
606 // On 64-bit platforms, we can run into an issue where a frame index
607 // includes a displacement that, when added to the explicit displacement,
608 // will overflow the displacement field. Assuming that the frame index
609 // displacement fits into a 31-bit integer (which is only slightly more
610 // aggressive than the current fundamental assumption that it fits into
611 // a 32-bit integer), a 31-bit disp should always be safe.
612 return isInt<31>(Val);
613}
614
Eli Friedmanef67e7d2011-07-13 20:44:23 +0000615bool X86DAGToDAGISel::FoldOffsetIntoAddress(uint64_t Offset,
616 X86ISelAddressMode &AM) {
Reid Kleckner9dad2272015-05-04 23:22:36 +0000617 // Cannot combine ExternalSymbol displacements with integer offsets.
Rafael Espindola36b718f2015-06-22 17:46:53 +0000618 if (Offset != 0 && (AM.ES || AM.MCSym))
Reid Kleckner9dad2272015-05-04 23:22:36 +0000619 return true;
Eli Friedmanef67e7d2011-07-13 20:44:23 +0000620 int64_t Val = AM.Disp + Offset;
621 CodeModel::Model M = TM.getCodeModel();
Eli Friedman344ec792011-07-13 21:29:53 +0000622 if (Subtarget->is64Bit()) {
623 if (!X86::isOffsetSuitableForCodeModel(Val, M,
624 AM.hasSymbolicDisplacement()))
625 return true;
626 // In addition to the checks required for a register base, check that
627 // we do not try to use an unsafe Disp with a frame index.
628 if (AM.BaseType == X86ISelAddressMode::FrameIndexBase &&
629 !isDispSafeForFrameIndex(Val))
630 return true;
Eli Friedmanef67e7d2011-07-13 20:44:23 +0000631 }
Eli Friedman344ec792011-07-13 21:29:53 +0000632 AM.Disp = Val;
633 return false;
634
Eli Friedmanef67e7d2011-07-13 20:44:23 +0000635}
Rafael Espindola3b2df102009-04-08 21:14:34 +0000636
Chris Lattner8a236b62010-09-22 04:39:11 +0000637bool X86DAGToDAGISel::MatchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM){
638 SDValue Address = N->getOperand(1);
Chad Rosier24c19d22012-08-01 18:39:17 +0000639
Chris Lattner8a236b62010-09-22 04:39:11 +0000640 // load gs:0 -> GS segment register.
641 // load fs:0 -> FS segment register.
642 //
Rafael Espindola3b2df102009-04-08 21:14:34 +0000643 // This optimization is valid because the GNU TLS model defines that
644 // gs:0 (or fs:0 on X86-64) contains its own address.
645 // For more information see http://people.redhat.com/drepper/tls.pdf
Chris Lattner8a236b62010-09-22 04:39:11 +0000646 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Address))
Craig Topper062a2ba2014-04-25 05:30:21 +0000647 if (C->getSExtValue() == 0 && AM.Segment.getNode() == nullptr &&
David Chisnall5b8c1682012-07-24 20:04:16 +0000648 Subtarget->isTargetLinux())
Chris Lattner8a236b62010-09-22 04:39:11 +0000649 switch (N->getPointerInfo().getAddrSpace()) {
650 case 256:
651 AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16);
652 return false;
653 case 257:
654 AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16);
655 return false;
656 }
Chad Rosier24c19d22012-08-01 18:39:17 +0000657
Rafael Espindola3b2df102009-04-08 21:14:34 +0000658 return true;
659}
660
Chris Lattnerfea81da2009-06-27 04:16:01 +0000661/// MatchWrapper - Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes
662/// into an addressing mode. These wrap things that will resolve down into a
663/// symbol reference. If no match is possible, this returns true, otherwise it
Anton Korobeynikov741ea0d2009-08-05 23:01:26 +0000664/// returns false.
Rafael Espindola6688b0a2009-04-12 21:55:03 +0000665bool X86DAGToDAGISel::MatchWrapper(SDValue N, X86ISelAddressMode &AM) {
Chris Lattnerfea81da2009-06-27 04:16:01 +0000666 // If the addressing mode already has a symbol as the displacement, we can
667 // never match another symbol.
Rafael Espindola6688b0a2009-04-12 21:55:03 +0000668 if (AM.hasSymbolicDisplacement())
669 return true;
Rafael Espindola6688b0a2009-04-12 21:55:03 +0000670
671 SDValue N0 = N.getOperand(0);
Anton Korobeynikov741ea0d2009-08-05 23:01:26 +0000672 CodeModel::Model M = TM.getCodeModel();
673
Chris Lattnerfea81da2009-06-27 04:16:01 +0000674 // Handle X86-64 rip-relative addresses. We check this before checking direct
675 // folding because RIP is preferable to non-RIP accesses.
Chandler Carruth3779ac12012-04-09 02:13:06 +0000676 if (Subtarget->is64Bit() && N.getOpcode() == X86ISD::WrapperRIP &&
Chris Lattnerfea81da2009-06-27 04:16:01 +0000677 // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
678 // they cannot be folded into immediate fields.
679 // FIXME: This can be improved for kernel and other models?
Chandler Carruth3779ac12012-04-09 02:13:06 +0000680 (M == CodeModel::Small || M == CodeModel::Kernel)) {
681 // Base and index reg must be 0 in order to use %rip as base.
682 if (AM.hasBaseOrIndexReg())
683 return true;
Chris Lattnerfea81da2009-06-27 04:16:01 +0000684 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
Eli Friedmanef67e7d2011-07-13 20:44:23 +0000685 X86ISelAddressMode Backup = AM;
Chris Lattnerfea81da2009-06-27 04:16:01 +0000686 AM.GV = G->getGlobal();
Chris Lattnerbd7e26d2009-06-26 05:51:45 +0000687 AM.SymbolFlags = G->getTargetFlags();
Eli Friedmanef67e7d2011-07-13 20:44:23 +0000688 if (FoldOffsetIntoAddress(G->getOffset(), AM)) {
689 AM = Backup;
690 return true;
691 }
Chris Lattnerfea81da2009-06-27 04:16:01 +0000692 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
Eli Friedmanef67e7d2011-07-13 20:44:23 +0000693 X86ISelAddressMode Backup = AM;
Rafael Espindola6688b0a2009-04-12 21:55:03 +0000694 AM.CP = CP->getConstVal();
695 AM.Align = CP->getAlignment();
Chris Lattner1d3b65a2009-06-26 05:56:49 +0000696 AM.SymbolFlags = CP->getTargetFlags();
Eli Friedmanef67e7d2011-07-13 20:44:23 +0000697 if (FoldOffsetIntoAddress(CP->getOffset(), AM)) {
698 AM = Backup;
699 return true;
700 }
Chris Lattnerfea81da2009-06-27 04:16:01 +0000701 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
702 AM.ES = S->getSymbol();
703 AM.SymbolFlags = S->getTargetFlags();
Rafael Espindola36b718f2015-06-22 17:46:53 +0000704 } else if (auto *S = dyn_cast<MCSymbolSDNode>(N0)) {
705 AM.MCSym = S->getMCSymbol();
Chris Lattner50ba5c32009-11-01 03:25:03 +0000706 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattnerfea81da2009-06-27 04:16:01 +0000707 AM.JT = J->getIndex();
708 AM.SymbolFlags = J->getTargetFlags();
Michael Liaoabb87d42012-09-12 21:43:09 +0000709 } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(N0)) {
710 X86ISelAddressMode Backup = AM;
711 AM.BlockAddr = BA->getBlockAddress();
712 AM.SymbolFlags = BA->getTargetFlags();
713 if (FoldOffsetIntoAddress(BA->getOffset(), AM)) {
714 AM = Backup;
715 return true;
716 }
717 } else
718 llvm_unreachable("Unhandled symbol reference node.");
Anton Korobeynikov741ea0d2009-08-05 23:01:26 +0000719
Chris Lattnerfea81da2009-06-27 04:16:01 +0000720 if (N.getOpcode() == X86ISD::WrapperRIP)
Owen Anderson9f944592009-08-11 20:47:22 +0000721 AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
Rafael Espindola6688b0a2009-04-12 21:55:03 +0000722 return false;
Chris Lattnerfea81da2009-06-27 04:16:01 +0000723 }
724
725 // Handle the case when globals fit in our immediate field: This is true for
Chandler Carruth3779ac12012-04-09 02:13:06 +0000726 // X86-32 always and X86-64 when in -mcmodel=small mode. In 64-bit
727 // mode, this only applies to a non-RIP-relative computation.
Chris Lattnerfea81da2009-06-27 04:16:01 +0000728 if (!Subtarget->is64Bit() ||
Chandler Carruth3779ac12012-04-09 02:13:06 +0000729 M == CodeModel::Small || M == CodeModel::Kernel) {
730 assert(N.getOpcode() != X86ISD::WrapperRIP &&
731 "RIP-relative addressing already handled");
Chris Lattnerfea81da2009-06-27 04:16:01 +0000732 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
733 AM.GV = G->getGlobal();
734 AM.Disp += G->getOffset();
735 AM.SymbolFlags = G->getTargetFlags();
736 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
737 AM.CP = CP->getConstVal();
738 AM.Align = CP->getAlignment();
739 AM.Disp += CP->getOffset();
740 AM.SymbolFlags = CP->getTargetFlags();
741 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
742 AM.ES = S->getSymbol();
743 AM.SymbolFlags = S->getTargetFlags();
Rafael Espindola36b718f2015-06-22 17:46:53 +0000744 } else if (auto *S = dyn_cast<MCSymbolSDNode>(N0)) {
745 AM.MCSym = S->getMCSymbol();
Chris Lattner50ba5c32009-11-01 03:25:03 +0000746 } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
Chris Lattnerfea81da2009-06-27 04:16:01 +0000747 AM.JT = J->getIndex();
748 AM.SymbolFlags = J->getTargetFlags();
Michael Liaoabb87d42012-09-12 21:43:09 +0000749 } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(N0)) {
750 AM.BlockAddr = BA->getBlockAddress();
751 AM.Disp += BA->getOffset();
752 AM.SymbolFlags = BA->getTargetFlags();
753 } else
754 llvm_unreachable("Unhandled symbol reference node.");
Rafael Espindola6688b0a2009-04-12 21:55:03 +0000755 return false;
756 }
757
758 return true;
759}
760
Chris Lattner3f0f71b2005-11-19 02:11:08 +0000761/// MatchAddress - Add the specified node to the specified addressing mode,
762/// returning true if it cannot be done. This just pattern matches for the
Chris Lattnerff87f05e2007-12-08 07:22:58 +0000763/// addressing mode.
Dan Gohman824ab402009-07-22 23:26:55 +0000764bool X86DAGToDAGISel::MatchAddress(SDValue N, X86ISelAddressMode &AM) {
Dan Gohman99ba4da2010-06-18 01:24:29 +0000765 if (MatchAddressRecursively(N, AM, 0))
Dan Gohman824ab402009-07-22 23:26:55 +0000766 return true;
767
768 // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
769 // a smaller encoding and avoids a scaled-index.
770 if (AM.Scale == 2 &&
771 AM.BaseType == X86ISelAddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +0000772 AM.Base_Reg.getNode() == nullptr) {
Dan Gohman0fd54fb2010-04-29 23:30:41 +0000773 AM.Base_Reg = AM.IndexReg;
Dan Gohman824ab402009-07-22 23:26:55 +0000774 AM.Scale = 1;
775 }
776
Dan Gohman05046082009-08-20 18:23:44 +0000777 // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
778 // because it has a smaller encoding.
779 // TODO: Which other code models can use this?
780 if (TM.getCodeModel() == CodeModel::Small &&
781 Subtarget->is64Bit() &&
782 AM.Scale == 1 &&
783 AM.BaseType == X86ISelAddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +0000784 AM.Base_Reg.getNode() == nullptr &&
785 AM.IndexReg.getNode() == nullptr &&
Dan Gohman0f6bf2d2009-08-25 17:47:44 +0000786 AM.SymbolFlags == X86II::MO_NO_FLAG &&
Dan Gohman05046082009-08-20 18:23:44 +0000787 AM.hasSymbolicDisplacement())
Dan Gohman0fd54fb2010-04-29 23:30:41 +0000788 AM.Base_Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
Dan Gohman05046082009-08-20 18:23:44 +0000789
Dan Gohman824ab402009-07-22 23:26:55 +0000790 return false;
791}
792
Chandler Carruth3eacfb82012-01-11 11:04:36 +0000793// Insert a node into the DAG at least before the Pos node's position. This
794// will reposition the node as needed, and will assign it a node ID that is <=
795// the Pos node's ID. Note that this does *not* preserve the uniqueness of node
796// IDs! The selection DAG must no longer depend on their uniqueness when this
797// is used.
798static void InsertDAGNode(SelectionDAG &DAG, SDValue Pos, SDValue N) {
799 if (N.getNode()->getNodeId() == -1 ||
800 N.getNode()->getNodeId() > Pos.getNode()->getNodeId()) {
801 DAG.RepositionNode(Pos.getNode(), N.getNode());
802 N.getNode()->setNodeId(Pos.getNode()->getNodeId());
803 }
804}
805
Adam Nemet0c7caf42014-09-16 17:14:10 +0000806// Transform "(X >> (8-C1)) & (0xff << C1)" to "((X >> 8) & 0xff) << C1" if
807// safe. This allows us to convert the shift and and into an h-register
808// extract and a scaled index. Returns false if the simplification is
809// performed.
Chandler Carruth51d30762012-01-11 08:48:20 +0000810static bool FoldMaskAndShiftToExtract(SelectionDAG &DAG, SDValue N,
811 uint64_t Mask,
812 SDValue Shift, SDValue X,
813 X86ISelAddressMode &AM) {
814 if (Shift.getOpcode() != ISD::SRL ||
815 !isa<ConstantSDNode>(Shift.getOperand(1)) ||
816 !Shift.hasOneUse())
817 return true;
818
819 int ScaleLog = 8 - Shift.getConstantOperandVal(1);
820 if (ScaleLog <= 0 || ScaleLog >= 4 ||
821 Mask != (0xffu << ScaleLog))
822 return true;
823
Craig Topper83e042a2013-08-15 05:57:07 +0000824 MVT VT = N.getSimpleValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000825 SDLoc DL(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000826 SDValue Eight = DAG.getConstant(8, DL, MVT::i8);
827 SDValue NewMask = DAG.getConstant(0xff, DL, VT);
Chandler Carruth51d30762012-01-11 08:48:20 +0000828 SDValue Srl = DAG.getNode(ISD::SRL, DL, VT, X, Eight);
829 SDValue And = DAG.getNode(ISD::AND, DL, VT, Srl, NewMask);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000830 SDValue ShlCount = DAG.getConstant(ScaleLog, DL, MVT::i8);
Chandler Carruth51d30762012-01-11 08:48:20 +0000831 SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, And, ShlCount);
832
Chandler Carrutheb21da02012-01-12 01:34:44 +0000833 // Insert the new nodes into the topological ordering. We must do this in
834 // a valid topological ordering as nothing is going to go back and re-sort
835 // these nodes. We continually insert before 'N' in sequence as this is
836 // essentially a pre-flattened and pre-sorted sequence of nodes. There is no
837 // hierarchy left to express.
838 InsertDAGNode(DAG, N, Eight);
839 InsertDAGNode(DAG, N, Srl);
840 InsertDAGNode(DAG, N, NewMask);
Chandler Carruth3eacfb82012-01-11 11:04:36 +0000841 InsertDAGNode(DAG, N, And);
Chandler Carrutheb21da02012-01-12 01:34:44 +0000842 InsertDAGNode(DAG, N, ShlCount);
Chandler Carruth3eacfb82012-01-11 11:04:36 +0000843 InsertDAGNode(DAG, N, Shl);
Chandler Carruth51d30762012-01-11 08:48:20 +0000844 DAG.ReplaceAllUsesWith(N, Shl);
845 AM.IndexReg = And;
846 AM.Scale = (1 << ScaleLog);
847 return false;
848}
849
Chandler Carruthaa01e662012-01-11 09:35:00 +0000850// Transforms "(X << C1) & C2" to "(X & (C2>>C1)) << C1" if safe and if this
851// allows us to fold the shift into this addressing mode. Returns false if the
852// transform succeeded.
853static bool FoldMaskedShiftToScaledMask(SelectionDAG &DAG, SDValue N,
854 uint64_t Mask,
855 SDValue Shift, SDValue X,
856 X86ISelAddressMode &AM) {
857 if (Shift.getOpcode() != ISD::SHL ||
858 !isa<ConstantSDNode>(Shift.getOperand(1)))
859 return true;
860
861 // Not likely to be profitable if either the AND or SHIFT node has more
862 // than one use (unless all uses are for address computation). Besides,
863 // isel mechanism requires their node ids to be reused.
864 if (!N.hasOneUse() || !Shift.hasOneUse())
865 return true;
866
867 // Verify that the shift amount is something we can fold.
868 unsigned ShiftAmt = Shift.getConstantOperandVal(1);
869 if (ShiftAmt != 1 && ShiftAmt != 2 && ShiftAmt != 3)
870 return true;
871
Craig Topper83e042a2013-08-15 05:57:07 +0000872 MVT VT = N.getSimpleValueType();
Andrew Trickef9de2a2013-05-25 02:42:55 +0000873 SDLoc DL(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000874 SDValue NewMask = DAG.getConstant(Mask >> ShiftAmt, DL, VT);
Chandler Carruthaa01e662012-01-11 09:35:00 +0000875 SDValue NewAnd = DAG.getNode(ISD::AND, DL, VT, X, NewMask);
876 SDValue NewShift = DAG.getNode(ISD::SHL, DL, VT, NewAnd, Shift.getOperand(1));
877
Chandler Carrutheb21da02012-01-12 01:34:44 +0000878 // Insert the new nodes into the topological ordering. We must do this in
879 // a valid topological ordering as nothing is going to go back and re-sort
880 // these nodes. We continually insert before 'N' in sequence as this is
881 // essentially a pre-flattened and pre-sorted sequence of nodes. There is no
882 // hierarchy left to express.
883 InsertDAGNode(DAG, N, NewMask);
884 InsertDAGNode(DAG, N, NewAnd);
Chandler Carruth3eacfb82012-01-11 11:04:36 +0000885 InsertDAGNode(DAG, N, NewShift);
Chandler Carruthaa01e662012-01-11 09:35:00 +0000886 DAG.ReplaceAllUsesWith(N, NewShift);
887
888 AM.Scale = 1 << ShiftAmt;
889 AM.IndexReg = NewAnd;
890 return false;
891}
892
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000893// Implement some heroics to detect shifts of masked values where the mask can
894// be replaced by extending the shift and undoing that in the addressing mode
895// scale. Patterns such as (shl (srl x, c1), c2) are canonicalized into (and
896// (srl x, SHIFT), MASK) by DAGCombines that don't know the shl can be done in
897// the addressing mode. This results in code such as:
898//
899// int f(short *y, int *lookup_table) {
900// ...
901// return *y + lookup_table[*y >> 11];
902// }
903//
904// Turning into:
905// movzwl (%rdi), %eax
906// movl %eax, %ecx
907// shrl $11, %ecx
908// addl (%rsi,%rcx,4), %eax
909//
910// Instead of:
911// movzwl (%rdi), %eax
912// movl %eax, %ecx
913// shrl $9, %ecx
914// andl $124, %rcx
915// addl (%rsi,%rcx), %eax
916//
Chandler Carruth3dbcda82012-01-11 09:35:02 +0000917// Note that this function assumes the mask is provided as a mask *after* the
918// value is shifted. The input chain may or may not match that, but computing
919// such a mask is trivial.
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000920static bool FoldMaskAndShiftToScale(SelectionDAG &DAG, SDValue N,
Chandler Carruth3dbcda82012-01-11 09:35:02 +0000921 uint64_t Mask,
922 SDValue Shift, SDValue X,
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000923 X86ISelAddressMode &AM) {
Chandler Carruth3dbcda82012-01-11 09:35:02 +0000924 if (Shift.getOpcode() != ISD::SRL || !Shift.hasOneUse() ||
925 !isa<ConstantSDNode>(Shift.getOperand(1)))
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000926 return true;
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000927
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000928 unsigned ShiftAmt = Shift.getConstantOperandVal(1);
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000929 unsigned MaskLZ = countLeadingZeros(Mask);
930 unsigned MaskTZ = countTrailingZeros(Mask);
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000931
932 // The amount of shift we're trying to fit into the addressing mode is taken
Chandler Carruth3dbcda82012-01-11 09:35:02 +0000933 // from the trailing zeros of the mask.
934 unsigned AMShiftAmt = MaskTZ;
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000935
936 // There is nothing we can do here unless the mask is removing some bits.
937 // Also, the addressing mode can only represent shifts of 1, 2, or 3 bits.
938 if (AMShiftAmt <= 0 || AMShiftAmt > 3) return true;
939
940 // We also need to ensure that mask is a continuous run of bits.
Benjamin Kramer5f6a9072015-02-12 15:35:40 +0000941 if (countTrailingOnes(Mask >> MaskTZ) + MaskTZ + MaskLZ != 64) return true;
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000942
943 // Scale the leading zero count down based on the actual size of the value.
Chandler Carruth3dbcda82012-01-11 09:35:02 +0000944 // Also scale it down based on the size of the shift.
Craig Topper83e042a2013-08-15 05:57:07 +0000945 MaskLZ -= (64 - X.getSimpleValueType().getSizeInBits()) + ShiftAmt;
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000946
947 // The final check is to ensure that any masked out high bits of X are
948 // already known to be zero. Otherwise, the mask has a semantic impact
949 // other than masking out a couple of low bits. Unfortunately, because of
950 // the mask, zero extensions will be removed from operands in some cases.
951 // This code works extra hard to look through extensions because we can
952 // replace them with zero extensions cheaply if necessary.
953 bool ReplacingAnyExtend = false;
954 if (X.getOpcode() == ISD::ANY_EXTEND) {
Craig Topper83e042a2013-08-15 05:57:07 +0000955 unsigned ExtendBits = X.getSimpleValueType().getSizeInBits() -
956 X.getOperand(0).getSimpleValueType().getSizeInBits();
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000957 // Assume that we'll replace the any-extend with a zero-extend, and
958 // narrow the search to the extended value.
959 X = X.getOperand(0);
960 MaskLZ = ExtendBits > MaskLZ ? 0 : MaskLZ - ExtendBits;
961 ReplacingAnyExtend = true;
962 }
Craig Topper83e042a2013-08-15 05:57:07 +0000963 APInt MaskedHighBits =
964 APInt::getHighBitsSet(X.getSimpleValueType().getSizeInBits(), MaskLZ);
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000965 APInt KnownZero, KnownOne;
Jay Foada0653a32014-05-14 21:14:37 +0000966 DAG.computeKnownBits(X, KnownZero, KnownOne);
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000967 if (MaskedHighBits != KnownZero) return true;
968
969 // We've identified a pattern that can be transformed into a single shift
970 // and an addressing mode. Make it so.
Craig Topper83e042a2013-08-15 05:57:07 +0000971 MVT VT = N.getSimpleValueType();
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000972 if (ReplacingAnyExtend) {
973 assert(X.getValueType() != VT);
974 // We looked through an ANY_EXTEND node, insert a ZERO_EXTEND.
Andrew Trickef9de2a2013-05-25 02:42:55 +0000975 SDValue NewX = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(X), VT, X);
Chandler Carruth3eacfb82012-01-11 11:04:36 +0000976 InsertDAGNode(DAG, N, NewX);
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000977 X = NewX;
978 }
Andrew Trickef9de2a2013-05-25 02:42:55 +0000979 SDLoc DL(N);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000980 SDValue NewSRLAmt = DAG.getConstant(ShiftAmt + AMShiftAmt, DL, MVT::i8);
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000981 SDValue NewSRL = DAG.getNode(ISD::SRL, DL, VT, X, NewSRLAmt);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000982 SDValue NewSHLAmt = DAG.getConstant(AMShiftAmt, DL, MVT::i8);
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000983 SDValue NewSHL = DAG.getNode(ISD::SHL, DL, VT, NewSRL, NewSHLAmt);
Chandler Carrutheb21da02012-01-12 01:34:44 +0000984
985 // Insert the new nodes into the topological ordering. We must do this in
986 // a valid topological ordering as nothing is going to go back and re-sort
987 // these nodes. We continually insert before 'N' in sequence as this is
988 // essentially a pre-flattened and pre-sorted sequence of nodes. There is no
989 // hierarchy left to express.
Chandler Carruth3eacfb82012-01-11 11:04:36 +0000990 InsertDAGNode(DAG, N, NewSRLAmt);
991 InsertDAGNode(DAG, N, NewSRL);
992 InsertDAGNode(DAG, N, NewSHLAmt);
993 InsertDAGNode(DAG, N, NewSHL);
Chandler Carruth55b2cde2012-01-11 08:41:08 +0000994 DAG.ReplaceAllUsesWith(N, NewSHL);
995
996 AM.Scale = 1 << AMShiftAmt;
997 AM.IndexReg = NewSRL;
998 return false;
999}
1000
Dan Gohman824ab402009-07-22 23:26:55 +00001001bool X86DAGToDAGISel::MatchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
1002 unsigned Depth) {
Andrew Trickef9de2a2013-05-25 02:42:55 +00001003 SDLoc dl(N);
Bill Wendlingfe3bdb42009-08-07 21:33:25 +00001004 DEBUG({
David Greenedbdb1b22010-01-05 01:29:08 +00001005 dbgs() << "MatchAddress: ";
Bill Wendlingfe3bdb42009-08-07 21:33:25 +00001006 AM.dump();
1007 });
Dan Gohmanccb36112007-08-13 20:03:06 +00001008 // Limit recursion.
1009 if (Depth > 5)
Rafael Espindola92773792009-03-31 16:16:57 +00001010 return MatchAddressBase(N, AM);
Anton Korobeynikov741ea0d2009-08-05 23:01:26 +00001011
Chris Lattnerfea81da2009-06-27 04:16:01 +00001012 // If this is already a %rip relative address, we can only merge immediates
1013 // into it. Instead of handling this in every case, we handle it here.
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001014 // RIP relative addressing: %rip + 32-bit displacement!
Chris Lattnerfea81da2009-06-27 04:16:01 +00001015 if (AM.isRIPRelative()) {
1016 // FIXME: JumpTable and ExternalSymbol address currently don't like
1017 // displacements. It isn't very important, but this should be fixed for
1018 // consistency.
Rafael Espindola36b718f2015-06-22 17:46:53 +00001019 if (!(AM.ES || AM.MCSym) && AM.JT != -1)
1020 return true;
Anton Korobeynikov741ea0d2009-08-05 23:01:26 +00001021
Eli Friedmanef67e7d2011-07-13 20:44:23 +00001022 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N))
1023 if (!FoldOffsetIntoAddress(Cst->getSExtValue(), AM))
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001024 return false;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001025 return true;
1026 }
1027
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001028 switch (N.getOpcode()) {
1029 default: break;
Reid Kleckner60381792015-07-07 22:25:32 +00001030 case ISD::LOCAL_RECOVER: {
Reid Kleckner9dad2272015-05-04 23:22:36 +00001031 if (!AM.hasSymbolicDisplacement() && AM.Disp == 0)
Rafael Espindola36b718f2015-06-22 17:46:53 +00001032 if (const auto *ESNode = dyn_cast<MCSymbolSDNode>(N.getOperand(0))) {
1033 // Use the symbol and don't prefix it.
1034 AM.MCSym = ESNode->getMCSymbol();
1035 return false;
1036 }
David Majnemer71b9b6b2015-03-05 18:50:12 +00001037 break;
1038 }
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001039 case ISD::Constant: {
Dan Gohman059c4fa2008-11-11 15:52:29 +00001040 uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
Eli Friedmanef67e7d2011-07-13 20:44:23 +00001041 if (!FoldOffsetIntoAddress(Val, AM))
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001042 return false;
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001043 break;
1044 }
Evan Cheng77d86ff2006-02-25 10:09:08 +00001045
Rafael Espindola6688b0a2009-04-12 21:55:03 +00001046 case X86ISD::Wrapper:
Chris Lattnerfea81da2009-06-27 04:16:01 +00001047 case X86ISD::WrapperRIP:
Rafael Espindola6688b0a2009-04-12 21:55:03 +00001048 if (!MatchWrapper(N, AM))
1049 return false;
Evan Cheng77d86ff2006-02-25 10:09:08 +00001050 break;
1051
Rafael Espindola3b2df102009-04-08 21:14:34 +00001052 case ISD::LOAD:
Chris Lattner8a236b62010-09-22 04:39:11 +00001053 if (!MatchLoadInAddress(cast<LoadSDNode>(N), AM))
Rafael Espindola3b2df102009-04-08 21:14:34 +00001054 return false;
1055 break;
1056
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001057 case ISD::FrameIndex:
Eli Friedman344ec792011-07-13 21:29:53 +00001058 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +00001059 AM.Base_Reg.getNode() == nullptr &&
Eli Friedman344ec792011-07-13 21:29:53 +00001060 (!Subtarget->is64Bit() || isDispSafeForFrameIndex(AM.Disp))) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001061 AM.BaseType = X86ISelAddressMode::FrameIndexBase;
Dan Gohman0fd54fb2010-04-29 23:30:41 +00001062 AM.Base_FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001063 return false;
1064 }
1065 break;
Evan Chengc9fab312005-12-08 02:01:35 +00001066
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001067 case ISD::SHL:
Craig Topper062a2ba2014-04-25 05:30:21 +00001068 if (AM.IndexReg.getNode() != nullptr || AM.Scale != 1)
Chris Lattnerff87f05e2007-12-08 07:22:58 +00001069 break;
Chad Rosier24c19d22012-08-01 18:39:17 +00001070
Gabor Greif81d6a382008-08-31 15:37:04 +00001071 if (ConstantSDNode
1072 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
Dan Gohmaneffb8942008-09-12 16:56:44 +00001073 unsigned Val = CN->getZExtValue();
Dan Gohman824ab402009-07-22 23:26:55 +00001074 // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
1075 // that the base operand remains free for further matching. If
1076 // the base doesn't end up getting used, a post-processing step
1077 // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
Chris Lattnerff87f05e2007-12-08 07:22:58 +00001078 if (Val == 1 || Val == 2 || Val == 3) {
1079 AM.Scale = 1 << Val;
Gabor Greiff304a7a2008-08-28 21:40:38 +00001080 SDValue ShVal = N.getNode()->getOperand(0);
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001081
Chris Lattnerff87f05e2007-12-08 07:22:58 +00001082 // Okay, we know that we have a scale by now. However, if the scaled
1083 // value is an add of something and a constant, we can fold the
1084 // constant into the disp field here.
Chris Lattner46c01a32011-02-13 22:25:43 +00001085 if (CurDAG->isBaseWithConstantOffset(ShVal)) {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001086 AM.IndexReg = ShVal.getNode()->getOperand(0);
Chris Lattnerff87f05e2007-12-08 07:22:58 +00001087 ConstantSDNode *AddVal =
Gabor Greiff304a7a2008-08-28 21:40:38 +00001088 cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
Richard Smith228e6d42012-08-24 23:29:28 +00001089 uint64_t Disp = (uint64_t)AddVal->getSExtValue() << Val;
Eli Friedmanef67e7d2011-07-13 20:44:23 +00001090 if (!FoldOffsetIntoAddress(Disp, AM))
1091 return false;
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001092 }
Eli Friedmanef67e7d2011-07-13 20:44:23 +00001093
1094 AM.IndexReg = ShVal;
Chris Lattnerff87f05e2007-12-08 07:22:58 +00001095 return false;
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001096 }
Chris Lattnerff87f05e2007-12-08 07:22:58 +00001097 }
Jakub Staszak43fafaf2013-01-04 23:01:26 +00001098 break;
Evan Chengc9fab312005-12-08 02:01:35 +00001099
Chandler Carruth3dbcda82012-01-11 09:35:02 +00001100 case ISD::SRL: {
1101 // Scale must not be used already.
Craig Topper062a2ba2014-04-25 05:30:21 +00001102 if (AM.IndexReg.getNode() != nullptr || AM.Scale != 1) break;
Chandler Carruth3dbcda82012-01-11 09:35:02 +00001103
1104 SDValue And = N.getOperand(0);
1105 if (And.getOpcode() != ISD::AND) break;
1106 SDValue X = And.getOperand(0);
1107
1108 // We only handle up to 64-bit values here as those are what matter for
1109 // addressing mode optimizations.
Craig Topper83e042a2013-08-15 05:57:07 +00001110 if (X.getSimpleValueType().getSizeInBits() > 64) break;
Chandler Carruth3dbcda82012-01-11 09:35:02 +00001111
1112 // The mask used for the transform is expected to be post-shift, but we
1113 // found the shift first so just apply the shift to the mask before passing
1114 // it down.
1115 if (!isa<ConstantSDNode>(N.getOperand(1)) ||
1116 !isa<ConstantSDNode>(And.getOperand(1)))
1117 break;
1118 uint64_t Mask = And.getConstantOperandVal(1) >> N.getConstantOperandVal(1);
1119
Chandler Carruth55b2cde2012-01-11 08:41:08 +00001120 // Try to fold the mask and shift into the scale, and return false if we
1121 // succeed.
Chandler Carruth3dbcda82012-01-11 09:35:02 +00001122 if (!FoldMaskAndShiftToScale(*CurDAG, N, Mask, N, X, AM))
Chandler Carruth55b2cde2012-01-11 08:41:08 +00001123 return false;
1124 break;
Chandler Carruth3dbcda82012-01-11 09:35:02 +00001125 }
Chandler Carruth55b2cde2012-01-11 08:41:08 +00001126
Dan Gohmanbf474952007-10-22 20:22:24 +00001127 case ISD::SMUL_LOHI:
1128 case ISD::UMUL_LOHI:
1129 // A mul_lohi where we need the low part can be folded as a plain multiply.
Gabor Greifabfdf922008-08-26 22:36:50 +00001130 if (N.getResNo() != 0) break;
Dan Gohmanbf474952007-10-22 20:22:24 +00001131 // FALL THROUGH
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001132 case ISD::MUL:
Evan Chenga84a3182009-03-30 21:36:47 +00001133 case X86ISD::MUL_IMM:
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001134 // X*[3,5,9] -> X+X*[2,4,8]
Dan Gohmanf14b77e2008-11-05 04:14:16 +00001135 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Craig Topper062a2ba2014-04-25 05:30:21 +00001136 AM.Base_Reg.getNode() == nullptr &&
1137 AM.IndexReg.getNode() == nullptr) {
Gabor Greif81d6a382008-08-31 15:37:04 +00001138 if (ConstantSDNode
1139 *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
Dan Gohmaneffb8942008-09-12 16:56:44 +00001140 if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
1141 CN->getZExtValue() == 9) {
1142 AM.Scale = unsigned(CN->getZExtValue())-1;
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001143
Gabor Greiff304a7a2008-08-28 21:40:38 +00001144 SDValue MulVal = N.getNode()->getOperand(0);
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001145 SDValue Reg;
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001146
1147 // Okay, we know that we have a scale by now. However, if the scaled
1148 // value is an add of something and a constant, we can fold the
1149 // constant into the disp field here.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001150 if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
1151 isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
1152 Reg = MulVal.getNode()->getOperand(0);
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001153 ConstantSDNode *AddVal =
Gabor Greiff304a7a2008-08-28 21:40:38 +00001154 cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
Eli Friedmanef67e7d2011-07-13 20:44:23 +00001155 uint64_t Disp = AddVal->getSExtValue() * CN->getZExtValue();
1156 if (FoldOffsetIntoAddress(Disp, AM))
Gabor Greiff304a7a2008-08-28 21:40:38 +00001157 Reg = N.getNode()->getOperand(0);
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001158 } else {
Gabor Greiff304a7a2008-08-28 21:40:38 +00001159 Reg = N.getNode()->getOperand(0);
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001160 }
1161
Dan Gohman0fd54fb2010-04-29 23:30:41 +00001162 AM.IndexReg = AM.Base_Reg = Reg;
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001163 return false;
1164 }
Chris Lattnerfe8c5302007-02-04 20:18:17 +00001165 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001166 break;
1167
Dan Gohmanfaf75c82009-05-11 18:02:53 +00001168 case ISD::SUB: {
1169 // Given A-B, if A can be completely folded into the address and
1170 // the index field with the index field unused, use -B as the index.
1171 // This is a win if a has multiple parts that can be folded into
1172 // the address. Also, this saves a mov if the base register has
1173 // other uses, since it avoids a two-address sub instruction, however
1174 // it costs an additional mov if the index register has other uses.
1175
Dan Gohman99ba4da2010-06-18 01:24:29 +00001176 // Add an artificial use to this node so that we can keep track of
1177 // it if it gets CSE'd with a different node.
1178 HandleSDNode Handle(N);
1179
Dan Gohmanfaf75c82009-05-11 18:02:53 +00001180 // Test if the LHS of the sub can be folded.
1181 X86ISelAddressMode Backup = AM;
Dan Gohman99ba4da2010-06-18 01:24:29 +00001182 if (MatchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
Dan Gohmanfaf75c82009-05-11 18:02:53 +00001183 AM = Backup;
1184 break;
1185 }
1186 // Test if the index field is free for use.
Chris Lattnerfea81da2009-06-27 04:16:01 +00001187 if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
Dan Gohmanfaf75c82009-05-11 18:02:53 +00001188 AM = Backup;
1189 break;
1190 }
Evan Cheng68333f52010-03-17 23:58:35 +00001191
Dan Gohmanfaf75c82009-05-11 18:02:53 +00001192 int Cost = 0;
Dan Gohman99ba4da2010-06-18 01:24:29 +00001193 SDValue RHS = Handle.getValue().getNode()->getOperand(1);
Dan Gohmanfaf75c82009-05-11 18:02:53 +00001194 // If the RHS involves a register with multiple uses, this
1195 // transformation incurs an extra mov, due to the neg instruction
1196 // clobbering its operand.
1197 if (!RHS.getNode()->hasOneUse() ||
1198 RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
1199 RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
1200 RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
1201 (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
Owen Anderson9f944592009-08-11 20:47:22 +00001202 RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
Dan Gohmanfaf75c82009-05-11 18:02:53 +00001203 ++Cost;
1204 // If the base is a register with multiple uses, this
1205 // transformation may save a mov.
1206 if ((AM.BaseType == X86ISelAddressMode::RegBase &&
Dan Gohman0fd54fb2010-04-29 23:30:41 +00001207 AM.Base_Reg.getNode() &&
1208 !AM.Base_Reg.getNode()->hasOneUse()) ||
Dan Gohmanfaf75c82009-05-11 18:02:53 +00001209 AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1210 --Cost;
1211 // If the folded LHS was interesting, this transformation saves
1212 // address arithmetic.
1213 if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
1214 ((AM.Disp != 0) && (Backup.Disp == 0)) +
1215 (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1216 --Cost;
1217 // If it doesn't look like it may be an overall win, don't do it.
1218 if (Cost >= 0) {
1219 AM = Backup;
1220 break;
1221 }
1222
1223 // Ok, the transformation is legal and appears profitable. Go for it.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001224 SDValue Zero = CurDAG->getConstant(0, dl, N.getValueType());
Dan Gohmanfaf75c82009-05-11 18:02:53 +00001225 SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1226 AM.IndexReg = Neg;
1227 AM.Scale = 1;
1228
1229 // Insert the new nodes into the topological ordering.
Chandler Carruth3eacfb82012-01-11 11:04:36 +00001230 InsertDAGNode(*CurDAG, N, Zero);
1231 InsertDAGNode(*CurDAG, N, Neg);
Dan Gohmanfaf75c82009-05-11 18:02:53 +00001232 return false;
1233 }
1234
Evan Chengbf38a5e2009-01-17 07:09:27 +00001235 case ISD::ADD: {
Dan Gohman99ba4da2010-06-18 01:24:29 +00001236 // Add an artificial use to this node so that we can keep track of
1237 // it if it gets CSE'd with a different node.
1238 HandleSDNode Handle(N);
Dan Gohman99ba4da2010-06-18 01:24:29 +00001239
Evan Chengbf38a5e2009-01-17 07:09:27 +00001240 X86ISelAddressMode Backup = AM;
Chris Lattner35a2e652011-01-16 08:48:11 +00001241 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
1242 !MatchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1))
Dan Gohman99ba4da2010-06-18 01:24:29 +00001243 return false;
1244 AM = Backup;
Chad Rosier24c19d22012-08-01 18:39:17 +00001245
Evan Cheng68333f52010-03-17 23:58:35 +00001246 // Try again after commuting the operands.
Chris Lattner35a2e652011-01-16 08:48:11 +00001247 if (!MatchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1)&&
1248 !MatchAddressRecursively(Handle.getValue().getOperand(0), AM, Depth+1))
Dan Gohman99ba4da2010-06-18 01:24:29 +00001249 return false;
Evan Chengbf38a5e2009-01-17 07:09:27 +00001250 AM = Backup;
Dan Gohmana1d92422009-03-13 02:25:09 +00001251
1252 // If we couldn't fold both operands into the address at the same time,
1253 // see if we can just put each operand into a register and fold at least
1254 // the add.
1255 if (AM.BaseType == X86ISelAddressMode::RegBase &&
Dan Gohman0fd54fb2010-04-29 23:30:41 +00001256 !AM.Base_Reg.getNode() &&
Chris Lattnerfea81da2009-06-27 04:16:01 +00001257 !AM.IndexReg.getNode()) {
Chris Lattner35a2e652011-01-16 08:48:11 +00001258 N = Handle.getValue();
1259 AM.Base_Reg = N.getOperand(0);
1260 AM.IndexReg = N.getOperand(1);
Dan Gohmana1d92422009-03-13 02:25:09 +00001261 AM.Scale = 1;
1262 return false;
1263 }
Chris Lattner35a2e652011-01-16 08:48:11 +00001264 N = Handle.getValue();
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001265 break;
Evan Chengbf38a5e2009-01-17 07:09:27 +00001266 }
Evan Cheng734e1e22006-05-30 06:59:36 +00001267
Chris Lattnerfe8c5302007-02-04 20:18:17 +00001268 case ISD::OR:
Sylvestre Ledru91ce36c2012-09-27 10:14:43 +00001269 // Handle "X | C" as "X + C" iff X is known to have C bits clear.
Chris Lattner46c01a32011-02-13 22:25:43 +00001270 if (CurDAG->isBaseWithConstantOffset(N)) {
Chris Lattnerff87f05e2007-12-08 07:22:58 +00001271 X86ISelAddressMode Backup = AM;
Chris Lattner84776782010-04-20 23:18:40 +00001272 ConstantSDNode *CN = cast<ConstantSDNode>(N.getOperand(1));
Evan Cheng68333f52010-03-17 23:58:35 +00001273
Chris Lattnerff87f05e2007-12-08 07:22:58 +00001274 // Start with the LHS as an addr mode.
Dan Gohman99ba4da2010-06-18 01:24:29 +00001275 if (!MatchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
Eli Friedmanef67e7d2011-07-13 20:44:23 +00001276 !FoldOffsetIntoAddress(CN->getSExtValue(), AM))
Chris Lattnerff87f05e2007-12-08 07:22:58 +00001277 return false;
Chris Lattnerff87f05e2007-12-08 07:22:58 +00001278 AM = Backup;
Evan Cheng734e1e22006-05-30 06:59:36 +00001279 }
1280 break;
Chad Rosier24c19d22012-08-01 18:39:17 +00001281
Evan Cheng827d30d2007-12-13 00:43:27 +00001282 case ISD::AND: {
Dan Gohman57d6bd32009-04-13 16:09:41 +00001283 // Perform some heroic transforms on an and of a constant-count shift
1284 // with a constant to enable use of the scaled offset field.
1285
Evan Cheng827d30d2007-12-13 00:43:27 +00001286 // Scale must not be used already.
Craig Topper062a2ba2014-04-25 05:30:21 +00001287 if (AM.IndexReg.getNode() != nullptr || AM.Scale != 1) break;
Evan Chenga20a7732008-02-07 08:53:49 +00001288
Chandler Carruthaa01e662012-01-11 09:35:00 +00001289 SDValue Shift = N.getOperand(0);
1290 if (Shift.getOpcode() != ISD::SRL && Shift.getOpcode() != ISD::SHL) break;
Dan Gohman57d6bd32009-04-13 16:09:41 +00001291 SDValue X = Shift.getOperand(0);
Chandler Carruthaa01e662012-01-11 09:35:00 +00001292
1293 // We only handle up to 64-bit values here as those are what matter for
1294 // addressing mode optimizations.
Craig Topper83e042a2013-08-15 05:57:07 +00001295 if (X.getSimpleValueType().getSizeInBits() > 64) break;
Chandler Carruthaa01e662012-01-11 09:35:00 +00001296
Chandler Carruthb0049f42012-01-11 09:35:04 +00001297 if (!isa<ConstantSDNode>(N.getOperand(1)))
1298 break;
1299 uint64_t Mask = N.getConstantOperandVal(1);
Evan Cheng827d30d2007-12-13 00:43:27 +00001300
Chandler Carruth51d30762012-01-11 08:48:20 +00001301 // Try to fold the mask and shift into an extract and scale.
Chandler Carruthb0049f42012-01-11 09:35:04 +00001302 if (!FoldMaskAndShiftToExtract(*CurDAG, N, Mask, Shift, X, AM))
Chandler Carruth51d30762012-01-11 08:48:20 +00001303 return false;
Dan Gohman57d6bd32009-04-13 16:09:41 +00001304
Chandler Carruth51d30762012-01-11 08:48:20 +00001305 // Try to fold the mask and shift directly into the scale.
Chandler Carruthb0049f42012-01-11 09:35:04 +00001306 if (!FoldMaskAndShiftToScale(*CurDAG, N, Mask, Shift, X, AM))
Chandler Carruth55b2cde2012-01-11 08:41:08 +00001307 return false;
1308
Chandler Carruthaa01e662012-01-11 09:35:00 +00001309 // Try to swap the mask and shift to place shifts which can be done as
1310 // a scale on the outside of the mask.
Chandler Carruthb0049f42012-01-11 09:35:04 +00001311 if (!FoldMaskedShiftToScaledMask(*CurDAG, N, Mask, Shift, X, AM))
Chandler Carruthaa01e662012-01-11 09:35:00 +00001312 return false;
1313 break;
Evan Cheng827d30d2007-12-13 00:43:27 +00001314 }
Evan Cheng734e1e22006-05-30 06:59:36 +00001315 }
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001316
Rafael Espindola92773792009-03-31 16:16:57 +00001317 return MatchAddressBase(N, AM);
Dan Gohmanccb36112007-08-13 20:03:06 +00001318}
1319
1320/// MatchAddressBase - Helper for MatchAddress. Add the specified node to the
1321/// specified addressing mode without any further recursion.
Rafael Espindola92773792009-03-31 16:16:57 +00001322bool X86DAGToDAGISel::MatchAddressBase(SDValue N, X86ISelAddressMode &AM) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001323 // Is the base register already occupied?
Dan Gohman0fd54fb2010-04-29 23:30:41 +00001324 if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base_Reg.getNode()) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001325 // If so, check to see if the scale index register is set.
Craig Topper062a2ba2014-04-25 05:30:21 +00001326 if (!AM.IndexReg.getNode()) {
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001327 AM.IndexReg = N;
1328 AM.Scale = 1;
1329 return false;
1330 }
1331
1332 // Otherwise, we cannot select it.
1333 return true;
1334 }
1335
1336 // Default, generate it as a register.
1337 AM.BaseType = X86ISelAddressMode::RegBase;
Dan Gohman0fd54fb2010-04-29 23:30:41 +00001338 AM.Base_Reg = N;
Chris Lattner3f0f71b2005-11-19 02:11:08 +00001339 return false;
1340}
1341
Elena Demikhovskye1eda8a2015-04-30 08:38:48 +00001342bool X86DAGToDAGISel::SelectVectorAddr(SDNode *Parent, SDValue N, SDValue &Base,
1343 SDValue &Scale, SDValue &Index,
1344 SDValue &Disp, SDValue &Segment) {
1345
1346 MaskedGatherScatterSDNode *Mgs = dyn_cast<MaskedGatherScatterSDNode>(Parent);
1347 if (!Mgs)
1348 return false;
1349 X86ISelAddressMode AM;
1350 unsigned AddrSpace = Mgs->getPointerInfo().getAddrSpace();
1351 // AddrSpace 256 -> GS, 257 -> FS.
1352 if (AddrSpace == 256)
1353 AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16);
1354 if (AddrSpace == 257)
1355 AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16);
1356
1357 SDLoc DL(N);
1358 Base = Mgs->getBasePtr();
1359 Index = Mgs->getIndex();
1360 unsigned ScalarSize = Mgs->getValue().getValueType().getScalarSizeInBits();
1361 Scale = getI8Imm(ScalarSize/8, DL);
1362
1363 // If Base is 0, the whole address is in index and the Scale is 1
Daniel Jasper232778a2015-04-30 09:01:21 +00001364 if (isa<ConstantSDNode>(Base)) {
1365 assert(dyn_cast<ConstantSDNode>(Base)->isNullValue() &&
1366 "Unexpected base in gather/scatter");
Elena Demikhovskye1eda8a2015-04-30 08:38:48 +00001367 Scale = getI8Imm(1, DL);
1368 Base = CurDAG->getRegister(0, MVT::i32);
1369 }
1370 if (AM.Segment.getNode())
1371 Segment = AM.Segment;
1372 else
1373 Segment = CurDAG->getRegister(0, MVT::i32);
1374 Disp = CurDAG->getTargetConstant(0, DL, MVT::i32);
1375 return true;
1376}
1377
Evan Chengc9fab312005-12-08 02:01:35 +00001378/// SelectAddr - returns true if it is able pattern match an addressing mode.
1379/// It returns the operands which make up the maximal addressing mode it can
1380/// match by reference.
Chris Lattnerd58d7c12010-09-21 22:07:31 +00001381///
1382/// Parent is the parent node of the addr operand that is being matched. It
1383/// is always a load, store, atomic node, or null. It is only null when
1384/// checking memory operands for inline asm nodes.
1385bool X86DAGToDAGISel::SelectAddr(SDNode *Parent, SDValue N, SDValue &Base,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001386 SDValue &Scale, SDValue &Index,
Rafael Espindola3b2df102009-04-08 21:14:34 +00001387 SDValue &Disp, SDValue &Segment) {
Evan Chengc9fab312005-12-08 02:01:35 +00001388 X86ISelAddressMode AM;
Chad Rosier24c19d22012-08-01 18:39:17 +00001389
Chris Lattner8a236b62010-09-22 04:39:11 +00001390 if (Parent &&
1391 // This list of opcodes are all the nodes that have an "addr:$ptr" operand
1392 // that are not a MemSDNode, and thus don't have proper addrspace info.
Chris Lattner8a236b62010-09-22 04:39:11 +00001393 Parent->getOpcode() != ISD::INTRINSIC_W_CHAIN && // unaligned loads, fixme
Eric Christopherc1b3e072010-09-22 20:42:08 +00001394 Parent->getOpcode() != ISD::INTRINSIC_VOID && // nontemporal stores
Michael Liao97bf3632012-10-15 22:39:43 +00001395 Parent->getOpcode() != X86ISD::TLSCALL && // Fixme
1396 Parent->getOpcode() != X86ISD::EH_SJLJ_SETJMP && // setjmp
1397 Parent->getOpcode() != X86ISD::EH_SJLJ_LONGJMP) { // longjmp
Chris Lattner8a236b62010-09-22 04:39:11 +00001398 unsigned AddrSpace =
1399 cast<MemSDNode>(Parent)->getPointerInfo().getAddrSpace();
1400 // AddrSpace 256 -> GS, 257 -> FS.
1401 if (AddrSpace == 256)
1402 AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16);
1403 if (AddrSpace == 257)
1404 AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16);
1405 }
Chad Rosier24c19d22012-08-01 18:39:17 +00001406
Evan Cheng3dfd04e2009-12-18 01:59:21 +00001407 if (MatchAddress(N, AM))
Evan Chengbc7a0f442006-01-11 06:09:51 +00001408 return false;
Evan Chengc9fab312005-12-08 02:01:35 +00001409
Craig Topper83e042a2013-08-15 05:57:07 +00001410 MVT VT = N.getSimpleValueType();
Evan Chengbc7a0f442006-01-11 06:09:51 +00001411 if (AM.BaseType == X86ISelAddressMode::RegBase) {
Dan Gohman0fd54fb2010-04-29 23:30:41 +00001412 if (!AM.Base_Reg.getNode())
1413 AM.Base_Reg = CurDAG->getRegister(0, VT);
Evan Chengc9fab312005-12-08 02:01:35 +00001414 }
Evan Chengbc7a0f442006-01-11 06:09:51 +00001415
Gabor Greiff304a7a2008-08-28 21:40:38 +00001416 if (!AM.IndexReg.getNode())
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001417 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Chengbc7a0f442006-01-11 06:09:51 +00001418
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001419 getAddressOperands(AM, SDLoc(N), Base, Scale, Index, Disp, Segment);
Evan Chengbc7a0f442006-01-11 06:09:51 +00001420 return true;
Evan Chengc9fab312005-12-08 02:01:35 +00001421}
1422
Chris Lattner398195e2006-10-07 21:55:32 +00001423/// SelectScalarSSELoad - Match a scalar SSE load. In particular, we want to
1424/// match a load whose top elements are either undef or zeros. The load flavor
1425/// is derived from the type of N, which is either v4f32 or v2f64.
Chris Lattner3f482152010-02-17 06:07:47 +00001426///
1427/// We also return:
Chris Lattner18a32ce2010-02-21 03:17:59 +00001428/// PatternChainNode: this is the matched node that has a chain input and
1429/// output.
Chris Lattnerbd6e1932010-03-01 22:51:11 +00001430bool X86DAGToDAGISel::SelectScalarSSELoad(SDNode *Root,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001431 SDValue N, SDValue &Base,
1432 SDValue &Scale, SDValue &Index,
Rafael Espindola3b2df102009-04-08 21:14:34 +00001433 SDValue &Disp, SDValue &Segment,
Chris Lattner18a32ce2010-02-21 03:17:59 +00001434 SDValue &PatternNodeWithChain) {
Chris Lattner398195e2006-10-07 21:55:32 +00001435 if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
Chris Lattner18a32ce2010-02-21 03:17:59 +00001436 PatternNodeWithChain = N.getOperand(0);
1437 if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) &&
1438 PatternNodeWithChain.hasOneUse() &&
Chris Lattner3c29aff2010-02-21 04:53:34 +00001439 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
Dan Gohman21cea8a2010-04-17 15:26:15 +00001440 IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
Chris Lattner18a32ce2010-02-21 03:17:59 +00001441 LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain);
Chris Lattnerd58d7c12010-09-21 22:07:31 +00001442 if (!SelectAddr(LD, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Chris Lattner398195e2006-10-07 21:55:32 +00001443 return false;
1444 return true;
1445 }
1446 }
Chris Lattnerd5fcfaa2006-10-11 22:09:58 +00001447
1448 // Also handle the case where we explicitly require zeros in the top
Chris Lattner398195e2006-10-07 21:55:32 +00001449 // elements. This is a vector shuffle from the zero vector.
Gabor Greiff304a7a2008-08-28 21:40:38 +00001450 if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
Chris Lattner5728bdd2007-11-25 00:24:49 +00001451 // Check to see if the top elements are all zeros (or bitcast of zeros).
Chad Rosier24c19d22012-08-01 18:39:17 +00001452 N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
Gabor Greiff304a7a2008-08-28 21:40:38 +00001453 N.getOperand(0).getNode()->hasOneUse() &&
1454 ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
Chris Lattnerafac7dad2010-02-16 22:35:06 +00001455 N.getOperand(0).getOperand(0).hasOneUse() &&
1456 IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
Dan Gohman21cea8a2010-04-17 15:26:15 +00001457 IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
Evan Cheng78af38c2008-05-08 00:57:18 +00001458 // Okay, this is a zero extending load. Fold it.
1459 LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
Chris Lattnerd58d7c12010-09-21 22:07:31 +00001460 if (!SelectAddr(LD, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
Evan Cheng78af38c2008-05-08 00:57:18 +00001461 return false;
Chris Lattner18a32ce2010-02-21 03:17:59 +00001462 PatternNodeWithChain = SDValue(LD, 0);
Evan Cheng78af38c2008-05-08 00:57:18 +00001463 return true;
Chris Lattnerd5fcfaa2006-10-11 22:09:58 +00001464 }
Chris Lattner398195e2006-10-07 21:55:32 +00001465 return false;
1466}
1467
1468
Tim Northover3a1fd4c2013-06-01 09:55:14 +00001469bool X86DAGToDAGISel::SelectMOV64Imm32(SDValue N, SDValue &Imm) {
1470 if (const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1471 uint64_t ImmVal = CN->getZExtValue();
1472 if ((uint32_t)ImmVal != (uint64_t)ImmVal)
1473 return false;
1474
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001475 Imm = CurDAG->getTargetConstant(ImmVal, SDLoc(N), MVT::i64);
Tim Northover3a1fd4c2013-06-01 09:55:14 +00001476 return true;
1477 }
1478
1479 // In static codegen with small code model, we can get the address of a label
1480 // into a register with 'movl'. TableGen has already made sure we're looking
1481 // at a label of some kind.
Tim Northover6833e3f2013-06-10 20:43:49 +00001482 assert(N->getOpcode() == X86ISD::Wrapper &&
1483 "Unexpected node type for MOV32ri64");
Tim Northover3a1fd4c2013-06-01 09:55:14 +00001484 N = N.getOperand(0);
1485
1486 if (N->getOpcode() != ISD::TargetConstantPool &&
1487 N->getOpcode() != ISD::TargetJumpTable &&
1488 N->getOpcode() != ISD::TargetGlobalAddress &&
1489 N->getOpcode() != ISD::TargetExternalSymbol &&
Rafael Espindola36b718f2015-06-22 17:46:53 +00001490 N->getOpcode() != ISD::MCSymbol &&
Tim Northover3a1fd4c2013-06-01 09:55:14 +00001491 N->getOpcode() != ISD::TargetBlockAddress)
1492 return false;
1493
1494 Imm = N;
1495 return TM.getCodeModel() == CodeModel::Small;
1496}
1497
Tim Northover6833e3f2013-06-10 20:43:49 +00001498bool X86DAGToDAGISel::SelectLEA64_32Addr(SDValue N, SDValue &Base,
1499 SDValue &Scale, SDValue &Index,
1500 SDValue &Disp, SDValue &Segment) {
1501 if (!SelectLEAAddr(N, Base, Scale, Index, Disp, Segment))
1502 return false;
1503
1504 SDLoc DL(N);
1505 RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Base);
1506 if (RN && RN->getReg() == 0)
1507 Base = CurDAG->getRegister(0, MVT::i64);
Pavel Chupin01a4e0a2014-08-20 11:59:22 +00001508 else if (Base.getValueType() == MVT::i32 && !dyn_cast<FrameIndexSDNode>(Base)) {
Tim Northover6833e3f2013-06-10 20:43:49 +00001509 // Base could already be %rip, particularly in the x32 ABI.
1510 Base = SDValue(CurDAG->getMachineNode(
1511 TargetOpcode::SUBREG_TO_REG, DL, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001512 CurDAG->getTargetConstant(0, DL, MVT::i64),
Tim Northover6833e3f2013-06-10 20:43:49 +00001513 Base,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001514 CurDAG->getTargetConstant(X86::sub_32bit, DL, MVT::i32)),
Tim Northover6833e3f2013-06-10 20:43:49 +00001515 0);
1516 }
1517
1518 RN = dyn_cast<RegisterSDNode>(Index);
1519 if (RN && RN->getReg() == 0)
1520 Index = CurDAG->getRegister(0, MVT::i64);
1521 else {
1522 assert(Index.getValueType() == MVT::i32 &&
1523 "Expect to be extending 32-bit registers for use in LEA");
1524 Index = SDValue(CurDAG->getMachineNode(
1525 TargetOpcode::SUBREG_TO_REG, DL, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001526 CurDAG->getTargetConstant(0, DL, MVT::i64),
Tim Northover6833e3f2013-06-10 20:43:49 +00001527 Index,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001528 CurDAG->getTargetConstant(X86::sub_32bit, DL,
1529 MVT::i32)),
Tim Northover6833e3f2013-06-10 20:43:49 +00001530 0);
1531 }
1532
1533 return true;
1534}
1535
Evan Cheng77d86ff2006-02-25 10:09:08 +00001536/// SelectLEAAddr - it calls SelectAddr and determines if the maximal addressing
1537/// mode it matches can be cost effectively emitted as an LEA instruction.
Chris Lattner0e023ea2010-09-21 20:31:19 +00001538bool X86DAGToDAGISel::SelectLEAAddr(SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001539 SDValue &Base, SDValue &Scale,
Chris Lattnerf4693072010-07-08 23:46:44 +00001540 SDValue &Index, SDValue &Disp,
1541 SDValue &Segment) {
Evan Cheng77d86ff2006-02-25 10:09:08 +00001542 X86ISelAddressMode AM;
Rafael Espindolabb834f02009-04-10 10:09:34 +00001543
1544 // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1545 // segments.
1546 SDValue Copy = AM.Segment;
Owen Anderson9f944592009-08-11 20:47:22 +00001547 SDValue T = CurDAG->getRegister(0, MVT::i32);
Rafael Espindolabb834f02009-04-10 10:09:34 +00001548 AM.Segment = T;
Evan Cheng77d86ff2006-02-25 10:09:08 +00001549 if (MatchAddress(N, AM))
1550 return false;
Rafael Espindolabb834f02009-04-10 10:09:34 +00001551 assert (T == AM.Segment);
1552 AM.Segment = Copy;
Rafael Espindola3b2df102009-04-08 21:14:34 +00001553
Craig Topper83e042a2013-08-15 05:57:07 +00001554 MVT VT = N.getSimpleValueType();
Evan Cheng77d86ff2006-02-25 10:09:08 +00001555 unsigned Complexity = 0;
1556 if (AM.BaseType == X86ISelAddressMode::RegBase)
Dan Gohman0fd54fb2010-04-29 23:30:41 +00001557 if (AM.Base_Reg.getNode())
Evan Cheng77d86ff2006-02-25 10:09:08 +00001558 Complexity = 1;
1559 else
Dan Gohman0fd54fb2010-04-29 23:30:41 +00001560 AM.Base_Reg = CurDAG->getRegister(0, VT);
Evan Cheng77d86ff2006-02-25 10:09:08 +00001561 else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1562 Complexity = 4;
1563
Gabor Greiff304a7a2008-08-28 21:40:38 +00001564 if (AM.IndexReg.getNode())
Evan Cheng77d86ff2006-02-25 10:09:08 +00001565 Complexity++;
1566 else
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001567 AM.IndexReg = CurDAG->getRegister(0, VT);
Evan Cheng77d86ff2006-02-25 10:09:08 +00001568
Chris Lattner3e1d9172007-03-20 06:08:29 +00001569 // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1570 // a simple shift.
1571 if (AM.Scale > 1)
Evan Cheng990c3602006-02-28 21:13:57 +00001572 Complexity++;
Evan Cheng77d86ff2006-02-25 10:09:08 +00001573
1574 // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1575 // to a LEA. This is determined with some expermentation but is by no means
1576 // optimal (especially for code size consideration). LEA is nice because of
1577 // its three-address nature. Tweak the cost function again when we can run
1578 // convertToThreeAddress() at register allocation time.
Dan Gohman4e3e3de2009-02-07 00:43:41 +00001579 if (AM.hasSymbolicDisplacement()) {
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001580 // For X86-64, we should always use lea to materialize RIP relative
1581 // addresses.
Evan Cheng47e181c2006-12-05 22:03:40 +00001582 if (Subtarget->is64Bit())
Evan Cheng11b0a5d2006-09-08 06:48:29 +00001583 Complexity = 4;
1584 else
1585 Complexity += 2;
1586 }
Evan Cheng77d86ff2006-02-25 10:09:08 +00001587
Dan Gohman0fd54fb2010-04-29 23:30:41 +00001588 if (AM.Disp && (AM.Base_Reg.getNode() || AM.IndexReg.getNode()))
Evan Cheng77d86ff2006-02-25 10:09:08 +00001589 Complexity++;
1590
Chris Lattner4d10f1a2009-07-11 22:50:33 +00001591 // If it isn't worth using an LEA, reject it.
Chris Lattner48cee9b2009-07-11 23:07:30 +00001592 if (Complexity <= 2)
Chris Lattner4d10f1a2009-07-11 22:50:33 +00001593 return false;
Chad Rosier24c19d22012-08-01 18:39:17 +00001594
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001595 getAddressOperands(AM, SDLoc(N), Base, Scale, Index, Disp, Segment);
Chris Lattner4d10f1a2009-07-11 22:50:33 +00001596 return true;
Evan Cheng77d86ff2006-02-25 10:09:08 +00001597}
1598
Chris Lattner7d2b0492009-06-20 20:38:48 +00001599/// SelectTLSADDRAddr - This is only run on TargetGlobalTLSAddress nodes.
Chris Lattner0e023ea2010-09-21 20:31:19 +00001600bool X86DAGToDAGISel::SelectTLSADDRAddr(SDValue N, SDValue &Base,
Chris Lattner7d2b0492009-06-20 20:38:48 +00001601 SDValue &Scale, SDValue &Index,
Chris Lattnerf4693072010-07-08 23:46:44 +00001602 SDValue &Disp, SDValue &Segment) {
Chris Lattner7d2b0492009-06-20 20:38:48 +00001603 assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1604 const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
Chad Rosier24c19d22012-08-01 18:39:17 +00001605
Chris Lattner7d2b0492009-06-20 20:38:48 +00001606 X86ISelAddressMode AM;
1607 AM.GV = GA->getGlobal();
1608 AM.Disp += GA->getOffset();
Dan Gohman0fd54fb2010-04-29 23:30:41 +00001609 AM.Base_Reg = CurDAG->getRegister(0, N.getValueType());
Chris Lattner899abc42009-06-26 21:18:37 +00001610 AM.SymbolFlags = GA->getTargetFlags();
1611
Owen Anderson9f944592009-08-11 20:47:22 +00001612 if (N.getValueType() == MVT::i32) {
Chris Lattner7d2b0492009-06-20 20:38:48 +00001613 AM.Scale = 1;
Owen Anderson9f944592009-08-11 20:47:22 +00001614 AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
Chris Lattner7d2b0492009-06-20 20:38:48 +00001615 } else {
Owen Anderson9f944592009-08-11 20:47:22 +00001616 AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
Chris Lattner7d2b0492009-06-20 20:38:48 +00001617 }
Chad Rosier24c19d22012-08-01 18:39:17 +00001618
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001619 getAddressOperands(AM, SDLoc(N), Base, Scale, Index, Disp, Segment);
Chris Lattner7d2b0492009-06-20 20:38:48 +00001620 return true;
1621}
1622
1623
Dan Gohmanea6f91f2010-01-05 01:24:18 +00001624bool X86DAGToDAGISel::TryFoldLoad(SDNode *P, SDValue N,
Dan Gohman2ce6f2a2008-07-27 21:46:04 +00001625 SDValue &Base, SDValue &Scale,
Rafael Espindola3b2df102009-04-08 21:14:34 +00001626 SDValue &Index, SDValue &Disp,
1627 SDValue &Segment) {
Chris Lattnerdd030702010-03-02 22:20:06 +00001628 if (!ISD::isNON_EXTLoad(N.getNode()) ||
1629 !IsProfitableToFold(N, P, P) ||
Dan Gohman21cea8a2010-04-17 15:26:15 +00001630 !IsLegalToFold(N, P, P, OptLevel))
Chris Lattnerdd030702010-03-02 22:20:06 +00001631 return false;
Chad Rosier24c19d22012-08-01 18:39:17 +00001632
Chris Lattnerd58d7c12010-09-21 22:07:31 +00001633 return SelectAddr(N.getNode(),
1634 N.getOperand(1), Base, Scale, Index, Disp, Segment);
Evan Cheng10d27902006-01-06 20:36:21 +00001635}
1636
Dan Gohman24300732008-09-23 18:22:58 +00001637/// getGlobalBaseReg - Return an SDNode that returns the value of
1638/// the global base register. Output instructions required to
1639/// initialize the global base register, if necessary.
Evan Cheng5588de92006-02-18 00:15:05 +00001640///
Evan Cheng61413a32006-08-26 05:34:46 +00001641SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
Dan Gohman4751bb92009-06-03 20:20:00 +00001642 unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
Mehdi Amini44ede332015-07-09 02:09:04 +00001643 auto &DL = MF->getDataLayout();
1644 return CurDAG->getRegister(GlobalBaseReg, TLI->getPointerTy(DL)).getNode();
Evan Cheng5588de92006-02-18 00:15:05 +00001645}
1646
Michael Liao83725392012-09-19 19:36:58 +00001647/// Atomic opcode table
1648///
Eric Christophereb47a2a2011-05-17 07:47:55 +00001649enum AtomicOpc {
Michael Liao83725392012-09-19 19:36:58 +00001650 ADD,
1651 SUB,
1652 INC,
1653 DEC,
Eric Christopherabfe3132011-05-17 07:50:41 +00001654 OR,
Eric Christophera1d9e292011-05-17 08:10:18 +00001655 AND,
1656 XOR,
Eric Christopherabfe3132011-05-17 07:50:41 +00001657 AtomicOpcEnd
Eric Christophereb47a2a2011-05-17 07:47:55 +00001658};
1659
1660enum AtomicSz {
1661 ConstantI8,
1662 I8,
1663 SextConstantI16,
1664 ConstantI16,
1665 I16,
1666 SextConstantI32,
1667 ConstantI32,
1668 I32,
1669 SextConstantI64,
1670 ConstantI64,
Eric Christopherabfe3132011-05-17 07:50:41 +00001671 I64,
1672 AtomicSzEnd
Eric Christophereb47a2a2011-05-17 07:47:55 +00001673};
1674
Craig Topper2dac9622012-03-09 07:45:21 +00001675static const uint16_t AtomicOpcTbl[AtomicOpcEnd][AtomicSzEnd] = {
Eric Christopher2a9dbbb2011-05-11 21:44:58 +00001676 {
Michael Liao83725392012-09-19 19:36:58 +00001677 X86::LOCK_ADD8mi,
1678 X86::LOCK_ADD8mr,
1679 X86::LOCK_ADD16mi8,
1680 X86::LOCK_ADD16mi,
1681 X86::LOCK_ADD16mr,
1682 X86::LOCK_ADD32mi8,
1683 X86::LOCK_ADD32mi,
1684 X86::LOCK_ADD32mr,
1685 X86::LOCK_ADD64mi8,
1686 X86::LOCK_ADD64mi32,
1687 X86::LOCK_ADD64mr,
1688 },
1689 {
1690 X86::LOCK_SUB8mi,
1691 X86::LOCK_SUB8mr,
1692 X86::LOCK_SUB16mi8,
1693 X86::LOCK_SUB16mi,
1694 X86::LOCK_SUB16mr,
1695 X86::LOCK_SUB32mi8,
1696 X86::LOCK_SUB32mi,
1697 X86::LOCK_SUB32mr,
1698 X86::LOCK_SUB64mi8,
1699 X86::LOCK_SUB64mi32,
1700 X86::LOCK_SUB64mr,
1701 },
1702 {
1703 0,
1704 X86::LOCK_INC8m,
1705 0,
1706 0,
1707 X86::LOCK_INC16m,
1708 0,
1709 0,
1710 X86::LOCK_INC32m,
1711 0,
1712 0,
1713 X86::LOCK_INC64m,
1714 },
1715 {
1716 0,
1717 X86::LOCK_DEC8m,
1718 0,
1719 0,
1720 X86::LOCK_DEC16m,
1721 0,
1722 0,
1723 X86::LOCK_DEC32m,
1724 0,
1725 0,
1726 X86::LOCK_DEC64m,
1727 },
1728 {
Eric Christopher2a9dbbb2011-05-11 21:44:58 +00001729 X86::LOCK_OR8mi,
1730 X86::LOCK_OR8mr,
1731 X86::LOCK_OR16mi8,
1732 X86::LOCK_OR16mi,
1733 X86::LOCK_OR16mr,
1734 X86::LOCK_OR32mi8,
1735 X86::LOCK_OR32mi,
1736 X86::LOCK_OR32mr,
1737 X86::LOCK_OR64mi8,
1738 X86::LOCK_OR64mi32,
Michael Liao83725392012-09-19 19:36:58 +00001739 X86::LOCK_OR64mr,
Eric Christophera1d9e292011-05-17 08:10:18 +00001740 },
1741 {
1742 X86::LOCK_AND8mi,
1743 X86::LOCK_AND8mr,
1744 X86::LOCK_AND16mi8,
1745 X86::LOCK_AND16mi,
1746 X86::LOCK_AND16mr,
1747 X86::LOCK_AND32mi8,
1748 X86::LOCK_AND32mi,
1749 X86::LOCK_AND32mr,
1750 X86::LOCK_AND64mi8,
1751 X86::LOCK_AND64mi32,
Michael Liao83725392012-09-19 19:36:58 +00001752 X86::LOCK_AND64mr,
Eric Christophera1d9e292011-05-17 08:10:18 +00001753 },
1754 {
1755 X86::LOCK_XOR8mi,
1756 X86::LOCK_XOR8mr,
1757 X86::LOCK_XOR16mi8,
1758 X86::LOCK_XOR16mi,
1759 X86::LOCK_XOR16mr,
1760 X86::LOCK_XOR32mi8,
1761 X86::LOCK_XOR32mi,
1762 X86::LOCK_XOR32mr,
1763 X86::LOCK_XOR64mi8,
1764 X86::LOCK_XOR64mi32,
Michael Liao83725392012-09-19 19:36:58 +00001765 X86::LOCK_XOR64mr,
Eric Christopher2a9dbbb2011-05-11 21:44:58 +00001766 }
1767};
1768
Michael Liao83725392012-09-19 19:36:58 +00001769// Return the target constant operand for atomic-load-op and do simple
1770// translations, such as from atomic-load-add to lock-sub. The return value is
1771// one of the following 3 cases:
1772// + target-constant, the operand could be supported as a target constant.
1773// + empty, the operand is not needed any more with the new op selected.
1774// + non-empty, otherwise.
1775static SDValue getAtomicLoadArithTargetConstant(SelectionDAG *CurDAG,
Andrew Trickef9de2a2013-05-25 02:42:55 +00001776 SDLoc dl,
Craig Topper83e042a2013-08-15 05:57:07 +00001777 enum AtomicOpc &Op, MVT NVT,
Robin Morisset6f3d04e2014-10-08 23:16:23 +00001778 SDValue Val,
1779 const X86Subtarget *Subtarget) {
Michael Liao83725392012-09-19 19:36:58 +00001780 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Val)) {
1781 int64_t CNVal = CN->getSExtValue();
1782 // Quit if not 32-bit imm.
1783 if ((int32_t)CNVal != CNVal)
1784 return Val;
Robin Morisset880580b2014-10-07 23:53:57 +00001785 // Quit if INT32_MIN: it would be negated as it is negative and overflow,
1786 // producing an immediate that does not fit in the 32 bits available for
1787 // an immediate operand to sub. However, it still fits in 32 bits for the
1788 // add (since it is not negated) so we can return target-constant.
1789 if (CNVal == INT32_MIN)
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001790 return CurDAG->getTargetConstant(CNVal, dl, NVT);
Michael Liao83725392012-09-19 19:36:58 +00001791 // For atomic-load-add, we could do some optimizations.
1792 if (Op == ADD) {
1793 // Translate to INC/DEC if ADD by 1 or -1.
Robin Morisset6f3d04e2014-10-08 23:16:23 +00001794 if (((CNVal == 1) || (CNVal == -1)) && !Subtarget->slowIncDec()) {
Michael Liao83725392012-09-19 19:36:58 +00001795 Op = (CNVal == 1) ? INC : DEC;
1796 // No more constant operand after being translated into INC/DEC.
1797 return SDValue();
1798 }
1799 // Translate to SUB if ADD by negative value.
1800 if (CNVal < 0) {
1801 Op = SUB;
1802 CNVal = -CNVal;
1803 }
1804 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001805 return CurDAG->getTargetConstant(CNVal, dl, NVT);
Michael Liao83725392012-09-19 19:36:58 +00001806 }
1807
1808 // If the value operand is single-used, try to optimize it.
1809 if (Op == ADD && Val.hasOneUse()) {
1810 // Translate (atomic-load-add ptr (sub 0 x)) back to (lock-sub x).
1811 if (Val.getOpcode() == ISD::SUB && X86::isZeroNode(Val.getOperand(0))) {
1812 Op = SUB;
1813 return Val.getOperand(1);
1814 }
1815 // A special case for i16, which needs truncating as, in most cases, it's
1816 // promoted to i32. We will translate
1817 // (atomic-load-add (truncate (sub 0 x))) to (lock-sub (EXTRACT_SUBREG x))
1818 if (Val.getOpcode() == ISD::TRUNCATE && NVT == MVT::i16 &&
1819 Val.getOperand(0).getOpcode() == ISD::SUB &&
1820 X86::isZeroNode(Val.getOperand(0).getOperand(0))) {
1821 Op = SUB;
1822 Val = Val.getOperand(0);
1823 return CurDAG->getTargetExtractSubreg(X86::sub_16bit, dl, NVT,
1824 Val.getOperand(1));
1825 }
1826 }
1827
1828 return Val;
1829}
1830
Craig Topper83e042a2013-08-15 05:57:07 +00001831SDNode *X86DAGToDAGISel::SelectAtomicLoadArith(SDNode *Node, MVT NVT) {
Eric Christopher4a34e612011-05-10 23:57:45 +00001832 if (Node->hasAnyUseOfValue(0))
Craig Topper062a2ba2014-04-25 05:30:21 +00001833 return nullptr;
Chad Rosier24c19d22012-08-01 18:39:17 +00001834
Andrew Trickef9de2a2013-05-25 02:42:55 +00001835 SDLoc dl(Node);
Michael Liao83725392012-09-19 19:36:58 +00001836
Eric Christopher56a42eb2011-05-17 08:16:14 +00001837 // Optimize common patterns for __sync_or_and_fetch and similar arith
1838 // operations where the result is not used. This allows us to use the "lock"
1839 // version of the arithmetic instruction.
Eric Christopher4a34e612011-05-10 23:57:45 +00001840 SDValue Chain = Node->getOperand(0);
1841 SDValue Ptr = Node->getOperand(1);
1842 SDValue Val = Node->getOperand(2);
Robin Morisset5ce0ce42014-08-29 20:19:23 +00001843 SDValue Base, Scale, Index, Disp, Segment;
1844 if (!SelectAddr(Node, Ptr, Base, Scale, Index, Disp, Segment))
Craig Topper062a2ba2014-04-25 05:30:21 +00001845 return nullptr;
Eric Christopher4a34e612011-05-10 23:57:45 +00001846
Eric Christophera1d9e292011-05-17 08:10:18 +00001847 // Which index into the table.
1848 enum AtomicOpc Op;
1849 switch (Node->getOpcode()) {
Michael Liao83725392012-09-19 19:36:58 +00001850 default:
Craig Topper062a2ba2014-04-25 05:30:21 +00001851 return nullptr;
Eric Christophera1d9e292011-05-17 08:10:18 +00001852 case ISD::ATOMIC_LOAD_OR:
1853 Op = OR;
1854 break;
1855 case ISD::ATOMIC_LOAD_AND:
1856 Op = AND;
1857 break;
1858 case ISD::ATOMIC_LOAD_XOR:
1859 Op = XOR;
1860 break;
Michael Liao83725392012-09-19 19:36:58 +00001861 case ISD::ATOMIC_LOAD_ADD:
1862 Op = ADD;
1863 break;
Eric Christophera1d9e292011-05-17 08:10:18 +00001864 }
Andrew Trick52b83872013-04-13 06:07:36 +00001865
Robin Morisset6f3d04e2014-10-08 23:16:23 +00001866 Val = getAtomicLoadArithTargetConstant(CurDAG, dl, Op, NVT, Val, Subtarget);
Michael Liao83725392012-09-19 19:36:58 +00001867 bool isUnOp = !Val.getNode();
1868 bool isCN = Val.getNode() && (Val.getOpcode() == ISD::TargetConstant);
Chad Rosier24c19d22012-08-01 18:39:17 +00001869
Eric Christopher4a34e612011-05-10 23:57:45 +00001870 unsigned Opc = 0;
Craig Topper83e042a2013-08-15 05:57:07 +00001871 switch (NVT.SimpleTy) {
Craig Topper062a2ba2014-04-25 05:30:21 +00001872 default: return nullptr;
Eric Christopher4a34e612011-05-10 23:57:45 +00001873 case MVT::i8:
1874 if (isCN)
Eric Christophereb47a2a2011-05-17 07:47:55 +00001875 Opc = AtomicOpcTbl[Op][ConstantI8];
Eric Christopher4a34e612011-05-10 23:57:45 +00001876 else
Eric Christophereb47a2a2011-05-17 07:47:55 +00001877 Opc = AtomicOpcTbl[Op][I8];
Eric Christopher4a34e612011-05-10 23:57:45 +00001878 break;
1879 case MVT::i16:
1880 if (isCN) {
1881 if (immSext8(Val.getNode()))
Eric Christophereb47a2a2011-05-17 07:47:55 +00001882 Opc = AtomicOpcTbl[Op][SextConstantI16];
Eric Christopher4a34e612011-05-10 23:57:45 +00001883 else
Eric Christophereb47a2a2011-05-17 07:47:55 +00001884 Opc = AtomicOpcTbl[Op][ConstantI16];
Eric Christopher4a34e612011-05-10 23:57:45 +00001885 } else
Eric Christophereb47a2a2011-05-17 07:47:55 +00001886 Opc = AtomicOpcTbl[Op][I16];
Eric Christopher4a34e612011-05-10 23:57:45 +00001887 break;
1888 case MVT::i32:
1889 if (isCN) {
1890 if (immSext8(Val.getNode()))
Eric Christophereb47a2a2011-05-17 07:47:55 +00001891 Opc = AtomicOpcTbl[Op][SextConstantI32];
Eric Christopher4a34e612011-05-10 23:57:45 +00001892 else
Eric Christophereb47a2a2011-05-17 07:47:55 +00001893 Opc = AtomicOpcTbl[Op][ConstantI32];
Eric Christopher4a34e612011-05-10 23:57:45 +00001894 } else
Eric Christophereb47a2a2011-05-17 07:47:55 +00001895 Opc = AtomicOpcTbl[Op][I32];
Eric Christopher4a34e612011-05-10 23:57:45 +00001896 break;
1897 case MVT::i64:
1898 if (isCN) {
1899 if (immSext8(Val.getNode()))
Eric Christophereb47a2a2011-05-17 07:47:55 +00001900 Opc = AtomicOpcTbl[Op][SextConstantI64];
Eric Christopher4a34e612011-05-10 23:57:45 +00001901 else if (i64immSExt32(Val.getNode()))
Eric Christophereb47a2a2011-05-17 07:47:55 +00001902 Opc = AtomicOpcTbl[Op][ConstantI64];
Robin Morisset880580b2014-10-07 23:53:57 +00001903 else
1904 llvm_unreachable("True 64 bits constant in SelectAtomicLoadArith");
Robin Morisset5ce0ce42014-08-29 20:19:23 +00001905 } else
1906 Opc = AtomicOpcTbl[Op][I64];
Eric Christopher4a34e612011-05-10 23:57:45 +00001907 break;
1908 }
Chad Rosier24c19d22012-08-01 18:39:17 +00001909
Eric Christopherc93217372011-06-30 00:48:30 +00001910 assert(Opc != 0 && "Invalid arith lock transform!");
1911
Robin Morisset5ce0ce42014-08-29 20:19:23 +00001912 // Building the new node.
Michael Liao83725392012-09-19 19:36:58 +00001913 SDValue Ret;
Michael Liao83725392012-09-19 19:36:58 +00001914 if (isUnOp) {
Robin Morisset5ce0ce42014-08-29 20:19:23 +00001915 SDValue Ops[] = { Base, Scale, Index, Disp, Segment, Chain };
Michael Liaob53d8962013-04-19 22:22:57 +00001916 Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops), 0);
Michael Liao83725392012-09-19 19:36:58 +00001917 } else {
Robin Morisset5ce0ce42014-08-29 20:19:23 +00001918 SDValue Ops[] = { Base, Scale, Index, Disp, Segment, Val, Chain };
Michael Liaob53d8962013-04-19 22:22:57 +00001919 Ret = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops), 0);
Michael Liao83725392012-09-19 19:36:58 +00001920 }
Robin Morisset5ce0ce42014-08-29 20:19:23 +00001921
1922 // Copying the MachineMemOperand.
1923 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1924 MemOp[0] = cast<MemSDNode>(Node)->getMemOperand();
Eric Christopher4a34e612011-05-10 23:57:45 +00001925 cast<MachineSDNode>(Ret)->setMemRefs(MemOp, MemOp + 1);
Robin Morisset5ce0ce42014-08-29 20:19:23 +00001926
1927 // We need to have two outputs as that is what the original instruction had.
1928 // So we add a dummy, undefined output. This is safe as we checked first
1929 // that no-one uses our output anyway.
1930 SDValue Undef = SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,
1931 dl, NVT), 0);
Eric Christopher4a34e612011-05-10 23:57:45 +00001932 SDValue RetVals[] = { Undef, Ret };
Craig Topper64941d92014-04-27 19:20:57 +00001933 return CurDAG->getMergeValues(RetVals, dl).getNode();
Eric Christopher4a34e612011-05-10 23:57:45 +00001934}
1935
Dan Gohman7d9dffb2009-10-09 20:35:19 +00001936/// HasNoSignedComparisonUses - Test whether the given X86ISD::CMP node has
1937/// any uses which require the SF or OF bits to be accurate.
1938static bool HasNoSignedComparisonUses(SDNode *N) {
1939 // Examine each user of the node.
1940 for (SDNode::use_iterator UI = N->use_begin(),
1941 UE = N->use_end(); UI != UE; ++UI) {
1942 // Only examine CopyToReg uses.
1943 if (UI->getOpcode() != ISD::CopyToReg)
1944 return false;
1945 // Only examine CopyToReg uses that copy to EFLAGS.
1946 if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1947 X86::EFLAGS)
1948 return false;
1949 // Examine each user of the CopyToReg use.
1950 for (SDNode::use_iterator FlagUI = UI->use_begin(),
1951 FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1952 // Only examine the Flag result.
1953 if (FlagUI.getUse().getResNo() != 1) continue;
1954 // Anything unusual: assume conservatively.
1955 if (!FlagUI->isMachineOpcode()) return false;
1956 // Examine the opcode of the user.
1957 switch (FlagUI->getMachineOpcode()) {
1958 // These comparisons don't treat the most significant bit specially.
1959 case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1960 case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1961 case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1962 case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
Craig Topper49758aa2015-01-06 04:23:53 +00001963 case X86::JA_1: case X86::JAE_1: case X86::JB_1: case X86::JBE_1:
1964 case X86::JE_1: case X86::JNE_1: case X86::JP_1: case X86::JNP_1:
Dan Gohman7d9dffb2009-10-09 20:35:19 +00001965 case X86::CMOVA16rr: case X86::CMOVA16rm:
1966 case X86::CMOVA32rr: case X86::CMOVA32rm:
1967 case X86::CMOVA64rr: case X86::CMOVA64rm:
1968 case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1969 case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1970 case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1971 case X86::CMOVB16rr: case X86::CMOVB16rm:
1972 case X86::CMOVB32rr: case X86::CMOVB32rm:
1973 case X86::CMOVB64rr: case X86::CMOVB64rm:
Chris Lattner1a1c6002010-10-05 23:00:14 +00001974 case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1975 case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1976 case X86::CMOVBE64rr: case X86::CMOVBE64rm:
Dan Gohman7d9dffb2009-10-09 20:35:19 +00001977 case X86::CMOVE16rr: case X86::CMOVE16rm:
1978 case X86::CMOVE32rr: case X86::CMOVE32rm:
1979 case X86::CMOVE64rr: case X86::CMOVE64rm:
1980 case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1981 case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1982 case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1983 case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1984 case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1985 case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1986 case X86::CMOVP16rr: case X86::CMOVP16rm:
1987 case X86::CMOVP32rr: case X86::CMOVP32rm:
1988 case X86::CMOVP64rr: case X86::CMOVP64rm:
1989 continue;
1990 // Anything else: assume conservatively.
1991 default: return false;
1992 }
1993 }
1994 }
1995 return true;
1996}
1997
Joel Jones68d59e82012-03-29 05:45:48 +00001998/// isLoadIncOrDecStore - Check whether or not the chain ending in StoreNode
1999/// is suitable for doing the {load; increment or decrement; store} to modify
2000/// transformation.
Chad Rosier24c19d22012-08-01 18:39:17 +00002001static bool isLoadIncOrDecStore(StoreSDNode *StoreNode, unsigned Opc,
Evan Cheng3e869f02012-04-12 19:14:21 +00002002 SDValue StoredVal, SelectionDAG *CurDAG,
2003 LoadSDNode* &LoadNode, SDValue &InputChain) {
Joel Jones68d59e82012-03-29 05:45:48 +00002004
2005 // is the value stored the result of a DEC or INC?
2006 if (!(Opc == X86ISD::DEC || Opc == X86ISD::INC)) return false;
2007
Joel Jones68d59e82012-03-29 05:45:48 +00002008 // is the stored value result 0 of the load?
2009 if (StoredVal.getResNo() != 0) return false;
2010
2011 // are there other uses of the loaded value than the inc or dec?
2012 if (!StoredVal.getNode()->hasNUsesOfValue(1, 0)) return false;
2013
Joel Jones68d59e82012-03-29 05:45:48 +00002014 // is the store non-extending and non-indexed?
Evan Cheng3e869f02012-04-12 19:14:21 +00002015 if (!ISD::isNormalStore(StoreNode) || StoreNode->isNonTemporal())
Joel Jones68d59e82012-03-29 05:45:48 +00002016 return false;
2017
Evan Cheng3e869f02012-04-12 19:14:21 +00002018 SDValue Load = StoredVal->getOperand(0);
2019 // Is the stored value a non-extending and non-indexed load?
2020 if (!ISD::isNormalLoad(Load.getNode())) return false;
2021
2022 // Return LoadNode by reference.
2023 LoadNode = cast<LoadSDNode>(Load);
2024 // is the size of the value one that we can handle? (i.e. 64, 32, 16, or 8)
Chad Rosier24c19d22012-08-01 18:39:17 +00002025 EVT LdVT = LoadNode->getMemoryVT();
2026 if (LdVT != MVT::i64 && LdVT != MVT::i32 && LdVT != MVT::i16 &&
Evan Cheng3e869f02012-04-12 19:14:21 +00002027 LdVT != MVT::i8)
2028 return false;
2029
2030 // Is store the only read of the loaded value?
2031 if (!Load.hasOneUse())
2032 return false;
Chad Rosier24c19d22012-08-01 18:39:17 +00002033
Evan Cheng3e869f02012-04-12 19:14:21 +00002034 // Is the address of the store the same as the load?
2035 if (LoadNode->getBasePtr() != StoreNode->getBasePtr() ||
2036 LoadNode->getOffset() != StoreNode->getOffset())
2037 return false;
2038
2039 // Check if the chain is produced by the load or is a TokenFactor with
2040 // the load output chain as an operand. Return InputChain by reference.
2041 SDValue Chain = StoreNode->getChain();
2042
2043 bool ChainCheck = false;
2044 if (Chain == Load.getValue(1)) {
2045 ChainCheck = true;
2046 InputChain = LoadNode->getChain();
2047 } else if (Chain.getOpcode() == ISD::TokenFactor) {
2048 SmallVector<SDValue, 4> ChainOps;
2049 for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) {
2050 SDValue Op = Chain.getOperand(i);
2051 if (Op == Load.getValue(1)) {
2052 ChainCheck = true;
2053 continue;
2054 }
Evan Cheng58a95f02012-05-16 01:54:27 +00002055
2056 // Make sure using Op as part of the chain would not cause a cycle here.
2057 // In theory, we could check whether the chain node is a predecessor of
2058 // the load. But that can be very expensive. Instead visit the uses and
2059 // make sure they all have smaller node id than the load.
2060 int LoadId = LoadNode->getNodeId();
2061 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
2062 UE = UI->use_end(); UI != UE; ++UI) {
2063 if (UI.getUse().getResNo() != 0)
2064 continue;
2065 if (UI->getNodeId() > LoadId)
2066 return false;
2067 }
2068
Evan Cheng3e869f02012-04-12 19:14:21 +00002069 ChainOps.push_back(Op);
2070 }
2071
2072 if (ChainCheck)
2073 // Make a new TokenFactor with all the other input chains except
2074 // for the load.
Andrew Trickef9de2a2013-05-25 02:42:55 +00002075 InputChain = CurDAG->getNode(ISD::TokenFactor, SDLoc(Chain),
Craig Topper48d114b2014-04-26 18:35:24 +00002076 MVT::Other, ChainOps);
Evan Cheng3e869f02012-04-12 19:14:21 +00002077 }
2078 if (!ChainCheck)
Joel Jones68d59e82012-03-29 05:45:48 +00002079 return false;
2080
2081 return true;
2082}
2083
Benjamin Kramer8619c372012-03-29 12:37:26 +00002084/// getFusedLdStOpcode - Get the appropriate X86 opcode for an in memory
2085/// increment or decrement. Opc should be X86ISD::DEC or X86ISD::INC.
Joel Jones68d59e82012-03-29 05:45:48 +00002086static unsigned getFusedLdStOpcode(EVT &LdVT, unsigned Opc) {
2087 if (Opc == X86ISD::DEC) {
2088 if (LdVT == MVT::i64) return X86::DEC64m;
2089 if (LdVT == MVT::i32) return X86::DEC32m;
2090 if (LdVT == MVT::i16) return X86::DEC16m;
2091 if (LdVT == MVT::i8) return X86::DEC8m;
Benjamin Kramer8619c372012-03-29 12:37:26 +00002092 } else {
2093 assert(Opc == X86ISD::INC && "unrecognized opcode");
Joel Jones68d59e82012-03-29 05:45:48 +00002094 if (LdVT == MVT::i64) return X86::INC64m;
2095 if (LdVT == MVT::i32) return X86::INC32m;
2096 if (LdVT == MVT::i16) return X86::INC16m;
2097 if (LdVT == MVT::i8) return X86::INC8m;
Joel Jones68d59e82012-03-29 05:45:48 +00002098 }
Benjamin Kramer8619c372012-03-29 12:37:26 +00002099 llvm_unreachable("unrecognized size for LdVT");
Joel Jones68d59e82012-03-29 05:45:48 +00002100}
2101
Manman Rena0982042012-06-26 19:47:59 +00002102/// SelectGather - Customized ISel for GATHER operations.
2103///
2104SDNode *X86DAGToDAGISel::SelectGather(SDNode *Node, unsigned Opc) {
2105 // Operands of Gather: VSrc, Base, VIdx, VMask, Scale
2106 SDValue Chain = Node->getOperand(0);
2107 SDValue VSrc = Node->getOperand(2);
2108 SDValue Base = Node->getOperand(3);
2109 SDValue VIdx = Node->getOperand(4);
2110 SDValue VMask = Node->getOperand(5);
2111 ConstantSDNode *Scale = dyn_cast<ConstantSDNode>(Node->getOperand(6));
Craig Topperfbb954f72012-07-01 02:17:08 +00002112 if (!Scale)
Craig Topper062a2ba2014-04-25 05:30:21 +00002113 return nullptr;
Manman Rena0982042012-06-26 19:47:59 +00002114
Craig Topperf7755df2012-07-12 06:52:41 +00002115 SDVTList VTs = CurDAG->getVTList(VSrc.getValueType(), VSrc.getValueType(),
2116 MVT::Other);
2117
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002118 SDLoc DL(Node);
2119
Manman Rena0982042012-06-26 19:47:59 +00002120 // Memory Operands: Base, Scale, Index, Disp, Segment
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002121 SDValue Disp = CurDAG->getTargetConstant(0, DL, MVT::i32);
Manman Rena0982042012-06-26 19:47:59 +00002122 SDValue Segment = CurDAG->getRegister(0, MVT::i32);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002123 const SDValue Ops[] = { VSrc, Base, getI8Imm(Scale->getSExtValue(), DL), VIdx,
Manman Rena0982042012-06-26 19:47:59 +00002124 Disp, Segment, VMask, Chain};
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002125 SDNode *ResNode = CurDAG->getMachineNode(Opc, DL, VTs, Ops);
Craig Topperf7755df2012-07-12 06:52:41 +00002126 // Node has 2 outputs: VDst and MVT::Other.
2127 // ResNode has 3 outputs: VDst, VMask_wb, and MVT::Other.
2128 // We replace VDst of Node with VDst of ResNode, and Other of Node with Other
2129 // of ResNode.
2130 ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
2131 ReplaceUses(SDValue(Node, 1), SDValue(ResNode, 2));
Manman Rena0982042012-06-26 19:47:59 +00002132 return ResNode;
2133}
2134
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002135SDNode *X86DAGToDAGISel::Select(SDNode *Node) {
Craig Topper83e042a2013-08-15 05:57:07 +00002136 MVT NVT = Node->getSimpleValueType(0);
Evan Cheng10d27902006-01-06 20:36:21 +00002137 unsigned Opc, MOpc;
2138 unsigned Opcode = Node->getOpcode();
Andrew Trickef9de2a2013-05-25 02:42:55 +00002139 SDLoc dl(Node);
Chad Rosier24c19d22012-08-01 18:39:17 +00002140
Chris Lattnerf98f1242010-03-02 06:34:30 +00002141 DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n');
Evan Chengd49cc362006-02-10 22:24:32 +00002142
Dan Gohman17059682008-07-17 19:10:17 +00002143 if (Node->isMachineOpcode()) {
Chris Lattnerf98f1242010-03-02 06:34:30 +00002144 DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << '\n');
Tim Northover31d093c2013-09-22 08:21:56 +00002145 Node->setNodeId(-1);
Craig Topper062a2ba2014-04-25 05:30:21 +00002146 return nullptr; // Already selected.
Evan Cheng6dc90ca2006-02-09 00:37:58 +00002147 }
Evan Cheng2ae799a2006-01-11 22:15:18 +00002148
Evan Cheng10d27902006-01-06 20:36:21 +00002149 switch (Opcode) {
Dan Gohman757eee82009-08-02 16:10:52 +00002150 default: break;
Manman Rena0982042012-06-26 19:47:59 +00002151 case ISD::INTRINSIC_W_CHAIN: {
2152 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
2153 switch (IntNo) {
2154 default: break;
2155 case Intrinsic::x86_avx2_gather_d_pd:
Manman Rena0982042012-06-26 19:47:59 +00002156 case Intrinsic::x86_avx2_gather_d_pd_256:
Manman Rena0982042012-06-26 19:47:59 +00002157 case Intrinsic::x86_avx2_gather_q_pd:
Manman Rena0982042012-06-26 19:47:59 +00002158 case Intrinsic::x86_avx2_gather_q_pd_256:
Manman Rena0982042012-06-26 19:47:59 +00002159 case Intrinsic::x86_avx2_gather_d_ps:
Manman Rena0982042012-06-26 19:47:59 +00002160 case Intrinsic::x86_avx2_gather_d_ps_256:
Manman Rena0982042012-06-26 19:47:59 +00002161 case Intrinsic::x86_avx2_gather_q_ps:
Manman Rena0982042012-06-26 19:47:59 +00002162 case Intrinsic::x86_avx2_gather_q_ps_256:
Manman Ren98a5bf22012-06-29 00:54:20 +00002163 case Intrinsic::x86_avx2_gather_d_q:
Manman Ren98a5bf22012-06-29 00:54:20 +00002164 case Intrinsic::x86_avx2_gather_d_q_256:
Manman Ren98a5bf22012-06-29 00:54:20 +00002165 case Intrinsic::x86_avx2_gather_q_q:
Manman Ren98a5bf22012-06-29 00:54:20 +00002166 case Intrinsic::x86_avx2_gather_q_q_256:
Manman Ren98a5bf22012-06-29 00:54:20 +00002167 case Intrinsic::x86_avx2_gather_d_d:
Manman Ren98a5bf22012-06-29 00:54:20 +00002168 case Intrinsic::x86_avx2_gather_d_d_256:
Manman Ren98a5bf22012-06-29 00:54:20 +00002169 case Intrinsic::x86_avx2_gather_q_d:
Craig Topperdef044b2012-07-01 02:05:52 +00002170 case Intrinsic::x86_avx2_gather_q_d_256: {
Michael Liao00b20cc2013-06-05 18:12:26 +00002171 if (!Subtarget->hasAVX2())
2172 break;
Craig Topperdef044b2012-07-01 02:05:52 +00002173 unsigned Opc;
2174 switch (IntNo) {
Craig Topper3af251d2012-07-01 02:55:34 +00002175 default: llvm_unreachable("Impossible intrinsic");
Craig Topperdef044b2012-07-01 02:05:52 +00002176 case Intrinsic::x86_avx2_gather_d_pd: Opc = X86::VGATHERDPDrm; break;
2177 case Intrinsic::x86_avx2_gather_d_pd_256: Opc = X86::VGATHERDPDYrm; break;
2178 case Intrinsic::x86_avx2_gather_q_pd: Opc = X86::VGATHERQPDrm; break;
2179 case Intrinsic::x86_avx2_gather_q_pd_256: Opc = X86::VGATHERQPDYrm; break;
2180 case Intrinsic::x86_avx2_gather_d_ps: Opc = X86::VGATHERDPSrm; break;
2181 case Intrinsic::x86_avx2_gather_d_ps_256: Opc = X86::VGATHERDPSYrm; break;
2182 case Intrinsic::x86_avx2_gather_q_ps: Opc = X86::VGATHERQPSrm; break;
2183 case Intrinsic::x86_avx2_gather_q_ps_256: Opc = X86::VGATHERQPSYrm; break;
2184 case Intrinsic::x86_avx2_gather_d_q: Opc = X86::VPGATHERDQrm; break;
2185 case Intrinsic::x86_avx2_gather_d_q_256: Opc = X86::VPGATHERDQYrm; break;
2186 case Intrinsic::x86_avx2_gather_q_q: Opc = X86::VPGATHERQQrm; break;
2187 case Intrinsic::x86_avx2_gather_q_q_256: Opc = X86::VPGATHERQQYrm; break;
2188 case Intrinsic::x86_avx2_gather_d_d: Opc = X86::VPGATHERDDrm; break;
2189 case Intrinsic::x86_avx2_gather_d_d_256: Opc = X86::VPGATHERDDYrm; break;
2190 case Intrinsic::x86_avx2_gather_q_d: Opc = X86::VPGATHERQDrm; break;
2191 case Intrinsic::x86_avx2_gather_q_d_256: Opc = X86::VPGATHERQDYrm; break;
2192 }
Craig Topperfbb954f72012-07-01 02:17:08 +00002193 SDNode *RetVal = SelectGather(Node, Opc);
2194 if (RetVal)
Craig Topperf7755df2012-07-12 06:52:41 +00002195 // We already called ReplaceUses inside SelectGather.
Craig Topper062a2ba2014-04-25 05:30:21 +00002196 return nullptr;
Craig Toppere15e5f72012-07-01 02:18:18 +00002197 break;
Craig Topperdef044b2012-07-01 02:05:52 +00002198 }
Manman Rena0982042012-06-26 19:47:59 +00002199 }
2200 break;
2201 }
Dan Gohman757eee82009-08-02 16:10:52 +00002202 case X86ISD::GlobalBaseReg:
2203 return getGlobalBaseReg();
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002204
Quentin Colombetdbe33e72014-11-06 02:25:03 +00002205 case X86ISD::SHRUNKBLEND: {
2206 // SHRUNKBLEND selects like a regular VSELECT.
2207 SDValue VSelect = CurDAG->getNode(
2208 ISD::VSELECT, SDLoc(Node), Node->getValueType(0), Node->getOperand(0),
2209 Node->getOperand(1), Node->getOperand(2));
2210 ReplaceUses(SDValue(Node, 0), VSelect);
2211 SelectCode(VSelect.getNode());
2212 // We already called ReplaceUses.
2213 return nullptr;
2214 }
Craig Topper3af251d2012-07-01 02:55:34 +00002215
Eric Christophera1d9e292011-05-17 08:10:18 +00002216 case ISD::ATOMIC_LOAD_XOR:
2217 case ISD::ATOMIC_LOAD_AND:
Michael Liao83725392012-09-19 19:36:58 +00002218 case ISD::ATOMIC_LOAD_OR:
2219 case ISD::ATOMIC_LOAD_ADD: {
Eric Christophera1d9e292011-05-17 08:10:18 +00002220 SDNode *RetVal = SelectAtomicLoadArith(Node, NVT);
Eric Christopher4a34e612011-05-10 23:57:45 +00002221 if (RetVal)
2222 return RetVal;
2223 break;
2224 }
Benjamin Kramer4c816242011-04-22 15:30:40 +00002225 case ISD::AND:
2226 case ISD::OR:
2227 case ISD::XOR: {
2228 // For operations of the form (x << C1) op C2, check if we can use a smaller
2229 // encoding for C2 by transforming it into (x op (C2>>C1)) << C1.
2230 SDValue N0 = Node->getOperand(0);
2231 SDValue N1 = Node->getOperand(1);
2232
2233 if (N0->getOpcode() != ISD::SHL || !N0->hasOneUse())
2234 break;
2235
2236 // i8 is unshrinkable, i16 should be promoted to i32.
2237 if (NVT != MVT::i32 && NVT != MVT::i64)
2238 break;
2239
2240 ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N1);
2241 ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(N0->getOperand(1));
2242 if (!Cst || !ShlCst)
2243 break;
2244
2245 int64_t Val = Cst->getSExtValue();
2246 uint64_t ShlVal = ShlCst->getZExtValue();
2247
2248 // Make sure that we don't change the operation by removing bits.
2249 // This only matters for OR and XOR, AND is unaffected.
Richard Smith228e6d42012-08-24 23:29:28 +00002250 uint64_t RemovedBitsMask = (1ULL << ShlVal) - 1;
2251 if (Opcode != ISD::AND && (Val & RemovedBitsMask) != 0)
Benjamin Kramer4c816242011-04-22 15:30:40 +00002252 break;
2253
Benjamin Kramer3a16a362015-04-01 19:01:09 +00002254 unsigned ShlOp, AddOp, Op;
Craig Topper83e042a2013-08-15 05:57:07 +00002255 MVT CstVT = NVT;
Benjamin Kramer4c816242011-04-22 15:30:40 +00002256
2257 // Check the minimum bitwidth for the new constant.
2258 // TODO: AND32ri is the same as AND64ri32 with zext imm.
2259 // TODO: MOV32ri+OR64r is cheaper than MOV64ri64+OR64rr
2260 // TODO: Using 16 and 8 bit operations is also possible for or32 & xor32.
2261 if (!isInt<8>(Val) && isInt<8>(Val >> ShlVal))
2262 CstVT = MVT::i8;
2263 else if (!isInt<32>(Val) && isInt<32>(Val >> ShlVal))
2264 CstVT = MVT::i32;
2265
2266 // Bail if there is no smaller encoding.
2267 if (NVT == CstVT)
2268 break;
2269
Craig Topper83e042a2013-08-15 05:57:07 +00002270 switch (NVT.SimpleTy) {
Benjamin Kramer4c816242011-04-22 15:30:40 +00002271 default: llvm_unreachable("Unsupported VT!");
2272 case MVT::i32:
2273 assert(CstVT == MVT::i8);
2274 ShlOp = X86::SHL32ri;
Benjamin Kramer3a16a362015-04-01 19:01:09 +00002275 AddOp = X86::ADD32rr;
Benjamin Kramer4c816242011-04-22 15:30:40 +00002276
2277 switch (Opcode) {
Craig Topper22cb0c52012-08-11 17:44:14 +00002278 default: llvm_unreachable("Impossible opcode");
Benjamin Kramer4c816242011-04-22 15:30:40 +00002279 case ISD::AND: Op = X86::AND32ri8; break;
2280 case ISD::OR: Op = X86::OR32ri8; break;
2281 case ISD::XOR: Op = X86::XOR32ri8; break;
2282 }
2283 break;
2284 case MVT::i64:
2285 assert(CstVT == MVT::i8 || CstVT == MVT::i32);
2286 ShlOp = X86::SHL64ri;
Benjamin Kramer3a16a362015-04-01 19:01:09 +00002287 AddOp = X86::ADD64rr;
Benjamin Kramer4c816242011-04-22 15:30:40 +00002288
2289 switch (Opcode) {
Craig Topper22cb0c52012-08-11 17:44:14 +00002290 default: llvm_unreachable("Impossible opcode");
Benjamin Kramer4c816242011-04-22 15:30:40 +00002291 case ISD::AND: Op = CstVT==MVT::i8? X86::AND64ri8 : X86::AND64ri32; break;
2292 case ISD::OR: Op = CstVT==MVT::i8? X86::OR64ri8 : X86::OR64ri32; break;
2293 case ISD::XOR: Op = CstVT==MVT::i8? X86::XOR64ri8 : X86::XOR64ri32; break;
2294 }
2295 break;
2296 }
2297
2298 // Emit the smaller op and the shift.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002299 SDValue NewCst = CurDAG->getTargetConstant(Val >> ShlVal, dl, CstVT);
Benjamin Kramer4c816242011-04-22 15:30:40 +00002300 SDNode *New = CurDAG->getMachineNode(Op, dl, NVT, N0->getOperand(0),NewCst);
Benjamin Kramer3a16a362015-04-01 19:01:09 +00002301 if (ShlVal == 1)
2302 return CurDAG->SelectNodeTo(Node, AddOp, NVT, SDValue(New, 0),
2303 SDValue(New, 0));
Benjamin Kramer4c816242011-04-22 15:30:40 +00002304 return CurDAG->SelectNodeTo(Node, ShlOp, NVT, SDValue(New, 0),
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002305 getI8Imm(ShlVal, dl));
Benjamin Kramer4c816242011-04-22 15:30:40 +00002306 }
Ahmed Bougacha5175bcf2014-10-23 21:55:31 +00002307 case X86ISD::UMUL8:
2308 case X86ISD::SMUL8: {
2309 SDValue N0 = Node->getOperand(0);
2310 SDValue N1 = Node->getOperand(1);
2311
2312 Opc = (Opcode == X86ISD::SMUL8 ? X86::IMUL8r : X86::MUL8r);
2313
2314 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, X86::AL,
2315 N0, SDValue()).getValue(1);
2316
2317 SDVTList VTs = CurDAG->getVTList(NVT, MVT::i32);
2318 SDValue Ops[] = {N1, InFlag};
2319 SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops);
2320
2321 ReplaceUses(SDValue(Node, 0), SDValue(CNode, 0));
2322 ReplaceUses(SDValue(Node, 1), SDValue(CNode, 1));
2323 return nullptr;
2324 }
2325
Chris Lattner364bb0a2010-12-05 07:30:36 +00002326 case X86ISD::UMUL: {
2327 SDValue N0 = Node->getOperand(0);
2328 SDValue N1 = Node->getOperand(1);
Chad Rosier24c19d22012-08-01 18:39:17 +00002329
Ted Kremenekb5241b22011-01-14 22:34:13 +00002330 unsigned LoReg;
Craig Topper83e042a2013-08-15 05:57:07 +00002331 switch (NVT.SimpleTy) {
Chris Lattner364bb0a2010-12-05 07:30:36 +00002332 default: llvm_unreachable("Unsupported VT!");
Ted Kremenekb5241b22011-01-14 22:34:13 +00002333 case MVT::i8: LoReg = X86::AL; Opc = X86::MUL8r; break;
2334 case MVT::i16: LoReg = X86::AX; Opc = X86::MUL16r; break;
2335 case MVT::i32: LoReg = X86::EAX; Opc = X86::MUL32r; break;
2336 case MVT::i64: LoReg = X86::RAX; Opc = X86::MUL64r; break;
Chris Lattner364bb0a2010-12-05 07:30:36 +00002337 }
Chad Rosier24c19d22012-08-01 18:39:17 +00002338
Chris Lattner364bb0a2010-12-05 07:30:36 +00002339 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
2340 N0, SDValue()).getValue(1);
Chad Rosier24c19d22012-08-01 18:39:17 +00002341
Chris Lattner364bb0a2010-12-05 07:30:36 +00002342 SDVTList VTs = CurDAG->getVTList(NVT, NVT, MVT::i32);
2343 SDValue Ops[] = {N1, InFlag};
Michael Liaob53d8962013-04-19 22:22:57 +00002344 SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops);
Chad Rosier24c19d22012-08-01 18:39:17 +00002345
Chris Lattner364bb0a2010-12-05 07:30:36 +00002346 ReplaceUses(SDValue(Node, 0), SDValue(CNode, 0));
2347 ReplaceUses(SDValue(Node, 1), SDValue(CNode, 1));
2348 ReplaceUses(SDValue(Node, 2), SDValue(CNode, 2));
Craig Topper062a2ba2014-04-25 05:30:21 +00002349 return nullptr;
Chris Lattner364bb0a2010-12-05 07:30:36 +00002350 }
Chad Rosier24c19d22012-08-01 18:39:17 +00002351
Dan Gohman757eee82009-08-02 16:10:52 +00002352 case ISD::SMUL_LOHI:
2353 case ISD::UMUL_LOHI: {
2354 SDValue N0 = Node->getOperand(0);
2355 SDValue N1 = Node->getOperand(1);
2356
2357 bool isSigned = Opcode == ISD::SMUL_LOHI;
Michael Liaof9f7b552012-09-26 08:22:37 +00002358 bool hasBMI2 = Subtarget->hasBMI2();
Bill Wendlingfe3bdb42009-08-07 21:33:25 +00002359 if (!isSigned) {
Craig Topper83e042a2013-08-15 05:57:07 +00002360 switch (NVT.SimpleTy) {
Dan Gohman757eee82009-08-02 16:10:52 +00002361 default: llvm_unreachable("Unsupported VT!");
Owen Anderson9f944592009-08-11 20:47:22 +00002362 case MVT::i8: Opc = X86::MUL8r; MOpc = X86::MUL8m; break;
2363 case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
Michael Liaof9f7b552012-09-26 08:22:37 +00002364 case MVT::i32: Opc = hasBMI2 ? X86::MULX32rr : X86::MUL32r;
2365 MOpc = hasBMI2 ? X86::MULX32rm : X86::MUL32m; break;
2366 case MVT::i64: Opc = hasBMI2 ? X86::MULX64rr : X86::MUL64r;
2367 MOpc = hasBMI2 ? X86::MULX64rm : X86::MUL64m; break;
Dan Gohman757eee82009-08-02 16:10:52 +00002368 }
Bill Wendlingfe3bdb42009-08-07 21:33:25 +00002369 } else {
Craig Topper83e042a2013-08-15 05:57:07 +00002370 switch (NVT.SimpleTy) {
Dan Gohman757eee82009-08-02 16:10:52 +00002371 default: llvm_unreachable("Unsupported VT!");
Owen Anderson9f944592009-08-11 20:47:22 +00002372 case MVT::i8: Opc = X86::IMUL8r; MOpc = X86::IMUL8m; break;
2373 case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
2374 case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
2375 case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
Dan Gohman757eee82009-08-02 16:10:52 +00002376 }
Bill Wendlingfe3bdb42009-08-07 21:33:25 +00002377 }
Dan Gohman757eee82009-08-02 16:10:52 +00002378
Michael Liaof9f7b552012-09-26 08:22:37 +00002379 unsigned SrcReg, LoReg, HiReg;
2380 switch (Opc) {
2381 default: llvm_unreachable("Unknown MUL opcode!");
2382 case X86::IMUL8r:
2383 case X86::MUL8r:
2384 SrcReg = LoReg = X86::AL; HiReg = X86::AH;
2385 break;
2386 case X86::IMUL16r:
2387 case X86::MUL16r:
2388 SrcReg = LoReg = X86::AX; HiReg = X86::DX;
2389 break;
2390 case X86::IMUL32r:
2391 case X86::MUL32r:
2392 SrcReg = LoReg = X86::EAX; HiReg = X86::EDX;
2393 break;
2394 case X86::IMUL64r:
2395 case X86::MUL64r:
2396 SrcReg = LoReg = X86::RAX; HiReg = X86::RDX;
2397 break;
2398 case X86::MULX32rr:
2399 SrcReg = X86::EDX; LoReg = HiReg = 0;
2400 break;
2401 case X86::MULX64rr:
2402 SrcReg = X86::RDX; LoReg = HiReg = 0;
2403 break;
Dan Gohman757eee82009-08-02 16:10:52 +00002404 }
2405
2406 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002407 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Bill Wendlingfe3bdb42009-08-07 21:33:25 +00002408 // Multiply is commmutative.
Dan Gohman757eee82009-08-02 16:10:52 +00002409 if (!foldedLoad) {
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002410 foldedLoad = TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman757eee82009-08-02 16:10:52 +00002411 if (foldedLoad)
2412 std::swap(N0, N1);
2413 }
2414
Michael Liaof9f7b552012-09-26 08:22:37 +00002415 SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, SrcReg,
Craig Toppera4fd6d62012-05-23 05:44:51 +00002416 N0, SDValue()).getValue(1);
Michael Liaof9f7b552012-09-26 08:22:37 +00002417 SDValue ResHi, ResLo;
Dan Gohman757eee82009-08-02 16:10:52 +00002418
2419 if (foldedLoad) {
Michael Liaof9f7b552012-09-26 08:22:37 +00002420 SDValue Chain;
Dan Gohman757eee82009-08-02 16:10:52 +00002421 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
2422 InFlag };
Michael Liaof9f7b552012-09-26 08:22:37 +00002423 if (MOpc == X86::MULX32rm || MOpc == X86::MULX64rm) {
2424 SDVTList VTs = CurDAG->getVTList(NVT, NVT, MVT::Other, MVT::Glue);
Michael Liaob53d8962013-04-19 22:22:57 +00002425 SDNode *CNode = CurDAG->getMachineNode(MOpc, dl, VTs, Ops);
Michael Liaof9f7b552012-09-26 08:22:37 +00002426 ResHi = SDValue(CNode, 0);
2427 ResLo = SDValue(CNode, 1);
2428 Chain = SDValue(CNode, 2);
2429 InFlag = SDValue(CNode, 3);
2430 } else {
2431 SDVTList VTs = CurDAG->getVTList(MVT::Other, MVT::Glue);
Michael Liaob53d8962013-04-19 22:22:57 +00002432 SDNode *CNode = CurDAG->getMachineNode(MOpc, dl, VTs, Ops);
Michael Liaof9f7b552012-09-26 08:22:37 +00002433 Chain = SDValue(CNode, 0);
2434 InFlag = SDValue(CNode, 1);
2435 }
Chris Lattner364bb0a2010-12-05 07:30:36 +00002436
Dan Gohman757eee82009-08-02 16:10:52 +00002437 // Update the chain.
Michael Liaof9f7b552012-09-26 08:22:37 +00002438 ReplaceUses(N1.getValue(1), Chain);
Dan Gohman757eee82009-08-02 16:10:52 +00002439 } else {
Michael Liaof9f7b552012-09-26 08:22:37 +00002440 SDValue Ops[] = { N1, InFlag };
2441 if (Opc == X86::MULX32rr || Opc == X86::MULX64rr) {
2442 SDVTList VTs = CurDAG->getVTList(NVT, NVT, MVT::Glue);
Michael Liaob53d8962013-04-19 22:22:57 +00002443 SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops);
Michael Liaof9f7b552012-09-26 08:22:37 +00002444 ResHi = SDValue(CNode, 0);
2445 ResLo = SDValue(CNode, 1);
2446 InFlag = SDValue(CNode, 2);
2447 } else {
2448 SDVTList VTs = CurDAG->getVTList(MVT::Glue);
Michael Liaob53d8962013-04-19 22:22:57 +00002449 SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops);
Michael Liaof9f7b552012-09-26 08:22:37 +00002450 InFlag = SDValue(CNode, 0);
2451 }
Dan Gohman757eee82009-08-02 16:10:52 +00002452 }
2453
Jakob Stoklund Olesend7d0d4e2010-06-26 00:39:23 +00002454 // Prevent use of AH in a REX instruction by referencing AX instead.
2455 if (HiReg == X86::AH && Subtarget->is64Bit() &&
2456 !SDValue(Node, 1).use_empty()) {
2457 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2458 X86::AX, MVT::i16, InFlag);
2459 InFlag = Result.getValue(2);
2460 // Get the low part if needed. Don't use getCopyFromReg for aliasing
2461 // registers.
2462 if (!SDValue(Node, 0).use_empty())
2463 ReplaceUses(SDValue(Node, 1),
2464 CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result));
2465
2466 // Shift AX down 8 bits.
2467 Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
2468 Result,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002469 CurDAG->getTargetConstant(8, dl, MVT::i8)),
2470 0);
Jakob Stoklund Olesend7d0d4e2010-06-26 00:39:23 +00002471 // Then truncate it down to i8.
2472 ReplaceUses(SDValue(Node, 1),
2473 CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result));
2474 }
Dan Gohman757eee82009-08-02 16:10:52 +00002475 // Copy the low half of the result, if it is needed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002476 if (!SDValue(Node, 0).use_empty()) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002477 if (!ResLo.getNode()) {
Michael Liaof9f7b552012-09-26 08:22:37 +00002478 assert(LoReg && "Register for low half is not defined!");
2479 ResLo = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, LoReg, NVT,
2480 InFlag);
2481 InFlag = ResLo.getValue(2);
2482 }
2483 ReplaceUses(SDValue(Node, 0), ResLo);
2484 DEBUG(dbgs() << "=> "; ResLo.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman757eee82009-08-02 16:10:52 +00002485 }
2486 // Copy the high half of the result, if it is needed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002487 if (!SDValue(Node, 1).use_empty()) {
Craig Topper062a2ba2014-04-25 05:30:21 +00002488 if (!ResHi.getNode()) {
Michael Liaof9f7b552012-09-26 08:22:37 +00002489 assert(HiReg && "Register for high half is not defined!");
2490 ResHi = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, HiReg, NVT,
2491 InFlag);
2492 InFlag = ResHi.getValue(2);
2493 }
2494 ReplaceUses(SDValue(Node, 1), ResHi);
2495 DEBUG(dbgs() << "=> "; ResHi.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman757eee82009-08-02 16:10:52 +00002496 }
Chad Rosier24c19d22012-08-01 18:39:17 +00002497
Craig Topper062a2ba2014-04-25 05:30:21 +00002498 return nullptr;
Dan Gohman757eee82009-08-02 16:10:52 +00002499 }
2500
2501 case ISD::SDIVREM:
Ahmed Bougacha12eb5582014-11-03 20:26:35 +00002502 case ISD::UDIVREM:
2503 case X86ISD::SDIVREM8_SEXT_HREG:
2504 case X86ISD::UDIVREM8_ZEXT_HREG: {
Dan Gohman757eee82009-08-02 16:10:52 +00002505 SDValue N0 = Node->getOperand(0);
2506 SDValue N1 = Node->getOperand(1);
2507
Ahmed Bougacha12eb5582014-11-03 20:26:35 +00002508 bool isSigned = (Opcode == ISD::SDIVREM ||
2509 Opcode == X86ISD::SDIVREM8_SEXT_HREG);
Bill Wendlingfe3bdb42009-08-07 21:33:25 +00002510 if (!isSigned) {
Craig Topper83e042a2013-08-15 05:57:07 +00002511 switch (NVT.SimpleTy) {
Dan Gohman757eee82009-08-02 16:10:52 +00002512 default: llvm_unreachable("Unsupported VT!");
Owen Anderson9f944592009-08-11 20:47:22 +00002513 case MVT::i8: Opc = X86::DIV8r; MOpc = X86::DIV8m; break;
2514 case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
2515 case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
2516 case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
Dan Gohman757eee82009-08-02 16:10:52 +00002517 }
Bill Wendlingfe3bdb42009-08-07 21:33:25 +00002518 } else {
Craig Topper83e042a2013-08-15 05:57:07 +00002519 switch (NVT.SimpleTy) {
Dan Gohman757eee82009-08-02 16:10:52 +00002520 default: llvm_unreachable("Unsupported VT!");
Owen Anderson9f944592009-08-11 20:47:22 +00002521 case MVT::i8: Opc = X86::IDIV8r; MOpc = X86::IDIV8m; break;
2522 case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
2523 case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
2524 case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
Dan Gohman757eee82009-08-02 16:10:52 +00002525 }
Bill Wendlingfe3bdb42009-08-07 21:33:25 +00002526 }
Dan Gohman757eee82009-08-02 16:10:52 +00002527
Chris Lattner518b0372009-12-23 01:45:04 +00002528 unsigned LoReg, HiReg, ClrReg;
Tim Northover64ec0ff2013-05-30 13:19:42 +00002529 unsigned SExtOpcode;
Craig Topper83e042a2013-08-15 05:57:07 +00002530 switch (NVT.SimpleTy) {
Dan Gohman757eee82009-08-02 16:10:52 +00002531 default: llvm_unreachable("Unsupported VT!");
Owen Anderson9f944592009-08-11 20:47:22 +00002532 case MVT::i8:
Chris Lattner518b0372009-12-23 01:45:04 +00002533 LoReg = X86::AL; ClrReg = HiReg = X86::AH;
Dan Gohman757eee82009-08-02 16:10:52 +00002534 SExtOpcode = X86::CBW;
2535 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002536 case MVT::i16:
Dan Gohman757eee82009-08-02 16:10:52 +00002537 LoReg = X86::AX; HiReg = X86::DX;
Tim Northover64ec0ff2013-05-30 13:19:42 +00002538 ClrReg = X86::DX;
Dan Gohman757eee82009-08-02 16:10:52 +00002539 SExtOpcode = X86::CWD;
2540 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002541 case MVT::i32:
Chris Lattner518b0372009-12-23 01:45:04 +00002542 LoReg = X86::EAX; ClrReg = HiReg = X86::EDX;
Dan Gohman757eee82009-08-02 16:10:52 +00002543 SExtOpcode = X86::CDQ;
2544 break;
Owen Anderson9f944592009-08-11 20:47:22 +00002545 case MVT::i64:
Chris Lattner518b0372009-12-23 01:45:04 +00002546 LoReg = X86::RAX; ClrReg = HiReg = X86::RDX;
Dan Gohman757eee82009-08-02 16:10:52 +00002547 SExtOpcode = X86::CQO;
Evan Chenge62288f2009-07-30 08:33:02 +00002548 break;
2549 }
2550
Dan Gohman757eee82009-08-02 16:10:52 +00002551 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002552 bool foldedLoad = TryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
Dan Gohman757eee82009-08-02 16:10:52 +00002553 bool signBitIsZero = CurDAG->SignBitIsZero(N0);
Dan Gohmana1603612007-10-08 18:33:35 +00002554
Dan Gohman757eee82009-08-02 16:10:52 +00002555 SDValue InFlag;
Owen Anderson9f944592009-08-11 20:47:22 +00002556 if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
Dan Gohman757eee82009-08-02 16:10:52 +00002557 // Special case for div8, just use a move with zero extension to AX to
2558 // clear the upper 8 bits (AH).
2559 SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002560 if (TryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
Dan Gohman757eee82009-08-02 16:10:52 +00002561 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
2562 Move =
Stuart Hastings91f1d242011-05-20 19:04:40 +00002563 SDValue(CurDAG->getMachineNode(X86::MOVZX32rm8, dl, MVT::i32,
Michael Liaob53d8962013-04-19 22:22:57 +00002564 MVT::Other, Ops), 0);
Dan Gohman757eee82009-08-02 16:10:52 +00002565 Chain = Move.getValue(1);
2566 ReplaceUses(N0.getValue(1), Chain);
Evan Cheng10d27902006-01-06 20:36:21 +00002567 } else {
Dan Gohman757eee82009-08-02 16:10:52 +00002568 Move =
Stuart Hastings91f1d242011-05-20 19:04:40 +00002569 SDValue(CurDAG->getMachineNode(X86::MOVZX32rr8, dl, MVT::i32, N0),0);
Dan Gohman757eee82009-08-02 16:10:52 +00002570 Chain = CurDAG->getEntryNode();
2571 }
Stuart Hastings91f1d242011-05-20 19:04:40 +00002572 Chain = CurDAG->getCopyToReg(Chain, dl, X86::EAX, Move, SDValue());
Dan Gohman757eee82009-08-02 16:10:52 +00002573 InFlag = Chain.getValue(1);
2574 } else {
2575 InFlag =
2576 CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
2577 LoReg, N0, SDValue()).getValue(1);
2578 if (isSigned && !signBitIsZero) {
2579 // Sign extend the low part into the high part.
Evan Chengd1b82d82006-02-09 07:17:49 +00002580 InFlag =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002581 SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Glue, InFlag),0);
Dan Gohman757eee82009-08-02 16:10:52 +00002582 } else {
2583 // Zero out the high part, effectively zero extending the input.
Michael Liao5bf95782014-12-04 05:20:33 +00002584 SDValue ClrNode = SDValue(CurDAG->getMachineNode(X86::MOV32r0, dl, NVT), 0);
Craig Topper83e042a2013-08-15 05:57:07 +00002585 switch (NVT.SimpleTy) {
Tim Northover64ec0ff2013-05-30 13:19:42 +00002586 case MVT::i16:
2587 ClrNode =
2588 SDValue(CurDAG->getMachineNode(
2589 TargetOpcode::EXTRACT_SUBREG, dl, MVT::i16, ClrNode,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002590 CurDAG->getTargetConstant(X86::sub_16bit, dl,
2591 MVT::i32)),
Tim Northover64ec0ff2013-05-30 13:19:42 +00002592 0);
2593 break;
2594 case MVT::i32:
2595 break;
2596 case MVT::i64:
2597 ClrNode =
2598 SDValue(CurDAG->getMachineNode(
2599 TargetOpcode::SUBREG_TO_REG, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002600 CurDAG->getTargetConstant(0, dl, MVT::i64), ClrNode,
2601 CurDAG->getTargetConstant(X86::sub_32bit, dl,
2602 MVT::i32)),
Tim Northover64ec0ff2013-05-30 13:19:42 +00002603 0);
2604 break;
2605 default:
2606 llvm_unreachable("Unexpected division source");
2607 }
2608
Chris Lattner518b0372009-12-23 01:45:04 +00002609 InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg,
Dan Gohman757eee82009-08-02 16:10:52 +00002610 ClrNode, InFlag).getValue(1);
Dan Gohmana1603612007-10-08 18:33:35 +00002611 }
Evan Cheng92e27972006-01-06 23:19:29 +00002612 }
Dan Gohmana1603612007-10-08 18:33:35 +00002613
Dan Gohman757eee82009-08-02 16:10:52 +00002614 if (foldedLoad) {
2615 SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
2616 InFlag };
2617 SDNode *CNode =
Michael Liaob53d8962013-04-19 22:22:57 +00002618 CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Glue, Ops);
Dan Gohman757eee82009-08-02 16:10:52 +00002619 InFlag = SDValue(CNode, 1);
2620 // Update the chain.
2621 ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
2622 } else {
2623 InFlag =
Chris Lattner3e5fbd72010-12-21 02:38:05 +00002624 SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, N1, InFlag), 0);
Dan Gohman757eee82009-08-02 16:10:52 +00002625 }
Evan Cheng92e27972006-01-06 23:19:29 +00002626
Ahmed Bougacha12eb5582014-11-03 20:26:35 +00002627 // Prevent use of AH in a REX instruction by explicitly copying it to
2628 // an ABCD_L register.
Jim Grosbach340b6da2013-07-09 02:07:28 +00002629 //
2630 // The current assumption of the register allocator is that isel
Ahmed Bougacha12eb5582014-11-03 20:26:35 +00002631 // won't generate explicit references to the GR8_ABCD_H registers. If
Jim Grosbach340b6da2013-07-09 02:07:28 +00002632 // the allocator and/or the backend get enhanced to be more robust in
2633 // that regard, this can be, and should be, removed.
Ahmed Bougacha12eb5582014-11-03 20:26:35 +00002634 if (HiReg == X86::AH && !SDValue(Node, 1).use_empty()) {
2635 SDValue AHCopy = CurDAG->getRegister(X86::AH, MVT::i8);
2636 unsigned AHExtOpcode =
2637 isSigned ? X86::MOVSX32_NOREXrr8 : X86::MOVZX32_NOREXrr8;
Jakob Stoklund Olesend7d0d4e2010-06-26 00:39:23 +00002638
Ahmed Bougacha12eb5582014-11-03 20:26:35 +00002639 SDNode *RNode = CurDAG->getMachineNode(AHExtOpcode, dl, MVT::i32,
2640 MVT::Glue, AHCopy, InFlag);
2641 SDValue Result(RNode, 0);
2642 InFlag = SDValue(RNode, 1);
Jakob Stoklund Olesend7d0d4e2010-06-26 00:39:23 +00002643
Ahmed Bougacha12eb5582014-11-03 20:26:35 +00002644 if (Opcode == X86ISD::UDIVREM8_ZEXT_HREG ||
2645 Opcode == X86ISD::SDIVREM8_SEXT_HREG) {
2646 if (Node->getValueType(1) == MVT::i64) {
2647 // It's not possible to directly movsx AH to a 64bit register, because
2648 // the latter needs the REX prefix, but the former can't have it.
2649 assert(Opcode != X86ISD::SDIVREM8_SEXT_HREG &&
2650 "Unexpected i64 sext of h-register");
2651 Result =
2652 SDValue(CurDAG->getMachineNode(
2653 TargetOpcode::SUBREG_TO_REG, dl, MVT::i64,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002654 CurDAG->getTargetConstant(0, dl, MVT::i64), Result,
2655 CurDAG->getTargetConstant(X86::sub_32bit, dl,
2656 MVT::i32)),
Ahmed Bougacha12eb5582014-11-03 20:26:35 +00002657 0);
2658 }
2659 } else {
2660 Result =
2661 CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result);
2662 }
2663 ReplaceUses(SDValue(Node, 1), Result);
2664 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Jakob Stoklund Olesend7d0d4e2010-06-26 00:39:23 +00002665 }
Dan Gohman757eee82009-08-02 16:10:52 +00002666 // Copy the division (low) result, if it is needed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002667 if (!SDValue(Node, 0).use_empty()) {
Dan Gohman757eee82009-08-02 16:10:52 +00002668 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2669 LoReg, NVT, InFlag);
2670 InFlag = Result.getValue(2);
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002671 ReplaceUses(SDValue(Node, 0), Result);
Chris Lattnerf98f1242010-03-02 06:34:30 +00002672 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman757eee82009-08-02 16:10:52 +00002673 }
2674 // Copy the remainder (high) result, if it is needed.
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002675 if (!SDValue(Node, 1).use_empty()) {
Jakob Stoklund Olesend7d0d4e2010-06-26 00:39:23 +00002676 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2677 HiReg, NVT, InFlag);
2678 InFlag = Result.getValue(2);
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002679 ReplaceUses(SDValue(Node, 1), Result);
Chris Lattnerf98f1242010-03-02 06:34:30 +00002680 DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
Dan Gohman757eee82009-08-02 16:10:52 +00002681 }
Craig Topper062a2ba2014-04-25 05:30:21 +00002682 return nullptr;
Dan Gohman757eee82009-08-02 16:10:52 +00002683 }
2684
Manman Ren1be131b2012-08-08 00:51:41 +00002685 case X86ISD::CMP:
2686 case X86ISD::SUB: {
2687 // Sometimes a SUB is used to perform comparison.
2688 if (Opcode == X86ISD::SUB && Node->hasAnyUseOfValue(0))
2689 // This node is not a CMP.
2690 break;
Dan Gohmanac33a902009-08-19 18:16:17 +00002691 SDValue N0 = Node->getOperand(0);
2692 SDValue N1 = Node->getOperand(1);
2693
Elena Demikhovsky34d2d762014-08-18 11:59:06 +00002694 if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse() &&
Elena Demikhovskyd2cb3c82015-02-12 08:40:34 +00002695 HasNoSignedComparisonUses(Node))
Elena Demikhovsky34d2d762014-08-18 11:59:06 +00002696 N0 = N0.getOperand(0);
Elena Demikhovskyd2cb3c82015-02-12 08:40:34 +00002697
Dan Gohmanac33a902009-08-19 18:16:17 +00002698 // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
2699 // use a smaller encoding.
Elena Demikhovsky34d2d762014-08-18 11:59:06 +00002700 // Look past the truncate if CMP is the only use of it.
Dan Gohman198b7ff2011-11-03 21:49:52 +00002701 if ((N0.getNode()->getOpcode() == ISD::AND ||
2702 (N0.getResNo() == 0 && N0.getNode()->getOpcode() == X86ISD::AND)) &&
2703 N0.getNode()->hasOneUse() &&
Dan Gohmanac33a902009-08-19 18:16:17 +00002704 N0.getValueType() != MVT::i8 &&
2705 X86::isZeroNode(N1)) {
2706 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
2707 if (!C) break;
2708
2709 // For example, convert "testl %eax, $8" to "testb %al, $8"
Dan Gohman7d9dffb2009-10-09 20:35:19 +00002710 if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
2711 (!(C->getZExtValue() & 0x80) ||
2712 HasNoSignedComparisonUses(Node))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002713 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), dl, MVT::i8);
Dan Gohmanac33a902009-08-19 18:16:17 +00002714 SDValue Reg = N0.getNode()->getOperand(0);
2715
2716 // On x86-32, only the ABCD registers have 8-bit subregisters.
2717 if (!Subtarget->is64Bit()) {
Craig Toppercc830f82012-02-22 07:28:11 +00002718 const TargetRegisterClass *TRC;
Craig Topper56710102013-08-15 02:33:50 +00002719 switch (N0.getSimpleValueType().SimpleTy) {
Dan Gohmanac33a902009-08-19 18:16:17 +00002720 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2721 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2722 default: llvm_unreachable("Unsupported TEST operand type!");
2723 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002724 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32);
Dan Gohman32f71d72009-09-25 18:54:59 +00002725 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2726 Reg.getValueType(), Reg, RC), 0);
Dan Gohmanac33a902009-08-19 18:16:17 +00002727 }
2728
2729 // Extract the l-register.
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00002730 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl,
Dan Gohmanac33a902009-08-19 18:16:17 +00002731 MVT::i8, Reg);
2732
2733 // Emit a testb.
Manman Ren511c6d02012-09-28 18:53:24 +00002734 SDNode *NewNode = CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
2735 Subreg, Imm);
2736 // Replace SUB|CMP with TEST, since SUB has two outputs while TEST has
2737 // one, do not call ReplaceAllUsesWith.
2738 ReplaceUses(SDValue(Node, (Opcode == X86ISD::SUB ? 1 : 0)),
2739 SDValue(NewNode, 0));
Craig Topper062a2ba2014-04-25 05:30:21 +00002740 return nullptr;
Dan Gohmanac33a902009-08-19 18:16:17 +00002741 }
2742
2743 // For example, "testl %eax, $2048" to "testb %ah, $8".
Dan Gohman7d9dffb2009-10-09 20:35:19 +00002744 if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
2745 (!(C->getZExtValue() & 0x8000) ||
2746 HasNoSignedComparisonUses(Node))) {
Dan Gohmanac33a902009-08-19 18:16:17 +00002747 // Shift the immediate right by 8 bits.
2748 SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002749 dl, MVT::i8);
Dan Gohmanac33a902009-08-19 18:16:17 +00002750 SDValue Reg = N0.getNode()->getOperand(0);
2751
2752 // Put the value in an ABCD register.
Craig Toppercc830f82012-02-22 07:28:11 +00002753 const TargetRegisterClass *TRC;
Craig Topper56710102013-08-15 02:33:50 +00002754 switch (N0.getSimpleValueType().SimpleTy) {
Dan Gohmanac33a902009-08-19 18:16:17 +00002755 case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2756 case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2757 case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2758 default: llvm_unreachable("Unsupported TEST operand type!");
2759 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002760 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32);
Dan Gohman32f71d72009-09-25 18:54:59 +00002761 Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2762 Reg.getValueType(), Reg, RC), 0);
Dan Gohmanac33a902009-08-19 18:16:17 +00002763
2764 // Extract the h-register.
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00002765 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit_hi, dl,
Dan Gohmanac33a902009-08-19 18:16:17 +00002766 MVT::i8, Reg);
2767
Jakob Stoklund Olesen729abd32011-10-08 18:28:28 +00002768 // Emit a testb. The EXTRACT_SUBREG becomes a COPY that can only
2769 // target GR8_NOREX registers, so make sure the register class is
2770 // forced.
Manman Ren511c6d02012-09-28 18:53:24 +00002771 SDNode *NewNode = CurDAG->getMachineNode(X86::TEST8ri_NOREX, dl,
2772 MVT::i32, Subreg, ShiftedImm);
2773 // Replace SUB|CMP with TEST, since SUB has two outputs while TEST has
2774 // one, do not call ReplaceAllUsesWith.
2775 ReplaceUses(SDValue(Node, (Opcode == X86ISD::SUB ? 1 : 0)),
2776 SDValue(NewNode, 0));
Craig Topper062a2ba2014-04-25 05:30:21 +00002777 return nullptr;
Dan Gohmanac33a902009-08-19 18:16:17 +00002778 }
2779
2780 // For example, "testl %eax, $32776" to "testw %ax, $32776".
2781 if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
Dan Gohman7d9dffb2009-10-09 20:35:19 +00002782 N0.getValueType() != MVT::i16 &&
2783 (!(C->getZExtValue() & 0x8000) ||
2784 HasNoSignedComparisonUses(Node))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002785 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), dl,
2786 MVT::i16);
Dan Gohmanac33a902009-08-19 18:16:17 +00002787 SDValue Reg = N0.getNode()->getOperand(0);
2788
2789 // Extract the 16-bit subregister.
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00002790 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_16bit, dl,
Dan Gohmanac33a902009-08-19 18:16:17 +00002791 MVT::i16, Reg);
2792
2793 // Emit a testw.
Manman Ren511c6d02012-09-28 18:53:24 +00002794 SDNode *NewNode = CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32,
2795 Subreg, Imm);
2796 // Replace SUB|CMP with TEST, since SUB has two outputs while TEST has
2797 // one, do not call ReplaceAllUsesWith.
2798 ReplaceUses(SDValue(Node, (Opcode == X86ISD::SUB ? 1 : 0)),
2799 SDValue(NewNode, 0));
Craig Topper062a2ba2014-04-25 05:30:21 +00002800 return nullptr;
Dan Gohmanac33a902009-08-19 18:16:17 +00002801 }
2802
2803 // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2804 if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
Dan Gohman7d9dffb2009-10-09 20:35:19 +00002805 N0.getValueType() == MVT::i64 &&
2806 (!(C->getZExtValue() & 0x80000000) ||
2807 HasNoSignedComparisonUses(Node))) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002808 SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), dl,
2809 MVT::i32);
Dan Gohmanac33a902009-08-19 18:16:17 +00002810 SDValue Reg = N0.getNode()->getOperand(0);
2811
2812 // Extract the 32-bit subregister.
Jakob Stoklund Olesen9340ea52010-05-24 14:48:17 +00002813 SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_32bit, dl,
Dan Gohmanac33a902009-08-19 18:16:17 +00002814 MVT::i32, Reg);
2815
2816 // Emit a testl.
Manman Ren511c6d02012-09-28 18:53:24 +00002817 SDNode *NewNode = CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32,
2818 Subreg, Imm);
2819 // Replace SUB|CMP with TEST, since SUB has two outputs while TEST has
2820 // one, do not call ReplaceAllUsesWith.
2821 ReplaceUses(SDValue(Node, (Opcode == X86ISD::SUB ? 1 : 0)),
2822 SDValue(NewNode, 0));
Craig Topper062a2ba2014-04-25 05:30:21 +00002823 return nullptr;
Dan Gohmanac33a902009-08-19 18:16:17 +00002824 }
2825 }
2826 break;
2827 }
Pete Cooper7c7ba1b2011-11-15 21:57:53 +00002828 case ISD::STORE: {
Joel Jones68d59e82012-03-29 05:45:48 +00002829 // Change a chain of {load; incr or dec; store} of the same value into
2830 // a simple increment or decrement through memory of that value, if the
2831 // uses of the modified value and its address are suitable.
Pete Cooper48784ed2011-11-16 19:03:23 +00002832 // The DEC64m tablegen pattern is currently not able to match the case where
Chad Rosier24c19d22012-08-01 18:39:17 +00002833 // the EFLAGS on the original DEC are used. (This also applies to
Joel Jones68d59e82012-03-29 05:45:48 +00002834 // {INC,DEC}X{64,32,16,8}.)
2835 // We'll need to improve tablegen to allow flags to be transferred from a
Pete Cooper48784ed2011-11-16 19:03:23 +00002836 // node in the pattern to the result node. probably with a new keyword
2837 // for example, we have this
2838 // def DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst), "dec{q}\t$dst",
2839 // [(store (add (loadi64 addr:$dst), -1), addr:$dst),
2840 // (implicit EFLAGS)]>;
2841 // but maybe need something like this
2842 // def DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst), "dec{q}\t$dst",
2843 // [(store (add (loadi64 addr:$dst), -1), addr:$dst),
2844 // (transferrable EFLAGS)]>;
Joel Jones68d59e82012-03-29 05:45:48 +00002845
Pete Cooper7c7ba1b2011-11-15 21:57:53 +00002846 StoreSDNode *StoreNode = cast<StoreSDNode>(Node);
Pete Cooper7c7ba1b2011-11-15 21:57:53 +00002847 SDValue StoredVal = StoreNode->getOperand(1);
Joel Jones68d59e82012-03-29 05:45:48 +00002848 unsigned Opc = StoredVal->getOpcode();
Pete Cooper7c7ba1b2011-11-15 21:57:53 +00002849
Craig Topper062a2ba2014-04-25 05:30:21 +00002850 LoadSDNode *LoadNode = nullptr;
Evan Cheng3e869f02012-04-12 19:14:21 +00002851 SDValue InputChain;
2852 if (!isLoadIncOrDecStore(StoreNode, Opc, StoredVal, CurDAG,
2853 LoadNode, InputChain))
2854 break;
Pete Cooper7c7ba1b2011-11-15 21:57:53 +00002855
2856 SDValue Base, Scale, Index, Disp, Segment;
2857 if (!SelectAddr(LoadNode, LoadNode->getBasePtr(),
2858 Base, Scale, Index, Disp, Segment))
2859 break;
2860
2861 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(2);
2862 MemOp[0] = StoreNode->getMemOperand();
2863 MemOp[1] = LoadNode->getMemOperand();
2864 const SDValue Ops[] = { Base, Scale, Index, Disp, Segment, InputChain };
Chad Rosier24c19d22012-08-01 18:39:17 +00002865 EVT LdVT = LoadNode->getMemoryVT();
Joel Jones68d59e82012-03-29 05:45:48 +00002866 unsigned newOpc = getFusedLdStOpcode(LdVT, Opc);
2867 MachineSDNode *Result = CurDAG->getMachineNode(newOpc,
Andrew Trickef9de2a2013-05-25 02:42:55 +00002868 SDLoc(Node),
Michael Liaob53d8962013-04-19 22:22:57 +00002869 MVT::i32, MVT::Other, Ops);
Pete Cooper7c7ba1b2011-11-15 21:57:53 +00002870 Result->setMemRefs(MemOp, MemOp + 2);
2871
2872 ReplaceUses(SDValue(StoreNode, 0), SDValue(Result, 1));
2873 ReplaceUses(SDValue(StoredVal.getNode(), 1), SDValue(Result, 0));
2874
2875 return Result;
2876 }
Chris Lattner655e7df2005-11-16 01:54:32 +00002877 }
2878
Dan Gohmanea6f91f2010-01-05 01:24:18 +00002879 SDNode *ResNode = SelectCode(Node);
Evan Chengbd1c5a82006-08-11 09:08:15 +00002880
Chris Lattnerf98f1242010-03-02 06:34:30 +00002881 DEBUG(dbgs() << "=> ";
Craig Toppere73658d2014-04-28 04:05:08 +00002882 if (ResNode == nullptr || ResNode == Node)
Chris Lattnerf98f1242010-03-02 06:34:30 +00002883 Node->dump(CurDAG);
2884 else
2885 ResNode->dump(CurDAG);
2886 dbgs() << '\n');
Evan Chengbd1c5a82006-08-11 09:08:15 +00002887
2888 return ResNode;
Chris Lattner655e7df2005-11-16 01:54:32 +00002889}
2890
Chris Lattnerba1ed582006-06-08 18:03:49 +00002891bool X86DAGToDAGISel::
Daniel Sanders60f1db02015-03-13 12:45:09 +00002892SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
Dan Gohmaneb0cee92008-08-23 02:25:05 +00002893 std::vector<SDValue> &OutOps) {
Rafael Espindola3b2df102009-04-08 21:14:34 +00002894 SDValue Op0, Op1, Op2, Op3, Op4;
Daniel Sanders60f1db02015-03-13 12:45:09 +00002895 switch (ConstraintID) {
Daniel Sandersd0496692015-05-16 12:09:54 +00002896 default:
2897 llvm_unreachable("Unexpected asm memory constraint");
2898 case InlineAsm::Constraint_i:
2899 // FIXME: It seems strange that 'i' is needed here since it's supposed to
2900 // be an immediate and not a memory constraint.
2901 // Fallthrough.
Daniel Sanders60f1db02015-03-13 12:45:09 +00002902 case InlineAsm::Constraint_o: // offsetable ??
2903 case InlineAsm::Constraint_v: // not offsetable ??
Daniel Sanders60f1db02015-03-13 12:45:09 +00002904 case InlineAsm::Constraint_m: // memory
Daniel Sandersd0496692015-05-16 12:09:54 +00002905 case InlineAsm::Constraint_X:
Craig Topper062a2ba2014-04-25 05:30:21 +00002906 if (!SelectAddr(nullptr, Op, Op0, Op1, Op2, Op3, Op4))
Chris Lattnerba1ed582006-06-08 18:03:49 +00002907 return true;
2908 break;
2909 }
Chad Rosier24c19d22012-08-01 18:39:17 +00002910
Evan Cheng2d487222006-08-26 01:05:16 +00002911 OutOps.push_back(Op0);
2912 OutOps.push_back(Op1);
2913 OutOps.push_back(Op2);
2914 OutOps.push_back(Op3);
Rafael Espindola3b2df102009-04-08 21:14:34 +00002915 OutOps.push_back(Op4);
Chris Lattnerba1ed582006-06-08 18:03:49 +00002916 return false;
2917}
2918
Chad Rosier24c19d22012-08-01 18:39:17 +00002919/// createX86ISelDag - This pass converts a legalized DAG into a
Chris Lattner655e7df2005-11-16 01:54:32 +00002920/// X86-specific DAG, ready for instruction scheduling.
2921///
Bill Wendling026e5d72009-04-29 23:29:43 +00002922FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
Craig Topperf6e7e122012-03-27 07:21:54 +00002923 CodeGenOpt::Level OptLevel) {
Bill Wendling084669a2009-04-29 00:15:41 +00002924 return new X86DAGToDAGISel(TM, OptLevel);
Chris Lattner655e7df2005-11-16 01:54:32 +00002925}