+// visitPHINode - Ensure that a PHI node is well formed.
+void Verifier::visitPHINode(PHINode &PN) {
+ // Ensure that the PHI nodes are all grouped together at the top of the block.
+ // This can be tested by checking whether the instruction before this is
+ // either nonexistant (because this is begin()) or is a PHI node. If not,
+ // then there is some other instruction before a PHI.
+ Assert2(PN.getPrev() == 0 || isa<PHINode>(PN.getPrev()),
+ "PHI nodes not grouped at top of basic block!",
+ &PN, PN.getParent());
+
+ std::vector<BasicBlock*> Preds(pred_begin(PN.getParent()),
+ pred_end(PN.getParent()));
+ // Loop over all of the incoming values, make sure that there are
+ // predecessors for each one...
+ //
+ for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
+ // Make sure all of the incoming values are the right types...
+ Assert2(PN.getType() == PN.getIncomingValue(i)->getType(),
+ "PHI node argument type does not agree with PHI node type!",
+ &PN, PN.getIncomingValue(i));
+
+ BasicBlock *BB = PN.getIncomingBlock(i);
+ std::vector<BasicBlock*>::iterator PI =
+ find(Preds.begin(), Preds.end(), BB);
+ Assert2(PI != Preds.end(), "PHI node has entry for basic block that"
+ " is not a predecessor!", &PN, BB);
+ Preds.erase(PI);
+ }
+
+ // 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!