[llvm] r367548 - [IR] Value: add replaceUsesWithIf() utility
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 1 05:32:08 PDT 2019
Author: lebedevri
Date: Thu Aug 1 05:32:08 2019
New Revision: 367548
URL: http://llvm.org/viewvc/llvm-project?rev=367548&view=rev
Log:
[IR] Value: add replaceUsesWithIf() utility
Summary:
While there is always a `Value::replaceAllUsesWith()`,
sometimes the replacement needs to be conditional.
I have only cleaned a few cases where `replaceUsesWithIf()`
could be used, to both add test coverage,
and show that it is actually useful.
Reviewers: jdoerfert, spatel, RKSimon, craig.topper
Reviewed By: jdoerfert
Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, aheejin, george.burgess.iv, asbirlea, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65528
Modified:
llvm/trunk/include/llvm/IR/Value.h
llvm/trunk/lib/Analysis/MemorySSAUpdater.cpp
llvm/trunk/lib/IR/Value.cpp
llvm/trunk/lib/Target/WebAssembly/WebAssemblyOptimizeReturned.cpp
llvm/trunk/lib/Transforms/IPO/LowerTypeTests.cpp
llvm/trunk/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
llvm/trunk/lib/Transforms/Scalar/LoopSink.cpp
Modified: llvm/trunk/include/llvm/IR/Value.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Value.h?rev=367548&r1=367547&r2=367548&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Value.h (original)
+++ llvm/trunk/include/llvm/IR/Value.h Thu Aug 1 05:32:08 2019
@@ -14,6 +14,7 @@
#define LLVM_IR_VALUE_H
#include "llvm-c/Types.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/IR/Use.h"
#include "llvm/Support/CBindingWrapping.h"
@@ -292,10 +293,29 @@ public:
/// "V" instead of "this". This function skips metadata entries in the list.
void replaceNonMetadataUsesWith(Value *V);
+ /// Go through the uses list for this definition and make each use point
+ /// to "V" if the callback ShouldReplace returns true for the given Use.
+ /// Unlike replaceAllUsesWith() this function does not support basic block
+ /// values or constant users.
+ void replaceUsesWithIf(Value *New,
+ llvm::function_ref<bool(Use &U)> ShouldReplace) {
+ assert(New && "Value::replaceUsesWithIf(<null>) is invalid!");
+ assert(New->getType() == getType() &&
+ "replaceUses of value with new value of different type!");
+
+ for (use_iterator UI = use_begin(), E = use_end(); UI != E;) {
+ Use &U = *UI;
+ ++UI;
+ if (!ShouldReplace(U))
+ continue;
+ U.set(New);
+ }
+ }
+
/// replaceUsesOutsideBlock - Go through the uses list for this definition and
/// make each use point to "V" instead of "this" when the use is outside the
/// block. 'This's use list is expected to have at least one element.
- /// Unlike replaceAllUsesWith this function does not support basic block
+ /// Unlike replaceAllUsesWith() this function does not support basic block
/// values or constant users.
void replaceUsesOutsideBlock(Value *V, BasicBlock *BB);
Modified: llvm/trunk/lib/Analysis/MemorySSAUpdater.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/MemorySSAUpdater.cpp?rev=367548&r1=367547&r2=367548&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/MemorySSAUpdater.cpp (original)
+++ llvm/trunk/lib/Analysis/MemorySSAUpdater.cpp Thu Aug 1 05:32:08 2019
@@ -267,17 +267,14 @@ void MemorySSAUpdater::insertDef(MemoryD
// before.
// We now define that def's memorydefs and memoryphis
if (DefBeforeSameBlock) {
- for (auto UI = DefBefore->use_begin(), UE = DefBefore->use_end();
- UI != UE;) {
- Use &U = *UI++;
+ DefBefore->replaceUsesWithIf(MD, [MD](Use &U) {
// Leave the MemoryUses alone.
// Also make sure we skip ourselves to avoid self references.
- if (isa<MemoryUse>(U.getUser()) || U.getUser() == MD)
- continue;
+ User *Usr = U.getUser();
+ return !isa<MemoryUse>(Usr) && Usr != MD;
// Defs are automatically unoptimized when the user is set to MD below,
// because the isOptimized() call will fail to find the same ID.
- U.set(MD);
- }
+ });
}
// and that def is now our defining access.
Modified: llvm/trunk/lib/IR/Value.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Value.cpp?rev=367548&r1=367547&r2=367548&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Value.cpp (original)
+++ llvm/trunk/lib/IR/Value.cpp Thu Aug 1 05:32:08 2019
@@ -444,15 +444,11 @@ void Value::replaceUsesOutsideBlock(Valu
"replaceUses of value with new value of different type!");
assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
- use_iterator UI = use_begin(), E = use_end();
- for (; UI != E;) {
- Use &U = *UI;
- ++UI;
- auto *Usr = dyn_cast<Instruction>(U.getUser());
- if (Usr && Usr->getParent() == BB)
- continue;
- U.set(New);
- }
+ replaceUsesWithIf(New, [BB](Use &U) {
+ auto *I = dyn_cast<Instruction>(U.getUser());
+ // Don't replace if it's an instruction in the BB basic block.
+ return !I || I->getParent() != BB;
+ });
}
namespace {
Modified: llvm/trunk/lib/Target/WebAssembly/WebAssemblyOptimizeReturned.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/WebAssembly/WebAssemblyOptimizeReturned.cpp?rev=367548&r1=367547&r2=367548&view=diff
==============================================================================
--- llvm/trunk/lib/Target/WebAssembly/WebAssemblyOptimizeReturned.cpp (original)
+++ llvm/trunk/lib/Target/WebAssembly/WebAssemblyOptimizeReturned.cpp Thu Aug 1 05:32:08 2019
@@ -64,11 +64,8 @@ void OptimizeReturned::visitCallSite(Cal
if (isa<Constant>(Arg))
continue;
// Like replaceDominatedUsesWith but using Instruction/Use dominance.
- for (auto UI = Arg->use_begin(), UE = Arg->use_end(); UI != UE;) {
- Use &U = *UI++;
- if (DT->dominates(Inst, U))
- U.set(Inst);
- }
+ Arg->replaceUsesWithIf(Inst,
+ [&](Use &U) { return DT->dominates(Inst, U); });
}
}
Modified: llvm/trunk/lib/Transforms/IPO/LowerTypeTests.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/LowerTypeTests.cpp?rev=367548&r1=367547&r2=367548&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/LowerTypeTests.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/LowerTypeTests.cpp Thu Aug 1 05:32:08 2019
@@ -1685,16 +1685,7 @@ void LowerTypeTestsModule::replaceCfiUse
}
void LowerTypeTestsModule::replaceDirectCalls(Value *Old, Value *New) {
- auto UI = Old->use_begin(), E = Old->use_end();
- for (; UI != E;) {
- Use &U = *UI;
- ++UI;
-
- if (!isDirectCall(U))
- continue;
-
- U.set(New);
- }
+ Old->replaceUsesWithIf(New, [](Use &U) { return isDirectCall(U); });
}
bool LowerTypeTestsModule::lower() {
Modified: llvm/trunk/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp?rev=367548&r1=367547&r2=367548&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp (original)
+++ llvm/trunk/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp Thu Aug 1 05:32:08 2019
@@ -1002,11 +1002,8 @@ bool HWAddressSanitizer::instrumentStack
AI->hasName() ? AI->getName().str() : "alloca." + itostr(N);
Replacement->setName(Name + ".hwasan");
- for (auto UI = AI->use_begin(), UE = AI->use_end(); UI != UE;) {
- Use &U = *UI++;
- if (U.getUser() != AILong)
- U.set(Replacement);
- }
+ AI->replaceUsesWithIf(Replacement,
+ [AILong](Use &U) { return U.getUser() != AILong; });
for (auto *DDI : AllocaDeclareMap.lookup(AI)) {
DIExpression *OldExpr = DDI->getExpression();
Modified: llvm/trunk/lib/Transforms/Scalar/LoopSink.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopSink.cpp?rev=367548&r1=367547&r2=367548&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopSink.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopSink.cpp Thu Aug 1 05:32:08 2019
@@ -230,12 +230,9 @@ static bool sinkInstruction(Loop &L, Ins
IC->setName(I.getName());
IC->insertBefore(&*N->getFirstInsertionPt());
// Replaces uses of I with IC in N
- for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); UI != UE;) {
- Use &U = *UI++;
- auto *I = cast<Instruction>(U.getUser());
- if (I->getParent() == N)
- U.set(IC);
- }
+ I.replaceUsesWithIf(IC, [N](Use &U) {
+ return cast<Instruction>(U.getUser())->getParent() == N;
+ });
// Replaces uses of I with IC in blocks dominated by N
replaceDominatedUsesWith(&I, IC, DT, N);
LLVM_DEBUG(dbgs() << "Sinking a clone of " << I << " To: " << N->getName()
More information about the llvm-commits
mailing list