[llvm] r179370 - Fix a disconcerting bug in Value::isUsedInBasicBlock, which gave wrong answers for blocks larger than 3 instrs.

Benjamin Kramer benny.kra at googlemail.com
Fri Apr 12 01:33:11 PDT 2013


Author: d0k
Date: Fri Apr 12 03:33:11 2013
New Revision: 179370

URL: http://llvm.org/viewvc/llvm-project?rev=179370&view=rev
Log:
Fix a disconcerting bug in Value::isUsedInBasicBlock, which gave wrong answers for blocks larger than 3 instrs.

Also add a unit test. PR15727.

Added:
    llvm/trunk/unittests/IR/ValueTest.cpp
Modified:
    llvm/trunk/lib/IR/Value.cpp
    llvm/trunk/unittests/IR/CMakeLists.txt

Modified: llvm/trunk/lib/IR/Value.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Value.cpp?rev=179370&r1=179369&r2=179370&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Value.cpp (original)
+++ llvm/trunk/lib/IR/Value.cpp Fri Apr 12 03:33:11 2013
@@ -118,7 +118,7 @@ bool Value::isUsedInBasicBlock(const Bas
   for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
     if (std::find(I->op_begin(), I->op_end(), this) != I->op_end())
       return true;
-    if (MaxBlockSize-- == 0) // If the block is larger fall back to use_iterator
+    if (--MaxBlockSize == 0) // If the block is larger fall back to use_iterator
       break;
   }
 

Modified: llvm/trunk/unittests/IR/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/CMakeLists.txt?rev=179370&r1=179369&r2=179370&view=diff
==============================================================================
--- llvm/trunk/unittests/IR/CMakeLists.txt (original)
+++ llvm/trunk/unittests/IR/CMakeLists.txt Fri Apr 12 03:33:11 2013
@@ -16,6 +16,7 @@ set(IRSources
   TypeBuilderTest.cpp
   TypesTest.cpp
   ValueMapTest.cpp
+  ValueTest.cpp
   VerifierTest.cpp
   WaymarkTest.cpp
   )

Added: llvm/trunk/unittests/IR/ValueTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/IR/ValueTest.cpp?rev=179370&view=auto
==============================================================================
--- llvm/trunk/unittests/IR/ValueTest.cpp (added)
+++ llvm/trunk/unittests/IR/ValueTest.cpp Fri Apr 12 03:33:11 2013
@@ -0,0 +1,46 @@
+//===- llvm/unittest/IR/ValueTest.cpp - Value unit tests ------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Assembly/Parser.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Value.h"
+#include "llvm/Support/SourceMgr.h"
+#include "gtest/gtest.h"
+using namespace llvm;
+
+namespace {
+
+TEST(ValueTest, UsedInBasicBlock) {
+  LLVMContext C;
+
+  const char *ModuleString = "define void @f(i32 %x, i32 %y) {\n"
+                             "bb0:\n"
+                             "  %y1 = add i32 %y, 1\n"
+                             "  %y2 = add i32 %y, 1\n"
+                             "  %y3 = add i32 %y, 1\n"
+                             "  %y4 = add i32 %y, 1\n"
+                             "  %y5 = add i32 %y, 1\n"
+                             "  %y6 = add i32 %y, 1\n"
+                             "  %y7 = add i32 %y, 1\n"
+                             "  %y8 = add i32 %x, 1\n"
+                             "  ret void\n"
+                             "}\n";
+  SMDiagnostic Err;
+  Module *M = ParseAssemblyString(ModuleString, NULL, Err, C);
+
+  Function *F = M->getFunction("f");
+
+  EXPECT_FALSE(F->isUsedInBasicBlock(F->begin()));
+  EXPECT_TRUE((++F->arg_begin())->isUsedInBasicBlock(F->begin()));
+  EXPECT_TRUE(F->arg_begin()->isUsedInBasicBlock(F->begin()));
+}
+
+} // end anonymous namespace





More information about the llvm-commits mailing list