2017-09-14 05:15:20 +08:00
|
|
|
//==- llvm/CodeGen/SelectionDAGAddressAnalysis.cpp - DAG Address Analysis --==//
|
2017-06-21 23:40:43 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-06-21 23:40:43 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
|
2020-11-20 08:52:03 +08:00
|
|
|
#include "llvm/Analysis/MemoryLocation.h"
|
2017-06-21 23:40:43 +08:00
|
|
|
#include "llvm/CodeGen/ISDOpcodes.h"
|
2017-06-29 23:48:11 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
2017-09-14 05:15:20 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2017-06-21 23:40:43 +08:00
|
|
|
#include "llvm/CodeGen/SelectionDAG.h"
|
|
|
|
#include "llvm/CodeGen/SelectionDAGNodes.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetLowering.h"
|
2017-09-14 05:15:20 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2019-10-19 09:31:09 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2017-09-14 05:15:20 +08:00
|
|
|
#include <cstdint>
|
2017-06-21 23:40:43 +08:00
|
|
|
|
2017-09-14 05:15:20 +08:00
|
|
|
using namespace llvm;
|
2017-06-21 23:40:43 +08:00
|
|
|
|
2018-10-31 02:26:43 +08:00
|
|
|
bool BaseIndexOffset::equalBaseIndex(const BaseIndexOffset &Other,
|
|
|
|
const SelectionDAG &DAG,
|
|
|
|
int64_t &Off) const {
|
2018-01-09 00:21:35 +08:00
|
|
|
// Conservatively fail if we a match failed..
|
|
|
|
if (!Base.getNode() || !Other.Base.getNode())
|
|
|
|
return false;
|
2019-03-27 22:14:46 +08:00
|
|
|
if (!hasValidOffset() || !Other.hasValidOffset())
|
|
|
|
return false;
|
2017-06-29 23:48:11 +08:00
|
|
|
// Initial Offset difference.
|
2019-03-27 22:14:46 +08:00
|
|
|
Off = *Other.Offset - *Offset;
|
2017-06-21 23:40:43 +08:00
|
|
|
|
2017-06-29 23:48:11 +08:00
|
|
|
if ((Other.Index == Index) && (Other.IsIndexSignExt == IsIndexSignExt)) {
|
|
|
|
// Trivial match.
|
|
|
|
if (Other.Base == Base)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Match GlobalAddresses
|
|
|
|
if (auto *A = dyn_cast<GlobalAddressSDNode>(Base))
|
|
|
|
if (auto *B = dyn_cast<GlobalAddressSDNode>(Other.Base))
|
2017-06-21 23:40:43 +08:00
|
|
|
if (A->getGlobal() == B->getGlobal()) {
|
|
|
|
Off += B->getOffset() - A->getOffset();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-12-23 05:20:55 +08:00
|
|
|
// Match Constants
|
|
|
|
if (auto *A = dyn_cast<ConstantPoolSDNode>(Base))
|
|
|
|
if (auto *B = dyn_cast<ConstantPoolSDNode>(Other.Base)) {
|
|
|
|
bool IsMatch =
|
|
|
|
A->isMachineConstantPoolEntry() == B->isMachineConstantPoolEntry();
|
|
|
|
if (IsMatch) {
|
|
|
|
if (A->isMachineConstantPoolEntry())
|
|
|
|
IsMatch = A->getMachineCPVal() == B->getMachineCPVal();
|
|
|
|
else
|
|
|
|
IsMatch = A->getConstVal() == B->getConstVal();
|
|
|
|
}
|
|
|
|
if (IsMatch) {
|
|
|
|
Off += B->getOffset() - A->getOffset();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-29 23:48:11 +08:00
|
|
|
const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
|
2017-06-21 23:40:43 +08:00
|
|
|
|
2019-02-05 15:36:20 +08:00
|
|
|
// Match FrameIndexes.
|
2017-06-29 23:48:11 +08:00
|
|
|
if (auto *A = dyn_cast<FrameIndexSDNode>(Base))
|
2019-02-05 15:36:20 +08:00
|
|
|
if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base)) {
|
|
|
|
// Equal FrameIndexes - offsets are directly comparable.
|
|
|
|
if (A->getIndex() == B->getIndex())
|
|
|
|
return true;
|
|
|
|
// Non-equal FrameIndexes - If both frame indices are fixed
|
|
|
|
// we know their relative offsets and can compare them. Otherwise
|
|
|
|
// we must be conservative.
|
2017-07-04 10:20:17 +08:00
|
|
|
if (MFI.isFixedObjectIndex(A->getIndex()) &&
|
|
|
|
MFI.isFixedObjectIndex(B->getIndex())) {
|
2017-06-29 23:48:11 +08:00
|
|
|
Off += MFI.getObjectOffset(B->getIndex()) -
|
|
|
|
MFI.getObjectOffset(A->getIndex());
|
|
|
|
return true;
|
|
|
|
}
|
2019-02-05 15:36:20 +08:00
|
|
|
}
|
2017-06-29 23:48:11 +08:00
|
|
|
}
|
2017-06-21 23:40:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-27 22:14:46 +08:00
|
|
|
bool BaseIndexOffset::computeAliasing(const SDNode *Op0,
|
|
|
|
const Optional<int64_t> NumBytes0,
|
|
|
|
const SDNode *Op1,
|
|
|
|
const Optional<int64_t> NumBytes1,
|
2019-02-20 23:45:58 +08:00
|
|
|
const SelectionDAG &DAG, bool &IsAlias) {
|
2019-03-27 22:14:46 +08:00
|
|
|
|
|
|
|
BaseIndexOffset BasePtr0 = match(Op0, DAG);
|
|
|
|
BaseIndexOffset BasePtr1 = match(Op1, DAG);
|
|
|
|
|
2019-02-20 23:45:58 +08:00
|
|
|
if (!(BasePtr0.getBase().getNode() && BasePtr1.getBase().getNode()))
|
|
|
|
return false;
|
|
|
|
int64_t PtrDiff;
|
2019-03-27 22:14:46 +08:00
|
|
|
if (NumBytes0.hasValue() && NumBytes1.hasValue() &&
|
|
|
|
BasePtr0.equalBaseIndex(BasePtr1, DAG, PtrDiff)) {
|
2020-11-20 08:52:03 +08:00
|
|
|
// If the size of memory access is unknown, do not use it to analysis.
|
|
|
|
// One example of unknown size memory access is to load/store scalable
|
|
|
|
// vector objects on the stack.
|
2019-02-20 23:45:58 +08:00
|
|
|
// BasePtr1 is PtrDiff away from BasePtr0. They alias if none of the
|
|
|
|
// following situations arise:
|
2020-11-20 08:52:03 +08:00
|
|
|
if (PtrDiff >= 0 &&
|
|
|
|
*NumBytes0 != static_cast<int64_t>(MemoryLocation::UnknownSize)) {
|
|
|
|
// [----BasePtr0----]
|
|
|
|
// [---BasePtr1--]
|
|
|
|
// ========PtrDiff========>
|
|
|
|
IsAlias = !(*NumBytes0 <= PtrDiff);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (PtrDiff < 0 &&
|
|
|
|
*NumBytes1 != static_cast<int64_t>(MemoryLocation::UnknownSize)) {
|
|
|
|
// [----BasePtr0----]
|
|
|
|
// [---BasePtr1--]
|
|
|
|
// =====(-PtrDiff)====>
|
|
|
|
IsAlias = !((PtrDiff + *NumBytes1) <= 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-02-20 23:45:58 +08:00
|
|
|
}
|
|
|
|
// If both BasePtr0 and BasePtr1 are FrameIndexes, we will not be
|
|
|
|
// able to calculate their relative offset if at least one arises
|
|
|
|
// from an alloca. However, these allocas cannot overlap and we
|
|
|
|
// can infer there is no alias.
|
|
|
|
if (auto *A = dyn_cast<FrameIndexSDNode>(BasePtr0.getBase()))
|
|
|
|
if (auto *B = dyn_cast<FrameIndexSDNode>(BasePtr1.getBase())) {
|
|
|
|
MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
|
|
|
|
// If the base are the same frame index but the we couldn't find a
|
|
|
|
// constant offset, (indices are different) be conservative.
|
|
|
|
if (A != B && (!MFI.isFixedObjectIndex(A->getIndex()) ||
|
|
|
|
!MFI.isFixedObjectIndex(B->getIndex()))) {
|
|
|
|
IsAlias = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsFI0 = isa<FrameIndexSDNode>(BasePtr0.getBase());
|
|
|
|
bool IsFI1 = isa<FrameIndexSDNode>(BasePtr1.getBase());
|
|
|
|
bool IsGV0 = isa<GlobalAddressSDNode>(BasePtr0.getBase());
|
|
|
|
bool IsGV1 = isa<GlobalAddressSDNode>(BasePtr1.getBase());
|
|
|
|
bool IsCV0 = isa<ConstantPoolSDNode>(BasePtr0.getBase());
|
|
|
|
bool IsCV1 = isa<ConstantPoolSDNode>(BasePtr1.getBase());
|
|
|
|
|
|
|
|
// If of mismatched base types or checkable indices we can check
|
|
|
|
// they do not alias.
|
|
|
|
if ((BasePtr0.getIndex() == BasePtr1.getIndex() || (IsFI0 != IsFI1) ||
|
|
|
|
(IsGV0 != IsGV1) || (IsCV0 != IsCV1)) &&
|
|
|
|
(IsFI0 || IsGV0 || IsCV0) && (IsFI1 || IsGV1 || IsCV1)) {
|
|
|
|
IsAlias = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false; // Cannot determine whether the pointers alias.
|
|
|
|
}
|
|
|
|
|
2019-02-26 23:02:32 +08:00
|
|
|
bool BaseIndexOffset::contains(const SelectionDAG &DAG, int64_t BitSize,
|
|
|
|
const BaseIndexOffset &Other,
|
|
|
|
int64_t OtherBitSize, int64_t &BitOffset) const {
|
|
|
|
int64_t Offset;
|
2019-02-20 23:45:58 +08:00
|
|
|
if (!equalBaseIndex(Other, DAG, Offset))
|
|
|
|
return false;
|
|
|
|
if (Offset >= 0) {
|
|
|
|
// Other is after *this:
|
|
|
|
// [-------*this---------]
|
|
|
|
// [---Other--]
|
|
|
|
// ==Offset==>
|
2019-02-26 23:02:32 +08:00
|
|
|
BitOffset = 8 * Offset;
|
|
|
|
return BitOffset + OtherBitSize <= BitSize;
|
2019-02-20 23:45:58 +08:00
|
|
|
}
|
|
|
|
// Other starts strictly before *this, it cannot be fully contained.
|
|
|
|
// [-------*this---------]
|
|
|
|
// [--Other--]
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-21 23:40:43 +08:00
|
|
|
/// Parses tree in Ptr for base, index, offset addresses.
|
2019-03-27 22:14:46 +08:00
|
|
|
static BaseIndexOffset matchLSNode(const LSBaseSDNode *N,
|
|
|
|
const SelectionDAG &DAG) {
|
2018-01-09 00:21:35 +08:00
|
|
|
SDValue Ptr = N->getBasePtr();
|
|
|
|
|
2017-06-21 23:40:43 +08:00
|
|
|
// (((B + I*M) + c)) + c ...
|
2017-08-11 21:21:35 +08:00
|
|
|
SDValue Base = DAG.getTargetLoweringInfo().unwrapAddress(Ptr);
|
2017-06-21 23:40:43 +08:00
|
|
|
SDValue Index = SDValue();
|
|
|
|
int64_t Offset = 0;
|
|
|
|
bool IsIndexSignExt = false;
|
|
|
|
|
2018-01-09 00:21:35 +08:00
|
|
|
// pre-inc/pre-dec ops are components of EA.
|
|
|
|
if (N->getAddressingMode() == ISD::PRE_INC) {
|
|
|
|
if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
|
|
|
|
Offset += C->getSExtValue();
|
|
|
|
else // If unknown, give up now.
|
|
|
|
return BaseIndexOffset(SDValue(), SDValue(), 0, false);
|
|
|
|
} else if (N->getAddressingMode() == ISD::PRE_DEC) {
|
|
|
|
if (auto *C = dyn_cast<ConstantSDNode>(N->getOffset()))
|
|
|
|
Offset -= C->getSExtValue();
|
|
|
|
else // If unknown, give up now.
|
|
|
|
return BaseIndexOffset(SDValue(), SDValue(), 0, false);
|
|
|
|
}
|
|
|
|
|
2017-07-05 09:21:23 +08:00
|
|
|
// Consume constant adds & ors with appropriate masking.
|
2018-01-27 00:51:27 +08:00
|
|
|
while (true) {
|
|
|
|
switch (Base->getOpcode()) {
|
|
|
|
case ISD::OR:
|
2017-07-05 09:21:23 +08:00
|
|
|
// Only consider ORs which act as adds.
|
2018-01-27 00:51:27 +08:00
|
|
|
if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1)))
|
|
|
|
if (DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue())) {
|
|
|
|
Offset += C->getSExtValue();
|
2018-11-27 04:16:33 +08:00
|
|
|
Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0));
|
2018-01-27 00:51:27 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ISD::ADD:
|
|
|
|
if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) {
|
|
|
|
Offset += C->getSExtValue();
|
2018-11-27 04:16:33 +08:00
|
|
|
Base = DAG.getTargetLoweringInfo().unwrapAddress(Base->getOperand(0));
|
2018-01-27 00:51:27 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ISD::LOAD:
|
|
|
|
case ISD::STORE: {
|
|
|
|
auto *LSBase = cast<LSBaseSDNode>(Base.getNode());
|
|
|
|
unsigned int IndexResNo = (Base->getOpcode() == ISD::LOAD) ? 1 : 0;
|
|
|
|
if (LSBase->isIndexed() && Base.getResNo() == IndexResNo)
|
|
|
|
if (auto *C = dyn_cast<ConstantSDNode>(LSBase->getOffset())) {
|
|
|
|
auto Off = C->getSExtValue();
|
|
|
|
if (LSBase->getAddressingMode() == ISD::PRE_DEC ||
|
|
|
|
LSBase->getAddressingMode() == ISD::POST_DEC)
|
|
|
|
Offset -= Off;
|
|
|
|
else
|
|
|
|
Offset += Off;
|
2018-11-27 04:16:33 +08:00
|
|
|
Base = DAG.getTargetLoweringInfo().unwrapAddress(LSBase->getBasePtr());
|
2018-01-27 00:51:27 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2017-07-05 09:21:23 +08:00
|
|
|
}
|
2018-01-27 00:51:27 +08:00
|
|
|
// If we get here break out of the loop.
|
2017-07-05 09:21:23 +08:00
|
|
|
break;
|
2017-06-21 23:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Base->getOpcode() == ISD::ADD) {
|
|
|
|
// TODO: The following code appears to be needless as it just
|
|
|
|
// bails on some Ptrs early, reducing the cases where we
|
|
|
|
// find equivalence. We should be able to remove this.
|
|
|
|
// Inside a loop the current BASE pointer is calculated using an ADD and a
|
|
|
|
// MUL instruction. In this case Base is the actual BASE pointer.
|
|
|
|
// (i64 add (i64 %array_ptr)
|
|
|
|
// (i64 mul (i64 %induction_var)
|
|
|
|
// (i64 %element_size)))
|
|
|
|
if (Base->getOperand(1)->getOpcode() == ISD::MUL)
|
|
|
|
return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
|
|
|
|
|
|
|
|
// Look at Base + Index + Offset cases.
|
|
|
|
Index = Base->getOperand(1);
|
|
|
|
SDValue PotentialBase = Base->getOperand(0);
|
|
|
|
|
|
|
|
// Skip signextends.
|
|
|
|
if (Index->getOpcode() == ISD::SIGN_EXTEND) {
|
|
|
|
Index = Index->getOperand(0);
|
|
|
|
IsIndexSignExt = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if Index Offset pattern
|
|
|
|
if (Index->getOpcode() != ISD::ADD ||
|
|
|
|
!isa<ConstantSDNode>(Index->getOperand(1)))
|
|
|
|
return BaseIndexOffset(PotentialBase, Index, Offset, IsIndexSignExt);
|
|
|
|
|
|
|
|
Offset += cast<ConstantSDNode>(Index->getOperand(1))->getSExtValue();
|
|
|
|
Index = Index->getOperand(0);
|
|
|
|
if (Index->getOpcode() == ISD::SIGN_EXTEND) {
|
|
|
|
Index = Index->getOperand(0);
|
|
|
|
IsIndexSignExt = true;
|
|
|
|
} else
|
|
|
|
IsIndexSignExt = false;
|
|
|
|
Base = PotentialBase;
|
|
|
|
}
|
|
|
|
return BaseIndexOffset(Base, Index, Offset, IsIndexSignExt);
|
|
|
|
}
|
2019-02-04 17:30:43 +08:00
|
|
|
|
2019-03-27 22:14:46 +08:00
|
|
|
BaseIndexOffset BaseIndexOffset::match(const SDNode *N,
|
|
|
|
const SelectionDAG &DAG) {
|
|
|
|
if (const auto *LS0 = dyn_cast<LSBaseSDNode>(N))
|
|
|
|
return matchLSNode(LS0, DAG);
|
|
|
|
if (const auto *LN = dyn_cast<LifetimeSDNode>(N)) {
|
|
|
|
if (LN->hasOffset())
|
|
|
|
return BaseIndexOffset(LN->getOperand(1), SDValue(), LN->getOffset(),
|
|
|
|
false);
|
|
|
|
return BaseIndexOffset(LN->getOperand(1), SDValue(), false);
|
|
|
|
}
|
|
|
|
return BaseIndexOffset();
|
|
|
|
}
|
2019-02-04 17:30:43 +08:00
|
|
|
|
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
|
|
|
|
|
|
|
LLVM_DUMP_METHOD void BaseIndexOffset::dump() const {
|
|
|
|
print(dbgs());
|
|
|
|
}
|
|
|
|
|
|
|
|
void BaseIndexOffset::print(raw_ostream& OS) const {
|
|
|
|
OS << "BaseIndexOffset base=[";
|
|
|
|
Base->print(OS);
|
|
|
|
OS << "] index=[";
|
|
|
|
if (Index)
|
|
|
|
Index->print(OS);
|
|
|
|
OS << "] offset=" << Offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|