Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 1 | //==- llvm/CodeGen/SelectionDAGAddressAnalysis.cpp - DAG Address Analysis --==// |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame^] | 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 |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 8 | |
| 9 | #include "llvm/CodeGen/SelectionDAGAddressAnalysis.h" |
| 10 | #include "llvm/CodeGen/ISDOpcodes.h" |
Nirav Dave | 168c5a6 | 2017-06-29 15:48:11 +0000 | [diff] [blame] | 11 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/MachineFunction.h" |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 13 | #include "llvm/CodeGen/SelectionDAG.h" |
| 14 | #include "llvm/CodeGen/SelectionDAGNodes.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/TargetLowering.h" |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Casting.h" |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 17 | #include <cstdint> |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 18 | |
Eugene Zelenko | 618c555 | 2017-09-13 21:15:20 +0000 | [diff] [blame] | 19 | using namespace llvm; |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 20 | |
Nirav Dave | eedd2cc | 2018-10-30 18:26:43 +0000 | [diff] [blame] | 21 | bool BaseIndexOffset::equalBaseIndex(const BaseIndexOffset &Other, |
| 22 | const SelectionDAG &DAG, |
| 23 | int64_t &Off) const { |
Nirav Dave | 6e2d03d | 2018-01-08 16:21:35 +0000 | [diff] [blame] | 24 | // Conservatively fail if we a match failed.. |
| 25 | if (!Base.getNode() || !Other.Base.getNode()) |
| 26 | return false; |
Nirav Dave | 168c5a6 | 2017-06-29 15:48:11 +0000 | [diff] [blame] | 27 | // Initial Offset difference. |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 28 | Off = Other.Offset - Offset; |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 29 | |
Nirav Dave | 168c5a6 | 2017-06-29 15:48:11 +0000 | [diff] [blame] | 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)) |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 38 | if (A->getGlobal() == B->getGlobal()) { |
| 39 | Off += B->getOffset() - A->getOffset(); |
| 40 | return true; |
| 41 | } |
| 42 | |
Nirav Dave | d8e3633d | 2017-12-22 21:20:55 +0000 | [diff] [blame] | 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 | |
Nirav Dave | 168c5a6 | 2017-06-29 15:48:11 +0000 | [diff] [blame] | 60 | const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 61 | |
Nirav Dave | a2810e6 | 2017-07-04 02:20:17 +0000 | [diff] [blame] | 62 | // 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 Dave | 168c5a6 | 2017-06-29 15:48:11 +0000 | [diff] [blame] | 65 | if (auto *A = dyn_cast<FrameIndexSDNode>(Base)) |
| 66 | if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base)) |
Nirav Dave | a2810e6 | 2017-07-04 02:20:17 +0000 | [diff] [blame] | 67 | if (MFI.isFixedObjectIndex(A->getIndex()) && |
| 68 | MFI.isFixedObjectIndex(B->getIndex())) { |
Nirav Dave | 168c5a6 | 2017-06-29 15:48:11 +0000 | [diff] [blame] | 69 | Off += MFI.getObjectOffset(B->getIndex()) - |
| 70 | MFI.getObjectOffset(A->getIndex()); |
| 71 | return true; |
| 72 | } |
| 73 | } |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | |
| 77 | /// Parses tree in Ptr for base, index, offset addresses. |
Nirav Dave | eedd2cc | 2018-10-30 18:26:43 +0000 | [diff] [blame] | 78 | BaseIndexOffset BaseIndexOffset::match(const LSBaseSDNode *N, |
Nirav Dave | 6e2d03d | 2018-01-08 16:21:35 +0000 | [diff] [blame] | 79 | const SelectionDAG &DAG) { |
| 80 | SDValue Ptr = N->getBasePtr(); |
| 81 | |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 82 | // (((B + I*M) + c)) + c ... |
Nirav Dave | d1b3f09 | 2017-08-11 13:21:35 +0000 | [diff] [blame] | 83 | SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr); |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 84 | SDValue Index = SDValue(); |
| 85 | int64_t Offset = 0; |
| 86 | bool IsIndexSignExt = false; |
| 87 | |
Nirav Dave | 6e2d03d | 2018-01-08 16:21:35 +0000 | [diff] [blame] | 88 | // 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 Dave | b320ef9 | 2017-07-05 01:21:23 +0000 | [diff] [blame] | 101 | // Consume constant adds & ors with appropriate masking. |
Nirav Dave | 9896238 | 2018-01-26 16:51:27 +0000 | [diff] [blame] | 102 | while (true) { |
| 103 | switch (Base->getOpcode()) { |
| 104 | case ISD::OR: |
Nirav Dave | b320ef9 | 2017-07-05 01:21:23 +0000 | [diff] [blame] | 105 | // Only consider ORs which act as adds. |
Nirav Dave | 9896238 | 2018-01-26 16:51:27 +0000 | [diff] [blame] | 106 | if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) |
| 107 | if (DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) { |
| 108 | Offset += C->getSExtValue(); |
Craig Topper | 923f463e | 2018-11-26 20:16:33 +0000 | [diff] [blame] | 109 | Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0)); |
Nirav Dave | 9896238 | 2018-01-26 16:51:27 +0000 | [diff] [blame] | 110 | continue; |
| 111 | } |
| 112 | break; |
| 113 | case ISD::ADD: |
| 114 | if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) { |
| 115 | Offset += C->getSExtValue(); |
Craig Topper | 923f463e | 2018-11-26 20:16:33 +0000 | [diff] [blame] | 116 | Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0)); |
Nirav Dave | 9896238 | 2018-01-26 16:51:27 +0000 | [diff] [blame] | 117 | continue; |
| 118 | } |
| 119 | break; |
| 120 | case ISD::LOAD: |
| 121 | case ISD::STORE: { |
| 122 | auto *LSBase = cast<LSBaseSDNode>(Base.getNode()); |
| 123 | unsigned int IndexResNo = (Base->getOpcode() == ISD::LOAD) ? 1 : 0; |
| 124 | if (LSBase->isIndexed() && Base.getResNo() == IndexResNo) |
| 125 | if (auto *C = dyn_cast<ConstantSDNode>(LSBase->getOffset())) { |
| 126 | auto Off = C->getSExtValue(); |
| 127 | if (LSBase->getAddressingMode() == ISD::PRE_DEC || |
| 128 | LSBase->getAddressingMode() == ISD::POST_DEC) |
| 129 | Offset -= Off; |
| 130 | else |
| 131 | Offset += Off; |
Craig Topper | 923f463e | 2018-11-26 20:16:33 +0000 | [diff] [blame] | 132 | Base = DAG.getTargetLoweringInfo().unwrapAddress(LSBase->getBasePtr()); |
Nirav Dave | 9896238 | 2018-01-26 16:51:27 +0000 | [diff] [blame] | 133 | continue; |
| 134 | } |
| 135 | break; |
Nirav Dave | b320ef9 | 2017-07-05 01:21:23 +0000 | [diff] [blame] | 136 | } |
Nirav Dave | 9896238 | 2018-01-26 16:51:27 +0000 | [diff] [blame] | 137 | } |
| 138 | // If we get here break out of the loop. |
Nirav Dave | b320ef9 | 2017-07-05 01:21:23 +0000 | [diff] [blame] | 139 | break; |
Nirav Dave | c1b6aa7 | 2017-06-21 15:40:43 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | if (Base->getOpcode() == ISD::ADD) { |
| 143 | // TODO: The following code appears to be needless as it just |
| 144 | // bails on some Ptrs early, reducing the cases where we |
| 145 | // find equivalence. We should be able to remove this. |
| 146 | // Inside a loop the current BASE pointer is calculated using an ADD and a |
| 147 | // MUL instruction. In this case Base is the actual BASE pointer. |
| 148 | // (i64 add (i64 %array_ptr) |
| 149 | // (i64 mul (i64 %induction_var) |
| 150 | // (i64 %element_size))) |
| 151 | if (Base->getOperand(1)->getOpcode() == ISD::MUL) |
| 152 | return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt); |
| 153 | |
| 154 | // Look at Base + Index + Offset cases. |
| 155 | Index = Base->getOperand(1); |
| 156 | SDValue PotentialBase = Base->getOperand(0); |
| 157 | |
| 158 | // Skip signextends. |
| 159 | if (Index->getOpcode() == ISD::SIGN_EXTEND) { |
| 160 | Index = Index->getOperand(0); |
| 161 | IsIndexSignExt = true; |
| 162 | } |
| 163 | |
| 164 | // Check if Index Offset pattern |
| 165 | if (Index->getOpcode() != ISD::ADD || |
| 166 | !isa<ConstantSDNode>(Index->getOperand(1))) |
| 167 | return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt); |
| 168 | |
| 169 | Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue(); |
| 170 | Index = Index->getOperand(0); |
| 171 | if (Index->getOpcode() == ISD::SIGN_EXTEND) { |
| 172 | Index = Index->getOperand(0); |
| 173 | IsIndexSignExt = true; |
| 174 | } else |
| 175 | IsIndexSignExt = false; |
| 176 | Base = PotentialBase; |
| 177 | } |
| 178 | return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt); |
| 179 | } |