Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions llvm/include/llvm/Transforms/IPO/FunctionSpecialization.h
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ class FunctionSpecializer {
return InstCostVisitor(GetBFI, F, M.getDataLayout(), TTI, Solver);
}

bool isDeadFunction(Function *F) { return FullySpecialized.contains(F); }

private:
Constant *getPromotableAlloca(AllocaInst *Alloca, CallInst *Call);

Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/Transforms/IPO/FunctionSpecialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,16 @@ void FunctionSpecializer::removeDeadFunctions() {
<< F->getName() << "\n");
if (FAM)
FAM->clear(*F, F->getName());

// Remove all the callsites that were proven unreachable once, and replace
// them with poison.
for (User *U : make_early_inc_range(F->users())) {
assert((isa<CallInst>(U) || isa<InvokeInst>(U)) &&
"User of dead function must be call or invoke");
Instruction *CS = cast<Instruction>(U);
CS->replaceAllUsesWith(PoisonValue::get(CS->getType()));
CS->eraseFromParent();
}
F->eraseFromParent();
}
FullySpecialized.clear();
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Transforms/IPO/SCCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,10 @@ static bool runIPSCCP(
// constants if we have found them to be of constant values.
bool MadeChanges = false;
for (Function &F : M) {
if (F.isDeclaration())
// Skip the dead functions marked by FunctionSpecializer, avoiding removing
// blocks in dead functions.
if (F.isDeclaration() ||
(IsFuncSpecEnabled && Specializer.isDeadFunction(&F)))
continue;

SmallVector<BasicBlock *, 512> BlocksToErase;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -passes=ipsccp --funcspec-min-function-size=1 -S < %s | FileCheck %s

define i32 @caller() {
; CHECK-LABEL: define i32 @caller() {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[CALL1:%.*]] = call i32 @callee.specialized.1(i32 1)
; CHECK-NEXT: [[CALL2:%.*]] = call i32 @callee.specialized.2(i32 0)
; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 undef, 0
; CHECK-NEXT: br i1 [[COND]], label %[[COMMON_RET:.*]], label %[[IF_THEN:.*]]
; CHECK: [[COMMON_RET]]:
; CHECK-NEXT: ret i32 0
; CHECK: [[IF_THEN]]:
; CHECK-NEXT: ret i32 0
;
entry:
%call1 = call i32 @callee(i32 1)
%call2 = call i32 @callee(i32 0)
%cond = icmp eq i32 %call2, 0
br i1 %cond, label %common.ret, label %if.then

common.ret: ; preds = %entry
ret i32 0

if.then: ; preds = %entry
%unreachable_call = call i32 @callee(i32 2)
ret i32 %unreachable_call
}

define internal i32 @callee(i32 %arg) {
entry:
br label %loop

loop: ; preds = %ai, %entry
%add = or i32 0, 0
%cond = icmp eq i32 %arg, 1
br i1 %cond, label %exit, label %loop

exit: ; preds = %ai
ret i32 0
}
Loading