blob: c773b80642868af827993f4b1d1ad0dd73557022 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==//
2//
Chris Lattner3e6e3e62002-03-29 19:06:18 +00003// This file defines the function verifier interface, that can be used for some
Chris Lattner2f7c9632001-06-06 20:29:01 +00004// 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 Lattner0e851da2002-04-18 20:37:37 +000012// * Both of a binary operator's parameters are the same type
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000013// * Verify that the indices of mem access instructions match other operands
Chris Lattner68289f02001-11-06 22:53:11 +000014// . Verify that arithmetic and other things are only performed on first class
15// types. No adding structures or arrays.
Chris Lattner2f7c9632001-06-06 20:29:01 +000016// . 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 Lattner7704e9f2002-03-14 16:53:48 +000020// * Only phi nodes can be self referential: 'add int %0, %0 ; <int>:0' is bad
Chris Lattnerd02f08d2002-02-20 17:55:43 +000021// * PHI nodes must have an entry for each predecessor, with no extras.
Chris Lattner069a7952002-06-25 15:56:27 +000022// * PHI nodes must be the first thing in a basic block, all grouped together
23// * All basic blocks should only end with terminator insts, not contain them
Chris Lattner3e6e3e62002-03-29 19:06:18 +000024// * The entry node to a function must not have predecessors
Chris Lattnerd02f08d2002-02-20 17:55:43 +000025// * All Instructions must be embeded into a basic block
Chris Lattner68289f02001-11-06 22:53:11 +000026// . Verify that none of the Value getType()'s are null.
Chris Lattner3e6e3e62002-03-29 19:06:18 +000027// . Function's cannot take a void typed parameter
Chris Lattneraf95e582002-04-13 22:48:46 +000028// * Verify that a function's argument list agrees with it's declared type.
Chris Lattner2f7c9632001-06-06 20:29:01 +000029// . Verify that arrays and structures have fixed elements: No unsized arrays.
Chris Lattnerfbf5be52002-03-15 20:25:09 +000030// * It is illegal to specify a name for a void value.
Chris Lattner3e6e3e62002-03-29 19:06:18 +000031// * It is illegal to have a internal function that is just a declaration
Chris Lattner486302a2002-04-12 18:20:49 +000032// * It is illegal to have a ret instruction that returns a value that does not
33// agree with the function return value type.
Chris Lattner338a4622002-05-08 19:49:50 +000034// * Function call argument types match the function prototype
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000035// * All other things that are tested by asserts spread about the code...
Chris Lattner2f7c9632001-06-06 20:29:01 +000036//
37//===----------------------------------------------------------------------===//
38
39#include "llvm/Analysis/Verifier.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000040#include "llvm/Pass.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000041#include "llvm/Module.h"
Chris Lattneraf95e582002-04-13 22:48:46 +000042#include "llvm/DerivedTypes.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000043#include "llvm/iPHINode.h"
Chris Lattner486302a2002-04-12 18:20:49 +000044#include "llvm/iTerminators.h"
Chris Lattner21ea83b2002-04-18 22:11:52 +000045#include "llvm/iOther.h"
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000046#include "llvm/iMemory.h"
Chris Lattnerfbf5be52002-03-15 20:25:09 +000047#include "llvm/SymbolTable.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000048#include "llvm/Support/CFG.h"
Chris Lattner0e851da2002-04-18 20:37:37 +000049#include "llvm/Support/InstVisitor.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000050#include "Support/STLExtras.h"
51#include <algorithm>
Anand Shukla8c377892002-06-25 22:07:38 +000052#include <iostream>
Chris Lattner7f74a562002-01-20 22:54:45 +000053
Chris Lattner0e851da2002-04-18 20:37:37 +000054namespace { // Anonymous namespace for class
Chris Lattner2f7c9632001-06-06 20:29:01 +000055
Chris Lattnerc8e66542002-04-27 06:56:12 +000056 struct Verifier : public FunctionPass, InstVisitor<Verifier> {
Chris Lattner0e851da2002-04-18 20:37:37 +000057 bool Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +000058
Chris Lattner0e851da2002-04-18 20:37:37 +000059 Verifier() : Broken(false) {}
Chris Lattner2f7c9632001-06-06 20:29:01 +000060
Chris Lattner37104aa2002-04-29 14:57:45 +000061 virtual const char *getPassName() const { return "Module Verifier"; }
62
Chris Lattner069a7952002-06-25 15:56:27 +000063 bool doInitialization(Module &M) {
64 verifySymbolTable(M.getSymbolTable());
Chris Lattner0e851da2002-04-18 20:37:37 +000065 return false;
66 }
67
Chris Lattner069a7952002-06-25 15:56:27 +000068 bool runOnFunction(Function &F) {
Chris Lattner0e851da2002-04-18 20:37:37 +000069 visit(F);
70 return false;
71 }
72
Chris Lattner069a7952002-06-25 15:56:27 +000073 bool doFinalization(Module &M) {
Chris Lattner9713b842002-04-28 16:04:26 +000074 // Scan through, checking all of the external function's linkage now...
Chris Lattner069a7952002-06-25 15:56:27 +000075 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
76 if (I->isExternal() && I->hasInternalLinkage())
77 CheckFailed("Function Declaration has Internal Linkage!", I);
Chris Lattner9713b842002-04-28 16:04:26 +000078
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000079 if (Broken) {
Anand Shukla8c377892002-06-25 22:07:38 +000080 std::cerr << "Broken module found, compilation aborted!\n";
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000081 abort();
82 }
83 return false;
84 }
85
Chris Lattnerf12cc842002-04-28 21:27:06 +000086 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
87 AU.setPreservesAll();
88 }
89
Chris Lattner0e851da2002-04-18 20:37:37 +000090 // Verification methods...
91 void verifySymbolTable(SymbolTable *ST);
Chris Lattner069a7952002-06-25 15:56:27 +000092 void visitFunction(Function &F);
93 void visitBasicBlock(BasicBlock &BB);
94 void visitPHINode(PHINode &PN);
95 void visitBinaryOperator(BinaryOperator &B);
96 void visitCallInst(CallInst &CI);
97 void visitGetElementPtrInst(GetElementPtrInst &GEP);
98 void visitLoadInst(LoadInst &LI);
99 void visitStoreInst(StoreInst &SI);
100 void visitInstruction(Instruction &I);
101 void visitTerminatorInst(TerminatorInst &I);
102 void visitReturnInst(ReturnInst &RI);
Chris Lattner0e851da2002-04-18 20:37:37 +0000103
104 // CheckFailed - A check failed, so print out the condition and the message
105 // that failed. This provides a nice place to put a breakpoint if you want
106 // to see why something is not correct.
107 //
Chris Lattner412d2772002-04-28 16:06:24 +0000108 inline void CheckFailed(const std::string &Message,
Chris Lattner069a7952002-06-25 15:56:27 +0000109 const Value *V1 = 0, const Value *V2 = 0,
110 const Value *V3 = 0, const Value *V4 = 0) {
Chris Lattner0e851da2002-04-18 20:37:37 +0000111 std::cerr << Message << "\n";
Chris Lattner069a7952002-06-25 15:56:27 +0000112 if (V1) std::cerr << *V1 << "\n";
113 if (V2) std::cerr << *V2 << "\n";
114 if (V3) std::cerr << *V3 << "\n";
115 if (V4) std::cerr << *V4 << "\n";
Chris Lattner0e851da2002-04-18 20:37:37 +0000116 Broken = true;
117 }
118 };
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000119}
Chris Lattner2f7c9632001-06-06 20:29:01 +0000120
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000121// Assert - We know that cond should be true, if not print an error message.
122#define Assert(C, M) \
Chris Lattner412d2772002-04-28 16:06:24 +0000123 do { if (!(C)) { CheckFailed(M); return; } } while (0)
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000124#define Assert1(C, M, V1) \
Chris Lattner412d2772002-04-28 16:06:24 +0000125 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000126#define Assert2(C, M, V1, V2) \
Chris Lattner412d2772002-04-28 16:06:24 +0000127 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
Chris Lattner069a7952002-06-25 15:56:27 +0000128#define Assert3(C, M, V1, V2, V3) \
129 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
130#define Assert4(C, M, V1, V2, V3, V4) \
131 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000132
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000133
Chris Lattneraf95e582002-04-13 22:48:46 +0000134// verifySymbolTable - Verify that a function or module symbol table is ok
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000135//
Chris Lattner0e851da2002-04-18 20:37:37 +0000136void Verifier::verifySymbolTable(SymbolTable *ST) {
137 if (ST == 0) return; // No symbol table to process
Chris Lattner2f7c9632001-06-06 20:29:01 +0000138
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000139 // Loop over all of the types in the symbol table...
Chris Lattner0e851da2002-04-18 20:37:37 +0000140 for (SymbolTable::iterator TI = ST->begin(), TE = ST->end(); TI != TE; ++TI)
141 for (SymbolTable::type_iterator I = TI->second.begin(),
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000142 E = TI->second.end(); I != E; ++I) {
143 Value *V = I->second;
144
145 // Check that there are no void typed values in the symbol table. Values
146 // with a void type cannot be put into symbol tables because they cannot
147 // have names!
148 Assert1(V->getType() != Type::VoidTy,
Chris Lattner412d2772002-04-28 16:06:24 +0000149 "Values with void type are not allowed to have names!", V);
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000150 }
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000151}
152
Chris Lattner0e851da2002-04-18 20:37:37 +0000153
154// visitFunction - Verify that a function is ok.
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000155//
Chris Lattner069a7952002-06-25 15:56:27 +0000156void Verifier::visitFunction(Function &F) {
157 if (F.isExternal()) return;
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000158
Chris Lattner069a7952002-06-25 15:56:27 +0000159 verifySymbolTable(F.getSymbolTable());
Chris Lattner3e6e3e62002-03-29 19:06:18 +0000160
Chris Lattneraf95e582002-04-13 22:48:46 +0000161 // Check function arguments...
Chris Lattner069a7952002-06-25 15:56:27 +0000162 const FunctionType *FT = F.getFunctionType();
163 unsigned NumArgs = F.getArgumentList().size();
Chris Lattneraf95e582002-04-13 22:48:46 +0000164
Chris Lattner069a7952002-06-25 15:56:27 +0000165 Assert2(!FT->isVarArg(), "Cannot define varargs functions in LLVM!", &F, FT);
166 Assert2(FT->getParamTypes().size() == NumArgs,
Chris Lattneraf95e582002-04-13 22:48:46 +0000167 "# formal arguments must match # of arguments for function type!",
Chris Lattner069a7952002-06-25 15:56:27 +0000168 &F, FT);
Chris Lattneraf95e582002-04-13 22:48:46 +0000169
170 // Check that the argument values match the function type for this function...
Chris Lattner069a7952002-06-25 15:56:27 +0000171 if (FT->getParamTypes().size() == NumArgs) {
172 unsigned i = 0;
173 for (Function::aiterator I = F.abegin(), E = F.aend(); I != E; ++I, ++i)
174 Assert2(I->getType() == FT->getParamType(i),
Chris Lattneraf95e582002-04-13 22:48:46 +0000175 "Argument value does not match function argument type!",
Chris Lattner069a7952002-06-25 15:56:27 +0000176 I, FT->getParamType(i));
Chris Lattneraf95e582002-04-13 22:48:46 +0000177 }
178
179 // Check the entry node
Chris Lattner069a7952002-06-25 15:56:27 +0000180 BasicBlock *Entry = &F.getEntryNode();
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000181 Assert1(pred_begin(Entry) == pred_end(Entry),
Chris Lattneraf95e582002-04-13 22:48:46 +0000182 "Entry block to function must not have predecessors!", Entry);
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000183}
184
185
Chris Lattner0e851da2002-04-18 20:37:37 +0000186// verifyBasicBlock - Verify that a basic block is well formed...
187//
Chris Lattner069a7952002-06-25 15:56:27 +0000188void Verifier::visitBasicBlock(BasicBlock &BB) {
189 // Ensure that basic blocks have terminators!
190 Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
191}
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000192
Chris Lattner069a7952002-06-25 15:56:27 +0000193void Verifier::visitTerminatorInst(TerminatorInst &I) {
194 // Ensure that terminators only exist at the end of the basic block.
195 Assert1(&I == I.getParent()->getTerminator(),
196 "Terminator found in the middle of a basic block!", I.getParent());
197}
198
199void Verifier::visitReturnInst(ReturnInst &RI) {
200 Function *F = RI.getParent()->getParent();
201 if (RI.getNumOperands() == 0)
202 Assert1(F->getReturnType() == Type::VoidTy,
203 "Function returns no value, but ret instruction found that does!",
204 &RI);
205 else
206 Assert2(F->getReturnType() == RI.getOperand(0)->getType(),
207 "Function return type does not match operand "
208 "type of return inst!", &RI, F->getReturnType());
209
210 // Check to make sure that the return value has neccesary properties for
211 // terminators...
212 visitTerminatorInst(RI);
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000213}
214
Chris Lattner0e851da2002-04-18 20:37:37 +0000215
216// visitPHINode - Ensure that a PHI node is well formed.
Chris Lattner069a7952002-06-25 15:56:27 +0000217void Verifier::visitPHINode(PHINode &PN) {
218 // Ensure that the PHI nodes are all grouped together at the top of the block.
219 // This can be tested by checking whether the instruction before this is
220 // either nonexistant (because this is begin()) or is a PHI node. If not,
221 // then there is some other instruction before a PHI.
222 Assert2(PN.getPrev() == 0 || isa<PHINode>(PN.getPrev()),
223 "PHI nodes not grouped at top of basic block!",
224 &PN, PN.getParent());
225
226 std::vector<BasicBlock*> Preds(pred_begin(PN.getParent()),
227 pred_end(PN.getParent()));
Chris Lattner0e851da2002-04-18 20:37:37 +0000228 // Loop over all of the incoming values, make sure that there are
229 // predecessors for each one...
230 //
Chris Lattner069a7952002-06-25 15:56:27 +0000231 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Chris Lattner0e851da2002-04-18 20:37:37 +0000232 // Make sure all of the incoming values are the right types...
Chris Lattner069a7952002-06-25 15:56:27 +0000233 Assert2(PN.getType() == PN.getIncomingValue(i)->getType(),
Chris Lattner0e851da2002-04-18 20:37:37 +0000234 "PHI node argument type does not agree with PHI node type!",
Chris Lattner069a7952002-06-25 15:56:27 +0000235 &PN, PN.getIncomingValue(i));
Chris Lattner0e851da2002-04-18 20:37:37 +0000236
Chris Lattner069a7952002-06-25 15:56:27 +0000237 BasicBlock *BB = PN.getIncomingBlock(i);
Chris Lattner0e851da2002-04-18 20:37:37 +0000238 std::vector<BasicBlock*>::iterator PI =
239 find(Preds.begin(), Preds.end(), BB);
240 Assert2(PI != Preds.end(), "PHI node has entry for basic block that"
Chris Lattner069a7952002-06-25 15:56:27 +0000241 " is not a predecessor!", &PN, BB);
Chris Lattner21ea83b2002-04-18 22:11:52 +0000242 Preds.erase(PI);
Chris Lattner0e851da2002-04-18 20:37:37 +0000243 }
244
245 // There should be no entries left in the predecessor list...
246 for (std::vector<BasicBlock*>::iterator I = Preds.begin(),
247 E = Preds.end(); I != E; ++I)
248 Assert2(0, "PHI node does not have entry for a predecessor basic block!",
Chris Lattner069a7952002-06-25 15:56:27 +0000249 &PN, *I);
250
251 // Now we go through and check to make sure that if there is more than one
252 // entry for a particular basic block in this PHI node, that the incoming
253 // values are all identical.
254 //
255 std::vector<std::pair<BasicBlock*, Value*> > Values;
256 Values.reserve(PN.getNumIncomingValues());
257 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
258 Values.push_back(std::make_pair(PN.getIncomingBlock(i),
259 PN.getIncomingValue(i)));
260
261 // Sort the Values vector so that identical basic block entries are adjacent.
262 std::sort(Values.begin(), Values.end());
263
264 // Check for identical basic blocks with differing incoming values...
265 for (unsigned i = 1, e = PN.getNumIncomingValues(); i < e; ++i)
266 Assert4(Values[i].first != Values[i-1].first ||
267 Values[i].second == Values[i-1].second,
268 "PHI node has multiple entries for the same basic block with "
269 "different incoming values!", &PN, Values[i].first,
270 Values[i].second, Values[i-1].second);
Chris Lattner0e851da2002-04-18 20:37:37 +0000271
272 visitInstruction(PN);
273}
274
Chris Lattner069a7952002-06-25 15:56:27 +0000275void Verifier::visitCallInst(CallInst &CI) {
276 Assert1(isa<PointerType>(CI.getOperand(0)->getType()),
277 "Called function must be a pointer!", &CI);
278 const PointerType *FPTy = cast<PointerType>(CI.getOperand(0)->getType());
Chris Lattner21ea83b2002-04-18 22:11:52 +0000279 Assert1(isa<FunctionType>(FPTy->getElementType()),
Chris Lattner069a7952002-06-25 15:56:27 +0000280 "Called function is not pointer to function type!", &CI);
Chris Lattner338a4622002-05-08 19:49:50 +0000281
Chris Lattner069a7952002-06-25 15:56:27 +0000282 const FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
Chris Lattner338a4622002-05-08 19:49:50 +0000283
284 // Verify that the correct number of arguments are being passed
285 if (FTy->isVarArg())
Chris Lattner069a7952002-06-25 15:56:27 +0000286 Assert1(CI.getNumOperands()-1 >= FTy->getNumParams(),
287 "Called function requires more parameters than were provided!",&CI);
Chris Lattner338a4622002-05-08 19:49:50 +0000288 else
Chris Lattner069a7952002-06-25 15:56:27 +0000289 Assert1(CI.getNumOperands()-1 == FTy->getNumParams(),
290 "Incorrect number of arguments passed to called function!", &CI);
Chris Lattner338a4622002-05-08 19:49:50 +0000291
292 // Verify that all arguments to the call match the function type...
293 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Chris Lattner069a7952002-06-25 15:56:27 +0000294 Assert2(CI.getOperand(i+1)->getType() == FTy->getParamType(i),
Chris Lattner338a4622002-05-08 19:49:50 +0000295 "Call parameter type does not match function signature!",
Chris Lattner069a7952002-06-25 15:56:27 +0000296 CI.getOperand(i+1), FTy->getParamType(i));
Chris Lattner21ea83b2002-04-18 22:11:52 +0000297}
Chris Lattner0e851da2002-04-18 20:37:37 +0000298
299// visitBinaryOperator - Check that both arguments to the binary operator are
300// of the same type!
301//
Chris Lattner069a7952002-06-25 15:56:27 +0000302void Verifier::visitBinaryOperator(BinaryOperator &B) {
303 Assert2(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
Chris Lattner0e851da2002-04-18 20:37:37 +0000304 "Both operands to a binary operator are not of the same type!",
Chris Lattner069a7952002-06-25 15:56:27 +0000305 B.getOperand(0), B.getOperand(1));
Chris Lattner0e851da2002-04-18 20:37:37 +0000306
307 visitInstruction(B);
308}
309
Chris Lattner069a7952002-06-25 15:56:27 +0000310void 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 Lattnerd46bb6e2002-04-24 19:12:21 +0000316 visitInstruction(GEP);
317}
318
Chris Lattner069a7952002-06-25 15:56:27 +0000319void 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 Lattnerd46bb6e2002-04-24 19:12:21 +0000325 visitInstruction(LI);
326}
327
Chris Lattner069a7952002-06-25 15:56:27 +0000328void 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 Lattnerd46bb6e2002-04-24 19:12:21 +0000334 visitInstruction(SI);
335}
336
Chris Lattner0e851da2002-04-18 20:37:37 +0000337
338// verifyInstruction - Verify that a non-terminator instruction is well formed.
339//
Chris Lattner069a7952002-06-25 15:56:27 +0000340void Verifier::visitInstruction(Instruction &I) {
341 Assert1(I.getParent(), "Instruction not embedded in basic block!", &I);
Chris Lattner0e851da2002-04-18 20:37:37 +0000342
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 Lattner069a7952002-06-25 15:56:27 +0000347 for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattner0e851da2002-04-18 20:37:37 +0000348 UI != UE; ++UI) {
349 Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!",
350 *UI);
Chris Lattner21ea83b2002-04-18 22:11:52 +0000351 Instruction *Used = cast<Instruction>(*UI);
352 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
Chris Lattner069a7952002-06-25 15:56:27 +0000353 " embeded in a basic block!", &I, Used);
Chris Lattner0e851da2002-04-18 20:37:37 +0000354 }
355
356 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
Chris Lattner069a7952002-06-25 15:56:27 +0000357 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattner0e851da2002-04-18 20:37:37 +0000358 UI != UE; ++UI)
Chris Lattner069a7952002-06-25 15:56:27 +0000359 Assert1(*UI != (User*)&I,
360 "Only PHI nodes may reference their own value!", &I);
Chris Lattner0e851da2002-04-18 20:37:37 +0000361 }
362
Chris Lattner069a7952002-06-25 15:56:27 +0000363 Assert1(I.getType() != Type::VoidTy || !I.hasName(),
364 "Instruction has a name, but provides a void value!", &I);
Chris Lattner0e851da2002-04-18 20:37:37 +0000365}
366
367
368//===----------------------------------------------------------------------===//
369// Implement the public interfaces to this file...
370//===----------------------------------------------------------------------===//
371
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000372Pass *createVerifierPass() {
Chris Lattner0e851da2002-04-18 20:37:37 +0000373 return new Verifier();
374}
375
Chris Lattner069a7952002-06-25 15:56:27 +0000376bool verifyFunction(const Function &F) {
Chris Lattner0e851da2002-04-18 20:37:37 +0000377 Verifier V;
Chris Lattner069a7952002-06-25 15:56:27 +0000378 V.visit((Function&)F);
Chris Lattner0e851da2002-04-18 20:37:37 +0000379 return V.Broken;
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000380}
381
382// verifyModule - Check a module for errors, printing messages on stderr.
383// Return true if the module is corrupt.
384//
Chris Lattner069a7952002-06-25 15:56:27 +0000385bool verifyModule(const Module &M) {
Chris Lattner0e851da2002-04-18 20:37:37 +0000386 Verifier V;
Chris Lattner069a7952002-06-25 15:56:27 +0000387 V.run((Module&)M);
Chris Lattner0e851da2002-04-18 20:37:37 +0000388 return V.Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +0000389}