LLVM API Documentation
00001 //===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the function verifier interface, that can be used for some 00011 // sanity checking of input to the system. 00012 // 00013 // Note that this does not provide full `Java style' security and verifications, 00014 // instead it just tries to ensure that code is well-formed. 00015 // 00016 // * Both of a binary operator's parameters are of the same type 00017 // * Verify that the indices of mem access instructions match other operands 00018 // * Verify that arithmetic and other things are only performed on first-class 00019 // types. Verify that shifts & logicals only happen on integrals f.e. 00020 // * All of the constants in a switch statement are of the correct type 00021 // * The code is in valid SSA form 00022 // * It should be illegal to put a label into any other type (like a structure) 00023 // or to return one. [except constant arrays!] 00024 // * Only phi nodes can be self referential: 'add i32 %0, %0 ; <int>:0' is bad 00025 // * PHI nodes must have an entry for each predecessor, with no extras. 00026 // * PHI nodes must be the first thing in a basic block, all grouped together 00027 // * PHI nodes must have at least one entry 00028 // * All basic blocks should only end with terminator insts, not contain them 00029 // * The entry node to a function must not have predecessors 00030 // * All Instructions must be embedded into a basic block 00031 // * Functions cannot take a void-typed parameter 00032 // * Verify that a function's argument list agrees with it's declared type. 00033 // * It is illegal to specify a name for a void value. 00034 // * It is illegal to have a internal global value with no initializer 00035 // * It is illegal to have a ret instruction that returns a value that does not 00036 // agree with the function return value type. 00037 // * Function call argument types match the function prototype 00038 // * All other things that are tested by asserts spread about the code... 00039 // 00040 //===----------------------------------------------------------------------===// 00041 00042 #include "llvm/Analysis/Verifier.h" 00043 #include "llvm/CallingConv.h" 00044 #include "llvm/Constants.h" 00045 #include "llvm/DerivedTypes.h" 00046 #include "llvm/InlineAsm.h" 00047 #include "llvm/IntrinsicInst.h" 00048 #include "llvm/Module.h" 00049 #include "llvm/ModuleProvider.h" 00050 #include "llvm/Pass.h" 00051 #include "llvm/PassManager.h" 00052 #include "llvm/Analysis/Dominators.h" 00053 #include "llvm/Assembly/Writer.h" 00054 #include "llvm/CodeGen/ValueTypes.h" 00055 #include "llvm/Support/CallSite.h" 00056 #include "llvm/Support/CFG.h" 00057 #include "llvm/Support/InstVisitor.h" 00058 #include "llvm/Support/Streams.h" 00059 #include "llvm/ADT/SmallPtrSet.h" 00060 #include "llvm/ADT/SmallVector.h" 00061 #include "llvm/ADT/StringExtras.h" 00062 #include "llvm/ADT/STLExtras.h" 00063 #include "llvm/Support/Compiler.h" 00064 #include <algorithm> 00065 #include <sstream> 00066 #include <cstdarg> 00067 using namespace llvm; 00068 00069 namespace { // Anonymous namespace for class 00070 struct VISIBILITY_HIDDEN PreVerifier : public FunctionPass { 00071 static char ID; // Pass ID, replacement for typeid 00072 00073 PreVerifier() : FunctionPass(&ID) { } 00074 00075 // Check that the prerequisites for successful DominatorTree construction 00076 // are satisfied. 00077 bool runOnFunction(Function &F) { 00078 bool Broken = false; 00079 00080 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { 00081 if (I->empty() || !I->back().isTerminator()) { 00082 cerr << "Basic Block does not have terminator!\n"; 00083 WriteAsOperand(*cerr, I, true); 00084 cerr << "\n"; 00085 Broken = true; 00086 } 00087 } 00088 00089 if (Broken) 00090 abort(); 00091 00092 return false; 00093 } 00094 }; 00095 } 00096 00097 char PreVerifier::ID = 0; 00098 static RegisterPass<PreVerifier> 00099 PreVer("preverify", "Preliminary module verification"); 00100 static const PassInfo *const PreVerifyID = &PreVer; 00101 00102 namespace { 00103 struct VISIBILITY_HIDDEN 00104 Verifier : public FunctionPass, InstVisitor<Verifier> { 00105 static char ID; // Pass ID, replacement for typeid 00106 bool Broken; // Is this module found to be broken? 00107 bool RealPass; // Are we not being run by a PassManager? 00108 VerifierFailureAction action; 00109 // What to do if verification fails. 00110 Module *Mod; // Module we are verifying right now 00111 DominatorTree *DT; // Dominator Tree, caution can be null! 00112 std::stringstream msgs; // A stringstream to collect messages 00113 00114 /// InstInThisBlock - when verifying a basic block, keep track of all of the 00115 /// instructions we have seen so far. This allows us to do efficient 00116 /// dominance checks for the case when an instruction has an operand that is 00117 /// an instruction in the same block. 00118 SmallPtrSet<Instruction*, 16> InstsInThisBlock; 00119 00120 Verifier() 00121 : FunctionPass(&ID), 00122 Broken(false), RealPass(true), action(AbortProcessAction), 00123 DT(0), msgs( std::ios::app | std::ios::out ) {} 00124 explicit Verifier(VerifierFailureAction ctn) 00125 : FunctionPass(&ID), 00126 Broken(false), RealPass(true), action(ctn), DT(0), 00127 msgs( std::ios::app | std::ios::out ) {} 00128 explicit Verifier(bool AB) 00129 : FunctionPass(&ID), 00130 Broken(false), RealPass(true), 00131 action( AB ? AbortProcessAction : PrintMessageAction), DT(0), 00132 msgs( std::ios::app | std::ios::out ) {} 00133 explicit Verifier(DominatorTree &dt) 00134 : FunctionPass(&ID), 00135 Broken(false), RealPass(false), action(PrintMessageAction), 00136 DT(&dt), msgs( std::ios::app | std::ios::out ) {} 00137 00138 00139 bool doInitialization(Module &M) { 00140 Mod = &M; 00141 verifyTypeSymbolTable(M.getTypeSymbolTable()); 00142 00143 // If this is a real pass, in a pass manager, we must abort before 00144 // returning back to the pass manager, or else the pass manager may try to 00145 // run other passes on the broken module. 00146 if (RealPass) 00147 return abortIfBroken(); 00148 return false; 00149 } 00150 00151 bool runOnFunction(Function &F) { 00152 // Get dominator information if we are being run by PassManager 00153 if (RealPass) DT = &getAnalysis<DominatorTree>(); 00154 00155 Mod = F.getParent(); 00156 00157 visit(F); 00158 InstsInThisBlock.clear(); 00159 00160 // If this is a real pass, in a pass manager, we must abort before 00161 // returning back to the pass manager, or else the pass manager may try to 00162 // run other passes on the broken module. 00163 if (RealPass) 00164 return abortIfBroken(); 00165 00166 return false; 00167 } 00168 00169 bool doFinalization(Module &M) { 00170 // Scan through, checking all of the external function's linkage now... 00171 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { 00172 visitGlobalValue(*I); 00173 00174 // Check to make sure function prototypes are okay. 00175 if (I->isDeclaration()) visitFunction(*I); 00176 } 00177 00178 for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 00179 I != E; ++I) 00180 visitGlobalVariable(*I); 00181 00182 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); 00183 I != E; ++I) 00184 visitGlobalAlias(*I); 00185 00186 // If the module is broken, abort at this time. 00187 return abortIfBroken(); 00188 } 00189 00190 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00191 AU.setPreservesAll(); 00192 AU.addRequiredID(PreVerifyID); 00193 if (RealPass) 00194 AU.addRequired<DominatorTree>(); 00195 } 00196 00197 /// abortIfBroken - If the module is broken and we are supposed to abort on 00198 /// this condition, do so. 00199 /// 00200 bool abortIfBroken() { 00201 if (!Broken) return false; 00202 msgs << "Broken module found, "; 00203 switch (action) { 00204 default: assert(0 && "Unknown action"); 00205 case AbortProcessAction: 00206 msgs << "compilation aborted!\n"; 00207 cerr << msgs.str(); 00208 abort(); 00209 case PrintMessageAction: 00210 msgs << "verification continues.\n"; 00211 cerr << msgs.str(); 00212 return false; 00213 case ReturnStatusAction: 00214 msgs << "compilation terminated.\n"; 00215 return Broken; 00216 } 00217 } 00218 00219 00220 // Verification methods... 00221 void verifyTypeSymbolTable(TypeSymbolTable &ST); 00222 void visitGlobalValue(GlobalValue &GV); 00223 void visitGlobalVariable(GlobalVariable &GV); 00224 void visitGlobalAlias(GlobalAlias &GA); 00225 void visitFunction(Function &F); 00226 void visitBasicBlock(BasicBlock &BB); 00227 using InstVisitor<Verifier>::visit; 00228 00229 void visit(Instruction &I); 00230 00231 void visitTruncInst(TruncInst &I); 00232 void visitZExtInst(ZExtInst &I); 00233 void visitSExtInst(SExtInst &I); 00234 void visitFPTruncInst(FPTruncInst &I); 00235 void visitFPExtInst(FPExtInst &I); 00236 void visitFPToUIInst(FPToUIInst &I); 00237 void visitFPToSIInst(FPToSIInst &I); 00238 void visitUIToFPInst(UIToFPInst &I); 00239 void visitSIToFPInst(SIToFPInst &I); 00240 void visitIntToPtrInst(IntToPtrInst &I); 00241 void visitPtrToIntInst(PtrToIntInst &I); 00242 void visitBitCastInst(BitCastInst &I); 00243 void visitPHINode(PHINode &PN); 00244 void visitBinaryOperator(BinaryOperator &B); 00245 void visitICmpInst(ICmpInst &IC); 00246 void visitFCmpInst(FCmpInst &FC); 00247 void visitExtractElementInst(ExtractElementInst &EI); 00248 void visitInsertElementInst(InsertElementInst &EI); 00249 void visitShuffleVectorInst(ShuffleVectorInst &EI); 00250 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); } 00251 void visitCallInst(CallInst &CI); 00252 void visitInvokeInst(InvokeInst &II); 00253 void visitGetElementPtrInst(GetElementPtrInst &GEP); 00254 void visitLoadInst(LoadInst &LI); 00255 void visitStoreInst(StoreInst &SI); 00256 void visitInstruction(Instruction &I); 00257 void visitTerminatorInst(TerminatorInst &I); 00258 void visitReturnInst(ReturnInst &RI); 00259 void visitSwitchInst(SwitchInst &SI); 00260 void visitSelectInst(SelectInst &SI); 00261 void visitUserOp1(Instruction &I); 00262 void visitUserOp2(Instruction &I) { visitUserOp1(I); } 00263 void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI); 00264 void visitAllocationInst(AllocationInst &AI); 00265 void visitExtractValueInst(ExtractValueInst &EVI); 00266 void visitInsertValueInst(InsertValueInst &IVI); 00267 00268 void VerifyCallSite(CallSite CS); 00269 void VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F, 00270 unsigned Count, ...); 00271 void VerifyAttrs(Attributes Attrs, const Type *Ty, 00272 bool isReturnValue, const Value *V); 00273 void VerifyFunctionAttrs(const FunctionType *FT, const AttrListPtr &Attrs, 00274 const Value *V); 00275 00276 void WriteValue(const Value *V) { 00277 if (!V) return; 00278 if (isa<Instruction>(V)) { 00279 msgs << *V; 00280 } else { 00281 WriteAsOperand(msgs, V, true, Mod); 00282 msgs << "\n"; 00283 } 00284 } 00285 00286 void WriteType(const Type *T) { 00287 if ( !T ) return; 00288 WriteTypeSymbolic(msgs, T, Mod ); 00289 } 00290 00291 00292 // CheckFailed - A check failed, so print out the condition and the message 00293 // that failed. This provides a nice place to put a breakpoint if you want 00294 // to see why something is not correct. 00295 void CheckFailed(const std::string &Message, 00296 const Value *V1 = 0, const Value *V2 = 0, 00297 const Value *V3 = 0, const Value *V4 = 0) { 00298 msgs << Message << "\n"; 00299 WriteValue(V1); 00300 WriteValue(V2); 00301 WriteValue(V3); 00302 WriteValue(V4); 00303 Broken = true; 00304 } 00305 00306 void CheckFailed( const std::string& Message, const Value* V1, 00307 const Type* T2, const Value* V3 = 0 ) { 00308 msgs << Message << "\n"; 00309 WriteValue(V1); 00310 WriteType(T2); 00311 WriteValue(V3); 00312 Broken = true; 00313 } 00314 }; 00315 } // End anonymous namespace 00316 00317 char Verifier::ID = 0; 00318 static RegisterPass<Verifier> X("verify", "Module Verifier"); 00319 00320 // Assert - We know that cond should be true, if not print an error message. 00321 #define Assert(C, M) \ 00322 do { if (!(C)) { CheckFailed(M); return; } } while (0) 00323 #define Assert1(C, M, V1) \ 00324 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) 00325 #define Assert2(C, M, V1, V2) \ 00326 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) 00327 #define Assert3(C, M, V1, V2, V3) \ 00328 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0) 00329 #define Assert4(C, M, V1, V2, V3, V4) \ 00330 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0) 00331 00332 00333 void Verifier::visit(Instruction &I) { 00334 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) 00335 Assert1(I.getOperand(i) != 0, "Operand is null", &I); 00336 InstVisitor<Verifier>::visit(I); 00337 } 00338 00339 00340 void Verifier::visitGlobalValue(GlobalValue &GV) { 00341 Assert1(!GV.isDeclaration() || 00342 GV.hasExternalLinkage() || 00343 GV.hasDLLImportLinkage() || 00344 GV.hasExternalWeakLinkage() || 00345 GV.hasGhostLinkage() || 00346 (isa<GlobalAlias>(GV) && 00347 (GV.hasInternalLinkage() || GV.hasWeakLinkage())), 00348 "Global is external, but doesn't have external or dllimport or weak linkage!", 00349 &GV); 00350 00351 Assert1(!GV.hasDLLImportLinkage() || GV.isDeclaration(), 00352 "Global is marked as dllimport, but not external", &GV); 00353 00354 Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV), 00355 "Only global variables can have appending linkage!", &GV); 00356 00357 if (GV.hasAppendingLinkage()) { 00358 GlobalVariable &GVar = cast<GlobalVariable>(GV); 00359 Assert1(isa<ArrayType>(GVar.getType()->getElementType()), 00360 "Only global arrays can have appending linkage!", &GV); 00361 } 00362 } 00363 00364 void Verifier::visitGlobalVariable(GlobalVariable &GV) { 00365 if (GV.hasInitializer()) { 00366 Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(), 00367 "Global variable initializer type does not match global " 00368 "variable type!", &GV); 00369 } else { 00370 Assert1(GV.hasExternalLinkage() || GV.hasDLLImportLinkage() || 00371 GV.hasExternalWeakLinkage(), 00372 "invalid linkage type for global declaration", &GV); 00373 } 00374 00375 visitGlobalValue(GV); 00376 } 00377 00378 void Verifier::visitGlobalAlias(GlobalAlias &GA) { 00379 Assert1(!GA.getName().empty(), 00380 "Alias name cannot be empty!", &GA); 00381 Assert1(GA.hasExternalLinkage() || GA.hasInternalLinkage() || 00382 GA.hasWeakLinkage(), 00383 "Alias should have external or external weak linkage!", &GA); 00384 Assert1(GA.getAliasee(), 00385 "Aliasee cannot be NULL!", &GA); 00386 Assert1(GA.getType() == GA.getAliasee()->getType(), 00387 "Alias and aliasee types should match!", &GA); 00388 00389 if (!isa<GlobalValue>(GA.getAliasee())) { 00390 const ConstantExpr *CE = dyn_cast<ConstantExpr>(GA.getAliasee()); 00391 Assert1(CE && CE->getOpcode() == Instruction::BitCast && 00392 isa<GlobalValue>(CE->getOperand(0)), 00393 "Aliasee should be either GlobalValue or bitcast of GlobalValue", 00394 &GA); 00395 } 00396 00397 const GlobalValue* Aliasee = GA.resolveAliasedGlobal(/*stopOnWeak*/ false); 00398 Assert1(Aliasee, 00399 "Aliasing chain should end with function or global variable", &GA); 00400 00401 visitGlobalValue(GA); 00402 } 00403 00404 void Verifier::verifyTypeSymbolTable(TypeSymbolTable &ST) { 00405 } 00406 00407 // VerifyAttrs - Check the given parameter attributes for an argument or return 00408 // value of the specified type. The value V is printed in error messages. 00409 void Verifier::VerifyAttrs(Attributes Attrs, const Type *Ty, 00410 bool isReturnValue, const Value *V) { 00411 if (Attrs == Attribute::None) 00412 return; 00413 00414 if (isReturnValue) { 00415 Attributes RetI = Attrs & Attribute::ParameterOnly; 00416 Assert1(!RetI, "Attribute " + Attribute::getAsString(RetI) + 00417 " does not apply to return values!", V); 00418 } 00419 Attributes FnCheckAttr = Attrs & Attribute::FunctionOnly; 00420 Assert1(!FnCheckAttr, "Attribute " + Attribute::getAsString(FnCheckAttr) + 00421 " only applies to functions!", V); 00422 00423 for (unsigned i = 0; 00424 i < array_lengthof(Attribute::MutuallyIncompatible); ++i) { 00425 Attributes MutI = Attrs & Attribute::MutuallyIncompatible[i]; 00426 Assert1(!(MutI & (MutI - 1)), "Attributes " + 00427 Attribute::getAsString(MutI) + " are incompatible!", V); 00428 } 00429 00430 Attributes TypeI = Attrs & Attribute::typeIncompatible(Ty); 00431 Assert1(!TypeI, "Wrong type for attribute " + 00432 Attribute::getAsString(TypeI), V); 00433 00434 Attributes ByValI = Attrs & Attribute::ByVal; 00435 if (const PointerType *PTy = dyn_cast<PointerType>(Ty)) { 00436 Assert1(!ByValI || PTy->getElementType()->isSized(), 00437 "Attribute " + Attribute::getAsString(ByValI) + 00438 " does not support unsized types!", V); 00439 } else { 00440 Assert1(!ByValI, 00441 "Attribute " + Attribute::getAsString(ByValI) + 00442 " only applies to parameters with pointer type!", V); 00443 } 00444 } 00445 00446 // VerifyFunctionAttrs - Check parameter attributes against a function type. 00447 // The value V is printed in error messages. 00448 void Verifier::VerifyFunctionAttrs(const FunctionType *FT, 00449 const AttrListPtr &Attrs, 00450 const Value *V) { 00451 if (Attrs.isEmpty()) 00452 return; 00453 00454 bool SawNest = false; 00455 00456 for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) { 00457 const AttributeWithIndex &Attr = Attrs.getSlot(i); 00458 00459 const Type *Ty; 00460 if (Attr.Index == 0) 00461 Ty = FT->getReturnType(); 00462 else if (Attr.Index-1 < FT->getNumParams()) 00463 Ty = FT->getParamType(Attr.Index-1); 00464 else 00465 break; // VarArgs attributes, don't verify. 00466 00467 VerifyAttrs(Attr.Attrs, Ty, Attr.Index == 0, V); 00468 00469 if (Attr.Attrs & Attribute::Nest) { 00470 Assert1(!SawNest, "More than one parameter has attribute nest!", V); 00471 SawNest = true; 00472 } 00473 00474 if (Attr.Attrs & Attribute::StructRet) 00475 Assert1(Attr.Index == 1, "Attribute sret not on first parameter!", V); 00476 } 00477 00478 Attributes FAttrs = Attrs.getFnAttributes(); 00479 Assert1(!(FAttrs & (~Attribute::FunctionOnly)), 00480 "Attribute " + Attribute::getAsString(FAttrs) + 00481 " does not apply to function!", V); 00482 00483 for (unsigned i = 0; 00484 i < array_lengthof(Attribute::MutuallyIncompatible); ++i) { 00485 Attributes MutI = FAttrs & Attribute::MutuallyIncompatible[i]; 00486 Assert1(!(MutI & (MutI - 1)), "Attributes " + 00487 Attribute::getAsString(MutI) + " are incompatible!", V); 00488 } 00489 } 00490 00491 static bool VerifyAttributeCount(const AttrListPtr &Attrs, unsigned Params) { 00492 if (Attrs.isEmpty()) 00493 return true; 00494 00495 unsigned LastSlot = Attrs.getNumSlots() - 1; 00496 unsigned LastIndex = Attrs.getSlot(LastSlot).Index; 00497 if (LastIndex <= Params 00498 || (LastIndex == (unsigned)~0 00499 && (LastSlot == 0 || Attrs.getSlot(LastSlot - 1).Index <= Params))) 00500 return true; 00501 00502 return false; 00503 } 00504 // visitFunction - Verify that a function is ok. 00505 // 00506 void Verifier::visitFunction(Function &F) { 00507 // Check function arguments. 00508 const FunctionType *FT = F.getFunctionType(); 00509 unsigned NumArgs = F.arg_size(); 00510 00511 Assert2(FT->getNumParams() == NumArgs, 00512 "# formal arguments must match # of arguments for function type!", 00513 &F, FT); 00514 Assert1(F.getReturnType()->isFirstClassType() || 00515 F.getReturnType() == Type::VoidTy || 00516 isa<StructType>(F.getReturnType()), 00517 "Functions cannot return aggregate values!", &F); 00518 00519 Assert1(!F.hasStructRetAttr() || F.getReturnType() == Type::VoidTy, 00520 "Invalid struct return type!", &F); 00521 00522 const AttrListPtr &Attrs = F.getAttributes(); 00523 00524 Assert1(VerifyAttributeCount(Attrs, FT->getNumParams()), 00525 "Attributes after last parameter!", &F); 00526 00527 // Check function attributes. 00528 VerifyFunctionAttrs(FT, Attrs, &F); 00529 00530 // Check that this function meets the restrictions on this calling convention. 00531 switch (F.getCallingConv()) { 00532 default: 00533 break; 00534 case CallingConv::C: 00535 break; 00536 case CallingConv::Fast: 00537 case CallingConv::Cold: 00538 case CallingConv::X86_FastCall: 00539 Assert1(!F.isVarArg(), 00540 "Varargs functions must have C calling conventions!", &F); 00541 break; 00542 } 00543 00544 // Check that the argument values match the function type for this function... 00545 unsigned i = 0; 00546 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); 00547 I != E; ++I, ++i) { 00548 Assert2(I->getType() == FT->getParamType(i), 00549 "Argument value does not match function argument type!", 00550 I, FT->getParamType(i)); 00551 Assert1(I->getType()->isFirstClassType(), 00552 "Function arguments must have first-class types!", I); 00553 } 00554 00555 if (F.isDeclaration()) { 00556 Assert1(F.hasExternalLinkage() || F.hasDLLImportLinkage() || 00557 F.hasExternalWeakLinkage() || F.hasGhostLinkage(), 00558 "invalid linkage type for function declaration", &F); 00559 } else { 00560 // Verify that this function (which has a body) is not named "llvm.*". It 00561 // is not legal to define intrinsics. 00562 if (F.getName().size() >= 5) 00563 Assert1(F.getName().substr(0, 5) != "llvm.", 00564 "llvm intrinsics cannot be defined!", &F); 00565 00566 // Check the entry node 00567 BasicBlock *Entry = &F.getEntryBlock(); 00568 Assert1(pred_begin(Entry) == pred_end(Entry), 00569 "Entry block to function must not have predecessors!", Entry); 00570 } 00571 } 00572 00573 00574 // verifyBasicBlock - Verify that a basic block is well formed... 00575 // 00576 void Verifier::visitBasicBlock(BasicBlock &BB) { 00577 InstsInThisBlock.clear(); 00578 00579 // Ensure that basic blocks have terminators! 00580 Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB); 00581 00582 // Check constraints that this basic block imposes on all of the PHI nodes in 00583 // it. 00584 if (isa<PHINode>(BB.front())) { 00585 SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB)); 00586 SmallVector<std::pair<BasicBlock*, Value*>, 8> Values; 00587 std::sort(Preds.begin(), Preds.end()); 00588 PHINode *PN; 00589 for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) { 00590 00591 // Ensure that PHI nodes have at least one entry! 00592 Assert1(PN->getNumIncomingValues() != 0, 00593 "PHI nodes must have at least one entry. If the block is dead, " 00594 "the PHI should be removed!", PN); 00595 Assert1(PN->getNumIncomingValues() == Preds.size(), 00596 "PHINode should have one entry for each predecessor of its " 00597 "parent basic block!", PN); 00598 00599 // Get and sort all incoming values in the PHI node... 00600 Values.clear(); 00601 Values.reserve(PN->getNumIncomingValues()); 00602 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) 00603 Values.push_back(std::make_pair(PN->getIncomingBlock(i), 00604 PN->getIncomingValue(i))); 00605 std::sort(Values.begin(), Values.end()); 00606 00607 for (unsigned i = 0, e = Values.size(); i != e; ++i) { 00608 // Check to make sure that if there is more than one entry for a 00609 // particular basic block in this PHI node, that the incoming values are 00610 // all identical. 00611 // 00612 Assert4(i == 0 || Values[i].first != Values[i-1].first || 00613 Values[i].second == Values[i-1].second, 00614 "PHI node has multiple entries for the same basic block with " 00615 "different incoming values!", PN, Values[i].first, 00616 Values[i].second, Values[i-1].second); 00617 00618 // Check to make sure that the predecessors and PHI node entries are 00619 // matched up. 00620 Assert3(Values[i].first == Preds[i], 00621 "PHI node entries do not match predecessors!", PN, 00622 Values[i].first, Preds[i]); 00623 } 00624 } 00625 } 00626 } 00627 00628 void Verifier::visitTerminatorInst(TerminatorInst &I) { 00629 // Ensure that terminators only exist at the end of the basic block. 00630 Assert1(&I == I.getParent()->getTerminator(), 00631 "Terminator found in the middle of a basic block!", I.getParent()); 00632 visitInstruction(I); 00633 } 00634 00635 void Verifier::visitReturnInst(ReturnInst &RI) { 00636 Function *F = RI.getParent()->getParent(); 00637 unsigned N = RI.getNumOperands(); 00638 if (F->getReturnType() == Type::VoidTy) 00639 Assert2(N == 0, 00640 "Found return instr that returns void in Function of non-void " 00641 "return type!", &RI, F->getReturnType()); 00642 else if (N == 1 && F->getReturnType() == RI.getOperand(0)->getType()) { 00643 // Exactly one return value and it matches the return type. Good. 00644 } else if (const StructType *STy = dyn_cast<StructType>(F->getReturnType())) { 00645 // The return type is a struct; check for multiple return values. 00646 Assert2(STy->getNumElements() == N, 00647 "Incorrect number of return values in ret instruction!", 00648 &RI, F->getReturnType()); 00649 for (unsigned i = 0; i != N; ++i) 00650 Assert2(STy->getElementType(i) == RI.getOperand(i)->getType(), 00651 "Function return type does not match operand " 00652 "type of return inst!", &RI, F->getReturnType()); 00653 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(F->getReturnType())) { 00654 // The return type is an array; check for multiple return values. 00655 Assert2(ATy->getNumElements() == N, 00656 "Incorrect number of return values in ret instruction!", 00657 &RI, F->getReturnType()); 00658 for (unsigned i = 0; i != N; ++i) 00659 Assert2(ATy->getElementType() == RI.getOperand(i)->getType(), 00660 "Function return type does not match operand " 00661 "type of return inst!", &RI, F->getReturnType()); 00662 } else { 00663 CheckFailed("Function return type does not match operand " 00664 "type of return inst!", &RI, F->getReturnType()); 00665 } 00666 00667 // Check to make sure that the return value has necessary properties for 00668 // terminators... 00669 visitTerminatorInst(RI); 00670 } 00671 00672 void Verifier::visitSwitchInst(SwitchInst &SI) { 00673 // Check to make sure that all of the constants in the switch instruction 00674 // have the same type as the switched-on value. 00675 const Type *SwitchTy = SI.getCondition()->getType(); 00676 for (unsigned i = 1, e = SI.getNumCases(); i != e; ++i) 00677 Assert1(SI.getCaseValue(i)->getType() == SwitchTy, 00678 "Switch constants must all be same type as switch value!", &SI); 00679 00680 visitTerminatorInst(SI); 00681 } 00682 00683 void Verifier::visitSelectInst(SelectInst &SI) { 00684 if (const VectorType* vt 00685 = dyn_cast<VectorType>(SI.getCondition()->getType())) { 00686 Assert1( vt->getElementType() == Type::Int1Ty, 00687 "Select condition type must be vector of bool!", &SI); 00688 if (const VectorType* val_vt 00689 = dyn_cast<VectorType>(SI.getTrueValue()->getType())) { 00690 Assert1( vt->getNumElements() == val_vt->getNumElements(), 00691 "Select vector size != value vector size", &SI); 00692 } else { 00693 Assert1(0, "Vector select values must have vector types", &SI); 00694 } 00695 } else { 00696 Assert1(SI.getCondition()->getType() == Type::Int1Ty, 00697 "Select condition type must be bool!", &SI); 00698 } 00699 Assert1(SI.getTrueValue()->getType() == SI.getFalseValue()->getType(), 00700 "Select values must have identical types!", &SI); 00701 Assert1(SI.getTrueValue()->getType() == SI.getType(), 00702 "Select values must have same type as select instruction!", &SI); 00703 visitInstruction(SI); 00704 } 00705 00706 00707 /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of 00708 /// a pass, if any exist, it's an error. 00709 /// 00710 void Verifier::visitUserOp1(Instruction &I) { 00711 Assert1(0, "User-defined operators should not live outside of a pass!", &I); 00712 } 00713 00714 void Verifier::visitTruncInst(TruncInst &I) { 00715 // Get the source and destination types 00716 const Type *SrcTy = I.getOperand(0)->getType(); 00717 const Type *DestTy = I.getType(); 00718 00719 // Get the size of the types in bits, we'll need this later 00720 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); 00721 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); 00722 00723 Assert1(SrcTy->isIntOrIntVector(), "Trunc only operates on integer", &I); 00724 Assert1(DestTy->isIntOrIntVector(), "Trunc only produces integer", &I); 00725 Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I); 00726 00727 visitInstruction(I); 00728 } 00729 00730 void Verifier::visitZExtInst(ZExtInst &I) { 00731 // Get the source and destination types 00732 const Type *SrcTy = I.getOperand(0)->getType(); 00733 const Type *DestTy = I.getType(); 00734 00735 // Get the size of the types in bits, we'll need this later 00736 Assert1(SrcTy->isIntOrIntVector(), "ZExt only operates on integer", &I); 00737 Assert1(DestTy->isIntOrIntVector(), "ZExt only produces an integer", &I); 00738 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); 00739 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); 00740 00741 Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I); 00742 00743 visitInstruction(I); 00744 } 00745 00746 void Verifier::visitSExtInst(SExtInst &I) { 00747 // Get the source and destination types 00748 const Type *SrcTy = I.getOperand(0)->getType(); 00749 const Type *DestTy = I.getType(); 00750 00751 // Get the size of the types in bits, we'll need this later 00752 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); 00753 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); 00754 00755 Assert1(SrcTy->isIntOrIntVector(), "SExt only operates on integer", &I); 00756 Assert1(DestTy->isIntOrIntVector(), "SExt only produces an integer", &I); 00757 Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I); 00758 00759 visitInstruction(I); 00760 } 00761 00762 void Verifier::visitFPTruncInst(FPTruncInst &I) { 00763 // Get the source and destination types 00764 const Type *SrcTy = I.getOperand(0)->getType(); 00765 const Type *DestTy = I.getType(); 00766 // Get the size of the types in bits, we'll need this later 00767 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); 00768 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); 00769 00770 Assert1(SrcTy->isFPOrFPVector(),"FPTrunc only operates on FP", &I); 00771 Assert1(DestTy->isFPOrFPVector(),"FPTrunc only produces an FP", &I); 00772 Assert1(SrcBitSize > DestBitSize,"DestTy too big for FPTrunc", &I); 00773 00774 visitInstruction(I); 00775 } 00776 00777 void Verifier::visitFPExtInst(FPExtInst &I) { 00778 // Get the source and destination types 00779 const Type *SrcTy = I.getOperand(0)->getType(); 00780 const Type *DestTy = I.getType(); 00781 00782 // Get the size of the types in bits, we'll need this later 00783 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); 00784 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); 00785 00786 Assert1(SrcTy->isFPOrFPVector(),"FPExt only operates on FP", &I); 00787 Assert1(DestTy->isFPOrFPVector(),"FPExt only produces an FP", &I); 00788 Assert1(SrcBitSize < DestBitSize,"DestTy too small for FPExt", &I); 00789 00790 visitInstruction(I); 00791 } 00792 00793 void Verifier::visitUIToFPInst(UIToFPInst &I) { 00794 // Get the source and destination types 00795 const Type *SrcTy = I.getOperand(0)->getType(); 00796 const Type *DestTy = I.getType(); 00797 00798 bool SrcVec = isa<VectorType>(SrcTy); 00799 bool DstVec = isa<VectorType>(DestTy); 00800 00801 Assert1(SrcVec == DstVec, 00802 "UIToFP source and dest must both be vector or scalar", &I); 00803 Assert1(SrcTy->isIntOrIntVector(), 00804 "UIToFP source must be integer or integer vector", &I); 00805 Assert1(DestTy->isFPOrFPVector(), 00806 "UIToFP result must be FP or FP vector", &I); 00807 00808 if (SrcVec && DstVec) 00809 Assert1(cast<VectorType>(SrcTy)->getNumElements() == 00810 cast<VectorType>(DestTy)->getNumElements(), 00811 "UIToFP source and dest vector length mismatch", &I); 00812 00813 visitInstruction(I); 00814 } 00815 00816 void Verifier::visitSIToFPInst(SIToFPInst &I) { 00817 // Get the source and destination types 00818