[llvm] r208663 - Assert that we don't RAUW a Constant with a ConstantExpr that contains it.
Rafael Espindola
rafael.espindola at gmail.com
Mon May 12 18:23:21 PDT 2014
Author: rafael
Date: Mon May 12 20:23:21 2014
New Revision: 208663
URL: http://llvm.org/viewvc/llvm-project?rev=208663&view=rev
Log:
Assert that we don't RAUW a Constant with a ConstantExpr that contains it.
We already had an assert for foo->RAUW(foo), but not for something like
foo->RAUW(GEP(foo)) and would go in an infinite loop trying to apply
the replacement.
Modified:
llvm/trunk/lib/IR/Value.cpp
llvm/trunk/unittests/IR/ConstantsTest.cpp
Modified: llvm/trunk/lib/IR/Value.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Value.cpp?rev=208663&r1=208662&r2=208663&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Value.cpp (original)
+++ llvm/trunk/lib/IR/Value.cpp Mon May 12 20:23:21 2014
@@ -301,10 +301,45 @@ void Value::takeName(Value *V) {
ST->reinsertValue(this);
}
+#ifndef NDEBUG
+static bool contains(SmallPtrSet<ConstantExpr *, 4> &Cache, ConstantExpr *Expr,
+ Constant *C) {
+ if (!Cache.insert(Expr))
+ return false;
+
+ for (auto &O : Expr->operands()) {
+ if (O == C)
+ return true;
+ auto *CE = dyn_cast<ConstantExpr>(O);
+ if (!CE)
+ continue;
+ if (contains(Cache, CE, C))
+ return true;
+ }
+ return false;
+}
+
+static bool contains(Value *Expr, Value *V) {
+ if (Expr == V)
+ return true;
+
+ auto *C = dyn_cast<Constant>(V);
+ if (!C)
+ return false;
+
+ auto *CE = dyn_cast<ConstantExpr>(Expr);
+ if (!CE)
+ return false;
+
+ SmallPtrSet<ConstantExpr *, 4> Cache;
+ return contains(Cache, CE, C);
+}
+#endif
void Value::replaceAllUsesWith(Value *New) {
assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
- assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!");
+ assert(!contains(New, this) &&
+ "this->replaceAllUsesWith(expr(this)) is NOT valid!");
assert(New->getType() == getType() &&
"replaceAllUses of value with new value of different type!");
Modified: llvm/trunk/unittests/IR/ConstantsTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/ConstantsTest.cpp?rev=208663&r1=208662&r2=208663&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/ConstantsTest.cpp (original)
+++ llvm/trunk/unittests/IR/ConstantsTest.cpp Mon May 12 20:23:21 2014
@@ -254,6 +254,23 @@ TEST(ConstantsTest, AsInstructionsTest)
P6STR ", i32 1");
}
+#ifdef GTEST_HAS_DEATH_TEST
+#ifndef NDEBUG
+TEST(ConstantsTest, ReplaceWithConstantTest) {
+ std::unique_ptr<Module> M(new Module("MyModule", getGlobalContext()));
+
+ Type *Int32Ty = Type::getInt32Ty(getGlobalContext());
+ Constant *One = ConstantInt::get(Int32Ty, 1);
+
+ Constant *Global =
+ M->getOrInsertGlobal("dummy", PointerType::getUnqual(Int32Ty));
+ Constant *GEP = ConstantExpr::getGetElementPtr(Global, One);
+ EXPECT_DEATH(Global->replaceAllUsesWith(GEP),
+ "this->replaceAllUsesWith\\(expr\\(this\\)\\) is NOT valid!");
+}
+#endif
+#endif
+
#undef CHECK
} // end anonymous namespace
More information about the llvm-commits
mailing list