Skip to content

Commit 8f681d5

Browse files
committed
[IR] Allow Value::replaceUsesWithIf() to process constants
The change is currently NFC, but exploited by the depending D102954. Code to handle constants is borrowed from the general implementation of Value::doRAUW(). Differential Revision: https://reviews.llvm.org/D103051
1 parent 78eaff2 commit 8f681d5

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

llvm/include/llvm/IR/Value.h

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -311,27 +311,15 @@ class Value {
311311
/// Go through the uses list for this definition and make each use point
312312
/// to "V" if the callback ShouldReplace returns true for the given Use.
313313
/// Unlike replaceAllUsesWith() this function does not support basic block
314-
/// values or constant users.
314+
/// values.
315315
void replaceUsesWithIf(Value *New,
316-
llvm::function_ref<bool(Use &U)> ShouldReplace) {
317-
assert(New && "Value::replaceUsesWithIf(<null>) is invalid!");
318-
assert(New->getType() == getType() &&
319-
"replaceUses of value with new value of different type!");
320-
321-
for (use_iterator UI = use_begin(), E = use_end(); UI != E;) {
322-
Use &U = *UI;
323-
++UI;
324-
if (!ShouldReplace(U))
325-
continue;
326-
U.set(New);
327-
}
328-
}
316+
llvm::function_ref<bool(Use &U)> ShouldReplace);
329317

330318
/// replaceUsesOutsideBlock - Go through the uses list for this definition and
331319
/// make each use point to "V" instead of "this" when the use is outside the
332320
/// block. 'This's use list is expected to have at least one element.
333321
/// Unlike replaceAllUsesWith() this function does not support basic block
334-
/// values or constant users.
322+
/// values.
335323
void replaceUsesOutsideBlock(Value *V, BasicBlock *BB);
336324

337325
//----------------------------------------------------------------------

llvm/lib/IR/Value.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,29 @@ void Value::replaceNonMetadataUsesWith(Value *New) {
532532
doRAUW(New, ReplaceMetadataUses::No);
533533
}
534534

535+
void Value::replaceUsesWithIf(Value *New,
536+
llvm::function_ref<bool(Use &U)> ShouldReplace) {
537+
assert(New && "Value::replaceUsesWithIf(<null>) is invalid!");
538+
assert(New->getType() == getType() &&
539+
"replaceUses of value with new value of different type!");
540+
541+
for (use_iterator UI = use_begin(), E = use_end(); UI != E;) {
542+
Use &U = *UI;
543+
++UI;
544+
if (!ShouldReplace(U))
545+
continue;
546+
// Must handle Constants specially, we cannot call replaceUsesOfWith on a
547+
// constant because they are uniqued.
548+
if (auto *C = dyn_cast<Constant>(U.getUser())) {
549+
if (!isa<GlobalValue>(C)) {
550+
C->handleOperandChange(this, New);
551+
continue;
552+
}
553+
}
554+
U.set(New);
555+
}
556+
}
557+
535558
/// Replace llvm.dbg.* uses of MetadataAsValue(ValueAsMetadata(V)) outside BB
536559
/// with New.
537560
static void replaceDbgUsesOutsideBlock(Value *V, Value *New, BasicBlock *BB) {

0 commit comments

Comments
 (0)