Skip to content

Commit

Permalink
[InstCombine] Refactor substitution of instruction in the parent BB (…
Browse files Browse the repository at this point in the history
…NFC)

Add the new method `LibCallSimplifier::substituteInParent()` that calls
`LibCallSimplifier::replaceAllUsesWith()' and
`LibCallSimplifier::eraseFromParent()` back to back, simplifying the
resulting code.

llvm-svn: 371264
  • Loading branch information
Evandro Menezes committed Sep 6, 2019
1 parent 88cddb7 commit 7d677ad
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
6 changes: 6 additions & 0 deletions llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h
Expand Up @@ -126,6 +126,12 @@ class LibCallSimplifier {
/// Erase an instruction from its parent with our eraser.
void eraseFromParent(Instruction *I);

/// Replace an instruction with a value and erase it from its parent.
void substituteInParent(Instruction *I, Value *With) {
replaceAllUsesWith(I, With);
eraseFromParent(I);
}

Value *foldMallocMemset(CallInst *Memset, IRBuilder<> &B);

public:
Expand Down
23 changes: 9 additions & 14 deletions llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
Expand Up @@ -1054,16 +1054,14 @@ Value *LibCallSimplifier::foldMallocMemset(CallInst *Memset, IRBuilder<> &B) {
B.SetInsertPoint(Malloc->getParent(), ++Malloc->getIterator());
const DataLayout &DL = Malloc->getModule()->getDataLayout();
IntegerType *SizeType = DL.getIntPtrType(B.GetInsertBlock()->getContext());
Value *Calloc = emitCalloc(ConstantInt::get(SizeType, 1),
Malloc->getArgOperand(0), Malloc->getAttributes(),
B, *TLI);
if (!Calloc)
return nullptr;

Malloc->replaceAllUsesWith(Calloc);
eraseFromParent(Malloc);
if (Value *Calloc = emitCalloc(ConstantInt::get(SizeType, 1),
Malloc->getArgOperand(0),
Malloc->getAttributes(), B, *TLI)) {
substituteInParent(Malloc, Calloc);
return Calloc;
}

return Calloc;
return nullptr;
}

Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B,
Expand Down Expand Up @@ -1380,9 +1378,7 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilder<> &B) {
// elimination cannot be trusted to remove it, since it may have side
// effects (e.g., errno). When the only consumer for the original
// exp{,2}() is pow(), then it has to be explicitly erased.
BaseFn->replaceAllUsesWith(ExpFn);
eraseFromParent(BaseFn);

substituteInParent(BaseFn, ExpFn);
return ExpFn;
}
}
Expand Down Expand Up @@ -2802,8 +2798,7 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
IRBuilder<> TmpBuilder(SimplifiedCI);
if (Value *V = optimizeStringMemoryLibCall(SimplifiedCI, TmpBuilder)) {
// If we were able to further simplify, remove the now redundant call.
SimplifiedCI->replaceAllUsesWith(V);
eraseFromParent(SimplifiedCI);
substituteInParent(SimplifiedCI, V);
return V;
}
}
Expand Down

0 comments on commit 7d677ad

Please sign in to comment.