Add support for the llvm.memmove intrinsic
[oota-llvm.git] / lib / VMCore / Verifier.cpp
index 5c70b93fc511a81793b3e887e922afa079311fb0..e2ccb5b12b99fed706ca20babc9e407821f6ffff 100644 (file)
@@ -1,4 +1,11 @@
 //===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This file defines the function verifier interface, that can be used for some
 // sanity checking of input to the system.
@@ -33,6 +40,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Analysis/Verifier.h"
+#include "llvm/Assembly/Writer.h"
 #include "llvm/Pass.h"
 #include "llvm/Module.h"
 #include "llvm/DerivedTypes.h"
@@ -49,6 +57,7 @@
 #include "llvm/Support/InstVisitor.h"
 #include "Support/STLExtras.h"
 #include <algorithm>
+using namespace llvm;
 
 namespace {  // Anonymous namespace for class
 
@@ -56,7 +65,7 @@ namespace {  // Anonymous namespace for class
     bool Broken;          // Is this module found to be broken?
     bool RealPass;        // Are we not being run by a PassManager?
     bool AbortBroken;     // If broken, should it or should it not abort?
-    
+    Module *Mod;      // Module we are verifying right now
     DominatorSet *DS; // Dominator set, caution can be null!
 
     Verifier() : Broken(false), RealPass(true), AbortBroken(true), DS(0) {}
@@ -66,6 +75,7 @@ namespace {  // Anonymous namespace for class
 
 
     bool doInitialization(Module &M) {
+      Mod = &M;
       verifySymbolTable(M.getSymbolTable());
 
       // If this is a real pass, in a pass manager, we must abort before
@@ -98,8 +108,7 @@ namespace {  // Anonymous namespace for class
         visitGlobalValue(*I);
 
       for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
-        if (I->isExternal() && I->hasInternalLinkage())
-          CheckFailed("Global Variable is external with internal linkage!", I);
+        visitGlobalValue(*I);
 
       // If the module is broken, abort at this time.
       abortIfBroken();
@@ -142,26 +151,41 @@ namespace {  // Anonymous namespace for class
     void visitReturnInst(ReturnInst &RI);
     void visitUserOp1(Instruction &I);
     void visitUserOp2(Instruction &I) { visitUserOp1(I); }
-    void visitIntrinsicFunctionCall(LLVMIntrinsic::ID ID, CallInst &CI);
+    void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI);
+
+
+    void WriteValue(const Value *V) {
+      if (!V) return;
+      if (isa<Instruction>(V)) {
+        std::cerr << *V;
+      } else if (const Type *Ty = dyn_cast<Type>(V)) {
+        WriteTypeSymbolic(std::cerr, Ty, Mod);
+      } else {
+        WriteAsOperand (std::cerr, V, true, true, Mod);
+        std::cerr << "\n";
+      }
+    }
+
 
     // CheckFailed - A check failed, so print out the condition and the message
     // that failed.  This provides a nice place to put a breakpoint if you want
     // to see why something is not correct.
     //
-    inline void CheckFailed(const std::string &Message,
-                            const Value *V1 = 0, const Value *V2 = 0,
-                            const Value *V3 = 0, const Value *V4 = 0) {
+    void CheckFailed(const std::string &Message,
+                     const Value *V1 = 0, const Value *V2 = 0,
+                     const Value *V3 = 0, const Value *V4 = 0) {
       std::cerr << Message << "\n";
-      if (V1) std::cerr << *V1 << "\n";
-      if (V2) std::cerr << *V2 << "\n";
-      if (V3) std::cerr << *V3 << "\n";
-      if (V4) std::cerr << *V4 << "\n";
+      WriteValue(V1);
+      WriteValue(V2);
+      WriteValue(V3);
+      WriteValue(V4);
       Broken = true;
     }
   };
 
-  RegisterPass<Verifier> X("verify", "Module Verifier");
-}
+  RegisterOpt<Verifier> X("verify", "Module Verifier");
+} // End anonymous namespace
+
 
 // Assert - We know that cond should be true, if not print an error message.
 #define Assert(C, M) \
@@ -178,7 +202,7 @@ namespace {  // Anonymous namespace for class
 
 void Verifier::visitGlobalValue(GlobalValue &GV) {
   Assert1(!GV.isExternal() || GV.hasExternalLinkage(),
-          "Global value has Internal Linkage!", &GV);
+          "Global is external, but doesn't have external linkage!", &GV);
   Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
           "Only global variables can have appending linkage!", &GV);
 
@@ -217,6 +241,9 @@ void Verifier::visitFunction(Function &F) {
   Assert2(FT->getNumParams() == NumArgs,
           "# formal arguments must match # of arguments for function type!",
           &F, FT);
+  Assert1(F.getReturnType()->isFirstClassType() ||
+          F.getReturnType() == Type::VoidTy,
+          "Functions cannot return aggregate values!", &F);
 
   // Check that the argument values match the function type for this function...
   unsigned i = 0;
@@ -332,6 +359,12 @@ void Verifier::visitPHINode(PHINode &PN) {
           "PHI nodes not grouped at top of basic block!",
           &PN, PN.getParent());
 
+  // Check that all of the operands of the PHI node have the same type as the
+  // result.
+  for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
+    Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
+            "PHI node operands are not the same type as the result!", &PN);
+
   // All other PHI node constraints are checked in the visitBasicBlock method.
 
   visitInstruction(PN);
@@ -361,7 +394,7 @@ void Verifier::visitCallInst(CallInst &CI) {
             CI.getOperand(i+1), FTy->getParamType(i));
 
   if (Function *F = CI.getCalledFunction())
-    if (LLVMIntrinsic::ID ID = (LLVMIntrinsic::ID)F->getIntrinsicID())
+    if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
       visitIntrinsicFunctionCall(ID, CI);
 
   visitInstruction(CI);
@@ -422,7 +455,7 @@ void Verifier::visitLoadInst(LoadInst &LI) {
   const Type *ElTy =
     cast<PointerType>(LI.getOperand(0)->getType())->getElementType();
   Assert2(ElTy == LI.getType(),
-          "Load is not of right type for indices!", &LI, ElTy);
+          "Load result type does not match pointer operand type!", &LI, ElTy);
   visitInstruction(LI);
 }
 
@@ -430,7 +463,7 @@ void Verifier::visitStoreInst(StoreInst &SI) {
   const Type *ElTy =
     cast<PointerType>(SI.getOperand(1)->getType())->getElementType();
   Assert2(ElTy == SI.getOperand(0)->getType(),
-          "Stored value is not of right type for indices!", &SI, ElTy);
+          "Stored value type does not match pointer operand type!", &SI, ElTy);
   visitInstruction(SI);
 }
 
@@ -473,18 +506,25 @@ void Verifier::visitInstruction(Instruction &I) {
               "Cannot take the address of an intrinsic!", &I);
 
     else if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) {
+      BasicBlock *OpBlock = Op->getParent();
+
       // Check that a definition dominates all of its uses.
       //
       if (!isa<PHINode>(I)) {
+        // Invoke results are only usable in the normal destination, not in the
+        // exceptional destination.
+        if (InvokeInst *II = dyn_cast<InvokeInst>(Op))
+          OpBlock = II->getNormalDest();
+
         // Definition must dominate use unless use is unreachable!
-        Assert2(DS->dominates(Op->getParent(), BB) ||
+        Assert2(DS->dominates(OpBlock, BB) ||
                 !DS->dominates(&BB->getParent()->getEntryBlock(), BB),
                 "Instruction does not dominate all uses!", Op, &I);
       } else {
         // PHI nodes are more difficult than other nodes because they actually
         // "use" the value in the predecessor basic blocks they correspond to.
         BasicBlock *PredBB = cast<BasicBlock>(I.getOperand(i+1));
-        Assert2(DS->dominates(Op->getParent(), PredBB) ||
+        Assert2(DS->dominates(OpBlock, PredBB) ||
                 !DS->dominates(&BB->getParent()->getEntryBlock(), PredBB),
                 "Instruction does not dominate all uses!", Op, &I);
       }
@@ -493,7 +533,7 @@ void Verifier::visitInstruction(Instruction &I) {
 }
 
 /// visitIntrinsicFunction - Allow intrinsics to be verified in different ways.
-void Verifier::visitIntrinsicFunctionCall(LLVMIntrinsic::ID ID, CallInst &CI) {
+void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
   Function *IF = CI.getCalledFunction();
   const FunctionType *FT = IF->getFunctionType();
   Assert1(IF->isExternal(), "Intrinsic functions should never be defined!", IF);
@@ -502,37 +542,46 @@ void Verifier::visitIntrinsicFunctionCall(LLVMIntrinsic::ID ID, CallInst &CI) {
   // FIXME: this should check the return type of each intrinsic as well, also
   // arguments!
   switch (ID) {
-  case LLVMIntrinsic::va_start:
+  case Intrinsic::va_start:
     Assert1(CI.getParent()->getParent()->getFunctionType()->isVarArg(),
             "llvm.va_start intrinsic may only occur in function with variable"
             " args!", &CI);
     NumArgs = 0;
     break;
-  case LLVMIntrinsic::va_end:          NumArgs = 1; break;
-  case LLVMIntrinsic::va_copy:         NumArgs = 1; break;
-
-  case LLVMIntrinsic::setjmp:          NumArgs = 1; break;
-  case LLVMIntrinsic::longjmp:         NumArgs = 2; break;
-  case LLVMIntrinsic::sigsetjmp:       NumArgs = 2; break;
-  case LLVMIntrinsic::siglongjmp:      NumArgs = 2; break;
+  case Intrinsic::va_end:          NumArgs = 1; break;
+  case Intrinsic::va_copy:         NumArgs = 1; break;
+
+  case Intrinsic::setjmp:          NumArgs = 1; break;
+  case Intrinsic::longjmp:         NumArgs = 2; break;
+  case Intrinsic::sigsetjmp:       NumArgs = 2; break;
+  case Intrinsic::siglongjmp:      NumArgs = 2; break;
+
+  case Intrinsic::dbg_stoppoint:   NumArgs = 4; break;
+  case Intrinsic::dbg_region_start:NumArgs = 1; break;
+  case Intrinsic::dbg_region_end:  NumArgs = 1; break;
+  case Intrinsic::dbg_func_start:  NumArgs = 1; break;
+  case Intrinsic::dbg_declare:     NumArgs = 1; break;
+
+  case Intrinsic::memcpy:          NumArgs = 4; break;
+  case Intrinsic::memmove:         NumArgs = 4; break;
  
-  case LLVMIntrinsic::alpha_ctlz:      NumArgs = 1; break;
-  case LLVMIntrinsic::alpha_cttz:      NumArgs = 1; break;
-  case LLVMIntrinsic::alpha_ctpop:     NumArgs = 1; break;
-  case LLVMIntrinsic::alpha_umulh:     NumArgs = 2; break;
-  case LLVMIntrinsic::alpha_vecop:     NumArgs = 4; break;
-  case LLVMIntrinsic::alpha_pup:       NumArgs = 3; break;
-  case LLVMIntrinsic::alpha_bytezap:   NumArgs = 2; break;
-  case LLVMIntrinsic::alpha_bytemanip: NumArgs = 3; break;
-  case LLVMIntrinsic::alpha_dfpbop:    NumArgs = 3; break;
-  case LLVMIntrinsic::alpha_dfpuop:    NumArgs = 2; break;
-  case LLVMIntrinsic::alpha_unordered: NumArgs = 2; break;
-  case LLVMIntrinsic::alpha_uqtodfp:   NumArgs = 2; break;
-  case LLVMIntrinsic::alpha_uqtosfp:   NumArgs = 2; break;
-  case LLVMIntrinsic::alpha_dfptosq:   NumArgs = 2; break;
-  case LLVMIntrinsic::alpha_sfptosq:   NumArgs = 2; break;
-
-  case LLVMIntrinsic::not_intrinsic: 
+  case Intrinsic::alpha_ctlz:      NumArgs = 1; break;
+  case Intrinsic::alpha_cttz:      NumArgs = 1; break;
+  case Intrinsic::alpha_ctpop:     NumArgs = 1; break;
+  case Intrinsic::alpha_umulh:     NumArgs = 2; break;
+  case Intrinsic::alpha_vecop:     NumArgs = 4; break;
+  case Intrinsic::alpha_pup:       NumArgs = 3; break;
+  case Intrinsic::alpha_bytezap:   NumArgs = 2; break;
+  case Intrinsic::alpha_bytemanip: NumArgs = 3; break;
+  case Intrinsic::alpha_dfpbop:    NumArgs = 3; break;
+  case Intrinsic::alpha_dfpuop:    NumArgs = 2; break;
+  case Intrinsic::alpha_unordered: NumArgs = 2; break;
+  case Intrinsic::alpha_uqtodfp:   NumArgs = 2; break;
+  case Intrinsic::alpha_uqtosfp:   NumArgs = 2; break;
+  case Intrinsic::alpha_dfptosq:   NumArgs = 2; break;
+  case Intrinsic::alpha_sfptosq:   NumArgs = 2; break;
+
+  case Intrinsic::not_intrinsic: 
     assert(0 && "Invalid intrinsic!"); NumArgs = 0; break;
   }
 
@@ -546,13 +595,13 @@ void Verifier::visitIntrinsicFunctionCall(LLVMIntrinsic::ID ID, CallInst &CI) {
 //  Implement the public interfaces to this file...
 //===----------------------------------------------------------------------===//
 
-FunctionPass *createVerifierPass() {
+FunctionPass *llvm::createVerifierPass() {
   return new Verifier();
 }
 
 
 // verifyFunction - Create 
-bool verifyFunction(const Function &f) {
+bool llvm::verifyFunction(const Function &f) {
   Function &F = (Function&)f;
   assert(!F.isExternal() && "Cannot verify external functions");
 
@@ -571,7 +620,7 @@ bool verifyFunction(const Function &f) {
 // verifyModule - Check a module for errors, printing messages on stderr.
 // Return true if the module is corrupt.
 //
-bool verifyModule(const Module &M) {
+bool llvm::verifyModule(const Module &M) {
   PassManager PM;
   Verifier *V = new Verifier();
   PM.add(V);