diff --git a/llvm/include/llvm/IR/Value.h b/llvm/include/llvm/IR/Value.h --- a/llvm/include/llvm/IR/Value.h +++ b/llvm/include/llvm/IR/Value.h @@ -311,27 +311,15 @@ /// 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. + /// values. void replaceUsesWithIf(Value *New, - llvm::function_ref ShouldReplace) { - assert(New && "Value::replaceUsesWithIf() 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); - } - } + llvm::function_ref ShouldReplace); /// 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 - /// values or constant users. + /// values. void replaceUsesOutsideBlock(Value *V, BasicBlock *BB); //---------------------------------------------------------------------- diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp --- a/llvm/lib/IR/Value.cpp +++ b/llvm/lib/IR/Value.cpp @@ -532,6 +532,29 @@ doRAUW(New, ReplaceMetadataUses::No); } +void Value::replaceUsesWithIf(Value *New, + llvm::function_ref ShouldReplace) { + assert(New && "Value::replaceUsesWithIf() 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; + // Must handle Constants specially, we cannot call replaceUsesOfWith on a + // constant because they are uniqued. + if (auto *C = dyn_cast(U.getUser())) { + if (!isa(C)) { + C->handleOperandChange(this, New); + continue; + } + } + U.set(New); + } +} + /// Replace llvm.dbg.* uses of MetadataAsValue(ValueAsMetadata(V)) outside BB /// with New. static void replaceDbgUsesOutsideBlock(Value *V, Value *New, BasicBlock *BB) {