+ // There should be no entries left in the predecessor list...
+ for (std::vector<BasicBlock*>::iterator I = Preds.begin(),
+ E = Preds.end(); I != E; ++I)
+ Assert2(0, "PHI node does not have entry for a predecessor basic block!",
+ &PN, *I);
+
+ // Now we go through and check to make sure that if there is more than one
+ // entry for a particular basic block in this PHI node, that the incoming
+ // values are all identical.
+ //
+ std::vector<std::pair<BasicBlock*, Value*> > Values;
+ Values.reserve(PN.getNumIncomingValues());
+ for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
+ Values.push_back(std::make_pair(PN.getIncomingBlock(i),
+ PN.getIncomingValue(i)));
+
+ // Sort the Values vector so that identical basic block entries are adjacent.
+ std::sort(Values.begin(), Values.end());
+
+ // Check for identical basic blocks with differing incoming values...
+ for (unsigned i = 1, e = PN.getNumIncomingValues(); i < e; ++i)
+ Assert4(Values[i].first != Values[i-1].first ||
+ Values[i].second == Values[i-1].second,
+ "PHI node has multiple entries for the same basic block with "
+ "different incoming values!", &PN, Values[i].first,
+ Values[i].second, Values[i-1].second);
+
+ visitInstruction(PN);
+}
+
+void Verifier::visitCallInst(CallInst &CI) {
+ Assert1(isa<PointerType>(CI.getOperand(0)->getType()),
+ "Called function must be a pointer!", &CI);
+ const PointerType *FPTy = cast<PointerType>(CI.getOperand(0)->getType());
+ Assert1(isa<FunctionType>(FPTy->getElementType()),
+ "Called function is not pointer to function type!", &CI);
+
+ const FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
+
+ // Verify that the correct number of arguments are being passed
+ if (FTy->isVarArg())
+ Assert1(CI.getNumOperands()-1 >= FTy->getNumParams(),
+ "Called function requires more parameters than were provided!",&CI);
+ else
+ Assert1(CI.getNumOperands()-1 == FTy->getNumParams(),
+ "Incorrect number of arguments passed to called function!", &CI);
+
+ // Verify that all arguments to the call match the function type...
+ for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
+ Assert2(CI.getOperand(i+1)->getType() == FTy->getParamType(i),
+ "Call parameter type does not match function signature!",
+ CI.getOperand(i+1), FTy->getParamType(i));
+
+ visitInstruction(CI);
+}
+
+// visitBinaryOperator - Check that both arguments to the binary operator are
+// of the same type!
+//
+void Verifier::visitBinaryOperator(BinaryOperator &B) {
+ Assert2(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
+ "Both operands to a binary operator are not of the same type!",
+ B.getOperand(0), B.getOperand(1));
+
+ visitInstruction(B);
+}
+
+void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
+ const Type *ElTy =
+ GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(),
+ std::vector<Value*>(GEP.idx_begin(), GEP.idx_end()), true);
+ Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
+ Assert2(PointerType::get(ElTy) == GEP.getType(),
+ "GEP is not of right type for indices!", &GEP, ElTy);
+ visitInstruction(GEP);
+}
+
+void Verifier::visitLoadInst(LoadInst &LI) {
+ const Type *ElTy =
+ cast<PointerType>(LI.getOperand(0)->getType())->getElementType();
+ Assert2(ElTy == LI.getType(),
+ "Load is not of right type for indices!", &LI, ElTy);
+ visitInstruction(LI);
+}
+
+void Verifier::visitStoreInst(StoreInst &SI) {
+ const Type *ElTy =
+ cast<PointerType>(SI.getOperand(1)->getType())->getElementType();
+ Assert2(ElTy == SI.getOperand(0)->getType(),
+ "Stored value is not of right type for indices!", &SI, ElTy);
+ visitInstruction(SI);
+}
+
+
+// verifyInstruction - Verify that an instruction is well formed.
+//
+void Verifier::visitInstruction(Instruction &I) {
+ Assert1(I.getParent(), "Instruction not embedded in basic block!", &I);
+
+ // Check that all uses of the instruction, if they are instructions
+ // themselves, actually have parent basic blocks. If the use is not an
+ // instruction, it is an error!
+ //
+ for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
+ UI != UE; ++UI) {
+ Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!",
+ *UI);
+ Instruction *Used = cast<Instruction>(*UI);
+ Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
+ " embeded in a basic block!", &I, Used);
+ }
+
+ if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
+ for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
+ UI != UE; ++UI)
+ Assert1(*UI != (User*)&I,
+ "Only PHI nodes may reference their own value!", &I);
+ }
+
+ // Check that void typed values don't have names
+ Assert1(I.getType() != Type::VoidTy || !I.hasName(),
+ "Instruction has a name, but provides a void value!", &I);
+
+ // Check that a definition dominates all of its uses.
+ //
+ for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
+ UI != UE; ++UI) {
+ Instruction *Use = cast<Instruction>(*UI);
+
+ // PHI nodes are more difficult than other nodes because they actually
+ // "use" the value in the predecessor basic blocks they correspond to.
+ if (PHINode *PN = dyn_cast<PHINode>(Use)) {
+ for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
+ if (&I == PN->getIncomingValue(i)) {
+ // Make sure that I dominates the end of pred(i)
+ BasicBlock *Pred = PN->getIncomingBlock(i);
+
+ Assert2(DS->dominates(I.getParent(), Pred),
+ "Instruction does not dominate all uses!",
+ &I, PN);
+ }
+
+ } else {
+ Assert2(DS->dominates(&I, Use),
+ "Instruction does not dominate all uses!", &I, Use);
+ }
+ }
+}
+
+
+//===----------------------------------------------------------------------===//
+// Implement the public interfaces to this file...
+//===----------------------------------------------------------------------===//
+
+Pass *createVerifierPass() {
+ return new Verifier();
+}
+
+
+// verifyFunction - Create
+bool verifyFunction(const Function &f) {
+ Function &F = (Function&)f;
+ assert(!F.isExternal() && "Cannot verify external functions");
+
+ DominatorSet DS;
+ DS.doInitialization(*F.getParent());
+ DS.runOnFunction(F);
+
+ Verifier V(DS);
+ V.runOnFunction(F);
+
+ DS.doFinalization(*F.getParent());
+
+ return V.Broken;
+}
+
+// verifyModule - Check a module for errors, printing messages on stderr.
+// Return true if the module is corrupt.
+//
+bool verifyModule(const Module &M) {
+ PassManager PM;
+ Verifier *V = new Verifier();
+ PM.add(V);
+ PM.run((Module&)M);
+ return V->Broken;