blob: b0dcf476db351b4467c36c500b45fe4c28c4d3d7 [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
138bool BaseIndexOffset::contains(int64_t Size, const BaseIndexOffset &Other,
Nirav Dave44037d72019-02-22 16:00:19 +0000139 int64_t OtherSize, const SelectionDAG &DAG,
140 int64_t &Offset) const {
Clement Courbet62b3b912019-02-20 15:45:58 +0000141 if (!equalBaseIndex(Other, DAG, Offset))
142 return false;
143 if (Offset >= 0) {
144 // Other is after *this:
145 // [-------*this---------]
146 // [---Other--]
147 // ==Offset==>
148 return Offset + OtherSize <= Size;
149 }
150 // Other starts strictly before *this, it cannot be fully contained.
151 // [-------*this---------]
152 // [--Other--]
153 return false;
154}
155
Nirav Davec1b6aa72017-06-21 15:40:43 +0000156/// Parses tree in Ptr for base, index, offset addresses.
Nirav Daveeedd2cc2018-10-30 18:26:43 +0000157BaseIndexOffset BaseIndexOffset::match(const LSBaseSDNode *N,
Nirav Dave6e2d03d2018-01-08 16:21:35 +0000158 const SelectionDAG &DAG) {
159 SDValue Ptr = N->getBasePtr();
160
Nirav Davec1b6aa72017-06-21 15:40:43 +0000161 // (((B + I*M) + c)) + c ...
Nirav Daved1b3f092017-08-11 13:21:35 +0000162 SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr);
Nirav Davec1b6aa72017-06-21 15:40:43 +0000163 SDValue Index = SDValue();
164 int64_t Offset = 0;
165 bool IsIndexSignExt = false;
166
Nirav Dave6e2d03d2018-01-08 16:21:35 +0000167 // pre-inc/pre-dec ops are components of EA.
168 if (N->getAddressingMode() == ISD::PRE_INC) {
169 if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
170 Offset += C->getSExtValue();
171 else // If unknown, give up now.
172 return BaseIndexOffset(SDValue(), SDValue(), 0, false);
173 } else if (N->getAddressingMode() == ISD::PRE_DEC) {
174 if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
175 Offset -= C->getSExtValue();
176 else // If unknown, give up now.
177 return BaseIndexOffset(SDValue(), SDValue(), 0, false);
178 }
179
Nirav Daveb320ef92017-07-05 01:21:23 +0000180 // Consume constant adds & ors with appropriate masking.
Nirav Dave98962382018-01-26 16:51:27 +0000181 while (true) {
182 switch (Base->getOpcode()) {
183 case ISD::OR:
Nirav Daveb320ef92017-07-05 01:21:23 +0000184 // Only consider ORs which act as adds.
Nirav Dave98962382018-01-26 16:51:27 +0000185 if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1)))
186 if (DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) {
187 Offset += C->getSExtValue();
Craig Topper923f463e2018-11-26 20:16:33 +0000188 Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0));
Nirav Dave98962382018-01-26 16:51:27 +0000189 continue;
190 }
191 break;
192 case ISD::ADD:
193 if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) {
194 Offset += C->getSExtValue();
Craig Topper923f463e2018-11-26 20:16:33 +0000195 Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0));
Nirav Dave98962382018-01-26 16:51:27 +0000196 continue;
197 }
198 break;
199 case ISD::LOAD:
200 case ISD::STORE: {
201 auto *LSBase = cast<LSBaseSDNode>(Base.getNode());
202 unsigned int IndexResNo = (Base->getOpcode() == ISD::LOAD) ? 1 : 0;
203 if (LSBase->isIndexed() && Base.getResNo() == IndexResNo)
204 if (auto *C = dyn_cast<ConstantSDNode>(LSBase->getOffset())) {
205 auto Off = C->getSExtValue();
206 if (LSBase->getAddressingMode() == ISD::PRE_DEC ||
207 LSBase->getAddressingMode() == ISD::POST_DEC)
208 Offset -= Off;
209 else
210 Offset += Off;
Craig Topper923f463e2018-11-26 20:16:33 +0000211 Base = DAG.getTargetLoweringInfo().unwrapAddress(LSBase->getBasePtr());
Nirav Dave98962382018-01-26 16:51:27 +0000212 continue;
213 }
214 break;
Nirav Daveb320ef92017-07-05 01:21:23 +0000215 }
Nirav Dave98962382018-01-26 16:51:27 +0000216 }
217 // If we get here break out of the loop.
Nirav Daveb320ef92017-07-05 01:21:23 +0000218 break;
Nirav Davec1b6aa72017-06-21 15:40:43 +0000219 }
220
221 if (Base->getOpcode() == ISD::ADD) {
222 // TODO: The following code appears to be needless as it just
223 // bails on some Ptrs early, reducing the cases where we
224 // find equivalence. We should be able to remove this.
225 // Inside a loop the current BASE pointer is calculated using an ADD and a
226 // MUL instruction. In this case Base is the actual BASE pointer.
227 // (i64 add (i64 %array_ptr)
228 // (i64 mul (i64 %induction_var)
229 // (i64 %element_size)))
230 if (Base->getOperand(1)->getOpcode() == ISD::MUL)
231 return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
232
233 // Look at Base + Index + Offset cases.
234 Index = Base->getOperand(1);
235 SDValue PotentialBase = Base->getOperand(0);
236
237 // Skip signextends.
238 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
239 Index = Index->getOperand(0);
240 IsIndexSignExt = true;
241 }
242
243 // Check if Index Offset pattern
244 if (Index->getOpcode() != ISD::ADD ||
245 !isa<ConstantSDNode>(Index->getOperand(1)))
246 return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt);
247
248 Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue();
249 Index = Index->getOperand(0);
250 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
251 Index = Index->getOperand(0);
252 IsIndexSignExt = true;
253 } else
254 IsIndexSignExt = false;
255 Base = PotentialBase;
256 }
257 return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
258}
Clement Courbet1bb0e5c2019-02-04 09:30:43 +0000259
260
261#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
262
263LLVM_DUMP_METHOD void BaseIndexOffset::dump() const {
264 print(dbgs());
265}
266
267void BaseIndexOffset::print(raw_ostream& OS) const {
268 OS << "BaseIndexOffset base=[";
269 Base->print(OS);
270 OS << "] index=[";
271 if (Index)
272 Index->print(OS);
273 OS << "] offset=" << Offset;
274}
275
276#endif