[llvm-branch-commits] [llvm] 05a4770 - [ConstantFold] Fix incorrect type assumptions

Nikita Popov via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Nov 3 13:41:20 PDT 2023


Author: Nikita Popov
Date: 2023-11-03T13:12:39+01:00
New Revision: 05a47706476f3cd467aa1c4347fdfb71bcbc1252

URL: https://github.com/llvm/llvm-project/commit/05a47706476f3cd467aa1c4347fdfb71bcbc1252
DIFF: https://github.com/llvm/llvm-project/commit/05a47706476f3cd467aa1c4347fdfb71bcbc1252.diff

LOG: [ConstantFold] Fix incorrect type assumptions

If a pointer isn't a constant expression, global or block address,
it's not guaranteed to be a null pointer. It can also be a no_cfi
or dso_local_equivalent constant.

Added: 
    

Modified: 
    llvm/lib/IR/ConstantFold.cpp
    llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index d4bc74e00f19bb9..3f5da236af211f6 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1172,30 +1172,24 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2) {
   }
 
   if (const BlockAddress *BA = dyn_cast<BlockAddress>(V1)) {
-    // Now we know that the RHS is a BlockAddress or simple
-    // constant (which, since the types must match, means that it is a
-    // ConstantPointerNull).
+    // Now we know that the RHS is a BlockAddress or simple constant.
     if (const BlockAddress *BA2 = dyn_cast<BlockAddress>(V2)) {
       // Block address in another function can't equal this one, but block
       // addresses in the current function might be the same if blocks are
       // empty.
       if (BA2->getFunction() != BA->getFunction())
         return ICmpInst::ICMP_NE;
-    } else {
-      // Block addresses aren't null.
-      assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
+    } else if (isa<ConstantPointerNull>(V2)) {
       return ICmpInst::ICMP_NE;
     }
   } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V1)) {
     // Now we know that the RHS is a GlobalValue, BlockAddress or simple
-    // constant (which, since the types must match, means that it's a
-    // ConstantPointerNull).
+    // constant.
     if (const GlobalValue *GV2 = dyn_cast<GlobalValue>(V2)) {
       return areGlobalsPotentiallyEqual(GV, GV2);
     } else if (isa<BlockAddress>(V2)) {
       return ICmpInst::ICMP_NE; // Globals never equal labels.
-    } else {
-      assert(isa<ConstantPointerNull>(V2) && "Canonicalization guarantee!");
+    } else if (isa<ConstantPointerNull>(V2)) {
       // GlobalVals can never be null unless they have external weak linkage.
       // We don't try to evaluate aliases here.
       // NOTE: We should not be doing this constant folding if null pointer

diff  --git a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
index 701d911ea892ac4..3851bd090aef9f4 100644
--- a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global.ll
@@ -275,3 +275,26 @@ define i1 @global_gep_ugt_global_gep_complex() {
   %cmp = icmp ugt ptr %gep3, @g
   ret i1 %cmp
 }
+
+declare void @func()
+
+define i1 @global_no_cfi() {
+; CHECK-LABEL: @global_no_cfi(
+; CHECK-NEXT:    ret i1 icmp eq (ptr @func, ptr no_cfi @func)
+;
+  %cmp = icmp eq ptr @func, no_cfi @func
+  ret i1 %cmp
+}
+
+define i1 @blockaddr_no_cfi() {
+; CHECK-LABEL: @blockaddr_no_cfi(
+; CHECK-NEXT:    br label [[BB:%.*]]
+; CHECK:       bb:
+; CHECK-NEXT:    ret i1 icmp eq (ptr blockaddress(@blockaddr_no_cfi, [[BB]]), ptr no_cfi @func)
+;
+  br label %bb
+
+bb:
+  %cmp = icmp eq ptr blockaddress(@blockaddr_no_cfi, %bb), no_cfi @func
+  ret i1 %cmp
+}


        


More information about the llvm-branch-commits mailing list