Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==// |
| 2 | // |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 3 | // This file defines the function verifier interface, that can be used for some |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 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 | // |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 9 | // * Both of a binary operator's parameters are the same type |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 10 | // * Verify that the indices of mem access instructions match other operands |
Chris Lattner | 68289f0 | 2001-11-06 22:53:11 +0000 | [diff] [blame] | 11 | // . Verify that arithmetic and other things are only performed on first class |
| 12 | // types. No adding structures or arrays. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 13 | // . All of the constants in a switch statement are of the correct type |
| 14 | // . The code is in valid SSA form |
| 15 | // . It should be illegal to put a label into any other type (like a structure) |
| 16 | // or to return one. [except constant arrays!] |
Chris Lattner | 7704e9f | 2002-03-14 16:53:48 +0000 | [diff] [blame] | 17 | // * Only phi nodes can be self referential: 'add int %0, %0 ; <int>:0' is bad |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 18 | // * PHI nodes must have an entry for each predecessor, with no extras. |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 19 | // * PHI nodes must be the first thing in a basic block, all grouped together |
| 20 | // * All basic blocks should only end with terminator insts, not contain them |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 21 | // * The entry node to a function must not have predecessors |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 22 | // * All Instructions must be embeded into a basic block |
Chris Lattner | 68289f0 | 2001-11-06 22:53:11 +0000 | [diff] [blame] | 23 | // . Verify that none of the Value getType()'s are null. |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 24 | // . Function's cannot take a void typed parameter |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 25 | // * Verify that a function's argument list agrees with it's declared type. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 26 | // . Verify that arrays and structures have fixed elements: No unsized arrays. |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 27 | // * It is illegal to specify a name for a void value. |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 28 | // * It is illegal to have a internal function that is just a declaration |
Chris Lattner | 486302a | 2002-04-12 18:20:49 +0000 | [diff] [blame] | 29 | // * It is illegal to have a ret instruction that returns a value that does not |
| 30 | // agree with the function return value type. |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 31 | // * Function call argument types match the function prototype |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 32 | // * All other things that are tested by asserts spread about the code... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 33 | // |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 37 | #include "llvm/Pass.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 38 | #include "llvm/Module.h" |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 39 | #include "llvm/DerivedTypes.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 40 | #include "llvm/iPHINode.h" |
Chris Lattner | 486302a | 2002-04-12 18:20:49 +0000 | [diff] [blame] | 41 | #include "llvm/iTerminators.h" |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 42 | #include "llvm/iOther.h" |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 43 | #include "llvm/iMemory.h" |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 44 | #include "llvm/SymbolTable.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 45 | #include "llvm/Support/CFG.h" |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 46 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 47 | #include "Support/STLExtras.h" |
| 48 | #include <algorithm> |
Anand Shukla | 8c37789 | 2002-06-25 22:07:38 +0000 | [diff] [blame] | 49 | #include <iostream> |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 51 | namespace { // Anonymous namespace for class |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 52 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 53 | struct Verifier : public FunctionPass, InstVisitor<Verifier> { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 54 | bool Broken; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 56 | Verifier() : Broken(false) {} |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 58 | virtual const char *getPassName() const { return "Module Verifier"; } |
| 59 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 60 | bool doInitialization(Module &M) { |
| 61 | verifySymbolTable(M.getSymbolTable()); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 62 | return false; |
| 63 | } |
| 64 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 65 | bool runOnFunction(Function &F) { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 66 | visit(F); |
| 67 | return false; |
| 68 | } |
| 69 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 70 | bool doFinalization(Module &M) { |
Chris Lattner | 9713b84 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 71 | // Scan through, checking all of the external function's linkage now... |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 72 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 73 | if (I->isExternal() && I->hasInternalLinkage()) |
| 74 | CheckFailed("Function Declaration has Internal Linkage!", I); |
Chris Lattner | 9713b84 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 75 | |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 76 | if (Broken) { |
Anand Shukla | 8c37789 | 2002-06-25 22:07:38 +0000 | [diff] [blame] | 77 | std::cerr << "Broken module found, compilation aborted!\n"; |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 78 | abort(); |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 83 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 84 | AU.setPreservesAll(); |
| 85 | } |
| 86 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 87 | // Verification methods... |
| 88 | void verifySymbolTable(SymbolTable *ST); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 89 | void visitFunction(Function &F); |
| 90 | void visitBasicBlock(BasicBlock &BB); |
| 91 | void visitPHINode(PHINode &PN); |
| 92 | void visitBinaryOperator(BinaryOperator &B); |
| 93 | void visitCallInst(CallInst &CI); |
| 94 | void visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 95 | void visitLoadInst(LoadInst &LI); |
| 96 | void visitStoreInst(StoreInst &SI); |
| 97 | void visitInstruction(Instruction &I); |
| 98 | void visitTerminatorInst(TerminatorInst &I); |
| 99 | void visitReturnInst(ReturnInst &RI); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 100 | |
| 101 | // CheckFailed - A check failed, so print out the condition and the message |
| 102 | // that failed. This provides a nice place to put a breakpoint if you want |
| 103 | // to see why something is not correct. |
| 104 | // |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 105 | inline void CheckFailed(const std::string &Message, |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 106 | const Value *V1 = 0, const Value *V2 = 0, |
| 107 | const Value *V3 = 0, const Value *V4 = 0) { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 108 | std::cerr << Message << "\n"; |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 109 | if (V1) std::cerr << *V1 << "\n"; |
| 110 | if (V2) std::cerr << *V2 << "\n"; |
| 111 | if (V3) std::cerr << *V3 << "\n"; |
| 112 | if (V4) std::cerr << *V4 << "\n"; |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 113 | Broken = true; |
| 114 | } |
| 115 | }; |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 116 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 117 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 118 | // Assert - We know that cond should be true, if not print an error message. |
| 119 | #define Assert(C, M) \ |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 120 | do { if (!(C)) { CheckFailed(M); return; } } while (0) |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 121 | #define Assert1(C, M, V1) \ |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 122 | do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 123 | #define Assert2(C, M, V1, V2) \ |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 124 | do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 125 | #define Assert3(C, M, V1, V2, V3) \ |
| 126 | do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0) |
| 127 | #define Assert4(C, M, V1, V2, V3, V4) \ |
| 128 | do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 129 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 130 | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 131 | // verifySymbolTable - Verify that a function or module symbol table is ok |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 132 | // |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 133 | void Verifier::verifySymbolTable(SymbolTable *ST) { |
| 134 | if (ST == 0) return; // No symbol table to process |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 135 | |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 136 | // Loop over all of the types in the symbol table... |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 137 | for (SymbolTable::iterator TI = ST->begin(), TE = ST->end(); TI != TE; ++TI) |
| 138 | for (SymbolTable::type_iterator I = TI->second.begin(), |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 139 | E = TI->second.end(); I != E; ++I) { |
| 140 | Value *V = I->second; |
| 141 | |
| 142 | // Check that there are no void typed values in the symbol table. Values |
| 143 | // with a void type cannot be put into symbol tables because they cannot |
| 144 | // have names! |
| 145 | Assert1(V->getType() != Type::VoidTy, |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 146 | "Values with void type are not allowed to have names!", V); |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 147 | } |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 150 | |
| 151 | // visitFunction - Verify that a function is ok. |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 152 | // |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 153 | void Verifier::visitFunction(Function &F) { |
| 154 | if (F.isExternal()) return; |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 156 | verifySymbolTable(F.getSymbolTable()); |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 157 | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 158 | // Check function arguments... |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 159 | const FunctionType *FT = F.getFunctionType(); |
| 160 | unsigned NumArgs = F.getArgumentList().size(); |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 162 | Assert2(!FT->isVarArg(), "Cannot define varargs functions in LLVM!", &F, FT); |
| 163 | Assert2(FT->getParamTypes().size() == NumArgs, |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 164 | "# formal arguments must match # of arguments for function type!", |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 165 | &F, FT); |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 166 | |
| 167 | // Check that the argument values match the function type for this function... |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 168 | if (FT->getParamTypes().size() == NumArgs) { |
| 169 | unsigned i = 0; |
| 170 | for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++i) |
| 171 | Assert2(I->getType() == FT->getParamType(i), |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 172 | "Argument value does not match function argument type!", |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 173 | I, FT->getParamType(i)); |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Check the entry node |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 177 | BasicBlock *Entry = &F.getEntryNode(); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 178 | Assert1(pred_begin(Entry) == pred_end(Entry), |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 179 | "Entry block to function must not have predecessors!", Entry); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 183 | // verifyBasicBlock - Verify that a basic block is well formed... |
| 184 | // |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 185 | void Verifier::visitBasicBlock(BasicBlock &BB) { |
| 186 | // Ensure that basic blocks have terminators! |
| 187 | Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB); |
| 188 | } |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 190 | void Verifier::visitTerminatorInst(TerminatorInst &I) { |
| 191 | // Ensure that terminators only exist at the end of the basic block. |
| 192 | Assert1(&I == I.getParent()->getTerminator(), |
| 193 | "Terminator found in the middle of a basic block!", I.getParent()); |
Chris Lattner | 7af3ee9 | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 194 | visitInstruction(I); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | void Verifier::visitReturnInst(ReturnInst &RI) { |
| 198 | Function *F = RI.getParent()->getParent(); |
| 199 | if (RI.getNumOperands() == 0) |
| 200 | Assert1(F->getReturnType() == Type::VoidTy, |
| 201 | "Function returns no value, but ret instruction found that does!", |
| 202 | &RI); |
| 203 | else |
| 204 | Assert2(F->getReturnType() == RI.getOperand(0)->getType(), |
| 205 | "Function return type does not match operand " |
| 206 | "type of return inst!", &RI, F->getReturnType()); |
| 207 | |
| 208 | // Check to make sure that the return value has neccesary properties for |
| 209 | // terminators... |
| 210 | visitTerminatorInst(RI); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 213 | |
| 214 | // visitPHINode - Ensure that a PHI node is well formed. |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 215 | void Verifier::visitPHINode(PHINode &PN) { |
| 216 | // Ensure that the PHI nodes are all grouped together at the top of the block. |
| 217 | // This can be tested by checking whether the instruction before this is |
| 218 | // either nonexistant (because this is begin()) or is a PHI node. If not, |
| 219 | // then there is some other instruction before a PHI. |
| 220 | Assert2(PN.getPrev() == 0 || isa<PHINode>(PN.getPrev()), |
| 221 | "PHI nodes not grouped at top of basic block!", |
| 222 | &PN, PN.getParent()); |
| 223 | |
| 224 | std::vector<BasicBlock*> Preds(pred_begin(PN.getParent()), |
| 225 | pred_end(PN.getParent())); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 226 | // Loop over all of the incoming values, make sure that there are |
| 227 | // predecessors for each one... |
| 228 | // |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 229 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 230 | // Make sure all of the incoming values are the right types... |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 231 | Assert2(PN.getType() == PN.getIncomingValue(i)->getType(), |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 232 | "PHI node argument type does not agree with PHI node type!", |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 233 | &PN, PN.getIncomingValue(i)); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 235 | BasicBlock *BB = PN.getIncomingBlock(i); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 236 | std::vector<BasicBlock*>::iterator PI = |
| 237 | find(Preds.begin(), Preds.end(), BB); |
| 238 | Assert2(PI != Preds.end(), "PHI node has entry for basic block that" |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 239 | " is not a predecessor!", &PN, BB); |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 240 | Preds.erase(PI); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // There should be no entries left in the predecessor list... |
| 244 | for (std::vector<BasicBlock*>::iterator I = Preds.begin(), |
| 245 | E = Preds.end(); I != E; ++I) |
| 246 | Assert2(0, "PHI node does not have entry for a predecessor basic block!", |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 247 | &PN, *I); |
| 248 | |
| 249 | // Now we go through and check to make sure that if there is more than one |
| 250 | // entry for a particular basic block in this PHI node, that the incoming |
| 251 | // values are all identical. |
| 252 | // |
| 253 | std::vector<std::pair<BasicBlock*, Value*> > Values; |
| 254 | Values.reserve(PN.getNumIncomingValues()); |
| 255 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 256 | Values.push_back(std::make_pair(PN.getIncomingBlock(i), |
| 257 | PN.getIncomingValue(i))); |
| 258 | |
| 259 | // Sort the Values vector so that identical basic block entries are adjacent. |
| 260 | std::sort(Values.begin(), Values.end()); |
| 261 | |
| 262 | // Check for identical basic blocks with differing incoming values... |
| 263 | for (unsigned i = 1, e = PN.getNumIncomingValues(); i < e; ++i) |
| 264 | Assert4(Values[i].first != Values[i-1].first || |
| 265 | Values[i].second == Values[i-1].second, |
| 266 | "PHI node has multiple entries for the same basic block with " |
| 267 | "different incoming values!", &PN, Values[i].first, |
| 268 | Values[i].second, Values[i-1].second); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 269 | |
| 270 | visitInstruction(PN); |
| 271 | } |
| 272 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 273 | void Verifier::visitCallInst(CallInst &CI) { |
| 274 | Assert1(isa<PointerType>(CI.getOperand(0)->getType()), |
| 275 | "Called function must be a pointer!", &CI); |
| 276 | const PointerType *FPTy = cast<PointerType>(CI.getOperand(0)->getType()); |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 277 | Assert1(isa<FunctionType>(FPTy->getElementType()), |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 278 | "Called function is not pointer to function type!", &CI); |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 279 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 280 | const FunctionType *FTy = cast<FunctionType>(FPTy->getElementType()); |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 281 | |
| 282 | // Verify that the correct number of arguments are being passed |
| 283 | if (FTy->isVarArg()) |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 284 | Assert1(CI.getNumOperands()-1 >= FTy->getNumParams(), |
| 285 | "Called function requires more parameters than were provided!",&CI); |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 286 | else |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 287 | Assert1(CI.getNumOperands()-1 == FTy->getNumParams(), |
| 288 | "Incorrect number of arguments passed to called function!", &CI); |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 289 | |
| 290 | // Verify that all arguments to the call match the function type... |
| 291 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 292 | Assert2(CI.getOperand(i+1)->getType() == FTy->getParamType(i), |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 293 | "Call parameter type does not match function signature!", |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 294 | CI.getOperand(i+1), FTy->getParamType(i)); |
Chris Lattner | 7af3ee9 | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 295 | |
| 296 | visitInstruction(CI); |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 297 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 298 | |
| 299 | // visitBinaryOperator - Check that both arguments to the binary operator are |
| 300 | // of the same type! |
| 301 | // |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 302 | void Verifier::visitBinaryOperator(BinaryOperator &B) { |
| 303 | Assert2(B.getOperand(0)->getType() == B.getOperand(1)->getType(), |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 304 | "Both operands to a binary operator are not of the same type!", |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 305 | B.getOperand(0), B.getOperand(1)); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 306 | |
| 307 | visitInstruction(B); |
| 308 | } |
| 309 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 310 | void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
| 311 | const Type *ElTy = MemAccessInst::getIndexedType(GEP.getOperand(0)->getType(), |
| 312 | GEP.copyIndices(), true); |
| 313 | Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP); |
| 314 | Assert2(PointerType::get(ElTy) == GEP.getType(), |
| 315 | "GEP is not of right type for indices!", &GEP, ElTy); |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 316 | visitInstruction(GEP); |
| 317 | } |
| 318 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 319 | void Verifier::visitLoadInst(LoadInst &LI) { |
| 320 | const Type *ElTy = LoadInst::getIndexedType(LI.getOperand(0)->getType(), |
| 321 | LI.copyIndices()); |
| 322 | Assert1(ElTy, "Invalid indices for load pointer type!", &LI); |
| 323 | Assert2(ElTy == LI.getType(), |
| 324 | "Load is not of right type for indices!", &LI, ElTy); |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 325 | visitInstruction(LI); |
| 326 | } |
| 327 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 328 | void Verifier::visitStoreInst(StoreInst &SI) { |
| 329 | const Type *ElTy = StoreInst::getIndexedType(SI.getOperand(1)->getType(), |
| 330 | SI.copyIndices()); |
| 331 | Assert1(ElTy, "Invalid indices for store pointer type!", &SI); |
| 332 | Assert2(ElTy == SI.getOperand(0)->getType(), |
| 333 | "Stored value is not of right type for indices!", &SI, ElTy); |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 334 | visitInstruction(SI); |
| 335 | } |
| 336 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 337 | |
Chris Lattner | 7af3ee9 | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 338 | // verifyInstruction - Verify that an instruction is well formed. |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 339 | // |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 340 | void Verifier::visitInstruction(Instruction &I) { |
| 341 | Assert1(I.getParent(), "Instruction not embedded in basic block!", &I); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 342 | |
| 343 | // Check that all uses of the instruction, if they are instructions |
| 344 | // themselves, actually have parent basic blocks. If the use is not an |
| 345 | // instruction, it is an error! |
| 346 | // |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 347 | for (User::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 348 | UI != UE; ++UI) { |
| 349 | Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!", |
| 350 | *UI); |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 351 | Instruction *Used = cast<Instruction>(*UI); |
| 352 | Assert2(Used->getParent() != 0, "Instruction referencing instruction not" |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 353 | " embeded in a basic block!", &I, Used); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 357 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 358 | UI != UE; ++UI) |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 359 | Assert1(*UI != (User*)&I, |
| 360 | "Only PHI nodes may reference their own value!", &I); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Chris Lattner | 7af3ee9 | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 363 | // Check that void typed values don't have names |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 364 | Assert1(I.getType() != Type::VoidTy || !I.hasName(), |
| 365 | "Instruction has a name, but provides a void value!", &I); |
Chris Lattner | 7af3ee9 | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 366 | |
| 367 | // Check that a definition dominates all of its uses. |
| 368 | // FIXME: This should use dominator set information, instead of this local |
| 369 | // hack that we have now. |
| 370 | // |
| 371 | for (User::use_iterator UI = I.use_begin(), UE = I.use_end(); |
| 372 | UI != UE; ++UI) { |
| 373 | Instruction *I2 = cast<Instruction>(*UI); |
| 374 | // Same basic block? |
| 375 | if (I.getParent() == I2->getParent() && !isa<PHINode>(I2)) { |
| 376 | // Make sure the instruction is not before the current instruction... |
| 377 | for (Instruction *Test = I.getPrev(); Test != 0; Test = Test->getPrev()) |
| 378 | Assert2(Test != I2, "Definition of value does not dominate a use!", |
| 379 | &I, I2); |
| 380 | } |
| 381 | } |
| 382 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | |
| 386 | //===----------------------------------------------------------------------===// |
| 387 | // Implement the public interfaces to this file... |
| 388 | //===----------------------------------------------------------------------===// |
| 389 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 390 | Pass *createVerifierPass() { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 391 | return new Verifier(); |
| 392 | } |
| 393 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 394 | bool verifyFunction(const Function &F) { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 395 | Verifier V; |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 396 | V.visit((Function&)F); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 397 | return V.Broken; |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // verifyModule - Check a module for errors, printing messages on stderr. |
| 401 | // Return true if the module is corrupt. |
| 402 | // |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 403 | bool verifyModule(const Module &M) { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 404 | Verifier V; |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 405 | V.run((Module&)M); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 406 | return V.Broken; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 407 | } |