[llvm-reduce] Skip updating calls where OldF isn't the called fn.

When replacing function calls, skip call instructions where the old
function is not the called function, but e.g. the old function is passed
as an argument.

This fixes a crash due to trying to construct invalid IR for the test
case.

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D109759
This commit is contained in:
Florian Hahn 2021-10-01 09:56:08 +01:00
parent 81d2cea690
commit 57fbb9ed0e
No known key found for this signature in database
GPG Key ID: 61D7554B5CECDC0D
2 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,23 @@
; Test that llvm-reduce can remove uninteresting function arguments from function definitions as well as their calls.
; This test checks that functions with different argument types are handled correctly
;
; RUN: llvm-reduce --test FileCheck --test-arg --check-prefixes=CHECK-ALL,CHECK-INTERESTINGNESS --test-arg %s --test-arg --input-file %s -o %t
; RUN: FileCheck --check-prefixes=CHECK-ALL,CHECK-FINAL %s --input-file %t
declare void @pass(void (i32, i8*, i64*)*)
define void @bar() {
entry:
; CHECK-INTERESTINGNESS: call void @pass({{.*}}@interesting
; CHECK-FINAL: call void @pass(void (i32, i8*, i64*)* bitcast (void (i64*)* @interesting to void (i32, i8*, i64*)*))
call void @pass(void (i32, i8*, i64*)* @interesting)
ret void
}
; CHECK-ALL: define internal void @interesting
; CHECK-INTERESTINGNESS-SAME: ({{.*}}%interesting{{.*}}) {
; CHECK-FINAL-SAME: (i64* %interesting)
define internal void @interesting(i32 %uninteresting1, i8* %uninteresting2, i64* %interesting) {
entry:
ret void
}

View File

@ -26,6 +26,10 @@ static void replaceFunctionCalls(Function &OldF, Function &NewF,
const auto &Users = OldF.users();
for (auto I = Users.begin(), E = Users.end(); I != E; )
if (auto *CI = dyn_cast<CallInst>(*I++)) {
// Skip uses in call instructions where OldF isn't the called function
// (e.g. if OldF is an argument of the call).
if (CI->getCalledFunction() != &OldF)
continue;
SmallVector<Value *, 8> Args;
for (auto ArgI = CI->arg_begin(), E = CI->arg_end(); ArgI != E; ++ArgI)
if (ArgIndexesToKeep.count(ArgI - CI->arg_begin()))