Skip to content

Commit 4126ba0

Browse files
Evandro Menezesjrtc27
authored andcommitted
[InstCombine] Refactor substitution of instruction in the parent BB (NFC)
Add the new method `LibCallSimplifier::substituteInParent()` that calls `LibCallSimplifier::replaceAllUsesWith()' and `LibCallSimplifier::eraseFromParent()` back to back, simplifying the resulting code. llvm-svn: 371264
2 parents 119c1e0 + 7d677ad commit 4126ba0

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

llvm/include/llvm/Transforms/Utils/SimplifyLibCalls.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ class LibCallSimplifier {
126126
/// Erase an instruction from its parent with our eraser.
127127
void eraseFromParent(Instruction *I);
128128

129+
/// Replace an instruction with a value and erase it from its parent.
130+
void substituteInParent(Instruction *I, Value *With) {
131+
replaceAllUsesWith(I, With);
132+
eraseFromParent(I);
133+
}
134+
129135
Value *foldMallocMemset(CallInst *Memset, IRBuilder<> &B);
130136

131137
public:

llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,16 +1060,14 @@ Value *LibCallSimplifier::foldMallocMemset(CallInst *Memset, IRBuilder<> &B) {
10601060
B.SetInsertPoint(Malloc->getParent(), ++Malloc->getIterator());
10611061
const DataLayout &DL = Malloc->getModule()->getDataLayout();
10621062
IntegerType *SizeType = DL.getIntPtrType(B.GetInsertBlock()->getContext());
1063-
Value *Calloc = emitCalloc(ConstantInt::get(SizeType, 1),
1064-
Malloc->getArgOperand(0), Malloc->getAttributes(),
1065-
B, *TLI);
1066-
if (!Calloc)
1067-
return nullptr;
1068-
1069-
Malloc->replaceAllUsesWith(Calloc);
1070-
eraseFromParent(Malloc);
1063+
if (Value *Calloc = emitCalloc(ConstantInt::get(SizeType, 1),
1064+
Malloc->getArgOperand(0),
1065+
Malloc->getAttributes(), B, *TLI)) {
1066+
substituteInParent(Malloc, Calloc);
1067+
return Calloc;
1068+
}
10711069

1072-
return Calloc;
1070+
return nullptr;
10731071
}
10741072

10751073
Value *LibCallSimplifier::optimizeMemSet(CallInst *CI, IRBuilder<> &B,
@@ -1386,9 +1384,7 @@ Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilder<> &B) {
13861384
// elimination cannot be trusted to remove it, since it may have side
13871385
// effects (e.g., errno). When the only consumer for the original
13881386
// exp{,2}() is pow(), then it has to be explicitly erased.
1389-
BaseFn->replaceAllUsesWith(ExpFn);
1390-
eraseFromParent(BaseFn);
1391-
1387+
substituteInParent(BaseFn, ExpFn);
13921388
return ExpFn;
13931389
}
13941390
}
@@ -2811,8 +2807,7 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) {
28112807
IRBuilder<> TmpBuilder(SimplifiedCI);
28122808
if (Value *V = optimizeStringMemoryLibCall(SimplifiedCI, TmpBuilder)) {
28132809
// If we were able to further simplify, remove the now redundant call.
2814-
SimplifiedCI->replaceAllUsesWith(V);
2815-
eraseFromParent(SimplifiedCI);
2810+
substituteInParent(SimplifiedCI, V);
28162811
return V;
28172812
}
28182813
}

0 commit comments

Comments
 (0)