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