[llvm] f85ce1b - ConstantFold: Reduce code duplication for checking commuted compare

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 26 11:00:22 PDT 2022


Author: Matt Arsenault
Date: 2022-10-26T10:59:58-07:00
New Revision: f85ce1b236d50ebc1dd5960ce88026138506697a

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

LOG: ConstantFold: Reduce code duplication for checking commuted compare

Added: 
    

Modified: 
    llvm/lib/IR/ConstantFold.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index 8e7ecfc0e563d..74b7d198e9338 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -1579,6 +1579,25 @@ static ICmpInst::Predicate evaluateICmpRelation(Constant *V1, Constant *V2,
   return ICmpInst::BAD_ICMP_PREDICATE;
 }
 
+static Constant *constantFoldCompareGlobalToNull(CmpInst::Predicate Predicate,
+                                                 Constant *C1, Constant *C2) {
+  const GlobalValue *GV = dyn_cast<GlobalValue>(C2);
+  if (!GV || !C1->isNullValue())
+    return nullptr;
+
+  // Don't try to evaluate aliases.  External weak GV can be null.
+  if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
+      !NullPointerIsDefined(nullptr /* F */,
+                            GV->getType()->getAddressSpace())) {
+    if (Predicate == ICmpInst::ICMP_EQ)
+      return ConstantInt::getFalse(C1->getContext());
+    else if (Predicate == ICmpInst::ICMP_NE)
+      return ConstantInt::getTrue(C1->getContext());
+  }
+
+  return nullptr;
+}
+
 Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
                                                Constant *C1, Constant *C2) {
   Type *ResultTy;
@@ -1618,31 +1637,14 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
   }
 
   // icmp eq/ne(null,GV) -> false/true
-  if (C1->isNullValue()) {
-    if (const GlobalValue *GV = dyn_cast<GlobalValue>(C2))
-      // Don't try to evaluate aliases.  External weak GV can be null.
-      if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
-          !NullPointerIsDefined(nullptr /* F */,
-                                GV->getType()->getAddressSpace())) {
-        if (Predicate == ICmpInst::ICMP_EQ)
-          return ConstantInt::getFalse(C1->getContext());
-        else if (Predicate == ICmpInst::ICMP_NE)
-          return ConstantInt::getTrue(C1->getContext());
-      }
+  if (Constant *Folded = constantFoldCompareGlobalToNull(Predicate, C1, C2))
+    return Folded;
+
   // icmp eq/ne(GV,null) -> false/true
-  } else if (C2->isNullValue()) {
-    if (const GlobalValue *GV = dyn_cast<GlobalValue>(C1)) {
-      // Don't try to evaluate aliases.  External weak GV can be null.
-      if (!isa<GlobalAlias>(GV) && !GV->hasExternalWeakLinkage() &&
-          !NullPointerIsDefined(nullptr /* F */,
-                                GV->getType()->getAddressSpace())) {
-        if (Predicate == ICmpInst::ICMP_EQ)
-          return ConstantInt::getFalse(C1->getContext());
-        else if (Predicate == ICmpInst::ICMP_NE)
-          return ConstantInt::getTrue(C1->getContext());
-      }
-    }
+  if (Constant *Folded = constantFoldCompareGlobalToNull(Predicate, C2, C1))
+    return Folded;
 
+  if (C2->isNullValue()) {
     // The caller is expected to commute the operands if the constant expression
     // is C2.
     // C1 >= 0 --> true


        


More information about the llvm-commits mailing list