From fc0fa85171e665f92cee97635bc3a5af8785aa8d Mon Sep 17 00:00:00 2001 From: Sjoerd Meijer Date: Tue, 12 Oct 2021 11:03:34 +0100 Subject: [PATCH] [FuncSpec] Allow ConstExprs that are function pointers This is a follow up of D110529 that disallowed constexprs. That change introduced a regression as this also disallowed constexprs that are function pointers, which is actually one of the motivating use cases that we do want to support. Differential Revision: https://reviews.llvm.org/D111567 --- llvm/lib/Transforms/IPO/FunctionSpecialization.cpp | 8 ++++-- ...function-specialization-constant-expression3.ll | 29 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-expression3.ll diff --git a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp index 0d2d55a..8ab5311 100644 --- a/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp +++ b/llvm/lib/Transforms/IPO/FunctionSpecialization.cpp @@ -677,8 +677,12 @@ private: auto *V = CS.getArgOperand(A->getArgNo()); if (isa(V)) return false; - if (isa(V)) - return false; + + // For now, constant expressions are fine but only if they are function + // calls. + if (auto *CE = dyn_cast(V)) + if (!isa(CE->getOperand(0))) + return false; // TrackValueOfGlobalVariable only tracks scalar global variables. if (auto *GV = dyn_cast(V)) { diff --git a/llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-expression3.ll b/llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-expression3.ll new file mode 100644 index 0000000..8776c4e --- /dev/null +++ b/llvm/test/Transforms/FunctionSpecialization/function-specialization-constant-expression3.ll @@ -0,0 +1,29 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt -function-specialization -force-function-specialization -S < %s | FileCheck %s + +define i32 @main() { +; CHECK-LABEL: @main( +; CHECK-NEXT: bb: +; CHECK-NEXT: tail call void @wombat.1(i8* undef, i64 undef, i64 undef, i32 (i8*, i8*)* bitcast (i32 ()* @quux to i32 (i8*, i8*)*)) +; CHECK-NEXT: tail call void @wombat.2(i8* undef, i64 undef, i64 undef, i32 (i8*, i8*)* bitcast (i32 ()* @eggs to i32 (i8*, i8*)*)) +; CHECK-NEXT: ret i32 undef +; +bb: + tail call void @wombat(i8* undef, i64 undef, i64 undef, i32 (i8*, i8*)* bitcast (i32 ()* @quux to i32 (i8*, i8*)*)) + tail call void @wombat(i8* undef, i64 undef, i64 undef, i32 (i8*, i8*)* bitcast (i32 ()* @eggs to i32 (i8*, i8*)*)) + ret i32 undef +} + +declare i32 @quux() +declare i32 @eggs() + +define internal void @wombat(i8* %arg, i64 %arg1, i64 %arg2, i32 (i8*, i8*)* %arg3) { +; CHECK-LABEL: @wombat( +; CHECK-NEXT: bb4: +; CHECK-NEXT: [[TMP:%.*]] = tail call i32 [[ARG3:%.*]](i8* undef, i8* undef) +; CHECK-NEXT: ret void +; +bb4: + %tmp = tail call i32 %arg3(i8* undef, i8* undef) + ret void +} -- 2.7.4