From 5f29f640f60bb92d6eebbd8eb13a0a6aa967dcb3 Mon Sep 17 00:00:00 2001 From: Kshitij Paranjape Date: Sat, 11 Jan 2025 12:40:23 +0530 Subject: [PATCH 1/8] [LibCallShrinkWrap] Added ilogb in performCallErrors() ilogb libcall was not being constant folded correctly. It was due to LibCallShrinkWrap pass not handling ilogbx, as a result SimplifyCFG pass did not remove redundant libcall. This patch adds ilogb case in performCallErrors(). Fixes #101873 --- llvm/lib/Transforms/Utils/LibCallsShrinkWrap.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/llvm/lib/Transforms/Utils/LibCallsShrinkWrap.cpp b/llvm/lib/Transforms/Utils/LibCallsShrinkWrap.cpp index 9fe655e548c22..1316137ca4f96 100644 --- a/llvm/lib/Transforms/Utils/LibCallsShrinkWrap.cpp +++ b/llvm/lib/Transforms/Utils/LibCallsShrinkWrap.cpp @@ -246,6 +246,7 @@ bool LibCallsShrinkWrap::performCallErrors(CallInst *CI, case LibFunc_logb: // Same as log case LibFunc_logbf: // Same as log case LibFunc_logbl: // Same as log + case LibFunc_ilogb: // Same as log { ++NumWrappedOneCond; Cond = createCond(CI, CmpInst::FCMP_OLE, 0.0f); From c171c4db0c144fbfc537678ab6f531f908c3522b Mon Sep 17 00:00:00 2001 From: Kshitij Paranjape Date: Sun, 12 Jan 2025 00:26:02 +0530 Subject: [PATCH 2/8] [ConstantFolding] Added ilogb in isMathLibCallNoop() ilogb libcall was not being constant folded correctly. This patch adds ilogb with its correct domain in isMathLibCallNoop(). Fixes #101873 --- llvm/lib/Analysis/ConstantFolding.cpp | 3 ++- llvm/lib/Transforms/Utils/LibCallsShrinkWrap.cpp | 1 - llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll | 2 ++ 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index 031d675c330ec..c1e1f115df16e 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -3806,7 +3806,8 @@ bool llvm::isMathLibCallNoop(const CallBase *Call, case LibFunc_log10: case LibFunc_log10f: return Op.isNaN() || (!Op.isZero() && !Op.isNegative()); - + case LibFunc_ilogb: + return !(Op.isNaN()) && !(Op.isZero() || Op.isInfinity()); case LibFunc_expl: case LibFunc_exp: case LibFunc_expf: diff --git a/llvm/lib/Transforms/Utils/LibCallsShrinkWrap.cpp b/llvm/lib/Transforms/Utils/LibCallsShrinkWrap.cpp index 1316137ca4f96..9fe655e548c22 100644 --- a/llvm/lib/Transforms/Utils/LibCallsShrinkWrap.cpp +++ b/llvm/lib/Transforms/Utils/LibCallsShrinkWrap.cpp @@ -246,7 +246,6 @@ bool LibCallsShrinkWrap::performCallErrors(CallInst *CI, case LibFunc_logb: // Same as log case LibFunc_logbf: // Same as log case LibFunc_logbl: // Same as log - case LibFunc_ilogb: // Same as log { ++NumWrappedOneCond; Cond = createCond(CI, CmpInst::FCMP_OLE, 0.0f); diff --git a/llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll b/llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll index f4dc79759d17e..f1a1c62c51a1d 100644 --- a/llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll +++ b/llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll @@ -334,6 +334,8 @@ entry: ; CHECK-NEXT: %call_11 = call float @log1pf(float %value) ; CHECK-NEXT: br label %[[END_LABEL]] ; CHECK: [[END_LABEL]]: + + %call_12 = call i32 @ilogb(float %value) strictfp ret void } From 9c58c9c237c712980f064f498847cf390e2e511e Mon Sep 17 00:00:00 2001 From: Kshitij Paranjape Date: Sun, 12 Jan 2025 01:06:59 +0530 Subject: [PATCH 3/8] added more tests --- llvm/lib/Analysis/ConstantFolding.cpp | 4 +++- llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index c1e1f115df16e..ba61345b5b7c0 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -3806,8 +3806,10 @@ bool llvm::isMathLibCallNoop(const CallBase *Call, case LibFunc_log10: case LibFunc_log10f: return Op.isNaN() || (!Op.isZero() && !Op.isNegative()); + case LibFunc_ilogb: - return !(Op.isNaN()) && !(Op.isZero() || Op.isInfinity()); + return !Op.isNaN() && (!Op.isZero() && !Op.isInfinity()); + case LibFunc_expl: case LibFunc_exp: case LibFunc_expf: diff --git a/llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll b/llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll index f1a1c62c51a1d..8c9af686c706f 100644 --- a/llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll +++ b/llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll @@ -223,6 +223,8 @@ entry: ; CHECK-NEXT: %call_11 = call float @log1pf(float %value) ; CHECK-NEXT: br label %[[END_LABEL]] ; CHECK: [[END_LABEL]]: + + %call_12 = call i32 @ilogb(float %value) ret void } @@ -351,5 +353,6 @@ declare float @log10f(float) declare float @log2f(float) declare float @logbf(float) declare float @log1pf(float) +declare i32 @ilogb(float) ; CHECK: ![[BRANCH_WEIGHT]] = !{!"branch_weights", i32 1, i32 1048575} From 05e14edf4b955d8a5852c7ea5cef1b45bfc4dc4d Mon Sep 17 00:00:00 2001 From: Kshitij Paranjape Date: Tue, 14 Jan 2025 12:00:35 +0530 Subject: [PATCH 4/8] made changes to test file --- llvm/lib/Analysis/ConstantFolding.cpp | 2 +- llvm/test/Transforms/InstSimplify/pr122582.ll | 13 +++++++++++++ .../Transforms/Util/libcalls-shrinkwrap-float.ll | 5 ----- 3 files changed, 14 insertions(+), 6 deletions(-) create mode 100644 llvm/test/Transforms/InstSimplify/pr122582.ll diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp index ba61345b5b7c0..6814a1e4d8394 100644 --- a/llvm/lib/Analysis/ConstantFolding.cpp +++ b/llvm/lib/Analysis/ConstantFolding.cpp @@ -3808,7 +3808,7 @@ bool llvm::isMathLibCallNoop(const CallBase *Call, return Op.isNaN() || (!Op.isZero() && !Op.isNegative()); case LibFunc_ilogb: - return !Op.isNaN() && (!Op.isZero() && !Op.isInfinity()); + return !Op.isNaN() && !Op.isZero() && !Op.isInfinity(); case LibFunc_expl: case LibFunc_exp: diff --git a/llvm/test/Transforms/InstSimplify/pr122582.ll b/llvm/test/Transforms/InstSimplify/pr122582.ll new file mode 100644 index 0000000000000..8e9109356e538 --- /dev/null +++ b/llvm/test/Transforms/InstSimplify/pr122582.ll @@ -0,0 +1,13 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 +; RUN: opt < %s -O1 -S | FileCheck %s + +define i32 @ilogb_const1() { +; CHECK-LABEL: define noundef i32 @ilogb_const1( +; CHECK-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] { +; CHECK-NEXT: ret i32 2 +; + %r = call i32 @ilogb(double -7.000000e+00) + ret i32 %r +} + +declare i32 @ilogb(double) \ No newline at end of file diff --git a/llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll b/llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll index 8c9af686c706f..f4dc79759d17e 100644 --- a/llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll +++ b/llvm/test/Transforms/Util/libcalls-shrinkwrap-float.ll @@ -223,8 +223,6 @@ entry: ; CHECK-NEXT: %call_11 = call float @log1pf(float %value) ; CHECK-NEXT: br label %[[END_LABEL]] ; CHECK: [[END_LABEL]]: - - %call_12 = call i32 @ilogb(float %value) ret void } @@ -336,8 +334,6 @@ entry: ; CHECK-NEXT: %call_11 = call float @log1pf(float %value) ; CHECK-NEXT: br label %[[END_LABEL]] ; CHECK: [[END_LABEL]]: - - %call_12 = call i32 @ilogb(float %value) strictfp ret void } @@ -353,6 +349,5 @@ declare float @log10f(float) declare float @log2f(float) declare float @logbf(float) declare float @log1pf(float) -declare i32 @ilogb(float) ; CHECK: ![[BRANCH_WEIGHT]] = !{!"branch_weights", i32 1, i32 1048575} From 48935e7574d553554c8c53bf1f867fd927cf870c Mon Sep 17 00:00:00 2001 From: Kshitij Paranjape Date: Tue, 14 Jan 2025 12:16:50 +0530 Subject: [PATCH 5/8] fixed newline --- llvm/test/Transforms/InstSimplify/pr122582.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/test/Transforms/InstSimplify/pr122582.ll b/llvm/test/Transforms/InstSimplify/pr122582.ll index 8e9109356e538..13b11c683706f 100644 --- a/llvm/test/Transforms/InstSimplify/pr122582.ll +++ b/llvm/test/Transforms/InstSimplify/pr122582.ll @@ -10,4 +10,4 @@ define i32 @ilogb_const1() { ret i32 %r } -declare i32 @ilogb(double) \ No newline at end of file +declare i32 @ilogb(double) From 56acc7fbc188b6f459ebf0f924b2da596c90f633 Mon Sep 17 00:00:00 2001 From: Kshitij Paranjape Date: Tue, 14 Jan 2025 14:57:09 +0530 Subject: [PATCH 6/8] added strictfp tests --- llvm/test/Transforms/InstCombine/ilogb.ll | 2 +- llvm/test/Transforms/InstSimplify/pr122582.ll | 206 +++++++++++++++++- 2 files changed, 204 insertions(+), 4 deletions(-) diff --git a/llvm/test/Transforms/InstCombine/ilogb.ll b/llvm/test/Transforms/InstCombine/ilogb.ll index e30791fe68e7b..82fa7ed0b95dd 100644 --- a/llvm/test/Transforms/InstCombine/ilogb.ll +++ b/llvm/test/Transforms/InstCombine/ilogb.ll @@ -1,5 +1,5 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 -; RUN: opt < %s -passes=instcombine -S | FileCheck %s +; RUN: opt < %s -passes=instsimplify -S | FileCheck %s define i32 @ilogbf_const1() { ; CHECK-LABEL: define i32 @ilogbf_const1() { diff --git a/llvm/test/Transforms/InstSimplify/pr122582.ll b/llvm/test/Transforms/InstSimplify/pr122582.ll index 13b11c683706f..0c341334b1773 100644 --- a/llvm/test/Transforms/InstSimplify/pr122582.ll +++ b/llvm/test/Transforms/InstSimplify/pr122582.ll @@ -1,13 +1,213 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 -; RUN: opt < %s -O1 -S | FileCheck %s +; RUN: opt < %s -passes=instsimplify -S | FileCheck %s + +define i32 @ilogbf_const1() { +; CHECK-LABEL: define i32 @ilogbf_const1() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogbf(float 7.000000e+00) +; CHECK-NEXT: ret i32 2 +; + %r = call i32 @ilogbf(float 7.000000e+00) + ret i32 %r +} define i32 @ilogb_const1() { -; CHECK-LABEL: define noundef i32 @ilogb_const1( -; CHECK-SAME: ) local_unnamed_addr #[[ATTR0:[0-9]+]] { +; CHECK-LABEL: define i32 @ilogb_const1() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogb(double -7.000000e+00) +; CHECK-NEXT: ret i32 2 +; + %r = call i32 @ilogb(double -7.000000e+00) + ret i32 %r +} + +define i32 @ilogbf_const2() { +; CHECK-LABEL: define i32 @ilogbf_const2() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogbf(float 5.000000e-01) +; CHECK-NEXT: ret i32 -1 +; + %r = call i32 @ilogbf(float 5.000000e-01) + ret i32 %r +} + +define i32 @ilogb_const2() { +; CHECK-LABEL: define i32 @ilogb_const2() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogb(double -5.000000e-01) +; CHECK-NEXT: ret i32 -1 +; + %r = call i32 @ilogb(double -5.000000e-01) + ret i32 %r +} + +define i32 @ilogbf_zero() { +; CHECK-LABEL: define i32 @ilogbf_zero() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogbf(float 0.000000e+00) +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogbf(float 0.000000e+00) + ret i32 %r +} + +define i32 @ilogb_zero() { +; CHECK-LABEL: define i32 @ilogb_zero() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogb(double 0.000000e+00) +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogb(double 0.000000e+00) + ret i32 %r +} + +define i32 @ilogbf_neg_zero() { +; CHECK-LABEL: define i32 @ilogbf_neg_zero() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogbf(float -0.000000e+00) +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogbf(float -0.000000e+00) + ret i32 %r +} + +define i32 @ilogb_neg_zero() { +; CHECK-LABEL: define i32 @ilogb_neg_zero() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogb(double -0.000000e+00) +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogb(double -0.000000e+00) + ret i32 %r +} + +define i32 @ilogbf_inf() { +; CHECK-LABEL: define i32 @ilogbf_inf() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogbf(float 0x7FF0000000000000) +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogbf(float 0x7FF0000000000000) + ret i32 %r +} + +define i32 @ilogb_inf() { +; CHECK-LABEL: define i32 @ilogb_inf() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogb(double 0x7FF0000000000000) +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogb(double 0x7FF0000000000000) + ret i32 %r +} + +define i32 @ilogbf_nan() { +; CHECK-LABEL: define i32 @ilogbf_nan() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogbf(float 0x7FF8000000000000) +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogbf(float 0x7FF8000000000000) + ret i32 %r +} + +define i32 @ilogb_nan() { +; CHECK-LABEL: define i32 @ilogb_nan() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogb(double 0x7FF8000000000000) +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogb(double 0x7FF8000000000000) + ret i32 %r +} + +define i32 @ilogbf_zero_readnone() { +; CHECK-LABEL: define i32 @ilogbf_zero_readnone() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogbf(float 0.000000e+00) #[[ATTR1:[0-9]+]] +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogbf(float 0.000000e+00) readnone + ret i32 %r +} + +define i32 @ilogb_zero_readnone() { +; CHECK-LABEL: define i32 @ilogb_zero_readnone() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogb(double 0.000000e+00) #[[ATTR1]] +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogb(double 0.000000e+00) readnone + ret i32 %r +} + +define i32 @ilogbf_neg_zero_readnone() { +; CHECK-LABEL: define i32 @ilogbf_neg_zero_readnone() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogbf(float -0.000000e+00) #[[ATTR1]] +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogbf(float -0.000000e+00) readnone + ret i32 %r +} + +define i32 @ilogb_neg_zero_readnone() { +; CHECK-LABEL: define i32 @ilogb_neg_zero_readnone() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogb(double -0.000000e+00) #[[ATTR1]] +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogb(double -0.000000e+00) readnone + ret i32 %r +} + +define i32 @ilogbf_inf_readnone() { +; CHECK-LABEL: define i32 @ilogbf_inf_readnone() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogbf(float 0x7FF0000000000000) #[[ATTR1]] +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogbf(float 0x7FF0000000000000) readnone + ret i32 %r +} + +define i32 @ilogb_inf_readnone() { +; CHECK-LABEL: define i32 @ilogb_inf_readnone() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogb(double 0x7FF0000000000000) #[[ATTR1]] +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogb(double 0x7FF0000000000000) readnone + ret i32 %r +} + +define i32 @ilogbf_nan_readnone() { +; CHECK-LABEL: define i32 @ilogbf_nan_readnone() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogbf(float 0x7FF8000000000000) #[[ATTR1]] +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogbf(float 0x7FF8000000000000) readnone + ret i32 %r +} + +define i32 @ilogb_nan_readnone() { +; CHECK-LABEL: define i32 @ilogb_nan_readnone() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogb(double 0x7FF8000000000000) #[[ATTR1]] +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogb(double 0x7FF8000000000000) readnone + ret i32 %r +} + +define i32 @ilogbf_poison() { +; CHECK-LABEL: define i32 @ilogbf_poison() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogbf(float poison) +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogbf(float poison) + ret i32 %r +} + +define i32 @ilogb_poison() { +; CHECK-LABEL: define i32 @ilogb_poison() { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogb(double poison) +; CHECK-NEXT: ret i32 [[R]] +; + %r = call i32 @ilogb(double poison) + ret i32 %r +} + +define i32 @ilogb_const1_1() strictfp { +; CHECK-LABEL: define i32 @ilogb_const1_1( +; CHECK-SAME: ) #[[ATTR0:[0-9]+]] { +; CHECK-NEXT: [[R:%.*]] = call i32 @ilogb(double -7.000000e+00) ; CHECK-NEXT: ret i32 2 ; %r = call i32 @ilogb(double -7.000000e+00) ret i32 %r } +declare i32 @ilogbf(float) declare i32 @ilogb(double) From 9ae6faf16b8a9a33f3e416cf321030ede8b09e0f Mon Sep 17 00:00:00 2001 From: Kshitij Paranjape Date: Wed, 15 Jan 2025 14:01:34 +0530 Subject: [PATCH 7/8] minor change --- llvm/test/Transforms/InstCombine/ilogb.ll | 2 +- llvm/test/Transforms/InstSimplify/pr122582.ll | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/llvm/test/Transforms/InstCombine/ilogb.ll b/llvm/test/Transforms/InstCombine/ilogb.ll index 82fa7ed0b95dd..e30791fe68e7b 100644 --- a/llvm/test/Transforms/InstCombine/ilogb.ll +++ b/llvm/test/Transforms/InstCombine/ilogb.ll @@ -1,5 +1,5 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5 -; RUN: opt < %s -passes=instsimplify -S | FileCheck %s +; RUN: opt < %s -passes=instcombine -S | FileCheck %s define i32 @ilogbf_const1() { ; CHECK-LABEL: define i32 @ilogbf_const1() { diff --git a/llvm/test/Transforms/InstSimplify/pr122582.ll b/llvm/test/Transforms/InstSimplify/pr122582.ll index 0c341334b1773..fd507e39eccdd 100644 --- a/llvm/test/Transforms/InstSimplify/pr122582.ll +++ b/llvm/test/Transforms/InstSimplify/pr122582.ll @@ -206,7 +206,7 @@ define i32 @ilogb_const1_1() strictfp { ; CHECK-NEXT: ret i32 2 ; %r = call i32 @ilogb(double -7.000000e+00) - ret i32 %r + ret i32 %r } declare i32 @ilogbf(float) From 685ce576beec4fef4f5f0d450e70021b8fd56570 Mon Sep 17 00:00:00 2001 From: Kshitij Paranjape Date: Wed, 15 Jan 2025 21:00:42 +0530 Subject: [PATCH 8/8] removed whitespace Co-authored-by: c8ef --- llvm/test/Transforms/InstSimplify/pr122582.ll | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/llvm/test/Transforms/InstSimplify/pr122582.ll b/llvm/test/Transforms/InstSimplify/pr122582.ll index fd507e39eccdd..0c341334b1773 100644 --- a/llvm/test/Transforms/InstSimplify/pr122582.ll +++ b/llvm/test/Transforms/InstSimplify/pr122582.ll @@ -206,7 +206,7 @@ define i32 @ilogb_const1_1() strictfp { ; CHECK-NEXT: ret i32 2 ; %r = call i32 @ilogb(double -7.000000e+00) - ret i32 %r + ret i32 %r } declare i32 @ilogbf(float)