1 //==- llvm/CodeGen/SelectionDAGAddressAnalysis.cpp - DAG Address Analysis --==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
10 #include "llvm/CodeGen/ISDOpcodes.h"
11 #include "llvm/CodeGen/MachineFrameInfo.h"
12 #include "llvm/CodeGen/MachineFunction.h"
13 #include "llvm/CodeGen/SelectionDAG.h"
14 #include "llvm/CodeGen/SelectionDAGNodes.h"
15 #include "llvm/CodeGen/TargetLowering.h"
16 #include "llvm/Support/Casting.h"
17 #include <cstdint>
18 
19 using namespace llvm;
20 
21 bool BaseIndexOffset::equalBaseIndex(const BaseIndexOffset &Other,
22                                      const SelectionDAG &DAG,
23                                      int64_t &Off) const {
24   // Conservatively fail if we a match failed..
25   if (!Base.getNode() || !Other.Base.getNode())
26     return false;
27   // Initial Offset difference.
28   Off = Other.Offset - Offset;
29 
30   if ((Other.Index == Index) && (Other.IsIndexSignExt == IsIndexSignExt)) {
31     // Trivial match.
32     if (Other.Base == Base)
33       return true;
34 
35     // Match GlobalAddresses
36     if (auto *A = dyn_cast<GlobalAddressSDNode>(Base))
37       if (auto *B = dyn_cast<GlobalAddressSDNode>(Other.Base))
38         if (A->getGlobal() == B->getGlobal()) {
39           Off += B->getOffset() - A->getOffset();
40           return true;
41         }
42 
43     // Match Constants
44     if (auto *A = dyn_cast<ConstantPoolSDNode>(Base))
45       if (auto *B = dyn_cast<ConstantPoolSDNode>(Other.Base)) {
46         bool IsMatch =
47             A->isMachineConstantPoolEntry() == B->isMachineConstantPoolEntry();
48         if (IsMatch) {
49           if (A->isMachineConstantPoolEntry())
50             IsMatch = A->getMachineCPVal() == B->getMachineCPVal();
51           else
52             IsMatch = A->getConstVal() == B->getConstVal();
53         }
54         if (IsMatch) {
55           Off += B->getOffset() - A->getOffset();
56           return true;
57         }
58       }
59 
60     const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
61 
62     // Match FrameIndexes.
63     if (auto *A = dyn_cast<FrameIndexSDNode>(Base))
64       if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base)) {
65         // Equal FrameIndexes - offsets are directly comparable.
66         if (A->getIndex() == B->getIndex())
67           return true;
68         // Non-equal FrameIndexes - If both frame indices are fixed
69         // we know their relative offsets and can compare them. Otherwise
70         // we must be conservative.
71         if (MFI.isFixedObjectIndex(A->getIndex()) &&
72             MFI.isFixedObjectIndex(B->getIndex())) {
73           Off += MFI.getObjectOffset(B->getIndex()) -
74                  MFI.getObjectOffset(A->getIndex());
75           return true;
76         }
77       }
78   }
79   return false;
80 }
81 
82 bool BaseIndexOffset::computeAliasing(const BaseIndexOffset &BasePtr0,
83                                       const int64_t NumBytes0,
84                                       const BaseIndexOffset &BasePtr1,
85                                       const int64_t NumBytes1,
86                                       const SelectionDAG &DAG, bool &IsAlias) {
87   if (!(BasePtr0.getBase().getNode() && BasePtr1.getBase().getNode()))
88     return false;
89   int64_t PtrDiff;
90   if (BasePtr0.equalBaseIndex(BasePtr1, DAG, PtrDiff)) {
91     // BasePtr1 is PtrDiff away from BasePtr0. They alias if none of the
92     // following situations arise:
93     IsAlias = !(
94         // [----BasePtr0----]
95         //                         [---BasePtr1--]
96         // ========PtrDiff========>
97         (NumBytes0 <= PtrDiff) ||
98         //                     [----BasePtr0----]
99         // [---BasePtr1--]
100         // =====(-PtrDiff)====>
101         (PtrDiff + NumBytes1 <= 0)); // i.e. NumBytes1 < -PtrDiff.
102     return true;
103   }
104   // If both BasePtr0 and BasePtr1 are FrameIndexes, we will not be
105   // able to calculate their relative offset if at least one arises
106   // from an alloca. However, these allocas cannot overlap and we
107   // can infer there is no alias.
108   if (auto *A = dyn_cast<FrameIndexSDNode>(BasePtr0.getBase()))
109     if (auto *B = dyn_cast<FrameIndexSDNode>(BasePtr1.getBase())) {
110       MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
111       // If the base are the same frame index but the we couldn't find a
112       // constant offset, (indices are different) be conservative.
113       if (A != B && (!MFI.isFixedObjectIndex(A->getIndex()) ||
114                      !MFI.isFixedObjectIndex(B->getIndex()))) {
115         IsAlias = false;
116         return true;
117       }
118     }
119 
120   bool IsFI0 = isa<FrameIndexSDNode>(BasePtr0.getBase());
121   bool IsFI1 = isa<FrameIndexSDNode>(BasePtr1.getBase());
122   bool IsGV0 = isa<GlobalAddressSDNode>(BasePtr0.getBase());
123   bool IsGV1 = isa<GlobalAddressSDNode>(BasePtr1.getBase());
124   bool IsCV0 = isa<ConstantPoolSDNode>(BasePtr0.getBase());
125   bool IsCV1 = isa<ConstantPoolSDNode>(BasePtr1.getBase());
126 
127   // If of mismatched base types or checkable indices we can check
128   // they do not alias.
129   if ((BasePtr0.getIndex() == BasePtr1.getIndex() || (IsFI0 != IsFI1) ||
130        (IsGV0 != IsGV1) || (IsCV0 != IsCV1)) &&
131       (IsFI0 || IsGV0 || IsCV0) && (IsFI1 || IsGV1 || IsCV1)) {
132     IsAlias = false;
133     return true;
134   }
135   return false; // Cannot determine whether the pointers alias.
136 }
137 
138 bool BaseIndexOffset::contains(int64_t Size, const BaseIndexOffset &Other,
139                                int64_t OtherSize,
140                                const SelectionDAG &DAG) const {
141   int64_t Offset;
142   if (!equalBaseIndex(Other, DAG, Offset))
143     return false;
144   if (Offset >= 0) {
145     // Other is after *this:
146     // [-------*this---------]
147     //            [---Other--]
148     // ==Offset==>
149     return Offset + OtherSize <= Size;
150   }
151   // Other starts strictly before *this, it cannot be fully contained.
152   //    [-------*this---------]
153   // [--Other--]
154   return false;
155 }
156 
157 /// Parses tree in Ptr for base, index, offset addresses.
158 BaseIndexOffset BaseIndexOffset::match(const LSBaseSDNode *N,
159                                        const SelectionDAG &DAG) {
160   SDValue Ptr = N->getBasePtr();
161 
162   // (((B + I*M) + c)) + c ...
163   SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr);
164   SDValue Index = SDValue();
165   int64_t Offset = 0;
166   bool IsIndexSignExt = false;
167 
168   // pre-inc/pre-dec ops are components of EA.
169   if (N->getAddressingMode() == ISD::PRE_INC) {
170     if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
171       Offset += C->getSExtValue();
172     else // If unknown, give up now.
173       return BaseIndexOffset(SDValue(), SDValue(), 0, false);
174   } else if (N->getAddressingMode() == ISD::PRE_DEC) {
175     if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
176       Offset -= C->getSExtValue();
177     else // If unknown, give up now.
178       return BaseIndexOffset(SDValue(), SDValue(), 0, false);
179   }
180 
181   // Consume constant adds & ors with appropriate masking.
182   while (true) {
183     switch (Base->getOpcode()) {
184     case ISD::OR:
185       // Only consider ORs which act as adds.
186       if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1)))
187         if (DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) {
188           Offset += C->getSExtValue();
189           Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0));
190           continue;
191         }
192       break;
193     case ISD::ADD:
194       if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) {
195         Offset += C->getSExtValue();
196         Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0));
197         continue;
198       }
199       break;
200     case ISD::LOAD:
201     case ISD::STORE: {
202       auto *LSBase = cast<LSBaseSDNode>(Base.getNode());
203       unsigned int IndexResNo = (Base->getOpcode() == ISD::LOAD) ? 1 : 0;
204       if (LSBase->isIndexed() && Base.getResNo() == IndexResNo)
205         if (auto *C = dyn_cast<ConstantSDNode>(LSBase->getOffset())) {
206           auto Off = C->getSExtValue();
207           if (LSBase->getAddressingMode() == ISD::PRE_DEC ||
208               LSBase->getAddressingMode() == ISD::POST_DEC)
209             Offset -= Off;
210           else
211             Offset += Off;
212           Base = DAG.getTargetLoweringInfo().unwrapAddress(LSBase->getBasePtr());
213           continue;
214         }
215       break;
216     }
217     }
218     // If we get here break out of the loop.
219     break;
220   }
221 
222   if (Base->getOpcode() == ISD::ADD) {
223     // TODO: The following code appears to be needless as it just
224     //       bails on some Ptrs early, reducing the cases where we
225     //       find equivalence. We should be able to remove this.
226     // Inside a loop the current BASE pointer is calculated using an ADD and a
227     // MUL instruction. In this case Base is the actual BASE pointer.
228     // (i64 add (i64 %array_ptr)
229     //          (i64 mul (i64 %induction_var)
230     //                   (i64 %element_size)))
231     if (Base->getOperand(1)->getOpcode() == ISD::MUL)
232       return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
233 
234     // Look at Base + Index + Offset cases.
235     Index = Base->getOperand(1);
236     SDValue PotentialBase = Base->getOperand(0);
237 
238     // Skip signextends.
239     if (Index->getOpcode() == ISD::SIGN_EXTEND) {
240       Index = Index->getOperand(0);
241       IsIndexSignExt = true;
242     }
243 
244     // Check if Index Offset pattern
245     if (Index->getOpcode() != ISD::ADD ||
246         !isa<ConstantSDNode>(Index->getOperand(1)))
247       return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt);
248 
249     Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue();
250     Index = Index->getOperand(0);
251     if (Index->getOpcode() == ISD::SIGN_EXTEND) {
252       Index = Index->getOperand(0);
253       IsIndexSignExt = true;
254     } else
255       IsIndexSignExt = false;
256     Base = PotentialBase;
257   }
258   return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
259 }
260 
261 
262 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
263 
264 LLVM_DUMP_METHOD void BaseIndexOffset::dump() const {
265   print(dbgs());
266 }
267 
268 void BaseIndexOffset::print(raw_ostream& OS) const {
269   OS << "BaseIndexOffset base=[";
270   Base->print(OS);
271   OS << "] index=[";
272   if (Index)
273     Index->print(OS);
274   OS << "] offset=" << Offset;
275 }
276 
277 #endif
278