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 
10 #include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
11 #include "llvm/CodeGen/ISDOpcodes.h"
12 #include "llvm/CodeGen/MachineFrameInfo.h"
13 #include "llvm/CodeGen/MachineFunction.h"
14 #include "llvm/CodeGen/SelectionDAG.h"
15 #include "llvm/CodeGen/SelectionDAGNodes.h"
16 #include "llvm/CodeGen/TargetLowering.h"
17 #include "llvm/Support/Casting.h"
18 #include <cstdint>
19 
20 using namespace llvm;
21 
22 bool BaseIndexOffset::equalBaseIndex(BaseIndexOffset &Other,
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 
40     // Match Constants
41     if (auto *A = dyn_cast<ConstantPoolSDNode>(Base))
42       if (auto *B = dyn_cast<ConstantPoolSDNode>(Other.Base)) {
43         bool IsMatch =
44             A->isMachineConstantPoolEntry() == B->isMachineConstantPoolEntry();
45         if (IsMatch) {
46           if (A->isMachineConstantPoolEntry())
47             IsMatch = A->getMachineCPVal() == B->getMachineCPVal();
48           else
49             IsMatch = A->getConstVal() == B->getConstVal();
50         }
51         if (IsMatch) {
52           Off += B->getOffset() - A->getOffset();
53           return true;
54         }
55       }
56 
57     const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
58 
59     // Match non-equal FrameIndexes - If both frame indices are fixed
60     // we know their relative offsets and can compare them. Otherwise
61     // we must be conservative.
62     if (auto *A = dyn_cast<FrameIndexSDNode>(Base))
63       if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base))
64         if (MFI.isFixedObjectIndex(A->getIndex()) &&
65             MFI.isFixedObjectIndex(B->getIndex())) {
66           Off += MFI.getObjectOffset(B->getIndex()) -
67                  MFI.getObjectOffset(A->getIndex());
68           return true;
69         }
70   }
71   return false;
72 }
73 
74 /// Parses tree in Ptr for base, index, offset addresses.
75 BaseIndexOffset BaseIndexOffset::match(SDValue Ptr, const SelectionDAG &DAG) {
76   // (((B + I*M) + c)) + c ...
77   SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr);
78   SDValue Index = SDValue();
79   int64_t Offset = 0;
80   bool IsIndexSignExt = false;
81 
82   // Consume constant adds & ors with appropriate masking.
83   while (Base->getOpcode() == ISD::ADD || Base->getOpcode() == ISD::OR) {
84     if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) {
85       // Only consider ORs which act as adds.
86       if (Base->getOpcode() == ISD::OR &&
87           !DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue()))
88         break;
89       Offset += C->getSExtValue();
90       Base = Base->getOperand(0);
91       continue;
92     }
93     break;
94   }
95 
96   if (Base->getOpcode() == ISD::ADD) {
97     // TODO: The following code appears to be needless as it just
98     //       bails on some Ptrs early, reducing the cases where we
99     //       find equivalence. We should be able to remove this.
100     // Inside a loop the current BASE pointer is calculated using an ADD and a
101     // MUL instruction. In this case Base is the actual BASE pointer.
102     // (i64 add (i64 %array_ptr)
103     //          (i64 mul (i64 %induction_var)
104     //                   (i64 %element_size)))
105     if (Base->getOperand(1)->getOpcode() == ISD::MUL)
106       return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
107 
108     // Look at Base + Index + Offset cases.
109     Index = Base->getOperand(1);
110     SDValue PotentialBase = Base->getOperand(0);
111 
112     // Skip signextends.
113     if (Index->getOpcode() == ISD::SIGN_EXTEND) {
114       Index = Index->getOperand(0);
115       IsIndexSignExt = true;
116     }
117 
118     // Check if Index Offset pattern
119     if (Index->getOpcode() != ISD::ADD ||
120         !isa<ConstantSDNode>(Index->getOperand(1)))
121       return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt);
122 
123     Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue();
124     Index = Index->getOperand(0);
125     if (Index->getOpcode() == ISD::SIGN_EXTEND) {
126       Index = Index->getOperand(0);
127       IsIndexSignExt = true;
128     } else
129       IsIndexSignExt = false;
130     Base = PotentialBase;
131   }
132   return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
133 }
134