Skip to content

Commit

Permalink
Rename WeakVH to WeakTrackingVH; NFC
Browse files Browse the repository at this point in the history
This PR pulls the upstream change, Rename WeakVH to WeakTrackingVH; NFC (llvm/llvm-project@e6bca0e), into DXC.

Here's the summary of the change:

>  I plan to use WeakVH to mean "nulls itself out on deletion, but does not track RAUW" in a subsequent commit.
>
>  Reviewers: dblaikie, davide
>
>  Reviewed By: davide
>
>  Subscribers: arsenm, mehdi_amini, mcrosier, mzolotukhin, jfb, llvm-commits, nhaehnle
>
>  Differential Revision: https://reviews.llvm.org/D32266

This is part 3 of the fix for #6659.
  • Loading branch information
lizhengxing committed Jun 12, 2024
1 parent a44c88e commit a776a77
Show file tree
Hide file tree
Showing 38 changed files with 137 additions and 131 deletions.
6 changes: 3 additions & 3 deletions docs/ProgrammersManual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1416,9 +1416,9 @@ llvm/IR/ValueMap.h
ValueMap is a wrapper around a :ref:`DenseMap <dss_densemap>` mapping
``Value*``\ s (or subclasses) to another type. When a Value is deleted or
RAUW'ed, ValueMap will update itself so the new version of the key is mapped to
the same value, just as if the key were a WeakVH. You can configure exactly how
this happens, and what else happens on these two events, by passing a ``Config``
parameter to the ValueMap template.
the same value, just as if the key were a WeakTrackingVH. You can configure
exactly how this happens, and what else happens on these two events, by passing
a ``Config`` parameter to the ValueMap template.

.. _dss_intervalmap:

