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" 17*349cc55cSDimitry 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 410b57cec5SDimitry 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 480b57cec5SDimitry Andric // Match Constants 490b57cec5SDimitry Andric if (auto *A = dyn_cast<ConstantPoolSDNode>(Base)) 500b57cec5SDimitry Andric if (auto *B = dyn_cast<ConstantPoolSDNode>(Other.Base)) { 510b57cec5SDimitry Andric bool IsMatch = 520b57cec5SDimitry Andric A->isMachineConstantPoolEntry() == B->isMachineConstantPoolEntry(); 530b57cec5SDimitry Andric if (IsMatch) { 540b57cec5SDimitry Andric if (A->isMachineConstantPoolEntry()) 550b57cec5SDimitry Andric IsMatch = A->getMachineCPVal() == B->getMachineCPVal(); 560b57cec5SDimitry Andric else 570b57cec5SDimitry Andric IsMatch = A->getConstVal() == B->getConstVal(); 580b57cec5SDimitry Andric } 590b57cec5SDimitry Andric if (IsMatch) { 600b57cec5SDimitry Andric Off += B->getOffset() - A->getOffset(); 610b57cec5SDimitry Andric return true; 620b57cec5SDimitry Andric } 630b57cec5SDimitry Andric } 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric // Match FrameIndexes. 680b57cec5SDimitry Andric if (auto *A = dyn_cast<FrameIndexSDNode>(Base)) 690b57cec5SDimitry Andric if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base)) { 700b57cec5SDimitry Andric // Equal FrameIndexes - offsets are directly comparable. 710b57cec5SDimitry Andric if (A->getIndex() == B->getIndex()) 720b57cec5SDimitry Andric return true; 730b57cec5SDimitry Andric // Non-equal FrameIndexes - If both frame indices are fixed 740b57cec5SDimitry Andric // we know their relative offsets and can compare them. Otherwise 750b57cec5SDimitry Andric // we must be conservative. 760b57cec5SDimitry Andric if (MFI.isFixedObjectIndex(A->getIndex()) && 770b57cec5SDimitry Andric MFI.isFixedObjectIndex(B->getIndex())) { 780b57cec5SDimitry Andric Off += MFI.getObjectOffset(B->getIndex()) - 790b57cec5SDimitry Andric MFI.getObjectOffset(A->getIndex()); 800b57cec5SDimitry Andric return true; 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric } 830b57cec5SDimitry Andric } 840b57cec5SDimitry Andric return false; 850b57cec5SDimitry Andric } 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric bool BaseIndexOffset::computeAliasing(const SDNode *Op0, 880b57cec5SDimitry Andric const Optional<int64_t> NumBytes0, 890b57cec5SDimitry Andric const SDNode *Op1, 900b57cec5SDimitry Andric const Optional<int64_t> NumBytes1, 910b57cec5SDimitry Andric const SelectionDAG &DAG, bool &IsAlias) { 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric BaseIndexOffset BasePtr0 = match(Op0, DAG); 940b57cec5SDimitry Andric BaseIndexOffset BasePtr1 = match(Op1, DAG); 950b57cec5SDimitry Andric 960b57cec5SDimitry Andric if (!(BasePtr0.getBase().getNode() && BasePtr1.getBase().getNode())) 970b57cec5SDimitry Andric return false; 980b57cec5SDimitry Andric int64_t PtrDiff; 990b57cec5SDimitry Andric if (NumBytes0.hasValue() && NumBytes1.hasValue() && 1000b57cec5SDimitry Andric BasePtr0.equalBaseIndex(BasePtr1, DAG, PtrDiff)) { 101e8d8bef9SDimitry Andric // If the size of memory access is unknown, do not use it to analysis. 102e8d8bef9SDimitry Andric // One example of unknown size memory access is to load/store scalable 103e8d8bef9SDimitry Andric // vector objects on the stack. 1040b57cec5SDimitry Andric // BasePtr1 is PtrDiff away from BasePtr0. They alias if none of the 1050b57cec5SDimitry Andric // following situations arise: 106e8d8bef9SDimitry Andric if (PtrDiff >= 0 && 107e8d8bef9SDimitry Andric *NumBytes0 != static_cast<int64_t>(MemoryLocation::UnknownSize)) { 1080b57cec5SDimitry Andric // [----BasePtr0----] 1090b57cec5SDimitry Andric // [---BasePtr1--] 1100b57cec5SDimitry Andric // ========PtrDiff========> 111e8d8bef9SDimitry Andric IsAlias = !(*NumBytes0 <= PtrDiff); 112e8d8bef9SDimitry Andric return true; 113e8d8bef9SDimitry Andric } 114e8d8bef9SDimitry Andric if (PtrDiff < 0 && 115e8d8bef9SDimitry Andric *NumBytes1 != static_cast<int64_t>(MemoryLocation::UnknownSize)) { 1160b57cec5SDimitry Andric // [----BasePtr0----] 1170b57cec5SDimitry Andric // [---BasePtr1--] 1180b57cec5SDimitry Andric // =====(-PtrDiff)====> 119e8d8bef9SDimitry Andric IsAlias = !((PtrDiff + *NumBytes1) <= 0); 1200b57cec5SDimitry Andric return true; 1210b57cec5SDimitry Andric } 122e8d8bef9SDimitry Andric return false; 123e8d8bef9SDimitry Andric } 1240b57cec5SDimitry Andric // If both BasePtr0 and BasePtr1 are FrameIndexes, we will not be 1250b57cec5SDimitry Andric // able to calculate their relative offset if at least one arises 1260b57cec5SDimitry Andric // from an alloca. However, these allocas cannot overlap and we 1270b57cec5SDimitry Andric // can infer there is no alias. 1280b57cec5SDimitry Andric if (auto *A = dyn_cast<FrameIndexSDNode>(BasePtr0.getBase())) 1290b57cec5SDimitry Andric if (auto *B = dyn_cast<FrameIndexSDNode>(BasePtr1.getBase())) { 1300b57cec5SDimitry Andric MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 1310b57cec5SDimitry Andric // If the base are the same frame index but the we couldn't find a 1320b57cec5SDimitry Andric // constant offset, (indices are different) be conservative. 1330b57cec5SDimitry Andric if (A != B && (!MFI.isFixedObjectIndex(A->getIndex()) || 1340b57cec5SDimitry Andric !MFI.isFixedObjectIndex(B->getIndex()))) { 1350b57cec5SDimitry Andric IsAlias = false; 1360b57cec5SDimitry Andric return true; 1370b57cec5SDimitry Andric } 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 1400b57cec5SDimitry Andric bool IsFI0 = isa<FrameIndexSDNode>(BasePtr0.getBase()); 1410b57cec5SDimitry Andric bool IsFI1 = isa<FrameIndexSDNode>(BasePtr1.getBase()); 1420b57cec5SDimitry Andric bool IsGV0 = isa<GlobalAddressSDNode>(BasePtr0.getBase()); 1430b57cec5SDimitry Andric bool IsGV1 = isa<GlobalAddressSDNode>(BasePtr1.getBase()); 1440b57cec5SDimitry Andric bool IsCV0 = isa<ConstantPoolSDNode>(BasePtr0.getBase()); 1450b57cec5SDimitry Andric bool IsCV1 = isa<ConstantPoolSDNode>(BasePtr1.getBase()); 1460b57cec5SDimitry Andric 147*349cc55cSDimitry Andric if ((IsFI0 || IsGV0 || IsCV0) && (IsFI1 || IsGV1 || IsCV1)) { 148*349cc55cSDimitry Andric // We can derive NoAlias In case of mismatched base types. 149*349cc55cSDimitry Andric if (IsFI0 != IsFI1 || IsGV0 != IsGV1 || IsCV0 != IsCV1) { 1500b57cec5SDimitry Andric IsAlias = false; 1510b57cec5SDimitry Andric return true; 1520b57cec5SDimitry Andric } 153*349cc55cSDimitry Andric if (IsGV0 && IsGV1) { 154*349cc55cSDimitry Andric auto *GV0 = cast<GlobalAddressSDNode>(BasePtr0.getBase())->getGlobal(); 155*349cc55cSDimitry Andric auto *GV1 = cast<GlobalAddressSDNode>(BasePtr1.getBase())->getGlobal(); 156*349cc55cSDimitry Andric // It doesn't make sense to access one global value using another globals 157*349cc55cSDimitry Andric // values address, so we can assume that there is no aliasing in case of 158*349cc55cSDimitry Andric // two different globals (unless we have symbols that may indirectly point 159*349cc55cSDimitry Andric // to each other). 160*349cc55cSDimitry Andric // FIXME: This is perhaps a bit too defensive. We could try to follow the 161*349cc55cSDimitry Andric // chain with aliasee information for GlobalAlias variables to find out if 162*349cc55cSDimitry Andric // we indirect symbols may alias or not. 163*349cc55cSDimitry Andric if (GV0 != GV1 && !isa<GlobalAlias>(GV0) && !isa<GlobalAlias>(GV1)) { 164*349cc55cSDimitry Andric IsAlias = false; 165*349cc55cSDimitry Andric return true; 166*349cc55cSDimitry Andric } 167*349cc55cSDimitry Andric } 168*349cc55cSDimitry Andric } 1690b57cec5SDimitry Andric return false; // Cannot determine whether the pointers alias. 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric bool BaseIndexOffset::contains(const SelectionDAG &DAG, int64_t BitSize, 1730b57cec5SDimitry Andric const BaseIndexOffset &Other, 1740b57cec5SDimitry Andric int64_t OtherBitSize, int64_t &BitOffset) const { 1750b57cec5SDimitry Andric int64_t Offset; 1760b57cec5SDimitry Andric if (!equalBaseIndex(Other, DAG, Offset)) 1770b57cec5SDimitry Andric return false; 1780b57cec5SDimitry Andric if (Offset >= 0) { 1790b57cec5SDimitry Andric // Other is after *this: 1800b57cec5SDimitry Andric // [-------*this---------] 1810b57cec5SDimitry Andric // [---Other--] 1820b57cec5SDimitry Andric // ==Offset==> 1830b57cec5SDimitry Andric BitOffset = 8 * Offset; 1840b57cec5SDimitry Andric return BitOffset + OtherBitSize <= BitSize; 1850b57cec5SDimitry Andric } 1860b57cec5SDimitry Andric // Other starts strictly before *this, it cannot be fully contained. 1870b57cec5SDimitry Andric // [-------*this---------] 1880b57cec5SDimitry Andric // [--Other--] 1890b57cec5SDimitry Andric return false; 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric /// Parses tree in Ptr for base, index, offset addresses. 1930b57cec5SDimitry Andric static BaseIndexOffset matchLSNode(const LSBaseSDNode *N, 1940b57cec5SDimitry Andric const SelectionDAG &DAG) { 1950b57cec5SDimitry Andric SDValue Ptr = N->getBasePtr(); 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric // (((B + I*M) + c)) + c ... 1980b57cec5SDimitry Andric SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr); 1990b57cec5SDimitry Andric SDValue Index = SDValue(); 2000b57cec5SDimitry Andric int64_t Offset = 0; 2010b57cec5SDimitry Andric bool IsIndexSignExt = false; 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric // pre-inc/pre-dec ops are components of EA. 2040b57cec5SDimitry Andric if (N->getAddressingMode() == ISD::PRE_INC) { 2050b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset())) 2060b57cec5SDimitry Andric Offset += C->getSExtValue(); 2070b57cec5SDimitry Andric else // If unknown, give up now. 2080b57cec5SDimitry Andric return BaseIndexOffset(SDValue(), SDValue(), 0, false); 2090b57cec5SDimitry Andric } else if (N->getAddressingMode() == ISD::PRE_DEC) { 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 } 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric // Consume constant adds & ors with appropriate masking. 2170b57cec5SDimitry Andric while (true) { 2180b57cec5SDimitry Andric switch (Base->getOpcode()) { 2190b57cec5SDimitry Andric case ISD::OR: 2200b57cec5SDimitry Andric // Only consider ORs which act as adds. 2210b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) 2220b57cec5SDimitry Andric if (DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) { 2230b57cec5SDimitry Andric Offset += C->getSExtValue(); 2240b57cec5SDimitry Andric Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0)); 2250b57cec5SDimitry Andric continue; 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric break; 2280b57cec5SDimitry Andric case ISD::ADD: 2290b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) { 2300b57cec5SDimitry Andric Offset += C->getSExtValue(); 2310b57cec5SDimitry Andric Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0)); 2320b57cec5SDimitry Andric continue; 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric break; 2350b57cec5SDimitry Andric case ISD::LOAD: 2360b57cec5SDimitry Andric case ISD::STORE: { 2370b57cec5SDimitry Andric auto *LSBase = cast<LSBaseSDNode>(Base.getNode()); 2380b57cec5SDimitry Andric unsigned int IndexResNo = (Base->getOpcode() == ISD::LOAD) ? 1 : 0; 2390b57cec5SDimitry Andric if (LSBase->isIndexed() && Base.getResNo() == IndexResNo) 2400b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(LSBase->getOffset())) { 2410b57cec5SDimitry Andric auto Off = C->getSExtValue(); 2420b57cec5SDimitry Andric if (LSBase->getAddressingMode() == ISD::PRE_DEC || 2430b57cec5SDimitry Andric LSBase->getAddressingMode() == ISD::POST_DEC) 2440b57cec5SDimitry Andric Offset -= Off; 2450b57cec5SDimitry Andric else 2460b57cec5SDimitry Andric Offset += Off; 2470b57cec5SDimitry Andric Base = DAG.getTargetLoweringInfo().unwrapAddress(LSBase->getBasePtr()); 2480b57cec5SDimitry Andric continue; 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric break; 2510b57cec5SDimitry Andric } 2520b57cec5SDimitry Andric } 2530b57cec5SDimitry Andric // If we get here break out of the loop. 2540b57cec5SDimitry Andric break; 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric if (Base->getOpcode() == ISD::ADD) { 2580b57cec5SDimitry Andric // TODO: The following code appears to be needless as it just 2590b57cec5SDimitry Andric // bails on some Ptrs early, reducing the cases where we 2600b57cec5SDimitry Andric // find equivalence. We should be able to remove this. 2610b57cec5SDimitry Andric // Inside a loop the current BASE pointer is calculated using an ADD and a 2620b57cec5SDimitry Andric // MUL instruction. In this case Base is the actual BASE pointer. 2630b57cec5SDimitry Andric // (i64 add (i64 %array_ptr) 2640b57cec5SDimitry Andric // (i64 mul (i64 %induction_var) 2650b57cec5SDimitry Andric // (i64 %element_size))) 2660b57cec5SDimitry Andric if (Base->getOperand(1)->getOpcode() == ISD::MUL) 2670b57cec5SDimitry Andric return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt); 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric // Look at Base + Index + Offset cases. 2700b57cec5SDimitry Andric Index = Base->getOperand(1); 2710b57cec5SDimitry Andric SDValue PotentialBase = Base->getOperand(0); 2720b57cec5SDimitry Andric 2730b57cec5SDimitry Andric // Skip signextends. 2740b57cec5SDimitry Andric if (Index->getOpcode() == ISD::SIGN_EXTEND) { 2750b57cec5SDimitry Andric Index = Index->getOperand(0); 2760b57cec5SDimitry Andric IsIndexSignExt = true; 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric // Check if Index Offset pattern 2800b57cec5SDimitry Andric if (Index->getOpcode() != ISD::ADD || 2810b57cec5SDimitry Andric !isa<ConstantSDNode>(Index->getOperand(1))) 2820b57cec5SDimitry Andric return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt); 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue(); 2850b57cec5SDimitry Andric Index = Index->getOperand(0); 2860b57cec5SDimitry Andric if (Index->getOpcode() == ISD::SIGN_EXTEND) { 2870b57cec5SDimitry Andric Index = Index->getOperand(0); 2880b57cec5SDimitry Andric IsIndexSignExt = true; 2890b57cec5SDimitry Andric } else 2900b57cec5SDimitry Andric IsIndexSignExt = false; 2910b57cec5SDimitry Andric Base = PotentialBase; 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt); 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric BaseIndexOffset BaseIndexOffset::match(const SDNode *N, 2970b57cec5SDimitry Andric const SelectionDAG &DAG) { 2980b57cec5SDimitry Andric if (const auto *LS0 = dyn_cast<LSBaseSDNode>(N)) 2990b57cec5SDimitry Andric return matchLSNode(LS0, DAG); 3000b57cec5SDimitry Andric if (const auto *LN = dyn_cast<LifetimeSDNode>(N)) { 3010b57cec5SDimitry Andric if (LN->hasOffset()) 3020b57cec5SDimitry Andric return BaseIndexOffset(LN->getOperand(1), SDValue(), LN->getOffset(), 3030b57cec5SDimitry Andric false); 3040b57cec5SDimitry Andric return BaseIndexOffset(LN->getOperand(1), SDValue(), false); 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric return BaseIndexOffset(); 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andric LLVM_DUMP_METHOD void BaseIndexOffset::dump() const { 3120b57cec5SDimitry Andric print(dbgs()); 3130b57cec5SDimitry Andric } 3140b57cec5SDimitry Andric 3150b57cec5SDimitry Andric void BaseIndexOffset::print(raw_ostream& OS) const { 3160b57cec5SDimitry Andric OS << "BaseIndexOffset base=["; 3170b57cec5SDimitry Andric Base->print(OS); 3180b57cec5SDimitry Andric OS << "] index=["; 3190b57cec5SDimitry Andric if (Index) 3200b57cec5SDimitry Andric Index->print(OS); 3210b57cec5SDimitry Andric OS << "] offset=" << Offset; 3220b57cec5SDimitry Andric } 3230b57cec5SDimitry Andric 3240b57cec5SDimitry Andric #endif 325