#include "llvm/Support/CallSite.h"
#include "llvm/Support/CFG.h"
#include "llvm/Support/InstVisitor.h"
-#include "llvm/ADT/SmallSet.h"
+#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/STLExtras.h"
-#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
using namespace llvm;
namespace { // Anonymous namespace for class
- struct VISIBILITY_HIDDEN PreVerifier : public FunctionPass {
+ struct PreVerifier : public FunctionPass {
static char ID; // Pass ID, replacement for typeid
PreVerifier() : FunctionPass(&ID) { }
static const PassInfo *const PreVerifyID = &PreVer;
namespace {
+ class TypeSet : public AbstractTypeUser {
+ public:
+ TypeSet() {}
+
+ /// Insert a type into the set of types.
+ bool insert(const Type *Ty) {
+ if (!Types.insert(Ty))
+ return false;
+ if (Ty->isAbstract())
+ Ty->addAbstractTypeUser(this);
+ return true;
+ }
+
+ // Remove ourselves as abstract type listeners for any types that remain
+ // abstract when the TypeSet is destroyed.
+ ~TypeSet() {
+ for (SmallSetVector<const Type *, 16>::iterator I = Types.begin(),
+ E = Types.end(); I != E; ++I) {
+ const Type *Ty = *I;
+ if (Ty->isAbstract())
+ Ty->removeAbstractTypeUser(this);
+ }
+ }
+
+ // Abstract type user interface.
+
+ /// Remove types from the set when refined. Do not insert the type it was
+ /// refined to because that type hasn't been verified yet.
+ void refineAbstractType(const DerivedType *OldTy, const Type *NewTy) {
+ Types.remove(OldTy);
+ OldTy->removeAbstractTypeUser(this);
+ }
+
+ /// Stop listening for changes to a type which is no longer abstract.
+ void typeBecameConcrete(const DerivedType *AbsTy) {
+ AbsTy->removeAbstractTypeUser(this);
+ }
+
+ void dump() const {}
+
+ private:
+ SmallSetVector<const Type *, 16> Types;
+
+ // Disallow copying.
+ TypeSet(const TypeSet &);
+ TypeSet &operator=(const TypeSet &);
+ };
+
struct Verifier : public FunctionPass, public InstVisitor<Verifier> {
static char ID; // Pass ID, replacement for typeid
bool Broken; // Is this module found to be broken?
/// an instruction in the same block.
SmallPtrSet<Instruction*, 16> InstsInThisBlock;
- /// CheckedTypes - keep track of the types that have been checked already.
- SmallSet<PATypeHolder, 16> CheckedTypes;
+ /// Types - keep track of the types that have been checked already.
+ TypeSet Types;
Verifier()
: FunctionPass(&ID),
void visitUserOp1(Instruction &I);
void visitUserOp2(Instruction &I) { visitUserOp1(I); }
void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI);
- void visitAllocationInst(AllocationInst &AI);
+ void visitAllocaInst(AllocaInst &AI);
void visitExtractValueInst(ExtractValueInst &EVI);
void visitInsertValueInst(InsertValueInst &IVI);
void WriteValue(const Value *V) {
if (!V) return;
if (isa<Instruction>(V)) {
- MessagesStr << *V;
+ MessagesStr << *V << '\n';
} else {
WriteAsOperand(MessagesStr, V, true, Mod);
- MessagesStr << "\n";
+ MessagesStr << '\n';
}
}
Broken = true;
}
- void CheckFailed(const Twine &Message, const Value* V1,
- const Type* T2, const Value* V3 = 0) {
+ void CheckFailed(const Twine &Message, const Value *V1,
+ const Type *T2, const Value *V3 = 0) {
MessagesStr << Message.str() << "\n";
WriteValue(V1);
WriteType(T2);
Broken = true;
}
- void CheckFailed(const Twine &Message, const Type* T1,
- const Type* T2 = 0, const Type* T3 = 0) {
+ void CheckFailed(const Twine &Message, const Type *T1,
+ const Type *T2 = 0, const Type *T3 = 0) {
MessagesStr << Message.str() << "\n";
WriteType(T1);
WriteType(T2);
Assert1(!GV.isConstant(), "'common' global may not be marked constant!",
&GV);
}
-
- // Verify that any metadata used in a global initializer points only to
- // other globals.
- if (MDNode *FirstNode = dyn_cast<MDNode>(GV.getInitializer())) {
- SmallVector<const MDNode *, 4> NodesToAnalyze;
- NodesToAnalyze.push_back(FirstNode);
- while (!NodesToAnalyze.empty()) {
- const MDNode *N = NodesToAnalyze.back();
- NodesToAnalyze.pop_back();
-
- for (MDNode::const_elem_iterator I = N->elem_begin(),
- E = N->elem_end(); I != E; ++I)
- if (const Value *V = *I) {
- if (const MDNode *Next = dyn_cast<MDNode>(V))
- NodesToAnalyze.push_back(Next);
- else
- Assert3(isa<Constant>(V),
- "reference to instruction from global metadata node",
- &GV, N, V);
- }
- }
- }
} else {
Assert1(GV.hasExternalLinkage() || GV.hasDLLImportLinkage() ||
GV.hasExternalWeakLinkage(),
"# formal arguments must match # of arguments for function type!",
&F, FT);
Assert1(F.getReturnType()->isFirstClassType() ||
- F.getReturnType() == Type::getVoidTy(F.getContext()) ||
+ F.getReturnType()->isVoidTy() ||
isa<StructType>(F.getReturnType()),
"Functions cannot return aggregate values!", &F);
- Assert1(!F.hasStructRetAttr() ||
- F.getReturnType() == Type::getVoidTy(F.getContext()),
+ Assert1(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
"Invalid struct return type!", &F);
const AttrListPtr &Attrs = F.getAttributes();
bool isLLVMdotName = F.getName().size() >= 5 &&
F.getName().substr(0, 5) == "llvm.";
- if (!isLLVMdotName)
- Assert1(F.getReturnType() != Type::getMetadataTy(F.getContext()),
- "Function may not return metadata unless it's an intrinsic", &F);
// Check that the argument values match the function type for this function...
unsigned i = 0;
Assert1(I->getType()->isFirstClassType(),
"Function arguments must have first-class types!", I);
if (!isLLVMdotName)
- Assert2(I->getType() != Type::getMetadataTy(F.getContext()),
+ Assert2(!I->getType()->isMetadataTy(),
"Function takes metadata but isn't an intrinsic", I, &F);
}
BasicBlock *Entry = &F.getEntryBlock();
Assert1(pred_begin(Entry) == pred_end(Entry),
"Entry block to function must not have predecessors!", Entry);
+
+ // The address of the entry block cannot be taken, unless it is dead.
+ if (Entry->hasAddressTaken()) {
+ Assert1(!BlockAddress::get(Entry)->isConstantUsed(),
+ "blockaddress may not be used with the entry block!", Entry);
+ }
+ }
+
+ // If this function is actually an intrinsic, verify that it is only used in
+ // direct call/invokes, never having its "address taken".
+ if (F.getIntrinsicID()) {
+ for (Value::use_iterator UI = F.use_begin(), E = F.use_end(); UI != E;++UI){
+ User *U = cast<User>(UI);
+ if ((isa<CallInst>(U) || isa<InvokeInst>(U)) && UI.getOperandNo() == 0)
+ continue; // Direct calls/invokes are ok.
+
+ Assert1(0, "Invalid user of intrinsic instruction!", U);
+ }
}
}
void Verifier::visitReturnInst(ReturnInst &RI) {
Function *F = RI.getParent()->getParent();
unsigned N = RI.getNumOperands();
- if (F->getReturnType() == Type::getVoidTy(RI.getContext()))
+ if (F->getReturnType()->isVoidTy())
Assert2(N == 0,
"Found return instr that returns non-void in Function of void "
"return type!", &RI, F->getReturnType());
// Check to make sure that all of the constants in the switch instruction
// have the same type as the switched-on value.
const Type *SwitchTy = SI.getCondition()->getType();
- for (unsigned i = 1, e = SI.getNumCases(); i != e; ++i)
+ SmallPtrSet<ConstantInt*, 32> Constants;
+ for (unsigned i = 1, e = SI.getNumCases(); i != e; ++i) {
Assert1(SI.getCaseValue(i)->getType() == SwitchTy,
"Switch constants must all be same type as switch value!", &SI);
+ Assert2(Constants.insert(SI.getCaseValue(i)),
+ "Duplicate integer as switch case", &SI, SI.getCaseValue(i));
+ }
visitTerminatorInst(SI);
}
for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
"PHI node operands are not the same type as the result!", &PN);
- Assert1(PN.getOperand(PHINode::getOperandNumForIncomingBlock(i)),
+ Assert1(isa<BasicBlock>(PN.getOperand(
+ PHINode::getOperandNumForIncomingBlock(i))),
"PHI node incoming block is not a BasicBlock!", &PN);
}
void Verifier::VerifyCallSite(CallSite CS) {
Instruction *I = CS.getInstruction();
- const PointerType *FPTy =
- dyn_cast<PointerType>(CS.getCalledValue()->getType());
- if (!FPTy) {
- CheckFailed("Called function must be a pointer!", I);
- visitInstruction(*I);
- return;
- }
+ Assert1(isa<PointerType>(CS.getCalledValue()->getType()),
+ "Called function must be a pointer!", I);
+ const PointerType *FPTy = cast<PointerType>(CS.getCalledValue()->getType());
- const FunctionType *FTy = dyn_cast<FunctionType>(FPTy->getElementType());
- if (!FTy) {
- CheckFailed("Called function is not pointer to function type!", I);
- visitInstruction(*I);
- return;
- }
+ Assert1(isa<FunctionType>(FPTy->getElementType()),
+ "Called function is not pointer to function type!", I);
+ const FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
// Verify that the correct number of arguments are being passed
if (FTy->isVarArg())
// Verify that there's no metadata unless it's a direct call to an intrinsic.
if (!CS.getCalledFunction() || CS.getCalledFunction()->getName().size() < 5 ||
CS.getCalledFunction()->getName().substr(0, 5) != "llvm.") {
- Assert1(FTy->getReturnType() != Type::getMetadataTy(I->getContext()),
- "Only intrinsics may return metadata", I);
for (FunctionType::param_iterator PI = FTy->param_begin(),
PE = FTy->param_end(); PI != PE; ++PI)
- Assert1(PI->get() != Type::getMetadataTy(I->getContext()),
+ Assert1(!PI->get()->isMetadataTy(),
"Function has metadata parameter but isn't an intrinsic", I);
}
void Verifier::visitLoadInst(LoadInst &LI) {
const PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
Assert1(PTy, "Load operand must be a pointer.", &LI);
- if (PTy) {
- const Type *ElTy = PTy->getElementType();
- Assert2(ElTy == LI.getType(),
- "Load result type does not match pointer operand type!", &LI, ElTy);
- Assert1(ElTy != Type::getMetadataTy(LI.getContext()),
- "Can't load metadata!", &LI);
- }
+ const Type *ElTy = PTy->getElementType();
+ Assert2(ElTy == LI.getType(),
+ "Load result type does not match pointer operand type!", &LI, ElTy);
visitInstruction(LI);
}
void Verifier::visitStoreInst(StoreInst &SI) {
const PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
Assert1(PTy, "Load operand must be a pointer.", &SI);
- if (PTy) {
- const Type *ElTy = PTy->getElementType();
- Assert2(ElTy == SI.getOperand(0)->getType(),
- "Stored value type does not match pointer operand type!",
- &SI, ElTy);
- Assert1(ElTy != Type::getMetadataTy(SI.getContext()),
- "Can't store metadata!", &SI);
- }
+ const Type *ElTy = PTy->getElementType();
+ Assert2(ElTy == SI.getOperand(0)->getType(),
+ "Stored value type does not match pointer operand type!",
+ &SI, ElTy);
visitInstruction(SI);
}
-void Verifier::visitAllocationInst(AllocationInst &AI) {
+void Verifier::visitAllocaInst(AllocaInst &AI) {
const PointerType *PTy = AI.getType();
Assert1(PTy->getAddressSpace() == 0,
"Allocation instruction pointer not in the generic address space!",
Assert1(BB->getTerminator() == &I, "Terminator not at end of block!", &I);
// Check that void typed values don't have names
- Assert1(I.getType() != Type::getVoidTy(I.getContext()) || !I.hasName(),
+ Assert1(!I.getType()->isVoidTy() || !I.hasName(),
"Instruction has a name, but provides a void value!", &I);
// Check that the return value of the instruction is either void or a legal
// value type.
- Assert1(I.getType() == Type::getVoidTy(I.getContext()) ||
- I.getType()->isFirstClassType()
- || ((isa<CallInst>(I) || isa<InvokeInst>(I))
- && isa<StructType>(I.getType())),
+ Assert1(I.getType()->isVoidTy() ||
+ I.getType()->isFirstClassType(),
"Instruction returns a non-scalar type!", &I);
- // Check that the instruction doesn't produce metadata or metadata*. Calls
- // all already checked against the callee type.
- Assert1(I.getType() != Type::getMetadataTy(I.getContext()) ||
+ // Check that the instruction doesn't produce metadata. Calls are already
+ // checked against the callee type.
+ Assert1(!I.getType()->isMetadataTy() ||
isa<CallInst>(I) || isa<InvokeInst>(I),
"Invalid use of metadata!", &I);
- if (const PointerType *PTy = dyn_cast<PointerType>(I.getType()))
- Assert1(PTy->getElementType() != Type::getMetadataTy(I.getContext()),
- "Instructions may not produce pointer to metadata.", &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!
if (Instruction *Used = dyn_cast<Instruction>(*UI))
Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
" embedded in a basic block!", &I, Used);
- else
+ else {
CheckFailed("Use of instruction is not an instruction!", *UI);
+ return;
+ }
}
for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Assert1(0, "Instruction operands must be first-class values!", &I);
}
- if (const PointerType *PTy =
- dyn_cast<PointerType>(I.getOperand(i)->getType()))
- Assert1(PTy->getElementType() != Type::getMetadataTy(I.getContext()),
- "Invalid use of metadata pointer.", &I);
-
if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
// Check to make sure that the "address of" an intrinsic function is never
// taken.
BasicBlock *UseBlock = BB;
if (isa<PHINode>(I))
UseBlock = dyn_cast<BasicBlock>(I.getOperand(i+1));
- // Avoid crash. The verifier will find this module broken anyways.
- if (!UseBlock) UseBlock = BB;
+ Assert2(UseBlock, "Invoke operand is PHI node with bad incoming-BB",
+ Op, &I);
if (isa<PHINode>(I) && UseBlock == OpBlock) {
// Special case of a phi node in the normal destination or the unwind
/// VerifyType - Verify that a type is well formed.
///
void Verifier::VerifyType(const Type *Ty) {
- // We insert complex types into CheckedTypes even if they failed verification
- // to prevent emitting messages about them multiple times if
+ if (!Types.insert(Ty)) return;
+
+ Assert1(&Mod->getContext() == &Ty->getContext(),
+ "Type context does not match Module context!", Ty);
switch (Ty->getTypeID()) {
case Type::FunctionTyID: {
- if (!CheckedTypes.insert(Ty)) return;
const FunctionType *FTy = cast<FunctionType>(Ty);
const Type *RetTy = FTy->getReturnType();
"Function type with invalid return type", RetTy, FTy);
VerifyType(RetTy);
- for (int i = 0, e = FTy->getNumParams(); i != e; ++i) {
+ for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) {
const Type *ElTy = FTy->getParamType(i);
Assert2(FunctionType::isValidArgumentType(ElTy),
"Function type with invalid parameter type", ElTy, FTy);
}
} break;
case Type::StructTyID: {
- if (!CheckedTypes.insert(Ty)) return;
const StructType *STy = cast<StructType>(Ty);
- for (int i = 0, e = STy->getNumElements(); i != e; ++i) {
+ for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
const Type *ElTy = STy->getElementType(i);
Assert2(StructType::isValidElementType(ElTy),
"Structure type with invalid element type", ElTy, STy);
}
} break;
case Type::ArrayTyID: {
- if (!CheckedTypes.insert(Ty)) return;
const ArrayType *ATy = cast<ArrayType>(Ty);
Assert1(ArrayType::isValidElementType(ATy->getElementType()),
"Array type with invalid element type", ATy);
VerifyType(ATy->getElementType());
} break;
case Type::PointerTyID: {
- if (!CheckedTypes.insert(Ty)) return;
const PointerType *PTy = cast<PointerType>(Ty);
Assert1(PointerType::isValidElementType(PTy->getElementType()),
"Pointer type with invalid element type", PTy);
VerifyType(PTy->getElementType());
- }
+ } break;
case Type::VectorTyID: {
- if (!CheckedTypes.insert(Ty)) return;
const VectorType *VTy = cast<VectorType>(Ty);
Assert1(VectorType::isValidElementType(VTy->getElementType()),
"Vector type with invalid element type", VTy);
VerifyType(VTy->getElementType());
- }
+ } break;
default:
- return;
+ break;
}
}
"llvm.stackprotector parameter #2 must resolve to an alloca.",
&CI);
break;
+ case Intrinsic::lifetime_start:
+ case Intrinsic::lifetime_end:
+ case Intrinsic::invariant_start:
+ Assert1(isa<ConstantInt>(CI.getOperand(1)),
+ "size argument of memory use markers must be a constant integer",
+ &CI);
+ break;
+ case Intrinsic::invariant_end:
+ Assert1(isa<ConstantInt>(CI.getOperand(2)),
+ "llvm.invariant.end parameter #2 must be a constant integer", &CI);
+ break;
}
}