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