// For each instruction used by the value, remove() the function that contains
// the instruction. This should happen right before a call to RAUW.
void MergeFunctions::removeUsers(Value *V) {
- std::vector<Value *> Worklist;
- Worklist.push_back(V);
- SmallPtrSet<Value*, 8> Visited;
- Visited.insert(V);
- while (!Worklist.empty()) {
- Value *V = Worklist.back();
- Worklist.pop_back();
-
- for (User *U : V->users()) {
- if (Instruction *I = dyn_cast<Instruction>(U)) {
- remove(I->getFunction());
- } else if (isa<GlobalValue>(U)) {
- // do nothing
- } else if (Constant *C = dyn_cast<Constant>(U)) {
- for (User *UU : C->users()) {
- if (!Visited.insert(UU).second)
- Worklist.push_back(UU);
- }
- }
- }
- }
+ for (User *U : V->users())
+ if (auto *I = dyn_cast<Instruction>(U))
+ remove(I->getFunction());
}
; RUN: opt -S -mergefunc < %s | FileCheck %s
-; After test3 and test4 have been merged, we should detect that
-; test1 and test2 can also be merged.
+; After the merge of test5 and test6, we can merge test3 and test4,
+; then test1 and test2.
+; CHECK: define void @test6() unnamed_addr
+; CHECK-NEXT: tail call void @test5()
; CHECK: define void @test4() unnamed_addr
; CHECK-NEXT: tail call void @test3()
; CHECK: define void @test2() unnamed_addr
; CHECK-NEXT: tail call void @test1()
declare void @dummy()
-
+
define void @test1() unnamed_addr {
call void @test3()
call void @test3()
}
define void @test3() unnamed_addr {
+ call void @test5()
+ call void @test5()
+ ret void
+}
+
+define void @test4() unnamed_addr {
+ call void @test6()
+ call void @test6()
+ ret void
+}
+
+define void @test5() unnamed_addr {
call void @dummy()
call void @dummy()
ret void
}
-define void @test4() unnamed_addr {
+define void @test6() unnamed_addr {
call void @dummy()
call void @dummy()
ret void