From aefab6f8d5b13c41d000feaa8f0e567d4b6a4681 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Tue, 4 Jan 2022 12:10:45 +0100 Subject: [PATCH] [InstSimplify] Use weak symbol in test to show miscompile (NFC) This fold is incorrect, because it assumes that all indices are non-zero. This happens to be true for the test as written, but doesn't hold if we use an extern weak global instead, for which ptrtoint might be zero. Add separate tests for the simple constant int case. --- .../InstSimplify/ConstProp/icmp-global.ll | 27 ++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll index e681c62..b2d6b3f 100644 --- a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll +++ b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll @@ -65,6 +65,7 @@ define i1 @ult_constexpr_constexpr_one(i8* %x) { @g = global [2 x i32] [i32 1, i32 2] @g2 = global i32 0 +@g2_weak = extern_weak global i32 define i1 @global_ne_null() { ; CHECK-LABEL: @global_ne_null( @@ -121,7 +122,7 @@ define i1 @null_gep_ne_null() { ; CHECK-LABEL: @null_gep_ne_null( ; CHECK-NEXT: ret i1 true ; - %gep = getelementptr i8, i8* null, i64 ptrtoint (i32* @g2 to i64) + %gep = getelementptr i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64) %cmp = icmp ne i8* %gep, null ret i1 %cmp } @@ -130,20 +131,38 @@ define i1 @null_gep_ugt_null() { ; CHECK-LABEL: @null_gep_ugt_null( ; CHECK-NEXT: ret i1 true ; - %gep = getelementptr i8, i8* null, i64 ptrtoint (i32* @g2 to i64) + %gep = getelementptr i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64) %cmp = icmp ugt i8* %gep, null ret i1 %cmp } define i1 @null_gep_sgt_null() { ; CHECK-LABEL: @null_gep_sgt_null( -; CHECK-NEXT: ret i1 icmp sgt (i8* getelementptr (i8, i8* null, i64 ptrtoint (i32* @g2 to i64)), i8* null) +; CHECK-NEXT: ret i1 icmp sgt (i8* getelementptr (i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64)), i8* null) ; - %gep = getelementptr i8, i8* null, i64 ptrtoint (i32* @g2 to i64) + %gep = getelementptr i8, i8* null, i64 ptrtoint (i32* @g2_weak to i64) %cmp = icmp sgt i8* %gep, null ret i1 %cmp } +define i1 @null_gep_ne_null_constant_int() { +; CHECK-LABEL: @null_gep_ne_null_constant_int( +; CHECK-NEXT: ret i1 true +; + %gep = getelementptr i8, i8* null, i64 1 + %cmp = icmp ne i8* %gep, null + ret i1 %cmp +} + +define i1 @null_gep_ugt_null_constant_int() { +; CHECK-LABEL: @null_gep_ugt_null_constant_int( +; CHECK-NEXT: ret i1 true +; + %gep = getelementptr i8, i8* null, i64 1 + %cmp = icmp ugt i8* %gep, null + ret i1 %cmp +} + define i1 @null_gep_ne_global() { ; CHECK-LABEL: @null_gep_ne_global( ; CHECK-NEXT: ret i1 true -- 2.7.4