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 | // |
| 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... |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 12 | // * Both of a binary operator's parameters are the same type |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 13 | // * Verify that the indices of mem access instructions match other operands |
Chris Lattner | 68289f0 | 2001-11-06 22:53:11 +0000 | [diff] [blame] | 14 | // . Verify that arithmetic and other things are only performed on first class |
| 15 | // types. No adding structures or arrays. |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 16 | // . All of the constants in a switch statement are of the correct type |
| 17 | // . The code is in valid SSA form |
| 18 | // . It should be illegal to put a label into any other type (like a structure) |
| 19 | // or to return one. [except constant arrays!] |
Chris Lattner | 7704e9f | 2002-03-14 16:53:48 +0000 | [diff] [blame] | 20 | // * 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] | 21 | // * PHI nodes must have an entry for each predecessor, with no extras. |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 22 | // . All basic blocks should only end with terminator insts, not contain them |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 23 | // * The entry node to a function must not have predecessors |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 24 | // * All Instructions must be embeded into a basic block |
Chris Lattner | 68289f0 | 2001-11-06 22:53:11 +0000 | [diff] [blame] | 25 | // . Verify that none of the Value getType()'s are null. |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 26 | // . Function's cannot take a void typed parameter |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 27 | // * 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] | 28 | // . Verify that arrays and structures have fixed elements: No unsized arrays. |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 29 | // * It is illegal to specify a name for a void value. |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 30 | // * 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] | 31 | // * It is illegal to have a ret instruction that returns a value that does not |
| 32 | // agree with the function return value type. |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 33 | // * All other things that are tested by asserts spread about the code... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 34 | // |
| 35 | //===----------------------------------------------------------------------===// |
| 36 | |
| 37 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 38 | #include "llvm/Pass.h" |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | #include "llvm/Module.h" |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 40 | #include "llvm/DerivedTypes.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 41 | #include "llvm/iPHINode.h" |
Chris Lattner | 486302a | 2002-04-12 18:20:49 +0000 | [diff] [blame] | 42 | #include "llvm/iTerminators.h" |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 43 | #include "llvm/iOther.h" |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 44 | #include "llvm/iMemory.h" |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 45 | #include "llvm/Argument.h" |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 46 | #include "llvm/SymbolTable.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 47 | #include "llvm/Support/CFG.h" |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 48 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 49 | #include "Support/STLExtras.h" |
| 50 | #include <algorithm> |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 52 | namespace { // Anonymous namespace for class |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 53 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 54 | struct Verifier : public FunctionPass, InstVisitor<Verifier> { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 55 | bool Broken; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 57 | Verifier() : Broken(false) {} |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 59 | virtual const char *getPassName() const { return "Module Verifier"; } |
| 60 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 61 | bool doInitialization(Module *M) { |
| 62 | verifySymbolTable(M->getSymbolTable()); |
| 63 | return false; |
| 64 | } |
| 65 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 66 | bool runOnFunction(Function *F) { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 67 | visit(F); |
| 68 | return false; |
| 69 | } |
| 70 | |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 71 | bool doFinalization(Module *M) { |
Chris Lattner | 9713b84 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 72 | // Scan through, checking all of the external function's linkage now... |
| 73 | for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 74 | if ((*I)->isExternal() && (*I)->hasInternalLinkage()) |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 75 | CheckFailed("Function Declaration has Internal Linkage!", (*I)); |
Chris Lattner | 9713b84 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 76 | |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 77 | if (Broken) { |
| 78 | cerr << "Broken module found, compilation aborted!\n"; |
| 79 | abort(); |
| 80 | } |
| 81 | return false; |
| 82 | } |
| 83 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 84 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 85 | AU.setPreservesAll(); |
| 86 | } |
| 87 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 88 | // Verification methods... |
| 89 | void verifySymbolTable(SymbolTable *ST); |
| 90 | void visitFunction(Function *F); |
| 91 | void visitBasicBlock(BasicBlock *BB); |
| 92 | void visitPHINode(PHINode *PN); |
| 93 | void visitBinaryOperator(BinaryOperator *B); |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 94 | void visitCallInst(CallInst *CI); |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 95 | void visitGetElementPtrInst(GetElementPtrInst *GEP); |
| 96 | void visitLoadInst(LoadInst *LI); |
| 97 | void visitStoreInst(StoreInst *SI); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 98 | void visitInstruction(Instruction *I); |
| 99 | |
| 100 | // CheckFailed - A check failed, so print out the condition and the message |
| 101 | // that failed. This provides a nice place to put a breakpoint if you want |
| 102 | // to see why something is not correct. |
| 103 | // |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 104 | inline void CheckFailed(const std::string &Message, |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 105 | const Value *V1 = 0, const Value *V2 = 0) { |
| 106 | std::cerr << Message << "\n"; |
| 107 | if (V1) { std::cerr << V1 << "\n"; } |
| 108 | if (V2) { std::cerr << V2 << "\n"; } |
| 109 | Broken = true; |
| 110 | } |
| 111 | }; |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 112 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 113 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 114 | // Assert - We know that cond should be true, if not print an error message. |
| 115 | #define Assert(C, M) \ |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 116 | do { if (!(C)) { CheckFailed(M); return; } } while (0) |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 117 | #define Assert1(C, M, V1) \ |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 118 | do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 119 | #define Assert2(C, M, V1, V2) \ |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 120 | do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 121 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 122 | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 123 | // verifySymbolTable - Verify that a function or module symbol table is ok |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 124 | // |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 125 | void Verifier::verifySymbolTable(SymbolTable *ST) { |
| 126 | if (ST == 0) return; // No symbol table to process |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 127 | |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 128 | // Loop over all of the types in the symbol table... |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 129 | for (SymbolTable::iterator TI = ST->begin(), TE = ST->end(); TI != TE; ++TI) |
| 130 | for (SymbolTable::type_iterator I = TI->second.begin(), |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 131 | E = TI->second.end(); I != E; ++I) { |
| 132 | Value *V = I->second; |
| 133 | |
| 134 | // Check that there are no void typed values in the symbol table. Values |
| 135 | // with a void type cannot be put into symbol tables because they cannot |
| 136 | // have names! |
| 137 | Assert1(V->getType() != Type::VoidTy, |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 138 | "Values with void type are not allowed to have names!", V); |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 139 | } |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 142 | |
| 143 | // visitFunction - Verify that a function is ok. |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 144 | // |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 145 | void Verifier::visitFunction(Function *F) { |
| 146 | if (F->isExternal()) return; |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 9713b84 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 148 | verifySymbolTable(F->getSymbolTable()); |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 149 | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 150 | // Check function arguments... |
| 151 | const FunctionType *FT = F->getFunctionType(); |
| 152 | const Function::ArgumentListType &ArgList = F->getArgumentList(); |
| 153 | |
| 154 | Assert2(!FT->isVarArg(), "Cannot define varargs functions in LLVM!", F, FT); |
| 155 | Assert2(FT->getParamTypes().size() == ArgList.size(), |
| 156 | "# formal arguments must match # of arguments for function type!", |
| 157 | F, FT); |
| 158 | |
| 159 | // Check that the argument values match the function type for this function... |
| 160 | if (FT->getParamTypes().size() == ArgList.size()) { |
| 161 | for (unsigned i = 0, e = ArgList.size(); i != e; ++i) |
| 162 | Assert2(ArgList[i]->getType() == FT->getParamType(i), |
| 163 | "Argument value does not match function argument type!", |
| 164 | ArgList[i], FT->getParamType(i)); |
| 165 | } |
| 166 | |
| 167 | // Check the entry node |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 168 | BasicBlock *Entry = F->getEntryNode(); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 169 | Assert1(pred_begin(Entry) == pred_end(Entry), |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 170 | "Entry block to function must not have predecessors!", Entry); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 174 | // verifyBasicBlock - Verify that a basic block is well formed... |
| 175 | // |
| 176 | void Verifier::visitBasicBlock(BasicBlock *BB) { |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 177 | Assert1(BB->getTerminator(), "Basic Block does not have terminator!", BB); |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 178 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 179 | // Check that the terminator is ok as well... |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 180 | if (isa<ReturnInst>(BB->getTerminator())) { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 181 | Instruction *I = BB->getTerminator(); |
| 182 | Function *F = I->getParent()->getParent(); |
| 183 | if (I->getNumOperands() == 0) |
| 184 | Assert1(F->getReturnType() == Type::VoidTy, |
| 185 | "Function returns no value, but ret instruction found that does!", |
| 186 | I); |
| 187 | else |
| 188 | Assert2(F->getReturnType() == I->getOperand(0)->getType(), |
| 189 | "Function return type does not match operand " |
| 190 | "type of return inst!", I, F->getReturnType()); |
| 191 | } |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 194 | |
| 195 | // visitPHINode - Ensure that a PHI node is well formed. |
| 196 | void Verifier::visitPHINode(PHINode *PN) { |
| 197 | std::vector<BasicBlock*> Preds(pred_begin(PN->getParent()), |
| 198 | pred_end(PN->getParent())); |
| 199 | // Loop over all of the incoming values, make sure that there are |
| 200 | // predecessors for each one... |
| 201 | // |
| 202 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 203 | // Make sure all of the incoming values are the right types... |
| 204 | Assert2(PN->getType() == PN->getIncomingValue(i)->getType(), |
| 205 | "PHI node argument type does not agree with PHI node type!", |
| 206 | PN, PN->getIncomingValue(i)); |
| 207 | |
| 208 | BasicBlock *BB = PN->getIncomingBlock(i); |
| 209 | std::vector<BasicBlock*>::iterator PI = |
| 210 | find(Preds.begin(), Preds.end(), BB); |
| 211 | Assert2(PI != Preds.end(), "PHI node has entry for basic block that" |
| 212 | " is not a predecessor!", PN, BB); |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 213 | Preds.erase(PI); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | // There should be no entries left in the predecessor list... |
| 217 | for (std::vector<BasicBlock*>::iterator I = Preds.begin(), |
| 218 | E = Preds.end(); I != E; ++I) |
| 219 | Assert2(0, "PHI node does not have entry for a predecessor basic block!", |
| 220 | PN, *I); |
| 221 | |
| 222 | visitInstruction(PN); |
| 223 | } |
| 224 | |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 225 | void Verifier::visitCallInst(CallInst *CI) { |
| 226 | Assert1(isa<PointerType>(CI->getOperand(0)->getType()), |
| 227 | "Called function must be a pointer!", CI); |
| 228 | PointerType *FPTy = cast<PointerType>(CI->getOperand(0)->getType()); |
| 229 | Assert1(isa<FunctionType>(FPTy->getElementType()), |
| 230 | "Called function is not pointer to function type!", CI); |
| 231 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 232 | |
| 233 | // visitBinaryOperator - Check that both arguments to the binary operator are |
| 234 | // of the same type! |
| 235 | // |
| 236 | void Verifier::visitBinaryOperator(BinaryOperator *B) { |
| 237 | Assert2(B->getOperand(0)->getType() == B->getOperand(1)->getType(), |
| 238 | "Both operands to a binary operator are not of the same type!", |
| 239 | B->getOperand(0), B->getOperand(1)); |
| 240 | |
| 241 | visitInstruction(B); |
| 242 | } |
| 243 | |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 244 | void Verifier::visitGetElementPtrInst(GetElementPtrInst *GEP) { |
| 245 | const Type *ElTy =MemAccessInst::getIndexedType(GEP->getOperand(0)->getType(), |
| 246 | GEP->copyIndices(), true); |
| 247 | Assert1(ElTy, "Invalid indices for GEP pointer type!", GEP); |
| 248 | Assert2(PointerType::get(ElTy) == GEP->getType(), |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 249 | "GEP is not of right type for indices!", GEP, ElTy); |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 250 | visitInstruction(GEP); |
| 251 | } |
| 252 | |
| 253 | void Verifier::visitLoadInst(LoadInst *LI) { |
| 254 | const Type *ElTy = LoadInst::getIndexedType(LI->getOperand(0)->getType(), |
| 255 | LI->copyIndices()); |
| 256 | Assert1(ElTy, "Invalid indices for load pointer type!", LI); |
| 257 | Assert2(ElTy == LI->getType(), |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 258 | "Load is not of right type for indices!", LI, ElTy); |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 259 | visitInstruction(LI); |
| 260 | } |
| 261 | |
| 262 | void Verifier::visitStoreInst(StoreInst *SI) { |
| 263 | const Type *ElTy = StoreInst::getIndexedType(SI->getOperand(1)->getType(), |
| 264 | SI->copyIndices()); |
| 265 | Assert1(ElTy, "Invalid indices for store pointer type!", SI); |
| 266 | Assert2(ElTy == SI->getOperand(0)->getType(), |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 267 | "Stored value is not of right type for indices!", SI, ElTy); |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 268 | visitInstruction(SI); |
| 269 | } |
| 270 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 271 | |
| 272 | // verifyInstruction - Verify that a non-terminator instruction is well formed. |
| 273 | // |
| 274 | void Verifier::visitInstruction(Instruction *I) { |
| 275 | assert(I->getParent() && "Instruction not embedded in basic block!"); |
| 276 | |
| 277 | // Check that all uses of the instruction, if they are instructions |
| 278 | // themselves, actually have parent basic blocks. If the use is not an |
| 279 | // instruction, it is an error! |
| 280 | // |
| 281 | for (User::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 282 | UI != UE; ++UI) { |
| 283 | Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!", |
| 284 | *UI); |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 285 | Instruction *Used = cast<Instruction>(*UI); |
| 286 | Assert2(Used->getParent() != 0, "Instruction referencing instruction not" |
| 287 | " embeded in a basic block!", I, Used); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential |
| 291 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 292 | UI != UE; ++UI) |
| 293 | Assert1(*UI != (User*)I, |
| 294 | "Only PHI nodes may reference their own value!", I); |
| 295 | } |
| 296 | |
| 297 | Assert1(I->getType() != Type::VoidTy || !I->hasName(), |
| 298 | "Instruction has a name, but provides a void value!", I); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | //===----------------------------------------------------------------------===// |
| 303 | // Implement the public interfaces to this file... |
| 304 | //===----------------------------------------------------------------------===// |
| 305 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 306 | Pass *createVerifierPass() { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 307 | return new Verifier(); |
| 308 | } |
| 309 | |
| 310 | bool verifyFunction(const Function *F) { |
| 311 | Verifier V; |
| 312 | V.visit((Function*)F); |
| 313 | return V.Broken; |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | // verifyModule - Check a module for errors, printing messages on stderr. |
| 317 | // Return true if the module is corrupt. |
| 318 | // |
Chris Lattner | b67f732 | 2002-02-26 21:45:33 +0000 | [diff] [blame] | 319 | bool verifyModule(const Module *M) { |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 320 | Verifier V; |
| 321 | V.run((Module*)M); |
| 322 | return V.Broken; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 323 | } |