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