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" 100b57cec5SDimitry Andric #include "llvm/CodeGen/ISDOpcodes.h" 110b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 120b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 130b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAG.h" 140b57cec5SDimitry Andric #include "llvm/CodeGen/SelectionDAGNodes.h" 150b57cec5SDimitry Andric #include "llvm/CodeGen/TargetLowering.h" 160b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 17*8bcb0991SDimitry Andric #include "llvm/Support/Debug.h" 180b57cec5SDimitry Andric #include <cstdint> 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric using namespace llvm; 210b57cec5SDimitry Andric 220b57cec5SDimitry Andric bool BaseIndexOffset::equalBaseIndex(const BaseIndexOffset &Other, 230b57cec5SDimitry Andric const SelectionDAG &DAG, 240b57cec5SDimitry Andric int64_t &Off) const { 250b57cec5SDimitry Andric // Conservatively fail if we a match failed.. 260b57cec5SDimitry Andric if (!Base.getNode() || !Other.Base.getNode()) 270b57cec5SDimitry Andric return false; 280b57cec5SDimitry Andric if (!hasValidOffset() || !Other.hasValidOffset()) 290b57cec5SDimitry Andric return false; 300b57cec5SDimitry Andric // Initial Offset difference. 310b57cec5SDimitry Andric Off = *Other.Offset - *Offset; 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric if ((Other.Index == Index) && (Other.IsIndexSignExt == IsIndexSignExt)) { 340b57cec5SDimitry Andric // Trivial match. 350b57cec5SDimitry Andric if (Other.Base == Base) 360b57cec5SDimitry Andric return true; 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric // Match GlobalAddresses 390b57cec5SDimitry Andric if (auto *A = dyn_cast<GlobalAddressSDNode>(Base)) 400b57cec5SDimitry Andric if (auto *B = dyn_cast<GlobalAddressSDNode>(Other.Base)) 410b57cec5SDimitry Andric if (A->getGlobal() == B->getGlobal()) { 420b57cec5SDimitry Andric Off += B->getOffset() - A->getOffset(); 430b57cec5SDimitry Andric return true; 440b57cec5SDimitry Andric } 450b57cec5SDimitry Andric 460b57cec5SDimitry Andric // Match Constants 470b57cec5SDimitry Andric if (auto *A = dyn_cast<ConstantPoolSDNode>(Base)) 480b57cec5SDimitry Andric if (auto *B = dyn_cast<ConstantPoolSDNode>(Other.Base)) { 490b57cec5SDimitry Andric bool IsMatch = 500b57cec5SDimitry Andric A->isMachineConstantPoolEntry() == B->isMachineConstantPoolEntry(); 510b57cec5SDimitry Andric if (IsMatch) { 520b57cec5SDimitry Andric if (A->isMachineConstantPoolEntry()) 530b57cec5SDimitry Andric IsMatch = A->getMachineCPVal() == B->getMachineCPVal(); 540b57cec5SDimitry Andric else 550b57cec5SDimitry Andric IsMatch = A->getConstVal() == B->getConstVal(); 560b57cec5SDimitry Andric } 570b57cec5SDimitry Andric if (IsMatch) { 580b57cec5SDimitry Andric Off += B->getOffset() - A->getOffset(); 590b57cec5SDimitry Andric return true; 600b57cec5SDimitry Andric } 610b57cec5SDimitry Andric } 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 640b57cec5SDimitry Andric 650b57cec5SDimitry Andric // Match FrameIndexes. 660b57cec5SDimitry Andric if (auto *A = dyn_cast<FrameIndexSDNode>(Base)) 670b57cec5SDimitry Andric if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base)) { 680b57cec5SDimitry Andric // Equal FrameIndexes - offsets are directly comparable. 690b57cec5SDimitry Andric if (A->getIndex() == B->getIndex()) 700b57cec5SDimitry Andric return true; 710b57cec5SDimitry Andric // Non-equal FrameIndexes - If both frame indices are fixed 720b57cec5SDimitry Andric // we know their relative offsets and can compare them. Otherwise 730b57cec5SDimitry Andric // we must be conservative. 740b57cec5SDimitry Andric if (MFI.isFixedObjectIndex(A->getIndex()) && 750b57cec5SDimitry Andric MFI.isFixedObjectIndex(B->getIndex())) { 760b57cec5SDimitry Andric Off += MFI.getObjectOffset(B->getIndex()) - 770b57cec5SDimitry Andric MFI.getObjectOffset(A->getIndex()); 780b57cec5SDimitry Andric return true; 790b57cec5SDimitry Andric } 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric } 820b57cec5SDimitry Andric return false; 830b57cec5SDimitry Andric } 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric bool BaseIndexOffset::computeAliasing(const SDNode *Op0, 860b57cec5SDimitry Andric const Optional<int64_t> NumBytes0, 870b57cec5SDimitry Andric const SDNode *Op1, 880b57cec5SDimitry Andric const Optional<int64_t> NumBytes1, 890b57cec5SDimitry Andric const SelectionDAG &DAG, bool &IsAlias) { 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric BaseIndexOffset BasePtr0 = match(Op0, DAG); 920b57cec5SDimitry Andric BaseIndexOffset BasePtr1 = match(Op1, DAG); 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric if (!(BasePtr0.getBase().getNode() && BasePtr1.getBase().getNode())) 950b57cec5SDimitry Andric return false; 960b57cec5SDimitry Andric int64_t PtrDiff; 970b57cec5SDimitry Andric if (NumBytes0.hasValue() && NumBytes1.hasValue() && 980b57cec5SDimitry Andric BasePtr0.equalBaseIndex(BasePtr1, DAG, PtrDiff)) { 990b57cec5SDimitry Andric // BasePtr1 is PtrDiff away from BasePtr0. They alias if none of the 1000b57cec5SDimitry Andric // following situations arise: 1010b57cec5SDimitry Andric IsAlias = !( 1020b57cec5SDimitry Andric // [----BasePtr0----] 1030b57cec5SDimitry Andric // [---BasePtr1--] 1040b57cec5SDimitry Andric // ========PtrDiff========> 1050b57cec5SDimitry Andric (*NumBytes0 <= PtrDiff) || 1060b57cec5SDimitry Andric // [----BasePtr0----] 1070b57cec5SDimitry Andric // [---BasePtr1--] 1080b57cec5SDimitry Andric // =====(-PtrDiff)====> 1090b57cec5SDimitry Andric (PtrDiff + *NumBytes1 <= 0)); // i.e. *NumBytes1 < -PtrDiff. 1100b57cec5SDimitry Andric return true; 1110b57cec5SDimitry Andric } 1120b57cec5SDimitry Andric // If both BasePtr0 and BasePtr1 are FrameIndexes, we will not be 1130b57cec5SDimitry Andric // able to calculate their relative offset if at least one arises 1140b57cec5SDimitry Andric // from an alloca. However, these allocas cannot overlap and we 1150b57cec5SDimitry Andric // can infer there is no alias. 1160b57cec5SDimitry Andric if (auto *A = dyn_cast<FrameIndexSDNode>(BasePtr0.getBase())) 1170b57cec5SDimitry Andric if (auto *B = dyn_cast<FrameIndexSDNode>(BasePtr1.getBase())) { 1180b57cec5SDimitry Andric MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo(); 1190b57cec5SDimitry Andric // If the base are the same frame index but the we couldn't find a 1200b57cec5SDimitry Andric // constant offset, (indices are different) be conservative. 1210b57cec5SDimitry Andric if (A != B && (!MFI.isFixedObjectIndex(A->getIndex()) || 1220b57cec5SDimitry Andric !MFI.isFixedObjectIndex(B->getIndex()))) { 1230b57cec5SDimitry Andric IsAlias = false; 1240b57cec5SDimitry Andric return true; 1250b57cec5SDimitry Andric } 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric bool IsFI0 = isa<FrameIndexSDNode>(BasePtr0.getBase()); 1290b57cec5SDimitry Andric bool IsFI1 = isa<FrameIndexSDNode>(BasePtr1.getBase()); 1300b57cec5SDimitry Andric bool IsGV0 = isa<GlobalAddressSDNode>(BasePtr0.getBase()); 1310b57cec5SDimitry Andric bool IsGV1 = isa<GlobalAddressSDNode>(BasePtr1.getBase()); 1320b57cec5SDimitry Andric bool IsCV0 = isa<ConstantPoolSDNode>(BasePtr0.getBase()); 1330b57cec5SDimitry Andric bool IsCV1 = isa<ConstantPoolSDNode>(BasePtr1.getBase()); 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric // If of mismatched base types or checkable indices we can check 1360b57cec5SDimitry Andric // they do not alias. 1370b57cec5SDimitry Andric if ((BasePtr0.getIndex() == BasePtr1.getIndex() || (IsFI0 != IsFI1) || 1380b57cec5SDimitry Andric (IsGV0 != IsGV1) || (IsCV0 != IsCV1)) && 1390b57cec5SDimitry Andric (IsFI0 || IsGV0 || IsCV0) && (IsFI1 || IsGV1 || IsCV1)) { 1400b57cec5SDimitry Andric IsAlias = false; 1410b57cec5SDimitry Andric return true; 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric return false; // Cannot determine whether the pointers alias. 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric bool BaseIndexOffset::contains(const SelectionDAG &DAG, int64_t BitSize, 1470b57cec5SDimitry Andric const BaseIndexOffset &Other, 1480b57cec5SDimitry Andric int64_t OtherBitSize, int64_t &BitOffset) const { 1490b57cec5SDimitry Andric int64_t Offset; 1500b57cec5SDimitry Andric if (!equalBaseIndex(Other, DAG, Offset)) 1510b57cec5SDimitry Andric return false; 1520b57cec5SDimitry Andric if (Offset >= 0) { 1530b57cec5SDimitry Andric // Other is after *this: 1540b57cec5SDimitry Andric // [-------*this---------] 1550b57cec5SDimitry Andric // [---Other--] 1560b57cec5SDimitry Andric // ==Offset==> 1570b57cec5SDimitry Andric BitOffset = 8 * Offset; 1580b57cec5SDimitry Andric return BitOffset + OtherBitSize <= BitSize; 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric // Other starts strictly before *this, it cannot be fully contained. 1610b57cec5SDimitry Andric // [-------*this---------] 1620b57cec5SDimitry Andric // [--Other--] 1630b57cec5SDimitry Andric return false; 1640b57cec5SDimitry Andric } 1650b57cec5SDimitry Andric 1660b57cec5SDimitry Andric /// Parses tree in Ptr for base, index, offset addresses. 1670b57cec5SDimitry Andric static BaseIndexOffset matchLSNode(const LSBaseSDNode *N, 1680b57cec5SDimitry Andric const SelectionDAG &DAG) { 1690b57cec5SDimitry Andric SDValue Ptr = N->getBasePtr(); 1700b57cec5SDimitry Andric 1710b57cec5SDimitry Andric // (((B + I*M) + c)) + c ... 1720b57cec5SDimitry Andric SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr); 1730b57cec5SDimitry Andric SDValue Index = SDValue(); 1740b57cec5SDimitry Andric int64_t Offset = 0; 1750b57cec5SDimitry Andric bool IsIndexSignExt = false; 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric // pre-inc/pre-dec ops are components of EA. 1780b57cec5SDimitry Andric if (N->getAddressingMode() == ISD::PRE_INC) { 1790b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset())) 1800b57cec5SDimitry Andric Offset += C->getSExtValue(); 1810b57cec5SDimitry Andric else // If unknown, give up now. 1820b57cec5SDimitry Andric return BaseIndexOffset(SDValue(), SDValue(), 0, false); 1830b57cec5SDimitry Andric } else if (N->getAddressingMode() == ISD::PRE_DEC) { 1840b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset())) 1850b57cec5SDimitry Andric Offset -= C->getSExtValue(); 1860b57cec5SDimitry Andric else // If unknown, give up now. 1870b57cec5SDimitry Andric return BaseIndexOffset(SDValue(), SDValue(), 0, false); 1880b57cec5SDimitry Andric } 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric // Consume constant adds & ors with appropriate masking. 1910b57cec5SDimitry Andric while (true) { 1920b57cec5SDimitry Andric switch (Base->getOpcode()) { 1930b57cec5SDimitry Andric case ISD::OR: 1940b57cec5SDimitry Andric // Only consider ORs which act as adds. 1950b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) 1960b57cec5SDimitry Andric if (DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) { 1970b57cec5SDimitry Andric Offset += C->getSExtValue(); 1980b57cec5SDimitry Andric Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0)); 1990b57cec5SDimitry Andric continue; 2000b57cec5SDimitry Andric } 2010b57cec5SDimitry Andric break; 2020b57cec5SDimitry Andric case ISD::ADD: 2030b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) { 2040b57cec5SDimitry Andric Offset += C->getSExtValue(); 2050b57cec5SDimitry Andric Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0)); 2060b57cec5SDimitry Andric continue; 2070b57cec5SDimitry Andric } 2080b57cec5SDimitry Andric break; 2090b57cec5SDimitry Andric case ISD::LOAD: 2100b57cec5SDimitry Andric case ISD::STORE: { 2110b57cec5SDimitry Andric auto *LSBase = cast<LSBaseSDNode>(Base.getNode()); 2120b57cec5SDimitry Andric unsigned int IndexResNo = (Base->getOpcode() == ISD::LOAD) ? 1 : 0; 2130b57cec5SDimitry Andric if (LSBase->isIndexed() && Base.getResNo() == IndexResNo) 2140b57cec5SDimitry Andric if (auto *C = dyn_cast<ConstantSDNode>(LSBase->getOffset())) { 2150b57cec5SDimitry Andric auto Off = C->getSExtValue(); 2160b57cec5SDimitry Andric if (LSBase->getAddressingMode() == ISD::PRE_DEC || 2170b57cec5SDimitry Andric LSBase->getAddressingMode() == ISD::POST_DEC) 2180b57cec5SDimitry Andric Offset -= Off; 2190b57cec5SDimitry Andric else 2200b57cec5SDimitry Andric Offset += Off; 2210b57cec5SDimitry Andric Base = DAG.getTargetLoweringInfo().unwrapAddress(LSBase->getBasePtr()); 2220b57cec5SDimitry Andric continue; 2230b57cec5SDimitry Andric } 2240b57cec5SDimitry Andric break; 2250b57cec5SDimitry Andric } 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric // If we get here break out of the loop. 2280b57cec5SDimitry Andric break; 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric if (Base->getOpcode() == ISD::ADD) { 2320b57cec5SDimitry Andric // TODO: The following code appears to be needless as it just 2330b57cec5SDimitry Andric // bails on some Ptrs early, reducing the cases where we 2340b57cec5SDimitry Andric // find equivalence. We should be able to remove this. 2350b57cec5SDimitry Andric // Inside a loop the current BASE pointer is calculated using an ADD and a 2360b57cec5SDimitry Andric // MUL instruction. In this case Base is the actual BASE pointer. 2370b57cec5SDimitry Andric // (i64 add (i64 %array_ptr) 2380b57cec5SDimitry Andric // (i64 mul (i64 %induction_var) 2390b57cec5SDimitry Andric // (i64 %element_size))) 2400b57cec5SDimitry Andric if (Base->getOperand(1)->getOpcode() == ISD::MUL) 2410b57cec5SDimitry Andric return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt); 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric // Look at Base + Index + Offset cases. 2440b57cec5SDimitry Andric Index = Base->getOperand(1); 2450b57cec5SDimitry Andric SDValue PotentialBase = Base->getOperand(0); 2460b57cec5SDimitry Andric 2470b57cec5SDimitry Andric // Skip signextends. 2480b57cec5SDimitry Andric if (Index->getOpcode() == ISD::SIGN_EXTEND) { 2490b57cec5SDimitry Andric Index = Index->getOperand(0); 2500b57cec5SDimitry Andric IsIndexSignExt = true; 2510b57cec5SDimitry Andric } 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric // Check if Index Offset pattern 2540b57cec5SDimitry Andric if (Index->getOpcode() != ISD::ADD || 2550b57cec5SDimitry Andric !isa<ConstantSDNode>(Index->getOperand(1))) 2560b57cec5SDimitry Andric return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt); 2570b57cec5SDimitry Andric 2580b57cec5SDimitry Andric Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue(); 2590b57cec5SDimitry Andric Index = Index->getOperand(0); 2600b57cec5SDimitry Andric if (Index->getOpcode() == ISD::SIGN_EXTEND) { 2610b57cec5SDimitry Andric Index = Index->getOperand(0); 2620b57cec5SDimitry Andric IsIndexSignExt = true; 2630b57cec5SDimitry Andric } else 2640b57cec5SDimitry Andric IsIndexSignExt = false; 2650b57cec5SDimitry Andric Base = PotentialBase; 2660b57cec5SDimitry Andric } 2670b57cec5SDimitry Andric return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt); 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric BaseIndexOffset BaseIndexOffset::match(const SDNode *N, 2710b57cec5SDimitry Andric const SelectionDAG &DAG) { 2720b57cec5SDimitry Andric if (const auto *LS0 = dyn_cast<LSBaseSDNode>(N)) 2730b57cec5SDimitry Andric return matchLSNode(LS0, DAG); 2740b57cec5SDimitry Andric if (const auto *LN = dyn_cast<LifetimeSDNode>(N)) { 2750b57cec5SDimitry Andric if (LN->hasOffset()) 2760b57cec5SDimitry Andric return BaseIndexOffset(LN->getOperand(1), SDValue(), LN->getOffset(), 2770b57cec5SDimitry Andric false); 2780b57cec5SDimitry Andric return BaseIndexOffset(LN->getOperand(1), SDValue(), false); 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric return BaseIndexOffset(); 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric LLVM_DUMP_METHOD void BaseIndexOffset::dump() const { 2860b57cec5SDimitry Andric print(dbgs()); 2870b57cec5SDimitry Andric } 2880b57cec5SDimitry Andric 2890b57cec5SDimitry Andric void BaseIndexOffset::print(raw_ostream& OS) const { 2900b57cec5SDimitry Andric OS << "BaseIndexOffset base=["; 2910b57cec5SDimitry Andric Base->print(OS); 2920b57cec5SDimitry Andric OS << "] index=["; 2930b57cec5SDimitry Andric if (Index) 2940b57cec5SDimitry Andric Index->print(OS); 2950b57cec5SDimitry Andric OS << "] offset=" << Offset; 2960b57cec5SDimitry Andric } 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric #endif 299