Adding dllimport, dllexport and external weak linkage types.
[oota-llvm.git] / lib / VMCore / Verifier.cpp
index f6fd236bd16996a94244c538319966e9643d84fd..cbef68b7dc35d951673edd40b66f1e18a1f59ed6 100644 (file)
 #include "llvm/Support/InstVisitor.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Compiler.h"
 #include <algorithm>
 #include <iostream>
 #include <sstream>
+#include <cstdarg>
 using namespace llvm;
 
 namespace {  // Anonymous namespace for class
 
-  struct Verifier : public FunctionPass, InstVisitor<Verifier> {
+  struct VISIBILITY_HIDDEN
+     Verifier : public FunctionPass, InstVisitor<Verifier> {
     bool Broken;          // Is this module found to be broken?
     bool RealPass;        // Are we not being run by a PassManager?
     VerifierFailureAction action;
@@ -102,7 +105,7 @@ namespace {  // Anonymous namespace for class
       // returning back to the pass manager, or else the pass manager may try to
       // run other passes on the broken module.
       if (RealPass)
-        abortIfBroken();
+        return abortIfBroken();
       return false;
     }
 
@@ -116,7 +119,7 @@ namespace {  // Anonymous namespace for class
       // returning back to the pass manager, or else the pass manager may try to
       // run other passes on the broken module.
       if (RealPass)
-        abortIfBroken();
+        return abortIfBroken();
 
       return false;
     }
@@ -135,8 +138,7 @@ namespace {  // Anonymous namespace for class
         visitGlobalVariable(*I);
 
       // If the module is broken, abort at this time.
-      abortIfBroken();
-      return false;
+      return abortIfBroken();
     }
 
     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
@@ -148,27 +150,24 @@ namespace {  // Anonymous namespace for class
     /// abortIfBroken - If the module is broken and we are supposed to abort on
     /// this condition, do so.
     ///
-    void abortIfBroken() {
-      if (Broken)
-      {
+    bool abortIfBroken() {
+      if (Broken) {
         msgs << "Broken module found, ";
-        switch (action)
-        {
+        switch (action) {
           case AbortProcessAction:
             msgs << "compilation aborted!\n";
             std::cerr << msgs.str();
             abort();
-          case ThrowExceptionAction:
-            msgs << "verification terminated.\n";
-            throw msgs.str();
           case PrintMessageAction:
             msgs << "verification continues.\n";
             std::cerr << msgs.str();
-            break;
+            return false;
           case ReturnStatusAction:
-            break;
+            msgs << "compilation terminated.\n";
+            return Broken;
         }
       }
+      return false;
     }
 
 
@@ -183,6 +182,7 @@ namespace {  // Anonymous namespace for class
     void visitShiftInst(ShiftInst &SI);
     void visitExtractElementInst(ExtractElementInst &EI);
     void visitInsertElementInst(InsertElementInst &EI);
+    void visitShuffleVectorInst(ShuffleVectorInst &EI);
     void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
     void visitCallInst(CallInst &CI);
     void visitGetElementPtrInst(GetElementPtrInst &GEP);
@@ -239,7 +239,7 @@ namespace {  // Anonymous namespace for class
     }
   };
 
-  RegisterOpt<Verifier> X("verify", "Module Verifier");
+  RegisterPass<Verifier> X("verify", "Module Verifier");
 } // End anonymous namespace
 
 
@@ -257,8 +257,16 @@ namespace {  // Anonymous namespace for class
 
 
 void Verifier::visitGlobalValue(GlobalValue &GV) {
-  Assert1(!GV.isExternal() || GV.hasExternalLinkage(),
-          "Global is external, but doesn't have external linkage!", &GV);
+  Assert1(!GV.isExternal() ||
+          GV.hasExternalLinkage() ||
+          GV.hasDLLImportLinkage() ||
+          GV.hasExternalWeakLinkage(),
+  "Global is external, but doesn't have external or dllimport or weak linkage!",
+          &GV);
+
+  Assert1(!GV.hasDLLImportLinkage() || GV.isExternal(),
+          "Global is marked as dllimport, but not external", &GV);
+  
   Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
           "Only global variables can have appending linkage!", &GV);
 
@@ -300,9 +308,6 @@ void Verifier::verifySymbolTable(SymbolTable &ST) {
 // visitFunction - Verify that a function is ok.
 //
 void Verifier::visitFunction(Function &F) {
-  Assert1(!F.isVarArg() || F.getCallingConv() == CallingConv::C,
-          "Varargs functions must have C calling conventions!", &F);
-
   // Check function arguments.
   const FunctionType *FT = F.getFunctionType();
   unsigned NumArgs = F.getArgumentList().size();
@@ -314,6 +319,24 @@ void Verifier::visitFunction(Function &F) {
           F.getReturnType() == Type::VoidTy,
           "Functions cannot return aggregate values!", &F);
 
+  // Check that this function meets the restrictions on this calling convention.
+  switch (F.getCallingConv()) {
+  default:
+    break;
+  case CallingConv::C:
+    break;
+  case CallingConv::CSRet:
+    Assert1(FT->getReturnType() == Type::VoidTy && 
+            FT->getNumParams() > 0 && isa<PointerType>(FT->getParamType(0)),
+            "Invalid struct-return function!", &F);
+    break;
+  case CallingConv::Fast:
+  case CallingConv::Cold:
+    Assert1(!F.isVarArg(),
+            "Varargs functions must have C calling conventions!", &F);
+    break;
+  }
+  
   // Check that the argument values match the function type for this function...
   unsigned i = 0;
   for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I, ++i) {
@@ -538,29 +561,43 @@ void Verifier::visitShiftInst(ShiftInst &SI) {
 }
 
 void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
-  Assert1(isa<PackedType>(EI.getOperand(0)->getType()),
-          "First operand to extractelement must be packed type!", &EI);
-  Assert1(EI.getOperand(1)->getType() == Type::UIntTy,
-          "Second operand to extractelement must be uint type!", &EI);
-  Assert1(EI.getType() == 
-          cast<PackedType>(EI.getOperand(0)->getType())->getElementType(),
-          "Extractelement return type must match "
-          "first operand element type!", &EI);
+  Assert1(ExtractElementInst::isValidOperands(EI.getOperand(0),
+                                              EI.getOperand(1)),
+          "Invalid extractelement operands!", &EI);
   visitInstruction(EI);
 }
 
 void Verifier::visitInsertElementInst(InsertElementInst &IE) {
-  Assert1(isa<PackedType>(IE.getOperand(0)->getType()),
-          "First operand to insertelement must be packed type!", &IE);
-  Assert1(IE.getOperand(1)->getType() == 
-          cast<PackedType>(IE.getOperand(0)->getType())->getElementType(),
-          "Second operand to insertelement must match "
-          "first operand element type!", &IE);
-  Assert1(IE.getOperand(2)->getType() == Type::UIntTy,
-          "Third operand to insertelement must be uint type!", &IE);
+  Assert1(InsertElementInst::isValidOperands(IE.getOperand(0),
+                                             IE.getOperand(1),
+                                             IE.getOperand(2)),
+          "Invalid insertelement operands!", &IE);
   visitInstruction(IE);
 }
 
+void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
+  Assert1(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
+                                             SV.getOperand(2)),
+          "Invalid shufflevector operands!", &SV);
+  Assert1(SV.getType() == SV.getOperand(0)->getType(),
+          "Result of shufflevector must match first operand type!", &SV);
+  
+  // Check to see if Mask is valid.
+  if (const ConstantPacked *MV = dyn_cast<ConstantPacked>(SV.getOperand(2))) {
+    for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
+      Assert1(isa<ConstantUInt>(MV->getOperand(i)) ||
+              isa<UndefValue>(MV->getOperand(i)),
+              "Invalid shufflevector shuffle mask!", &SV);
+    }
+  } else {
+    Assert1(isa<UndefValue>(SV.getOperand(2)) || 
+            isa<ConstantAggregateZero>(SV.getOperand(2)),
+            "Invalid shufflevector shuffle mask!", &SV);
+  }
+  
+  visitInstruction(SV);
+}
+
 void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
   const Type *ElTy =
     GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(),
@@ -624,10 +661,16 @@ void Verifier::visitInstruction(Instruction &I) {
   }
 
   for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
-    // Check to make sure that the "address of" an intrinsic function is never
-    // taken.
     Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I);
+
+    // Check to make sure that only first-class-values are operands to
+    // instructions.
+    Assert1(I.getOperand(i)->getType()->isFirstClassType(),
+            "Instruction operands must be first-class values!", &I);
+  
     if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
+      // Check to make sure that the "address of" an intrinsic function is never
+      // taken.
       Assert1(!F->isIntrinsic() || (i == 0 && isa<CallInst>(I)),
               "Cannot take the address of an intrinsic!", &I);
     } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
@@ -677,7 +720,6 @@ void Verifier::visitInstruction(Instruction &I) {
 ///
 void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
   Function *IF = CI.getCalledFunction();
-  const FunctionType *FTy = IF->getFunctionType();
   Assert1(IF->isExternal(), "Intrinsic functions should never be defined!", IF);
   
 #define GET_INTRINSIC_VERIFIER
@@ -767,11 +809,15 @@ bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) {
 /// verifyModule - Check a module for errors, printing messages on stderr.
 /// Return true if the module is corrupt.
 ///
-bool llvm::verifyModule(const Module &M, VerifierFailureAction action) {
+bool llvm::verifyModule(const Module &M, VerifierFailureAction action,
+                        std::string *ErrorInfo) {
   PassManager PM;
   Verifier *V = new Verifier(action);
   PM.add(V);
   PM.run((Module&)M);
+  
+  if (ErrorInfo && V->Broken)
+    *ErrorInfo = V->msgs.str();
   return V->Broken;
 }