2017-09-14 05:15:20 +08:00
|
|
|
//==- llvm/CodeGen/SelectionDAGAddressAnalysis.cpp - DAG Address Analysis --==//
|
2017-06-21 23:40:43 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/SelectionDAGAddressAnalysis.h"
|
|
|
|
#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-09-14 05:15:20 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2017-08-11 21:21:35 +08:00
|
|
|
#include "llvm/Target/TargetLowering.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
|
|
|
|
|
|
|
bool BaseIndexOffset::equalBaseIndex(BaseIndexOffset &Other,
|
|
|
|
const SelectionDAG &DAG, int64_t &Off) {
|
2017-06-29 23:48:11 +08:00
|
|
|
// Initial Offset difference.
|
2017-06-21 23:40:43 +08:00
|
|
|
Off = Other.Offset - Offset;
|
|
|
|
|
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-06-29 23:48:11 +08:00
|
|
|
const MachineFrameInfo &MFI = DAG.getMachineFunction().getFrameInfo();
|
2017-06-21 23:40:43 +08:00
|
|
|
|
2017-07-04 10:20:17 +08:00
|
|
|
// Match non-equal FrameIndexes - If both frame indices are fixed
|
|
|
|
// we know their relative offsets and can compare them. Otherwise
|
|
|
|
// we must be conservative.
|
2017-06-29 23:48:11 +08:00
|
|
|
if (auto *A = dyn_cast<FrameIndexSDNode>(Base))
|
|
|
|
if (auto *B = dyn_cast<FrameIndexSDNode>(Other.Base))
|
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;
|
|
|
|
}
|
|
|
|
}
|
2017-06-21 23:40:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Parses tree in Ptr for base, index, offset addresses.
|
2017-06-29 23:48:11 +08:00
|
|
|
BaseIndexOffset BaseIndexOffset::match(SDValue Ptr, const SelectionDAG &DAG) {
|
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;
|
|
|
|
|
2017-07-05 09:21:23 +08:00
|
|
|
// Consume constant adds & ors with appropriate masking.
|
|
|
|
while (Base->getOpcode() == ISD::ADD || Base->getOpcode() == ISD::OR) {
|
|
|
|
if (auto *C = dyn_cast<ConstantSDNode>(Base->getOperand(1))) {
|
|
|
|
// Only consider ORs which act as adds.
|
|
|
|
if (Base->getOpcode() == ISD::OR &&
|
|
|
|
!DAG.MaskedValueIsZero(Base->getOperand(0), C->getAPIntValue()))
|
|
|
|
break;
|
|
|
|
Offset += C->getSExtValue();
|
|
|
|
Base = Base->getOperand(0);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|