OSDN Git Service

Teach the verifier to check the condition on a branch and ensure that it has
authorNick Lewycky <nicholas@mxc.ca>
Mon, 15 Feb 2010 22:09:09 +0000 (22:09 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Mon, 15 Feb 2010 22:09:09 +0000 (22:09 +0000)
'i1' type.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@96282 91177308-0d34-0410-b5e6-96231b3b80d8

lib/VMCore/Verifier.cpp
unittests/VMCore/VerifierTest.cpp [new file with mode: 0644]

index be86614..2b4892b 100644 (file)
@@ -317,6 +317,7 @@ namespace {
     void visitStoreInst(StoreInst &SI);
     void visitInstruction(Instruction &I);
     void visitTerminatorInst(TerminatorInst &I);
+    void visitBranchInst(BranchInst &BI);
     void visitReturnInst(ReturnInst &RI);
     void visitSwitchInst(SwitchInst &SI);
     void visitSelectInst(SelectInst &SI);
@@ -749,6 +750,14 @@ void Verifier::visitTerminatorInst(TerminatorInst &I) {
   visitInstruction(I);
 }
 
+void Verifier::visitBranchInst(BranchInst &BI) {
+  if (BI.isConditional()) {
+    Assert2(BI.getCondition()->getType()->isIntegerTy(1),
+            "Branch condition is not 'i1' type!", &BI, BI.getCondition());
+  }
+  visitTerminatorInst(BI);
+}
+
 void Verifier::visitReturnInst(ReturnInst &RI) {
   Function *F = RI.getParent()->getParent();
   unsigned N = RI.getNumOperands();
diff --git a/unittests/VMCore/VerifierTest.cpp b/unittests/VMCore/VerifierTest.cpp
new file mode 100644 (file)
index 0000000..c8838c5
--- /dev/null
@@ -0,0 +1,44 @@
+//===- llvm/unittest/VMCore/VerifierTest.cpp - Verifier 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/Constants.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Function.h"
+#include "llvm/Instructions.h"
+#include "llvm/LLVMContext.h"
+#include "llvm/Analysis/Verifier.h"
+#include "gtest/gtest.h"
+
+namespace llvm {
+namespace {
+
+TEST(VerifierTest, Branch_i1) {
+  LLVMContext &C = getGlobalContext();
+  FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), /*isVarArg=*/false);
+  Function *F = Function::Create(FTy, GlobalValue::ExternalLinkage);
+  BasicBlock *Entry = BasicBlock::Create(C, "entry", F);
+  BasicBlock *Exit = BasicBlock::Create(C, "exit", F);
+  ReturnInst::Create(C, Exit);
+
+  // To avoid triggering an assertion in BranchInst::Create, we first create
+  // a branch with an 'i1' condition ...
+
+  Constant *False = ConstantInt::getFalse(C);
+  BranchInst *BI = BranchInst::Create(Exit, Exit, False, Entry);
+
+  // ... then use setOperand to redirect it to a value of different type.
+
+  Constant *Zero32 = ConstantInt::get(IntegerType::get(C, 32), 0);
+  BI->setOperand(0, Zero32);
+
+  EXPECT_TRUE(verifyFunction(*F, ReturnStatusAction));
+}
+
+}
+}