https://github.com/nikic created https://github.com/llvm/llvm-project/pull/99620
We already infer this in IPSCCP, but as that pass runs very early, it cannot make use of simplifications (in particular post-inline simplifications). This fixes most cases from https://github.com/llvm/llvm-project/issues/98946 (everything apart from f2 where the assume is dropped). (Draft because there is a significant compile-time regression in lencod ThinLTO that I should take a look at first: http://llvm-compile-time-tracker.com/compare.php?from=a1d77caaabbb5279b734c061dab36b2138ec476d&to=66f84aa164ca7c42e02c4b1c3ba1b4fb71d6220e&stat=instructions%3Au) >From 9756800f3f5cbba0f84e2e7b551c2a45b3e70fc9 Mon Sep 17 00:00:00 2001 From: Nikita Popov <npo...@redhat.com> Date: Fri, 19 Jul 2024 11:02:56 +0200 Subject: [PATCH] [CVP] Infer range return attribute We already infer this in IPSCCP, but as that pass runs very early, it cannot make use of simplifications (in particular post-inline simplifications). This fixes most cases from https://github.com/llvm/llvm-project/issues/98946 (everything apart from f2 where the assume is dropped). --- clang/test/CodeGen/attr-counted-by.c | 8 ++--- .../Scalar/CorrelatedValuePropagation.cpp | 12 +++++++ .../CorrelatedValuePropagation/add.ll | 2 +- .../CorrelatedValuePropagation/basic.ll | 10 +++--- .../cond-using-block-value.ll | 2 +- .../CorrelatedValuePropagation/select.ll | 6 ++-- .../CorrelatedValuePropagation/vectors.ll | 34 +++++++++---------- 7 files changed, 43 insertions(+), 31 deletions(-) diff --git a/clang/test/CodeGen/attr-counted-by.c b/clang/test/CodeGen/attr-counted-by.c index 32db136076d7d..0fd3d4cc85f83 100644 --- a/clang/test/CodeGen/attr-counted-by.c +++ b/clang/test/CodeGen/attr-counted-by.c @@ -639,7 +639,7 @@ void test6(struct anon_struct *p, int index) { p->array[index] = __builtin_dynamic_object_size(p->array, 1); } -// SANITIZE-WITH-ATTR-LABEL: define dso_local i64 @test6_bdos( +// SANITIZE-WITH-ATTR-LABEL: define dso_local range(i64 0, -3) i64 @test6_bdos( // SANITIZE-WITH-ATTR-SAME: ptr nocapture noundef readonly [[P:%.*]]) local_unnamed_addr #[[ATTR2]] { // SANITIZE-WITH-ATTR-NEXT: entry: // SANITIZE-WITH-ATTR-NEXT: [[DOT_COUNTED_BY_GEP:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 8 @@ -649,7 +649,7 @@ void test6(struct anon_struct *p, int index) { // SANITIZE-WITH-ATTR-NEXT: [[TMP1:%.*]] = select i1 [[DOTINV]], i64 0, i64 [[TMP0]] // SANITIZE-WITH-ATTR-NEXT: ret i64 [[TMP1]] // -// NO-SANITIZE-WITH-ATTR-LABEL: define dso_local i64 @test6_bdos( +// NO-SANITIZE-WITH-ATTR-LABEL: define dso_local range(i64 0, -3) i64 @test6_bdos( // NO-SANITIZE-WITH-ATTR-SAME: ptr nocapture noundef readonly [[P:%.*]]) local_unnamed_addr #[[ATTR2]] { // NO-SANITIZE-WITH-ATTR-NEXT: entry: // NO-SANITIZE-WITH-ATTR-NEXT: [[DOT_COUNTED_BY_GEP:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 8 @@ -955,7 +955,7 @@ void test10(struct union_of_fams *p, int index) { p->bytes[index] = (unsigned char)__builtin_dynamic_object_size(p->bytes, 1); } -// SANITIZE-WITH-ATTR-LABEL: define dso_local range(i64 -2147483648, 2147483648) i64 @test10_bdos( +// SANITIZE-WITH-ATTR-LABEL: define dso_local range(i64 0, 2147483648) i64 @test10_bdos( // SANITIZE-WITH-ATTR-SAME: ptr nocapture noundef readonly [[P:%.*]]) local_unnamed_addr #[[ATTR2]] { // SANITIZE-WITH-ATTR-NEXT: entry: // SANITIZE-WITH-ATTR-NEXT: [[DOT_COUNTED_BY_GEP:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 8 @@ -964,7 +964,7 @@ void test10(struct union_of_fams *p, int index) { // SANITIZE-WITH-ATTR-NEXT: [[TMP0:%.*]] = zext nneg i32 [[NARROW]] to i64 // SANITIZE-WITH-ATTR-NEXT: ret i64 [[TMP0]] // -// NO-SANITIZE-WITH-ATTR-LABEL: define dso_local range(i64 -2147483648, 2147483648) i64 @test10_bdos( +// NO-SANITIZE-WITH-ATTR-LABEL: define dso_local range(i64 0, 2147483648) i64 @test10_bdos( // NO-SANITIZE-WITH-ATTR-SAME: ptr nocapture noundef readonly [[P:%.*]]) local_unnamed_addr #[[ATTR2]] { // NO-SANITIZE-WITH-ATTR-NEXT: entry: // NO-SANITIZE-WITH-ATTR-NEXT: [[DOT_COUNTED_BY_GEP:%.*]] = getelementptr inbounds i8, ptr [[P]], i64 8 diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp index 95de8eceb6be5..c34fb0746931e 100644 --- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp +++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp @@ -1283,6 +1283,18 @@ static bool runImpl(Function &F, LazyValueInfo *LVI, DominatorTree *DT, RI->replaceUsesOfWith(RetVal, C); BBChanged = true; } + // Infer range attribute on return value. + if (RetVal->getType()->isIntOrIntVectorTy()) { + ConstantRange CR = LVI->getConstantRange(RetVal, RI, + /*UndefAllowed=*/false); + if (!CR.isFullSet()) { + Attribute RangeAttr = F.getRetAttribute(Attribute::Range); + if (RangeAttr.isValid()) + CR = CR.intersectWith(RangeAttr.getRange()); + if (!CR.isEmptySet()) + F.addRangeRetAttr(CR); + } + } } } diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/add.ll b/llvm/test/Transforms/CorrelatedValuePropagation/add.ll index b29a496ab1deb..3118b0ba3adfe 100644 --- a/llvm/test/Transforms/CorrelatedValuePropagation/add.ll +++ b/llvm/test/Transforms/CorrelatedValuePropagation/add.ll @@ -311,7 +311,7 @@ exit: @limit = external global i32 define i32 @test11(ptr %p, i32 %i) { -; CHECK-LABEL: define i32 @test11( +; CHECK-LABEL: define range(i32 6, 2147483645) i32 @test11( ; CHECK-SAME: ptr [[P:%.*]], i32 [[I:%.*]]) { ; CHECK-NEXT: [[LIMIT:%.*]] = load i32, ptr [[P]], align 4, !range [[RNG0:![0-9]+]] ; CHECK-NEXT: [[WITHIN_1:%.*]] = icmp ugt i32 [[LIMIT]], [[I]] diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll b/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll index c529bab4ef4a7..4786969a99b45 100644 --- a/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll +++ b/llvm/test/Transforms/CorrelatedValuePropagation/basic.ll @@ -210,7 +210,7 @@ return: } define i32 @switch1(i32 %s) { -; CHECK-LABEL: define i32 @switch1 +; CHECK-LABEL: define range(i32 -1, 2) i32 @switch1 ; CHECK-SAME: (i32 [[S:%.*]]) { ; CHECK-NEXT: entry: ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[S]], 0 @@ -250,7 +250,7 @@ next: } define i32 @switch2(i32 %s) { -; CHECK-LABEL: define i32 @switch2 +; CHECK-LABEL: define range(i32 -1, 2) i32 @switch2 ; CHECK-SAME: (i32 [[S:%.*]]) { ; CHECK-NEXT: entry: ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[S]], 0 @@ -284,7 +284,7 @@ next: } define i32 @switch3(i32 %s) { -; CHECK-LABEL: define i32 @switch3 +; CHECK-LABEL: define range(i32 -1, 2) i32 @switch3 ; CHECK-SAME: (i32 [[S:%.*]]) { ; CHECK-NEXT: entry: ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[S]], 0 @@ -2052,7 +2052,7 @@ define i1 @binop_eval_order(i32 %x) { } define range(i32 0, 1024) i32 @range_larger(i8 %x) { -; CHECK-LABEL: define range(i32 0, 1024) i32 @range_larger +; CHECK-LABEL: define range(i32 0, 256) i32 @range_larger ; CHECK-SAME: (i8 [[X:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext i8 [[X]] to i32 ; CHECK-NEXT: ret i32 [[ZEXT]] @@ -2072,7 +2072,7 @@ define range(i32 0, 128) i32 @range_smaller(i8 %x) { } define range(i32 128, 512) i32 @range_intersect(i8 %x) { -; CHECK-LABEL: define range(i32 128, 512) i32 @range_intersect +; CHECK-LABEL: define range(i32 128, 256) i32 @range_intersect ; CHECK-SAME: (i8 [[X:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext i8 [[X]] to i32 ; CHECK-NEXT: ret i32 [[ZEXT]] diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/cond-using-block-value.ll b/llvm/test/Transforms/CorrelatedValuePropagation/cond-using-block-value.ll index 252f6596cedc5..a7a1803bccc26 100644 --- a/llvm/test/Transforms/CorrelatedValuePropagation/cond-using-block-value.ll +++ b/llvm/test/Transforms/CorrelatedValuePropagation/cond-using-block-value.ll @@ -38,7 +38,7 @@ end: } define i64 @test_sext_from_implied_cond(i32 %a, i32 %b) { -; CHECK-LABEL: define i64 @test_sext_from_implied_cond( +; CHECK-LABEL: define range(i64 0, 2147483647) i64 @test_sext_from_implied_cond( ; CHECK-SAME: i32 [[A:%.*]], i32 [[B:%.*]]) { ; CHECK-NEXT: [[A_CMP:%.*]] = icmp slt i32 [[A]], 0 ; CHECK-NEXT: br i1 [[A_CMP]], label [[END:%.*]], label [[L1:%.*]] diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/select.ll b/llvm/test/Transforms/CorrelatedValuePropagation/select.ll index 2054b0fc99d49..5fe1565f3dae2 100644 --- a/llvm/test/Transforms/CorrelatedValuePropagation/select.ll +++ b/llvm/test/Transforms/CorrelatedValuePropagation/select.ll @@ -141,7 +141,7 @@ else: } define i8 @not_correlated(i1, i1) { -; CHECK-LABEL: define i8 @not_correlated +; CHECK-LABEL: define range(i8 0, 2) i8 @not_correlated ; CHECK-SAME: (i1 [[TMP0:%.*]], i1 [[TMP1:%.*]]) { ; CHECK-NEXT: entry: ; CHECK-NEXT: [[S:%.*]] = select i1 [[TMP0]], i8 0, i8 1 @@ -168,7 +168,7 @@ else: @b = global i32 0, align 4 define i32 @PR23752() { -; CHECK-LABEL: define i32 @PR23752() { +; CHECK-LABEL: define range(i32 1, 2) i32 @PR23752() { ; CHECK-NEXT: entry: ; CHECK-NEXT: br label [[FOR_BODY:%.*]] ; CHECK: for.body: @@ -361,7 +361,7 @@ exit: } define i64 @select_cond_may_undef(i32 %a) { -; CHECK-LABEL: define i64 @select_cond_may_undef +; CHECK-LABEL: define range(i64 -2147483648, 2147483648) i64 @select_cond_may_undef ; CHECK-SAME: (i32 [[A:%.*]]) { ; CHECK-NEXT: [[IS_A_NONNEGATIVE:%.*]] = icmp sgt i32 [[A]], 1 ; CHECK-NEXT: [[NARROW:%.*]] = select i1 [[IS_A_NONNEGATIVE]], i32 [[A]], i32 0 diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll b/llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll index 351a2c79cdff4..33e15319e87a1 100644 --- a/llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll +++ b/llvm/test/Transforms/CorrelatedValuePropagation/vectors.ll @@ -60,7 +60,7 @@ define <2 x i1> @cmp_signedness(<2 x i8> %a) { } define <2 x i16> @infer_nowrap(<2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @infer_nowrap( +; CHECK-LABEL: define range(i16 1, 257) <2 x i16> @infer_nowrap( ; CHECK-SAME: <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> ; CHECK-NEXT: [[RES:%.*]] = add nuw nsw <2 x i16> [[ZEXT]], <i16 1, i16 1> @@ -72,7 +72,7 @@ define <2 x i16> @infer_nowrap(<2 x i8> %a) { } define <2 x i16> @infer_nowrap_nonsplat(<2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @infer_nowrap_nonsplat( +; CHECK-LABEL: define range(i16 1, 258) <2 x i16> @infer_nowrap_nonsplat( ; CHECK-SAME: <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> ; CHECK-NEXT: [[RES:%.*]] = add nuw nsw <2 x i16> [[ZEXT]], <i16 1, i16 2> @@ -84,7 +84,7 @@ define <2 x i16> @infer_nowrap_nonsplat(<2 x i8> %a) { } define <vscale x 2 x i16> @infer_nowrap_scalable(<vscale x 2 x i8> %a) { -; CHECK-LABEL: define <vscale x 2 x i16> @infer_nowrap_scalable( +; CHECK-LABEL: define range(i16 1, 257) <vscale x 2 x i16> @infer_nowrap_scalable( ; CHECK-SAME: <vscale x 2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext <vscale x 2 x i8> [[A]] to <vscale x 2 x i16> ; CHECK-NEXT: [[RES:%.*]] = add nuw nsw <vscale x 2 x i16> [[ZEXT]], shufflevector (<vscale x 2 x i16> insertelement (<vscale x 2 x i16> poison, i16 1, i64 0), <vscale x 2 x i16> poison, <vscale x 2 x i32> zeroinitializer) @@ -96,7 +96,7 @@ define <vscale x 2 x i16> @infer_nowrap_scalable(<vscale x 2 x i8> %a) { } define <2 x i16> @infer_nowrap_poison(<2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @infer_nowrap_poison( +; CHECK-LABEL: define range(i16 1, 257) <2 x i16> @infer_nowrap_poison( ; CHECK-SAME: <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> ; CHECK-NEXT: [[RES:%.*]] = add nuw nsw <2 x i16> [[ZEXT]], <i16 1, i16 poison> @@ -108,7 +108,7 @@ define <2 x i16> @infer_nowrap_poison(<2 x i8> %a) { } define <2 x i16> @infer_nowrap_nonsplat_nsw_only(<2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @infer_nowrap_nonsplat_nsw_only( +; CHECK-LABEL: define range(i16 -1, 257) <2 x i16> @infer_nowrap_nonsplat_nsw_only( ; CHECK-SAME: <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> ; CHECK-NEXT: [[RES:%.*]] = add nsw <2 x i16> [[ZEXT]], <i16 1, i16 -1> @@ -120,7 +120,7 @@ define <2 x i16> @infer_nowrap_nonsplat_nsw_only(<2 x i8> %a) { } define <2 x i16> @abs(<2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @abs( +; CHECK-LABEL: define range(i16 0, 256) <2 x i16> @abs( ; CHECK-SAME: <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> ; CHECK-NEXT: ret <2 x i16> [[ZEXT]] @@ -131,7 +131,7 @@ define <2 x i16> @abs(<2 x i8> %a) { } define <2 x i16> @saturating(<2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @saturating( +; CHECK-LABEL: define range(i16 1, 257) <2 x i16> @saturating( ; CHECK-SAME: <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> ; CHECK-NEXT: [[RES:%.*]] = add nuw nsw <2 x i16> [[ZEXT]], <i16 1, i16 1> @@ -156,7 +156,7 @@ define {<2 x i16>, <2 x i1>} @with_overflow(<2 x i8> %a) { } define <2 x i16> @srem1(<2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @srem1( +; CHECK-LABEL: define range(i16 0, 42) <2 x i16> @srem1( ; CHECK-SAME: <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> ; CHECK-NEXT: [[RES1_LHS_TRUNC:%.*]] = trunc <2 x i16> [[ZEXT]] to <2 x i8> @@ -170,7 +170,7 @@ define <2 x i16> @srem1(<2 x i8> %a) { } define <2 x i16> @srem2(<2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @srem2( +; CHECK-LABEL: define range(i16 -41, 42) <2 x i16> @srem2( ; CHECK-SAME: <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = sext <2 x i8> [[A]] to <2 x i16> ; CHECK-NEXT: [[RES_LHS_TRUNC:%.*]] = trunc <2 x i16> [[ZEXT]] to <2 x i8> @@ -184,7 +184,7 @@ define <2 x i16> @srem2(<2 x i8> %a) { } define <2 x i16> @ashr(<2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @ashr( +; CHECK-LABEL: define range(i16 0, 128) <2 x i16> @ashr( ; CHECK-SAME: <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> ; CHECK-NEXT: [[RES:%.*]] = lshr <2 x i16> [[ZEXT]], <i16 1, i16 1> @@ -196,7 +196,7 @@ define <2 x i16> @ashr(<2 x i8> %a) { } define <2 x i32> @sext(<2 x i8> %a) { -; CHECK-LABEL: define <2 x i32> @sext( +; CHECK-LABEL: define range(i32 0, 256) <2 x i32> @sext( ; CHECK-SAME: <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> ; CHECK-NEXT: [[RES:%.*]] = zext nneg <2 x i16> [[ZEXT]] to <2 x i32> @@ -220,7 +220,7 @@ define <2 x float> @sitofp(<2 x i8> %a) { } define <2 x i16> @and(<2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @and( +; CHECK-LABEL: define range(i16 0, 256) <2 x i16> @and( ; CHECK-SAME: <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> ; CHECK-NEXT: ret <2 x i16> [[ZEXT]] @@ -231,7 +231,7 @@ define <2 x i16> @and(<2 x i8> %a) { } define <2 x i16> @and_with_poison(<2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @and_with_poison( +; CHECK-LABEL: define range(i16 0, 256) <2 x i16> @and_with_poison( ; CHECK-SAME: <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> ; CHECK-NEXT: ret <2 x i16> [[ZEXT]] @@ -242,7 +242,7 @@ define <2 x i16> @and_with_poison(<2 x i8> %a) { } define <4 x i64> @issue_97674_getConstantOnEdge(i1 %cond) { -; CHECK-LABEL: define <4 x i64> @issue_97674_getConstantOnEdge( +; CHECK-LABEL: define range(i64 0, 2) <4 x i64> @issue_97674_getConstantOnEdge( ; CHECK-SAME: i1 [[COND:%.*]]) { ; CHECK-NEXT: [[ENTRY:.*]]: ; CHECK-NEXT: br i1 [[COND]], label %[[IF_THEN:.*]], label %[[IF_END:.*]] @@ -266,7 +266,7 @@ if.end: } define <4 x i64> @issue_97674_getConstant() { -; CHECK-LABEL: define <4 x i64> @issue_97674_getConstant() { +; CHECK-LABEL: define range(i64 0, 1) <4 x i64> @issue_97674_getConstant() { ; CHECK-NEXT: [[ENTRY:.*:]] ; CHECK-NEXT: [[FOLDS:%.*]] = add nuw nsw <4 x i64> zeroinitializer, zeroinitializer ; CHECK-NEXT: ret <4 x i64> zeroinitializer @@ -277,7 +277,7 @@ entry: } define <2 x i16> @phi_merge1(i1 %c, <2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @phi_merge1( +; CHECK-LABEL: define range(i16 2, 259) <2 x i16> @phi_merge1( ; CHECK-SAME: i1 [[C:%.*]], <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ENTRY:.*]]: ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> @@ -303,7 +303,7 @@ join: } define <2 x i16> @phi_merge2(i1 %c, <2 x i8> %a) { -; CHECK-LABEL: define <2 x i16> @phi_merge2( +; CHECK-LABEL: define range(i16 2, 259) <2 x i16> @phi_merge2( ; CHECK-SAME: i1 [[C:%.*]], <2 x i8> [[A:%.*]]) { ; CHECK-NEXT: [[ENTRY:.*]]: ; CHECK-NEXT: [[ZEXT:%.*]] = zext <2 x i8> [[A]] to <2 x i16> _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits