xref: /aosp_15_r20/external/llvm/lib/Target/X86/X86ISelDAGToDAG.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- X86ISelDAGToDAG.cpp - A DAG pattern matching inst selector for X86 -===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines a DAG pattern matching instruction selector for X86,
11*9880d681SAndroid Build Coastguard Worker // converting from a legalized dag to a X86 dag.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker 
15*9880d681SAndroid Build Coastguard Worker #include "X86.h"
16*9880d681SAndroid Build Coastguard Worker #include "X86InstrBuilder.h"
17*9880d681SAndroid Build Coastguard Worker #include "X86MachineFunctionInfo.h"
18*9880d681SAndroid Build Coastguard Worker #include "X86RegisterInfo.h"
19*9880d681SAndroid Build Coastguard Worker #include "X86Subtarget.h"
20*9880d681SAndroid Build Coastguard Worker #include "X86TargetMachine.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFrameInfo.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunction.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/SelectionDAGISel.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Intrinsics.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ErrorHandling.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MathExtras.h"
34*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
35*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
36*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetOptions.h"
37*9880d681SAndroid Build Coastguard Worker #include <stdint.h>
38*9880d681SAndroid Build Coastguard Worker using namespace llvm;
39*9880d681SAndroid Build Coastguard Worker 
40*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "x86-isel"
41*9880d681SAndroid Build Coastguard Worker 
42*9880d681SAndroid Build Coastguard Worker STATISTIC(NumLoadMoved, "Number of loads moved below TokenFactor");
43*9880d681SAndroid Build Coastguard Worker 
44*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
45*9880d681SAndroid Build Coastguard Worker //                      Pattern Matcher Implementation
46*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
47*9880d681SAndroid Build Coastguard Worker 
48*9880d681SAndroid Build Coastguard Worker namespace {
49*9880d681SAndroid Build Coastguard Worker   /// This corresponds to X86AddressMode, but uses SDValue's instead of register
50*9880d681SAndroid Build Coastguard Worker   /// numbers for the leaves of the matched tree.
51*9880d681SAndroid Build Coastguard Worker   struct X86ISelAddressMode {
52*9880d681SAndroid Build Coastguard Worker     enum {
53*9880d681SAndroid Build Coastguard Worker       RegBase,
54*9880d681SAndroid Build Coastguard Worker       FrameIndexBase
55*9880d681SAndroid Build Coastguard Worker     } BaseType;
56*9880d681SAndroid Build Coastguard Worker 
57*9880d681SAndroid Build Coastguard Worker     // This is really a union, discriminated by BaseType!
58*9880d681SAndroid Build Coastguard Worker     SDValue Base_Reg;
59*9880d681SAndroid Build Coastguard Worker     int Base_FrameIndex;
60*9880d681SAndroid Build Coastguard Worker 
61*9880d681SAndroid Build Coastguard Worker     unsigned Scale;
62*9880d681SAndroid Build Coastguard Worker     SDValue IndexReg;
63*9880d681SAndroid Build Coastguard Worker     int32_t Disp;
64*9880d681SAndroid Build Coastguard Worker     SDValue Segment;
65*9880d681SAndroid Build Coastguard Worker     const GlobalValue *GV;
66*9880d681SAndroid Build Coastguard Worker     const Constant *CP;
67*9880d681SAndroid Build Coastguard Worker     const BlockAddress *BlockAddr;
68*9880d681SAndroid Build Coastguard Worker     const char *ES;
69*9880d681SAndroid Build Coastguard Worker     MCSymbol *MCSym;
70*9880d681SAndroid Build Coastguard Worker     int JT;
71*9880d681SAndroid Build Coastguard Worker     unsigned Align;    // CP alignment.
72*9880d681SAndroid Build Coastguard Worker     unsigned char SymbolFlags;  // X86II::MO_*
73*9880d681SAndroid Build Coastguard Worker 
X86ISelAddressMode__anon6d41d3610111::X86ISelAddressMode74*9880d681SAndroid Build Coastguard Worker     X86ISelAddressMode()
75*9880d681SAndroid Build Coastguard Worker         : BaseType(RegBase), Base_FrameIndex(0), Scale(1), IndexReg(), Disp(0),
76*9880d681SAndroid Build Coastguard Worker           Segment(), GV(nullptr), CP(nullptr), BlockAddr(nullptr), ES(nullptr),
77*9880d681SAndroid Build Coastguard Worker           MCSym(nullptr), JT(-1), Align(0), SymbolFlags(X86II::MO_NO_FLAG) {}
78*9880d681SAndroid Build Coastguard Worker 
hasSymbolicDisplacement__anon6d41d3610111::X86ISelAddressMode79*9880d681SAndroid Build Coastguard Worker     bool hasSymbolicDisplacement() const {
80*9880d681SAndroid Build Coastguard Worker       return GV != nullptr || CP != nullptr || ES != nullptr ||
81*9880d681SAndroid Build Coastguard Worker              MCSym != nullptr || JT != -1 || BlockAddr != nullptr;
82*9880d681SAndroid Build Coastguard Worker     }
83*9880d681SAndroid Build Coastguard Worker 
hasBaseOrIndexReg__anon6d41d3610111::X86ISelAddressMode84*9880d681SAndroid Build Coastguard Worker     bool hasBaseOrIndexReg() const {
85*9880d681SAndroid Build Coastguard Worker       return BaseType == FrameIndexBase ||
86*9880d681SAndroid Build Coastguard Worker              IndexReg.getNode() != nullptr || Base_Reg.getNode() != nullptr;
87*9880d681SAndroid Build Coastguard Worker     }
88*9880d681SAndroid Build Coastguard Worker 
89*9880d681SAndroid Build Coastguard Worker     /// Return true if this addressing mode is already RIP-relative.
isRIPRelative__anon6d41d3610111::X86ISelAddressMode90*9880d681SAndroid Build Coastguard Worker     bool isRIPRelative() const {
91*9880d681SAndroid Build Coastguard Worker       if (BaseType != RegBase) return false;
92*9880d681SAndroid Build Coastguard Worker       if (RegisterSDNode *RegNode =
93*9880d681SAndroid Build Coastguard Worker             dyn_cast_or_null<RegisterSDNode>(Base_Reg.getNode()))
94*9880d681SAndroid Build Coastguard Worker         return RegNode->getReg() == X86::RIP;
95*9880d681SAndroid Build Coastguard Worker       return false;
96*9880d681SAndroid Build Coastguard Worker     }
97*9880d681SAndroid Build Coastguard Worker 
setBaseReg__anon6d41d3610111::X86ISelAddressMode98*9880d681SAndroid Build Coastguard Worker     void setBaseReg(SDValue Reg) {
99*9880d681SAndroid Build Coastguard Worker       BaseType = RegBase;
100*9880d681SAndroid Build Coastguard Worker       Base_Reg = Reg;
101*9880d681SAndroid Build Coastguard Worker     }
102*9880d681SAndroid Build Coastguard Worker 
103*9880d681SAndroid Build Coastguard Worker #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump__anon6d41d3610111::X86ISelAddressMode104*9880d681SAndroid Build Coastguard Worker     void dump() {
105*9880d681SAndroid Build Coastguard Worker       dbgs() << "X86ISelAddressMode " << this << '\n';
106*9880d681SAndroid Build Coastguard Worker       dbgs() << "Base_Reg ";
107*9880d681SAndroid Build Coastguard Worker       if (Base_Reg.getNode())
108*9880d681SAndroid Build Coastguard Worker         Base_Reg.getNode()->dump();
109*9880d681SAndroid Build Coastguard Worker       else
110*9880d681SAndroid Build Coastguard Worker         dbgs() << "nul";
111*9880d681SAndroid Build Coastguard Worker       dbgs() << " Base.FrameIndex " << Base_FrameIndex << '\n'
112*9880d681SAndroid Build Coastguard Worker              << " Scale" << Scale << '\n'
113*9880d681SAndroid Build Coastguard Worker              << "IndexReg ";
114*9880d681SAndroid Build Coastguard Worker       if (IndexReg.getNode())
115*9880d681SAndroid Build Coastguard Worker         IndexReg.getNode()->dump();
116*9880d681SAndroid Build Coastguard Worker       else
117*9880d681SAndroid Build Coastguard Worker         dbgs() << "nul";
118*9880d681SAndroid Build Coastguard Worker       dbgs() << " Disp " << Disp << '\n'
119*9880d681SAndroid Build Coastguard Worker              << "GV ";
120*9880d681SAndroid Build Coastguard Worker       if (GV)
121*9880d681SAndroid Build Coastguard Worker         GV->dump();
122*9880d681SAndroid Build Coastguard Worker       else
123*9880d681SAndroid Build Coastguard Worker         dbgs() << "nul";
124*9880d681SAndroid Build Coastguard Worker       dbgs() << " CP ";
125*9880d681SAndroid Build Coastguard Worker       if (CP)
126*9880d681SAndroid Build Coastguard Worker         CP->dump();
127*9880d681SAndroid Build Coastguard Worker       else
128*9880d681SAndroid Build Coastguard Worker         dbgs() << "nul";
129*9880d681SAndroid Build Coastguard Worker       dbgs() << '\n'
130*9880d681SAndroid Build Coastguard Worker              << "ES ";
131*9880d681SAndroid Build Coastguard Worker       if (ES)
132*9880d681SAndroid Build Coastguard Worker         dbgs() << ES;
133*9880d681SAndroid Build Coastguard Worker       else
134*9880d681SAndroid Build Coastguard Worker         dbgs() << "nul";
135*9880d681SAndroid Build Coastguard Worker       dbgs() << " MCSym ";
136*9880d681SAndroid Build Coastguard Worker       if (MCSym)
137*9880d681SAndroid Build Coastguard Worker         dbgs() << MCSym;
138*9880d681SAndroid Build Coastguard Worker       else
139*9880d681SAndroid Build Coastguard Worker         dbgs() << "nul";
140*9880d681SAndroid Build Coastguard Worker       dbgs() << " JT" << JT << " Align" << Align << '\n';
141*9880d681SAndroid Build Coastguard Worker     }
142*9880d681SAndroid Build Coastguard Worker #endif
143*9880d681SAndroid Build Coastguard Worker   };
144*9880d681SAndroid Build Coastguard Worker }
145*9880d681SAndroid Build Coastguard Worker 
146*9880d681SAndroid Build Coastguard Worker namespace {
147*9880d681SAndroid Build Coastguard Worker   //===--------------------------------------------------------------------===//
148*9880d681SAndroid Build Coastguard Worker   /// ISel - X86-specific code to select X86 machine instructions for
149*9880d681SAndroid Build Coastguard Worker   /// SelectionDAG operations.
150*9880d681SAndroid Build Coastguard Worker   ///
151*9880d681SAndroid Build Coastguard Worker   class X86DAGToDAGISel final : public SelectionDAGISel {
152*9880d681SAndroid Build Coastguard Worker     /// Keep a pointer to the X86Subtarget around so that we can
153*9880d681SAndroid Build Coastguard Worker     /// make the right decision when generating code for different targets.
154*9880d681SAndroid Build Coastguard Worker     const X86Subtarget *Subtarget;
155*9880d681SAndroid Build Coastguard Worker 
156*9880d681SAndroid Build Coastguard Worker     /// If true, selector should try to optimize for code size instead of
157*9880d681SAndroid Build Coastguard Worker     /// performance.
158*9880d681SAndroid Build Coastguard Worker     bool OptForSize;
159*9880d681SAndroid Build Coastguard Worker 
160*9880d681SAndroid Build Coastguard Worker     /// If true, selector should try to optimize for minimum code size.
161*9880d681SAndroid Build Coastguard Worker     bool OptForMinSize;
162*9880d681SAndroid Build Coastguard Worker 
163*9880d681SAndroid Build Coastguard Worker   public:
X86DAGToDAGISel(X86TargetMachine & tm,CodeGenOpt::Level OptLevel)164*9880d681SAndroid Build Coastguard Worker     explicit X86DAGToDAGISel(X86TargetMachine &tm, CodeGenOpt::Level OptLevel)
165*9880d681SAndroid Build Coastguard Worker         : SelectionDAGISel(tm, OptLevel), OptForSize(false),
166*9880d681SAndroid Build Coastguard Worker           OptForMinSize(false) {}
167*9880d681SAndroid Build Coastguard Worker 
getPassName() const168*9880d681SAndroid Build Coastguard Worker     const char *getPassName() const override {
169*9880d681SAndroid Build Coastguard Worker       return "X86 DAG->DAG Instruction Selection";
170*9880d681SAndroid Build Coastguard Worker     }
171*9880d681SAndroid Build Coastguard Worker 
runOnMachineFunction(MachineFunction & MF)172*9880d681SAndroid Build Coastguard Worker     bool runOnMachineFunction(MachineFunction &MF) override {
173*9880d681SAndroid Build Coastguard Worker       // Reset the subtarget each time through.
174*9880d681SAndroid Build Coastguard Worker       Subtarget = &MF.getSubtarget<X86Subtarget>();
175*9880d681SAndroid Build Coastguard Worker       SelectionDAGISel::runOnMachineFunction(MF);
176*9880d681SAndroid Build Coastguard Worker       return true;
177*9880d681SAndroid Build Coastguard Worker     }
178*9880d681SAndroid Build Coastguard Worker 
179*9880d681SAndroid Build Coastguard Worker     void EmitFunctionEntryCode() override;
180*9880d681SAndroid Build Coastguard Worker 
181*9880d681SAndroid Build Coastguard Worker     bool IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const override;
182*9880d681SAndroid Build Coastguard Worker 
183*9880d681SAndroid Build Coastguard Worker     void PreprocessISelDAG() override;
184*9880d681SAndroid Build Coastguard Worker 
immSext8(SDNode * N) const185*9880d681SAndroid Build Coastguard Worker     inline bool immSext8(SDNode *N) const {
186*9880d681SAndroid Build Coastguard Worker       return isInt<8>(cast<ConstantSDNode>(N)->getSExtValue());
187*9880d681SAndroid Build Coastguard Worker     }
188*9880d681SAndroid Build Coastguard Worker 
189*9880d681SAndroid Build Coastguard Worker     // True if the 64-bit immediate fits in a 32-bit sign-extended field.
i64immSExt32(SDNode * N) const190*9880d681SAndroid Build Coastguard Worker     inline bool i64immSExt32(SDNode *N) const {
191*9880d681SAndroid Build Coastguard Worker       uint64_t v = cast<ConstantSDNode>(N)->getZExtValue();
192*9880d681SAndroid Build Coastguard Worker       return (int64_t)v == (int32_t)v;
193*9880d681SAndroid Build Coastguard Worker     }
194*9880d681SAndroid Build Coastguard Worker 
195*9880d681SAndroid Build Coastguard Worker // Include the pieces autogenerated from the target description.
196*9880d681SAndroid Build Coastguard Worker #include "X86GenDAGISel.inc"
197*9880d681SAndroid Build Coastguard Worker 
198*9880d681SAndroid Build Coastguard Worker   private:
199*9880d681SAndroid Build Coastguard Worker     void Select(SDNode *N) override;
200*9880d681SAndroid Build Coastguard Worker     bool tryGather(SDNode *N, unsigned Opc);
201*9880d681SAndroid Build Coastguard Worker 
202*9880d681SAndroid Build Coastguard Worker     bool foldOffsetIntoAddress(uint64_t Offset, X86ISelAddressMode &AM);
203*9880d681SAndroid Build Coastguard Worker     bool matchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM);
204*9880d681SAndroid Build Coastguard Worker     bool matchWrapper(SDValue N, X86ISelAddressMode &AM);
205*9880d681SAndroid Build Coastguard Worker     bool matchAddress(SDValue N, X86ISelAddressMode &AM);
206*9880d681SAndroid Build Coastguard Worker     bool matchAdd(SDValue N, X86ISelAddressMode &AM, unsigned Depth);
207*9880d681SAndroid Build Coastguard Worker     bool matchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
208*9880d681SAndroid Build Coastguard Worker                                  unsigned Depth);
209*9880d681SAndroid Build Coastguard Worker     bool matchAddressBase(SDValue N, X86ISelAddressMode &AM);
210*9880d681SAndroid Build Coastguard Worker     bool selectAddr(SDNode *Parent, SDValue N, SDValue &Base,
211*9880d681SAndroid Build Coastguard Worker                     SDValue &Scale, SDValue &Index, SDValue &Disp,
212*9880d681SAndroid Build Coastguard Worker                     SDValue &Segment);
213*9880d681SAndroid Build Coastguard Worker     bool selectVectorAddr(SDNode *Parent, SDValue N, SDValue &Base,
214*9880d681SAndroid Build Coastguard Worker                           SDValue &Scale, SDValue &Index, SDValue &Disp,
215*9880d681SAndroid Build Coastguard Worker                           SDValue &Segment);
216*9880d681SAndroid Build Coastguard Worker     bool selectMOV64Imm32(SDValue N, SDValue &Imm);
217*9880d681SAndroid Build Coastguard Worker     bool selectLEAAddr(SDValue N, SDValue &Base,
218*9880d681SAndroid Build Coastguard Worker                        SDValue &Scale, SDValue &Index, SDValue &Disp,
219*9880d681SAndroid Build Coastguard Worker                        SDValue &Segment);
220*9880d681SAndroid Build Coastguard Worker     bool selectLEA64_32Addr(SDValue N, SDValue &Base,
221*9880d681SAndroid Build Coastguard Worker                             SDValue &Scale, SDValue &Index, SDValue &Disp,
222*9880d681SAndroid Build Coastguard Worker                             SDValue &Segment);
223*9880d681SAndroid Build Coastguard Worker     bool selectTLSADDRAddr(SDValue N, SDValue &Base,
224*9880d681SAndroid Build Coastguard Worker                            SDValue &Scale, SDValue &Index, SDValue &Disp,
225*9880d681SAndroid Build Coastguard Worker                            SDValue &Segment);
226*9880d681SAndroid Build Coastguard Worker     bool selectScalarSSELoad(SDNode *Root, SDValue N,
227*9880d681SAndroid Build Coastguard Worker                              SDValue &Base, SDValue &Scale,
228*9880d681SAndroid Build Coastguard Worker                              SDValue &Index, SDValue &Disp,
229*9880d681SAndroid Build Coastguard Worker                              SDValue &Segment,
230*9880d681SAndroid Build Coastguard Worker                              SDValue &NodeWithChain);
231*9880d681SAndroid Build Coastguard Worker 
232*9880d681SAndroid Build Coastguard Worker     bool tryFoldLoad(SDNode *P, SDValue N,
233*9880d681SAndroid Build Coastguard Worker                      SDValue &Base, SDValue &Scale,
234*9880d681SAndroid Build Coastguard Worker                      SDValue &Index, SDValue &Disp,
235*9880d681SAndroid Build Coastguard Worker                      SDValue &Segment);
236*9880d681SAndroid Build Coastguard Worker 
237*9880d681SAndroid Build Coastguard Worker     /// Implement addressing mode selection for inline asm expressions.
238*9880d681SAndroid Build Coastguard Worker     bool SelectInlineAsmMemoryOperand(const SDValue &Op,
239*9880d681SAndroid Build Coastguard Worker                                       unsigned ConstraintID,
240*9880d681SAndroid Build Coastguard Worker                                       std::vector<SDValue> &OutOps) override;
241*9880d681SAndroid Build Coastguard Worker 
242*9880d681SAndroid Build Coastguard Worker     void emitSpecialCodeForMain();
243*9880d681SAndroid Build Coastguard Worker 
getAddressOperands(X86ISelAddressMode & AM,const SDLoc & DL,SDValue & Base,SDValue & Scale,SDValue & Index,SDValue & Disp,SDValue & Segment)244*9880d681SAndroid Build Coastguard Worker     inline void getAddressOperands(X86ISelAddressMode &AM, const SDLoc &DL,
245*9880d681SAndroid Build Coastguard Worker                                    SDValue &Base, SDValue &Scale,
246*9880d681SAndroid Build Coastguard Worker                                    SDValue &Index, SDValue &Disp,
247*9880d681SAndroid Build Coastguard Worker                                    SDValue &Segment) {
248*9880d681SAndroid Build Coastguard Worker       Base = (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
249*9880d681SAndroid Build Coastguard Worker                  ? CurDAG->getTargetFrameIndex(
250*9880d681SAndroid Build Coastguard Worker                        AM.Base_FrameIndex,
251*9880d681SAndroid Build Coastguard Worker                        TLI->getPointerTy(CurDAG->getDataLayout()))
252*9880d681SAndroid Build Coastguard Worker                  : AM.Base_Reg;
253*9880d681SAndroid Build Coastguard Worker       Scale = getI8Imm(AM.Scale, DL);
254*9880d681SAndroid Build Coastguard Worker       Index = AM.IndexReg;
255*9880d681SAndroid Build Coastguard Worker       // These are 32-bit even in 64-bit mode since RIP-relative offset
256*9880d681SAndroid Build Coastguard Worker       // is 32-bit.
257*9880d681SAndroid Build Coastguard Worker       if (AM.GV)
258*9880d681SAndroid Build Coastguard Worker         Disp = CurDAG->getTargetGlobalAddress(AM.GV, SDLoc(),
259*9880d681SAndroid Build Coastguard Worker                                               MVT::i32, AM.Disp,
260*9880d681SAndroid Build Coastguard Worker                                               AM.SymbolFlags);
261*9880d681SAndroid Build Coastguard Worker       else if (AM.CP)
262*9880d681SAndroid Build Coastguard Worker         Disp = CurDAG->getTargetConstantPool(AM.CP, MVT::i32,
263*9880d681SAndroid Build Coastguard Worker                                              AM.Align, AM.Disp, AM.SymbolFlags);
264*9880d681SAndroid Build Coastguard Worker       else if (AM.ES) {
265*9880d681SAndroid Build Coastguard Worker         assert(!AM.Disp && "Non-zero displacement is ignored with ES.");
266*9880d681SAndroid Build Coastguard Worker         Disp = CurDAG->getTargetExternalSymbol(AM.ES, MVT::i32, AM.SymbolFlags);
267*9880d681SAndroid Build Coastguard Worker       } else if (AM.MCSym) {
268*9880d681SAndroid Build Coastguard Worker         assert(!AM.Disp && "Non-zero displacement is ignored with MCSym.");
269*9880d681SAndroid Build Coastguard Worker         assert(AM.SymbolFlags == 0 && "oo");
270*9880d681SAndroid Build Coastguard Worker         Disp = CurDAG->getMCSymbol(AM.MCSym, MVT::i32);
271*9880d681SAndroid Build Coastguard Worker       } else if (AM.JT != -1) {
272*9880d681SAndroid Build Coastguard Worker         assert(!AM.Disp && "Non-zero displacement is ignored with JT.");
273*9880d681SAndroid Build Coastguard Worker         Disp = CurDAG->getTargetJumpTable(AM.JT, MVT::i32, AM.SymbolFlags);
274*9880d681SAndroid Build Coastguard Worker       } else if (AM.BlockAddr)
275*9880d681SAndroid Build Coastguard Worker         Disp = CurDAG->getTargetBlockAddress(AM.BlockAddr, MVT::i32, AM.Disp,
276*9880d681SAndroid Build Coastguard Worker                                              AM.SymbolFlags);
277*9880d681SAndroid Build Coastguard Worker       else
278*9880d681SAndroid Build Coastguard Worker         Disp = CurDAG->getTargetConstant(AM.Disp, DL, MVT::i32);
279*9880d681SAndroid Build Coastguard Worker 
280*9880d681SAndroid Build Coastguard Worker       if (AM.Segment.getNode())
281*9880d681SAndroid Build Coastguard Worker         Segment = AM.Segment;
282*9880d681SAndroid Build Coastguard Worker       else
283*9880d681SAndroid Build Coastguard Worker         Segment = CurDAG->getRegister(0, MVT::i32);
284*9880d681SAndroid Build Coastguard Worker     }
285*9880d681SAndroid Build Coastguard Worker 
286*9880d681SAndroid Build Coastguard Worker     // Utility function to determine whether we should avoid selecting
287*9880d681SAndroid Build Coastguard Worker     // immediate forms of instructions for better code size or not.
288*9880d681SAndroid Build Coastguard Worker     // At a high level, we'd like to avoid such instructions when
289*9880d681SAndroid Build Coastguard Worker     // we have similar constants used within the same basic block
290*9880d681SAndroid Build Coastguard Worker     // that can be kept in a register.
291*9880d681SAndroid Build Coastguard Worker     //
shouldAvoidImmediateInstFormsForSize(SDNode * N) const292*9880d681SAndroid Build Coastguard Worker     bool shouldAvoidImmediateInstFormsForSize(SDNode *N) const {
293*9880d681SAndroid Build Coastguard Worker       uint32_t UseCount = 0;
294*9880d681SAndroid Build Coastguard Worker 
295*9880d681SAndroid Build Coastguard Worker       // Do not want to hoist if we're not optimizing for size.
296*9880d681SAndroid Build Coastguard Worker       // TODO: We'd like to remove this restriction.
297*9880d681SAndroid Build Coastguard Worker       // See the comment in X86InstrInfo.td for more info.
298*9880d681SAndroid Build Coastguard Worker       if (!OptForSize)
299*9880d681SAndroid Build Coastguard Worker         return false;
300*9880d681SAndroid Build Coastguard Worker 
301*9880d681SAndroid Build Coastguard Worker       // Walk all the users of the immediate.
302*9880d681SAndroid Build Coastguard Worker       for (SDNode::use_iterator UI = N->use_begin(),
303*9880d681SAndroid Build Coastguard Worker            UE = N->use_end(); (UI != UE) && (UseCount < 2); ++UI) {
304*9880d681SAndroid Build Coastguard Worker 
305*9880d681SAndroid Build Coastguard Worker         SDNode *User = *UI;
306*9880d681SAndroid Build Coastguard Worker 
307*9880d681SAndroid Build Coastguard Worker         // This user is already selected. Count it as a legitimate use and
308*9880d681SAndroid Build Coastguard Worker         // move on.
309*9880d681SAndroid Build Coastguard Worker         if (User->isMachineOpcode()) {
310*9880d681SAndroid Build Coastguard Worker           UseCount++;
311*9880d681SAndroid Build Coastguard Worker           continue;
312*9880d681SAndroid Build Coastguard Worker         }
313*9880d681SAndroid Build Coastguard Worker 
314*9880d681SAndroid Build Coastguard Worker         // We want to count stores of immediates as real uses.
315*9880d681SAndroid Build Coastguard Worker         if (User->getOpcode() == ISD::STORE &&
316*9880d681SAndroid Build Coastguard Worker             User->getOperand(1).getNode() == N) {
317*9880d681SAndroid Build Coastguard Worker           UseCount++;
318*9880d681SAndroid Build Coastguard Worker           continue;
319*9880d681SAndroid Build Coastguard Worker         }
320*9880d681SAndroid Build Coastguard Worker 
321*9880d681SAndroid Build Coastguard Worker         // We don't currently match users that have > 2 operands (except
322*9880d681SAndroid Build Coastguard Worker         // for stores, which are handled above)
323*9880d681SAndroid Build Coastguard Worker         // Those instruction won't match in ISEL, for now, and would
324*9880d681SAndroid Build Coastguard Worker         // be counted incorrectly.
325*9880d681SAndroid Build Coastguard Worker         // This may change in the future as we add additional instruction
326*9880d681SAndroid Build Coastguard Worker         // types.
327*9880d681SAndroid Build Coastguard Worker         if (User->getNumOperands() != 2)
328*9880d681SAndroid Build Coastguard Worker           continue;
329*9880d681SAndroid Build Coastguard Worker 
330*9880d681SAndroid Build Coastguard Worker         // Immediates that are used for offsets as part of stack
331*9880d681SAndroid Build Coastguard Worker         // manipulation should be left alone. These are typically
332*9880d681SAndroid Build Coastguard Worker         // used to indicate SP offsets for argument passing and
333*9880d681SAndroid Build Coastguard Worker         // will get pulled into stores/pushes (implicitly).
334*9880d681SAndroid Build Coastguard Worker         if (User->getOpcode() == X86ISD::ADD ||
335*9880d681SAndroid Build Coastguard Worker             User->getOpcode() == ISD::ADD    ||
336*9880d681SAndroid Build Coastguard Worker             User->getOpcode() == X86ISD::SUB ||
337*9880d681SAndroid Build Coastguard Worker             User->getOpcode() == ISD::SUB) {
338*9880d681SAndroid Build Coastguard Worker 
339*9880d681SAndroid Build Coastguard Worker           // Find the other operand of the add/sub.
340*9880d681SAndroid Build Coastguard Worker           SDValue OtherOp = User->getOperand(0);
341*9880d681SAndroid Build Coastguard Worker           if (OtherOp.getNode() == N)
342*9880d681SAndroid Build Coastguard Worker             OtherOp = User->getOperand(1);
343*9880d681SAndroid Build Coastguard Worker 
344*9880d681SAndroid Build Coastguard Worker           // Don't count if the other operand is SP.
345*9880d681SAndroid Build Coastguard Worker           RegisterSDNode *RegNode;
346*9880d681SAndroid Build Coastguard Worker           if (OtherOp->getOpcode() == ISD::CopyFromReg &&
347*9880d681SAndroid Build Coastguard Worker               (RegNode = dyn_cast_or_null<RegisterSDNode>(
348*9880d681SAndroid Build Coastguard Worker                  OtherOp->getOperand(1).getNode())))
349*9880d681SAndroid Build Coastguard Worker             if ((RegNode->getReg() == X86::ESP) ||
350*9880d681SAndroid Build Coastguard Worker                 (RegNode->getReg() == X86::RSP))
351*9880d681SAndroid Build Coastguard Worker               continue;
352*9880d681SAndroid Build Coastguard Worker         }
353*9880d681SAndroid Build Coastguard Worker 
354*9880d681SAndroid Build Coastguard Worker         // ... otherwise, count this and move on.
355*9880d681SAndroid Build Coastguard Worker         UseCount++;
356*9880d681SAndroid Build Coastguard Worker       }
357*9880d681SAndroid Build Coastguard Worker 
358*9880d681SAndroid Build Coastguard Worker       // If we have more than 1 use, then recommend for hoisting.
359*9880d681SAndroid Build Coastguard Worker       return (UseCount > 1);
360*9880d681SAndroid Build Coastguard Worker     }
361*9880d681SAndroid Build Coastguard Worker 
362*9880d681SAndroid Build Coastguard Worker     /// Return a target constant with the specified value of type i8.
getI8Imm(unsigned Imm,const SDLoc & DL)363*9880d681SAndroid Build Coastguard Worker     inline SDValue getI8Imm(unsigned Imm, const SDLoc &DL) {
364*9880d681SAndroid Build Coastguard Worker       return CurDAG->getTargetConstant(Imm, DL, MVT::i8);
365*9880d681SAndroid Build Coastguard Worker     }
366*9880d681SAndroid Build Coastguard Worker 
367*9880d681SAndroid Build Coastguard Worker     /// Return a target constant with the specified value, of type i32.
getI32Imm(unsigned Imm,const SDLoc & DL)368*9880d681SAndroid Build Coastguard Worker     inline SDValue getI32Imm(unsigned Imm, const SDLoc &DL) {
369*9880d681SAndroid Build Coastguard Worker       return CurDAG->getTargetConstant(Imm, DL, MVT::i32);
370*9880d681SAndroid Build Coastguard Worker     }
371*9880d681SAndroid Build Coastguard Worker 
372*9880d681SAndroid Build Coastguard Worker     /// Return an SDNode that returns the value of the global base register.
373*9880d681SAndroid Build Coastguard Worker     /// Output instructions required to initialize the global base register,
374*9880d681SAndroid Build Coastguard Worker     /// if necessary.
375*9880d681SAndroid Build Coastguard Worker     SDNode *getGlobalBaseReg();
376*9880d681SAndroid Build Coastguard Worker 
377*9880d681SAndroid Build Coastguard Worker     /// Return a reference to the TargetMachine, casted to the target-specific
378*9880d681SAndroid Build Coastguard Worker     /// type.
getTargetMachine() const379*9880d681SAndroid Build Coastguard Worker     const X86TargetMachine &getTargetMachine() const {
380*9880d681SAndroid Build Coastguard Worker       return static_cast<const X86TargetMachine &>(TM);
381*9880d681SAndroid Build Coastguard Worker     }
382*9880d681SAndroid Build Coastguard Worker 
383*9880d681SAndroid Build Coastguard Worker     /// Return a reference to the TargetInstrInfo, casted to the target-specific
384*9880d681SAndroid Build Coastguard Worker     /// type.
getInstrInfo() const385*9880d681SAndroid Build Coastguard Worker     const X86InstrInfo *getInstrInfo() const {
386*9880d681SAndroid Build Coastguard Worker       return Subtarget->getInstrInfo();
387*9880d681SAndroid Build Coastguard Worker     }
388*9880d681SAndroid Build Coastguard Worker 
389*9880d681SAndroid Build Coastguard Worker     /// \brief Address-mode matching performs shift-of-and to and-of-shift
390*9880d681SAndroid Build Coastguard Worker     /// reassociation in order to expose more scaled addressing
391*9880d681SAndroid Build Coastguard Worker     /// opportunities.
ComplexPatternFuncMutatesDAG() const392*9880d681SAndroid Build Coastguard Worker     bool ComplexPatternFuncMutatesDAG() const override {
393*9880d681SAndroid Build Coastguard Worker       return true;
394*9880d681SAndroid Build Coastguard Worker     }
395*9880d681SAndroid Build Coastguard Worker   };
396*9880d681SAndroid Build Coastguard Worker }
397*9880d681SAndroid Build Coastguard Worker 
398*9880d681SAndroid Build Coastguard Worker 
399*9880d681SAndroid Build Coastguard Worker bool
IsProfitableToFold(SDValue N,SDNode * U,SDNode * Root) const400*9880d681SAndroid Build Coastguard Worker X86DAGToDAGISel::IsProfitableToFold(SDValue N, SDNode *U, SDNode *Root) const {
401*9880d681SAndroid Build Coastguard Worker   if (OptLevel == CodeGenOpt::None) return false;
402*9880d681SAndroid Build Coastguard Worker 
403*9880d681SAndroid Build Coastguard Worker   if (!N.hasOneUse())
404*9880d681SAndroid Build Coastguard Worker     return false;
405*9880d681SAndroid Build Coastguard Worker 
406*9880d681SAndroid Build Coastguard Worker   if (N.getOpcode() != ISD::LOAD)
407*9880d681SAndroid Build Coastguard Worker     return true;
408*9880d681SAndroid Build Coastguard Worker 
409*9880d681SAndroid Build Coastguard Worker   // If N is a load, do additional profitability checks.
410*9880d681SAndroid Build Coastguard Worker   if (U == Root) {
411*9880d681SAndroid Build Coastguard Worker     switch (U->getOpcode()) {
412*9880d681SAndroid Build Coastguard Worker     default: break;
413*9880d681SAndroid Build Coastguard Worker     case X86ISD::ADD:
414*9880d681SAndroid Build Coastguard Worker     case X86ISD::SUB:
415*9880d681SAndroid Build Coastguard Worker     case X86ISD::AND:
416*9880d681SAndroid Build Coastguard Worker     case X86ISD::XOR:
417*9880d681SAndroid Build Coastguard Worker     case X86ISD::OR:
418*9880d681SAndroid Build Coastguard Worker     case ISD::ADD:
419*9880d681SAndroid Build Coastguard Worker     case ISD::ADDC:
420*9880d681SAndroid Build Coastguard Worker     case ISD::ADDE:
421*9880d681SAndroid Build Coastguard Worker     case ISD::AND:
422*9880d681SAndroid Build Coastguard Worker     case ISD::OR:
423*9880d681SAndroid Build Coastguard Worker     case ISD::XOR: {
424*9880d681SAndroid Build Coastguard Worker       SDValue Op1 = U->getOperand(1);
425*9880d681SAndroid Build Coastguard Worker 
426*9880d681SAndroid Build Coastguard Worker       // If the other operand is a 8-bit immediate we should fold the immediate
427*9880d681SAndroid Build Coastguard Worker       // instead. This reduces code size.
428*9880d681SAndroid Build Coastguard Worker       // e.g.
429*9880d681SAndroid Build Coastguard Worker       // movl 4(%esp), %eax
430*9880d681SAndroid Build Coastguard Worker       // addl $4, %eax
431*9880d681SAndroid Build Coastguard Worker       // vs.
432*9880d681SAndroid Build Coastguard Worker       // movl $4, %eax
433*9880d681SAndroid Build Coastguard Worker       // addl 4(%esp), %eax
434*9880d681SAndroid Build Coastguard Worker       // The former is 2 bytes shorter. In case where the increment is 1, then
435*9880d681SAndroid Build Coastguard Worker       // the saving can be 4 bytes (by using incl %eax).
436*9880d681SAndroid Build Coastguard Worker       if (ConstantSDNode *Imm = dyn_cast<ConstantSDNode>(Op1))
437*9880d681SAndroid Build Coastguard Worker         if (Imm->getAPIntValue().isSignedIntN(8))
438*9880d681SAndroid Build Coastguard Worker           return false;
439*9880d681SAndroid Build Coastguard Worker 
440*9880d681SAndroid Build Coastguard Worker       // If the other operand is a TLS address, we should fold it instead.
441*9880d681SAndroid Build Coastguard Worker       // This produces
442*9880d681SAndroid Build Coastguard Worker       // movl    %gs:0, %eax
443*9880d681SAndroid Build Coastguard Worker       // leal    i@NTPOFF(%eax), %eax
444*9880d681SAndroid Build Coastguard Worker       // instead of
445*9880d681SAndroid Build Coastguard Worker       // movl    $i@NTPOFF, %eax
446*9880d681SAndroid Build Coastguard Worker       // addl    %gs:0, %eax
447*9880d681SAndroid Build Coastguard Worker       // if the block also has an access to a second TLS address this will save
448*9880d681SAndroid Build Coastguard Worker       // a load.
449*9880d681SAndroid Build Coastguard Worker       // FIXME: This is probably also true for non-TLS addresses.
450*9880d681SAndroid Build Coastguard Worker       if (Op1.getOpcode() == X86ISD::Wrapper) {
451*9880d681SAndroid Build Coastguard Worker         SDValue Val = Op1.getOperand(0);
452*9880d681SAndroid Build Coastguard Worker         if (Val.getOpcode() == ISD::TargetGlobalTLSAddress)
453*9880d681SAndroid Build Coastguard Worker           return false;
454*9880d681SAndroid Build Coastguard Worker       }
455*9880d681SAndroid Build Coastguard Worker     }
456*9880d681SAndroid Build Coastguard Worker     }
457*9880d681SAndroid Build Coastguard Worker   }
458*9880d681SAndroid Build Coastguard Worker 
459*9880d681SAndroid Build Coastguard Worker   return true;
460*9880d681SAndroid Build Coastguard Worker }
461*9880d681SAndroid Build Coastguard Worker 
462*9880d681SAndroid Build Coastguard Worker /// Replace the original chain operand of the call with
463*9880d681SAndroid Build Coastguard Worker /// load's chain operand and move load below the call's chain operand.
moveBelowOrigChain(SelectionDAG * CurDAG,SDValue Load,SDValue Call,SDValue OrigChain)464*9880d681SAndroid Build Coastguard Worker static void moveBelowOrigChain(SelectionDAG *CurDAG, SDValue Load,
465*9880d681SAndroid Build Coastguard Worker                                SDValue Call, SDValue OrigChain) {
466*9880d681SAndroid Build Coastguard Worker   SmallVector<SDValue, 8> Ops;
467*9880d681SAndroid Build Coastguard Worker   SDValue Chain = OrigChain.getOperand(0);
468*9880d681SAndroid Build Coastguard Worker   if (Chain.getNode() == Load.getNode())
469*9880d681SAndroid Build Coastguard Worker     Ops.push_back(Load.getOperand(0));
470*9880d681SAndroid Build Coastguard Worker   else {
471*9880d681SAndroid Build Coastguard Worker     assert(Chain.getOpcode() == ISD::TokenFactor &&
472*9880d681SAndroid Build Coastguard Worker            "Unexpected chain operand");
473*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i)
474*9880d681SAndroid Build Coastguard Worker       if (Chain.getOperand(i).getNode() == Load.getNode())
475*9880d681SAndroid Build Coastguard Worker         Ops.push_back(Load.getOperand(0));
476*9880d681SAndroid Build Coastguard Worker       else
477*9880d681SAndroid Build Coastguard Worker         Ops.push_back(Chain.getOperand(i));
478*9880d681SAndroid Build Coastguard Worker     SDValue NewChain =
479*9880d681SAndroid Build Coastguard Worker       CurDAG->getNode(ISD::TokenFactor, SDLoc(Load), MVT::Other, Ops);
480*9880d681SAndroid Build Coastguard Worker     Ops.clear();
481*9880d681SAndroid Build Coastguard Worker     Ops.push_back(NewChain);
482*9880d681SAndroid Build Coastguard Worker   }
483*9880d681SAndroid Build Coastguard Worker   Ops.append(OrigChain->op_begin() + 1, OrigChain->op_end());
484*9880d681SAndroid Build Coastguard Worker   CurDAG->UpdateNodeOperands(OrigChain.getNode(), Ops);
485*9880d681SAndroid Build Coastguard Worker   CurDAG->UpdateNodeOperands(Load.getNode(), Call.getOperand(0),
486*9880d681SAndroid Build Coastguard Worker                              Load.getOperand(1), Load.getOperand(2));
487*9880d681SAndroid Build Coastguard Worker 
488*9880d681SAndroid Build Coastguard Worker   Ops.clear();
489*9880d681SAndroid Build Coastguard Worker   Ops.push_back(SDValue(Load.getNode(), 1));
490*9880d681SAndroid Build Coastguard Worker   Ops.append(Call->op_begin() + 1, Call->op_end());
491*9880d681SAndroid Build Coastguard Worker   CurDAG->UpdateNodeOperands(Call.getNode(), Ops);
492*9880d681SAndroid Build Coastguard Worker }
493*9880d681SAndroid Build Coastguard Worker 
494*9880d681SAndroid Build Coastguard Worker /// Return true if call address is a load and it can be
495*9880d681SAndroid Build Coastguard Worker /// moved below CALLSEQ_START and the chains leading up to the call.
496*9880d681SAndroid Build Coastguard Worker /// Return the CALLSEQ_START by reference as a second output.
497*9880d681SAndroid Build Coastguard Worker /// In the case of a tail call, there isn't a callseq node between the call
498*9880d681SAndroid Build Coastguard Worker /// chain and the load.
isCalleeLoad(SDValue Callee,SDValue & Chain,bool HasCallSeq)499*9880d681SAndroid Build Coastguard Worker static bool isCalleeLoad(SDValue Callee, SDValue &Chain, bool HasCallSeq) {
500*9880d681SAndroid Build Coastguard Worker   // The transformation is somewhat dangerous if the call's chain was glued to
501*9880d681SAndroid Build Coastguard Worker   // the call. After MoveBelowOrigChain the load is moved between the call and
502*9880d681SAndroid Build Coastguard Worker   // the chain, this can create a cycle if the load is not folded. So it is
503*9880d681SAndroid Build Coastguard Worker   // *really* important that we are sure the load will be folded.
504*9880d681SAndroid Build Coastguard Worker   if (Callee.getNode() == Chain.getNode() || !Callee.hasOneUse())
505*9880d681SAndroid Build Coastguard Worker     return false;
506*9880d681SAndroid Build Coastguard Worker   LoadSDNode *LD = dyn_cast<LoadSDNode>(Callee.getNode());
507*9880d681SAndroid Build Coastguard Worker   if (!LD ||
508*9880d681SAndroid Build Coastguard Worker       LD->isVolatile() ||
509*9880d681SAndroid Build Coastguard Worker       LD->getAddressingMode() != ISD::UNINDEXED ||
510*9880d681SAndroid Build Coastguard Worker       LD->getExtensionType() != ISD::NON_EXTLOAD)
511*9880d681SAndroid Build Coastguard Worker     return false;
512*9880d681SAndroid Build Coastguard Worker 
513*9880d681SAndroid Build Coastguard Worker   // Now let's find the callseq_start.
514*9880d681SAndroid Build Coastguard Worker   while (HasCallSeq && Chain.getOpcode() != ISD::CALLSEQ_START) {
515*9880d681SAndroid Build Coastguard Worker     if (!Chain.hasOneUse())
516*9880d681SAndroid Build Coastguard Worker       return false;
517*9880d681SAndroid Build Coastguard Worker     Chain = Chain.getOperand(0);
518*9880d681SAndroid Build Coastguard Worker   }
519*9880d681SAndroid Build Coastguard Worker 
520*9880d681SAndroid Build Coastguard Worker   if (!Chain.getNumOperands())
521*9880d681SAndroid Build Coastguard Worker     return false;
522*9880d681SAndroid Build Coastguard Worker   // Since we are not checking for AA here, conservatively abort if the chain
523*9880d681SAndroid Build Coastguard Worker   // writes to memory. It's not safe to move the callee (a load) across a store.
524*9880d681SAndroid Build Coastguard Worker   if (isa<MemSDNode>(Chain.getNode()) &&
525*9880d681SAndroid Build Coastguard Worker       cast<MemSDNode>(Chain.getNode())->writeMem())
526*9880d681SAndroid Build Coastguard Worker     return false;
527*9880d681SAndroid Build Coastguard Worker   if (Chain.getOperand(0).getNode() == Callee.getNode())
528*9880d681SAndroid Build Coastguard Worker     return true;
529*9880d681SAndroid Build Coastguard Worker   if (Chain.getOperand(0).getOpcode() == ISD::TokenFactor &&
530*9880d681SAndroid Build Coastguard Worker       Callee.getValue(1).isOperandOf(Chain.getOperand(0).getNode()) &&
531*9880d681SAndroid Build Coastguard Worker       Callee.getValue(1).hasOneUse())
532*9880d681SAndroid Build Coastguard Worker     return true;
533*9880d681SAndroid Build Coastguard Worker   return false;
534*9880d681SAndroid Build Coastguard Worker }
535*9880d681SAndroid Build Coastguard Worker 
PreprocessISelDAG()536*9880d681SAndroid Build Coastguard Worker void X86DAGToDAGISel::PreprocessISelDAG() {
537*9880d681SAndroid Build Coastguard Worker   // OptFor[Min]Size are used in pattern predicates that isel is matching.
538*9880d681SAndroid Build Coastguard Worker   OptForSize = MF->getFunction()->optForSize();
539*9880d681SAndroid Build Coastguard Worker   OptForMinSize = MF->getFunction()->optForMinSize();
540*9880d681SAndroid Build Coastguard Worker   assert((!OptForMinSize || OptForSize) && "OptForMinSize implies OptForSize");
541*9880d681SAndroid Build Coastguard Worker 
542*9880d681SAndroid Build Coastguard Worker   for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
543*9880d681SAndroid Build Coastguard Worker        E = CurDAG->allnodes_end(); I != E; ) {
544*9880d681SAndroid Build Coastguard Worker     SDNode *N = &*I++; // Preincrement iterator to avoid invalidation issues.
545*9880d681SAndroid Build Coastguard Worker 
546*9880d681SAndroid Build Coastguard Worker     if (OptLevel != CodeGenOpt::None &&
547*9880d681SAndroid Build Coastguard Worker         // Only does this when target favors doesn't favor register indirect
548*9880d681SAndroid Build Coastguard Worker         // call.
549*9880d681SAndroid Build Coastguard Worker         ((N->getOpcode() == X86ISD::CALL && !Subtarget->callRegIndirect()) ||
550*9880d681SAndroid Build Coastguard Worker          (N->getOpcode() == X86ISD::TC_RETURN &&
551*9880d681SAndroid Build Coastguard Worker           // Only does this if load can be folded into TC_RETURN.
552*9880d681SAndroid Build Coastguard Worker           (Subtarget->is64Bit() ||
553*9880d681SAndroid Build Coastguard Worker            !getTargetMachine().isPositionIndependent())))) {
554*9880d681SAndroid Build Coastguard Worker       /// Also try moving call address load from outside callseq_start to just
555*9880d681SAndroid Build Coastguard Worker       /// before the call to allow it to be folded.
556*9880d681SAndroid Build Coastguard Worker       ///
557*9880d681SAndroid Build Coastguard Worker       ///     [Load chain]
558*9880d681SAndroid Build Coastguard Worker       ///         ^
559*9880d681SAndroid Build Coastguard Worker       ///         |
560*9880d681SAndroid Build Coastguard Worker       ///       [Load]
561*9880d681SAndroid Build Coastguard Worker       ///       ^    ^
562*9880d681SAndroid Build Coastguard Worker       ///       |    |
563*9880d681SAndroid Build Coastguard Worker       ///      /      \--
564*9880d681SAndroid Build Coastguard Worker       ///     /          |
565*9880d681SAndroid Build Coastguard Worker       ///[CALLSEQ_START] |
566*9880d681SAndroid Build Coastguard Worker       ///     ^          |
567*9880d681SAndroid Build Coastguard Worker       ///     |          |
568*9880d681SAndroid Build Coastguard Worker       /// [LOAD/C2Reg]   |
569*9880d681SAndroid Build Coastguard Worker       ///     |          |
570*9880d681SAndroid Build Coastguard Worker       ///      \        /
571*9880d681SAndroid Build Coastguard Worker       ///       \      /
572*9880d681SAndroid Build Coastguard Worker       ///       [CALL]
573*9880d681SAndroid Build Coastguard Worker       bool HasCallSeq = N->getOpcode() == X86ISD::CALL;
574*9880d681SAndroid Build Coastguard Worker       SDValue Chain = N->getOperand(0);
575*9880d681SAndroid Build Coastguard Worker       SDValue Load  = N->getOperand(1);
576*9880d681SAndroid Build Coastguard Worker       if (!isCalleeLoad(Load, Chain, HasCallSeq))
577*9880d681SAndroid Build Coastguard Worker         continue;
578*9880d681SAndroid Build Coastguard Worker       moveBelowOrigChain(CurDAG, Load, SDValue(N, 0), Chain);
579*9880d681SAndroid Build Coastguard Worker       ++NumLoadMoved;
580*9880d681SAndroid Build Coastguard Worker       continue;
581*9880d681SAndroid Build Coastguard Worker     }
582*9880d681SAndroid Build Coastguard Worker 
583*9880d681SAndroid Build Coastguard Worker     // Lower fpround and fpextend nodes that target the FP stack to be store and
584*9880d681SAndroid Build Coastguard Worker     // load to the stack.  This is a gross hack.  We would like to simply mark
585*9880d681SAndroid Build Coastguard Worker     // these as being illegal, but when we do that, legalize produces these when
586*9880d681SAndroid Build Coastguard Worker     // it expands calls, then expands these in the same legalize pass.  We would
587*9880d681SAndroid Build Coastguard Worker     // like dag combine to be able to hack on these between the call expansion
588*9880d681SAndroid Build Coastguard Worker     // and the node legalization.  As such this pass basically does "really
589*9880d681SAndroid Build Coastguard Worker     // late" legalization of these inline with the X86 isel pass.
590*9880d681SAndroid Build Coastguard Worker     // FIXME: This should only happen when not compiled with -O0.
591*9880d681SAndroid Build Coastguard Worker     if (N->getOpcode() != ISD::FP_ROUND && N->getOpcode() != ISD::FP_EXTEND)
592*9880d681SAndroid Build Coastguard Worker       continue;
593*9880d681SAndroid Build Coastguard Worker 
594*9880d681SAndroid Build Coastguard Worker     MVT SrcVT = N->getOperand(0).getSimpleValueType();
595*9880d681SAndroid Build Coastguard Worker     MVT DstVT = N->getSimpleValueType(0);
596*9880d681SAndroid Build Coastguard Worker 
597*9880d681SAndroid Build Coastguard Worker     // If any of the sources are vectors, no fp stack involved.
598*9880d681SAndroid Build Coastguard Worker     if (SrcVT.isVector() || DstVT.isVector())
599*9880d681SAndroid Build Coastguard Worker       continue;
600*9880d681SAndroid Build Coastguard Worker 
601*9880d681SAndroid Build Coastguard Worker     // If the source and destination are SSE registers, then this is a legal
602*9880d681SAndroid Build Coastguard Worker     // conversion that should not be lowered.
603*9880d681SAndroid Build Coastguard Worker     const X86TargetLowering *X86Lowering =
604*9880d681SAndroid Build Coastguard Worker         static_cast<const X86TargetLowering *>(TLI);
605*9880d681SAndroid Build Coastguard Worker     bool SrcIsSSE = X86Lowering->isScalarFPTypeInSSEReg(SrcVT);
606*9880d681SAndroid Build Coastguard Worker     bool DstIsSSE = X86Lowering->isScalarFPTypeInSSEReg(DstVT);
607*9880d681SAndroid Build Coastguard Worker     if (SrcIsSSE && DstIsSSE)
608*9880d681SAndroid Build Coastguard Worker       continue;
609*9880d681SAndroid Build Coastguard Worker 
610*9880d681SAndroid Build Coastguard Worker     if (!SrcIsSSE && !DstIsSSE) {
611*9880d681SAndroid Build Coastguard Worker       // If this is an FPStack extension, it is a noop.
612*9880d681SAndroid Build Coastguard Worker       if (N->getOpcode() == ISD::FP_EXTEND)
613*9880d681SAndroid Build Coastguard Worker         continue;
614*9880d681SAndroid Build Coastguard Worker       // If this is a value-preserving FPStack truncation, it is a noop.
615*9880d681SAndroid Build Coastguard Worker       if (N->getConstantOperandVal(1))
616*9880d681SAndroid Build Coastguard Worker         continue;
617*9880d681SAndroid Build Coastguard Worker     }
618*9880d681SAndroid Build Coastguard Worker 
619*9880d681SAndroid Build Coastguard Worker     // Here we could have an FP stack truncation or an FPStack <-> SSE convert.
620*9880d681SAndroid Build Coastguard Worker     // FPStack has extload and truncstore.  SSE can fold direct loads into other
621*9880d681SAndroid Build Coastguard Worker     // operations.  Based on this, decide what we want to do.
622*9880d681SAndroid Build Coastguard Worker     MVT MemVT;
623*9880d681SAndroid Build Coastguard Worker     if (N->getOpcode() == ISD::FP_ROUND)
624*9880d681SAndroid Build Coastguard Worker       MemVT = DstVT;  // FP_ROUND must use DstVT, we can't do a 'trunc load'.
625*9880d681SAndroid Build Coastguard Worker     else
626*9880d681SAndroid Build Coastguard Worker       MemVT = SrcIsSSE ? SrcVT : DstVT;
627*9880d681SAndroid Build Coastguard Worker 
628*9880d681SAndroid Build Coastguard Worker     SDValue MemTmp = CurDAG->CreateStackTemporary(MemVT);
629*9880d681SAndroid Build Coastguard Worker     SDLoc dl(N);
630*9880d681SAndroid Build Coastguard Worker 
631*9880d681SAndroid Build Coastguard Worker     // FIXME: optimize the case where the src/dest is a load or store?
632*9880d681SAndroid Build Coastguard Worker     SDValue Store = CurDAG->getTruncStore(CurDAG->getEntryNode(), dl,
633*9880d681SAndroid Build Coastguard Worker                                           N->getOperand(0),
634*9880d681SAndroid Build Coastguard Worker                                           MemTmp, MachinePointerInfo(), MemVT,
635*9880d681SAndroid Build Coastguard Worker                                           false, false, 0);
636*9880d681SAndroid Build Coastguard Worker     SDValue Result = CurDAG->getExtLoad(ISD::EXTLOAD, dl, DstVT, Store, MemTmp,
637*9880d681SAndroid Build Coastguard Worker                                         MachinePointerInfo(),
638*9880d681SAndroid Build Coastguard Worker                                         MemVT, false, false, false, 0);
639*9880d681SAndroid Build Coastguard Worker 
640*9880d681SAndroid Build Coastguard Worker     // We're about to replace all uses of the FP_ROUND/FP_EXTEND with the
641*9880d681SAndroid Build Coastguard Worker     // extload we created.  This will cause general havok on the dag because
642*9880d681SAndroid Build Coastguard Worker     // anything below the conversion could be folded into other existing nodes.
643*9880d681SAndroid Build Coastguard Worker     // To avoid invalidating 'I', back it up to the convert node.
644*9880d681SAndroid Build Coastguard Worker     --I;
645*9880d681SAndroid Build Coastguard Worker     CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Result);
646*9880d681SAndroid Build Coastguard Worker 
647*9880d681SAndroid Build Coastguard Worker     // Now that we did that, the node is dead.  Increment the iterator to the
648*9880d681SAndroid Build Coastguard Worker     // next node to process, then delete N.
649*9880d681SAndroid Build Coastguard Worker     ++I;
650*9880d681SAndroid Build Coastguard Worker     CurDAG->DeleteNode(N);
651*9880d681SAndroid Build Coastguard Worker   }
652*9880d681SAndroid Build Coastguard Worker }
653*9880d681SAndroid Build Coastguard Worker 
654*9880d681SAndroid Build Coastguard Worker 
655*9880d681SAndroid Build Coastguard Worker /// Emit any code that needs to be executed only in the main function.
emitSpecialCodeForMain()656*9880d681SAndroid Build Coastguard Worker void X86DAGToDAGISel::emitSpecialCodeForMain() {
657*9880d681SAndroid Build Coastguard Worker   if (Subtarget->isTargetCygMing()) {
658*9880d681SAndroid Build Coastguard Worker     TargetLowering::ArgListTy Args;
659*9880d681SAndroid Build Coastguard Worker     auto &DL = CurDAG->getDataLayout();
660*9880d681SAndroid Build Coastguard Worker 
661*9880d681SAndroid Build Coastguard Worker     TargetLowering::CallLoweringInfo CLI(*CurDAG);
662*9880d681SAndroid Build Coastguard Worker     CLI.setChain(CurDAG->getRoot())
663*9880d681SAndroid Build Coastguard Worker         .setCallee(CallingConv::C, Type::getVoidTy(*CurDAG->getContext()),
664*9880d681SAndroid Build Coastguard Worker                    CurDAG->getExternalSymbol("__main", TLI->getPointerTy(DL)),
665*9880d681SAndroid Build Coastguard Worker                    std::move(Args));
666*9880d681SAndroid Build Coastguard Worker     const TargetLowering &TLI = CurDAG->getTargetLoweringInfo();
667*9880d681SAndroid Build Coastguard Worker     std::pair<SDValue, SDValue> Result = TLI.LowerCallTo(CLI);
668*9880d681SAndroid Build Coastguard Worker     CurDAG->setRoot(Result.second);
669*9880d681SAndroid Build Coastguard Worker   }
670*9880d681SAndroid Build Coastguard Worker }
671*9880d681SAndroid Build Coastguard Worker 
EmitFunctionEntryCode()672*9880d681SAndroid Build Coastguard Worker void X86DAGToDAGISel::EmitFunctionEntryCode() {
673*9880d681SAndroid Build Coastguard Worker   // If this is main, emit special code for main.
674*9880d681SAndroid Build Coastguard Worker   if (const Function *Fn = MF->getFunction())
675*9880d681SAndroid Build Coastguard Worker     if (Fn->hasExternalLinkage() && Fn->getName() == "main")
676*9880d681SAndroid Build Coastguard Worker       emitSpecialCodeForMain();
677*9880d681SAndroid Build Coastguard Worker }
678*9880d681SAndroid Build Coastguard Worker 
isDispSafeForFrameIndex(int64_t Val)679*9880d681SAndroid Build Coastguard Worker static bool isDispSafeForFrameIndex(int64_t Val) {
680*9880d681SAndroid Build Coastguard Worker   // On 64-bit platforms, we can run into an issue where a frame index
681*9880d681SAndroid Build Coastguard Worker   // includes a displacement that, when added to the explicit displacement,
682*9880d681SAndroid Build Coastguard Worker   // will overflow the displacement field. Assuming that the frame index
683*9880d681SAndroid Build Coastguard Worker   // displacement fits into a 31-bit integer  (which is only slightly more
684*9880d681SAndroid Build Coastguard Worker   // aggressive than the current fundamental assumption that it fits into
685*9880d681SAndroid Build Coastguard Worker   // a 32-bit integer), a 31-bit disp should always be safe.
686*9880d681SAndroid Build Coastguard Worker   return isInt<31>(Val);
687*9880d681SAndroid Build Coastguard Worker }
688*9880d681SAndroid Build Coastguard Worker 
foldOffsetIntoAddress(uint64_t Offset,X86ISelAddressMode & AM)689*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::foldOffsetIntoAddress(uint64_t Offset,
690*9880d681SAndroid Build Coastguard Worker                                             X86ISelAddressMode &AM) {
691*9880d681SAndroid Build Coastguard Worker   // Cannot combine ExternalSymbol displacements with integer offsets.
692*9880d681SAndroid Build Coastguard Worker   if (Offset != 0 && (AM.ES || AM.MCSym))
693*9880d681SAndroid Build Coastguard Worker     return true;
694*9880d681SAndroid Build Coastguard Worker   int64_t Val = AM.Disp + Offset;
695*9880d681SAndroid Build Coastguard Worker   CodeModel::Model M = TM.getCodeModel();
696*9880d681SAndroid Build Coastguard Worker   if (Subtarget->is64Bit()) {
697*9880d681SAndroid Build Coastguard Worker     if (!X86::isOffsetSuitableForCodeModel(Val, M,
698*9880d681SAndroid Build Coastguard Worker                                            AM.hasSymbolicDisplacement()))
699*9880d681SAndroid Build Coastguard Worker       return true;
700*9880d681SAndroid Build Coastguard Worker     // In addition to the checks required for a register base, check that
701*9880d681SAndroid Build Coastguard Worker     // we do not try to use an unsafe Disp with a frame index.
702*9880d681SAndroid Build Coastguard Worker     if (AM.BaseType == X86ISelAddressMode::FrameIndexBase &&
703*9880d681SAndroid Build Coastguard Worker         !isDispSafeForFrameIndex(Val))
704*9880d681SAndroid Build Coastguard Worker       return true;
705*9880d681SAndroid Build Coastguard Worker   }
706*9880d681SAndroid Build Coastguard Worker   AM.Disp = Val;
707*9880d681SAndroid Build Coastguard Worker   return false;
708*9880d681SAndroid Build Coastguard Worker 
709*9880d681SAndroid Build Coastguard Worker }
710*9880d681SAndroid Build Coastguard Worker 
matchLoadInAddress(LoadSDNode * N,X86ISelAddressMode & AM)711*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::matchLoadInAddress(LoadSDNode *N, X86ISelAddressMode &AM){
712*9880d681SAndroid Build Coastguard Worker   SDValue Address = N->getOperand(1);
713*9880d681SAndroid Build Coastguard Worker 
714*9880d681SAndroid Build Coastguard Worker   // load gs:0 -> GS segment register.
715*9880d681SAndroid Build Coastguard Worker   // load fs:0 -> FS segment register.
716*9880d681SAndroid Build Coastguard Worker   //
717*9880d681SAndroid Build Coastguard Worker   // This optimization is valid because the GNU TLS model defines that
718*9880d681SAndroid Build Coastguard Worker   // gs:0 (or fs:0 on X86-64) contains its own address.
719*9880d681SAndroid Build Coastguard Worker   // For more information see http://people.redhat.com/drepper/tls.pdf
720*9880d681SAndroid Build Coastguard Worker   if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Address))
721*9880d681SAndroid Build Coastguard Worker     if (C->getSExtValue() == 0 && AM.Segment.getNode() == nullptr &&
722*9880d681SAndroid Build Coastguard Worker         Subtarget->isTargetGlibc())
723*9880d681SAndroid Build Coastguard Worker       switch (N->getPointerInfo().getAddrSpace()) {
724*9880d681SAndroid Build Coastguard Worker       case 256:
725*9880d681SAndroid Build Coastguard Worker         AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16);
726*9880d681SAndroid Build Coastguard Worker         return false;
727*9880d681SAndroid Build Coastguard Worker       case 257:
728*9880d681SAndroid Build Coastguard Worker         AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16);
729*9880d681SAndroid Build Coastguard Worker         return false;
730*9880d681SAndroid Build Coastguard Worker       // Address space 258 is not handled here, because it is not used to
731*9880d681SAndroid Build Coastguard Worker       // address TLS areas.
732*9880d681SAndroid Build Coastguard Worker       }
733*9880d681SAndroid Build Coastguard Worker 
734*9880d681SAndroid Build Coastguard Worker   return true;
735*9880d681SAndroid Build Coastguard Worker }
736*9880d681SAndroid Build Coastguard Worker 
737*9880d681SAndroid Build Coastguard Worker /// Try to match X86ISD::Wrapper and X86ISD::WrapperRIP nodes into an addressing
738*9880d681SAndroid Build Coastguard Worker /// mode. These wrap things that will resolve down into a symbol reference.
739*9880d681SAndroid Build Coastguard Worker /// If no match is possible, this returns true, otherwise it returns false.
matchWrapper(SDValue N,X86ISelAddressMode & AM)740*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::matchWrapper(SDValue N, X86ISelAddressMode &AM) {
741*9880d681SAndroid Build Coastguard Worker   // If the addressing mode already has a symbol as the displacement, we can
742*9880d681SAndroid Build Coastguard Worker   // never match another symbol.
743*9880d681SAndroid Build Coastguard Worker   if (AM.hasSymbolicDisplacement())
744*9880d681SAndroid Build Coastguard Worker     return true;
745*9880d681SAndroid Build Coastguard Worker 
746*9880d681SAndroid Build Coastguard Worker   SDValue N0 = N.getOperand(0);
747*9880d681SAndroid Build Coastguard Worker   CodeModel::Model M = TM.getCodeModel();
748*9880d681SAndroid Build Coastguard Worker 
749*9880d681SAndroid Build Coastguard Worker   // Handle X86-64 rip-relative addresses.  We check this before checking direct
750*9880d681SAndroid Build Coastguard Worker   // folding because RIP is preferable to non-RIP accesses.
751*9880d681SAndroid Build Coastguard Worker   if (Subtarget->is64Bit() && N.getOpcode() == X86ISD::WrapperRIP &&
752*9880d681SAndroid Build Coastguard Worker       // Under X86-64 non-small code model, GV (and friends) are 64-bits, so
753*9880d681SAndroid Build Coastguard Worker       // they cannot be folded into immediate fields.
754*9880d681SAndroid Build Coastguard Worker       // FIXME: This can be improved for kernel and other models?
755*9880d681SAndroid Build Coastguard Worker       (M == CodeModel::Small || M == CodeModel::Kernel)) {
756*9880d681SAndroid Build Coastguard Worker     // Base and index reg must be 0 in order to use %rip as base.
757*9880d681SAndroid Build Coastguard Worker     if (AM.hasBaseOrIndexReg())
758*9880d681SAndroid Build Coastguard Worker       return true;
759*9880d681SAndroid Build Coastguard Worker     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
760*9880d681SAndroid Build Coastguard Worker       X86ISelAddressMode Backup = AM;
761*9880d681SAndroid Build Coastguard Worker       AM.GV = G->getGlobal();
762*9880d681SAndroid Build Coastguard Worker       AM.SymbolFlags = G->getTargetFlags();
763*9880d681SAndroid Build Coastguard Worker       if (foldOffsetIntoAddress(G->getOffset(), AM)) {
764*9880d681SAndroid Build Coastguard Worker         AM = Backup;
765*9880d681SAndroid Build Coastguard Worker         return true;
766*9880d681SAndroid Build Coastguard Worker       }
767*9880d681SAndroid Build Coastguard Worker     } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
768*9880d681SAndroid Build Coastguard Worker       X86ISelAddressMode Backup = AM;
769*9880d681SAndroid Build Coastguard Worker       AM.CP = CP->getConstVal();
770*9880d681SAndroid Build Coastguard Worker       AM.Align = CP->getAlignment();
771*9880d681SAndroid Build Coastguard Worker       AM.SymbolFlags = CP->getTargetFlags();
772*9880d681SAndroid Build Coastguard Worker       if (foldOffsetIntoAddress(CP->getOffset(), AM)) {
773*9880d681SAndroid Build Coastguard Worker         AM = Backup;
774*9880d681SAndroid Build Coastguard Worker         return true;
775*9880d681SAndroid Build Coastguard Worker       }
776*9880d681SAndroid Build Coastguard Worker     } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
777*9880d681SAndroid Build Coastguard Worker       AM.ES = S->getSymbol();
778*9880d681SAndroid Build Coastguard Worker       AM.SymbolFlags = S->getTargetFlags();
779*9880d681SAndroid Build Coastguard Worker     } else if (auto *S = dyn_cast<MCSymbolSDNode>(N0)) {
780*9880d681SAndroid Build Coastguard Worker       AM.MCSym = S->getMCSymbol();
781*9880d681SAndroid Build Coastguard Worker     } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
782*9880d681SAndroid Build Coastguard Worker       AM.JT = J->getIndex();
783*9880d681SAndroid Build Coastguard Worker       AM.SymbolFlags = J->getTargetFlags();
784*9880d681SAndroid Build Coastguard Worker     } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(N0)) {
785*9880d681SAndroid Build Coastguard Worker       X86ISelAddressMode Backup = AM;
786*9880d681SAndroid Build Coastguard Worker       AM.BlockAddr = BA->getBlockAddress();
787*9880d681SAndroid Build Coastguard Worker       AM.SymbolFlags = BA->getTargetFlags();
788*9880d681SAndroid Build Coastguard Worker       if (foldOffsetIntoAddress(BA->getOffset(), AM)) {
789*9880d681SAndroid Build Coastguard Worker         AM = Backup;
790*9880d681SAndroid Build Coastguard Worker         return true;
791*9880d681SAndroid Build Coastguard Worker       }
792*9880d681SAndroid Build Coastguard Worker     } else
793*9880d681SAndroid Build Coastguard Worker       llvm_unreachable("Unhandled symbol reference node.");
794*9880d681SAndroid Build Coastguard Worker 
795*9880d681SAndroid Build Coastguard Worker     if (N.getOpcode() == X86ISD::WrapperRIP)
796*9880d681SAndroid Build Coastguard Worker       AM.setBaseReg(CurDAG->getRegister(X86::RIP, MVT::i64));
797*9880d681SAndroid Build Coastguard Worker     return false;
798*9880d681SAndroid Build Coastguard Worker   }
799*9880d681SAndroid Build Coastguard Worker 
800*9880d681SAndroid Build Coastguard Worker   // Handle the case when globals fit in our immediate field: This is true for
801*9880d681SAndroid Build Coastguard Worker   // X86-32 always and X86-64 when in -mcmodel=small mode.  In 64-bit
802*9880d681SAndroid Build Coastguard Worker   // mode, this only applies to a non-RIP-relative computation.
803*9880d681SAndroid Build Coastguard Worker   if (!Subtarget->is64Bit() ||
804*9880d681SAndroid Build Coastguard Worker       M == CodeModel::Small || M == CodeModel::Kernel) {
805*9880d681SAndroid Build Coastguard Worker     assert(N.getOpcode() != X86ISD::WrapperRIP &&
806*9880d681SAndroid Build Coastguard Worker            "RIP-relative addressing already handled");
807*9880d681SAndroid Build Coastguard Worker     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(N0)) {
808*9880d681SAndroid Build Coastguard Worker       AM.GV = G->getGlobal();
809*9880d681SAndroid Build Coastguard Worker       AM.Disp += G->getOffset();
810*9880d681SAndroid Build Coastguard Worker       AM.SymbolFlags = G->getTargetFlags();
811*9880d681SAndroid Build Coastguard Worker     } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N0)) {
812*9880d681SAndroid Build Coastguard Worker       AM.CP = CP->getConstVal();
813*9880d681SAndroid Build Coastguard Worker       AM.Align = CP->getAlignment();
814*9880d681SAndroid Build Coastguard Worker       AM.Disp += CP->getOffset();
815*9880d681SAndroid Build Coastguard Worker       AM.SymbolFlags = CP->getTargetFlags();
816*9880d681SAndroid Build Coastguard Worker     } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(N0)) {
817*9880d681SAndroid Build Coastguard Worker       AM.ES = S->getSymbol();
818*9880d681SAndroid Build Coastguard Worker       AM.SymbolFlags = S->getTargetFlags();
819*9880d681SAndroid Build Coastguard Worker     } else if (auto *S = dyn_cast<MCSymbolSDNode>(N0)) {
820*9880d681SAndroid Build Coastguard Worker       AM.MCSym = S->getMCSymbol();
821*9880d681SAndroid Build Coastguard Worker     } else if (JumpTableSDNode *J = dyn_cast<JumpTableSDNode>(N0)) {
822*9880d681SAndroid Build Coastguard Worker       AM.JT = J->getIndex();
823*9880d681SAndroid Build Coastguard Worker       AM.SymbolFlags = J->getTargetFlags();
824*9880d681SAndroid Build Coastguard Worker     } else if (BlockAddressSDNode *BA = dyn_cast<BlockAddressSDNode>(N0)) {
825*9880d681SAndroid Build Coastguard Worker       AM.BlockAddr = BA->getBlockAddress();
826*9880d681SAndroid Build Coastguard Worker       AM.Disp += BA->getOffset();
827*9880d681SAndroid Build Coastguard Worker       AM.SymbolFlags = BA->getTargetFlags();
828*9880d681SAndroid Build Coastguard Worker     } else
829*9880d681SAndroid Build Coastguard Worker       llvm_unreachable("Unhandled symbol reference node.");
830*9880d681SAndroid Build Coastguard Worker     return false;
831*9880d681SAndroid Build Coastguard Worker   }
832*9880d681SAndroid Build Coastguard Worker 
833*9880d681SAndroid Build Coastguard Worker   return true;
834*9880d681SAndroid Build Coastguard Worker }
835*9880d681SAndroid Build Coastguard Worker 
836*9880d681SAndroid Build Coastguard Worker /// Add the specified node to the specified addressing mode, returning true if
837*9880d681SAndroid Build Coastguard Worker /// it cannot be done. This just pattern matches for the addressing mode.
matchAddress(SDValue N,X86ISelAddressMode & AM)838*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::matchAddress(SDValue N, X86ISelAddressMode &AM) {
839*9880d681SAndroid Build Coastguard Worker   if (matchAddressRecursively(N, AM, 0))
840*9880d681SAndroid Build Coastguard Worker     return true;
841*9880d681SAndroid Build Coastguard Worker 
842*9880d681SAndroid Build Coastguard Worker   // Post-processing: Convert lea(,%reg,2) to lea(%reg,%reg), which has
843*9880d681SAndroid Build Coastguard Worker   // a smaller encoding and avoids a scaled-index.
844*9880d681SAndroid Build Coastguard Worker   if (AM.Scale == 2 &&
845*9880d681SAndroid Build Coastguard Worker       AM.BaseType == X86ISelAddressMode::RegBase &&
846*9880d681SAndroid Build Coastguard Worker       AM.Base_Reg.getNode() == nullptr) {
847*9880d681SAndroid Build Coastguard Worker     AM.Base_Reg = AM.IndexReg;
848*9880d681SAndroid Build Coastguard Worker     AM.Scale = 1;
849*9880d681SAndroid Build Coastguard Worker   }
850*9880d681SAndroid Build Coastguard Worker 
851*9880d681SAndroid Build Coastguard Worker   // Post-processing: Convert foo to foo(%rip), even in non-PIC mode,
852*9880d681SAndroid Build Coastguard Worker   // because it has a smaller encoding.
853*9880d681SAndroid Build Coastguard Worker   // TODO: Which other code models can use this?
854*9880d681SAndroid Build Coastguard Worker   if (TM.getCodeModel() == CodeModel::Small &&
855*9880d681SAndroid Build Coastguard Worker       Subtarget->is64Bit() &&
856*9880d681SAndroid Build Coastguard Worker       AM.Scale == 1 &&
857*9880d681SAndroid Build Coastguard Worker       AM.BaseType == X86ISelAddressMode::RegBase &&
858*9880d681SAndroid Build Coastguard Worker       AM.Base_Reg.getNode() == nullptr &&
859*9880d681SAndroid Build Coastguard Worker       AM.IndexReg.getNode() == nullptr &&
860*9880d681SAndroid Build Coastguard Worker       AM.SymbolFlags == X86II::MO_NO_FLAG &&
861*9880d681SAndroid Build Coastguard Worker       AM.hasSymbolicDisplacement())
862*9880d681SAndroid Build Coastguard Worker     AM.Base_Reg = CurDAG->getRegister(X86::RIP, MVT::i64);
863*9880d681SAndroid Build Coastguard Worker 
864*9880d681SAndroid Build Coastguard Worker   return false;
865*9880d681SAndroid Build Coastguard Worker }
866*9880d681SAndroid Build Coastguard Worker 
matchAdd(SDValue N,X86ISelAddressMode & AM,unsigned Depth)867*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::matchAdd(SDValue N, X86ISelAddressMode &AM,
868*9880d681SAndroid Build Coastguard Worker                                unsigned Depth) {
869*9880d681SAndroid Build Coastguard Worker   // Add an artificial use to this node so that we can keep track of
870*9880d681SAndroid Build Coastguard Worker   // it if it gets CSE'd with a different node.
871*9880d681SAndroid Build Coastguard Worker   HandleSDNode Handle(N);
872*9880d681SAndroid Build Coastguard Worker 
873*9880d681SAndroid Build Coastguard Worker   X86ISelAddressMode Backup = AM;
874*9880d681SAndroid Build Coastguard Worker   if (!matchAddressRecursively(N.getOperand(0), AM, Depth+1) &&
875*9880d681SAndroid Build Coastguard Worker       !matchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1))
876*9880d681SAndroid Build Coastguard Worker     return false;
877*9880d681SAndroid Build Coastguard Worker   AM = Backup;
878*9880d681SAndroid Build Coastguard Worker 
879*9880d681SAndroid Build Coastguard Worker   // Try again after commuting the operands.
880*9880d681SAndroid Build Coastguard Worker   if (!matchAddressRecursively(Handle.getValue().getOperand(1), AM, Depth+1) &&
881*9880d681SAndroid Build Coastguard Worker       !matchAddressRecursively(Handle.getValue().getOperand(0), AM, Depth+1))
882*9880d681SAndroid Build Coastguard Worker     return false;
883*9880d681SAndroid Build Coastguard Worker   AM = Backup;
884*9880d681SAndroid Build Coastguard Worker 
885*9880d681SAndroid Build Coastguard Worker   // If we couldn't fold both operands into the address at the same time,
886*9880d681SAndroid Build Coastguard Worker   // see if we can just put each operand into a register and fold at least
887*9880d681SAndroid Build Coastguard Worker   // the add.
888*9880d681SAndroid Build Coastguard Worker   if (AM.BaseType == X86ISelAddressMode::RegBase &&
889*9880d681SAndroid Build Coastguard Worker       !AM.Base_Reg.getNode() &&
890*9880d681SAndroid Build Coastguard Worker       !AM.IndexReg.getNode()) {
891*9880d681SAndroid Build Coastguard Worker     N = Handle.getValue();
892*9880d681SAndroid Build Coastguard Worker     AM.Base_Reg = N.getOperand(0);
893*9880d681SAndroid Build Coastguard Worker     AM.IndexReg = N.getOperand(1);
894*9880d681SAndroid Build Coastguard Worker     AM.Scale = 1;
895*9880d681SAndroid Build Coastguard Worker     return false;
896*9880d681SAndroid Build Coastguard Worker   }
897*9880d681SAndroid Build Coastguard Worker   N = Handle.getValue();
898*9880d681SAndroid Build Coastguard Worker   return true;
899*9880d681SAndroid Build Coastguard Worker }
900*9880d681SAndroid Build Coastguard Worker 
901*9880d681SAndroid Build Coastguard Worker // Insert a node into the DAG at least before the Pos node's position. This
902*9880d681SAndroid Build Coastguard Worker // will reposition the node as needed, and will assign it a node ID that is <=
903*9880d681SAndroid Build Coastguard Worker // the Pos node's ID. Note that this does *not* preserve the uniqueness of node
904*9880d681SAndroid Build Coastguard Worker // IDs! The selection DAG must no longer depend on their uniqueness when this
905*9880d681SAndroid Build Coastguard Worker // is used.
insertDAGNode(SelectionDAG & DAG,SDValue Pos,SDValue N)906*9880d681SAndroid Build Coastguard Worker static void insertDAGNode(SelectionDAG &DAG, SDValue Pos, SDValue N) {
907*9880d681SAndroid Build Coastguard Worker   if (N.getNode()->getNodeId() == -1 ||
908*9880d681SAndroid Build Coastguard Worker       N.getNode()->getNodeId() > Pos.getNode()->getNodeId()) {
909*9880d681SAndroid Build Coastguard Worker     DAG.RepositionNode(Pos.getNode()->getIterator(), N.getNode());
910*9880d681SAndroid Build Coastguard Worker     N.getNode()->setNodeId(Pos.getNode()->getNodeId());
911*9880d681SAndroid Build Coastguard Worker   }
912*9880d681SAndroid Build Coastguard Worker }
913*9880d681SAndroid Build Coastguard Worker 
914*9880d681SAndroid Build Coastguard Worker // Transform "(X >> (8-C1)) & (0xff << C1)" to "((X >> 8) & 0xff) << C1" if
915*9880d681SAndroid Build Coastguard Worker // safe. This allows us to convert the shift and and into an h-register
916*9880d681SAndroid Build Coastguard Worker // extract and a scaled index. Returns false if the simplification is
917*9880d681SAndroid Build Coastguard Worker // performed.
foldMaskAndShiftToExtract(SelectionDAG & DAG,SDValue N,uint64_t Mask,SDValue Shift,SDValue X,X86ISelAddressMode & AM)918*9880d681SAndroid Build Coastguard Worker static bool foldMaskAndShiftToExtract(SelectionDAG &DAG, SDValue N,
919*9880d681SAndroid Build Coastguard Worker                                       uint64_t Mask,
920*9880d681SAndroid Build Coastguard Worker                                       SDValue Shift, SDValue X,
921*9880d681SAndroid Build Coastguard Worker                                       X86ISelAddressMode &AM) {
922*9880d681SAndroid Build Coastguard Worker   if (Shift.getOpcode() != ISD::SRL ||
923*9880d681SAndroid Build Coastguard Worker       !isa<ConstantSDNode>(Shift.getOperand(1)) ||
924*9880d681SAndroid Build Coastguard Worker       !Shift.hasOneUse())
925*9880d681SAndroid Build Coastguard Worker     return true;
926*9880d681SAndroid Build Coastguard Worker 
927*9880d681SAndroid Build Coastguard Worker   int ScaleLog = 8 - Shift.getConstantOperandVal(1);
928*9880d681SAndroid Build Coastguard Worker   if (ScaleLog <= 0 || ScaleLog >= 4 ||
929*9880d681SAndroid Build Coastguard Worker       Mask != (0xffu << ScaleLog))
930*9880d681SAndroid Build Coastguard Worker     return true;
931*9880d681SAndroid Build Coastguard Worker 
932*9880d681SAndroid Build Coastguard Worker   MVT VT = N.getSimpleValueType();
933*9880d681SAndroid Build Coastguard Worker   SDLoc DL(N);
934*9880d681SAndroid Build Coastguard Worker   SDValue Eight = DAG.getConstant(8, DL, MVT::i8);
935*9880d681SAndroid Build Coastguard Worker   SDValue NewMask = DAG.getConstant(0xff, DL, VT);
936*9880d681SAndroid Build Coastguard Worker   SDValue Srl = DAG.getNode(ISD::SRL, DL, VT, X, Eight);
937*9880d681SAndroid Build Coastguard Worker   SDValue And = DAG.getNode(ISD::AND, DL, VT, Srl, NewMask);
938*9880d681SAndroid Build Coastguard Worker   SDValue ShlCount = DAG.getConstant(ScaleLog, DL, MVT::i8);
939*9880d681SAndroid Build Coastguard Worker   SDValue Shl = DAG.getNode(ISD::SHL, DL, VT, And, ShlCount);
940*9880d681SAndroid Build Coastguard Worker 
941*9880d681SAndroid Build Coastguard Worker   // Insert the new nodes into the topological ordering. We must do this in
942*9880d681SAndroid Build Coastguard Worker   // a valid topological ordering as nothing is going to go back and re-sort
943*9880d681SAndroid Build Coastguard Worker   // these nodes. We continually insert before 'N' in sequence as this is
944*9880d681SAndroid Build Coastguard Worker   // essentially a pre-flattened and pre-sorted sequence of nodes. There is no
945*9880d681SAndroid Build Coastguard Worker   // hierarchy left to express.
946*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, Eight);
947*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, Srl);
948*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, NewMask);
949*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, And);
950*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, ShlCount);
951*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, Shl);
952*9880d681SAndroid Build Coastguard Worker   DAG.ReplaceAllUsesWith(N, Shl);
953*9880d681SAndroid Build Coastguard Worker   AM.IndexReg = And;
954*9880d681SAndroid Build Coastguard Worker   AM.Scale = (1 << ScaleLog);
955*9880d681SAndroid Build Coastguard Worker   return false;
956*9880d681SAndroid Build Coastguard Worker }
957*9880d681SAndroid Build Coastguard Worker 
958*9880d681SAndroid Build Coastguard Worker // Transforms "(X << C1) & C2" to "(X & (C2>>C1)) << C1" if safe and if this
959*9880d681SAndroid Build Coastguard Worker // allows us to fold the shift into this addressing mode. Returns false if the
960*9880d681SAndroid Build Coastguard Worker // transform succeeded.
foldMaskedShiftToScaledMask(SelectionDAG & DAG,SDValue N,uint64_t Mask,SDValue Shift,SDValue X,X86ISelAddressMode & AM)961*9880d681SAndroid Build Coastguard Worker static bool foldMaskedShiftToScaledMask(SelectionDAG &DAG, SDValue N,
962*9880d681SAndroid Build Coastguard Worker                                         uint64_t Mask,
963*9880d681SAndroid Build Coastguard Worker                                         SDValue Shift, SDValue X,
964*9880d681SAndroid Build Coastguard Worker                                         X86ISelAddressMode &AM) {
965*9880d681SAndroid Build Coastguard Worker   if (Shift.getOpcode() != ISD::SHL ||
966*9880d681SAndroid Build Coastguard Worker       !isa<ConstantSDNode>(Shift.getOperand(1)))
967*9880d681SAndroid Build Coastguard Worker     return true;
968*9880d681SAndroid Build Coastguard Worker 
969*9880d681SAndroid Build Coastguard Worker   // Not likely to be profitable if either the AND or SHIFT node has more
970*9880d681SAndroid Build Coastguard Worker   // than one use (unless all uses are for address computation). Besides,
971*9880d681SAndroid Build Coastguard Worker   // isel mechanism requires their node ids to be reused.
972*9880d681SAndroid Build Coastguard Worker   if (!N.hasOneUse() || !Shift.hasOneUse())
973*9880d681SAndroid Build Coastguard Worker     return true;
974*9880d681SAndroid Build Coastguard Worker 
975*9880d681SAndroid Build Coastguard Worker   // Verify that the shift amount is something we can fold.
976*9880d681SAndroid Build Coastguard Worker   unsigned ShiftAmt = Shift.getConstantOperandVal(1);
977*9880d681SAndroid Build Coastguard Worker   if (ShiftAmt != 1 && ShiftAmt != 2 && ShiftAmt != 3)
978*9880d681SAndroid Build Coastguard Worker     return true;
979*9880d681SAndroid Build Coastguard Worker 
980*9880d681SAndroid Build Coastguard Worker   MVT VT = N.getSimpleValueType();
981*9880d681SAndroid Build Coastguard Worker   SDLoc DL(N);
982*9880d681SAndroid Build Coastguard Worker   SDValue NewMask = DAG.getConstant(Mask >> ShiftAmt, DL, VT);
983*9880d681SAndroid Build Coastguard Worker   SDValue NewAnd = DAG.getNode(ISD::AND, DL, VT, X, NewMask);
984*9880d681SAndroid Build Coastguard Worker   SDValue NewShift = DAG.getNode(ISD::SHL, DL, VT, NewAnd, Shift.getOperand(1));
985*9880d681SAndroid Build Coastguard Worker 
986*9880d681SAndroid Build Coastguard Worker   // Insert the new nodes into the topological ordering. We must do this in
987*9880d681SAndroid Build Coastguard Worker   // a valid topological ordering as nothing is going to go back and re-sort
988*9880d681SAndroid Build Coastguard Worker   // these nodes. We continually insert before 'N' in sequence as this is
989*9880d681SAndroid Build Coastguard Worker   // essentially a pre-flattened and pre-sorted sequence of nodes. There is no
990*9880d681SAndroid Build Coastguard Worker   // hierarchy left to express.
991*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, NewMask);
992*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, NewAnd);
993*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, NewShift);
994*9880d681SAndroid Build Coastguard Worker   DAG.ReplaceAllUsesWith(N, NewShift);
995*9880d681SAndroid Build Coastguard Worker 
996*9880d681SAndroid Build Coastguard Worker   AM.Scale = 1 << ShiftAmt;
997*9880d681SAndroid Build Coastguard Worker   AM.IndexReg = NewAnd;
998*9880d681SAndroid Build Coastguard Worker   return false;
999*9880d681SAndroid Build Coastguard Worker }
1000*9880d681SAndroid Build Coastguard Worker 
1001*9880d681SAndroid Build Coastguard Worker // Implement some heroics to detect shifts of masked values where the mask can
1002*9880d681SAndroid Build Coastguard Worker // be replaced by extending the shift and undoing that in the addressing mode
1003*9880d681SAndroid Build Coastguard Worker // scale. Patterns such as (shl (srl x, c1), c2) are canonicalized into (and
1004*9880d681SAndroid Build Coastguard Worker // (srl x, SHIFT), MASK) by DAGCombines that don't know the shl can be done in
1005*9880d681SAndroid Build Coastguard Worker // the addressing mode. This results in code such as:
1006*9880d681SAndroid Build Coastguard Worker //
1007*9880d681SAndroid Build Coastguard Worker //   int f(short *y, int *lookup_table) {
1008*9880d681SAndroid Build Coastguard Worker //     ...
1009*9880d681SAndroid Build Coastguard Worker //     return *y + lookup_table[*y >> 11];
1010*9880d681SAndroid Build Coastguard Worker //   }
1011*9880d681SAndroid Build Coastguard Worker //
1012*9880d681SAndroid Build Coastguard Worker // Turning into:
1013*9880d681SAndroid Build Coastguard Worker //   movzwl (%rdi), %eax
1014*9880d681SAndroid Build Coastguard Worker //   movl %eax, %ecx
1015*9880d681SAndroid Build Coastguard Worker //   shrl $11, %ecx
1016*9880d681SAndroid Build Coastguard Worker //   addl (%rsi,%rcx,4), %eax
1017*9880d681SAndroid Build Coastguard Worker //
1018*9880d681SAndroid Build Coastguard Worker // Instead of:
1019*9880d681SAndroid Build Coastguard Worker //   movzwl (%rdi), %eax
1020*9880d681SAndroid Build Coastguard Worker //   movl %eax, %ecx
1021*9880d681SAndroid Build Coastguard Worker //   shrl $9, %ecx
1022*9880d681SAndroid Build Coastguard Worker //   andl $124, %rcx
1023*9880d681SAndroid Build Coastguard Worker //   addl (%rsi,%rcx), %eax
1024*9880d681SAndroid Build Coastguard Worker //
1025*9880d681SAndroid Build Coastguard Worker // Note that this function assumes the mask is provided as a mask *after* the
1026*9880d681SAndroid Build Coastguard Worker // value is shifted. The input chain may or may not match that, but computing
1027*9880d681SAndroid Build Coastguard Worker // such a mask is trivial.
foldMaskAndShiftToScale(SelectionDAG & DAG,SDValue N,uint64_t Mask,SDValue Shift,SDValue X,X86ISelAddressMode & AM)1028*9880d681SAndroid Build Coastguard Worker static bool foldMaskAndShiftToScale(SelectionDAG &DAG, SDValue N,
1029*9880d681SAndroid Build Coastguard Worker                                     uint64_t Mask,
1030*9880d681SAndroid Build Coastguard Worker                                     SDValue Shift, SDValue X,
1031*9880d681SAndroid Build Coastguard Worker                                     X86ISelAddressMode &AM) {
1032*9880d681SAndroid Build Coastguard Worker   if (Shift.getOpcode() != ISD::SRL || !Shift.hasOneUse() ||
1033*9880d681SAndroid Build Coastguard Worker       !isa<ConstantSDNode>(Shift.getOperand(1)))
1034*9880d681SAndroid Build Coastguard Worker     return true;
1035*9880d681SAndroid Build Coastguard Worker 
1036*9880d681SAndroid Build Coastguard Worker   unsigned ShiftAmt = Shift.getConstantOperandVal(1);
1037*9880d681SAndroid Build Coastguard Worker   unsigned MaskLZ = countLeadingZeros(Mask);
1038*9880d681SAndroid Build Coastguard Worker   unsigned MaskTZ = countTrailingZeros(Mask);
1039*9880d681SAndroid Build Coastguard Worker 
1040*9880d681SAndroid Build Coastguard Worker   // The amount of shift we're trying to fit into the addressing mode is taken
1041*9880d681SAndroid Build Coastguard Worker   // from the trailing zeros of the mask.
1042*9880d681SAndroid Build Coastguard Worker   unsigned AMShiftAmt = MaskTZ;
1043*9880d681SAndroid Build Coastguard Worker 
1044*9880d681SAndroid Build Coastguard Worker   // There is nothing we can do here unless the mask is removing some bits.
1045*9880d681SAndroid Build Coastguard Worker   // Also, the addressing mode can only represent shifts of 1, 2, or 3 bits.
1046*9880d681SAndroid Build Coastguard Worker   if (AMShiftAmt <= 0 || AMShiftAmt > 3) return true;
1047*9880d681SAndroid Build Coastguard Worker 
1048*9880d681SAndroid Build Coastguard Worker   // We also need to ensure that mask is a continuous run of bits.
1049*9880d681SAndroid Build Coastguard Worker   if (countTrailingOnes(Mask >> MaskTZ) + MaskTZ + MaskLZ != 64) return true;
1050*9880d681SAndroid Build Coastguard Worker 
1051*9880d681SAndroid Build Coastguard Worker   // Scale the leading zero count down based on the actual size of the value.
1052*9880d681SAndroid Build Coastguard Worker   // Also scale it down based on the size of the shift.
1053*9880d681SAndroid Build Coastguard Worker   MaskLZ -= (64 - X.getSimpleValueType().getSizeInBits()) + ShiftAmt;
1054*9880d681SAndroid Build Coastguard Worker 
1055*9880d681SAndroid Build Coastguard Worker   // The final check is to ensure that any masked out high bits of X are
1056*9880d681SAndroid Build Coastguard Worker   // already known to be zero. Otherwise, the mask has a semantic impact
1057*9880d681SAndroid Build Coastguard Worker   // other than masking out a couple of low bits. Unfortunately, because of
1058*9880d681SAndroid Build Coastguard Worker   // the mask, zero extensions will be removed from operands in some cases.
1059*9880d681SAndroid Build Coastguard Worker   // This code works extra hard to look through extensions because we can
1060*9880d681SAndroid Build Coastguard Worker   // replace them with zero extensions cheaply if necessary.
1061*9880d681SAndroid Build Coastguard Worker   bool ReplacingAnyExtend = false;
1062*9880d681SAndroid Build Coastguard Worker   if (X.getOpcode() == ISD::ANY_EXTEND) {
1063*9880d681SAndroid Build Coastguard Worker     unsigned ExtendBits = X.getSimpleValueType().getSizeInBits() -
1064*9880d681SAndroid Build Coastguard Worker                           X.getOperand(0).getSimpleValueType().getSizeInBits();
1065*9880d681SAndroid Build Coastguard Worker     // Assume that we'll replace the any-extend with a zero-extend, and
1066*9880d681SAndroid Build Coastguard Worker     // narrow the search to the extended value.
1067*9880d681SAndroid Build Coastguard Worker     X = X.getOperand(0);
1068*9880d681SAndroid Build Coastguard Worker     MaskLZ = ExtendBits > MaskLZ ? 0 : MaskLZ - ExtendBits;
1069*9880d681SAndroid Build Coastguard Worker     ReplacingAnyExtend = true;
1070*9880d681SAndroid Build Coastguard Worker   }
1071*9880d681SAndroid Build Coastguard Worker   APInt MaskedHighBits =
1072*9880d681SAndroid Build Coastguard Worker     APInt::getHighBitsSet(X.getSimpleValueType().getSizeInBits(), MaskLZ);
1073*9880d681SAndroid Build Coastguard Worker   APInt KnownZero, KnownOne;
1074*9880d681SAndroid Build Coastguard Worker   DAG.computeKnownBits(X, KnownZero, KnownOne);
1075*9880d681SAndroid Build Coastguard Worker   if (MaskedHighBits != KnownZero) return true;
1076*9880d681SAndroid Build Coastguard Worker 
1077*9880d681SAndroid Build Coastguard Worker   // We've identified a pattern that can be transformed into a single shift
1078*9880d681SAndroid Build Coastguard Worker   // and an addressing mode. Make it so.
1079*9880d681SAndroid Build Coastguard Worker   MVT VT = N.getSimpleValueType();
1080*9880d681SAndroid Build Coastguard Worker   if (ReplacingAnyExtend) {
1081*9880d681SAndroid Build Coastguard Worker     assert(X.getValueType() != VT);
1082*9880d681SAndroid Build Coastguard Worker     // We looked through an ANY_EXTEND node, insert a ZERO_EXTEND.
1083*9880d681SAndroid Build Coastguard Worker     SDValue NewX = DAG.getNode(ISD::ZERO_EXTEND, SDLoc(X), VT, X);
1084*9880d681SAndroid Build Coastguard Worker     insertDAGNode(DAG, N, NewX);
1085*9880d681SAndroid Build Coastguard Worker     X = NewX;
1086*9880d681SAndroid Build Coastguard Worker   }
1087*9880d681SAndroid Build Coastguard Worker   SDLoc DL(N);
1088*9880d681SAndroid Build Coastguard Worker   SDValue NewSRLAmt = DAG.getConstant(ShiftAmt + AMShiftAmt, DL, MVT::i8);
1089*9880d681SAndroid Build Coastguard Worker   SDValue NewSRL = DAG.getNode(ISD::SRL, DL, VT, X, NewSRLAmt);
1090*9880d681SAndroid Build Coastguard Worker   SDValue NewSHLAmt = DAG.getConstant(AMShiftAmt, DL, MVT::i8);
1091*9880d681SAndroid Build Coastguard Worker   SDValue NewSHL = DAG.getNode(ISD::SHL, DL, VT, NewSRL, NewSHLAmt);
1092*9880d681SAndroid Build Coastguard Worker 
1093*9880d681SAndroid Build Coastguard Worker   // Insert the new nodes into the topological ordering. We must do this in
1094*9880d681SAndroid Build Coastguard Worker   // a valid topological ordering as nothing is going to go back and re-sort
1095*9880d681SAndroid Build Coastguard Worker   // these nodes. We continually insert before 'N' in sequence as this is
1096*9880d681SAndroid Build Coastguard Worker   // essentially a pre-flattened and pre-sorted sequence of nodes. There is no
1097*9880d681SAndroid Build Coastguard Worker   // hierarchy left to express.
1098*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, NewSRLAmt);
1099*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, NewSRL);
1100*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, NewSHLAmt);
1101*9880d681SAndroid Build Coastguard Worker   insertDAGNode(DAG, N, NewSHL);
1102*9880d681SAndroid Build Coastguard Worker   DAG.ReplaceAllUsesWith(N, NewSHL);
1103*9880d681SAndroid Build Coastguard Worker 
1104*9880d681SAndroid Build Coastguard Worker   AM.Scale = 1 << AMShiftAmt;
1105*9880d681SAndroid Build Coastguard Worker   AM.IndexReg = NewSRL;
1106*9880d681SAndroid Build Coastguard Worker   return false;
1107*9880d681SAndroid Build Coastguard Worker }
1108*9880d681SAndroid Build Coastguard Worker 
matchAddressRecursively(SDValue N,X86ISelAddressMode & AM,unsigned Depth)1109*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::matchAddressRecursively(SDValue N, X86ISelAddressMode &AM,
1110*9880d681SAndroid Build Coastguard Worker                                               unsigned Depth) {
1111*9880d681SAndroid Build Coastguard Worker   SDLoc dl(N);
1112*9880d681SAndroid Build Coastguard Worker   DEBUG({
1113*9880d681SAndroid Build Coastguard Worker       dbgs() << "MatchAddress: ";
1114*9880d681SAndroid Build Coastguard Worker       AM.dump();
1115*9880d681SAndroid Build Coastguard Worker     });
1116*9880d681SAndroid Build Coastguard Worker   // Limit recursion.
1117*9880d681SAndroid Build Coastguard Worker   if (Depth > 5)
1118*9880d681SAndroid Build Coastguard Worker     return matchAddressBase(N, AM);
1119*9880d681SAndroid Build Coastguard Worker 
1120*9880d681SAndroid Build Coastguard Worker   // If this is already a %rip relative address, we can only merge immediates
1121*9880d681SAndroid Build Coastguard Worker   // into it.  Instead of handling this in every case, we handle it here.
1122*9880d681SAndroid Build Coastguard Worker   // RIP relative addressing: %rip + 32-bit displacement!
1123*9880d681SAndroid Build Coastguard Worker   if (AM.isRIPRelative()) {
1124*9880d681SAndroid Build Coastguard Worker     // FIXME: JumpTable and ExternalSymbol address currently don't like
1125*9880d681SAndroid Build Coastguard Worker     // displacements.  It isn't very important, but this should be fixed for
1126*9880d681SAndroid Build Coastguard Worker     // consistency.
1127*9880d681SAndroid Build Coastguard Worker     if (!(AM.ES || AM.MCSym) && AM.JT != -1)
1128*9880d681SAndroid Build Coastguard Worker       return true;
1129*9880d681SAndroid Build Coastguard Worker 
1130*9880d681SAndroid Build Coastguard Worker     if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N))
1131*9880d681SAndroid Build Coastguard Worker       if (!foldOffsetIntoAddress(Cst->getSExtValue(), AM))
1132*9880d681SAndroid Build Coastguard Worker         return false;
1133*9880d681SAndroid Build Coastguard Worker     return true;
1134*9880d681SAndroid Build Coastguard Worker   }
1135*9880d681SAndroid Build Coastguard Worker 
1136*9880d681SAndroid Build Coastguard Worker   switch (N.getOpcode()) {
1137*9880d681SAndroid Build Coastguard Worker   default: break;
1138*9880d681SAndroid Build Coastguard Worker   case ISD::LOCAL_RECOVER: {
1139*9880d681SAndroid Build Coastguard Worker     if (!AM.hasSymbolicDisplacement() && AM.Disp == 0)
1140*9880d681SAndroid Build Coastguard Worker       if (const auto *ESNode = dyn_cast<MCSymbolSDNode>(N.getOperand(0))) {
1141*9880d681SAndroid Build Coastguard Worker         // Use the symbol and don't prefix it.
1142*9880d681SAndroid Build Coastguard Worker         AM.MCSym = ESNode->getMCSymbol();
1143*9880d681SAndroid Build Coastguard Worker         return false;
1144*9880d681SAndroid Build Coastguard Worker       }
1145*9880d681SAndroid Build Coastguard Worker     break;
1146*9880d681SAndroid Build Coastguard Worker   }
1147*9880d681SAndroid Build Coastguard Worker   case ISD::Constant: {
1148*9880d681SAndroid Build Coastguard Worker     uint64_t Val = cast<ConstantSDNode>(N)->getSExtValue();
1149*9880d681SAndroid Build Coastguard Worker     if (!foldOffsetIntoAddress(Val, AM))
1150*9880d681SAndroid Build Coastguard Worker       return false;
1151*9880d681SAndroid Build Coastguard Worker     break;
1152*9880d681SAndroid Build Coastguard Worker   }
1153*9880d681SAndroid Build Coastguard Worker 
1154*9880d681SAndroid Build Coastguard Worker   case X86ISD::Wrapper:
1155*9880d681SAndroid Build Coastguard Worker   case X86ISD::WrapperRIP:
1156*9880d681SAndroid Build Coastguard Worker     if (!matchWrapper(N, AM))
1157*9880d681SAndroid Build Coastguard Worker       return false;
1158*9880d681SAndroid Build Coastguard Worker     break;
1159*9880d681SAndroid Build Coastguard Worker 
1160*9880d681SAndroid Build Coastguard Worker   case ISD::LOAD:
1161*9880d681SAndroid Build Coastguard Worker     if (!matchLoadInAddress(cast<LoadSDNode>(N), AM))
1162*9880d681SAndroid Build Coastguard Worker       return false;
1163*9880d681SAndroid Build Coastguard Worker     break;
1164*9880d681SAndroid Build Coastguard Worker 
1165*9880d681SAndroid Build Coastguard Worker   case ISD::FrameIndex:
1166*9880d681SAndroid Build Coastguard Worker     if (AM.BaseType == X86ISelAddressMode::RegBase &&
1167*9880d681SAndroid Build Coastguard Worker         AM.Base_Reg.getNode() == nullptr &&
1168*9880d681SAndroid Build Coastguard Worker         (!Subtarget->is64Bit() || isDispSafeForFrameIndex(AM.Disp))) {
1169*9880d681SAndroid Build Coastguard Worker       AM.BaseType = X86ISelAddressMode::FrameIndexBase;
1170*9880d681SAndroid Build Coastguard Worker       AM.Base_FrameIndex = cast<FrameIndexSDNode>(N)->getIndex();
1171*9880d681SAndroid Build Coastguard Worker       return false;
1172*9880d681SAndroid Build Coastguard Worker     }
1173*9880d681SAndroid Build Coastguard Worker     break;
1174*9880d681SAndroid Build Coastguard Worker 
1175*9880d681SAndroid Build Coastguard Worker   case ISD::SHL:
1176*9880d681SAndroid Build Coastguard Worker     if (AM.IndexReg.getNode() != nullptr || AM.Scale != 1)
1177*9880d681SAndroid Build Coastguard Worker       break;
1178*9880d681SAndroid Build Coastguard Worker 
1179*9880d681SAndroid Build Coastguard Worker     if (ConstantSDNode
1180*9880d681SAndroid Build Coastguard Worker           *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1))) {
1181*9880d681SAndroid Build Coastguard Worker       unsigned Val = CN->getZExtValue();
1182*9880d681SAndroid Build Coastguard Worker       // Note that we handle x<<1 as (,x,2) rather than (x,x) here so
1183*9880d681SAndroid Build Coastguard Worker       // that the base operand remains free for further matching. If
1184*9880d681SAndroid Build Coastguard Worker       // the base doesn't end up getting used, a post-processing step
1185*9880d681SAndroid Build Coastguard Worker       // in MatchAddress turns (,x,2) into (x,x), which is cheaper.
1186*9880d681SAndroid Build Coastguard Worker       if (Val == 1 || Val == 2 || Val == 3) {
1187*9880d681SAndroid Build Coastguard Worker         AM.Scale = 1 << Val;
1188*9880d681SAndroid Build Coastguard Worker         SDValue ShVal = N.getNode()->getOperand(0);
1189*9880d681SAndroid Build Coastguard Worker 
1190*9880d681SAndroid Build Coastguard Worker         // Okay, we know that we have a scale by now.  However, if the scaled
1191*9880d681SAndroid Build Coastguard Worker         // value is an add of something and a constant, we can fold the
1192*9880d681SAndroid Build Coastguard Worker         // constant into the disp field here.
1193*9880d681SAndroid Build Coastguard Worker         if (CurDAG->isBaseWithConstantOffset(ShVal)) {
1194*9880d681SAndroid Build Coastguard Worker           AM.IndexReg = ShVal.getNode()->getOperand(0);
1195*9880d681SAndroid Build Coastguard Worker           ConstantSDNode *AddVal =
1196*9880d681SAndroid Build Coastguard Worker             cast<ConstantSDNode>(ShVal.getNode()->getOperand(1));
1197*9880d681SAndroid Build Coastguard Worker           uint64_t Disp = (uint64_t)AddVal->getSExtValue() << Val;
1198*9880d681SAndroid Build Coastguard Worker           if (!foldOffsetIntoAddress(Disp, AM))
1199*9880d681SAndroid Build Coastguard Worker             return false;
1200*9880d681SAndroid Build Coastguard Worker         }
1201*9880d681SAndroid Build Coastguard Worker 
1202*9880d681SAndroid Build Coastguard Worker         AM.IndexReg = ShVal;
1203*9880d681SAndroid Build Coastguard Worker         return false;
1204*9880d681SAndroid Build Coastguard Worker       }
1205*9880d681SAndroid Build Coastguard Worker     }
1206*9880d681SAndroid Build Coastguard Worker     break;
1207*9880d681SAndroid Build Coastguard Worker 
1208*9880d681SAndroid Build Coastguard Worker   case ISD::SRL: {
1209*9880d681SAndroid Build Coastguard Worker     // Scale must not be used already.
1210*9880d681SAndroid Build Coastguard Worker     if (AM.IndexReg.getNode() != nullptr || AM.Scale != 1) break;
1211*9880d681SAndroid Build Coastguard Worker 
1212*9880d681SAndroid Build Coastguard Worker     SDValue And = N.getOperand(0);
1213*9880d681SAndroid Build Coastguard Worker     if (And.getOpcode() != ISD::AND) break;
1214*9880d681SAndroid Build Coastguard Worker     SDValue X = And.getOperand(0);
1215*9880d681SAndroid Build Coastguard Worker 
1216*9880d681SAndroid Build Coastguard Worker     // We only handle up to 64-bit values here as those are what matter for
1217*9880d681SAndroid Build Coastguard Worker     // addressing mode optimizations.
1218*9880d681SAndroid Build Coastguard Worker     if (X.getSimpleValueType().getSizeInBits() > 64) break;
1219*9880d681SAndroid Build Coastguard Worker 
1220*9880d681SAndroid Build Coastguard Worker     // The mask used for the transform is expected to be post-shift, but we
1221*9880d681SAndroid Build Coastguard Worker     // found the shift first so just apply the shift to the mask before passing
1222*9880d681SAndroid Build Coastguard Worker     // it down.
1223*9880d681SAndroid Build Coastguard Worker     if (!isa<ConstantSDNode>(N.getOperand(1)) ||
1224*9880d681SAndroid Build Coastguard Worker         !isa<ConstantSDNode>(And.getOperand(1)))
1225*9880d681SAndroid Build Coastguard Worker       break;
1226*9880d681SAndroid Build Coastguard Worker     uint64_t Mask = And.getConstantOperandVal(1) >> N.getConstantOperandVal(1);
1227*9880d681SAndroid Build Coastguard Worker 
1228*9880d681SAndroid Build Coastguard Worker     // Try to fold the mask and shift into the scale, and return false if we
1229*9880d681SAndroid Build Coastguard Worker     // succeed.
1230*9880d681SAndroid Build Coastguard Worker     if (!foldMaskAndShiftToScale(*CurDAG, N, Mask, N, X, AM))
1231*9880d681SAndroid Build Coastguard Worker       return false;
1232*9880d681SAndroid Build Coastguard Worker     break;
1233*9880d681SAndroid Build Coastguard Worker   }
1234*9880d681SAndroid Build Coastguard Worker 
1235*9880d681SAndroid Build Coastguard Worker   case ISD::SMUL_LOHI:
1236*9880d681SAndroid Build Coastguard Worker   case ISD::UMUL_LOHI:
1237*9880d681SAndroid Build Coastguard Worker     // A mul_lohi where we need the low part can be folded as a plain multiply.
1238*9880d681SAndroid Build Coastguard Worker     if (N.getResNo() != 0) break;
1239*9880d681SAndroid Build Coastguard Worker     // FALL THROUGH
1240*9880d681SAndroid Build Coastguard Worker   case ISD::MUL:
1241*9880d681SAndroid Build Coastguard Worker   case X86ISD::MUL_IMM:
1242*9880d681SAndroid Build Coastguard Worker     // X*[3,5,9] -> X+X*[2,4,8]
1243*9880d681SAndroid Build Coastguard Worker     if (AM.BaseType == X86ISelAddressMode::RegBase &&
1244*9880d681SAndroid Build Coastguard Worker         AM.Base_Reg.getNode() == nullptr &&
1245*9880d681SAndroid Build Coastguard Worker         AM.IndexReg.getNode() == nullptr) {
1246*9880d681SAndroid Build Coastguard Worker       if (ConstantSDNode
1247*9880d681SAndroid Build Coastguard Worker             *CN = dyn_cast<ConstantSDNode>(N.getNode()->getOperand(1)))
1248*9880d681SAndroid Build Coastguard Worker         if (CN->getZExtValue() == 3 || CN->getZExtValue() == 5 ||
1249*9880d681SAndroid Build Coastguard Worker             CN->getZExtValue() == 9) {
1250*9880d681SAndroid Build Coastguard Worker           AM.Scale = unsigned(CN->getZExtValue())-1;
1251*9880d681SAndroid Build Coastguard Worker 
1252*9880d681SAndroid Build Coastguard Worker           SDValue MulVal = N.getNode()->getOperand(0);
1253*9880d681SAndroid Build Coastguard Worker           SDValue Reg;
1254*9880d681SAndroid Build Coastguard Worker 
1255*9880d681SAndroid Build Coastguard Worker           // Okay, we know that we have a scale by now.  However, if the scaled
1256*9880d681SAndroid Build Coastguard Worker           // value is an add of something and a constant, we can fold the
1257*9880d681SAndroid Build Coastguard Worker           // constant into the disp field here.
1258*9880d681SAndroid Build Coastguard Worker           if (MulVal.getNode()->getOpcode() == ISD::ADD && MulVal.hasOneUse() &&
1259*9880d681SAndroid Build Coastguard Worker               isa<ConstantSDNode>(MulVal.getNode()->getOperand(1))) {
1260*9880d681SAndroid Build Coastguard Worker             Reg = MulVal.getNode()->getOperand(0);
1261*9880d681SAndroid Build Coastguard Worker             ConstantSDNode *AddVal =
1262*9880d681SAndroid Build Coastguard Worker               cast<ConstantSDNode>(MulVal.getNode()->getOperand(1));
1263*9880d681SAndroid Build Coastguard Worker             uint64_t Disp = AddVal->getSExtValue() * CN->getZExtValue();
1264*9880d681SAndroid Build Coastguard Worker             if (foldOffsetIntoAddress(Disp, AM))
1265*9880d681SAndroid Build Coastguard Worker               Reg = N.getNode()->getOperand(0);
1266*9880d681SAndroid Build Coastguard Worker           } else {
1267*9880d681SAndroid Build Coastguard Worker             Reg = N.getNode()->getOperand(0);
1268*9880d681SAndroid Build Coastguard Worker           }
1269*9880d681SAndroid Build Coastguard Worker 
1270*9880d681SAndroid Build Coastguard Worker           AM.IndexReg = AM.Base_Reg = Reg;
1271*9880d681SAndroid Build Coastguard Worker           return false;
1272*9880d681SAndroid Build Coastguard Worker         }
1273*9880d681SAndroid Build Coastguard Worker     }
1274*9880d681SAndroid Build Coastguard Worker     break;
1275*9880d681SAndroid Build Coastguard Worker 
1276*9880d681SAndroid Build Coastguard Worker   case ISD::SUB: {
1277*9880d681SAndroid Build Coastguard Worker     // Given A-B, if A can be completely folded into the address and
1278*9880d681SAndroid Build Coastguard Worker     // the index field with the index field unused, use -B as the index.
1279*9880d681SAndroid Build Coastguard Worker     // This is a win if a has multiple parts that can be folded into
1280*9880d681SAndroid Build Coastguard Worker     // the address. Also, this saves a mov if the base register has
1281*9880d681SAndroid Build Coastguard Worker     // other uses, since it avoids a two-address sub instruction, however
1282*9880d681SAndroid Build Coastguard Worker     // it costs an additional mov if the index register has other uses.
1283*9880d681SAndroid Build Coastguard Worker 
1284*9880d681SAndroid Build Coastguard Worker     // Add an artificial use to this node so that we can keep track of
1285*9880d681SAndroid Build Coastguard Worker     // it if it gets CSE'd with a different node.
1286*9880d681SAndroid Build Coastguard Worker     HandleSDNode Handle(N);
1287*9880d681SAndroid Build Coastguard Worker 
1288*9880d681SAndroid Build Coastguard Worker     // Test if the LHS of the sub can be folded.
1289*9880d681SAndroid Build Coastguard Worker     X86ISelAddressMode Backup = AM;
1290*9880d681SAndroid Build Coastguard Worker     if (matchAddressRecursively(N.getNode()->getOperand(0), AM, Depth+1)) {
1291*9880d681SAndroid Build Coastguard Worker       AM = Backup;
1292*9880d681SAndroid Build Coastguard Worker       break;
1293*9880d681SAndroid Build Coastguard Worker     }
1294*9880d681SAndroid Build Coastguard Worker     // Test if the index field is free for use.
1295*9880d681SAndroid Build Coastguard Worker     if (AM.IndexReg.getNode() || AM.isRIPRelative()) {
1296*9880d681SAndroid Build Coastguard Worker       AM = Backup;
1297*9880d681SAndroid Build Coastguard Worker       break;
1298*9880d681SAndroid Build Coastguard Worker     }
1299*9880d681SAndroid Build Coastguard Worker 
1300*9880d681SAndroid Build Coastguard Worker     int Cost = 0;
1301*9880d681SAndroid Build Coastguard Worker     SDValue RHS = Handle.getValue().getNode()->getOperand(1);
1302*9880d681SAndroid Build Coastguard Worker     // If the RHS involves a register with multiple uses, this
1303*9880d681SAndroid Build Coastguard Worker     // transformation incurs an extra mov, due to the neg instruction
1304*9880d681SAndroid Build Coastguard Worker     // clobbering its operand.
1305*9880d681SAndroid Build Coastguard Worker     if (!RHS.getNode()->hasOneUse() ||
1306*9880d681SAndroid Build Coastguard Worker         RHS.getNode()->getOpcode() == ISD::CopyFromReg ||
1307*9880d681SAndroid Build Coastguard Worker         RHS.getNode()->getOpcode() == ISD::TRUNCATE ||
1308*9880d681SAndroid Build Coastguard Worker         RHS.getNode()->getOpcode() == ISD::ANY_EXTEND ||
1309*9880d681SAndroid Build Coastguard Worker         (RHS.getNode()->getOpcode() == ISD::ZERO_EXTEND &&
1310*9880d681SAndroid Build Coastguard Worker          RHS.getNode()->getOperand(0).getValueType() == MVT::i32))
1311*9880d681SAndroid Build Coastguard Worker       ++Cost;
1312*9880d681SAndroid Build Coastguard Worker     // If the base is a register with multiple uses, this
1313*9880d681SAndroid Build Coastguard Worker     // transformation may save a mov.
1314*9880d681SAndroid Build Coastguard Worker     if ((AM.BaseType == X86ISelAddressMode::RegBase &&
1315*9880d681SAndroid Build Coastguard Worker          AM.Base_Reg.getNode() &&
1316*9880d681SAndroid Build Coastguard Worker          !AM.Base_Reg.getNode()->hasOneUse()) ||
1317*9880d681SAndroid Build Coastguard Worker         AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1318*9880d681SAndroid Build Coastguard Worker       --Cost;
1319*9880d681SAndroid Build Coastguard Worker     // If the folded LHS was interesting, this transformation saves
1320*9880d681SAndroid Build Coastguard Worker     // address arithmetic.
1321*9880d681SAndroid Build Coastguard Worker     if ((AM.hasSymbolicDisplacement() && !Backup.hasSymbolicDisplacement()) +
1322*9880d681SAndroid Build Coastguard Worker         ((AM.Disp != 0) && (Backup.Disp == 0)) +
1323*9880d681SAndroid Build Coastguard Worker         (AM.Segment.getNode() && !Backup.Segment.getNode()) >= 2)
1324*9880d681SAndroid Build Coastguard Worker       --Cost;
1325*9880d681SAndroid Build Coastguard Worker     // If it doesn't look like it may be an overall win, don't do it.
1326*9880d681SAndroid Build Coastguard Worker     if (Cost >= 0) {
1327*9880d681SAndroid Build Coastguard Worker       AM = Backup;
1328*9880d681SAndroid Build Coastguard Worker       break;
1329*9880d681SAndroid Build Coastguard Worker     }
1330*9880d681SAndroid Build Coastguard Worker 
1331*9880d681SAndroid Build Coastguard Worker     // Ok, the transformation is legal and appears profitable. Go for it.
1332*9880d681SAndroid Build Coastguard Worker     SDValue Zero = CurDAG->getConstant(0, dl, N.getValueType());
1333*9880d681SAndroid Build Coastguard Worker     SDValue Neg = CurDAG->getNode(ISD::SUB, dl, N.getValueType(), Zero, RHS);
1334*9880d681SAndroid Build Coastguard Worker     AM.IndexReg = Neg;
1335*9880d681SAndroid Build Coastguard Worker     AM.Scale = 1;
1336*9880d681SAndroid Build Coastguard Worker 
1337*9880d681SAndroid Build Coastguard Worker     // Insert the new nodes into the topological ordering.
1338*9880d681SAndroid Build Coastguard Worker     insertDAGNode(*CurDAG, N, Zero);
1339*9880d681SAndroid Build Coastguard Worker     insertDAGNode(*CurDAG, N, Neg);
1340*9880d681SAndroid Build Coastguard Worker     return false;
1341*9880d681SAndroid Build Coastguard Worker   }
1342*9880d681SAndroid Build Coastguard Worker 
1343*9880d681SAndroid Build Coastguard Worker   case ISD::ADD:
1344*9880d681SAndroid Build Coastguard Worker     if (!matchAdd(N, AM, Depth))
1345*9880d681SAndroid Build Coastguard Worker       return false;
1346*9880d681SAndroid Build Coastguard Worker     break;
1347*9880d681SAndroid Build Coastguard Worker 
1348*9880d681SAndroid Build Coastguard Worker   case ISD::OR:
1349*9880d681SAndroid Build Coastguard Worker     // We want to look through a transform in InstCombine and DAGCombiner that
1350*9880d681SAndroid Build Coastguard Worker     // turns 'add' into 'or', so we can treat this 'or' exactly like an 'add'.
1351*9880d681SAndroid Build Coastguard Worker     // Example: (or (and x, 1), (shl y, 3)) --> (add (and x, 1), (shl y, 3))
1352*9880d681SAndroid Build Coastguard Worker     // An 'lea' can then be used to match the shift (multiply) and add:
1353*9880d681SAndroid Build Coastguard Worker     // and $1, %esi
1354*9880d681SAndroid Build Coastguard Worker     // lea (%rsi, %rdi, 8), %rax
1355*9880d681SAndroid Build Coastguard Worker     if (CurDAG->haveNoCommonBitsSet(N.getOperand(0), N.getOperand(1)) &&
1356*9880d681SAndroid Build Coastguard Worker         !matchAdd(N, AM, Depth))
1357*9880d681SAndroid Build Coastguard Worker       return false;
1358*9880d681SAndroid Build Coastguard Worker     break;
1359*9880d681SAndroid Build Coastguard Worker 
1360*9880d681SAndroid Build Coastguard Worker   case ISD::AND: {
1361*9880d681SAndroid Build Coastguard Worker     // Perform some heroic transforms on an and of a constant-count shift
1362*9880d681SAndroid Build Coastguard Worker     // with a constant to enable use of the scaled offset field.
1363*9880d681SAndroid Build Coastguard Worker 
1364*9880d681SAndroid Build Coastguard Worker     // Scale must not be used already.
1365*9880d681SAndroid Build Coastguard Worker     if (AM.IndexReg.getNode() != nullptr || AM.Scale != 1) break;
1366*9880d681SAndroid Build Coastguard Worker 
1367*9880d681SAndroid Build Coastguard Worker     SDValue Shift = N.getOperand(0);
1368*9880d681SAndroid Build Coastguard Worker     if (Shift.getOpcode() != ISD::SRL && Shift.getOpcode() != ISD::SHL) break;
1369*9880d681SAndroid Build Coastguard Worker     SDValue X = Shift.getOperand(0);
1370*9880d681SAndroid Build Coastguard Worker 
1371*9880d681SAndroid Build Coastguard Worker     // We only handle up to 64-bit values here as those are what matter for
1372*9880d681SAndroid Build Coastguard Worker     // addressing mode optimizations.
1373*9880d681SAndroid Build Coastguard Worker     if (X.getSimpleValueType().getSizeInBits() > 64) break;
1374*9880d681SAndroid Build Coastguard Worker 
1375*9880d681SAndroid Build Coastguard Worker     if (!isa<ConstantSDNode>(N.getOperand(1)))
1376*9880d681SAndroid Build Coastguard Worker       break;
1377*9880d681SAndroid Build Coastguard Worker     uint64_t Mask = N.getConstantOperandVal(1);
1378*9880d681SAndroid Build Coastguard Worker 
1379*9880d681SAndroid Build Coastguard Worker     // Try to fold the mask and shift into an extract and scale.
1380*9880d681SAndroid Build Coastguard Worker     if (!foldMaskAndShiftToExtract(*CurDAG, N, Mask, Shift, X, AM))
1381*9880d681SAndroid Build Coastguard Worker       return false;
1382*9880d681SAndroid Build Coastguard Worker 
1383*9880d681SAndroid Build Coastguard Worker     // Try to fold the mask and shift directly into the scale.
1384*9880d681SAndroid Build Coastguard Worker     if (!foldMaskAndShiftToScale(*CurDAG, N, Mask, Shift, X, AM))
1385*9880d681SAndroid Build Coastguard Worker       return false;
1386*9880d681SAndroid Build Coastguard Worker 
1387*9880d681SAndroid Build Coastguard Worker     // Try to swap the mask and shift to place shifts which can be done as
1388*9880d681SAndroid Build Coastguard Worker     // a scale on the outside of the mask.
1389*9880d681SAndroid Build Coastguard Worker     if (!foldMaskedShiftToScaledMask(*CurDAG, N, Mask, Shift, X, AM))
1390*9880d681SAndroid Build Coastguard Worker       return false;
1391*9880d681SAndroid Build Coastguard Worker     break;
1392*9880d681SAndroid Build Coastguard Worker   }
1393*9880d681SAndroid Build Coastguard Worker   }
1394*9880d681SAndroid Build Coastguard Worker 
1395*9880d681SAndroid Build Coastguard Worker   return matchAddressBase(N, AM);
1396*9880d681SAndroid Build Coastguard Worker }
1397*9880d681SAndroid Build Coastguard Worker 
1398*9880d681SAndroid Build Coastguard Worker /// Helper for MatchAddress. Add the specified node to the
1399*9880d681SAndroid Build Coastguard Worker /// specified addressing mode without any further recursion.
matchAddressBase(SDValue N,X86ISelAddressMode & AM)1400*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::matchAddressBase(SDValue N, X86ISelAddressMode &AM) {
1401*9880d681SAndroid Build Coastguard Worker   // Is the base register already occupied?
1402*9880d681SAndroid Build Coastguard Worker   if (AM.BaseType != X86ISelAddressMode::RegBase || AM.Base_Reg.getNode()) {
1403*9880d681SAndroid Build Coastguard Worker     // If so, check to see if the scale index register is set.
1404*9880d681SAndroid Build Coastguard Worker     if (!AM.IndexReg.getNode()) {
1405*9880d681SAndroid Build Coastguard Worker       AM.IndexReg = N;
1406*9880d681SAndroid Build Coastguard Worker       AM.Scale = 1;
1407*9880d681SAndroid Build Coastguard Worker       return false;
1408*9880d681SAndroid Build Coastguard Worker     }
1409*9880d681SAndroid Build Coastguard Worker 
1410*9880d681SAndroid Build Coastguard Worker     // Otherwise, we cannot select it.
1411*9880d681SAndroid Build Coastguard Worker     return true;
1412*9880d681SAndroid Build Coastguard Worker   }
1413*9880d681SAndroid Build Coastguard Worker 
1414*9880d681SAndroid Build Coastguard Worker   // Default, generate it as a register.
1415*9880d681SAndroid Build Coastguard Worker   AM.BaseType = X86ISelAddressMode::RegBase;
1416*9880d681SAndroid Build Coastguard Worker   AM.Base_Reg = N;
1417*9880d681SAndroid Build Coastguard Worker   return false;
1418*9880d681SAndroid Build Coastguard Worker }
1419*9880d681SAndroid Build Coastguard Worker 
selectVectorAddr(SDNode * Parent,SDValue N,SDValue & Base,SDValue & Scale,SDValue & Index,SDValue & Disp,SDValue & Segment)1420*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::selectVectorAddr(SDNode *Parent, SDValue N, SDValue &Base,
1421*9880d681SAndroid Build Coastguard Worker                                       SDValue &Scale, SDValue &Index,
1422*9880d681SAndroid Build Coastguard Worker                                       SDValue &Disp, SDValue &Segment) {
1423*9880d681SAndroid Build Coastguard Worker 
1424*9880d681SAndroid Build Coastguard Worker   MaskedGatherScatterSDNode *Mgs = dyn_cast<MaskedGatherScatterSDNode>(Parent);
1425*9880d681SAndroid Build Coastguard Worker   if (!Mgs)
1426*9880d681SAndroid Build Coastguard Worker     return false;
1427*9880d681SAndroid Build Coastguard Worker   X86ISelAddressMode AM;
1428*9880d681SAndroid Build Coastguard Worker   unsigned AddrSpace = Mgs->getPointerInfo().getAddrSpace();
1429*9880d681SAndroid Build Coastguard Worker   // AddrSpace 256 -> GS, 257 -> FS, 258 -> SS.
1430*9880d681SAndroid Build Coastguard Worker   if (AddrSpace == 256)
1431*9880d681SAndroid Build Coastguard Worker     AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16);
1432*9880d681SAndroid Build Coastguard Worker   if (AddrSpace == 257)
1433*9880d681SAndroid Build Coastguard Worker     AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16);
1434*9880d681SAndroid Build Coastguard Worker   if (AddrSpace == 258)
1435*9880d681SAndroid Build Coastguard Worker     AM.Segment = CurDAG->getRegister(X86::SS, MVT::i16);
1436*9880d681SAndroid Build Coastguard Worker 
1437*9880d681SAndroid Build Coastguard Worker   SDLoc DL(N);
1438*9880d681SAndroid Build Coastguard Worker   Base = Mgs->getBasePtr();
1439*9880d681SAndroid Build Coastguard Worker   Index = Mgs->getIndex();
1440*9880d681SAndroid Build Coastguard Worker   unsigned ScalarSize = Mgs->getValue().getValueType().getScalarSizeInBits();
1441*9880d681SAndroid Build Coastguard Worker   Scale = getI8Imm(ScalarSize/8, DL);
1442*9880d681SAndroid Build Coastguard Worker 
1443*9880d681SAndroid Build Coastguard Worker   // If Base is 0, the whole address is in index and the Scale is 1
1444*9880d681SAndroid Build Coastguard Worker   if (isa<ConstantSDNode>(Base)) {
1445*9880d681SAndroid Build Coastguard Worker     assert(cast<ConstantSDNode>(Base)->isNullValue() &&
1446*9880d681SAndroid Build Coastguard Worker            "Unexpected base in gather/scatter");
1447*9880d681SAndroid Build Coastguard Worker     Scale = getI8Imm(1, DL);
1448*9880d681SAndroid Build Coastguard Worker     Base = CurDAG->getRegister(0, MVT::i32);
1449*9880d681SAndroid Build Coastguard Worker   }
1450*9880d681SAndroid Build Coastguard Worker   if (AM.Segment.getNode())
1451*9880d681SAndroid Build Coastguard Worker     Segment = AM.Segment;
1452*9880d681SAndroid Build Coastguard Worker   else
1453*9880d681SAndroid Build Coastguard Worker     Segment = CurDAG->getRegister(0, MVT::i32);
1454*9880d681SAndroid Build Coastguard Worker   Disp = CurDAG->getTargetConstant(0, DL, MVT::i32);
1455*9880d681SAndroid Build Coastguard Worker   return true;
1456*9880d681SAndroid Build Coastguard Worker }
1457*9880d681SAndroid Build Coastguard Worker 
1458*9880d681SAndroid Build Coastguard Worker /// Returns true if it is able to pattern match an addressing mode.
1459*9880d681SAndroid Build Coastguard Worker /// It returns the operands which make up the maximal addressing mode it can
1460*9880d681SAndroid Build Coastguard Worker /// match by reference.
1461*9880d681SAndroid Build Coastguard Worker ///
1462*9880d681SAndroid Build Coastguard Worker /// Parent is the parent node of the addr operand that is being matched.  It
1463*9880d681SAndroid Build Coastguard Worker /// is always a load, store, atomic node, or null.  It is only null when
1464*9880d681SAndroid Build Coastguard Worker /// checking memory operands for inline asm nodes.
selectAddr(SDNode * Parent,SDValue N,SDValue & Base,SDValue & Scale,SDValue & Index,SDValue & Disp,SDValue & Segment)1465*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::selectAddr(SDNode *Parent, SDValue N, SDValue &Base,
1466*9880d681SAndroid Build Coastguard Worker                                  SDValue &Scale, SDValue &Index,
1467*9880d681SAndroid Build Coastguard Worker                                  SDValue &Disp, SDValue &Segment) {
1468*9880d681SAndroid Build Coastguard Worker   X86ISelAddressMode AM;
1469*9880d681SAndroid Build Coastguard Worker 
1470*9880d681SAndroid Build Coastguard Worker   if (Parent &&
1471*9880d681SAndroid Build Coastguard Worker       // This list of opcodes are all the nodes that have an "addr:$ptr" operand
1472*9880d681SAndroid Build Coastguard Worker       // that are not a MemSDNode, and thus don't have proper addrspace info.
1473*9880d681SAndroid Build Coastguard Worker       Parent->getOpcode() != ISD::INTRINSIC_W_CHAIN && // unaligned loads, fixme
1474*9880d681SAndroid Build Coastguard Worker       Parent->getOpcode() != ISD::INTRINSIC_VOID && // nontemporal stores
1475*9880d681SAndroid Build Coastguard Worker       Parent->getOpcode() != X86ISD::TLSCALL && // Fixme
1476*9880d681SAndroid Build Coastguard Worker       Parent->getOpcode() != X86ISD::EH_SJLJ_SETJMP && // setjmp
1477*9880d681SAndroid Build Coastguard Worker       Parent->getOpcode() != X86ISD::EH_SJLJ_LONGJMP) { // longjmp
1478*9880d681SAndroid Build Coastguard Worker     unsigned AddrSpace =
1479*9880d681SAndroid Build Coastguard Worker       cast<MemSDNode>(Parent)->getPointerInfo().getAddrSpace();
1480*9880d681SAndroid Build Coastguard Worker     // AddrSpace 256 -> GS, 257 -> FS, 258 -> SS.
1481*9880d681SAndroid Build Coastguard Worker     if (AddrSpace == 256)
1482*9880d681SAndroid Build Coastguard Worker       AM.Segment = CurDAG->getRegister(X86::GS, MVT::i16);
1483*9880d681SAndroid Build Coastguard Worker     if (AddrSpace == 257)
1484*9880d681SAndroid Build Coastguard Worker       AM.Segment = CurDAG->getRegister(X86::FS, MVT::i16);
1485*9880d681SAndroid Build Coastguard Worker     if (AddrSpace == 258)
1486*9880d681SAndroid Build Coastguard Worker       AM.Segment = CurDAG->getRegister(X86::SS, MVT::i16);
1487*9880d681SAndroid Build Coastguard Worker   }
1488*9880d681SAndroid Build Coastguard Worker 
1489*9880d681SAndroid Build Coastguard Worker   if (matchAddress(N, AM))
1490*9880d681SAndroid Build Coastguard Worker     return false;
1491*9880d681SAndroid Build Coastguard Worker 
1492*9880d681SAndroid Build Coastguard Worker   MVT VT = N.getSimpleValueType();
1493*9880d681SAndroid Build Coastguard Worker   if (AM.BaseType == X86ISelAddressMode::RegBase) {
1494*9880d681SAndroid Build Coastguard Worker     if (!AM.Base_Reg.getNode())
1495*9880d681SAndroid Build Coastguard Worker       AM.Base_Reg = CurDAG->getRegister(0, VT);
1496*9880d681SAndroid Build Coastguard Worker   }
1497*9880d681SAndroid Build Coastguard Worker 
1498*9880d681SAndroid Build Coastguard Worker   if (!AM.IndexReg.getNode())
1499*9880d681SAndroid Build Coastguard Worker     AM.IndexReg = CurDAG->getRegister(0, VT);
1500*9880d681SAndroid Build Coastguard Worker 
1501*9880d681SAndroid Build Coastguard Worker   getAddressOperands(AM, SDLoc(N), Base, Scale, Index, Disp, Segment);
1502*9880d681SAndroid Build Coastguard Worker   return true;
1503*9880d681SAndroid Build Coastguard Worker }
1504*9880d681SAndroid Build Coastguard Worker 
1505*9880d681SAndroid Build Coastguard Worker /// Match a scalar SSE load. In particular, we want to match a load whose top
1506*9880d681SAndroid Build Coastguard Worker /// elements are either undef or zeros. The load flavor is derived from the
1507*9880d681SAndroid Build Coastguard Worker /// type of N, which is either v4f32 or v2f64.
1508*9880d681SAndroid Build Coastguard Worker ///
1509*9880d681SAndroid Build Coastguard Worker /// We also return:
1510*9880d681SAndroid Build Coastguard Worker ///   PatternChainNode: this is the matched node that has a chain input and
1511*9880d681SAndroid Build Coastguard Worker ///   output.
selectScalarSSELoad(SDNode * Root,SDValue N,SDValue & Base,SDValue & Scale,SDValue & Index,SDValue & Disp,SDValue & Segment,SDValue & PatternNodeWithChain)1512*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::selectScalarSSELoad(SDNode *Root,
1513*9880d681SAndroid Build Coastguard Worker                                           SDValue N, SDValue &Base,
1514*9880d681SAndroid Build Coastguard Worker                                           SDValue &Scale, SDValue &Index,
1515*9880d681SAndroid Build Coastguard Worker                                           SDValue &Disp, SDValue &Segment,
1516*9880d681SAndroid Build Coastguard Worker                                           SDValue &PatternNodeWithChain) {
1517*9880d681SAndroid Build Coastguard Worker   if (N.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1518*9880d681SAndroid Build Coastguard Worker     PatternNodeWithChain = N.getOperand(0);
1519*9880d681SAndroid Build Coastguard Worker     if (ISD::isNON_EXTLoad(PatternNodeWithChain.getNode()) &&
1520*9880d681SAndroid Build Coastguard Worker         PatternNodeWithChain.hasOneUse() &&
1521*9880d681SAndroid Build Coastguard Worker         IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
1522*9880d681SAndroid Build Coastguard Worker         IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
1523*9880d681SAndroid Build Coastguard Worker       LoadSDNode *LD = cast<LoadSDNode>(PatternNodeWithChain);
1524*9880d681SAndroid Build Coastguard Worker       if (!selectAddr(LD, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
1525*9880d681SAndroid Build Coastguard Worker         return false;
1526*9880d681SAndroid Build Coastguard Worker       return true;
1527*9880d681SAndroid Build Coastguard Worker     }
1528*9880d681SAndroid Build Coastguard Worker   }
1529*9880d681SAndroid Build Coastguard Worker 
1530*9880d681SAndroid Build Coastguard Worker   // Also handle the case where we explicitly require zeros in the top
1531*9880d681SAndroid Build Coastguard Worker   // elements.  This is a vector shuffle from the zero vector.
1532*9880d681SAndroid Build Coastguard Worker   if (N.getOpcode() == X86ISD::VZEXT_MOVL && N.getNode()->hasOneUse() &&
1533*9880d681SAndroid Build Coastguard Worker       // Check to see if the top elements are all zeros (or bitcast of zeros).
1534*9880d681SAndroid Build Coastguard Worker       N.getOperand(0).getOpcode() == ISD::SCALAR_TO_VECTOR &&
1535*9880d681SAndroid Build Coastguard Worker       N.getOperand(0).getNode()->hasOneUse() &&
1536*9880d681SAndroid Build Coastguard Worker       ISD::isNON_EXTLoad(N.getOperand(0).getOperand(0).getNode()) &&
1537*9880d681SAndroid Build Coastguard Worker       N.getOperand(0).getOperand(0).hasOneUse() &&
1538*9880d681SAndroid Build Coastguard Worker       IsProfitableToFold(N.getOperand(0), N.getNode(), Root) &&
1539*9880d681SAndroid Build Coastguard Worker       IsLegalToFold(N.getOperand(0), N.getNode(), Root, OptLevel)) {
1540*9880d681SAndroid Build Coastguard Worker     // Okay, this is a zero extending load.  Fold it.
1541*9880d681SAndroid Build Coastguard Worker     LoadSDNode *LD = cast<LoadSDNode>(N.getOperand(0).getOperand(0));
1542*9880d681SAndroid Build Coastguard Worker     if (!selectAddr(LD, LD->getBasePtr(), Base, Scale, Index, Disp, Segment))
1543*9880d681SAndroid Build Coastguard Worker       return false;
1544*9880d681SAndroid Build Coastguard Worker     PatternNodeWithChain = SDValue(LD, 0);
1545*9880d681SAndroid Build Coastguard Worker     return true;
1546*9880d681SAndroid Build Coastguard Worker   }
1547*9880d681SAndroid Build Coastguard Worker   return false;
1548*9880d681SAndroid Build Coastguard Worker }
1549*9880d681SAndroid Build Coastguard Worker 
1550*9880d681SAndroid Build Coastguard Worker 
selectMOV64Imm32(SDValue N,SDValue & Imm)1551*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::selectMOV64Imm32(SDValue N, SDValue &Imm) {
1552*9880d681SAndroid Build Coastguard Worker   if (const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
1553*9880d681SAndroid Build Coastguard Worker     uint64_t ImmVal = CN->getZExtValue();
1554*9880d681SAndroid Build Coastguard Worker     if ((uint32_t)ImmVal != (uint64_t)ImmVal)
1555*9880d681SAndroid Build Coastguard Worker       return false;
1556*9880d681SAndroid Build Coastguard Worker 
1557*9880d681SAndroid Build Coastguard Worker     Imm = CurDAG->getTargetConstant(ImmVal, SDLoc(N), MVT::i64);
1558*9880d681SAndroid Build Coastguard Worker     return true;
1559*9880d681SAndroid Build Coastguard Worker   }
1560*9880d681SAndroid Build Coastguard Worker 
1561*9880d681SAndroid Build Coastguard Worker   // In static codegen with small code model, we can get the address of a label
1562*9880d681SAndroid Build Coastguard Worker   // into a register with 'movl'. TableGen has already made sure we're looking
1563*9880d681SAndroid Build Coastguard Worker   // at a label of some kind.
1564*9880d681SAndroid Build Coastguard Worker   assert(N->getOpcode() == X86ISD::Wrapper &&
1565*9880d681SAndroid Build Coastguard Worker          "Unexpected node type for MOV32ri64");
1566*9880d681SAndroid Build Coastguard Worker   N = N.getOperand(0);
1567*9880d681SAndroid Build Coastguard Worker 
1568*9880d681SAndroid Build Coastguard Worker   if (N->getOpcode() != ISD::TargetConstantPool &&
1569*9880d681SAndroid Build Coastguard Worker       N->getOpcode() != ISD::TargetJumpTable &&
1570*9880d681SAndroid Build Coastguard Worker       N->getOpcode() != ISD::TargetGlobalAddress &&
1571*9880d681SAndroid Build Coastguard Worker       N->getOpcode() != ISD::TargetExternalSymbol &&
1572*9880d681SAndroid Build Coastguard Worker       N->getOpcode() != ISD::MCSymbol &&
1573*9880d681SAndroid Build Coastguard Worker       N->getOpcode() != ISD::TargetBlockAddress)
1574*9880d681SAndroid Build Coastguard Worker     return false;
1575*9880d681SAndroid Build Coastguard Worker 
1576*9880d681SAndroid Build Coastguard Worker   Imm = N;
1577*9880d681SAndroid Build Coastguard Worker   return TM.getCodeModel() == CodeModel::Small;
1578*9880d681SAndroid Build Coastguard Worker }
1579*9880d681SAndroid Build Coastguard Worker 
selectLEA64_32Addr(SDValue N,SDValue & Base,SDValue & Scale,SDValue & Index,SDValue & Disp,SDValue & Segment)1580*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::selectLEA64_32Addr(SDValue N, SDValue &Base,
1581*9880d681SAndroid Build Coastguard Worker                                          SDValue &Scale, SDValue &Index,
1582*9880d681SAndroid Build Coastguard Worker                                          SDValue &Disp, SDValue &Segment) {
1583*9880d681SAndroid Build Coastguard Worker   // Save the debug loc before calling selectLEAAddr, in case it invalidates N.
1584*9880d681SAndroid Build Coastguard Worker   SDLoc DL(N);
1585*9880d681SAndroid Build Coastguard Worker 
1586*9880d681SAndroid Build Coastguard Worker   if (!selectLEAAddr(N, Base, Scale, Index, Disp, Segment))
1587*9880d681SAndroid Build Coastguard Worker     return false;
1588*9880d681SAndroid Build Coastguard Worker 
1589*9880d681SAndroid Build Coastguard Worker   RegisterSDNode *RN = dyn_cast<RegisterSDNode>(Base);
1590*9880d681SAndroid Build Coastguard Worker   if (RN && RN->getReg() == 0)
1591*9880d681SAndroid Build Coastguard Worker     Base = CurDAG->getRegister(0, MVT::i64);
1592*9880d681SAndroid Build Coastguard Worker   else if (Base.getValueType() == MVT::i32 && !dyn_cast<FrameIndexSDNode>(Base)) {
1593*9880d681SAndroid Build Coastguard Worker     // Base could already be %rip, particularly in the x32 ABI.
1594*9880d681SAndroid Build Coastguard Worker     Base = SDValue(CurDAG->getMachineNode(
1595*9880d681SAndroid Build Coastguard Worker                        TargetOpcode::SUBREG_TO_REG, DL, MVT::i64,
1596*9880d681SAndroid Build Coastguard Worker                        CurDAG->getTargetConstant(0, DL, MVT::i64),
1597*9880d681SAndroid Build Coastguard Worker                        Base,
1598*9880d681SAndroid Build Coastguard Worker                        CurDAG->getTargetConstant(X86::sub_32bit, DL, MVT::i32)),
1599*9880d681SAndroid Build Coastguard Worker                    0);
1600*9880d681SAndroid Build Coastguard Worker   }
1601*9880d681SAndroid Build Coastguard Worker 
1602*9880d681SAndroid Build Coastguard Worker   RN = dyn_cast<RegisterSDNode>(Index);
1603*9880d681SAndroid Build Coastguard Worker   if (RN && RN->getReg() == 0)
1604*9880d681SAndroid Build Coastguard Worker     Index = CurDAG->getRegister(0, MVT::i64);
1605*9880d681SAndroid Build Coastguard Worker   else {
1606*9880d681SAndroid Build Coastguard Worker     assert(Index.getValueType() == MVT::i32 &&
1607*9880d681SAndroid Build Coastguard Worker            "Expect to be extending 32-bit registers for use in LEA");
1608*9880d681SAndroid Build Coastguard Worker     Index = SDValue(CurDAG->getMachineNode(
1609*9880d681SAndroid Build Coastguard Worker                         TargetOpcode::SUBREG_TO_REG, DL, MVT::i64,
1610*9880d681SAndroid Build Coastguard Worker                         CurDAG->getTargetConstant(0, DL, MVT::i64),
1611*9880d681SAndroid Build Coastguard Worker                         Index,
1612*9880d681SAndroid Build Coastguard Worker                         CurDAG->getTargetConstant(X86::sub_32bit, DL,
1613*9880d681SAndroid Build Coastguard Worker                                                   MVT::i32)),
1614*9880d681SAndroid Build Coastguard Worker                     0);
1615*9880d681SAndroid Build Coastguard Worker   }
1616*9880d681SAndroid Build Coastguard Worker 
1617*9880d681SAndroid Build Coastguard Worker   return true;
1618*9880d681SAndroid Build Coastguard Worker }
1619*9880d681SAndroid Build Coastguard Worker 
1620*9880d681SAndroid Build Coastguard Worker /// Calls SelectAddr and determines if the maximal addressing
1621*9880d681SAndroid Build Coastguard Worker /// mode it matches can be cost effectively emitted as an LEA instruction.
selectLEAAddr(SDValue N,SDValue & Base,SDValue & Scale,SDValue & Index,SDValue & Disp,SDValue & Segment)1622*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::selectLEAAddr(SDValue N,
1623*9880d681SAndroid Build Coastguard Worker                                     SDValue &Base, SDValue &Scale,
1624*9880d681SAndroid Build Coastguard Worker                                     SDValue &Index, SDValue &Disp,
1625*9880d681SAndroid Build Coastguard Worker                                     SDValue &Segment) {
1626*9880d681SAndroid Build Coastguard Worker   X86ISelAddressMode AM;
1627*9880d681SAndroid Build Coastguard Worker 
1628*9880d681SAndroid Build Coastguard Worker   // Save the DL and VT before calling matchAddress, it can invalidate N.
1629*9880d681SAndroid Build Coastguard Worker   SDLoc DL(N);
1630*9880d681SAndroid Build Coastguard Worker   MVT VT = N.getSimpleValueType();
1631*9880d681SAndroid Build Coastguard Worker 
1632*9880d681SAndroid Build Coastguard Worker   // Set AM.Segment to prevent MatchAddress from using one. LEA doesn't support
1633*9880d681SAndroid Build Coastguard Worker   // segments.
1634*9880d681SAndroid Build Coastguard Worker   SDValue Copy = AM.Segment;
1635*9880d681SAndroid Build Coastguard Worker   SDValue T = CurDAG->getRegister(0, MVT::i32);
1636*9880d681SAndroid Build Coastguard Worker   AM.Segment = T;
1637*9880d681SAndroid Build Coastguard Worker   if (matchAddress(N, AM))
1638*9880d681SAndroid Build Coastguard Worker     return false;
1639*9880d681SAndroid Build Coastguard Worker   assert (T == AM.Segment);
1640*9880d681SAndroid Build Coastguard Worker   AM.Segment = Copy;
1641*9880d681SAndroid Build Coastguard Worker 
1642*9880d681SAndroid Build Coastguard Worker   unsigned Complexity = 0;
1643*9880d681SAndroid Build Coastguard Worker   if (AM.BaseType == X86ISelAddressMode::RegBase)
1644*9880d681SAndroid Build Coastguard Worker     if (AM.Base_Reg.getNode())
1645*9880d681SAndroid Build Coastguard Worker       Complexity = 1;
1646*9880d681SAndroid Build Coastguard Worker     else
1647*9880d681SAndroid Build Coastguard Worker       AM.Base_Reg = CurDAG->getRegister(0, VT);
1648*9880d681SAndroid Build Coastguard Worker   else if (AM.BaseType == X86ISelAddressMode::FrameIndexBase)
1649*9880d681SAndroid Build Coastguard Worker     Complexity = 4;
1650*9880d681SAndroid Build Coastguard Worker 
1651*9880d681SAndroid Build Coastguard Worker   if (AM.IndexReg.getNode())
1652*9880d681SAndroid Build Coastguard Worker     Complexity++;
1653*9880d681SAndroid Build Coastguard Worker   else
1654*9880d681SAndroid Build Coastguard Worker     AM.IndexReg = CurDAG->getRegister(0, VT);
1655*9880d681SAndroid Build Coastguard Worker 
1656*9880d681SAndroid Build Coastguard Worker   // Don't match just leal(,%reg,2). It's cheaper to do addl %reg, %reg, or with
1657*9880d681SAndroid Build Coastguard Worker   // a simple shift.
1658*9880d681SAndroid Build Coastguard Worker   if (AM.Scale > 1)
1659*9880d681SAndroid Build Coastguard Worker     Complexity++;
1660*9880d681SAndroid Build Coastguard Worker 
1661*9880d681SAndroid Build Coastguard Worker   // FIXME: We are artificially lowering the criteria to turn ADD %reg, $GA
1662*9880d681SAndroid Build Coastguard Worker   // to a LEA. This is determined with some experimentation but is by no means
1663*9880d681SAndroid Build Coastguard Worker   // optimal (especially for code size consideration). LEA is nice because of
1664*9880d681SAndroid Build Coastguard Worker   // its three-address nature. Tweak the cost function again when we can run
1665*9880d681SAndroid Build Coastguard Worker   // convertToThreeAddress() at register allocation time.
1666*9880d681SAndroid Build Coastguard Worker   if (AM.hasSymbolicDisplacement()) {
1667*9880d681SAndroid Build Coastguard Worker     // For X86-64, always use LEA to materialize RIP-relative addresses.
1668*9880d681SAndroid Build Coastguard Worker     if (Subtarget->is64Bit())
1669*9880d681SAndroid Build Coastguard Worker       Complexity = 4;
1670*9880d681SAndroid Build Coastguard Worker     else
1671*9880d681SAndroid Build Coastguard Worker       Complexity += 2;
1672*9880d681SAndroid Build Coastguard Worker   }
1673*9880d681SAndroid Build Coastguard Worker 
1674*9880d681SAndroid Build Coastguard Worker   if (AM.Disp && (AM.Base_Reg.getNode() || AM.IndexReg.getNode()))
1675*9880d681SAndroid Build Coastguard Worker     Complexity++;
1676*9880d681SAndroid Build Coastguard Worker 
1677*9880d681SAndroid Build Coastguard Worker   // If it isn't worth using an LEA, reject it.
1678*9880d681SAndroid Build Coastguard Worker   if (Complexity <= 2)
1679*9880d681SAndroid Build Coastguard Worker     return false;
1680*9880d681SAndroid Build Coastguard Worker 
1681*9880d681SAndroid Build Coastguard Worker   getAddressOperands(AM, DL, Base, Scale, Index, Disp, Segment);
1682*9880d681SAndroid Build Coastguard Worker   return true;
1683*9880d681SAndroid Build Coastguard Worker }
1684*9880d681SAndroid Build Coastguard Worker 
1685*9880d681SAndroid Build Coastguard Worker /// This is only run on TargetGlobalTLSAddress nodes.
selectTLSADDRAddr(SDValue N,SDValue & Base,SDValue & Scale,SDValue & Index,SDValue & Disp,SDValue & Segment)1686*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::selectTLSADDRAddr(SDValue N, SDValue &Base,
1687*9880d681SAndroid Build Coastguard Worker                                         SDValue &Scale, SDValue &Index,
1688*9880d681SAndroid Build Coastguard Worker                                         SDValue &Disp, SDValue &Segment) {
1689*9880d681SAndroid Build Coastguard Worker   assert(N.getOpcode() == ISD::TargetGlobalTLSAddress);
1690*9880d681SAndroid Build Coastguard Worker   const GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(N);
1691*9880d681SAndroid Build Coastguard Worker 
1692*9880d681SAndroid Build Coastguard Worker   X86ISelAddressMode AM;
1693*9880d681SAndroid Build Coastguard Worker   AM.GV = GA->getGlobal();
1694*9880d681SAndroid Build Coastguard Worker   AM.Disp += GA->getOffset();
1695*9880d681SAndroid Build Coastguard Worker   AM.Base_Reg = CurDAG->getRegister(0, N.getValueType());
1696*9880d681SAndroid Build Coastguard Worker   AM.SymbolFlags = GA->getTargetFlags();
1697*9880d681SAndroid Build Coastguard Worker 
1698*9880d681SAndroid Build Coastguard Worker   if (N.getValueType() == MVT::i32) {
1699*9880d681SAndroid Build Coastguard Worker     AM.Scale = 1;
1700*9880d681SAndroid Build Coastguard Worker     AM.IndexReg = CurDAG->getRegister(X86::EBX, MVT::i32);
1701*9880d681SAndroid Build Coastguard Worker   } else {
1702*9880d681SAndroid Build Coastguard Worker     AM.IndexReg = CurDAG->getRegister(0, MVT::i64);
1703*9880d681SAndroid Build Coastguard Worker   }
1704*9880d681SAndroid Build Coastguard Worker 
1705*9880d681SAndroid Build Coastguard Worker   getAddressOperands(AM, SDLoc(N), Base, Scale, Index, Disp, Segment);
1706*9880d681SAndroid Build Coastguard Worker   return true;
1707*9880d681SAndroid Build Coastguard Worker }
1708*9880d681SAndroid Build Coastguard Worker 
1709*9880d681SAndroid Build Coastguard Worker 
tryFoldLoad(SDNode * P,SDValue N,SDValue & Base,SDValue & Scale,SDValue & Index,SDValue & Disp,SDValue & Segment)1710*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::tryFoldLoad(SDNode *P, SDValue N,
1711*9880d681SAndroid Build Coastguard Worker                                   SDValue &Base, SDValue &Scale,
1712*9880d681SAndroid Build Coastguard Worker                                   SDValue &Index, SDValue &Disp,
1713*9880d681SAndroid Build Coastguard Worker                                   SDValue &Segment) {
1714*9880d681SAndroid Build Coastguard Worker   if (!ISD::isNON_EXTLoad(N.getNode()) ||
1715*9880d681SAndroid Build Coastguard Worker       !IsProfitableToFold(N, P, P) ||
1716*9880d681SAndroid Build Coastguard Worker       !IsLegalToFold(N, P, P, OptLevel))
1717*9880d681SAndroid Build Coastguard Worker     return false;
1718*9880d681SAndroid Build Coastguard Worker 
1719*9880d681SAndroid Build Coastguard Worker   return selectAddr(N.getNode(),
1720*9880d681SAndroid Build Coastguard Worker                     N.getOperand(1), Base, Scale, Index, Disp, Segment);
1721*9880d681SAndroid Build Coastguard Worker }
1722*9880d681SAndroid Build Coastguard Worker 
1723*9880d681SAndroid Build Coastguard Worker /// Return an SDNode that returns the value of the global base register.
1724*9880d681SAndroid Build Coastguard Worker /// Output instructions required to initialize the global base register,
1725*9880d681SAndroid Build Coastguard Worker /// if necessary.
getGlobalBaseReg()1726*9880d681SAndroid Build Coastguard Worker SDNode *X86DAGToDAGISel::getGlobalBaseReg() {
1727*9880d681SAndroid Build Coastguard Worker   unsigned GlobalBaseReg = getInstrInfo()->getGlobalBaseReg(MF);
1728*9880d681SAndroid Build Coastguard Worker   auto &DL = MF->getDataLayout();
1729*9880d681SAndroid Build Coastguard Worker   return CurDAG->getRegister(GlobalBaseReg, TLI->getPointerTy(DL)).getNode();
1730*9880d681SAndroid Build Coastguard Worker }
1731*9880d681SAndroid Build Coastguard Worker 
1732*9880d681SAndroid Build Coastguard Worker /// Test whether the given X86ISD::CMP node has any uses which require the SF
1733*9880d681SAndroid Build Coastguard Worker /// or OF bits to be accurate.
hasNoSignedComparisonUses(SDNode * N)1734*9880d681SAndroid Build Coastguard Worker static bool hasNoSignedComparisonUses(SDNode *N) {
1735*9880d681SAndroid Build Coastguard Worker   // Examine each user of the node.
1736*9880d681SAndroid Build Coastguard Worker   for (SDNode::use_iterator UI = N->use_begin(),
1737*9880d681SAndroid Build Coastguard Worker          UE = N->use_end(); UI != UE; ++UI) {
1738*9880d681SAndroid Build Coastguard Worker     // Only examine CopyToReg uses.
1739*9880d681SAndroid Build Coastguard Worker     if (UI->getOpcode() != ISD::CopyToReg)
1740*9880d681SAndroid Build Coastguard Worker       return false;
1741*9880d681SAndroid Build Coastguard Worker     // Only examine CopyToReg uses that copy to EFLAGS.
1742*9880d681SAndroid Build Coastguard Worker     if (cast<RegisterSDNode>(UI->getOperand(1))->getReg() !=
1743*9880d681SAndroid Build Coastguard Worker           X86::EFLAGS)
1744*9880d681SAndroid Build Coastguard Worker       return false;
1745*9880d681SAndroid Build Coastguard Worker     // Examine each user of the CopyToReg use.
1746*9880d681SAndroid Build Coastguard Worker     for (SDNode::use_iterator FlagUI = UI->use_begin(),
1747*9880d681SAndroid Build Coastguard Worker            FlagUE = UI->use_end(); FlagUI != FlagUE; ++FlagUI) {
1748*9880d681SAndroid Build Coastguard Worker       // Only examine the Flag result.
1749*9880d681SAndroid Build Coastguard Worker       if (FlagUI.getUse().getResNo() != 1) continue;
1750*9880d681SAndroid Build Coastguard Worker       // Anything unusual: assume conservatively.
1751*9880d681SAndroid Build Coastguard Worker       if (!FlagUI->isMachineOpcode()) return false;
1752*9880d681SAndroid Build Coastguard Worker       // Examine the opcode of the user.
1753*9880d681SAndroid Build Coastguard Worker       switch (FlagUI->getMachineOpcode()) {
1754*9880d681SAndroid Build Coastguard Worker       // These comparisons don't treat the most significant bit specially.
1755*9880d681SAndroid Build Coastguard Worker       case X86::SETAr: case X86::SETAEr: case X86::SETBr: case X86::SETBEr:
1756*9880d681SAndroid Build Coastguard Worker       case X86::SETEr: case X86::SETNEr: case X86::SETPr: case X86::SETNPr:
1757*9880d681SAndroid Build Coastguard Worker       case X86::SETAm: case X86::SETAEm: case X86::SETBm: case X86::SETBEm:
1758*9880d681SAndroid Build Coastguard Worker       case X86::SETEm: case X86::SETNEm: case X86::SETPm: case X86::SETNPm:
1759*9880d681SAndroid Build Coastguard Worker       case X86::JA_1: case X86::JAE_1: case X86::JB_1: case X86::JBE_1:
1760*9880d681SAndroid Build Coastguard Worker       case X86::JE_1: case X86::JNE_1: case X86::JP_1: case X86::JNP_1:
1761*9880d681SAndroid Build Coastguard Worker       case X86::CMOVA16rr: case X86::CMOVA16rm:
1762*9880d681SAndroid Build Coastguard Worker       case X86::CMOVA32rr: case X86::CMOVA32rm:
1763*9880d681SAndroid Build Coastguard Worker       case X86::CMOVA64rr: case X86::CMOVA64rm:
1764*9880d681SAndroid Build Coastguard Worker       case X86::CMOVAE16rr: case X86::CMOVAE16rm:
1765*9880d681SAndroid Build Coastguard Worker       case X86::CMOVAE32rr: case X86::CMOVAE32rm:
1766*9880d681SAndroid Build Coastguard Worker       case X86::CMOVAE64rr: case X86::CMOVAE64rm:
1767*9880d681SAndroid Build Coastguard Worker       case X86::CMOVB16rr: case X86::CMOVB16rm:
1768*9880d681SAndroid Build Coastguard Worker       case X86::CMOVB32rr: case X86::CMOVB32rm:
1769*9880d681SAndroid Build Coastguard Worker       case X86::CMOVB64rr: case X86::CMOVB64rm:
1770*9880d681SAndroid Build Coastguard Worker       case X86::CMOVBE16rr: case X86::CMOVBE16rm:
1771*9880d681SAndroid Build Coastguard Worker       case X86::CMOVBE32rr: case X86::CMOVBE32rm:
1772*9880d681SAndroid Build Coastguard Worker       case X86::CMOVBE64rr: case X86::CMOVBE64rm:
1773*9880d681SAndroid Build Coastguard Worker       case X86::CMOVE16rr: case X86::CMOVE16rm:
1774*9880d681SAndroid Build Coastguard Worker       case X86::CMOVE32rr: case X86::CMOVE32rm:
1775*9880d681SAndroid Build Coastguard Worker       case X86::CMOVE64rr: case X86::CMOVE64rm:
1776*9880d681SAndroid Build Coastguard Worker       case X86::CMOVNE16rr: case X86::CMOVNE16rm:
1777*9880d681SAndroid Build Coastguard Worker       case X86::CMOVNE32rr: case X86::CMOVNE32rm:
1778*9880d681SAndroid Build Coastguard Worker       case X86::CMOVNE64rr: case X86::CMOVNE64rm:
1779*9880d681SAndroid Build Coastguard Worker       case X86::CMOVNP16rr: case X86::CMOVNP16rm:
1780*9880d681SAndroid Build Coastguard Worker       case X86::CMOVNP32rr: case X86::CMOVNP32rm:
1781*9880d681SAndroid Build Coastguard Worker       case X86::CMOVNP64rr: case X86::CMOVNP64rm:
1782*9880d681SAndroid Build Coastguard Worker       case X86::CMOVP16rr: case X86::CMOVP16rm:
1783*9880d681SAndroid Build Coastguard Worker       case X86::CMOVP32rr: case X86::CMOVP32rm:
1784*9880d681SAndroid Build Coastguard Worker       case X86::CMOVP64rr: case X86::CMOVP64rm:
1785*9880d681SAndroid Build Coastguard Worker         continue;
1786*9880d681SAndroid Build Coastguard Worker       // Anything else: assume conservatively.
1787*9880d681SAndroid Build Coastguard Worker       default: return false;
1788*9880d681SAndroid Build Coastguard Worker       }
1789*9880d681SAndroid Build Coastguard Worker     }
1790*9880d681SAndroid Build Coastguard Worker   }
1791*9880d681SAndroid Build Coastguard Worker   return true;
1792*9880d681SAndroid Build Coastguard Worker }
1793*9880d681SAndroid Build Coastguard Worker 
1794*9880d681SAndroid Build Coastguard Worker /// Check whether or not the chain ending in StoreNode is suitable for doing
1795*9880d681SAndroid Build Coastguard Worker /// the {load; increment or decrement; store} to modify transformation.
isLoadIncOrDecStore(StoreSDNode * StoreNode,unsigned Opc,SDValue StoredVal,SelectionDAG * CurDAG,LoadSDNode * & LoadNode,SDValue & InputChain)1796*9880d681SAndroid Build Coastguard Worker static bool isLoadIncOrDecStore(StoreSDNode *StoreNode, unsigned Opc,
1797*9880d681SAndroid Build Coastguard Worker                                 SDValue StoredVal, SelectionDAG *CurDAG,
1798*9880d681SAndroid Build Coastguard Worker                                 LoadSDNode* &LoadNode, SDValue &InputChain) {
1799*9880d681SAndroid Build Coastguard Worker 
1800*9880d681SAndroid Build Coastguard Worker   // is the value stored the result of a DEC or INC?
1801*9880d681SAndroid Build Coastguard Worker   if (!(Opc == X86ISD::DEC || Opc == X86ISD::INC)) return false;
1802*9880d681SAndroid Build Coastguard Worker 
1803*9880d681SAndroid Build Coastguard Worker   // is the stored value result 0 of the load?
1804*9880d681SAndroid Build Coastguard Worker   if (StoredVal.getResNo() != 0) return false;
1805*9880d681SAndroid Build Coastguard Worker 
1806*9880d681SAndroid Build Coastguard Worker   // are there other uses of the loaded value than the inc or dec?
1807*9880d681SAndroid Build Coastguard Worker   if (!StoredVal.getNode()->hasNUsesOfValue(1, 0)) return false;
1808*9880d681SAndroid Build Coastguard Worker 
1809*9880d681SAndroid Build Coastguard Worker   // is the store non-extending and non-indexed?
1810*9880d681SAndroid Build Coastguard Worker   if (!ISD::isNormalStore(StoreNode) || StoreNode->isNonTemporal())
1811*9880d681SAndroid Build Coastguard Worker     return false;
1812*9880d681SAndroid Build Coastguard Worker 
1813*9880d681SAndroid Build Coastguard Worker   SDValue Load = StoredVal->getOperand(0);
1814*9880d681SAndroid Build Coastguard Worker   // Is the stored value a non-extending and non-indexed load?
1815*9880d681SAndroid Build Coastguard Worker   if (!ISD::isNormalLoad(Load.getNode())) return false;
1816*9880d681SAndroid Build Coastguard Worker 
1817*9880d681SAndroid Build Coastguard Worker   // Return LoadNode by reference.
1818*9880d681SAndroid Build Coastguard Worker   LoadNode = cast<LoadSDNode>(Load);
1819*9880d681SAndroid Build Coastguard Worker   // is the size of the value one that we can handle? (i.e. 64, 32, 16, or 8)
1820*9880d681SAndroid Build Coastguard Worker   EVT LdVT = LoadNode->getMemoryVT();
1821*9880d681SAndroid Build Coastguard Worker   if (LdVT != MVT::i64 && LdVT != MVT::i32 && LdVT != MVT::i16 &&
1822*9880d681SAndroid Build Coastguard Worker       LdVT != MVT::i8)
1823*9880d681SAndroid Build Coastguard Worker     return false;
1824*9880d681SAndroid Build Coastguard Worker 
1825*9880d681SAndroid Build Coastguard Worker   // Is store the only read of the loaded value?
1826*9880d681SAndroid Build Coastguard Worker   if (!Load.hasOneUse())
1827*9880d681SAndroid Build Coastguard Worker     return false;
1828*9880d681SAndroid Build Coastguard Worker 
1829*9880d681SAndroid Build Coastguard Worker   // Is the address of the store the same as the load?
1830*9880d681SAndroid Build Coastguard Worker   if (LoadNode->getBasePtr() != StoreNode->getBasePtr() ||
1831*9880d681SAndroid Build Coastguard Worker       LoadNode->getOffset() != StoreNode->getOffset())
1832*9880d681SAndroid Build Coastguard Worker     return false;
1833*9880d681SAndroid Build Coastguard Worker 
1834*9880d681SAndroid Build Coastguard Worker   // Check if the chain is produced by the load or is a TokenFactor with
1835*9880d681SAndroid Build Coastguard Worker   // the load output chain as an operand. Return InputChain by reference.
1836*9880d681SAndroid Build Coastguard Worker   SDValue Chain = StoreNode->getChain();
1837*9880d681SAndroid Build Coastguard Worker 
1838*9880d681SAndroid Build Coastguard Worker   bool ChainCheck = false;
1839*9880d681SAndroid Build Coastguard Worker   if (Chain == Load.getValue(1)) {
1840*9880d681SAndroid Build Coastguard Worker     ChainCheck = true;
1841*9880d681SAndroid Build Coastguard Worker     InputChain = LoadNode->getChain();
1842*9880d681SAndroid Build Coastguard Worker   } else if (Chain.getOpcode() == ISD::TokenFactor) {
1843*9880d681SAndroid Build Coastguard Worker     SmallVector<SDValue, 4> ChainOps;
1844*9880d681SAndroid Build Coastguard Worker     for (unsigned i = 0, e = Chain.getNumOperands(); i != e; ++i) {
1845*9880d681SAndroid Build Coastguard Worker       SDValue Op = Chain.getOperand(i);
1846*9880d681SAndroid Build Coastguard Worker       if (Op == Load.getValue(1)) {
1847*9880d681SAndroid Build Coastguard Worker         ChainCheck = true;
1848*9880d681SAndroid Build Coastguard Worker         continue;
1849*9880d681SAndroid Build Coastguard Worker       }
1850*9880d681SAndroid Build Coastguard Worker 
1851*9880d681SAndroid Build Coastguard Worker       // Make sure using Op as part of the chain would not cause a cycle here.
1852*9880d681SAndroid Build Coastguard Worker       // In theory, we could check whether the chain node is a predecessor of
1853*9880d681SAndroid Build Coastguard Worker       // the load. But that can be very expensive. Instead visit the uses and
1854*9880d681SAndroid Build Coastguard Worker       // make sure they all have smaller node id than the load.
1855*9880d681SAndroid Build Coastguard Worker       int LoadId = LoadNode->getNodeId();
1856*9880d681SAndroid Build Coastguard Worker       for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
1857*9880d681SAndroid Build Coastguard Worker              UE = UI->use_end(); UI != UE; ++UI) {
1858*9880d681SAndroid Build Coastguard Worker         if (UI.getUse().getResNo() != 0)
1859*9880d681SAndroid Build Coastguard Worker           continue;
1860*9880d681SAndroid Build Coastguard Worker         if (UI->getNodeId() > LoadId)
1861*9880d681SAndroid Build Coastguard Worker           return false;
1862*9880d681SAndroid Build Coastguard Worker       }
1863*9880d681SAndroid Build Coastguard Worker 
1864*9880d681SAndroid Build Coastguard Worker       ChainOps.push_back(Op);
1865*9880d681SAndroid Build Coastguard Worker     }
1866*9880d681SAndroid Build Coastguard Worker 
1867*9880d681SAndroid Build Coastguard Worker     if (ChainCheck)
1868*9880d681SAndroid Build Coastguard Worker       // Make a new TokenFactor with all the other input chains except
1869*9880d681SAndroid Build Coastguard Worker       // for the load.
1870*9880d681SAndroid Build Coastguard Worker       InputChain = CurDAG->getNode(ISD::TokenFactor, SDLoc(Chain),
1871*9880d681SAndroid Build Coastguard Worker                                    MVT::Other, ChainOps);
1872*9880d681SAndroid Build Coastguard Worker   }
1873*9880d681SAndroid Build Coastguard Worker   if (!ChainCheck)
1874*9880d681SAndroid Build Coastguard Worker     return false;
1875*9880d681SAndroid Build Coastguard Worker 
1876*9880d681SAndroid Build Coastguard Worker   return true;
1877*9880d681SAndroid Build Coastguard Worker }
1878*9880d681SAndroid Build Coastguard Worker 
1879*9880d681SAndroid Build Coastguard Worker /// Get the appropriate X86 opcode for an in-memory increment or decrement.
1880*9880d681SAndroid Build Coastguard Worker /// Opc should be X86ISD::DEC or X86ISD::INC.
getFusedLdStOpcode(EVT & LdVT,unsigned Opc)1881*9880d681SAndroid Build Coastguard Worker static unsigned getFusedLdStOpcode(EVT &LdVT, unsigned Opc) {
1882*9880d681SAndroid Build Coastguard Worker   if (Opc == X86ISD::DEC) {
1883*9880d681SAndroid Build Coastguard Worker     if (LdVT == MVT::i64) return X86::DEC64m;
1884*9880d681SAndroid Build Coastguard Worker     if (LdVT == MVT::i32) return X86::DEC32m;
1885*9880d681SAndroid Build Coastguard Worker     if (LdVT == MVT::i16) return X86::DEC16m;
1886*9880d681SAndroid Build Coastguard Worker     if (LdVT == MVT::i8)  return X86::DEC8m;
1887*9880d681SAndroid Build Coastguard Worker   } else {
1888*9880d681SAndroid Build Coastguard Worker     assert(Opc == X86ISD::INC && "unrecognized opcode");
1889*9880d681SAndroid Build Coastguard Worker     if (LdVT == MVT::i64) return X86::INC64m;
1890*9880d681SAndroid Build Coastguard Worker     if (LdVT == MVT::i32) return X86::INC32m;
1891*9880d681SAndroid Build Coastguard Worker     if (LdVT == MVT::i16) return X86::INC16m;
1892*9880d681SAndroid Build Coastguard Worker     if (LdVT == MVT::i8)  return X86::INC8m;
1893*9880d681SAndroid Build Coastguard Worker   }
1894*9880d681SAndroid Build Coastguard Worker   llvm_unreachable("unrecognized size for LdVT");
1895*9880d681SAndroid Build Coastguard Worker }
1896*9880d681SAndroid Build Coastguard Worker 
1897*9880d681SAndroid Build Coastguard Worker /// Customized ISel for GATHER operations.
tryGather(SDNode * Node,unsigned Opc)1898*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::tryGather(SDNode *Node, unsigned Opc) {
1899*9880d681SAndroid Build Coastguard Worker   // Operands of Gather: VSrc, Base, VIdx, VMask, Scale
1900*9880d681SAndroid Build Coastguard Worker   SDValue Chain = Node->getOperand(0);
1901*9880d681SAndroid Build Coastguard Worker   SDValue VSrc = Node->getOperand(2);
1902*9880d681SAndroid Build Coastguard Worker   SDValue Base = Node->getOperand(3);
1903*9880d681SAndroid Build Coastguard Worker   SDValue VIdx = Node->getOperand(4);
1904*9880d681SAndroid Build Coastguard Worker   SDValue VMask = Node->getOperand(5);
1905*9880d681SAndroid Build Coastguard Worker   ConstantSDNode *Scale = dyn_cast<ConstantSDNode>(Node->getOperand(6));
1906*9880d681SAndroid Build Coastguard Worker   if (!Scale)
1907*9880d681SAndroid Build Coastguard Worker     return false;
1908*9880d681SAndroid Build Coastguard Worker 
1909*9880d681SAndroid Build Coastguard Worker   SDVTList VTs = CurDAG->getVTList(VSrc.getValueType(), VSrc.getValueType(),
1910*9880d681SAndroid Build Coastguard Worker                                    MVT::Other);
1911*9880d681SAndroid Build Coastguard Worker 
1912*9880d681SAndroid Build Coastguard Worker   SDLoc DL(Node);
1913*9880d681SAndroid Build Coastguard Worker 
1914*9880d681SAndroid Build Coastguard Worker   // Memory Operands: Base, Scale, Index, Disp, Segment
1915*9880d681SAndroid Build Coastguard Worker   SDValue Disp = CurDAG->getTargetConstant(0, DL, MVT::i32);
1916*9880d681SAndroid Build Coastguard Worker   SDValue Segment = CurDAG->getRegister(0, MVT::i32);
1917*9880d681SAndroid Build Coastguard Worker   const SDValue Ops[] = { VSrc, Base, getI8Imm(Scale->getSExtValue(), DL), VIdx,
1918*9880d681SAndroid Build Coastguard Worker                           Disp, Segment, VMask, Chain};
1919*9880d681SAndroid Build Coastguard Worker   SDNode *ResNode = CurDAG->getMachineNode(Opc, DL, VTs, Ops);
1920*9880d681SAndroid Build Coastguard Worker   // Node has 2 outputs: VDst and MVT::Other.
1921*9880d681SAndroid Build Coastguard Worker   // ResNode has 3 outputs: VDst, VMask_wb, and MVT::Other.
1922*9880d681SAndroid Build Coastguard Worker   // We replace VDst of Node with VDst of ResNode, and Other of Node with Other
1923*9880d681SAndroid Build Coastguard Worker   // of ResNode.
1924*9880d681SAndroid Build Coastguard Worker   ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
1925*9880d681SAndroid Build Coastguard Worker   ReplaceUses(SDValue(Node, 1), SDValue(ResNode, 2));
1926*9880d681SAndroid Build Coastguard Worker   CurDAG->RemoveDeadNode(Node);
1927*9880d681SAndroid Build Coastguard Worker   return true;
1928*9880d681SAndroid Build Coastguard Worker }
1929*9880d681SAndroid Build Coastguard Worker 
Select(SDNode * Node)1930*9880d681SAndroid Build Coastguard Worker void X86DAGToDAGISel::Select(SDNode *Node) {
1931*9880d681SAndroid Build Coastguard Worker   MVT NVT = Node->getSimpleValueType(0);
1932*9880d681SAndroid Build Coastguard Worker   unsigned Opc, MOpc;
1933*9880d681SAndroid Build Coastguard Worker   unsigned Opcode = Node->getOpcode();
1934*9880d681SAndroid Build Coastguard Worker   SDLoc dl(Node);
1935*9880d681SAndroid Build Coastguard Worker 
1936*9880d681SAndroid Build Coastguard Worker   DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << '\n');
1937*9880d681SAndroid Build Coastguard Worker 
1938*9880d681SAndroid Build Coastguard Worker   if (Node->isMachineOpcode()) {
1939*9880d681SAndroid Build Coastguard Worker     DEBUG(dbgs() << "== ";  Node->dump(CurDAG); dbgs() << '\n');
1940*9880d681SAndroid Build Coastguard Worker     Node->setNodeId(-1);
1941*9880d681SAndroid Build Coastguard Worker     return;   // Already selected.
1942*9880d681SAndroid Build Coastguard Worker   }
1943*9880d681SAndroid Build Coastguard Worker 
1944*9880d681SAndroid Build Coastguard Worker   switch (Opcode) {
1945*9880d681SAndroid Build Coastguard Worker   default: break;
1946*9880d681SAndroid Build Coastguard Worker   case ISD::BRIND: {
1947*9880d681SAndroid Build Coastguard Worker     if (Subtarget->isTargetNaCl())
1948*9880d681SAndroid Build Coastguard Worker       // NaCl has its own pass where jmp %r32 are converted to jmp %r64. We
1949*9880d681SAndroid Build Coastguard Worker       // leave the instruction alone.
1950*9880d681SAndroid Build Coastguard Worker       break;
1951*9880d681SAndroid Build Coastguard Worker     if (Subtarget->isTarget64BitILP32()) {
1952*9880d681SAndroid Build Coastguard Worker       // Converts a 32-bit register to a 64-bit, zero-extended version of
1953*9880d681SAndroid Build Coastguard Worker       // it. This is needed because x86-64 can do many things, but jmp %r32
1954*9880d681SAndroid Build Coastguard Worker       // ain't one of them.
1955*9880d681SAndroid Build Coastguard Worker       const SDValue &Target = Node->getOperand(1);
1956*9880d681SAndroid Build Coastguard Worker       assert(Target.getSimpleValueType() == llvm::MVT::i32);
1957*9880d681SAndroid Build Coastguard Worker       SDValue ZextTarget = CurDAG->getZExtOrTrunc(Target, dl, EVT(MVT::i64));
1958*9880d681SAndroid Build Coastguard Worker       SDValue Brind = CurDAG->getNode(ISD::BRIND, dl, MVT::Other,
1959*9880d681SAndroid Build Coastguard Worker                                       Node->getOperand(0), ZextTarget);
1960*9880d681SAndroid Build Coastguard Worker       ReplaceNode(Node, Brind.getNode());
1961*9880d681SAndroid Build Coastguard Worker       SelectCode(ZextTarget.getNode());
1962*9880d681SAndroid Build Coastguard Worker       SelectCode(Brind.getNode());
1963*9880d681SAndroid Build Coastguard Worker       return;
1964*9880d681SAndroid Build Coastguard Worker     }
1965*9880d681SAndroid Build Coastguard Worker     break;
1966*9880d681SAndroid Build Coastguard Worker   }
1967*9880d681SAndroid Build Coastguard Worker   case ISD::INTRINSIC_W_CHAIN: {
1968*9880d681SAndroid Build Coastguard Worker     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
1969*9880d681SAndroid Build Coastguard Worker     switch (IntNo) {
1970*9880d681SAndroid Build Coastguard Worker     default: break;
1971*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_d_pd:
1972*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_d_pd_256:
1973*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_q_pd:
1974*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_q_pd_256:
1975*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_d_ps:
1976*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_d_ps_256:
1977*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_q_ps:
1978*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_q_ps_256:
1979*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_d_q:
1980*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_d_q_256:
1981*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_q_q:
1982*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_q_q_256:
1983*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_d_d:
1984*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_d_d_256:
1985*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_q_d:
1986*9880d681SAndroid Build Coastguard Worker     case Intrinsic::x86_avx2_gather_q_d_256: {
1987*9880d681SAndroid Build Coastguard Worker       if (!Subtarget->hasAVX2())
1988*9880d681SAndroid Build Coastguard Worker         break;
1989*9880d681SAndroid Build Coastguard Worker       unsigned Opc;
1990*9880d681SAndroid Build Coastguard Worker       switch (IntNo) {
1991*9880d681SAndroid Build Coastguard Worker       default: llvm_unreachable("Impossible intrinsic");
1992*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_d_pd:     Opc = X86::VGATHERDPDrm;  break;
1993*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_d_pd_256: Opc = X86::VGATHERDPDYrm; break;
1994*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_q_pd:     Opc = X86::VGATHERQPDrm;  break;
1995*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_q_pd_256: Opc = X86::VGATHERQPDYrm; break;
1996*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_d_ps:     Opc = X86::VGATHERDPSrm;  break;
1997*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_d_ps_256: Opc = X86::VGATHERDPSYrm; break;
1998*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_q_ps:     Opc = X86::VGATHERQPSrm;  break;
1999*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_q_ps_256: Opc = X86::VGATHERQPSYrm; break;
2000*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_d_q:      Opc = X86::VPGATHERDQrm;  break;
2001*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_d_q_256:  Opc = X86::VPGATHERDQYrm; break;
2002*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_q_q:      Opc = X86::VPGATHERQQrm;  break;
2003*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_q_q_256:  Opc = X86::VPGATHERQQYrm; break;
2004*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_d_d:      Opc = X86::VPGATHERDDrm;  break;
2005*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_d_d_256:  Opc = X86::VPGATHERDDYrm; break;
2006*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_q_d:      Opc = X86::VPGATHERQDrm;  break;
2007*9880d681SAndroid Build Coastguard Worker       case Intrinsic::x86_avx2_gather_q_d_256:  Opc = X86::VPGATHERQDYrm; break;
2008*9880d681SAndroid Build Coastguard Worker       }
2009*9880d681SAndroid Build Coastguard Worker       if (tryGather(Node, Opc))
2010*9880d681SAndroid Build Coastguard Worker         return;
2011*9880d681SAndroid Build Coastguard Worker       break;
2012*9880d681SAndroid Build Coastguard Worker     }
2013*9880d681SAndroid Build Coastguard Worker     }
2014*9880d681SAndroid Build Coastguard Worker     break;
2015*9880d681SAndroid Build Coastguard Worker   }
2016*9880d681SAndroid Build Coastguard Worker   case X86ISD::GlobalBaseReg:
2017*9880d681SAndroid Build Coastguard Worker     ReplaceNode(Node, getGlobalBaseReg());
2018*9880d681SAndroid Build Coastguard Worker     return;
2019*9880d681SAndroid Build Coastguard Worker 
2020*9880d681SAndroid Build Coastguard Worker   case X86ISD::SHRUNKBLEND: {
2021*9880d681SAndroid Build Coastguard Worker     // SHRUNKBLEND selects like a regular VSELECT.
2022*9880d681SAndroid Build Coastguard Worker     SDValue VSelect = CurDAG->getNode(
2023*9880d681SAndroid Build Coastguard Worker         ISD::VSELECT, SDLoc(Node), Node->getValueType(0), Node->getOperand(0),
2024*9880d681SAndroid Build Coastguard Worker         Node->getOperand(1), Node->getOperand(2));
2025*9880d681SAndroid Build Coastguard Worker     ReplaceUses(SDValue(Node, 0), VSelect);
2026*9880d681SAndroid Build Coastguard Worker     SelectCode(VSelect.getNode());
2027*9880d681SAndroid Build Coastguard Worker     // We already called ReplaceUses.
2028*9880d681SAndroid Build Coastguard Worker     return;
2029*9880d681SAndroid Build Coastguard Worker   }
2030*9880d681SAndroid Build Coastguard Worker 
2031*9880d681SAndroid Build Coastguard Worker   case ISD::AND:
2032*9880d681SAndroid Build Coastguard Worker   case ISD::OR:
2033*9880d681SAndroid Build Coastguard Worker   case ISD::XOR: {
2034*9880d681SAndroid Build Coastguard Worker     // For operations of the form (x << C1) op C2, check if we can use a smaller
2035*9880d681SAndroid Build Coastguard Worker     // encoding for C2 by transforming it into (x op (C2>>C1)) << C1.
2036*9880d681SAndroid Build Coastguard Worker     SDValue N0 = Node->getOperand(0);
2037*9880d681SAndroid Build Coastguard Worker     SDValue N1 = Node->getOperand(1);
2038*9880d681SAndroid Build Coastguard Worker 
2039*9880d681SAndroid Build Coastguard Worker     if (N0->getOpcode() != ISD::SHL || !N0->hasOneUse())
2040*9880d681SAndroid Build Coastguard Worker       break;
2041*9880d681SAndroid Build Coastguard Worker 
2042*9880d681SAndroid Build Coastguard Worker     // i8 is unshrinkable, i16 should be promoted to i32.
2043*9880d681SAndroid Build Coastguard Worker     if (NVT != MVT::i32 && NVT != MVT::i64)
2044*9880d681SAndroid Build Coastguard Worker       break;
2045*9880d681SAndroid Build Coastguard Worker 
2046*9880d681SAndroid Build Coastguard Worker     ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(N1);
2047*9880d681SAndroid Build Coastguard Worker     ConstantSDNode *ShlCst = dyn_cast<ConstantSDNode>(N0->getOperand(1));
2048*9880d681SAndroid Build Coastguard Worker     if (!Cst || !ShlCst)
2049*9880d681SAndroid Build Coastguard Worker       break;
2050*9880d681SAndroid Build Coastguard Worker 
2051*9880d681SAndroid Build Coastguard Worker     int64_t Val = Cst->getSExtValue();
2052*9880d681SAndroid Build Coastguard Worker     uint64_t ShlVal = ShlCst->getZExtValue();
2053*9880d681SAndroid Build Coastguard Worker 
2054*9880d681SAndroid Build Coastguard Worker     // Make sure that we don't change the operation by removing bits.
2055*9880d681SAndroid Build Coastguard Worker     // This only matters for OR and XOR, AND is unaffected.
2056*9880d681SAndroid Build Coastguard Worker     uint64_t RemovedBitsMask = (1ULL << ShlVal) - 1;
2057*9880d681SAndroid Build Coastguard Worker     if (Opcode != ISD::AND && (Val & RemovedBitsMask) != 0)
2058*9880d681SAndroid Build Coastguard Worker       break;
2059*9880d681SAndroid Build Coastguard Worker 
2060*9880d681SAndroid Build Coastguard Worker     unsigned ShlOp, AddOp, Op;
2061*9880d681SAndroid Build Coastguard Worker     MVT CstVT = NVT;
2062*9880d681SAndroid Build Coastguard Worker 
2063*9880d681SAndroid Build Coastguard Worker     // Check the minimum bitwidth for the new constant.
2064*9880d681SAndroid Build Coastguard Worker     // TODO: AND32ri is the same as AND64ri32 with zext imm.
2065*9880d681SAndroid Build Coastguard Worker     // TODO: MOV32ri+OR64r is cheaper than MOV64ri64+OR64rr
2066*9880d681SAndroid Build Coastguard Worker     // TODO: Using 16 and 8 bit operations is also possible for or32 & xor32.
2067*9880d681SAndroid Build Coastguard Worker     if (!isInt<8>(Val) && isInt<8>(Val >> ShlVal))
2068*9880d681SAndroid Build Coastguard Worker       CstVT = MVT::i8;
2069*9880d681SAndroid Build Coastguard Worker     else if (!isInt<32>(Val) && isInt<32>(Val >> ShlVal))
2070*9880d681SAndroid Build Coastguard Worker       CstVT = MVT::i32;
2071*9880d681SAndroid Build Coastguard Worker 
2072*9880d681SAndroid Build Coastguard Worker     // Bail if there is no smaller encoding.
2073*9880d681SAndroid Build Coastguard Worker     if (NVT == CstVT)
2074*9880d681SAndroid Build Coastguard Worker       break;
2075*9880d681SAndroid Build Coastguard Worker 
2076*9880d681SAndroid Build Coastguard Worker     switch (NVT.SimpleTy) {
2077*9880d681SAndroid Build Coastguard Worker     default: llvm_unreachable("Unsupported VT!");
2078*9880d681SAndroid Build Coastguard Worker     case MVT::i32:
2079*9880d681SAndroid Build Coastguard Worker       assert(CstVT == MVT::i8);
2080*9880d681SAndroid Build Coastguard Worker       ShlOp = X86::SHL32ri;
2081*9880d681SAndroid Build Coastguard Worker       AddOp = X86::ADD32rr;
2082*9880d681SAndroid Build Coastguard Worker 
2083*9880d681SAndroid Build Coastguard Worker       switch (Opcode) {
2084*9880d681SAndroid Build Coastguard Worker       default: llvm_unreachable("Impossible opcode");
2085*9880d681SAndroid Build Coastguard Worker       case ISD::AND: Op = X86::AND32ri8; break;
2086*9880d681SAndroid Build Coastguard Worker       case ISD::OR:  Op =  X86::OR32ri8; break;
2087*9880d681SAndroid Build Coastguard Worker       case ISD::XOR: Op = X86::XOR32ri8; break;
2088*9880d681SAndroid Build Coastguard Worker       }
2089*9880d681SAndroid Build Coastguard Worker       break;
2090*9880d681SAndroid Build Coastguard Worker     case MVT::i64:
2091*9880d681SAndroid Build Coastguard Worker       assert(CstVT == MVT::i8 || CstVT == MVT::i32);
2092*9880d681SAndroid Build Coastguard Worker       ShlOp = X86::SHL64ri;
2093*9880d681SAndroid Build Coastguard Worker       AddOp = X86::ADD64rr;
2094*9880d681SAndroid Build Coastguard Worker 
2095*9880d681SAndroid Build Coastguard Worker       switch (Opcode) {
2096*9880d681SAndroid Build Coastguard Worker       default: llvm_unreachable("Impossible opcode");
2097*9880d681SAndroid Build Coastguard Worker       case ISD::AND: Op = CstVT==MVT::i8? X86::AND64ri8 : X86::AND64ri32; break;
2098*9880d681SAndroid Build Coastguard Worker       case ISD::OR:  Op = CstVT==MVT::i8?  X86::OR64ri8 :  X86::OR64ri32; break;
2099*9880d681SAndroid Build Coastguard Worker       case ISD::XOR: Op = CstVT==MVT::i8? X86::XOR64ri8 : X86::XOR64ri32; break;
2100*9880d681SAndroid Build Coastguard Worker       }
2101*9880d681SAndroid Build Coastguard Worker       break;
2102*9880d681SAndroid Build Coastguard Worker     }
2103*9880d681SAndroid Build Coastguard Worker 
2104*9880d681SAndroid Build Coastguard Worker     // Emit the smaller op and the shift.
2105*9880d681SAndroid Build Coastguard Worker     SDValue NewCst = CurDAG->getTargetConstant(Val >> ShlVal, dl, CstVT);
2106*9880d681SAndroid Build Coastguard Worker     SDNode *New = CurDAG->getMachineNode(Op, dl, NVT, N0->getOperand(0),NewCst);
2107*9880d681SAndroid Build Coastguard Worker     if (ShlVal == 1)
2108*9880d681SAndroid Build Coastguard Worker       CurDAG->SelectNodeTo(Node, AddOp, NVT, SDValue(New, 0),
2109*9880d681SAndroid Build Coastguard Worker                            SDValue(New, 0));
2110*9880d681SAndroid Build Coastguard Worker     else
2111*9880d681SAndroid Build Coastguard Worker       CurDAG->SelectNodeTo(Node, ShlOp, NVT, SDValue(New, 0),
2112*9880d681SAndroid Build Coastguard Worker                            getI8Imm(ShlVal, dl));
2113*9880d681SAndroid Build Coastguard Worker     return;
2114*9880d681SAndroid Build Coastguard Worker   }
2115*9880d681SAndroid Build Coastguard Worker   case X86ISD::UMUL8:
2116*9880d681SAndroid Build Coastguard Worker   case X86ISD::SMUL8: {
2117*9880d681SAndroid Build Coastguard Worker     SDValue N0 = Node->getOperand(0);
2118*9880d681SAndroid Build Coastguard Worker     SDValue N1 = Node->getOperand(1);
2119*9880d681SAndroid Build Coastguard Worker 
2120*9880d681SAndroid Build Coastguard Worker     Opc = (Opcode == X86ISD::SMUL8 ? X86::IMUL8r : X86::MUL8r);
2121*9880d681SAndroid Build Coastguard Worker 
2122*9880d681SAndroid Build Coastguard Worker     SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, X86::AL,
2123*9880d681SAndroid Build Coastguard Worker                                           N0, SDValue()).getValue(1);
2124*9880d681SAndroid Build Coastguard Worker 
2125*9880d681SAndroid Build Coastguard Worker     SDVTList VTs = CurDAG->getVTList(NVT, MVT::i32);
2126*9880d681SAndroid Build Coastguard Worker     SDValue Ops[] = {N1, InFlag};
2127*9880d681SAndroid Build Coastguard Worker     SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops);
2128*9880d681SAndroid Build Coastguard Worker 
2129*9880d681SAndroid Build Coastguard Worker     ReplaceNode(Node, CNode);
2130*9880d681SAndroid Build Coastguard Worker     return;
2131*9880d681SAndroid Build Coastguard Worker   }
2132*9880d681SAndroid Build Coastguard Worker 
2133*9880d681SAndroid Build Coastguard Worker   case X86ISD::UMUL: {
2134*9880d681SAndroid Build Coastguard Worker     SDValue N0 = Node->getOperand(0);
2135*9880d681SAndroid Build Coastguard Worker     SDValue N1 = Node->getOperand(1);
2136*9880d681SAndroid Build Coastguard Worker 
2137*9880d681SAndroid Build Coastguard Worker     unsigned LoReg;
2138*9880d681SAndroid Build Coastguard Worker     switch (NVT.SimpleTy) {
2139*9880d681SAndroid Build Coastguard Worker     default: llvm_unreachable("Unsupported VT!");
2140*9880d681SAndroid Build Coastguard Worker     case MVT::i8:  LoReg = X86::AL;  Opc = X86::MUL8r; break;
2141*9880d681SAndroid Build Coastguard Worker     case MVT::i16: LoReg = X86::AX;  Opc = X86::MUL16r; break;
2142*9880d681SAndroid Build Coastguard Worker     case MVT::i32: LoReg = X86::EAX; Opc = X86::MUL32r; break;
2143*9880d681SAndroid Build Coastguard Worker     case MVT::i64: LoReg = X86::RAX; Opc = X86::MUL64r; break;
2144*9880d681SAndroid Build Coastguard Worker     }
2145*9880d681SAndroid Build Coastguard Worker 
2146*9880d681SAndroid Build Coastguard Worker     SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, LoReg,
2147*9880d681SAndroid Build Coastguard Worker                                           N0, SDValue()).getValue(1);
2148*9880d681SAndroid Build Coastguard Worker 
2149*9880d681SAndroid Build Coastguard Worker     SDVTList VTs = CurDAG->getVTList(NVT, NVT, MVT::i32);
2150*9880d681SAndroid Build Coastguard Worker     SDValue Ops[] = {N1, InFlag};
2151*9880d681SAndroid Build Coastguard Worker     SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops);
2152*9880d681SAndroid Build Coastguard Worker 
2153*9880d681SAndroid Build Coastguard Worker     ReplaceNode(Node, CNode);
2154*9880d681SAndroid Build Coastguard Worker     return;
2155*9880d681SAndroid Build Coastguard Worker   }
2156*9880d681SAndroid Build Coastguard Worker 
2157*9880d681SAndroid Build Coastguard Worker   case ISD::SMUL_LOHI:
2158*9880d681SAndroid Build Coastguard Worker   case ISD::UMUL_LOHI: {
2159*9880d681SAndroid Build Coastguard Worker     SDValue N0 = Node->getOperand(0);
2160*9880d681SAndroid Build Coastguard Worker     SDValue N1 = Node->getOperand(1);
2161*9880d681SAndroid Build Coastguard Worker 
2162*9880d681SAndroid Build Coastguard Worker     bool isSigned = Opcode == ISD::SMUL_LOHI;
2163*9880d681SAndroid Build Coastguard Worker     bool hasBMI2 = Subtarget->hasBMI2();
2164*9880d681SAndroid Build Coastguard Worker     if (!isSigned) {
2165*9880d681SAndroid Build Coastguard Worker       switch (NVT.SimpleTy) {
2166*9880d681SAndroid Build Coastguard Worker       default: llvm_unreachable("Unsupported VT!");
2167*9880d681SAndroid Build Coastguard Worker       case MVT::i8:  Opc = X86::MUL8r;  MOpc = X86::MUL8m;  break;
2168*9880d681SAndroid Build Coastguard Worker       case MVT::i16: Opc = X86::MUL16r; MOpc = X86::MUL16m; break;
2169*9880d681SAndroid Build Coastguard Worker       case MVT::i32: Opc = hasBMI2 ? X86::MULX32rr : X86::MUL32r;
2170*9880d681SAndroid Build Coastguard Worker                      MOpc = hasBMI2 ? X86::MULX32rm : X86::MUL32m; break;
2171*9880d681SAndroid Build Coastguard Worker       case MVT::i64: Opc = hasBMI2 ? X86::MULX64rr : X86::MUL64r;
2172*9880d681SAndroid Build Coastguard Worker                      MOpc = hasBMI2 ? X86::MULX64rm : X86::MUL64m; break;
2173*9880d681SAndroid Build Coastguard Worker       }
2174*9880d681SAndroid Build Coastguard Worker     } else {
2175*9880d681SAndroid Build Coastguard Worker       switch (NVT.SimpleTy) {
2176*9880d681SAndroid Build Coastguard Worker       default: llvm_unreachable("Unsupported VT!");
2177*9880d681SAndroid Build Coastguard Worker       case MVT::i8:  Opc = X86::IMUL8r;  MOpc = X86::IMUL8m;  break;
2178*9880d681SAndroid Build Coastguard Worker       case MVT::i16: Opc = X86::IMUL16r; MOpc = X86::IMUL16m; break;
2179*9880d681SAndroid Build Coastguard Worker       case MVT::i32: Opc = X86::IMUL32r; MOpc = X86::IMUL32m; break;
2180*9880d681SAndroid Build Coastguard Worker       case MVT::i64: Opc = X86::IMUL64r; MOpc = X86::IMUL64m; break;
2181*9880d681SAndroid Build Coastguard Worker       }
2182*9880d681SAndroid Build Coastguard Worker     }
2183*9880d681SAndroid Build Coastguard Worker 
2184*9880d681SAndroid Build Coastguard Worker     unsigned SrcReg, LoReg, HiReg;
2185*9880d681SAndroid Build Coastguard Worker     switch (Opc) {
2186*9880d681SAndroid Build Coastguard Worker     default: llvm_unreachable("Unknown MUL opcode!");
2187*9880d681SAndroid Build Coastguard Worker     case X86::IMUL8r:
2188*9880d681SAndroid Build Coastguard Worker     case X86::MUL8r:
2189*9880d681SAndroid Build Coastguard Worker       SrcReg = LoReg = X86::AL; HiReg = X86::AH;
2190*9880d681SAndroid Build Coastguard Worker       break;
2191*9880d681SAndroid Build Coastguard Worker     case X86::IMUL16r:
2192*9880d681SAndroid Build Coastguard Worker     case X86::MUL16r:
2193*9880d681SAndroid Build Coastguard Worker       SrcReg = LoReg = X86::AX; HiReg = X86::DX;
2194*9880d681SAndroid Build Coastguard Worker       break;
2195*9880d681SAndroid Build Coastguard Worker     case X86::IMUL32r:
2196*9880d681SAndroid Build Coastguard Worker     case X86::MUL32r:
2197*9880d681SAndroid Build Coastguard Worker       SrcReg = LoReg = X86::EAX; HiReg = X86::EDX;
2198*9880d681SAndroid Build Coastguard Worker       break;
2199*9880d681SAndroid Build Coastguard Worker     case X86::IMUL64r:
2200*9880d681SAndroid Build Coastguard Worker     case X86::MUL64r:
2201*9880d681SAndroid Build Coastguard Worker       SrcReg = LoReg = X86::RAX; HiReg = X86::RDX;
2202*9880d681SAndroid Build Coastguard Worker       break;
2203*9880d681SAndroid Build Coastguard Worker     case X86::MULX32rr:
2204*9880d681SAndroid Build Coastguard Worker       SrcReg = X86::EDX; LoReg = HiReg = 0;
2205*9880d681SAndroid Build Coastguard Worker       break;
2206*9880d681SAndroid Build Coastguard Worker     case X86::MULX64rr:
2207*9880d681SAndroid Build Coastguard Worker       SrcReg = X86::RDX; LoReg = HiReg = 0;
2208*9880d681SAndroid Build Coastguard Worker       break;
2209*9880d681SAndroid Build Coastguard Worker     }
2210*9880d681SAndroid Build Coastguard Worker 
2211*9880d681SAndroid Build Coastguard Worker     SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
2212*9880d681SAndroid Build Coastguard Worker     bool foldedLoad = tryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
2213*9880d681SAndroid Build Coastguard Worker     // Multiply is commmutative.
2214*9880d681SAndroid Build Coastguard Worker     if (!foldedLoad) {
2215*9880d681SAndroid Build Coastguard Worker       foldedLoad = tryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
2216*9880d681SAndroid Build Coastguard Worker       if (foldedLoad)
2217*9880d681SAndroid Build Coastguard Worker         std::swap(N0, N1);
2218*9880d681SAndroid Build Coastguard Worker     }
2219*9880d681SAndroid Build Coastguard Worker 
2220*9880d681SAndroid Build Coastguard Worker     SDValue InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, SrcReg,
2221*9880d681SAndroid Build Coastguard Worker                                           N0, SDValue()).getValue(1);
2222*9880d681SAndroid Build Coastguard Worker     SDValue ResHi, ResLo;
2223*9880d681SAndroid Build Coastguard Worker 
2224*9880d681SAndroid Build Coastguard Worker     if (foldedLoad) {
2225*9880d681SAndroid Build Coastguard Worker       SDValue Chain;
2226*9880d681SAndroid Build Coastguard Worker       MachineSDNode *CNode = nullptr;
2227*9880d681SAndroid Build Coastguard Worker       SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
2228*9880d681SAndroid Build Coastguard Worker                         InFlag };
2229*9880d681SAndroid Build Coastguard Worker       if (MOpc == X86::MULX32rm || MOpc == X86::MULX64rm) {
2230*9880d681SAndroid Build Coastguard Worker         SDVTList VTs = CurDAG->getVTList(NVT, NVT, MVT::Other, MVT::Glue);
2231*9880d681SAndroid Build Coastguard Worker         CNode = CurDAG->getMachineNode(MOpc, dl, VTs, Ops);
2232*9880d681SAndroid Build Coastguard Worker         ResHi = SDValue(CNode, 0);
2233*9880d681SAndroid Build Coastguard Worker         ResLo = SDValue(CNode, 1);
2234*9880d681SAndroid Build Coastguard Worker         Chain = SDValue(CNode, 2);
2235*9880d681SAndroid Build Coastguard Worker         InFlag = SDValue(CNode, 3);
2236*9880d681SAndroid Build Coastguard Worker       } else {
2237*9880d681SAndroid Build Coastguard Worker         SDVTList VTs = CurDAG->getVTList(MVT::Other, MVT::Glue);
2238*9880d681SAndroid Build Coastguard Worker         CNode = CurDAG->getMachineNode(MOpc, dl, VTs, Ops);
2239*9880d681SAndroid Build Coastguard Worker         Chain = SDValue(CNode, 0);
2240*9880d681SAndroid Build Coastguard Worker         InFlag = SDValue(CNode, 1);
2241*9880d681SAndroid Build Coastguard Worker       }
2242*9880d681SAndroid Build Coastguard Worker 
2243*9880d681SAndroid Build Coastguard Worker       // Update the chain.
2244*9880d681SAndroid Build Coastguard Worker       ReplaceUses(N1.getValue(1), Chain);
2245*9880d681SAndroid Build Coastguard Worker       // Record the mem-refs
2246*9880d681SAndroid Build Coastguard Worker       LoadSDNode *LoadNode = cast<LoadSDNode>(N1);
2247*9880d681SAndroid Build Coastguard Worker       if (LoadNode) {
2248*9880d681SAndroid Build Coastguard Worker         MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2249*9880d681SAndroid Build Coastguard Worker         MemOp[0] = LoadNode->getMemOperand();
2250*9880d681SAndroid Build Coastguard Worker         CNode->setMemRefs(MemOp, MemOp + 1);
2251*9880d681SAndroid Build Coastguard Worker       }
2252*9880d681SAndroid Build Coastguard Worker     } else {
2253*9880d681SAndroid Build Coastguard Worker       SDValue Ops[] = { N1, InFlag };
2254*9880d681SAndroid Build Coastguard Worker       if (Opc == X86::MULX32rr || Opc == X86::MULX64rr) {
2255*9880d681SAndroid Build Coastguard Worker         SDVTList VTs = CurDAG->getVTList(NVT, NVT, MVT::Glue);
2256*9880d681SAndroid Build Coastguard Worker         SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops);
2257*9880d681SAndroid Build Coastguard Worker         ResHi = SDValue(CNode, 0);
2258*9880d681SAndroid Build Coastguard Worker         ResLo = SDValue(CNode, 1);
2259*9880d681SAndroid Build Coastguard Worker         InFlag = SDValue(CNode, 2);
2260*9880d681SAndroid Build Coastguard Worker       } else {
2261*9880d681SAndroid Build Coastguard Worker         SDVTList VTs = CurDAG->getVTList(MVT::Glue);
2262*9880d681SAndroid Build Coastguard Worker         SDNode *CNode = CurDAG->getMachineNode(Opc, dl, VTs, Ops);
2263*9880d681SAndroid Build Coastguard Worker         InFlag = SDValue(CNode, 0);
2264*9880d681SAndroid Build Coastguard Worker       }
2265*9880d681SAndroid Build Coastguard Worker     }
2266*9880d681SAndroid Build Coastguard Worker 
2267*9880d681SAndroid Build Coastguard Worker     // Prevent use of AH in a REX instruction by referencing AX instead.
2268*9880d681SAndroid Build Coastguard Worker     if (HiReg == X86::AH && Subtarget->is64Bit() &&
2269*9880d681SAndroid Build Coastguard Worker         !SDValue(Node, 1).use_empty()) {
2270*9880d681SAndroid Build Coastguard Worker       SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2271*9880d681SAndroid Build Coastguard Worker                                               X86::AX, MVT::i16, InFlag);
2272*9880d681SAndroid Build Coastguard Worker       InFlag = Result.getValue(2);
2273*9880d681SAndroid Build Coastguard Worker       // Get the low part if needed. Don't use getCopyFromReg for aliasing
2274*9880d681SAndroid Build Coastguard Worker       // registers.
2275*9880d681SAndroid Build Coastguard Worker       if (!SDValue(Node, 0).use_empty())
2276*9880d681SAndroid Build Coastguard Worker         ReplaceUses(SDValue(Node, 1),
2277*9880d681SAndroid Build Coastguard Worker           CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result));
2278*9880d681SAndroid Build Coastguard Worker 
2279*9880d681SAndroid Build Coastguard Worker       // Shift AX down 8 bits.
2280*9880d681SAndroid Build Coastguard Worker       Result = SDValue(CurDAG->getMachineNode(X86::SHR16ri, dl, MVT::i16,
2281*9880d681SAndroid Build Coastguard Worker                                               Result,
2282*9880d681SAndroid Build Coastguard Worker                                      CurDAG->getTargetConstant(8, dl, MVT::i8)),
2283*9880d681SAndroid Build Coastguard Worker                        0);
2284*9880d681SAndroid Build Coastguard Worker       // Then truncate it down to i8.
2285*9880d681SAndroid Build Coastguard Worker       ReplaceUses(SDValue(Node, 1),
2286*9880d681SAndroid Build Coastguard Worker         CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result));
2287*9880d681SAndroid Build Coastguard Worker     }
2288*9880d681SAndroid Build Coastguard Worker     // Copy the low half of the result, if it is needed.
2289*9880d681SAndroid Build Coastguard Worker     if (!SDValue(Node, 0).use_empty()) {
2290*9880d681SAndroid Build Coastguard Worker       if (!ResLo.getNode()) {
2291*9880d681SAndroid Build Coastguard Worker         assert(LoReg && "Register for low half is not defined!");
2292*9880d681SAndroid Build Coastguard Worker         ResLo = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, LoReg, NVT,
2293*9880d681SAndroid Build Coastguard Worker                                        InFlag);
2294*9880d681SAndroid Build Coastguard Worker         InFlag = ResLo.getValue(2);
2295*9880d681SAndroid Build Coastguard Worker       }
2296*9880d681SAndroid Build Coastguard Worker       ReplaceUses(SDValue(Node, 0), ResLo);
2297*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "=> "; ResLo.getNode()->dump(CurDAG); dbgs() << '\n');
2298*9880d681SAndroid Build Coastguard Worker     }
2299*9880d681SAndroid Build Coastguard Worker     // Copy the high half of the result, if it is needed.
2300*9880d681SAndroid Build Coastguard Worker     if (!SDValue(Node, 1).use_empty()) {
2301*9880d681SAndroid Build Coastguard Worker       if (!ResHi.getNode()) {
2302*9880d681SAndroid Build Coastguard Worker         assert(HiReg && "Register for high half is not defined!");
2303*9880d681SAndroid Build Coastguard Worker         ResHi = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl, HiReg, NVT,
2304*9880d681SAndroid Build Coastguard Worker                                        InFlag);
2305*9880d681SAndroid Build Coastguard Worker         InFlag = ResHi.getValue(2);
2306*9880d681SAndroid Build Coastguard Worker       }
2307*9880d681SAndroid Build Coastguard Worker       ReplaceUses(SDValue(Node, 1), ResHi);
2308*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "=> "; ResHi.getNode()->dump(CurDAG); dbgs() << '\n');
2309*9880d681SAndroid Build Coastguard Worker     }
2310*9880d681SAndroid Build Coastguard Worker 
2311*9880d681SAndroid Build Coastguard Worker     return;
2312*9880d681SAndroid Build Coastguard Worker   }
2313*9880d681SAndroid Build Coastguard Worker 
2314*9880d681SAndroid Build Coastguard Worker   case ISD::SDIVREM:
2315*9880d681SAndroid Build Coastguard Worker   case ISD::UDIVREM:
2316*9880d681SAndroid Build Coastguard Worker   case X86ISD::SDIVREM8_SEXT_HREG:
2317*9880d681SAndroid Build Coastguard Worker   case X86ISD::UDIVREM8_ZEXT_HREG: {
2318*9880d681SAndroid Build Coastguard Worker     SDValue N0 = Node->getOperand(0);
2319*9880d681SAndroid Build Coastguard Worker     SDValue N1 = Node->getOperand(1);
2320*9880d681SAndroid Build Coastguard Worker 
2321*9880d681SAndroid Build Coastguard Worker     bool isSigned = (Opcode == ISD::SDIVREM ||
2322*9880d681SAndroid Build Coastguard Worker                      Opcode == X86ISD::SDIVREM8_SEXT_HREG);
2323*9880d681SAndroid Build Coastguard Worker     if (!isSigned) {
2324*9880d681SAndroid Build Coastguard Worker       switch (NVT.SimpleTy) {
2325*9880d681SAndroid Build Coastguard Worker       default: llvm_unreachable("Unsupported VT!");
2326*9880d681SAndroid Build Coastguard Worker       case MVT::i8:  Opc = X86::DIV8r;  MOpc = X86::DIV8m;  break;
2327*9880d681SAndroid Build Coastguard Worker       case MVT::i16: Opc = X86::DIV16r; MOpc = X86::DIV16m; break;
2328*9880d681SAndroid Build Coastguard Worker       case MVT::i32: Opc = X86::DIV32r; MOpc = X86::DIV32m; break;
2329*9880d681SAndroid Build Coastguard Worker       case MVT::i64: Opc = X86::DIV64r; MOpc = X86::DIV64m; break;
2330*9880d681SAndroid Build Coastguard Worker       }
2331*9880d681SAndroid Build Coastguard Worker     } else {
2332*9880d681SAndroid Build Coastguard Worker       switch (NVT.SimpleTy) {
2333*9880d681SAndroid Build Coastguard Worker       default: llvm_unreachable("Unsupported VT!");
2334*9880d681SAndroid Build Coastguard Worker       case MVT::i8:  Opc = X86::IDIV8r;  MOpc = X86::IDIV8m;  break;
2335*9880d681SAndroid Build Coastguard Worker       case MVT::i16: Opc = X86::IDIV16r; MOpc = X86::IDIV16m; break;
2336*9880d681SAndroid Build Coastguard Worker       case MVT::i32: Opc = X86::IDIV32r; MOpc = X86::IDIV32m; break;
2337*9880d681SAndroid Build Coastguard Worker       case MVT::i64: Opc = X86::IDIV64r; MOpc = X86::IDIV64m; break;
2338*9880d681SAndroid Build Coastguard Worker       }
2339*9880d681SAndroid Build Coastguard Worker     }
2340*9880d681SAndroid Build Coastguard Worker 
2341*9880d681SAndroid Build Coastguard Worker     unsigned LoReg, HiReg, ClrReg;
2342*9880d681SAndroid Build Coastguard Worker     unsigned SExtOpcode;
2343*9880d681SAndroid Build Coastguard Worker     switch (NVT.SimpleTy) {
2344*9880d681SAndroid Build Coastguard Worker     default: llvm_unreachable("Unsupported VT!");
2345*9880d681SAndroid Build Coastguard Worker     case MVT::i8:
2346*9880d681SAndroid Build Coastguard Worker       LoReg = X86::AL;  ClrReg = HiReg = X86::AH;
2347*9880d681SAndroid Build Coastguard Worker       SExtOpcode = X86::CBW;
2348*9880d681SAndroid Build Coastguard Worker       break;
2349*9880d681SAndroid Build Coastguard Worker     case MVT::i16:
2350*9880d681SAndroid Build Coastguard Worker       LoReg = X86::AX;  HiReg = X86::DX;
2351*9880d681SAndroid Build Coastguard Worker       ClrReg = X86::DX;
2352*9880d681SAndroid Build Coastguard Worker       SExtOpcode = X86::CWD;
2353*9880d681SAndroid Build Coastguard Worker       break;
2354*9880d681SAndroid Build Coastguard Worker     case MVT::i32:
2355*9880d681SAndroid Build Coastguard Worker       LoReg = X86::EAX; ClrReg = HiReg = X86::EDX;
2356*9880d681SAndroid Build Coastguard Worker       SExtOpcode = X86::CDQ;
2357*9880d681SAndroid Build Coastguard Worker       break;
2358*9880d681SAndroid Build Coastguard Worker     case MVT::i64:
2359*9880d681SAndroid Build Coastguard Worker       LoReg = X86::RAX; ClrReg = HiReg = X86::RDX;
2360*9880d681SAndroid Build Coastguard Worker       SExtOpcode = X86::CQO;
2361*9880d681SAndroid Build Coastguard Worker       break;
2362*9880d681SAndroid Build Coastguard Worker     }
2363*9880d681SAndroid Build Coastguard Worker 
2364*9880d681SAndroid Build Coastguard Worker     SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4;
2365*9880d681SAndroid Build Coastguard Worker     bool foldedLoad = tryFoldLoad(Node, N1, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4);
2366*9880d681SAndroid Build Coastguard Worker     bool signBitIsZero = CurDAG->SignBitIsZero(N0);
2367*9880d681SAndroid Build Coastguard Worker 
2368*9880d681SAndroid Build Coastguard Worker     SDValue InFlag;
2369*9880d681SAndroid Build Coastguard Worker     if (NVT == MVT::i8 && (!isSigned || signBitIsZero)) {
2370*9880d681SAndroid Build Coastguard Worker       // Special case for div8, just use a move with zero extension to AX to
2371*9880d681SAndroid Build Coastguard Worker       // clear the upper 8 bits (AH).
2372*9880d681SAndroid Build Coastguard Worker       SDValue Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, Move, Chain;
2373*9880d681SAndroid Build Coastguard Worker       if (tryFoldLoad(Node, N0, Tmp0, Tmp1, Tmp2, Tmp3, Tmp4)) {
2374*9880d681SAndroid Build Coastguard Worker         SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N0.getOperand(0) };
2375*9880d681SAndroid Build Coastguard Worker         Move =
2376*9880d681SAndroid Build Coastguard Worker           SDValue(CurDAG->getMachineNode(X86::MOVZX32rm8, dl, MVT::i32,
2377*9880d681SAndroid Build Coastguard Worker                                          MVT::Other, Ops), 0);
2378*9880d681SAndroid Build Coastguard Worker         Chain = Move.getValue(1);
2379*9880d681SAndroid Build Coastguard Worker         ReplaceUses(N0.getValue(1), Chain);
2380*9880d681SAndroid Build Coastguard Worker       } else {
2381*9880d681SAndroid Build Coastguard Worker         Move =
2382*9880d681SAndroid Build Coastguard Worker           SDValue(CurDAG->getMachineNode(X86::MOVZX32rr8, dl, MVT::i32, N0),0);
2383*9880d681SAndroid Build Coastguard Worker         Chain = CurDAG->getEntryNode();
2384*9880d681SAndroid Build Coastguard Worker       }
2385*9880d681SAndroid Build Coastguard Worker       Chain  = CurDAG->getCopyToReg(Chain, dl, X86::EAX, Move, SDValue());
2386*9880d681SAndroid Build Coastguard Worker       InFlag = Chain.getValue(1);
2387*9880d681SAndroid Build Coastguard Worker     } else {
2388*9880d681SAndroid Build Coastguard Worker       InFlag =
2389*9880d681SAndroid Build Coastguard Worker         CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl,
2390*9880d681SAndroid Build Coastguard Worker                              LoReg, N0, SDValue()).getValue(1);
2391*9880d681SAndroid Build Coastguard Worker       if (isSigned && !signBitIsZero) {
2392*9880d681SAndroid Build Coastguard Worker         // Sign extend the low part into the high part.
2393*9880d681SAndroid Build Coastguard Worker         InFlag =
2394*9880d681SAndroid Build Coastguard Worker           SDValue(CurDAG->getMachineNode(SExtOpcode, dl, MVT::Glue, InFlag),0);
2395*9880d681SAndroid Build Coastguard Worker       } else {
2396*9880d681SAndroid Build Coastguard Worker         // Zero out the high part, effectively zero extending the input.
2397*9880d681SAndroid Build Coastguard Worker         SDValue ClrNode = SDValue(CurDAG->getMachineNode(X86::MOV32r0, dl, NVT), 0);
2398*9880d681SAndroid Build Coastguard Worker         switch (NVT.SimpleTy) {
2399*9880d681SAndroid Build Coastguard Worker         case MVT::i16:
2400*9880d681SAndroid Build Coastguard Worker           ClrNode =
2401*9880d681SAndroid Build Coastguard Worker               SDValue(CurDAG->getMachineNode(
2402*9880d681SAndroid Build Coastguard Worker                           TargetOpcode::EXTRACT_SUBREG, dl, MVT::i16, ClrNode,
2403*9880d681SAndroid Build Coastguard Worker                           CurDAG->getTargetConstant(X86::sub_16bit, dl,
2404*9880d681SAndroid Build Coastguard Worker                                                     MVT::i32)),
2405*9880d681SAndroid Build Coastguard Worker                       0);
2406*9880d681SAndroid Build Coastguard Worker           break;
2407*9880d681SAndroid Build Coastguard Worker         case MVT::i32:
2408*9880d681SAndroid Build Coastguard Worker           break;
2409*9880d681SAndroid Build Coastguard Worker         case MVT::i64:
2410*9880d681SAndroid Build Coastguard Worker           ClrNode =
2411*9880d681SAndroid Build Coastguard Worker               SDValue(CurDAG->getMachineNode(
2412*9880d681SAndroid Build Coastguard Worker                           TargetOpcode::SUBREG_TO_REG, dl, MVT::i64,
2413*9880d681SAndroid Build Coastguard Worker                           CurDAG->getTargetConstant(0, dl, MVT::i64), ClrNode,
2414*9880d681SAndroid Build Coastguard Worker                           CurDAG->getTargetConstant(X86::sub_32bit, dl,
2415*9880d681SAndroid Build Coastguard Worker                                                     MVT::i32)),
2416*9880d681SAndroid Build Coastguard Worker                       0);
2417*9880d681SAndroid Build Coastguard Worker           break;
2418*9880d681SAndroid Build Coastguard Worker         default:
2419*9880d681SAndroid Build Coastguard Worker           llvm_unreachable("Unexpected division source");
2420*9880d681SAndroid Build Coastguard Worker         }
2421*9880d681SAndroid Build Coastguard Worker 
2422*9880d681SAndroid Build Coastguard Worker         InFlag = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, ClrReg,
2423*9880d681SAndroid Build Coastguard Worker                                       ClrNode, InFlag).getValue(1);
2424*9880d681SAndroid Build Coastguard Worker       }
2425*9880d681SAndroid Build Coastguard Worker     }
2426*9880d681SAndroid Build Coastguard Worker 
2427*9880d681SAndroid Build Coastguard Worker     if (foldedLoad) {
2428*9880d681SAndroid Build Coastguard Worker       SDValue Ops[] = { Tmp0, Tmp1, Tmp2, Tmp3, Tmp4, N1.getOperand(0),
2429*9880d681SAndroid Build Coastguard Worker                         InFlag };
2430*9880d681SAndroid Build Coastguard Worker       SDNode *CNode =
2431*9880d681SAndroid Build Coastguard Worker         CurDAG->getMachineNode(MOpc, dl, MVT::Other, MVT::Glue, Ops);
2432*9880d681SAndroid Build Coastguard Worker       InFlag = SDValue(CNode, 1);
2433*9880d681SAndroid Build Coastguard Worker       // Update the chain.
2434*9880d681SAndroid Build Coastguard Worker       ReplaceUses(N1.getValue(1), SDValue(CNode, 0));
2435*9880d681SAndroid Build Coastguard Worker     } else {
2436*9880d681SAndroid Build Coastguard Worker       InFlag =
2437*9880d681SAndroid Build Coastguard Worker         SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, N1, InFlag), 0);
2438*9880d681SAndroid Build Coastguard Worker     }
2439*9880d681SAndroid Build Coastguard Worker 
2440*9880d681SAndroid Build Coastguard Worker     // Prevent use of AH in a REX instruction by explicitly copying it to
2441*9880d681SAndroid Build Coastguard Worker     // an ABCD_L register.
2442*9880d681SAndroid Build Coastguard Worker     //
2443*9880d681SAndroid Build Coastguard Worker     // The current assumption of the register allocator is that isel
2444*9880d681SAndroid Build Coastguard Worker     // won't generate explicit references to the GR8_ABCD_H registers. If
2445*9880d681SAndroid Build Coastguard Worker     // the allocator and/or the backend get enhanced to be more robust in
2446*9880d681SAndroid Build Coastguard Worker     // that regard, this can be, and should be, removed.
2447*9880d681SAndroid Build Coastguard Worker     if (HiReg == X86::AH && !SDValue(Node, 1).use_empty()) {
2448*9880d681SAndroid Build Coastguard Worker       SDValue AHCopy = CurDAG->getRegister(X86::AH, MVT::i8);
2449*9880d681SAndroid Build Coastguard Worker       unsigned AHExtOpcode =
2450*9880d681SAndroid Build Coastguard Worker           isSigned ? X86::MOVSX32_NOREXrr8 : X86::MOVZX32_NOREXrr8;
2451*9880d681SAndroid Build Coastguard Worker 
2452*9880d681SAndroid Build Coastguard Worker       SDNode *RNode = CurDAG->getMachineNode(AHExtOpcode, dl, MVT::i32,
2453*9880d681SAndroid Build Coastguard Worker                                              MVT::Glue, AHCopy, InFlag);
2454*9880d681SAndroid Build Coastguard Worker       SDValue Result(RNode, 0);
2455*9880d681SAndroid Build Coastguard Worker       InFlag = SDValue(RNode, 1);
2456*9880d681SAndroid Build Coastguard Worker 
2457*9880d681SAndroid Build Coastguard Worker       if (Opcode == X86ISD::UDIVREM8_ZEXT_HREG ||
2458*9880d681SAndroid Build Coastguard Worker           Opcode == X86ISD::SDIVREM8_SEXT_HREG) {
2459*9880d681SAndroid Build Coastguard Worker         if (Node->getValueType(1) == MVT::i64) {
2460*9880d681SAndroid Build Coastguard Worker           // It's not possible to directly movsx AH to a 64bit register, because
2461*9880d681SAndroid Build Coastguard Worker           // the latter needs the REX prefix, but the former can't have it.
2462*9880d681SAndroid Build Coastguard Worker           assert(Opcode != X86ISD::SDIVREM8_SEXT_HREG &&
2463*9880d681SAndroid Build Coastguard Worker                  "Unexpected i64 sext of h-register");
2464*9880d681SAndroid Build Coastguard Worker           Result =
2465*9880d681SAndroid Build Coastguard Worker               SDValue(CurDAG->getMachineNode(
2466*9880d681SAndroid Build Coastguard Worker                           TargetOpcode::SUBREG_TO_REG, dl, MVT::i64,
2467*9880d681SAndroid Build Coastguard Worker                           CurDAG->getTargetConstant(0, dl, MVT::i64), Result,
2468*9880d681SAndroid Build Coastguard Worker                           CurDAG->getTargetConstant(X86::sub_32bit, dl,
2469*9880d681SAndroid Build Coastguard Worker                                                     MVT::i32)),
2470*9880d681SAndroid Build Coastguard Worker                       0);
2471*9880d681SAndroid Build Coastguard Worker         }
2472*9880d681SAndroid Build Coastguard Worker       } else {
2473*9880d681SAndroid Build Coastguard Worker         Result =
2474*9880d681SAndroid Build Coastguard Worker             CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl, MVT::i8, Result);
2475*9880d681SAndroid Build Coastguard Worker       }
2476*9880d681SAndroid Build Coastguard Worker       ReplaceUses(SDValue(Node, 1), Result);
2477*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
2478*9880d681SAndroid Build Coastguard Worker     }
2479*9880d681SAndroid Build Coastguard Worker     // Copy the division (low) result, if it is needed.
2480*9880d681SAndroid Build Coastguard Worker     if (!SDValue(Node, 0).use_empty()) {
2481*9880d681SAndroid Build Coastguard Worker       SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2482*9880d681SAndroid Build Coastguard Worker                                                 LoReg, NVT, InFlag);
2483*9880d681SAndroid Build Coastguard Worker       InFlag = Result.getValue(2);
2484*9880d681SAndroid Build Coastguard Worker       ReplaceUses(SDValue(Node, 0), Result);
2485*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
2486*9880d681SAndroid Build Coastguard Worker     }
2487*9880d681SAndroid Build Coastguard Worker     // Copy the remainder (high) result, if it is needed.
2488*9880d681SAndroid Build Coastguard Worker     if (!SDValue(Node, 1).use_empty()) {
2489*9880d681SAndroid Build Coastguard Worker       SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), dl,
2490*9880d681SAndroid Build Coastguard Worker                                               HiReg, NVT, InFlag);
2491*9880d681SAndroid Build Coastguard Worker       InFlag = Result.getValue(2);
2492*9880d681SAndroid Build Coastguard Worker       ReplaceUses(SDValue(Node, 1), Result);
2493*9880d681SAndroid Build Coastguard Worker       DEBUG(dbgs() << "=> "; Result.getNode()->dump(CurDAG); dbgs() << '\n');
2494*9880d681SAndroid Build Coastguard Worker     }
2495*9880d681SAndroid Build Coastguard Worker     return;
2496*9880d681SAndroid Build Coastguard Worker   }
2497*9880d681SAndroid Build Coastguard Worker 
2498*9880d681SAndroid Build Coastguard Worker   case X86ISD::CMP:
2499*9880d681SAndroid Build Coastguard Worker   case X86ISD::SUB: {
2500*9880d681SAndroid Build Coastguard Worker     // Sometimes a SUB is used to perform comparison.
2501*9880d681SAndroid Build Coastguard Worker     if (Opcode == X86ISD::SUB && Node->hasAnyUseOfValue(0))
2502*9880d681SAndroid Build Coastguard Worker       // This node is not a CMP.
2503*9880d681SAndroid Build Coastguard Worker       break;
2504*9880d681SAndroid Build Coastguard Worker     SDValue N0 = Node->getOperand(0);
2505*9880d681SAndroid Build Coastguard Worker     SDValue N1 = Node->getOperand(1);
2506*9880d681SAndroid Build Coastguard Worker 
2507*9880d681SAndroid Build Coastguard Worker     if (N0.getOpcode() == ISD::TRUNCATE && N0.hasOneUse() &&
2508*9880d681SAndroid Build Coastguard Worker         hasNoSignedComparisonUses(Node))
2509*9880d681SAndroid Build Coastguard Worker       N0 = N0.getOperand(0);
2510*9880d681SAndroid Build Coastguard Worker 
2511*9880d681SAndroid Build Coastguard Worker     // Look for (X86cmp (and $op, $imm), 0) and see if we can convert it to
2512*9880d681SAndroid Build Coastguard Worker     // use a smaller encoding.
2513*9880d681SAndroid Build Coastguard Worker     // Look past the truncate if CMP is the only use of it.
2514*9880d681SAndroid Build Coastguard Worker     if ((N0.getNode()->getOpcode() == ISD::AND ||
2515*9880d681SAndroid Build Coastguard Worker          (N0.getResNo() == 0 && N0.getNode()->getOpcode() == X86ISD::AND)) &&
2516*9880d681SAndroid Build Coastguard Worker         N0.getNode()->hasOneUse() &&
2517*9880d681SAndroid Build Coastguard Worker         N0.getValueType() != MVT::i8 &&
2518*9880d681SAndroid Build Coastguard Worker         X86::isZeroNode(N1)) {
2519*9880d681SAndroid Build Coastguard Worker       ConstantSDNode *C = dyn_cast<ConstantSDNode>(N0.getNode()->getOperand(1));
2520*9880d681SAndroid Build Coastguard Worker       if (!C) break;
2521*9880d681SAndroid Build Coastguard Worker 
2522*9880d681SAndroid Build Coastguard Worker       // For example, convert "testl %eax, $8" to "testb %al, $8"
2523*9880d681SAndroid Build Coastguard Worker       if ((C->getZExtValue() & ~UINT64_C(0xff)) == 0 &&
2524*9880d681SAndroid Build Coastguard Worker           (!(C->getZExtValue() & 0x80) ||
2525*9880d681SAndroid Build Coastguard Worker            hasNoSignedComparisonUses(Node))) {
2526*9880d681SAndroid Build Coastguard Worker         SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), dl, MVT::i8);
2527*9880d681SAndroid Build Coastguard Worker         SDValue Reg = N0.getNode()->getOperand(0);
2528*9880d681SAndroid Build Coastguard Worker 
2529*9880d681SAndroid Build Coastguard Worker         // On x86-32, only the ABCD registers have 8-bit subregisters.
2530*9880d681SAndroid Build Coastguard Worker         if (!Subtarget->is64Bit()) {
2531*9880d681SAndroid Build Coastguard Worker           const TargetRegisterClass *TRC;
2532*9880d681SAndroid Build Coastguard Worker           switch (N0.getSimpleValueType().SimpleTy) {
2533*9880d681SAndroid Build Coastguard Worker           case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2534*9880d681SAndroid Build Coastguard Worker           case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2535*9880d681SAndroid Build Coastguard Worker           default: llvm_unreachable("Unsupported TEST operand type!");
2536*9880d681SAndroid Build Coastguard Worker           }
2537*9880d681SAndroid Build Coastguard Worker           SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32);
2538*9880d681SAndroid Build Coastguard Worker           Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2539*9880d681SAndroid Build Coastguard Worker                                                Reg.getValueType(), Reg, RC), 0);
2540*9880d681SAndroid Build Coastguard Worker         }
2541*9880d681SAndroid Build Coastguard Worker 
2542*9880d681SAndroid Build Coastguard Worker         // Extract the l-register.
2543*9880d681SAndroid Build Coastguard Worker         SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit, dl,
2544*9880d681SAndroid Build Coastguard Worker                                                         MVT::i8, Reg);
2545*9880d681SAndroid Build Coastguard Worker 
2546*9880d681SAndroid Build Coastguard Worker         // Emit a testb.
2547*9880d681SAndroid Build Coastguard Worker         SDNode *NewNode = CurDAG->getMachineNode(X86::TEST8ri, dl, MVT::i32,
2548*9880d681SAndroid Build Coastguard Worker                                                  Subreg, Imm);
2549*9880d681SAndroid Build Coastguard Worker         // Replace SUB|CMP with TEST, since SUB has two outputs while TEST has
2550*9880d681SAndroid Build Coastguard Worker         // one, do not call ReplaceAllUsesWith.
2551*9880d681SAndroid Build Coastguard Worker         ReplaceUses(SDValue(Node, (Opcode == X86ISD::SUB ? 1 : 0)),
2552*9880d681SAndroid Build Coastguard Worker                     SDValue(NewNode, 0));
2553*9880d681SAndroid Build Coastguard Worker         return;
2554*9880d681SAndroid Build Coastguard Worker       }
2555*9880d681SAndroid Build Coastguard Worker 
2556*9880d681SAndroid Build Coastguard Worker       // For example, "testl %eax, $2048" to "testb %ah, $8".
2557*9880d681SAndroid Build Coastguard Worker       if ((C->getZExtValue() & ~UINT64_C(0xff00)) == 0 &&
2558*9880d681SAndroid Build Coastguard Worker           (!(C->getZExtValue() & 0x8000) ||
2559*9880d681SAndroid Build Coastguard Worker            hasNoSignedComparisonUses(Node))) {
2560*9880d681SAndroid Build Coastguard Worker         // Shift the immediate right by 8 bits.
2561*9880d681SAndroid Build Coastguard Worker         SDValue ShiftedImm = CurDAG->getTargetConstant(C->getZExtValue() >> 8,
2562*9880d681SAndroid Build Coastguard Worker                                                        dl, MVT::i8);
2563*9880d681SAndroid Build Coastguard Worker         SDValue Reg = N0.getNode()->getOperand(0);
2564*9880d681SAndroid Build Coastguard Worker 
2565*9880d681SAndroid Build Coastguard Worker         // Put the value in an ABCD register.
2566*9880d681SAndroid Build Coastguard Worker         const TargetRegisterClass *TRC;
2567*9880d681SAndroid Build Coastguard Worker         switch (N0.getSimpleValueType().SimpleTy) {
2568*9880d681SAndroid Build Coastguard Worker         case MVT::i64: TRC = &X86::GR64_ABCDRegClass; break;
2569*9880d681SAndroid Build Coastguard Worker         case MVT::i32: TRC = &X86::GR32_ABCDRegClass; break;
2570*9880d681SAndroid Build Coastguard Worker         case MVT::i16: TRC = &X86::GR16_ABCDRegClass; break;
2571*9880d681SAndroid Build Coastguard Worker         default: llvm_unreachable("Unsupported TEST operand type!");
2572*9880d681SAndroid Build Coastguard Worker         }
2573*9880d681SAndroid Build Coastguard Worker         SDValue RC = CurDAG->getTargetConstant(TRC->getID(), dl, MVT::i32);
2574*9880d681SAndroid Build Coastguard Worker         Reg = SDValue(CurDAG->getMachineNode(X86::COPY_TO_REGCLASS, dl,
2575*9880d681SAndroid Build Coastguard Worker                                              Reg.getValueType(), Reg, RC), 0);
2576*9880d681SAndroid Build Coastguard Worker 
2577*9880d681SAndroid Build Coastguard Worker         // Extract the h-register.
2578*9880d681SAndroid Build Coastguard Worker         SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_8bit_hi, dl,
2579*9880d681SAndroid Build Coastguard Worker                                                         MVT::i8, Reg);
2580*9880d681SAndroid Build Coastguard Worker 
2581*9880d681SAndroid Build Coastguard Worker         // Emit a testb.  The EXTRACT_SUBREG becomes a COPY that can only
2582*9880d681SAndroid Build Coastguard Worker         // target GR8_NOREX registers, so make sure the register class is
2583*9880d681SAndroid Build Coastguard Worker         // forced.
2584*9880d681SAndroid Build Coastguard Worker         SDNode *NewNode = CurDAG->getMachineNode(X86::TEST8ri_NOREX, dl,
2585*9880d681SAndroid Build Coastguard Worker                                                  MVT::i32, Subreg, ShiftedImm);
2586*9880d681SAndroid Build Coastguard Worker         // Replace SUB|CMP with TEST, since SUB has two outputs while TEST has
2587*9880d681SAndroid Build Coastguard Worker         // one, do not call ReplaceAllUsesWith.
2588*9880d681SAndroid Build Coastguard Worker         ReplaceUses(SDValue(Node, (Opcode == X86ISD::SUB ? 1 : 0)),
2589*9880d681SAndroid Build Coastguard Worker                     SDValue(NewNode, 0));
2590*9880d681SAndroid Build Coastguard Worker         return;
2591*9880d681SAndroid Build Coastguard Worker       }
2592*9880d681SAndroid Build Coastguard Worker 
2593*9880d681SAndroid Build Coastguard Worker       // For example, "testl %eax, $32776" to "testw %ax, $32776".
2594*9880d681SAndroid Build Coastguard Worker       if ((C->getZExtValue() & ~UINT64_C(0xffff)) == 0 &&
2595*9880d681SAndroid Build Coastguard Worker           N0.getValueType() != MVT::i16 &&
2596*9880d681SAndroid Build Coastguard Worker           (!(C->getZExtValue() & 0x8000) ||
2597*9880d681SAndroid Build Coastguard Worker            hasNoSignedComparisonUses(Node))) {
2598*9880d681SAndroid Build Coastguard Worker         SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), dl,
2599*9880d681SAndroid Build Coastguard Worker                                                 MVT::i16);
2600*9880d681SAndroid Build Coastguard Worker         SDValue Reg = N0.getNode()->getOperand(0);
2601*9880d681SAndroid Build Coastguard Worker 
2602*9880d681SAndroid Build Coastguard Worker         // Extract the 16-bit subregister.
2603*9880d681SAndroid Build Coastguard Worker         SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_16bit, dl,
2604*9880d681SAndroid Build Coastguard Worker                                                         MVT::i16, Reg);
2605*9880d681SAndroid Build Coastguard Worker 
2606*9880d681SAndroid Build Coastguard Worker         // Emit a testw.
2607*9880d681SAndroid Build Coastguard Worker         SDNode *NewNode = CurDAG->getMachineNode(X86::TEST16ri, dl, MVT::i32,
2608*9880d681SAndroid Build Coastguard Worker                                                  Subreg, Imm);
2609*9880d681SAndroid Build Coastguard Worker         // Replace SUB|CMP with TEST, since SUB has two outputs while TEST has
2610*9880d681SAndroid Build Coastguard Worker         // one, do not call ReplaceAllUsesWith.
2611*9880d681SAndroid Build Coastguard Worker         ReplaceUses(SDValue(Node, (Opcode == X86ISD::SUB ? 1 : 0)),
2612*9880d681SAndroid Build Coastguard Worker                     SDValue(NewNode, 0));
2613*9880d681SAndroid Build Coastguard Worker         return;
2614*9880d681SAndroid Build Coastguard Worker       }
2615*9880d681SAndroid Build Coastguard Worker 
2616*9880d681SAndroid Build Coastguard Worker       // For example, "testq %rax, $268468232" to "testl %eax, $268468232".
2617*9880d681SAndroid Build Coastguard Worker       if ((C->getZExtValue() & ~UINT64_C(0xffffffff)) == 0 &&
2618*9880d681SAndroid Build Coastguard Worker           N0.getValueType() == MVT::i64 &&
2619*9880d681SAndroid Build Coastguard Worker           (!(C->getZExtValue() & 0x80000000) ||
2620*9880d681SAndroid Build Coastguard Worker            hasNoSignedComparisonUses(Node))) {
2621*9880d681SAndroid Build Coastguard Worker         SDValue Imm = CurDAG->getTargetConstant(C->getZExtValue(), dl,
2622*9880d681SAndroid Build Coastguard Worker                                                 MVT::i32);
2623*9880d681SAndroid Build Coastguard Worker         SDValue Reg = N0.getNode()->getOperand(0);
2624*9880d681SAndroid Build Coastguard Worker 
2625*9880d681SAndroid Build Coastguard Worker         // Extract the 32-bit subregister.
2626*9880d681SAndroid Build Coastguard Worker         SDValue Subreg = CurDAG->getTargetExtractSubreg(X86::sub_32bit, dl,
2627*9880d681SAndroid Build Coastguard Worker                                                         MVT::i32, Reg);
2628*9880d681SAndroid Build Coastguard Worker 
2629*9880d681SAndroid Build Coastguard Worker         // Emit a testl.
2630*9880d681SAndroid Build Coastguard Worker         SDNode *NewNode = CurDAG->getMachineNode(X86::TEST32ri, dl, MVT::i32,
2631*9880d681SAndroid Build Coastguard Worker                                                  Subreg, Imm);
2632*9880d681SAndroid Build Coastguard Worker         // Replace SUB|CMP with TEST, since SUB has two outputs while TEST has
2633*9880d681SAndroid Build Coastguard Worker         // one, do not call ReplaceAllUsesWith.
2634*9880d681SAndroid Build Coastguard Worker         ReplaceUses(SDValue(Node, (Opcode == X86ISD::SUB ? 1 : 0)),
2635*9880d681SAndroid Build Coastguard Worker                     SDValue(NewNode, 0));
2636*9880d681SAndroid Build Coastguard Worker         return;
2637*9880d681SAndroid Build Coastguard Worker       }
2638*9880d681SAndroid Build Coastguard Worker     }
2639*9880d681SAndroid Build Coastguard Worker     break;
2640*9880d681SAndroid Build Coastguard Worker   }
2641*9880d681SAndroid Build Coastguard Worker   case ISD::STORE: {
2642*9880d681SAndroid Build Coastguard Worker     // Change a chain of {load; incr or dec; store} of the same value into
2643*9880d681SAndroid Build Coastguard Worker     // a simple increment or decrement through memory of that value, if the
2644*9880d681SAndroid Build Coastguard Worker     // uses of the modified value and its address are suitable.
2645*9880d681SAndroid Build Coastguard Worker     // The DEC64m tablegen pattern is currently not able to match the case where
2646*9880d681SAndroid Build Coastguard Worker     // the EFLAGS on the original DEC are used. (This also applies to
2647*9880d681SAndroid Build Coastguard Worker     // {INC,DEC}X{64,32,16,8}.)
2648*9880d681SAndroid Build Coastguard Worker     // We'll need to improve tablegen to allow flags to be transferred from a
2649*9880d681SAndroid Build Coastguard Worker     // node in the pattern to the result node.  probably with a new keyword
2650*9880d681SAndroid Build Coastguard Worker     // for example, we have this
2651*9880d681SAndroid Build Coastguard Worker     // def DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst), "dec{q}\t$dst",
2652*9880d681SAndroid Build Coastguard Worker     //  [(store (add (loadi64 addr:$dst), -1), addr:$dst),
2653*9880d681SAndroid Build Coastguard Worker     //   (implicit EFLAGS)]>;
2654*9880d681SAndroid Build Coastguard Worker     // but maybe need something like this
2655*9880d681SAndroid Build Coastguard Worker     // def DEC64m : RI<0xFF, MRM1m, (outs), (ins i64mem:$dst), "dec{q}\t$dst",
2656*9880d681SAndroid Build Coastguard Worker     //  [(store (add (loadi64 addr:$dst), -1), addr:$dst),
2657*9880d681SAndroid Build Coastguard Worker     //   (transferrable EFLAGS)]>;
2658*9880d681SAndroid Build Coastguard Worker 
2659*9880d681SAndroid Build Coastguard Worker     StoreSDNode *StoreNode = cast<StoreSDNode>(Node);
2660*9880d681SAndroid Build Coastguard Worker     SDValue StoredVal = StoreNode->getOperand(1);
2661*9880d681SAndroid Build Coastguard Worker     unsigned Opc = StoredVal->getOpcode();
2662*9880d681SAndroid Build Coastguard Worker 
2663*9880d681SAndroid Build Coastguard Worker     LoadSDNode *LoadNode = nullptr;
2664*9880d681SAndroid Build Coastguard Worker     SDValue InputChain;
2665*9880d681SAndroid Build Coastguard Worker     if (!isLoadIncOrDecStore(StoreNode, Opc, StoredVal, CurDAG,
2666*9880d681SAndroid Build Coastguard Worker                              LoadNode, InputChain))
2667*9880d681SAndroid Build Coastguard Worker       break;
2668*9880d681SAndroid Build Coastguard Worker 
2669*9880d681SAndroid Build Coastguard Worker     SDValue Base, Scale, Index, Disp, Segment;
2670*9880d681SAndroid Build Coastguard Worker     if (!selectAddr(LoadNode, LoadNode->getBasePtr(),
2671*9880d681SAndroid Build Coastguard Worker                     Base, Scale, Index, Disp, Segment))
2672*9880d681SAndroid Build Coastguard Worker       break;
2673*9880d681SAndroid Build Coastguard Worker 
2674*9880d681SAndroid Build Coastguard Worker     MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(2);
2675*9880d681SAndroid Build Coastguard Worker     MemOp[0] = StoreNode->getMemOperand();
2676*9880d681SAndroid Build Coastguard Worker     MemOp[1] = LoadNode->getMemOperand();
2677*9880d681SAndroid Build Coastguard Worker     const SDValue Ops[] = { Base, Scale, Index, Disp, Segment, InputChain };
2678*9880d681SAndroid Build Coastguard Worker     EVT LdVT = LoadNode->getMemoryVT();
2679*9880d681SAndroid Build Coastguard Worker     unsigned newOpc = getFusedLdStOpcode(LdVT, Opc);
2680*9880d681SAndroid Build Coastguard Worker     MachineSDNode *Result = CurDAG->getMachineNode(newOpc,
2681*9880d681SAndroid Build Coastguard Worker                                                    SDLoc(Node),
2682*9880d681SAndroid Build Coastguard Worker                                                    MVT::i32, MVT::Other, Ops);
2683*9880d681SAndroid Build Coastguard Worker     Result->setMemRefs(MemOp, MemOp + 2);
2684*9880d681SAndroid Build Coastguard Worker 
2685*9880d681SAndroid Build Coastguard Worker     ReplaceUses(SDValue(StoreNode, 0), SDValue(Result, 1));
2686*9880d681SAndroid Build Coastguard Worker     ReplaceUses(SDValue(StoredVal.getNode(), 1), SDValue(Result, 0));
2687*9880d681SAndroid Build Coastguard Worker     CurDAG->RemoveDeadNode(Node);
2688*9880d681SAndroid Build Coastguard Worker     return;
2689*9880d681SAndroid Build Coastguard Worker   }
2690*9880d681SAndroid Build Coastguard Worker   }
2691*9880d681SAndroid Build Coastguard Worker 
2692*9880d681SAndroid Build Coastguard Worker   SelectCode(Node);
2693*9880d681SAndroid Build Coastguard Worker }
2694*9880d681SAndroid Build Coastguard Worker 
2695*9880d681SAndroid Build Coastguard Worker bool X86DAGToDAGISel::
SelectInlineAsmMemoryOperand(const SDValue & Op,unsigned ConstraintID,std::vector<SDValue> & OutOps)2696*9880d681SAndroid Build Coastguard Worker SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
2697*9880d681SAndroid Build Coastguard Worker                              std::vector<SDValue> &OutOps) {
2698*9880d681SAndroid Build Coastguard Worker   SDValue Op0, Op1, Op2, Op3, Op4;
2699*9880d681SAndroid Build Coastguard Worker   switch (ConstraintID) {
2700*9880d681SAndroid Build Coastguard Worker   default:
2701*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("Unexpected asm memory constraint");
2702*9880d681SAndroid Build Coastguard Worker   case InlineAsm::Constraint_i:
2703*9880d681SAndroid Build Coastguard Worker     // FIXME: It seems strange that 'i' is needed here since it's supposed to
2704*9880d681SAndroid Build Coastguard Worker     //        be an immediate and not a memory constraint.
2705*9880d681SAndroid Build Coastguard Worker     // Fallthrough.
2706*9880d681SAndroid Build Coastguard Worker   case InlineAsm::Constraint_o: // offsetable        ??
2707*9880d681SAndroid Build Coastguard Worker   case InlineAsm::Constraint_v: // not offsetable    ??
2708*9880d681SAndroid Build Coastguard Worker   case InlineAsm::Constraint_m: // memory
2709*9880d681SAndroid Build Coastguard Worker   case InlineAsm::Constraint_X:
2710*9880d681SAndroid Build Coastguard Worker     if (!selectAddr(nullptr, Op, Op0, Op1, Op2, Op3, Op4))
2711*9880d681SAndroid Build Coastguard Worker       return true;
2712*9880d681SAndroid Build Coastguard Worker     break;
2713*9880d681SAndroid Build Coastguard Worker   }
2714*9880d681SAndroid Build Coastguard Worker 
2715*9880d681SAndroid Build Coastguard Worker   OutOps.push_back(Op0);
2716*9880d681SAndroid Build Coastguard Worker   OutOps.push_back(Op1);
2717*9880d681SAndroid Build Coastguard Worker   OutOps.push_back(Op2);
2718*9880d681SAndroid Build Coastguard Worker   OutOps.push_back(Op3);
2719*9880d681SAndroid Build Coastguard Worker   OutOps.push_back(Op4);
2720*9880d681SAndroid Build Coastguard Worker   return false;
2721*9880d681SAndroid Build Coastguard Worker }
2722*9880d681SAndroid Build Coastguard Worker 
2723*9880d681SAndroid Build Coastguard Worker /// This pass converts a legalized DAG into a X86-specific DAG,
2724*9880d681SAndroid Build Coastguard Worker /// ready for instruction scheduling.
createX86ISelDag(X86TargetMachine & TM,CodeGenOpt::Level OptLevel)2725*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createX86ISelDag(X86TargetMachine &TM,
2726*9880d681SAndroid Build Coastguard Worker                                      CodeGenOpt::Level OptLevel) {
2727*9880d681SAndroid Build Coastguard Worker   return new X86DAGToDAGISel(TM, OptLevel);
2728*9880d681SAndroid Build Coastguard Worker }
2729