Expand Down
4 changes: 2 additions & 2 deletions include/llvm/Analysis/AssumptionCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AssumptionCache {

/// \brief Vector of weak value handles to calls of the @llvm.assume
/// intrinsic.
SmallVector<WeakVH, 4> AssumeHandles;
SmallVector<WeakTrackingVH, 4> AssumeHandles;

/// \brief Flag tracking whether we have scanned the function yet.
///
Expand Down Expand Up @@ -86,7 +86,7 @@ class AssumptionCache {
/// FIXME: We should replace this with pointee_iterator<filter_iterator<...>>
/// when we can write that to filter out the null values. Then caller code
/// will become simpler.
MutableArrayRef<WeakVH> assumptions() {
MutableArrayRef<WeakTrackingVH> assumptions() {
if (!Scanned)
scanFunction();
return AssumeHandles;
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/Analysis/CallGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class CallGraphNode {
public:
/// \brief A pair of the calling instruction (a call or invoke)
/// and the call graph node being called.
typedef std::pair<WeakVH, CallGraphNode *> CallRecord;
typedef std::pair<WeakTrackingVH, CallGraphNode *> CallRecord;

public:
typedef std::vector<CallRecord> CalledFunctionsVector;
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/Analysis/DxilValueCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct DxilValueCache : public ImmutablePass {
void allUsesReplacedWith(Value *) override { setValPtr(nullptr); }
};
struct ValueEntry {
WeakVH Value;
WeakTrackingVH Value;
ValueVH Self;
ValueEntry() : Value(nullptr), Self(nullptr) {}
inline void Set(llvm::Value *Key, llvm::Value *V) {
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/Analysis/IVUsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class IVStrideUse : public CallbackVH, public ilist_node<IVStrideUse> {

/// OperandValToReplace - The Value of the operand in the user instruction
/// that this IVStrideUse is representing.
WeakVH OperandValToReplace;
WeakTrackingVH OperandValToReplace;

/// PostIncLoops - The set of loops for which Expr has been adjusted to
/// use post-inc mode. This corresponds with SCEVExpander's post-inc concept.
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/Analysis/MemoryBuiltins.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class ObjectSizeOffsetEvaluator
: public InstVisitor<ObjectSizeOffsetEvaluator, SizeOffsetEvalType> {

typedef IRBuilder<true, TargetFolder> BuilderTy;
typedef std::pair<WeakVH, WeakVH> WeakEvalType;
typedef std::pair<WeakTrackingVH, WeakTrackingVH> WeakEvalType;
typedef DenseMap<const Value*, WeakEvalType> CacheMapTy;
typedef SmallPtrSet<const Value*, 8> PtrSetTy;

Expand Down
2 changes: 1 addition & 1 deletion include/llvm/Analysis/ScalarEvolutionExpander.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ namespace llvm {
/// \brief replace congruent phis with their most canonical
/// representative. Return the number of phis eliminated.
unsigned replaceCongruentIVs(Loop *L, const DominatorTree *DT,
SmallVectorImpl<WeakVH> &DeadInsts,
SmallVectorImpl<WeakTrackingVH> &DeadInsts,
const TargetTransformInfo *TTI = nullptr);

/// \brief Insert code to directly compute the specified SCEV expression
Expand Down
34 changes: 19 additions & 15 deletions include/llvm/IR/ValueHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ValueHandleBase {
///
/// This is to avoid having a vtable for the light-weight handle pointers. The
/// fully general Callback version does have a vtable.
enum HandleBaseKind { Assert, Callback, Weak };
enum HandleBaseKind { Assert, Callback, WeakTracking };

private:
PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
Expand Down Expand Up @@ -141,14 +141,14 @@ class ValueHandleBase {
/// is useful for advisory sorts of information, but should not be used as the
/// key of a map (since the map would have to rearrange itself when the pointer
/// changes).
class WeakVH : public ValueHandleBase {
class WeakTrackingVH : public ValueHandleBase {
public:
WeakVH() : ValueHandleBase(Weak) {}
WeakVH(Value *P) : ValueHandleBase(Weak, P) {}
WeakVH(const WeakVH &RHS)
: ValueHandleBase(Weak, RHS) {}
WeakTrackingVH() : ValueHandleBase(WeakTracking) {}
WeakTrackingVH(Value *P) : ValueHandleBase(WeakTracking, P) {}
WeakTrackingVH(const WeakTrackingVH &RHS)
: ValueHandleBase(WeakTracking, RHS) {}

WeakVH &operator=(const WeakVH &RHS) = default;
WeakTrackingVH &operator=(const WeakTrackingVH &RHS) = default;

Value *operator=(Value *RHS) {
return ValueHandleBase::operator=(RHS);
Expand All @@ -166,15 +166,17 @@ class WeakVH : public ValueHandleBase {
}
};

// Specialize simplify_type to allow WeakVH to participate in
// Specialize simplify_type to allow WeakTrackingVH to participate in
// dyn_cast, isa, etc.
template <> struct simplify_type<WeakVH> {
template <> struct simplify_type<WeakTrackingVH> {
typedef Value *SimpleType;
static SimpleType getSimplifiedValue(WeakVH &WVH) { return WVH; }
static SimpleType getSimplifiedValue(WeakTrackingVH &WVH) { return WVH; }
};
template <> struct simplify_type<const WeakVH> {
template <> struct simplify_type<const WeakTrackingVH> {
typedef Value *SimpleType;
static SimpleType getSimplifiedValue(const WeakVH &WVH) { return WVH; }
static SimpleType getSimplifiedValue(const WeakTrackingVH &WVH) {
return WVH;
}
};

/// \brief Value handle that asserts if the Value is deleted.
Expand Down Expand Up @@ -291,7 +293,7 @@ struct isPodLike<AssertingVH<T> > {
/// Assigning a value to a TrackingVH is always allowed, even if said TrackingVH
/// no longer points to a valid value.
template <typename ValueTy> class TrackingVH {
WeakVH InnerHandle;
WeakTrackingVH InnerHandle;

public:
ValueTy *getValPtr() const {
Expand Down Expand Up @@ -396,7 +398,8 @@ class CallbackVH : public ValueHandleBase {
///
/// Called when this->getValPtr() is destroyed, inside ~Value(), so you
/// may call any non-virtual Value method on getValPtr(), but no subclass
/// methods. If WeakVH were implemented as a CallbackVH, it would use this
/// methods. If WeakTrackingVH were implemented as a CallbackVH, it would use
/// this
/// method to call setValPtr(NULL). AssertingVH would use this method to
/// cause an assertion failure.
///
Expand All @@ -407,7 +410,8 @@ class CallbackVH : public ValueHandleBase {
/// \brief Callback for Value RAUW.
///
/// Called when this->getValPtr()->replaceAllUsesWith(new_value) is called,
/// _before_ any of the uses have actually been replaced. If WeakVH were
/// _before_ any of the uses have actually been replaced. If WeakTrackingVH
/// were
/// implemented as a CallbackVH, it would use this method to call
/// setValPtr(new_value). AssertingVH would do nothing in this method.
virtual void allUsesReplacedWith(Value *) {}
Expand Down
2 changes: 1 addition & 1 deletion include/llvm/Transforms/Utils/Cloning.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ class InlineFunctionInfo {

/// InlinedCalls - InlineFunction fills this in with callsites that were
/// inlined from the callee. This is only filled in if CG is non-null.
SmallVector<WeakVH, 8> InlinedCalls;
SmallVector<WeakTrackingVH, 8> InlinedCalls;

void reset() {
StaticAllocas.clear();
Expand Down
5 changes: 3 additions & 2 deletions include/llvm/Transforms/Utils/SimplifyIndVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ class IVVisitor {
/// simplifyUsersOfIV - Simplify instructions that use this induction variable
/// by using ScalarEvolution to analyze the IV's recurrence.
bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, LPPassManager *LPM,
SmallVectorImpl<WeakVH> &Dead, IVVisitor *V = nullptr);
SmallVectorImpl<WeakTrackingVH> &Dead,
IVVisitor *V = nullptr);

/// SimplifyLoopIVs - Simplify users of induction variables within this
/// loop. This does not actually change or add IVs.
bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, LPPassManager *LPM,
SmallVectorImpl<WeakVH> &Dead);
SmallVectorImpl<WeakTrackingVH> &Dead);

} // namespace llvm

Expand Down
2 changes: 1 addition & 1 deletion include/llvm/Transforms/Utils/ValueMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
namespace llvm {
class Value;
class Instruction;
typedef ValueMap<const Value *, WeakVH> ValueToValueMapTy;
typedef ValueMap<const Value *, WeakTrackingVH> ValueToValueMapTy;

/// ValueMapTypeRemapper - This is a class that can be implemented by clients
/// to remap types when cloning constants and instructions.
Expand Down
5 changes: 3 additions & 2 deletions lib/Analysis/IPA/CallGraphSCCPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ bool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC,
// Get the set of call sites currently in the function.
for (CallGraphNode::iterator I = CGN->begin(), E = CGN->end(); I != E; ) {
// If this call site is null, then the function pass deleted the call
// entirely and the WeakVH nulled it out.
// entirely and the WeakTrackingVH nulled it out.
if (!I->first ||
// If we've already seen this call site, then the FunctionPass RAUW'd
// one call with another, which resulted in two "uses" in the edge
Expand Down Expand Up @@ -362,7 +362,8 @@ bool CGPassManager::RefreshCallGraph(CallGraphSCC &CurSCC,
DevirtualizedCall = true;

// After scanning this function, if we still have entries in callsites, then
// they are dangling pointers. WeakVH should save us for this, so abort if
// they are dangling pointers. WeakTrackingVH should save us for this, so
// abort if
// this happens.
assert(CallSites.empty() && "Dangling pointers found in call sites map");

Expand Down
7 changes: 4 additions & 3 deletions lib/Analysis/ScalarEvolutionExpander.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1689,9 +1689,10 @@ SCEVExpander::getOrInsertCanonicalInductionVariable(const Loop *L,
///
/// This does not depend on any SCEVExpander state but should be used in
/// the same context that SCEVExpander is used.
unsigned SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT,
SmallVectorImpl<WeakVH> &DeadInsts,
const TargetTransformInfo *TTI) {
unsigned
SCEVExpander::replaceCongruentIVs(Loop *L, const DominatorTree *DT,
SmallVectorImpl<WeakTrackingVH> &DeadInsts,
const TargetTransformInfo *TTI) {
// Find integer phis in order of increasing width.
SmallVector<PHINode*, 8> Phis;
for (BasicBlock::iterator I = L->getHeader()->begin();
Expand Down
4 changes: 2 additions & 2 deletions lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ enum {
};

class BitcodeReaderValueList {
std::vector<WeakVH> ValuePtrs;
std::vector<WeakTrackingVH> ValuePtrs;

/// As we resolve forward-referenced constants, we add information about them
/// to this vector. This allows us to resolve them in bulk instead of
Expand Down Expand Up @@ -786,7 +786,7 @@ void BitcodeReaderValueList::assignValue(Value *V, unsigned Idx) {
if (Idx >= size())
resize(Idx+1);

WeakVH &OldV = ValuePtrs[Idx];
WeakTrackingVH &OldV = ValuePtrs[Idx];
if (!OldV) {
OldV = V;
return;
Expand Down
8 changes: 4 additions & 4 deletions lib/CodeGen/CodeGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1370,9 +1370,9 @@ bool CodeGenPrepare::OptimizeCallInst(CallInst *CI, bool& ModifiedDT) {
Constant *RetVal = ConstantInt::get(ReturnTy, Min ? 0 : -1ULL);

// Substituting this can cause recursive simplifications, which can
// invalidate our iterator. Use a WeakVH to hold onto it in case this
// invalidate our iterator. Use WeakTrackingVH to hold onto it in case this
// happens.
WeakVH IterHandle(CurInstIterator);
WeakTrackingVH IterHandle(CurInstIterator);

replaceAndRecursivelySimplify(CI, RetVal,
TLInfo, nullptr);
Expand Down Expand Up @@ -3531,8 +3531,8 @@ bool CodeGenPrepare::OptimizeMemoryInst(Instruction *MemoryInst, Value *Addr,
// using it.
if (Repl->use_empty()) {
// This can cause recursive deletion, which can invalidate our iterator.
// Use a WeakVH to hold onto it in case this happens.
WeakVH IterHandle(CurInstIterator);
// Use a WeakTrackingVH to hold onto it in case this happens.
WeakTrackingVH IterHandle(CurInstIterator);
BasicBlock *BB = CurInstIterator->getParent();

RecursivelyDeleteTriviallyDeadInstructions(Repl, TLInfo);
Expand Down
2 changes: 1 addition & 1 deletion lib/HLSL/DxilPreparePasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1410,7 +1410,7 @@ bool CleanupSharedMemoryAddrSpaceCast(Module &M) {
}

// Cleanup unused replacement instructions
SmallVector<WeakVH, 8> cleanupInsts;
SmallVector<WeakTrackingVH, 8> cleanupInsts;
for (auto it : valueMap) {
if (isa<Instruction>(it.first))
cleanupInsts.push_back(it.first);
Expand Down
10 changes: 5 additions & 5 deletions lib/IR/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ void ValueHandleBase::ValueIsDeleted(Value *V) {
switch (Entry->getKind()) {
case Assert:
break;
case Weak:
// Weak just goes to null, which will unlink it from the list.
case WeakTracking:
// WeakTracking just goes to null, which will unlink it from the list.
Entry->operator=(nullptr);
break;
case Callback:
Expand Down Expand Up @@ -724,7 +724,7 @@ void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
case Assert:
// Asserting handle does not follow RAUW implicitly.
break;
case Weak:
case WeakTracking:
// Weak goes to the new value, which will unlink it from Old's list.
Entry->operator=(New);
break;
Expand All @@ -741,12 +741,12 @@ void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
if (Old->HasValueHandle)
for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
switch (Entry->getKind()) {
case Weak:
case WeakTracking:
dbgs() << "After RAUW from " << *Old->getType() << " %"
<< Old->getName() << " to " << *New->getType() << " %"
<< New->getName() << "\n";
llvm_unreachable(
"A weak value handle still pointed to the old value!\n");
"A weak tracking value handle still pointed to the old value!\n");
default:
break;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Transforms/IPO/GlobalOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ static bool CleanupConstantGlobalUsers(Value *V, Constant *Init,
// we delete a constant array, we may also be holding pointer to one of its
// elements (or an element of one of its elements if we're dealing with an
// array of arrays) in the worklist.
SmallVector<WeakVH, 8> WorkList(V->user_begin(), V->user_end());
SmallVector<WeakTrackingVH, 8> WorkList(V->user_begin(), V->user_end());
while (!WorkList.empty()) {
Value *UV = WorkList.pop_back_val();
if (!UV)
Expand Down
20 changes: 10 additions & 10 deletions lib/Transforms/IPO/MergeFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,11 +1097,11 @@ class MergeFunctions : public ModulePass {

/// A work queue of functions that may have been modified and should be
/// analyzed again.
std::vector<WeakVH> Deferred;
std::vector<WeakTrackingVH> Deferred;

/// Checks the rules of order relation introduced among functions set.
/// Returns true, if sanity check has been passed, and false if failed.
bool doSanityCheck(std::vector<WeakVH> &Worklist);
bool doSanityCheck(std::vector<WeakTrackingVH> &Worklist);

/// Insert a ComparableFunction into the FnTree, or merge it away if it's
/// equal to one that's already present.
Expand Down Expand Up @@ -1154,7 +1154,7 @@ ModulePass *llvm::createMergeFunctionsPass() {
return new MergeFunctions();
}

bool MergeFunctions::doSanityCheck(std::vector<WeakVH> &Worklist) {
bool MergeFunctions::doSanityCheck(std::vector<WeakTrackingVH> &Worklist) {
#if 0 // Begin HLSL Change (NumFunctionsForSanityCheck is always zero)
if (const unsigned Max = NumFunctionsForSanityCheck) {
unsigned TripleNumber = 0;
Expand All @@ -1163,10 +1163,10 @@ bool MergeFunctions::doSanityCheck(std::vector<WeakVH> &Worklist) {
dbgs() << "MERGEFUNC-SANITY: Started for first " << Max << " functions.\n";

unsigned i = 0;
for (std::vector<WeakVH>::iterator I = Worklist.begin(), E = Worklist.end();
for (std::vector<WeakTrackingVH>::iterator I = Worklist.begin(), E = Worklist.end();
I != E && i < Max; ++I, ++i) {
unsigned j = i;
for (std::vector<WeakVH>::iterator J = I; J != E && j < Max; ++J, ++j) {
for (std::vector<WeakTrackingVH>::iterator J = I; J != E && j < Max; ++J, ++j) {
Function *F1 = cast<Function>(*I);
Function *F2 = cast<Function>(*J);
int Res1 = FunctionComparator(F1, F2).compare();
Expand All @@ -1185,7 +1185,7 @@ bool MergeFunctions::doSanityCheck(std::vector<WeakVH> &Worklist) {
continue;

unsigned k = j;
for (std::vector<WeakVH>::iterator K = J; K != E && k < Max;
for (std::vector<WeakTrackingVH>::iterator K = J; K != E && k < Max;
++k, ++K, ++TripleNumber) {
if (K == J)
continue;
Expand Down Expand Up @@ -1233,11 +1233,11 @@ bool MergeFunctions::runOnModule(Module &M) {

for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
if (!I->isDeclaration() && !I->hasAvailableExternallyLinkage())
Deferred.push_back(WeakVH(I));
Deferred.push_back(WeakTrackingVH(I));
}

do {
std::vector<WeakVH> Worklist;
std::vector<WeakTrackingVH> Worklist;
Deferred.swap(Worklist);

DEBUG(doSanityCheck(Worklist));
Expand All @@ -1247,7 +1247,7 @@ bool MergeFunctions::runOnModule(Module &M) {

// Insert only strong functions and merge them. Strong function merging
// always deletes one of them.
for (std::vector<WeakVH>::iterator I = Worklist.begin(),
for (std::vector<WeakTrackingVH>::iterator I = Worklist.begin(),
E = Worklist.end(); I != E; ++I) {
if (!*I) continue;
Function *F = cast<Function>(*I);
Expand All @@ -1261,7 +1261,7 @@ bool MergeFunctions::runOnModule(Module &M) {
// create thunks to the strong function when possible. When two weak
// functions are identical, we create a new strong function with two weak
// weak thunks to it which are identical but not mergable.
for (std::vector<WeakVH>::iterator I = Worklist.begin(),
for (std::vector<WeakTrackingVH>::iterator I = Worklist.begin(),
E = Worklist.end(); I != E; ++I) {
if (!*I) continue;
Function *F = cast<Function>(*I);
Expand Down
Loading

0 comments on commit a776a77

Please sign in to comment.