[llvm] ce190e4 - [ConstraintElimination] Negate IR condition directly.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 1 09:34:28 PST 2021


Author: Florian Hahn
Date: 2021-02-01T17:21:40Z
New Revision: ce190e41445eb52e560ac70dd1df74717a1e80d2

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

LOG: [ConstraintElimination] Negate IR condition directly.

Instead of using ConstraintSystem::negate when adding new constraints,
flip the condition in IR.

The main advantage is that EQ predicates can be represented by 2
constraints, which makes negating based on the constraint tricky. The IR
condition can easily negated.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
index a3f739fd5c34..dc105e4eaaa7 100644
--- a/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
+++ b/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp
@@ -13,6 +13,7 @@
 
 #include "llvm/Transforms/Scalar/ConstraintElimination.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/ScopeExit.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Analysis/ConstraintSystem.h"
@@ -336,6 +337,22 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
       continue;
     }
 
+    // Set up a function to restore the predicate at the end of the scope if it
+    // has been negated. Negate the predicate in-place, if required.
+    auto *CI = dyn_cast<CmpInst>(CB.Condition);
+    auto PredicateRestorer = make_scope_exit([CI, &CB]() {
+      if (CB.Not && CI)
+        CI->setPredicate(CI->getInversePredicate());
+    });
+    if (CB.Not) {
+      if (CI) {
+        CI->setPredicate(CI->getInversePredicate());
+      } else {
+        LLVM_DEBUG(dbgs() << "Can only negate compares so far.\n");
+        continue;
+      }
+    }
+
     // Otherwise, add the condition to the system and stack, if we can transform
     // it into a constraint.
     auto R = getConstraint(CB.Condition, Value2Index, true);
@@ -343,8 +360,6 @@ static bool eliminateConstraints(Function &F, DominatorTree &DT) {
       continue;
 
     LLVM_DEBUG(dbgs() << "Adding " << *CB.Condition << " " << CB.Not << "\n");
-    if (CB.Not)
-      R = ConstraintSystem::negate(R);
 
     // If R has been added to the system, queue it for removal once it goes
     // out-of-scope.


        


More information about the llvm-commits mailing list