[llvm] 84b4f5a - [InstCombine] Negator: while there, add detection for cycles during negation

Roman Lebedev via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 17 12:48:16 PDT 2020


Author: Roman Lebedev
Date: 2020-06-17T22:47:20+03:00
New Revision: 84b4f5a6a6bc834c325edef7baec335b81b6e367

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

LOG: [InstCombine] Negator: while there, add detection for cycles during negation

I don't have any testcases showing it happening,
and i haven't succeeded in creating one,
but i'm also not positive it can't ever happen,
and i recall having something that looked like
that in the very beginning of Negator creation.

But since we now already have a negation cache,
we can now detect such cases practically for free.

Let's do so instead of "relying" on stack overflow :D

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
index deb14f1818ef..3fe615ac5439 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineNegator.cpp
@@ -388,16 +388,30 @@ LLVM_NODISCARD Value *Negator::negate(Value *V, unsigned Depth) {
   ++NumValuesVisitedInThisNegator;
 #endif
 
+#ifndef NDEBUG
+  // We can't ever have a Value with such an address.
+  Value *Placeholder = reinterpret_cast<Value *>(static_cast<uintptr_t>(-1));
+#endif
+
   // Did we already try to negate this value?
   auto NegationsCacheIterator = NegationsCache.find(V);
   if (NegationsCacheIterator != NegationsCache.end()) {
     ++NegatorNumNegationsFoundInCache;
-    return NegationsCacheIterator->second;
+    Value *NegatedV = NegationsCacheIterator->second;
+    assert(NegatedV != Placeholder && "Encountered a cycle during negation.");
+    return NegatedV;
   }
 
+#ifndef NDEBUG
+  // We did not find a cached result for negation of V. While there,
+  // let's temporairly cache a placeholder value, with the idea that if later
+  // during negation we fetch it from cache, we'll know we're in a cycle.
+  NegationsCache[V] = Placeholder;
+#endif
+
   // No luck. Try negating it for real.
   Value *NegatedV = visitImpl(V, Depth);
-  // And cache the result for the future.
+  // And cache the (real) result for the future.
   NegationsCache[V] = NegatedV;
 
   return NegatedV;


        


More information about the llvm-commits mailing list