LLVM  5.0.0
SelectionDAGAddressAnalysis.cpp
Go to the documentation of this file.
1 //===-- llvm/CodeGen/SelectionDAGAddressAnalysis.cpp ------- DAG Address
2 //Analysis ---*- C++ -*-===//
3 //
4 // The LLVM Compiler Infrastructure
5 //
6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10 //
11 
17 
18 namespace llvm {
19 
21  const SelectionDAG &DAG, int64_t &Off) {
22  // Initial Offset difference.
23  Off = Other.Offset - Offset;
24 
25  if ((Other.Index == Index) && (Other.IsIndexSignExt == IsIndexSignExt)) {
26  // Trivial match.
27  if (Other.Base == Base)
28  return true;
29 
30  // Match GlobalAddresses
31  if (auto *A = dyn_cast<GlobalAddressSDNode>(Base))
32  if (auto *B = dyn_cast<GlobalAddressSDNode>(Other.Base))
33  if (A->getGlobal() == B->getGlobal()) {
34  Off += B->getOffset() - A->getOffset();
35  return true;
36  }
37 
39 
40  // Match non-equal FrameIndexes - If both frame indices are fixed
41  // we know their relative offsets and can compare them. Otherwise
42  // we must be conservative.
43  if (auto *A = dyn_cast<FrameIndexSDNode>(Base))
44  if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base))
45  if (MFI.isFixedObjectIndex(A->getIndex()) &&
46  MFI.isFixedObjectIndex(B->getIndex())) {
47  Off += MFI.getObjectOffset(B->getIndex()) -
48  MFI.getObjectOffset(A->getIndex());
49  return true;
50  }
51  }
52  return false;
53 }
54 
55 /// Parses tree in Ptr for base, index, offset addresses.
57  // (((B + I*M) + c)) + c ...
58  SDValue Base = Ptr;
59  SDValue Index = SDValue();
60  int64_t Offset = 0;
61  bool IsIndexSignExt = false;
62 
63  // Consume constant adds & ors with appropriate masking.
64  while (Base->getOpcode() == ISD::ADD || Base->getOpcode() == ISD::OR) {
65  if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) {
66  // Only consider ORs which act as adds.
67  if (Base->getOpcode() == ISD::OR &&
68  !DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue()))
69  break;
70  Offset += C->getSExtValue();
71  Base = Base->getOperand(0);
72  continue;
73  }
74  break;
75  }
76 
77  if (Base->getOpcode() == ISD::ADD) {
78  // TODO: The following code appears to be needless as it just
79  // bails on some Ptrs early, reducing the cases where we
80  // find equivalence. We should be able to remove this.
81  // Inside a loop the current BASE pointer is calculated using an ADD and a
82  // MUL instruction. In this case Base is the actual BASE pointer.
83  // (i64 add (i64 %array_ptr)
84  // (i64 mul (i64 %induction_var)
85  // (i64 %element_size)))
86  if (Base->getOperand(1)->getOpcode() == ISD::MUL)
87  return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
88 
89  // Look at Base + Index + Offset cases.
90  Index = Base->getOperand(1);
91  SDValue PotentialBase = Base->getOperand(0);
92 
93  // Skip signextends.
94  if (Index->getOpcode() == ISD::SIGN_EXTEND) {
95  Index = Index->getOperand(0);
96  IsIndexSignExt = true;
97  }
98 
99  // Check if Index Offset pattern
100  if (Index->getOpcode() != ISD::ADD ||
101  !isa<ConstantSDNode>(Index->getOperand(1)))
102  return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt);
103 
104  Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue();
105  Index = Index->getOperand(0);
106  if (Index->getOpcode() == ISD::SIGN_EXTEND) {
107  Index = Index->getOperand(0);
108  IsIndexSignExt = true;
109  } else
110  IsIndexSignExt = false;
111  Base = PotentialBase;
112  }
113  return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
114 }
115 } // end namespace llvm
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
const SDValue & getOperand(unsigned Num) const
bool equalBaseIndex(BaseIndexOffset &Other, const SelectionDAG &DAG)
MachineFunction & getMachineFunction() const
Definition: SelectionDAG.h:375
The MachineFrameInfo class represents an abstract stack frame until prolog/epilog code is inserted...
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
Simple integer binary arithmetic operators.
Definition: ISDOpcodes.h:200
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
#define A
Definition: LargeTest.cpp:12
bool MaskedValueIsZero(SDValue Op, const APInt &Mask, unsigned Depth=0) const
Return true if 'Op & Mask' is known to be zero.
int64_t getObjectOffset(int ObjectIdx) const
Return the assigned stack offset of the specified object from the incoming stack pointer.
Helper struct to parse and store a memory address as base + index + offset.
#define B
Definition: LargeTest.cpp:24
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
Definition: SelectionDAG.h:209
static BaseIndexOffset match(SDValue Ptr, const SelectionDAG &DAG)
Parses tree in Ptr for base, index, offset addresses.
#define C
Definition: LargeTest.cpp:25
Conversion operators.
Definition: ISDOpcodes.h:441
int * Ptr
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation...