blob: f5adfa542a3266534b9ab018de70883067ca93e0 [file] [log] [blame]
Eugene Zelenko618c5552017-09-13 21:15:20 +00001//==- llvm/CodeGen/SelectionDAGAddressAnalysis.cpp - DAG Address Analysis --==//
Nirav Davec1b6aa72017-06-21 15:40:43 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Nirav Davec1b6aa72017-06-21 15:40:43 +00006//
7//===----------------------------------------------------------------------===//
Nirav Davec1b6aa72017-06-21 15:40:43 +00008
9#include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
10#include "llvm/CodeGen/ISDOpcodes.h"
Nirav Dave168c5a62017-06-29 15:48:11 +000011#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000012#include "llvm/CodeGen/MachineFunction.h"
Nirav Davec1b6aa72017-06-21 15:40:43 +000013#include "llvm/CodeGen/SelectionDAG.h"
14#include "llvm/CodeGen/SelectionDAGNodes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000015#include "llvm/CodeGen/TargetLowering.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000016#include "llvm/Support/Casting.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000017#include <cstdint>
Nirav Davec1b6aa72017-06-21 15:40:43 +000018
Eugene Zelenko618c5552017-09-13 21:15:20 +000019using namespace llvm;
Nirav Davec1b6aa72017-06-21 15:40:43 +000020
Nirav Daveeedd2cc2018-10-30 18:26:43 +000021bool BaseIndexOffset::equalBaseIndex(const BaseIndexOffset &Other,
22 const SelectionDAG &DAG,
23 int64_t &Off) const {
Nirav Dave6e2d03d2018-01-08 16:21:35 +000024 // Conservatively fail if we a match failed..
25 if (!Base.getNode() || !Other.Base.getNode())
26 return false;
Nirav Dave168c5a62017-06-29 15:48:11 +000027 // Initial Offset difference.
Nirav Davec1b6aa72017-06-21 15:40:43 +000028 Off = Other.Offset - Offset;
Nirav Davec1b6aa72017-06-21 15:40:43 +000029
Nirav Dave168c5a62017-06-29 15:48:11 +000030 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))
Nirav Davec1b6aa72017-06-21 15:40:43 +000038 if (A->getGlobal() == B->getGlobal()) {
39 Off += B->getOffset() - A->getOffset();
40 return true;
41 }
42
Nirav Daved8e3633d2017-12-22 21:20:55 +000043 // 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
Nirav Dave168c5a62017-06-29 15:48:11 +000060 const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
Nirav Davec1b6aa72017-06-21 15:40:43 +000061
Clement Courbet17b51b62019-02-05 07:36:20 +000062 // Match FrameIndexes.
Nirav Dave168c5a62017-06-29 15:48:11 +000063 if (auto *A = dyn_cast<FrameIndexSDNode>(Base))
Clement Courbet17b51b62019-02-05 07:36:20 +000064 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.
Nirav Davea2810e62017-07-04 02:20:17 +000071 if (MFI.isFixedObjectIndex(A->getIndex()) &&
72 MFI.isFixedObjectIndex(B->getIndex())) {
Nirav Dave168c5a62017-06-29 15:48:11 +000073 Off += MFI.getObjectOffset(B->getIndex()) -
74 MFI.getObjectOffset(A->getIndex());
75 return true;
76 }
Clement Courbet17b51b62019-02-05 07:36:20 +000077 }
Nirav Dave168c5a62017-06-29 15:48:11 +000078 }
Nirav Davec1b6aa72017-06-21 15:40:43 +000079 return false;
80}
81
Clement Courbet62b3b912019-02-20 15:45:58 +000082bool 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
Nirav Dave582d4632019-02-26 15:02:32 +0000138bool BaseIndexOffset::contains(const SelectionDAG &DAG, int64_t BitSize,
139 const BaseIndexOffset &Other,
140 int64_t OtherBitSize, int64_t &BitOffset) const {
141 int64_t Offset;
Clement Courbet62b3b912019-02-20 15:45:58 +0000142 if (!equalBaseIndex(Other, DAG, Offset))
143 return false;
144 if (Offset >= 0) {
145 // Other is after *this:
146 // [-------*this---------]
147 // [---Other--]
148 // ==Offset==>
Nirav Dave582d4632019-02-26 15:02:32 +0000149 BitOffset = 8 * Offset;
150 return BitOffset + OtherBitSize <= BitSize;
Clement Courbet62b3b912019-02-20 15:45:58 +0000151 }
152 // Other starts strictly before *this, it cannot be fully contained.
153 // [-------*this---------]
154 // [--Other--]
155 return false;
156}
157
Nirav Davec1b6aa72017-06-21 15:40:43 +0000158/// Parses tree in Ptr for base, index, offset addresses.
Nirav Daveeedd2cc2018-10-30 18:26:43 +0000159BaseIndexOffset BaseIndexOffset::match(const LSBaseSDNode *N,
Nirav Dave6e2d03d2018-01-08 16:21:35 +0000160 const SelectionDAG &DAG) {
161 SDValue Ptr = N->getBasePtr();
162
Nirav Davec1b6aa72017-06-21 15:40:43 +0000163 // (((B + I*M) + c)) + c ...
Nirav Daved1b3f092017-08-11 13:21:35 +0000164 SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr);
Nirav Davec1b6aa72017-06-21 15:40:43 +0000165 SDValue Index = SDValue();
166 int64_t Offset = 0;
167 bool IsIndexSignExt = false;
168
Nirav Dave6e2d03d2018-01-08 16:21:35 +0000169 // pre-inc/pre-dec ops are components of EA.
170 if (N->getAddressingMode() == ISD::PRE_INC) {
171 if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
172 Offset += C->getSExtValue();
173 else // If unknown, give up now.
174 return BaseIndexOffset(SDValue(), SDValue(), 0, false);
175 } else if (N->getAddressingMode() == ISD::PRE_DEC) {
176 if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
177 Offset -= C->getSExtValue();
178 else // If unknown, give up now.
179 return BaseIndexOffset(SDValue(), SDValue(), 0, false);
180 }
181
Nirav Daveb320ef92017-07-05 01:21:23 +0000182 // Consume constant adds & ors with appropriate masking.
Nirav Dave98962382018-01-26 16:51:27 +0000183 while (true) {
184 switch (Base->getOpcode()) {
185 case ISD::OR:
Nirav Daveb320ef92017-07-05 01:21:23 +0000186 // Only consider ORs which act as adds.
Nirav Dave98962382018-01-26 16:51:27 +0000187 if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1)))
188 if (DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) {
189 Offset += C->getSExtValue();
Craig Topper923f463e2018-11-26 20:16:33 +0000190 Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0));
Nirav Dave98962382018-01-26 16:51:27 +0000191 continue;
192 }
193 break;
194 case ISD::ADD:
195 if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) {
196 Offset += C->getSExtValue();
Craig Topper923f463e2018-11-26 20:16:33 +0000197 Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0));
Nirav Dave98962382018-01-26 16:51:27 +0000198 continue;
199 }
200 break;
201 case ISD::LOAD:
202 case ISD::STORE: {
203 auto *LSBase = cast<LSBaseSDNode>(Base.getNode());
204 unsigned int IndexResNo = (Base->getOpcode() == ISD::LOAD) ? 1 : 0;
205 if (LSBase->isIndexed() && Base.getResNo() == IndexResNo)
206 if (auto *C = dyn_cast<ConstantSDNode>(LSBase->getOffset())) {
207 auto Off = C->getSExtValue();
208 if (LSBase->getAddressingMode() == ISD::PRE_DEC ||
209 LSBase->getAddressingMode() == ISD::POST_DEC)
210 Offset -= Off;
211 else
212 Offset += Off;
Craig Topper923f463e2018-11-26 20:16:33 +0000213 Base = DAG.getTargetLoweringInfo().unwrapAddress(LSBase->getBasePtr());
Nirav Dave98962382018-01-26 16:51:27 +0000214 continue;
215 }
216 break;
Nirav Daveb320ef92017-07-05 01:21:23 +0000217 }
Nirav Dave98962382018-01-26 16:51:27 +0000218 }
219 // If we get here break out of the loop.
Nirav Daveb320ef92017-07-05 01:21:23 +0000220 break;
Nirav Davec1b6aa72017-06-21 15:40:43 +0000221 }
222
223 if (Base->getOpcode() == ISD::ADD) {
224 // TODO: The following code appears to be needless as it just
225 // bails on some Ptrs early, reducing the cases where we
226 // find equivalence. We should be able to remove this.
227 // Inside a loop the current BASE pointer is calculated using an ADD and a
228 // MUL instruction. In this case Base is the actual BASE pointer.
229 // (i64 add (i64 %array_ptr)
230 // (i64 mul (i64 %induction_var)
231 // (i64 %element_size)))
232 if (Base->getOperand(1)->getOpcode() == ISD::MUL)
233 return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
234
235 // Look at Base + Index + Offset cases.
236 Index = Base->getOperand(1);
237 SDValue PotentialBase = Base->getOperand(0);
238
239 // Skip signextends.
240 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
241 Index = Index->getOperand(0);
242 IsIndexSignExt = true;
243 }
244
245 // Check if Index Offset pattern
246 if (Index->getOpcode() != ISD::ADD ||
247 !isa<ConstantSDNode>(Index->getOperand(1)))
248 return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt);
249
250 Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue();
251 Index = Index->getOperand(0);
252 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
253 Index = Index->getOperand(0);
254 IsIndexSignExt = true;
255 } else
256 IsIndexSignExt = false;
257 Base = PotentialBase;
258 }
259 return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
260}
Clement Courbet1bb0e5c2019-02-04 09:30:43 +0000261
262
263#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
264
265LLVM_DUMP_METHOD void BaseIndexOffset::dump() const {
266 print(dbgs());
267}
268
269void BaseIndexOffset::print(raw_ostream& OS) const {
270 OS << "BaseIndexOffset base=[";
271 Base->print(OS);
272 OS << "] index=[";
273 if (Index)
274 Index->print(OS);
275 OS << "] offset=" << Offset;
276}
277
278#endif