1 //==- llvm/CodeGen/SelectionDAGAddressAnalysis.cpp - DAG Address Analysis --==// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/CodeGen/SelectionDAGAddressAnalysis.h" 10 #include "llvm/CodeGen/ISDOpcodes.h" 11 #include "llvm/CodeGen/MachineFrameInfo.h" 12 #include "llvm/CodeGen/MachineFunction.h" 13 #include "llvm/CodeGen/SelectionDAG.h" 14 #include "llvm/CodeGen/SelectionDAGNodes.h" 15 #include "llvm/CodeGen/TargetLowering.h" 16 #include "llvm/Support/Casting.h" 17 #include <cstdint> 18 19 using namespace llvm; 20 21 bool BaseIndexOffset::equalBaseIndex(const BaseIndexOffset &Other, 22 const SelectionDAG &DAG, 23 int64_t &Off) const { 24 // Conservatively fail if we a match failed.. 25 if (!Base.getNode() || !Other.Base.getNode()) 26 return false; 27 // Initial Offset difference. 28 Off = Other.Offset - Offset; 29 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)) 38 if (A->getGlobal() == B->getGlobal()) { 39 Off += B->getOffset() - A->getOffset(); 40 return true; 41 } 42 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 60 const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 61 62 // Match FrameIndexes. 63 if (auto *A = dyn_cast<FrameIndexSDNode>(Base)) 64 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. 71 if (MFI.isFixedObjectIndex(A->getIndex()) && 72 MFI.isFixedObjectIndex(B->getIndex())) { 73 Off += MFI.getObjectOffset(B->getIndex()) - 74 MFI.getObjectOffset(A->getIndex()); 75 return true; 76 } 77 } 78 } 79 return false; 80 } 81 82 bool 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 138 bool BaseIndexOffset::contains(int64_t Size, const BaseIndexOffset &Other, 139 int64_t OtherSize, const SelectionDAG &DAG, 140 int64_t &Offset) const { 141 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 156 /// Parses tree in Ptr for base, index, offset addresses. 157 BaseIndexOffset BaseIndexOffset::match(const LSBaseSDNode *N, 158 const SelectionDAG &DAG) { 159 SDValue Ptr = N->getBasePtr(); 160 161 // (((B + I*M) + c)) + c ... 162 SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr); 163 SDValue Index = SDValue(); 164 int64_t Offset = 0; 165 bool IsIndexSignExt = false; 166 167 // 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 180 // Consume constant adds & ors with appropriate masking. 181 while (true) { 182 switch (Base->getOpcode()) { 183 case ISD::OR: 184 // Only consider ORs which act as adds. 185 if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) 186 if (DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) { 187 Offset += C->getSExtValue(); 188 Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0)); 189 continue; 190 } 191 break; 192 case ISD::ADD: 193 if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) { 194 Offset += C->getSExtValue(); 195 Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0)); 196 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; 211 Base = DAG.getTargetLoweringInfo().unwrapAddress(LSBase->getBasePtr()); 212 continue; 213 } 214 break; 215 } 216 } 217 // If we get here break out of the loop. 218 break; 219 } 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 } 259 260 261 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 262 263 LLVM_DUMP_METHOD void BaseIndexOffset::dump() const { 264 print(dbgs()); 265 } 266 267 void 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 277