Convert analyses to new pass structure
[oota-llvm.git] / lib / VMCore / Verifier.cpp
1 //===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==//
2 //
3 // This file defines the method verifier interface, that can be used for some
4 // sanity checking of input to the system.
5 //
6 // Note that this does not provide full 'java style' security and verifications,
7 // instead it just tries to ensure that code is well formed.
8 //
9 //  . There are no duplicated names in a symbol table... ie there !exist a val
10 //    with the same name as something in the symbol table, but with a different
11 //    address as what is in the symbol table...
12 //  . Both of a binary operator's parameters are the same type
13 //  . Verify that arithmetic and other things are only performed on first class
14 //    types.  No adding structures or arrays.
15 //  . All of the constants in a switch statement are of the correct type
16 //  . The code is in valid SSA form
17 //  . It should be illegal to put a label into any other type (like a structure)
18 //    or to return one. [except constant arrays!]
19 //  . Right now 'add bool 0, 0' is valid.  This isn't particularly good.
20 //  . Only phi nodes can be self referential: 'add int 0, 0 ; <int>:0' is bad
21 //  . PHI nodes must have an entry for each predecessor, with no extras.
22 //  . All other things that are tested by asserts spread about the code...
23 //  . All basic blocks should only end with terminator insts, not contain them
24 //  . The entry node to a method must not have predecessors!
25 //  . Verify that none of the Value getType()'s are null.
26 //  . Method's cannot take a void typed parameter
27 //  . Verify that a method's argument list agrees with it's declared type.
28 //  . Verify that arrays and structures have fixed elements: No unsized arrays.
29 //
30 //===----------------------------------------------------------------------===//
31
32 #include "llvm/Analysis/Verifier.h"
33 #include "llvm/Method.h"
34 #include "llvm/Module.h"
35 #include "llvm/BasicBlock.h"
36 #include "llvm/Type.h"
37 using std::string;
38 using std::vector;
39
40
41 // Error - Define a macro to do the common task of pushing a message onto the
42 // end of the error list and setting Bad to true.
43 //
44 #define Error(msg) do { ErrorMsgs.push_back(msg); Bad = true; } while (0)
45
46 #define t(x) (1 << (unsigned)Type::x)
47
48 #define SignedIntegralTypes (t(SByteTyID) | t(ShortTyID) |  \
49                              t(IntTyID)   | t(LongTyID))
50 static long UnsignedIntegralTypes = t(UByteTyID) | t(UShortTyID) | 
51                                           t(UIntTyID)  | t(ULongTyID);
52 static const long FloatingPointTypes    = t(FloatTyID) | t(DoubleTyID);
53
54 static const long IntegralTypes = SignedIntegralTypes | UnsignedIntegralTypes;
55
56 #if 0
57 static long ValidTypes[Type::FirstDerivedTyID] = {
58   [(unsigned)Instruction::UnaryOps::Not] t(BoolTyID),
59   //[Instruction::UnaryOps::Add] = IntegralTypes,
60   //  [Instruction::Sub] = IntegralTypes,
61 };
62 #endif
63
64 #undef t
65
66 static bool verify(const BasicBlock *BB, vector<string> &ErrorMsgs) {
67   bool Bad = false;
68   if (BB->getTerminator() == 0) Error("Basic Block does not have terminator!");
69
70   
71   return Bad;
72 }
73
74
75 bool verify(const Method *M, vector<string> &ErrorMsgs) {
76   bool Bad = false;
77   
78   for (Method::const_iterator BBIt = M->begin();
79        BBIt != M->end(); ++BBIt)
80     Bad |= verify(*BBIt, ErrorMsgs);
81
82   return Bad;
83 }
84
85 bool verify(const Module *C, vector<string> &ErrorMsgs) {
86   bool Bad = false;
87   assert(Type::FirstDerivedTyID-1 < sizeof(long)*8 && 
88          "Resize ValidTypes table to handle more than 32 primitive types!");
89
90   for (Module::const_iterator MI = C->begin(); MI != C->end(); ++MI)
91     Bad |= verify(*MI, ErrorMsgs);
92   
93   return Bad;
94 }