blob: da1574f6052400f0a39d82efced3736aaee6f59a [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//
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//===----------------------------------------------------------------------===//
Nirav Davec1b6aa72017-06-21 15:40:43 +00009
10#include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
11#include "llvm/CodeGen/ISDOpcodes.h"
Nirav Dave168c5a62017-06-29 15:48:11 +000012#include "llvm/CodeGen/MachineFrameInfo.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000013#include "llvm/CodeGen/MachineFunction.h"
Nirav Davec1b6aa72017-06-21 15:40:43 +000014#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/SelectionDAGNodes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000016#include "llvm/CodeGen/TargetLowering.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000017#include "llvm/Support/Casting.h"
Eugene Zelenko618c5552017-09-13 21:15:20 +000018#include <cstdint>
Nirav Davec1b6aa72017-06-21 15:40:43 +000019
Eugene Zelenko618c5552017-09-13 21:15:20 +000020using namespace llvm;
Nirav Davec1b6aa72017-06-21 15:40:43 +000021
22bool BaseIndexOffset::equalBaseIndex(BaseIndexOffset &Other,
23 const SelectionDAG &DAG, int64_t &Off) {
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
Nirav Davea2810e62017-07-04 02:20:17 +000062 // Match non-equal FrameIndexes - If both frame indices are fixed
63 // we know their relative offsets and can compare them. Otherwise
64 // we must be conservative.
Nirav Dave168c5a62017-06-29 15:48:11 +000065 if (auto *A = dyn_cast<FrameIndexSDNode>(Base))
66 if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base))
Nirav Davea2810e62017-07-04 02:20:17 +000067 if (MFI.isFixedObjectIndex(A->getIndex()) &&
68 MFI.isFixedObjectIndex(B->getIndex())) {
Nirav Dave168c5a62017-06-29 15:48:11 +000069 Off += MFI.getObjectOffset(B->getIndex()) -
70 MFI.getObjectOffset(A->getIndex());
71 return true;
72 }
73 }
Nirav Davec1b6aa72017-06-21 15:40:43 +000074 return false;
75}
76
77/// Parses tree in Ptr for base, index, offset addresses.
Nirav Dave6e2d03d2018-01-08 16:21:35 +000078BaseIndexOffset BaseIndexOffset::match(LSBaseSDNode *N,
79 const SelectionDAG &DAG) {
80 SDValue Ptr = N->getBasePtr();
81
Nirav Davec1b6aa72017-06-21 15:40:43 +000082 // (((B + I*M) + c)) + c ...
Nirav Daved1b3f092017-08-11 13:21:35 +000083 SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr);
Nirav Davec1b6aa72017-06-21 15:40:43 +000084 SDValue Index = SDValue();
85 int64_t Offset = 0;
86 bool IsIndexSignExt = false;
87
Nirav Dave6e2d03d2018-01-08 16:21:35 +000088 // pre-inc/pre-dec ops are components of EA.
89 if (N->getAddressingMode() == ISD::PRE_INC) {
90 if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
91 Offset += C->getSExtValue();
92 else // If unknown, give up now.
93 return BaseIndexOffset(SDValue(), SDValue(), 0, false);
94 } else if (N->getAddressingMode() == ISD::PRE_DEC) {
95 if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
96 Offset -= C->getSExtValue();
97 else // If unknown, give up now.
98 return BaseIndexOffset(SDValue(), SDValue(), 0, false);
99 }
100
Nirav Daveb320ef92017-07-05 01:21:23 +0000101 // Consume constant adds & ors with appropriate masking.
102 while (Base->getOpcode() == ISD::ADD || Base->getOpcode() == ISD::OR) {
103 if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) {
104 // Only consider ORs which act as adds.
105 if (Base->getOpcode() == ISD::OR &&
106 !DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue()))
107 break;
108 Offset += C->getSExtValue();
109 Base = Base->getOperand(0);
110 continue;
111 }
112 break;
Nirav Davec1b6aa72017-06-21 15:40:43 +0000113 }
114
115 if (Base->getOpcode() == ISD::ADD) {
116 // TODO: The following code appears to be needless as it just
117 // bails on some Ptrs early, reducing the cases where we
118 // find equivalence. We should be able to remove this.
119 // Inside a loop the current BASE pointer is calculated using an ADD and a
120 // MUL instruction. In this case Base is the actual BASE pointer.
121 // (i64 add (i64 %array_ptr)
122 // (i64 mul (i64 %induction_var)
123 // (i64 %element_size)))
124 if (Base->getOperand(1)->getOpcode() == ISD::MUL)
125 return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
126
127 // Look at Base + Index + Offset cases.
128 Index = Base->getOperand(1);
129 SDValue PotentialBase = Base->getOperand(0);
130
131 // Skip signextends.
132 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
133 Index = Index->getOperand(0);
134 IsIndexSignExt = true;
135 }
136
137 // Check if Index Offset pattern
138 if (Index->getOpcode() != ISD::ADD ||
139 !isa<ConstantSDNode>(Index->getOperand(1)))
140 return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt);
141
142 Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue();
143 Index = Index->getOperand(0);
144 if (Index->getOpcode() == ISD::SIGN_EXTEND) {
145 Index = Index->getOperand(0);
146 IsIndexSignExt = true;
147 } else
148 IsIndexSignExt = false;
149 Base = PotentialBase;
150 }
151 return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
152}