10b57cec5SDimitry Andric //==- llvm/CodeGen/SelectionDAGAddressAnalysis.cpp - DAG Address Analysis --==// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGAddressAnalysis.h" 10e8d8bef9SDimitry Andric #include "llvm/Analysis/MemoryLocation.h" 110b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h" 120b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 130b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 140b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h" 150b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h" 160b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 17349cc55cSDimitry Andric #include "llvm/IR/GlobalAlias.h" 180b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 198bcb0991SDimitry Andric #include "llvm/Support/Debug.h" 200b57cec5SDimitry Andric #include <cstdint> 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric using namespace llvm; 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric bool BaseIndexOffset::equalBaseIndex(const BaseIndexOffset &Other, 250b57cec5SDimitry Andric const SelectionDAG &DAG, 260b57cec5SDimitry Andric int64_t &Off) const { 270b57cec5SDimitry Andric // Conservatively fail if we a match failed.. 280b57cec5SDimitry Andric if (!Base.getNode() || !Other.Base.getNode()) 290b57cec5SDimitry Andric return false; 300b57cec5SDimitry Andric if (!hasValidOffset() || !Other.hasValidOffset()) 310b57cec5SDimitry Andric return false; 320b57cec5SDimitry Andric // Initial Offset difference. 330b57cec5SDimitry Andric Off = *Other.Offset - *Offset; 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric if ((Other.Index == Index) && (Other.IsIndexSignExt == IsIndexSignExt)) { 360b57cec5SDimitry Andric // Trivial match. 370b57cec5SDimitry Andric if (Other.Base == Base) 380b57cec5SDimitry Andric return true; 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric // Match GlobalAddresses 411db9f3b2SDimitry Andric if (auto *A = dyn_cast<GlobalAddressSDNode>(Base)) { 420b57cec5SDimitry Andric if (auto *B = dyn_cast<GlobalAddressSDNode>(Other.Base)) 430b57cec5SDimitry Andric if (A->getGlobal() == B->getGlobal()) { 440b57cec5SDimitry Andric Off += B->getOffset() - A->getOffset(); 450b57cec5SDimitry Andric return true; 460b57cec5SDimitry Andric } 470b57cec5SDimitry Andric 481db9f3b2SDimitry Andric return false; 491db9f3b2SDimitry Andric } 501db9f3b2SDimitry Andric 510b57cec5SDimitry Andric // Match Constants 521db9f3b2SDimitry Andric if (auto *A = dyn_cast<ConstantPoolSDNode>(Base)) { 530b57cec5SDimitry Andric if (auto *B = dyn_cast<ConstantPoolSDNode>(Other.Base)) { 540b57cec5SDimitry Andric bool IsMatch = 550b57cec5SDimitry Andric A->isMachineConstantPoolEntry() == B->isMachineConstantPoolEntry(); 560b57cec5SDimitry Andric if (IsMatch) { 570b57cec5SDimitry Andric if (A->isMachineConstantPoolEntry()) 580b57cec5SDimitry Andric IsMatch = A->getMachineCPVal() == B->getMachineCPVal(); 590b57cec5SDimitry Andric else 600b57cec5SDimitry Andric IsMatch = A->getConstVal() == B->getConstVal(); 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric if (IsMatch) { 630b57cec5SDimitry Andric Off += B->getOffset() - A->getOffset(); 640b57cec5SDimitry Andric return true; 650b57cec5SDimitry Andric } 660b57cec5SDimitry Andric } 670b57cec5SDimitry Andric 681db9f3b2SDimitry Andric return false; 691db9f3b2SDimitry Andric } 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric // Match FrameIndexes. 720b57cec5SDimitry Andric if (auto *A = dyn_cast<FrameIndexSDNode>(Base)) 730b57cec5SDimitry Andric if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base)) { 740b57cec5SDimitry Andric // Equal FrameIndexes - offsets are directly comparable. 750b57cec5SDimitry Andric if (A->getIndex() == B->getIndex()) 760b57cec5SDimitry Andric return true; 770b57cec5SDimitry Andric // Non-equal FrameIndexes - If both frame indices are fixed 780b57cec5SDimitry Andric // we know their relative offsets and can compare them. Otherwise 790b57cec5SDimitry Andric // we must be conservative. 801db9f3b2SDimitry Andric const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 810b57cec5SDimitry Andric if (MFI.isFixedObjectIndex(A->getIndex()) && 820b57cec5SDimitry Andric MFI.isFixedObjectIndex(B->getIndex())) { 830b57cec5SDimitry Andric Off += MFI.getObjectOffset(B->getIndex()) - 840b57cec5SDimitry Andric MFI.getObjectOffset(A->getIndex()); 850b57cec5SDimitry Andric return true; 860b57cec5SDimitry Andric } 870b57cec5SDimitry Andric } 880b57cec5SDimitry Andric } 891db9f3b2SDimitry Andric 900b57cec5SDimitry Andric return false; 910b57cec5SDimitry Andric } 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric bool BaseIndexOffset::computeAliasing(const SDNode *Op0, 94*0fca6ea1SDimitry Andric const LocationSize NumBytes0, 950b57cec5SDimitry Andric const SDNode *Op1, 96*0fca6ea1SDimitry Andric const LocationSize NumBytes1, 970b57cec5SDimitry Andric const SelectionDAG &DAG, bool &IsAlias) { 980b57cec5SDimitry Andric BaseIndexOffset BasePtr0 = match(Op0, DAG); 991db9f3b2SDimitry Andric if (!BasePtr0.getBase().getNode()) 1000b57cec5SDimitry Andric return false; 1011db9f3b2SDimitry Andric 1021db9f3b2SDimitry Andric BaseIndexOffset BasePtr1 = match(Op1, DAG); 1031db9f3b2SDimitry Andric if (!BasePtr1.getBase().getNode()) 1041db9f3b2SDimitry Andric return false; 1051db9f3b2SDimitry Andric 1060b57cec5SDimitry Andric int64_t PtrDiff; 107*0fca6ea1SDimitry Andric if (BasePtr0.equalBaseIndex(BasePtr1, DAG, PtrDiff)) { 108e8d8bef9SDimitry Andric // If the size of memory access is unknown, do not use it to analysis. 1090b57cec5SDimitry Andric // BasePtr1 is PtrDiff away from BasePtr0. They alias if none of the 1100b57cec5SDimitry Andric // following situations arise: 111*0fca6ea1SDimitry Andric if (PtrDiff >= 0 && NumBytes0.hasValue() && !NumBytes0.isScalable()) { 1120b57cec5SDimitry Andric // [----BasePtr0----] 1130b57cec5SDimitry Andric // [---BasePtr1--] 1140b57cec5SDimitry Andric // ========PtrDiff========> 115*0fca6ea1SDimitry Andric IsAlias = !(static_cast<int64_t>(NumBytes0.getValue().getFixedValue()) <= 116*0fca6ea1SDimitry Andric PtrDiff); 117e8d8bef9SDimitry Andric return true; 118e8d8bef9SDimitry Andric } 119*0fca6ea1SDimitry Andric if (PtrDiff < 0 && NumBytes1.hasValue() && !NumBytes1.isScalable()) { 1200b57cec5SDimitry Andric // [----BasePtr0----] 1210b57cec5SDimitry Andric // [---BasePtr1--] 1220b57cec5SDimitry Andric // =====(-PtrDiff)====> 123*0fca6ea1SDimitry Andric IsAlias = !((PtrDiff + static_cast<int64_t>( 124*0fca6ea1SDimitry Andric NumBytes1.getValue().getFixedValue())) <= 0); 1250b57cec5SDimitry Andric return true; 1260b57cec5SDimitry Andric } 127e8d8bef9SDimitry Andric return false; 128e8d8bef9SDimitry Andric } 1290b57cec5SDimitry Andric // If both BasePtr0 and BasePtr1 are FrameIndexes, we will not be 1300b57cec5SDimitry Andric // able to calculate their relative offset if at least one arises 1310b57cec5SDimitry Andric // from an alloca. However, these allocas cannot overlap and we 1320b57cec5SDimitry Andric // can infer there is no alias. 1330b57cec5SDimitry Andric if (auto *A = dyn_cast<FrameIndexSDNode>(BasePtr0.getBase())) 1340b57cec5SDimitry Andric if (auto *B = dyn_cast<FrameIndexSDNode>(BasePtr1.getBase())) { 1350b57cec5SDimitry Andric MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 1360b57cec5SDimitry Andric // If the base are the same frame index but the we couldn't find a 1370b57cec5SDimitry Andric // constant offset, (indices are different) be conservative. 1385f757f3fSDimitry Andric if (A->getIndex() != B->getIndex() && (!MFI.isFixedObjectIndex(A->getIndex()) || 1390b57cec5SDimitry Andric !MFI.isFixedObjectIndex(B->getIndex()))) { 1400b57cec5SDimitry Andric IsAlias = false; 1410b57cec5SDimitry Andric return true; 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric } 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric bool IsFI0 = isa<FrameIndexSDNode>(BasePtr0.getBase()); 1460b57cec5SDimitry Andric bool IsFI1 = isa<FrameIndexSDNode>(BasePtr1.getBase()); 1470b57cec5SDimitry Andric bool IsGV0 = isa<GlobalAddressSDNode>(BasePtr0.getBase()); 1480b57cec5SDimitry Andric bool IsGV1 = isa<GlobalAddressSDNode>(BasePtr1.getBase()); 1490b57cec5SDimitry Andric bool IsCV0 = isa<ConstantPoolSDNode>(BasePtr0.getBase()); 1500b57cec5SDimitry Andric bool IsCV1 = isa<ConstantPoolSDNode>(BasePtr1.getBase()); 1510b57cec5SDimitry Andric 152349cc55cSDimitry Andric if ((IsFI0 || IsGV0 || IsCV0) && (IsFI1 || IsGV1 || IsCV1)) { 153349cc55cSDimitry Andric // We can derive NoAlias In case of mismatched base types. 154349cc55cSDimitry Andric if (IsFI0 != IsFI1 || IsGV0 != IsGV1 || IsCV0 != IsCV1) { 1550b57cec5SDimitry Andric IsAlias = false; 1560b57cec5SDimitry Andric return true; 1570b57cec5SDimitry Andric } 158349cc55cSDimitry Andric if (IsGV0 && IsGV1) { 159349cc55cSDimitry Andric auto *GV0 = cast<GlobalAddressSDNode>(BasePtr0.getBase())->getGlobal(); 160349cc55cSDimitry Andric auto *GV1 = cast<GlobalAddressSDNode>(BasePtr1.getBase())->getGlobal(); 161349cc55cSDimitry Andric // It doesn't make sense to access one global value using another globals 162349cc55cSDimitry Andric // values address, so we can assume that there is no aliasing in case of 163349cc55cSDimitry Andric // two different globals (unless we have symbols that may indirectly point 164349cc55cSDimitry Andric // to each other). 165349cc55cSDimitry Andric // FIXME: This is perhaps a bit too defensive. We could try to follow the 166349cc55cSDimitry Andric // chain with aliasee information for GlobalAlias variables to find out if 167349cc55cSDimitry Andric // we indirect symbols may alias or not. 168349cc55cSDimitry Andric if (GV0 != GV1 && !isa<GlobalAlias>(GV0) && !isa<GlobalAlias>(GV1)) { 169349cc55cSDimitry Andric IsAlias = false; 170349cc55cSDimitry Andric return true; 171349cc55cSDimitry Andric } 172349cc55cSDimitry Andric } 173349cc55cSDimitry Andric } 1740b57cec5SDimitry Andric return false; // Cannot determine whether the pointers alias. 1750b57cec5SDimitry Andric } 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric bool BaseIndexOffset::contains(const SelectionDAG &DAG, int64_t BitSize, 1780b57cec5SDimitry Andric const BaseIndexOffset &Other, 1790b57cec5SDimitry Andric int64_t OtherBitSize, int64_t &BitOffset) const { 1800b57cec5SDimitry Andric int64_t Offset; 1810b57cec5SDimitry Andric if (!equalBaseIndex(Other, DAG, Offset)) 1820b57cec5SDimitry Andric return false; 1830b57cec5SDimitry Andric if (Offset >= 0) { 1840b57cec5SDimitry Andric // Other is after *this: 1850b57cec5SDimitry Andric // [-------*this---------] 1860b57cec5SDimitry Andric // [---Other--] 1870b57cec5SDimitry Andric // ==Offset==> 1880b57cec5SDimitry Andric BitOffset = 8 * Offset; 1890b57cec5SDimitry Andric return BitOffset + OtherBitSize <= BitSize; 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric // Other starts strictly before *this, it cannot be fully contained. 1920b57cec5SDimitry Andric // [-------*this---------] 1930b57cec5SDimitry Andric // [--Other--] 1940b57cec5SDimitry Andric return false; 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric /// Parses tree in Ptr for base, index, offset addresses. 1980b57cec5SDimitry Andric static BaseIndexOffset matchLSNode(const LSBaseSDNode *N, 1990b57cec5SDimitry Andric const SelectionDAG &DAG) { 2000b57cec5SDimitry Andric SDValue Ptr = N->getBasePtr(); 2010b57cec5SDimitry Andric 2020b57cec5SDimitry Andric // (((B + I*M) + c)) + c ... 2030b57cec5SDimitry Andric SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr); 2040b57cec5SDimitry Andric SDValue Index = SDValue(); 2050b57cec5SDimitry Andric int64_t Offset = 0; 2060b57cec5SDimitry Andric bool IsIndexSignExt = false; 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric // pre-inc/pre-dec ops are components of EA. 2090b57cec5SDimitry Andric if (N->getAddressingMode() == ISD::PRE_INC) { 2100b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset())) 2110b57cec5SDimitry Andric Offset += C->getSExtValue(); 2120b57cec5SDimitry Andric else // If unknown, give up now. 2130b57cec5SDimitry Andric return BaseIndexOffset(SDValue(), SDValue(), 0, false); 2140b57cec5SDimitry Andric } else if (N->getAddressingMode() == ISD::PRE_DEC) { 2150b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset())) 2160b57cec5SDimitry Andric Offset -= C->getSExtValue(); 2170b57cec5SDimitry Andric else // If unknown, give up now. 2180b57cec5SDimitry Andric return BaseIndexOffset(SDValue(), SDValue(), 0, false); 2190b57cec5SDimitry Andric } 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric // Consume constant adds & ors with appropriate masking. 2220b57cec5SDimitry Andric while (true) { 2230b57cec5SDimitry Andric switch (Base->getOpcode()) { 2240b57cec5SDimitry Andric case ISD::OR: 2250b57cec5SDimitry Andric // Only consider ORs which act as adds. 2260b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) 2270b57cec5SDimitry Andric if (DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) { 2280b57cec5SDimitry Andric Offset += C->getSExtValue(); 2290b57cec5SDimitry Andric Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0)); 2300b57cec5SDimitry Andric continue; 2310b57cec5SDimitry Andric } 2320b57cec5SDimitry Andric break; 2330b57cec5SDimitry Andric case ISD::ADD: 2340b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) { 2350b57cec5SDimitry Andric Offset += C->getSExtValue(); 2360b57cec5SDimitry Andric Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0)); 2370b57cec5SDimitry Andric continue; 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric break; 2400b57cec5SDimitry Andric case ISD::LOAD: 2410b57cec5SDimitry Andric case ISD::STORE: { 2420b57cec5SDimitry Andric auto *LSBase = cast<LSBaseSDNode>(Base.getNode()); 2430b57cec5SDimitry Andric unsigned int IndexResNo = (Base->getOpcode() == ISD::LOAD) ? 1 : 0; 2440b57cec5SDimitry Andric if (LSBase->isIndexed() && Base.getResNo() == IndexResNo) 2450b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(LSBase->getOffset())) { 2460b57cec5SDimitry Andric auto Off = C->getSExtValue(); 2470b57cec5SDimitry Andric if (LSBase->getAddressingMode() == ISD::PRE_DEC || 2480b57cec5SDimitry Andric LSBase->getAddressingMode() == ISD::POST_DEC) 2490b57cec5SDimitry Andric Offset -= Off; 2500b57cec5SDimitry Andric else 2510b57cec5SDimitry Andric Offset += Off; 2520b57cec5SDimitry Andric Base = DAG.getTargetLoweringInfo().unwrapAddress(LSBase->getBasePtr()); 2530b57cec5SDimitry Andric continue; 2540b57cec5SDimitry Andric } 2550b57cec5SDimitry Andric break; 2560b57cec5SDimitry Andric } 2570b57cec5SDimitry Andric } 2580b57cec5SDimitry Andric // If we get here break out of the loop. 2590b57cec5SDimitry Andric break; 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric if (Base->getOpcode() == ISD::ADD) { 2630b57cec5SDimitry Andric // TODO: The following code appears to be needless as it just 2640b57cec5SDimitry Andric // bails on some Ptrs early, reducing the cases where we 2650b57cec5SDimitry Andric // find equivalence. We should be able to remove this. 2660b57cec5SDimitry Andric // Inside a loop the current BASE pointer is calculated using an ADD and a 2670b57cec5SDimitry Andric // MUL instruction. In this case Base is the actual BASE pointer. 2680b57cec5SDimitry Andric // (i64 add (i64 %array_ptr) 2690b57cec5SDimitry Andric // (i64 mul (i64 %induction_var) 2700b57cec5SDimitry Andric // (i64 %element_size))) 2710b57cec5SDimitry Andric if (Base->getOperand(1)->getOpcode() == ISD::MUL) 2720b57cec5SDimitry Andric return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt); 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric // Look at Base + Index + Offset cases. 2750b57cec5SDimitry Andric Index = Base->getOperand(1); 2760b57cec5SDimitry Andric SDValue PotentialBase = Base->getOperand(0); 2770b57cec5SDimitry Andric 2780b57cec5SDimitry Andric // Skip signextends. 2790b57cec5SDimitry Andric if (Index->getOpcode() == ISD::SIGN_EXTEND) { 2800b57cec5SDimitry Andric Index = Index->getOperand(0); 2810b57cec5SDimitry Andric IsIndexSignExt = true; 2820b57cec5SDimitry Andric } 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric // Check if Index Offset pattern 2850b57cec5SDimitry Andric if (Index->getOpcode() != ISD::ADD || 2860b57cec5SDimitry Andric !isa<ConstantSDNode>(Index->getOperand(1))) 2870b57cec5SDimitry Andric return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt); 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue(); 2900b57cec5SDimitry Andric Index = Index->getOperand(0); 2910b57cec5SDimitry Andric if (Index->getOpcode() == ISD::SIGN_EXTEND) { 2920b57cec5SDimitry Andric Index = Index->getOperand(0); 2930b57cec5SDimitry Andric IsIndexSignExt = true; 2940b57cec5SDimitry Andric } else 2950b57cec5SDimitry Andric IsIndexSignExt = false; 2960b57cec5SDimitry Andric Base = PotentialBase; 2970b57cec5SDimitry Andric } 2980b57cec5SDimitry Andric return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt); 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric 3010b57cec5SDimitry Andric BaseIndexOffset BaseIndexOffset::match(const SDNode *N, 3020b57cec5SDimitry Andric const SelectionDAG &DAG) { 3030b57cec5SDimitry Andric if (const auto *LS0 = dyn_cast<LSBaseSDNode>(N)) 3040b57cec5SDimitry Andric return matchLSNode(LS0, DAG); 3050b57cec5SDimitry Andric if (const auto *LN = dyn_cast<LifetimeSDNode>(N)) { 3060b57cec5SDimitry Andric if (LN->hasOffset()) 3070b57cec5SDimitry Andric return BaseIndexOffset(LN->getOperand(1), SDValue(), LN->getOffset(), 3080b57cec5SDimitry Andric false); 3090b57cec5SDimitry Andric return BaseIndexOffset(LN->getOperand(1), SDValue(), false); 3100b57cec5SDimitry Andric } 3110b57cec5SDimitry Andric return BaseIndexOffset(); 3120b57cec5SDimitry Andric } 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric LLVM_DUMP_METHOD void BaseIndexOffset::dump() const { 3170b57cec5SDimitry Andric print(dbgs()); 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric void BaseIndexOffset::print(raw_ostream& OS) const { 3210b57cec5SDimitry Andric OS << "BaseIndexOffset base=["; 3220b57cec5SDimitry Andric Base->print(OS); 3230b57cec5SDimitry Andric OS << "] index=["; 3240b57cec5SDimitry Andric if (Index) 3250b57cec5SDimitry Andric Index->print(OS); 3260b57cec5SDimitry Andric OS << "] offset=" << Offset; 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric #endif 330