Skip to content

Commit d11fbf0

Browse files
committed
[TSAR, Memory] Bug fixes for DefinedMemoryPass.
1 parent 54e3e21 commit d11fbf0

File tree

1 file changed

+38
-11
lines changed

1 file changed

+38
-11
lines changed

lib/Analysis/Memory/DefinedMemory.cpp

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include <llvm/IR/Function.h>
4848
#include <llvm/IR/Instructions.h>
4949
#include <llvm/IR/InstIterator.h>
50+
#include <llvm/IR/Operator.h>
5051
#include <llvm/Support/Debug.h>
5152
#include <functional>
5253

@@ -276,8 +277,7 @@ std::pair<MemoryLocationRange, bool> aggregate(
276277
auto ArrayPtr = LocInfo.first;
277278
if (!ArrayPtr || !ArrayPtr->isDelinearized() || !LocInfo.second->isValid()) {
278279
LLVM_DEBUG(dbgs() << "[AGGREGATE] Failed to delinearize location.\n");
279-
ResLoc.Kind = LocKind::Default;
280-
return std::make_pair(ResLoc, true);
280+
return std::make_pair(Loc, true);
281281
}
282282
LLVM_DEBUG(dbgs() << "[AGGREGATE] Array info: " << *ArrayPtr->getBase() <<
283283
", IsAddress: " << ArrayPtr->isAddressOfVariable() << ".\n");
@@ -292,7 +292,6 @@ std::pair<MemoryLocationRange, bool> aggregate(
292292
auto ArraySizeInfo = arraySize(ArrayType);
293293
if (ArraySizeInfo == std::make_tuple(0, 1, ArrayType) &&
294294
ArrayPtr->getNumberOfDims() != 1) {
295-
LLVM_DEBUG(dbgs() << "[AGGREGATE] Failed to get array size.\n");
296295
ResLoc.Kind = LocKind::NonCollapsable;
297296
return std::make_pair(ResLoc, true);
298297
}
@@ -550,7 +549,11 @@ std::pair<MemoryLocationRange, bool> aggregate(
550549
ResLoc.DimList.push_back(DimInfo);
551550
ResLoc.Kind = LocKind::Collapsed;
552551
} else {
553-
break;
552+
DimInfo.Start = DimInfo.End = SCEV;
553+
DimInfo.Step = SE->getOne(Int64Ty);
554+
ResLoc.DimList.push_back(DimInfo);
555+
ResLoc.Kind = LocKind::Collapsed;
556+
++ConstMatchCount;
554557
}
555558
++DimensionN;
556559
}
@@ -615,7 +618,7 @@ class AddKnownAccessFunctor :
615618
auto AR = aliasRelation(this->mAA, this->mDL, mLoc, ALoc);
616619
if (AR.template is_any<trait::CoverAlias, trait::CoincideAlias>()) {
617620
MemoryLocationRange Loc(ALoc);
618-
if (mEM->getParent() == &EM)
621+
if (mEM->isDescendantOf(EM) && mEM != &EM)
619622
Loc.Kind |= MemoryLocationRange::LocKind::Auxiliary;
620623
addMust(Loc);
621624
} else if (AR.template is<trait::ContainedAlias>()) {
@@ -644,14 +647,14 @@ class AddKnownAccessFunctor :
644647
} else {
645648
Range = MemoryLocationRange{ALoc.Ptr, 0, mLoc.Size , ALoc.AATags};
646649
}
647-
if (mEM->getParent() == &EM)
650+
if (mEM->isDescendantOf(EM) && mEM != &EM)
648651
Range.Kind |= MemoryLocationRange::LocKind::Auxiliary;
649652
if (this->mDU.hasDef(Range))
650653
continue;
651654
addMust(Range);
652655
} else if (!AR.template is<trait::NoAlias>()) {
653656
MemoryLocationRange Loc(ALoc);
654-
if (mEM->getParent() == &EM)
657+
if (mEM->isDescendantOf(EM) && mEM != &EM)
655658
Loc.Kind |= MemoryLocationRange::LocKind::Auxiliary;
656659
addMay(Loc);
657660
}
@@ -801,8 +804,19 @@ void DataFlowTraits<ReachDFFwk*>::initialize(
801804
if (isMemoryMarkerIntrinsic(II->getIntrinsicID()) ||
802805
isDbgInfoIntrinsic(II->getIntrinsicID()))
803806
continue;
804-
if (I.getType() && I.getType()->isPointerTy())
805-
DU->addAddressAccess(&I);
807+
if (I.getType() && I.getType()->isPointerTy()) {
808+
if (isa<GEPOperator>(I)) {
809+
auto IsOnlyGEPUsers{true};
810+
for_each_user_insts(I,
811+
[&IsOnlyGEPUsers](auto *U) {
812+
IsOnlyGEPUsers &= isa<GEPOperator>(U);
813+
});
814+
if (!IsOnlyGEPUsers)
815+
DU->addAddressAccess(&I);
816+
} else {
817+
DU->addAddressAccess(&I);
818+
}
819+
}
806820
auto isAddressAccess = [&F](const Value *V) {
807821
if (const ConstantPointerNull *CPN = dyn_cast<ConstantPointerNull>(V)) {
808822
if (!NullPointerIsDefined(F, CPN->getType()->getAddressSpace()))
@@ -817,6 +831,13 @@ void DataFlowTraits<ReachDFFwk*>::initialize(
817831
} else if (auto *GV{dyn_cast<GlobalValue>(V)};
818832
GV && GV->hasGlobalUnnamedAddr()) {
819833
return false;
834+
} else if (isa<GEPOperator>(V)) {
835+
auto IsOnlyGEPUsers{true};
836+
for_each_user_insts(const_cast<Value &>(*V),
837+
[&IsOnlyGEPUsers](auto *U) {
838+
IsOnlyGEPUsers &= isa<GEPOperator>(U);
839+
});
840+
return !IsOnlyGEPUsers;
820841
}
821842
return true;
822843
};
@@ -1135,11 +1156,15 @@ void ReachDFFwk::collapse(DFRegion *R) {
11351156
const LocationSet &LS) -> bool {
11361157
bool AreAllCollapsedOrAuxiliary = true;
11371158
bool HasCollapsed = false;
1159+
bool HasOtherGV = false;
11381160
auto *GV{dyn_cast<GlobalValue>(getUnderlyingObject(Loc.Ptr, 0))};
11391161
for (auto &OtherLoc : LS) {
1162+
if (&Loc == &OtherLoc)
1163+
continue;
11401164
auto *OtherGV{dyn_cast<GlobalValue>(
11411165
getUnderlyingObject(OtherLoc.Ptr, 0))};
11421166
if (GV == OtherGV) {
1167+
HasOtherGV = true;
11431168
if ((OtherLoc.Kind & MemoryLocationRange::LocKind::Collapsed) &&
11441169
!(OtherLoc.Kind & MemoryLocationRange::LocKind::Auxiliary))
11451170
HasCollapsed = true;
@@ -1150,7 +1175,7 @@ void ReachDFFwk::collapse(DFRegion *R) {
11501175
}
11511176
}
11521177
}
1153-
if (AreAllCollapsedOrAuxiliary && HasCollapsed)
1178+
if (AreAllCollapsedOrAuxiliary && HasCollapsed || !HasOtherGV)
11541179
return true;
11551180
return false;
11561181
};
@@ -1228,7 +1253,9 @@ void ReachDFFwk::collapse(DFRegion *R) {
12281253
}
12291254
}
12301255
}
1231-
if (AreAllCollapsed && SameUnderlyingObjCount > 1)
1256+
if (AreAllCollapsed && SameUnderlyingObjCount > 1 ||
1257+
Loc.Kind == MemoryLocationRange::LocKind::Auxiliary &&
1258+
SameUnderlyingObjCount == 1 && AreAllCollapsed)
12321259
SkipExcessUse = true;
12331260
} else if (Loc.Kind & MemoryLocationRange::LocKind::Auxiliary) {
12341261
SkipExcessUse = MaySkipAuxiliaryLocation(Loc, NewUses);

0 commit comments

Comments
 (0)