1 //===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the function verifier interface, that can be used for some
11 // sanity checking of input to the system.
13 // Note that this does not provide full `Java style' security and verifications,
14 // instead it just tries to ensure that code is well-formed.
16 // * Both of a binary operator's parameters are of the same type
17 // * Verify that the indices of mem access instructions match other operands
18 // * Verify that arithmetic and other things are only performed on first-class
19 // types. Verify that shifts & logicals only happen on integrals f.e.
20 // * All of the constants in a switch statement are of the correct type
21 // * The code is in valid SSA form
22 // * It should be illegal to put a label into any other type (like a structure)
23 // or to return one. [except constant arrays!]
24 // * Only phi nodes can be self referential: 'add i32 %0, %0 ; <int>:0' is bad
25 // * PHI nodes must have an entry for each predecessor, with no extras.
26 // * PHI nodes must be the first thing in a basic block, all grouped together
27 // * PHI nodes must have at least one entry
28 // * All basic blocks should only end with terminator insts, not contain them
29 // * The entry node to a function must not have predecessors
30 // * All Instructions must be embedded into a basic block
31 // * Functions cannot take a void-typed parameter
32 // * Verify that a function's argument list agrees with it's declared type.
33 // * It is illegal to specify a name for a void value.
34 // * It is illegal to have a internal global value with no initializer
35 // * It is illegal to have a ret instruction that returns a value that does not
36 // agree with the function return value type.
37 // * Function call argument types match the function prototype
38 // * A landing pad is defined by a landingpad instruction, and can be jumped to
39 // only by the unwind edge of an invoke instruction.
40 // * A landingpad instruction must be the first non-PHI instruction in the
42 // * All landingpad instructions must use the same personality function with
44 // * All other things that are tested by asserts spread about the code...
46 //===----------------------------------------------------------------------===//
48 #include "llvm/Analysis/Verifier.h"
49 #include "llvm/CallingConv.h"
50 #include "llvm/Constants.h"
51 #include "llvm/DerivedTypes.h"
52 #include "llvm/InlineAsm.h"
53 #include "llvm/IntrinsicInst.h"
54 #include "llvm/Metadata.h"
55 #include "llvm/Module.h"
56 #include "llvm/Pass.h"
57 #include "llvm/PassManager.h"
58 #include "llvm/Analysis/Dominators.h"
59 #include "llvm/Assembly/Writer.h"
60 #include "llvm/CodeGen/ValueTypes.h"
61 #include "llvm/Support/CallSite.h"
62 #include "llvm/Support/CFG.h"
63 #include "llvm/Support/Debug.h"
64 #include "llvm/Support/InstVisitor.h"
65 #include "llvm/ADT/SetVector.h"
66 #include "llvm/ADT/SmallPtrSet.h"
67 #include "llvm/ADT/SmallVector.h"
68 #include "llvm/ADT/StringExtras.h"
69 #include "llvm/ADT/STLExtras.h"
70 #include "llvm/Support/ErrorHandling.h"
71 #include "llvm/Support/raw_ostream.h"
76 namespace { // Anonymous namespace for class
77 struct PreVerifier : public FunctionPass {
78 static char ID; // Pass ID, replacement for typeid
80 PreVerifier() : FunctionPass(ID) {
81 initializePreVerifierPass(*PassRegistry::getPassRegistry());
84 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
88 // Check that the prerequisites for successful DominatorTree construction
90 bool runOnFunction(Function &F) {
93 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
94 if (I->empty() || !I->back().isTerminator()) {
95 dbgs() << "Basic Block in function '" << F.getName()
96 << "' does not have terminator!\n";
97 WriteAsOperand(dbgs(), I, true);
104 report_fatal_error("Broken module, no Basic Block terminator!");
111 char PreVerifier::ID = 0;
112 INITIALIZE_PASS(PreVerifier, "preverify", "Preliminary module verification",
114 static char &PreVerifyID = PreVerifier::ID;
117 struct Verifier : public FunctionPass, public InstVisitor<Verifier> {
118 static char ID; // Pass ID, replacement for typeid
119 bool Broken; // Is this module found to be broken?
120 bool RealPass; // Are we not being run by a PassManager?
121 VerifierFailureAction action;
122 // What to do if verification fails.
123 Module *Mod; // Module we are verifying right now
124 LLVMContext *Context; // Context within which we are verifying
125 DominatorTree *DT; // Dominator Tree, caution can be null!
127 std::string Messages;
128 raw_string_ostream MessagesStr;
130 /// InstInThisBlock - when verifying a basic block, keep track of all of the
131 /// instructions we have seen so far. This allows us to do efficient
132 /// dominance checks for the case when an instruction has an operand that is
133 /// an instruction in the same block.
134 SmallPtrSet<Instruction*, 16> InstsInThisBlock;
136 /// MDNodes - keep track of the metadata nodes that have been checked
138 SmallPtrSet<MDNode *, 32> MDNodes;
140 /// PersonalityFn - The personality function referenced by the
141 /// LandingPadInsts. All LandingPadInsts within the same function must use
142 /// the same personality function.
143 const Value *PersonalityFn;
146 : FunctionPass(ID), Broken(false), RealPass(true),
147 action(AbortProcessAction), Mod(0), Context(0), DT(0),
148 MessagesStr(Messages), PersonalityFn(0) {
149 initializeVerifierPass(*PassRegistry::getPassRegistry());
151 explicit Verifier(VerifierFailureAction ctn)
152 : FunctionPass(ID), Broken(false), RealPass(true), action(ctn), Mod(0),
153 Context(0), DT(0), MessagesStr(Messages), PersonalityFn(0) {
154 initializeVerifierPass(*PassRegistry::getPassRegistry());
157 bool doInitialization(Module &M) {
159 Context = &M.getContext();
161 // If this is a real pass, in a pass manager, we must abort before
162 // returning back to the pass manager, or else the pass manager may try to
163 // run other passes on the broken module.
165 return abortIfBroken();
169 bool runOnFunction(Function &F) {
170 // Get dominator information if we are being run by PassManager
171 if (RealPass) DT = &getAnalysis<DominatorTree>();
174 if (!Context) Context = &F.getContext();
177 InstsInThisBlock.clear();
179 // If this is a real pass, in a pass manager, we must abort before
180 // returning back to the pass manager, or else the pass manager may try to
181 // run other passes on the broken module.
183 return abortIfBroken();
188 bool doFinalization(Module &M) {
189 // Scan through, checking all of the external function's linkage now...
190 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
191 visitGlobalValue(*I);
193 // Check to make sure function prototypes are okay.
194 if (I->isDeclaration()) visitFunction(*I);
197 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
199 visitGlobalVariable(*I);
201 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
203 visitGlobalAlias(*I);
205 for (Module::named_metadata_iterator I = M.named_metadata_begin(),
206 E = M.named_metadata_end(); I != E; ++I)
207 visitNamedMDNode(*I);
209 // If the module is broken, abort at this time.
210 return abortIfBroken();
213 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
214 AU.setPreservesAll();
215 AU.addRequiredID(PreVerifyID);
217 AU.addRequired<DominatorTree>();
220 /// abortIfBroken - If the module is broken and we are supposed to abort on
221 /// this condition, do so.
223 bool abortIfBroken() {
224 if (!Broken) return false;
225 MessagesStr << "Broken module found, ";
227 default: llvm_unreachable("Unknown action");
228 case AbortProcessAction:
229 MessagesStr << "compilation aborted!\n";
230 dbgs() << MessagesStr.str();
231 // Client should choose different reaction if abort is not desired
233 case PrintMessageAction:
234 MessagesStr << "verification continues.\n";
235 dbgs() << MessagesStr.str();
237 case ReturnStatusAction:
238 MessagesStr << "compilation terminated.\n";
244 // Verification methods...
245 void visitGlobalValue(GlobalValue &GV);
246 void visitGlobalVariable(GlobalVariable &GV);
247 void visitGlobalAlias(GlobalAlias &GA);
248 void visitNamedMDNode(NamedMDNode &NMD);
249 void visitMDNode(MDNode &MD, Function *F);
250 void visitFunction(Function &F);
251 void visitBasicBlock(BasicBlock &BB);
252 using InstVisitor<Verifier>::visit;
254 void visit(Instruction &I);
256 void visitTruncInst(TruncInst &I);
257 void visitZExtInst(ZExtInst &I);
258 void visitSExtInst(SExtInst &I);
259 void visitFPTruncInst(FPTruncInst &I);
260 void visitFPExtInst(FPExtInst &I);
261 void visitFPToUIInst(FPToUIInst &I);
262 void visitFPToSIInst(FPToSIInst &I);
263 void visitUIToFPInst(UIToFPInst &I);
264 void visitSIToFPInst(SIToFPInst &I);
265 void visitIntToPtrInst(IntToPtrInst &I);
266 void visitPtrToIntInst(PtrToIntInst &I);
267 void visitBitCastInst(BitCastInst &I);
268 void visitPHINode(PHINode &PN);
269 void visitBinaryOperator(BinaryOperator &B);
270 void visitICmpInst(ICmpInst &IC);
271 void visitFCmpInst(FCmpInst &FC);
272 void visitExtractElementInst(ExtractElementInst &EI);
273 void visitInsertElementInst(InsertElementInst &EI);
274 void visitShuffleVectorInst(ShuffleVectorInst &EI);
275 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
276 void visitCallInst(CallInst &CI);
277 void visitInvokeInst(InvokeInst &II);
278 void visitGetElementPtrInst(GetElementPtrInst &GEP);
279 void visitLoadInst(LoadInst &LI);
280 void visitStoreInst(StoreInst &SI);
281 void visitInstruction(Instruction &I);
282 void visitTerminatorInst(TerminatorInst &I);
283 void visitBranchInst(BranchInst &BI);
284 void visitReturnInst(ReturnInst &RI);
285 void visitSwitchInst(SwitchInst &SI);
286 void visitIndirectBrInst(IndirectBrInst &BI);
287 void visitSelectInst(SelectInst &SI);
288 void visitUserOp1(Instruction &I);
289 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
290 void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI);
291 void visitFenceInst(FenceInst &FI);
292 void visitAllocaInst(AllocaInst &AI);
293 void visitExtractValueInst(ExtractValueInst &EVI);
294 void visitInsertValueInst(InsertValueInst &IVI);
295 void visitLandingPadInst(LandingPadInst &LPI);
297 void VerifyCallSite(CallSite CS);
298 bool PerformTypeCheck(Intrinsic::ID ID, Function *F, Type *Ty,
299 int VT, unsigned ArgNo, std::string &Suffix);
300 void VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F,
301 unsigned RetNum, unsigned ParamNum, ...);
302 void VerifyParameterAttrs(Attributes Attrs, Type *Ty,
303 bool isReturnValue, const Value *V);
304 void VerifyFunctionAttrs(FunctionType *FT, const AttrListPtr &Attrs,
307 void WriteValue(const Value *V) {
309 if (isa<Instruction>(V)) {
310 MessagesStr << *V << '\n';
312 WriteAsOperand(MessagesStr, V, true, Mod);
317 void WriteType(Type *T) {
319 MessagesStr << ' ' << *T;
323 // CheckFailed - A check failed, so print out the condition and the message
324 // that failed. This provides a nice place to put a breakpoint if you want
325 // to see why something is not correct.
326 void CheckFailed(const Twine &Message,
327 const Value *V1 = 0, const Value *V2 = 0,
328 const Value *V3 = 0, const Value *V4 = 0) {
329 MessagesStr << Message.str() << "\n";
337 void CheckFailed(const Twine &Message, const Value *V1,
338 Type *T2, const Value *V3 = 0) {
339 MessagesStr << Message.str() << "\n";
346 void CheckFailed(const Twine &Message, Type *T1,
347 Type *T2 = 0, Type *T3 = 0) {
348 MessagesStr << Message.str() << "\n";
355 } // End anonymous namespace
357 char Verifier::ID = 0;
358 INITIALIZE_PASS_BEGIN(Verifier, "verify", "Module Verifier", false, false)
359 INITIALIZE_PASS_DEPENDENCY(PreVerifier)
360 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
361 INITIALIZE_PASS_END(Verifier, "verify", "Module Verifier", false, false)
363 // Assert - We know that cond should be true, if not print an error message.
364 #define Assert(C, M) \
365 do { if (!(C)) { CheckFailed(M); return; } } while (0)
366 #define Assert1(C, M, V1) \
367 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
368 #define Assert2(C, M, V1, V2) \
369 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
370 #define Assert3(C, M, V1, V2, V3) \
371 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
372 #define Assert4(C, M, V1, V2, V3, V4) \
373 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
375 void Verifier::visit(Instruction &I) {
376 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
377 Assert1(I.getOperand(i) != 0, "Operand is null", &I);
378 InstVisitor<Verifier>::visit(I);
382 void Verifier::visitGlobalValue(GlobalValue &GV) {
383 Assert1(!GV.isDeclaration() ||
384 GV.isMaterializable() ||
385 GV.hasExternalLinkage() ||
386 GV.hasDLLImportLinkage() ||
387 GV.hasExternalWeakLinkage() ||
388 (isa<GlobalAlias>(GV) &&
389 (GV.hasLocalLinkage() || GV.hasWeakLinkage())),
390 "Global is external, but doesn't have external or dllimport or weak linkage!",
393 Assert1(!GV.hasDLLImportLinkage() || GV.isDeclaration(),
394 "Global is marked as dllimport, but not external", &GV);
396 Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
397 "Only global variables can have appending linkage!", &GV);
399 if (GV.hasAppendingLinkage()) {
400 GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
401 Assert1(GVar && GVar->getType()->getElementType()->isArrayTy(),
402 "Only global arrays can have appending linkage!", GVar);
405 Assert1(!GV.hasLinkerPrivateWeakDefAutoLinkage() || GV.hasDefaultVisibility(),
406 "linker_private_weak_def_auto can only have default visibility!",
410 void Verifier::visitGlobalVariable(GlobalVariable &GV) {
411 if (GV.hasInitializer()) {
412 Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(),
413 "Global variable initializer type does not match global "
414 "variable type!", &GV);
416 // If the global has common linkage, it must have a zero initializer and
417 // cannot be constant.
418 if (GV.hasCommonLinkage()) {
419 Assert1(GV.getInitializer()->isNullValue(),
420 "'common' global must have a zero initializer!", &GV);
421 Assert1(!GV.isConstant(), "'common' global may not be marked constant!",
425 Assert1(GV.hasExternalLinkage() || GV.hasDLLImportLinkage() ||
426 GV.hasExternalWeakLinkage(),
427 "invalid linkage type for global declaration", &GV);
430 if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
431 GV.getName() == "llvm.global_dtors")) {
432 Assert1(!GV.hasInitializer() || GV.hasAppendingLinkage(),
433 "invalid linkage for intrinsic global variable", &GV);
434 // Don't worry about emitting an error for it not being an array,
435 // visitGlobalValue will complain on appending non-array.
436 if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getType())) {
437 StructType *STy = dyn_cast<StructType>(ATy->getElementType());
438 PointerType *FuncPtrTy =
439 FunctionType::get(Type::getVoidTy(*Context), false)->getPointerTo();
440 Assert1(STy && STy->getNumElements() == 2 &&
441 STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
442 STy->getTypeAtIndex(1) == FuncPtrTy,
443 "wrong type for intrinsic global variable", &GV);
447 visitGlobalValue(GV);
450 void Verifier::visitGlobalAlias(GlobalAlias &GA) {
451 Assert1(!GA.getName().empty(),
452 "Alias name cannot be empty!", &GA);
453 Assert1(GA.hasExternalLinkage() || GA.hasLocalLinkage() ||
455 "Alias should have external or external weak linkage!", &GA);
456 Assert1(GA.getAliasee(),
457 "Aliasee cannot be NULL!", &GA);
458 Assert1(GA.getType() == GA.getAliasee()->getType(),
459 "Alias and aliasee types should match!", &GA);
460 Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA);
462 if (!isa<GlobalValue>(GA.getAliasee())) {
463 const ConstantExpr *CE = dyn_cast<ConstantExpr>(GA.getAliasee());
465 (CE->getOpcode() == Instruction::BitCast ||
466 CE->getOpcode() == Instruction::GetElementPtr) &&
467 isa<GlobalValue>(CE->getOperand(0)),
468 "Aliasee should be either GlobalValue or bitcast of GlobalValue",
472 const GlobalValue* Aliasee = GA.resolveAliasedGlobal(/*stopOnWeak*/ false);
474 "Aliasing chain should end with function or global variable", &GA);
476 visitGlobalValue(GA);
479 void Verifier::visitNamedMDNode(NamedMDNode &NMD) {
480 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) {
481 MDNode *MD = NMD.getOperand(i);
485 Assert1(!MD->isFunctionLocal(),
486 "Named metadata operand cannot be function local!", MD);
491 void Verifier::visitMDNode(MDNode &MD, Function *F) {
492 // Only visit each node once. Metadata can be mutually recursive, so this
493 // avoids infinite recursion here, as well as being an optimization.
494 if (!MDNodes.insert(&MD))
497 for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) {
498 Value *Op = MD.getOperand(i);
501 if (isa<Constant>(Op) || isa<MDString>(Op))
503 if (MDNode *N = dyn_cast<MDNode>(Op)) {
504 Assert2(MD.isFunctionLocal() || !N->isFunctionLocal(),
505 "Global metadata operand cannot be function local!", &MD, N);
509 Assert2(MD.isFunctionLocal(), "Invalid operand for global metadata!", &MD, Op);
511 // If this was an instruction, bb, or argument, verify that it is in the
512 // function that we expect.
513 Function *ActualF = 0;
514 if (Instruction *I = dyn_cast<Instruction>(Op))
515 ActualF = I->getParent()->getParent();
516 else if (BasicBlock *BB = dyn_cast<BasicBlock>(Op))
517 ActualF = BB->getParent();
518 else if (Argument *A = dyn_cast<Argument>(Op))
519 ActualF = A->getParent();
520 assert(ActualF && "Unimplemented function local metadata case!");
522 Assert2(ActualF == F, "function-local metadata used in wrong function",
527 // VerifyParameterAttrs - Check the given attributes for an argument or return
528 // value of the specified type. The value V is printed in error messages.
529 void Verifier::VerifyParameterAttrs(Attributes Attrs, Type *Ty,
530 bool isReturnValue, const Value *V) {
531 if (Attrs == Attribute::None)
534 Attributes FnCheckAttr = Attrs & Attribute::FunctionOnly;
535 Assert1(!FnCheckAttr, "Attribute " + Attribute::getAsString(FnCheckAttr) +
536 " only applies to the function!", V);
539 Attributes RetI = Attrs & Attribute::ParameterOnly;
540 Assert1(!RetI, "Attribute " + Attribute::getAsString(RetI) +
541 " does not apply to return values!", V);
545 i < array_lengthof(Attribute::MutuallyIncompatible); ++i) {
546 Attributes MutI = Attrs & Attribute::MutuallyIncompatible[i];
547 Assert1(!(MutI & (MutI - 1)), "Attributes " +
548 Attribute::getAsString(MutI) + " are incompatible!", V);
551 Attributes TypeI = Attrs & Attribute::typeIncompatible(Ty);
552 Assert1(!TypeI, "Wrong type for attribute " +
553 Attribute::getAsString(TypeI), V);
555 Attributes ByValI = Attrs & Attribute::ByVal;
556 if (PointerType *PTy = dyn_cast<PointerType>(Ty)) {
557 Assert1(!ByValI || PTy->getElementType()->isSized(),
558 "Attribute " + Attribute::getAsString(ByValI) +
559 " does not support unsized types!", V);
562 "Attribute " + Attribute::getAsString(ByValI) +
563 " only applies to parameters with pointer type!", V);
567 // VerifyFunctionAttrs - Check parameter attributes against a function type.
568 // The value V is printed in error messages.
569 void Verifier::VerifyFunctionAttrs(FunctionType *FT,
570 const AttrListPtr &Attrs,
575 bool SawNest = false;
577 for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
578 const AttributeWithIndex &Attr = Attrs.getSlot(i);
582 Ty = FT->getReturnType();
583 else if (Attr.Index-1 < FT->getNumParams())
584 Ty = FT->getParamType(Attr.Index-1);
586 break; // VarArgs attributes, verified elsewhere.
588 VerifyParameterAttrs(Attr.Attrs, Ty, Attr.Index == 0, V);
590 if (Attr.Attrs & Attribute::Nest) {
591 Assert1(!SawNest, "More than one parameter has attribute nest!", V);
595 if (Attr.Attrs & Attribute::StructRet)
596 Assert1(Attr.Index == 1, "Attribute sret not on first parameter!", V);
599 Attributes FAttrs = Attrs.getFnAttributes();
600 Attributes NotFn = FAttrs & (~Attribute::FunctionOnly);
601 Assert1(!NotFn, "Attribute " + Attribute::getAsString(NotFn) +
602 " does not apply to the function!", V);
605 i < array_lengthof(Attribute::MutuallyIncompatible); ++i) {
606 Attributes MutI = FAttrs & Attribute::MutuallyIncompatible[i];
607 Assert1(!(MutI & (MutI - 1)), "Attributes " +
608 Attribute::getAsString(MutI) + " are incompatible!", V);
612 static bool VerifyAttributeCount(const AttrListPtr &Attrs, unsigned Params) {
616 unsigned LastSlot = Attrs.getNumSlots() - 1;
617 unsigned LastIndex = Attrs.getSlot(LastSlot).Index;
618 if (LastIndex <= Params
619 || (LastIndex == (unsigned)~0
620 && (LastSlot == 0 || Attrs.getSlot(LastSlot - 1).Index <= Params)))
626 // visitFunction - Verify that a function is ok.
628 void Verifier::visitFunction(Function &F) {
629 // Check function arguments.
630 FunctionType *FT = F.getFunctionType();
631 unsigned NumArgs = F.arg_size();
633 Assert1(Context == &F.getContext(),
634 "Function context does not match Module context!", &F);
636 Assert1(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
637 Assert2(FT->getNumParams() == NumArgs,
638 "# formal arguments must match # of arguments for function type!",
640 Assert1(F.getReturnType()->isFirstClassType() ||
641 F.getReturnType()->isVoidTy() ||
642 F.getReturnType()->isStructTy(),
643 "Functions cannot return aggregate values!", &F);
645 Assert1(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
646 "Invalid struct return type!", &F);
648 const AttrListPtr &Attrs = F.getAttributes();
650 Assert1(VerifyAttributeCount(Attrs, FT->getNumParams()),
651 "Attributes after last parameter!", &F);
653 // Check function attributes.
654 VerifyFunctionAttrs(FT, Attrs, &F);
656 // Check that this function meets the restrictions on this calling convention.
657 switch (F.getCallingConv()) {
662 case CallingConv::Fast:
663 case CallingConv::Cold:
664 case CallingConv::X86_FastCall:
665 case CallingConv::X86_ThisCall:
666 case CallingConv::PTX_Kernel:
667 case CallingConv::PTX_Device:
668 Assert1(!F.isVarArg(),
669 "Varargs functions must have C calling conventions!", &F);
673 bool isLLVMdotName = F.getName().size() >= 5 &&
674 F.getName().substr(0, 5) == "llvm.";
676 // Check that the argument values match the function type for this function...
678 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
680 Assert2(I->getType() == FT->getParamType(i),
681 "Argument value does not match function argument type!",
682 I, FT->getParamType(i));
683 Assert1(I->getType()->isFirstClassType(),
684 "Function arguments must have first-class types!", I);
686 Assert2(!I->getType()->isMetadataTy(),
687 "Function takes metadata but isn't an intrinsic", I, &F);
690 if (F.isMaterializable()) {
691 // Function has a body somewhere we can't see.
692 } else if (F.isDeclaration()) {
693 Assert1(F.hasExternalLinkage() || F.hasDLLImportLinkage() ||
694 F.hasExternalWeakLinkage(),
695 "invalid linkage type for function declaration", &F);
697 // Verify that this function (which has a body) is not named "llvm.*". It
698 // is not legal to define intrinsics.
699 Assert1(!isLLVMdotName, "llvm intrinsics cannot be defined!", &F);
701 // Check the entry node
702 BasicBlock *Entry = &F.getEntryBlock();
703 Assert1(pred_begin(Entry) == pred_end(Entry),
704 "Entry block to function must not have predecessors!", Entry);
706 // The address of the entry block cannot be taken, unless it is dead.
707 if (Entry->hasAddressTaken()) {
708 Assert1(!BlockAddress::get(Entry)->isConstantUsed(),
709 "blockaddress may not be used with the entry block!", Entry);
713 // If this function is actually an intrinsic, verify that it is only used in
714 // direct call/invokes, never having its "address taken".
715 if (F.getIntrinsicID()) {
717 if (F.hasAddressTaken(&U))
718 Assert1(0, "Invalid user of intrinsic instruction!", U);
722 // verifyBasicBlock - Verify that a basic block is well formed...
724 void Verifier::visitBasicBlock(BasicBlock &BB) {
725 InstsInThisBlock.clear();
727 // Ensure that basic blocks have terminators!
728 Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
730 // Check constraints that this basic block imposes on all of the PHI nodes in
732 if (isa<PHINode>(BB.front())) {
733 SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB));
734 SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
735 std::sort(Preds.begin(), Preds.end());
737 for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) {
738 // Ensure that PHI nodes have at least one entry!
739 Assert1(PN->getNumIncomingValues() != 0,
740 "PHI nodes must have at least one entry. If the block is dead, "
741 "the PHI should be removed!", PN);
742 Assert1(PN->getNumIncomingValues() == Preds.size(),
743 "PHINode should have one entry for each predecessor of its "
744 "parent basic block!", PN);
746 // Get and sort all incoming values in the PHI node...
748 Values.reserve(PN->getNumIncomingValues());
749 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
750 Values.push_back(std::make_pair(PN->getIncomingBlock(i),
751 PN->getIncomingValue(i)));
752 std::sort(Values.begin(), Values.end());
754 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
755 // Check to make sure that if there is more than one entry for a
756 // particular basic block in this PHI node, that the incoming values are
759 Assert4(i == 0 || Values[i].first != Values[i-1].first ||
760 Values[i].second == Values[i-1].second,
761 "PHI node has multiple entries for the same basic block with "
762 "different incoming values!", PN, Values[i].first,
763 Values[i].second, Values[i-1].second);
765 // Check to make sure that the predecessors and PHI node entries are
767 Assert3(Values[i].first == Preds[i],
768 "PHI node entries do not match predecessors!", PN,
769 Values[i].first, Preds[i]);
775 void Verifier::visitTerminatorInst(TerminatorInst &I) {
776 // Ensure that terminators only exist at the end of the basic block.
777 Assert1(&I == I.getParent()->getTerminator(),
778 "Terminator found in the middle of a basic block!", I.getParent());
782 void Verifier::visitBranchInst(BranchInst &BI) {
783 if (BI.isConditional()) {
784 Assert2(BI.getCondition()->getType()->isIntegerTy(1),
785 "Branch condition is not 'i1' type!", &BI, BI.getCondition());
787 visitTerminatorInst(BI);
790 void Verifier::visitReturnInst(ReturnInst &RI) {
791 Function *F = RI.getParent()->getParent();
792 unsigned N = RI.getNumOperands();
793 if (F->getReturnType()->isVoidTy())
795 "Found return instr that returns non-void in Function of void "
796 "return type!", &RI, F->getReturnType());
798 Assert2(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
799 "Function return type does not match operand "
800 "type of return inst!", &RI, F->getReturnType());
802 // Check to make sure that the return value has necessary properties for
804 visitTerminatorInst(RI);
807 void Verifier::visitSwitchInst(SwitchInst &SI) {
808 // Check to make sure that all of the constants in the switch instruction
809 // have the same type as the switched-on value.
810 Type *SwitchTy = SI.getCondition()->getType();
811 SmallPtrSet<ConstantInt*, 32> Constants;
812 for (unsigned i = 1, e = SI.getNumCases(); i != e; ++i) {
813 Assert1(SI.getCaseValue(i)->getType() == SwitchTy,
814 "Switch constants must all be same type as switch value!", &SI);
815 Assert2(Constants.insert(SI.getCaseValue(i)),
816 "Duplicate integer as switch case", &SI, SI.getCaseValue(i));
819 visitTerminatorInst(SI);
822 void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
823 Assert1(BI.getAddress()->getType()->isPointerTy(),
824 "Indirectbr operand must have pointer type!", &BI);
825 for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
826 Assert1(BI.getDestination(i)->getType()->isLabelTy(),
827 "Indirectbr destinations must all have pointer type!", &BI);
829 visitTerminatorInst(BI);
832 void Verifier::visitSelectInst(SelectInst &SI) {
833 Assert1(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
835 "Invalid operands for select instruction!", &SI);
837 Assert1(SI.getTrueValue()->getType() == SI.getType(),
838 "Select values must have same type as select instruction!", &SI);
839 visitInstruction(SI);
842 /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
843 /// a pass, if any exist, it's an error.
845 void Verifier::visitUserOp1(Instruction &I) {
846 Assert1(0, "User-defined operators should not live outside of a pass!", &I);
849 void Verifier::visitTruncInst(TruncInst &I) {
850 // Get the source and destination types
851 Type *SrcTy = I.getOperand(0)->getType();
852 Type *DestTy = I.getType();
854 // Get the size of the types in bits, we'll need this later
855 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
856 unsigned DestBitSize = DestTy->getScalarSizeInBits();
858 Assert1(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
859 Assert1(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
860 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
861 "trunc source and destination must both be a vector or neither", &I);
862 Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I);
867 void Verifier::visitZExtInst(ZExtInst &I) {
868 // Get the source and destination types
869 Type *SrcTy = I.getOperand(0)->getType();
870 Type *DestTy = I.getType();
872 // Get the size of the types in bits, we'll need this later
873 Assert1(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
874 Assert1(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
875 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
876 "zext source and destination must both be a vector or neither", &I);
877 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
878 unsigned DestBitSize = DestTy->getScalarSizeInBits();
880 Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I);
885 void Verifier::visitSExtInst(SExtInst &I) {
886 // Get the source and destination types
887 Type *SrcTy = I.getOperand(0)->getType();
888 Type *DestTy = I.getType();
890 // Get the size of the types in bits, we'll need this later
891 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
892 unsigned DestBitSize = DestTy->getScalarSizeInBits();
894 Assert1(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
895 Assert1(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
896 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
897 "sext source and destination must both be a vector or neither", &I);
898 Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I);
903 void Verifier::visitFPTruncInst(FPTruncInst &I) {
904 // Get the source and destination types
905 Type *SrcTy = I.getOperand(0)->getType();
906 Type *DestTy = I.getType();
907 // Get the size of the types in bits, we'll need this later
908 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
909 unsigned DestBitSize = DestTy->getScalarSizeInBits();
911 Assert1(SrcTy->isFPOrFPVectorTy(),"FPTrunc only operates on FP", &I);
912 Assert1(DestTy->isFPOrFPVectorTy(),"FPTrunc only produces an FP", &I);
913 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
914 "fptrunc source and destination must both be a vector or neither",&I);
915 Assert1(SrcBitSize > DestBitSize,"DestTy too big for FPTrunc", &I);
920 void Verifier::visitFPExtInst(FPExtInst &I) {
921 // Get the source and destination types
922 Type *SrcTy = I.getOperand(0)->getType();
923 Type *DestTy = I.getType();
925 // Get the size of the types in bits, we'll need this later
926 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
927 unsigned DestBitSize = DestTy->getScalarSizeInBits();
929 Assert1(SrcTy->isFPOrFPVectorTy(),"FPExt only operates on FP", &I);
930 Assert1(DestTy->isFPOrFPVectorTy(),"FPExt only produces an FP", &I);
931 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
932 "fpext source and destination must both be a vector or neither", &I);
933 Assert1(SrcBitSize < DestBitSize,"DestTy too small for FPExt", &I);
938 void Verifier::visitUIToFPInst(UIToFPInst &I) {
939 // Get the source and destination types
940 Type *SrcTy = I.getOperand(0)->getType();
941 Type *DestTy = I.getType();
943 bool SrcVec = SrcTy->isVectorTy();
944 bool DstVec = DestTy->isVectorTy();
946 Assert1(SrcVec == DstVec,
947 "UIToFP source and dest must both be vector or scalar", &I);
948 Assert1(SrcTy->isIntOrIntVectorTy(),
949 "UIToFP source must be integer or integer vector", &I);
950 Assert1(DestTy->isFPOrFPVectorTy(),
951 "UIToFP result must be FP or FP vector", &I);
953 if (SrcVec && DstVec)
954 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
955 cast<VectorType>(DestTy)->getNumElements(),
956 "UIToFP source and dest vector length mismatch", &I);
961 void Verifier::visitSIToFPInst(SIToFPInst &I) {
962 // Get the source and destination types
963 Type *SrcTy = I.getOperand(0)->getType();
964 Type *DestTy = I.getType();
966 bool SrcVec = SrcTy->isVectorTy();
967 bool DstVec = DestTy->isVectorTy();
969 Assert1(SrcVec == DstVec,
970 "SIToFP source and dest must both be vector or scalar", &I);
971 Assert1(SrcTy->isIntOrIntVectorTy(),
972 "SIToFP source must be integer or integer vector", &I);
973 Assert1(DestTy->isFPOrFPVectorTy(),
974 "SIToFP result must be FP or FP vector", &I);
976 if (SrcVec && DstVec)
977 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
978 cast<VectorType>(DestTy)->getNumElements(),
979 "SIToFP source and dest vector length mismatch", &I);
984 void Verifier::visitFPToUIInst(FPToUIInst &I) {
985 // Get the source and destination types
986 Type *SrcTy = I.getOperand(0)->getType();
987 Type *DestTy = I.getType();
989 bool SrcVec = SrcTy->isVectorTy();
990 bool DstVec = DestTy->isVectorTy();
992 Assert1(SrcVec == DstVec,
993 "FPToUI source and dest must both be vector or scalar", &I);
994 Assert1(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector",
996 Assert1(DestTy->isIntOrIntVectorTy(),
997 "FPToUI result must be integer or integer vector", &I);
999 if (SrcVec && DstVec)
1000 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1001 cast<VectorType>(DestTy)->getNumElements(),
1002 "FPToUI source and dest vector length mismatch", &I);
1004 visitInstruction(I);
1007 void Verifier::visitFPToSIInst(FPToSIInst &I) {
1008 // Get the source and destination types
1009 Type *SrcTy = I.getOperand(0)->getType();
1010 Type *DestTy = I.getType();
1012 bool SrcVec = SrcTy->isVectorTy();
1013 bool DstVec = DestTy->isVectorTy();
1015 Assert1(SrcVec == DstVec,
1016 "FPToSI source and dest must both be vector or scalar", &I);
1017 Assert1(SrcTy->isFPOrFPVectorTy(),
1018 "FPToSI source must be FP or FP vector", &I);
1019 Assert1(DestTy->isIntOrIntVectorTy(),
1020 "FPToSI result must be integer or integer vector", &I);
1022 if (SrcVec && DstVec)
1023 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1024 cast<VectorType>(DestTy)->getNumElements(),
1025 "FPToSI source and dest vector length mismatch", &I);
1027 visitInstruction(I);
1030 void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
1031 // Get the source and destination types
1032 Type *SrcTy = I.getOperand(0)->getType();
1033 Type *DestTy = I.getType();
1035 Assert1(SrcTy->isPointerTy(), "PtrToInt source must be pointer", &I);
1036 Assert1(DestTy->isIntegerTy(), "PtrToInt result must be integral", &I);
1038 visitInstruction(I);
1041 void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
1042 // Get the source and destination types
1043 Type *SrcTy = I.getOperand(0)->getType();
1044 Type *DestTy = I.getType();
1046 Assert1(SrcTy->isIntegerTy(), "IntToPtr source must be an integral", &I);
1047 Assert1(DestTy->isPointerTy(), "IntToPtr result must be a pointer",&I);
1049 visitInstruction(I);
1052 void Verifier::visitBitCastInst(BitCastInst &I) {
1053 // Get the source and destination types
1054 Type *SrcTy = I.getOperand(0)->getType();
1055 Type *DestTy = I.getType();
1057 // Get the size of the types in bits, we'll need this later
1058 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1059 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
1061 // BitCast implies a no-op cast of type only. No bits change.
1062 // However, you can't cast pointers to anything but pointers.
1063 Assert1(DestTy->isPointerTy() == DestTy->isPointerTy(),
1064 "Bitcast requires both operands to be pointer or neither", &I);
1065 Assert1(SrcBitSize == DestBitSize, "Bitcast requires types of same width",&I);
1067 // Disallow aggregates.
1068 Assert1(!SrcTy->isAggregateType(),
1069 "Bitcast operand must not be aggregate", &I);
1070 Assert1(!DestTy->isAggregateType(),
1071 "Bitcast type must not be aggregate", &I);
1073 visitInstruction(I);
1076 /// visitPHINode - Ensure that a PHI node is well formed.
1078 void Verifier::visitPHINode(PHINode &PN) {
1079 // Ensure that the PHI nodes are all grouped together at the top of the block.
1080 // This can be tested by checking whether the instruction before this is
1081 // either nonexistent (because this is begin()) or is a PHI node. If not,
1082 // then there is some other instruction before a PHI.
1083 Assert2(&PN == &PN.getParent()->front() ||
1084 isa<PHINode>(--BasicBlock::iterator(&PN)),
1085 "PHI nodes not grouped at top of basic block!",
1086 &PN, PN.getParent());
1088 // Check that all of the values of the PHI node have the same type as the
1089 // result, and that the incoming blocks are really basic blocks.
1090 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
1091 Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
1092 "PHI node operands are not the same type as the result!", &PN);
1095 // All other PHI node constraints are checked in the visitBasicBlock method.
1097 visitInstruction(PN);
1100 void Verifier::VerifyCallSite(CallSite CS) {
1101 Instruction *I = CS.getInstruction();
1103 Assert1(CS.getCalledValue()->getType()->isPointerTy(),
1104 "Called function must be a pointer!", I);
1105 PointerType *FPTy = cast<PointerType>(CS.getCalledValue()->getType());
1107 Assert1(FPTy->getElementType()->isFunctionTy(),
1108 "Called function is not pointer to function type!", I);
1109 FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
1111 // Verify that the correct number of arguments are being passed
1112 if (FTy->isVarArg())
1113 Assert1(CS.arg_size() >= FTy->getNumParams(),
1114 "Called function requires more parameters than were provided!",I);
1116 Assert1(CS.arg_size() == FTy->getNumParams(),
1117 "Incorrect number of arguments passed to called function!", I);
1119 // Verify that all arguments to the call match the function type.
1120 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
1121 Assert3(CS.getArgument(i)->getType() == FTy->getParamType(i),
1122 "Call parameter type does not match function signature!",
1123 CS.getArgument(i), FTy->getParamType(i), I);
1125 const AttrListPtr &Attrs = CS.getAttributes();
1127 Assert1(VerifyAttributeCount(Attrs, CS.arg_size()),
1128 "Attributes after last parameter!", I);
1130 // Verify call attributes.
1131 VerifyFunctionAttrs(FTy, Attrs, I);
1133 if (FTy->isVarArg())
1134 // Check attributes on the varargs part.
1135 for (unsigned Idx = 1 + FTy->getNumParams(); Idx <= CS.arg_size(); ++Idx) {
1136 Attributes Attr = Attrs.getParamAttributes(Idx);
1138 VerifyParameterAttrs(Attr, CS.getArgument(Idx-1)->getType(), false, I);
1140 Attributes VArgI = Attr & Attribute::VarArgsIncompatible;
1141 Assert1(!VArgI, "Attribute " + Attribute::getAsString(VArgI) +
1142 " cannot be used for vararg call arguments!", I);
1145 // Verify that there's no metadata unless it's a direct call to an intrinsic.
1146 if (CS.getCalledFunction() == 0 ||
1147 !CS.getCalledFunction()->getName().startswith("llvm.")) {
1148 for (FunctionType::param_iterator PI = FTy->param_begin(),
1149 PE = FTy->param_end(); PI != PE; ++PI)
1150 Assert1(!(*PI)->isMetadataTy(),
1151 "Function has metadata parameter but isn't an intrinsic", I);
1154 visitInstruction(*I);
1157 void Verifier::visitCallInst(CallInst &CI) {
1158 VerifyCallSite(&CI);
1160 if (Function *F = CI.getCalledFunction())
1161 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
1162 visitIntrinsicFunctionCall(ID, CI);
1165 void Verifier::visitInvokeInst(InvokeInst &II) {
1166 VerifyCallSite(&II);
1167 visitTerminatorInst(II);
1170 /// visitBinaryOperator - Check that both arguments to the binary operator are
1171 /// of the same type!
1173 void Verifier::visitBinaryOperator(BinaryOperator &B) {
1174 Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
1175 "Both operands to a binary operator are not of the same type!", &B);
1177 switch (B.getOpcode()) {
1178 // Check that integer arithmetic operators are only used with
1179 // integral operands.
1180 case Instruction::Add:
1181 case Instruction::Sub:
1182 case Instruction::Mul:
1183 case Instruction::SDiv:
1184 case Instruction::UDiv:
1185 case Instruction::SRem:
1186 case Instruction::URem:
1187 Assert1(B.getType()->isIntOrIntVectorTy(),
1188 "Integer arithmetic operators only work with integral types!", &B);
1189 Assert1(B.getType() == B.getOperand(0)->getType(),
1190 "Integer arithmetic operators must have same type "
1191 "for operands and result!", &B);
1193 // Check that floating-point arithmetic operators are only used with
1194 // floating-point operands.
1195 case Instruction::FAdd:
1196 case Instruction::FSub:
1197 case Instruction::FMul:
1198 case Instruction::FDiv:
1199 case Instruction::FRem:
1200 Assert1(B.getType()->isFPOrFPVectorTy(),
1201 "Floating-point arithmetic operators only work with "
1202 "floating-point types!", &B);
1203 Assert1(B.getType() == B.getOperand(0)->getType(),
1204 "Floating-point arithmetic operators must have same type "
1205 "for operands and result!", &B);
1207 // Check that logical operators are only used with integral operands.
1208 case Instruction::And:
1209 case Instruction::Or:
1210 case Instruction::Xor:
1211 Assert1(B.getType()->isIntOrIntVectorTy(),
1212 "Logical operators only work with integral types!", &B);
1213 Assert1(B.getType() == B.getOperand(0)->getType(),
1214 "Logical operators must have same type for operands and result!",
1217 case Instruction::Shl:
1218 case Instruction::LShr:
1219 case Instruction::AShr:
1220 Assert1(B.getType()->isIntOrIntVectorTy(),
1221 "Shifts only work with integral types!", &B);
1222 Assert1(B.getType() == B.getOperand(0)->getType(),
1223 "Shift return type must be same as operands!", &B);
1226 llvm_unreachable("Unknown BinaryOperator opcode!");
1229 visitInstruction(B);
1232 void Verifier::visitICmpInst(ICmpInst &IC) {
1233 // Check that the operands are the same type
1234 Type *Op0Ty = IC.getOperand(0)->getType();
1235 Type *Op1Ty = IC.getOperand(1)->getType();
1236 Assert1(Op0Ty == Op1Ty,
1237 "Both operands to ICmp instruction are not of the same type!", &IC);
1238 // Check that the operands are the right type
1239 Assert1(Op0Ty->isIntOrIntVectorTy() || Op0Ty->isPointerTy(),
1240 "Invalid operand types for ICmp instruction", &IC);
1241 // Check that the predicate is valid.
1242 Assert1(IC.getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1243 IC.getPredicate() <= CmpInst::LAST_ICMP_PREDICATE,
1244 "Invalid predicate in ICmp instruction!", &IC);
1246 visitInstruction(IC);
1249 void Verifier::visitFCmpInst(FCmpInst &FC) {
1250 // Check that the operands are the same type
1251 Type *Op0Ty = FC.getOperand(0)->getType();
1252 Type *Op1Ty = FC.getOperand(1)->getType();
1253 Assert1(Op0Ty == Op1Ty,
1254 "Both operands to FCmp instruction are not of the same type!", &FC);
1255 // Check that the operands are the right type
1256 Assert1(Op0Ty->isFPOrFPVectorTy(),
1257 "Invalid operand types for FCmp instruction", &FC);
1258 // Check that the predicate is valid.
1259 Assert1(FC.getPredicate() >= CmpInst::FIRST_FCMP_PREDICATE &&
1260 FC.getPredicate() <= CmpInst::LAST_FCMP_PREDICATE,
1261 "Invalid predicate in FCmp instruction!", &FC);
1263 visitInstruction(FC);
1266 void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
1267 Assert1(ExtractElementInst::isValidOperands(EI.getOperand(0),
1269 "Invalid extractelement operands!", &EI);
1270 visitInstruction(EI);
1273 void Verifier::visitInsertElementInst(InsertElementInst &IE) {
1274 Assert1(InsertElementInst::isValidOperands(IE.getOperand(0),
1277 "Invalid insertelement operands!", &IE);
1278 visitInstruction(IE);
1281 void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
1282 Assert1(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
1284 "Invalid shufflevector operands!", &SV);
1285 visitInstruction(SV);
1288 void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
1289 SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end());
1291 GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(), Idxs);
1292 Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
1293 Assert2(GEP.getType()->isPointerTy() &&
1294 cast<PointerType>(GEP.getType())->getElementType() == ElTy,
1295 "GEP is not of right type for indices!", &GEP, ElTy);
1296 visitInstruction(GEP);
1299 void Verifier::visitLoadInst(LoadInst &LI) {
1300 PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
1301 Assert1(PTy, "Load operand must be a pointer.", &LI);
1302 Type *ElTy = PTy->getElementType();
1303 Assert2(ElTy == LI.getType(),
1304 "Load result type does not match pointer operand type!", &LI, ElTy);
1305 visitInstruction(LI);
1308 void Verifier::visitStoreInst(StoreInst &SI) {
1309 PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
1310 Assert1(PTy, "Store operand must be a pointer.", &SI);
1311 Type *ElTy = PTy->getElementType();
1312 Assert2(ElTy == SI.getOperand(0)->getType(),
1313 "Stored value type does not match pointer operand type!",
1315 visitInstruction(SI);
1318 void Verifier::visitAllocaInst(AllocaInst &AI) {
1319 PointerType *PTy = AI.getType();
1320 Assert1(PTy->getAddressSpace() == 0,
1321 "Allocation instruction pointer not in the generic address space!",
1323 Assert1(PTy->getElementType()->isSized(), "Cannot allocate unsized type",
1325 Assert1(AI.getArraySize()->getType()->isIntegerTy(),
1326 "Alloca array size must have integer type", &AI);
1327 visitInstruction(AI);
1330 void Verifier::visitFenceInst(FenceInst &FI) {
1331 const AtomicOrdering Ordering = FI.getOrdering();
1332 Assert1(Ordering == Acquire || Ordering == Release ||
1333 Ordering == AcquireRelease || Ordering == SequentiallyConsistent,
1334 "fence instructions may only have "
1335 "acquire, release, acq_rel, or seq_cst ordering.", &FI);
1336 visitInstruction(FI);
1339 void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
1340 Assert1(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(),
1341 EVI.getIndices()) ==
1343 "Invalid ExtractValueInst operands!", &EVI);
1345 visitInstruction(EVI);
1348 void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
1349 Assert1(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(),
1350 IVI.getIndices()) ==
1351 IVI.getOperand(1)->getType(),
1352 "Invalid InsertValueInst operands!", &IVI);
1354 visitInstruction(IVI);
1357 void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
1358 BasicBlock *BB = LPI.getParent();
1360 // The landingpad instruction is ill-formed if it doesn't have any clauses and
1362 Assert1(LPI.getNumClauses() > 0 || LPI.isCleanup(),
1363 "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
1365 // The landingpad instruction defines its parent as a landing pad block. The
1366 // landing pad block may be branched to only by the unwind edge of an invoke.
1367 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
1368 const InvokeInst *II = dyn_cast<InvokeInst>((*I)->getTerminator());
1369 Assert1(II && II->getUnwindDest() == BB,
1370 "Block containing LandingPadInst must be jumped to "
1371 "only by the unwind edge of an invoke.", &LPI);
1374 // The landingpad instruction must be the first non-PHI instruction in the
1376 BasicBlock::iterator I = BB->begin(), E = BB->end();
1377 while (I != E && isa<PHINode>(I))
1379 Assert1(I != E && isa<LandingPadInst>(I) && I == LPI,
1380 "LandingPadInst not the first non-PHI instruction in the block.",
1383 // The personality functions for all landingpad instructions within the same
1384 // function should match.
1386 Assert1(LPI.getPersonalityFn() == PersonalityFn,
1387 "Personality function doesn't match others in function", &LPI);
1388 PersonalityFn = LPI.getPersonalityFn();
1390 visitInstruction(LPI);
1393 /// verifyInstruction - Verify that an instruction is well formed.
1395 void Verifier::visitInstruction(Instruction &I) {
1396 BasicBlock *BB = I.getParent();
1397 Assert1(BB, "Instruction not embedded in basic block!", &I);
1399 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
1400 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
1402 Assert1(*UI != (User*)&I || !DT->isReachableFromEntry(BB),
1403 "Only PHI nodes may reference their own value!", &I);
1406 // Check that void typed values don't have names
1407 Assert1(!I.getType()->isVoidTy() || !I.hasName(),
1408 "Instruction has a name, but provides a void value!", &I);
1410 // Check that the return value of the instruction is either void or a legal
1412 Assert1(I.getType()->isVoidTy() ||
1413 I.getType()->isFirstClassType(),
1414 "Instruction returns a non-scalar type!", &I);
1416 // Check that the instruction doesn't produce metadata. Calls are already
1417 // checked against the callee type.
1418 Assert1(!I.getType()->isMetadataTy() ||
1419 isa<CallInst>(I) || isa<InvokeInst>(I),
1420 "Invalid use of metadata!", &I);
1422 // Check that all uses of the instruction, if they are instructions
1423 // themselves, actually have parent basic blocks. If the use is not an
1424 // instruction, it is an error!
1425 for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
1427 if (Instruction *Used = dyn_cast<Instruction>(*UI))
1428 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
1429 " embedded in a basic block!", &I, Used);
1431 CheckFailed("Use of instruction is not an instruction!", *UI);
1436 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
1437 Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I);
1439 // Check to make sure that only first-class-values are operands to
1441 if (!I.getOperand(i)->getType()->isFirstClassType()) {
1442 Assert1(0, "Instruction operands must be first-class values!", &I);
1445 if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
1446 // Check to make sure that the "address of" an intrinsic function is never
1448 Assert1(!F->isIntrinsic() || (i + 1 == e && isa<CallInst>(I)),
1449 "Cannot take the address of an intrinsic!", &I);
1450 Assert1(F->getParent() == Mod, "Referencing function in another module!",
1452 } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
1453 Assert1(OpBB->getParent() == BB->getParent(),
1454 "Referring to a basic block in another function!", &I);
1455 } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
1456 Assert1(OpArg->getParent() == BB->getParent(),
1457 "Referring to an argument in another function!", &I);
1458 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
1459 Assert1(GV->getParent() == Mod, "Referencing global in another module!",
1461 } else if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) {
1462 BasicBlock *OpBlock = Op->getParent();
1464 // Check that a definition dominates all of its uses.
1465 if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
1466 // Invoke results are only usable in the normal destination, not in the
1467 // exceptional destination.
1468 BasicBlock *NormalDest = II->getNormalDest();
1470 Assert2(NormalDest != II->getUnwindDest(),
1471 "No uses of invoke possible due to dominance structure!",
1474 // PHI nodes differ from other nodes because they actually "use" the
1475 // value in the predecessor basic blocks they correspond to.
1476 BasicBlock *UseBlock = BB;
1477 if (PHINode *PN = dyn_cast<PHINode>(&I)) {
1478 unsigned j = PHINode::getIncomingValueNumForOperand(i);
1479 UseBlock = PN->getIncomingBlock(j);
1481 Assert2(UseBlock, "Invoke operand is PHI node with bad incoming-BB",
1484 if (isa<PHINode>(I) && UseBlock == OpBlock) {
1485 // Special case of a phi node in the normal destination or the unwind
1487 Assert2(BB == NormalDest || !DT->isReachableFromEntry(UseBlock),
1488 "Invoke result not available in the unwind destination!",
1491 Assert2(DT->dominates(NormalDest, UseBlock) ||
1492 !DT->isReachableFromEntry(UseBlock),
1493 "Invoke result does not dominate all uses!", Op, &I);
1495 // If the normal successor of an invoke instruction has multiple
1496 // predecessors, then the normal edge from the invoke is critical,
1497 // so the invoke value can only be live if the destination block
1498 // dominates all of it's predecessors (other than the invoke).
1499 if (!NormalDest->getSinglePredecessor() &&
1500 DT->isReachableFromEntry(UseBlock))
1501 // If it is used by something non-phi, then the other case is that
1502 // 'NormalDest' dominates all of its predecessors other than the
1503 // invoke. In this case, the invoke value can still be used.
1504 for (pred_iterator PI = pred_begin(NormalDest),
1505 E = pred_end(NormalDest); PI != E; ++PI)
1506 if (*PI != II->getParent() && !DT->dominates(NormalDest, *PI) &&
1507 DT->isReachableFromEntry(*PI)) {
1508 CheckFailed("Invoke result does not dominate all uses!", Op,&I);
1512 } else if (PHINode *PN = dyn_cast<PHINode>(&I)) {
1513 // PHI nodes are more difficult than other nodes because they actually
1514 // "use" the value in the predecessor basic blocks they correspond to.
1515 unsigned j = PHINode::getIncomingValueNumForOperand(i);
1516 BasicBlock *PredBB = PN->getIncomingBlock(j);
1517 Assert2(PredBB && (DT->dominates(OpBlock, PredBB) ||
1518 !DT->isReachableFromEntry(PredBB)),
1519 "Instruction does not dominate all uses!", Op, &I);
1521 if (OpBlock == BB) {
1522 // If they are in the same basic block, make sure that the definition
1523 // comes before the use.
1524 Assert2(InstsInThisBlock.count(Op) || !DT->isReachableFromEntry(BB),
1525 "Instruction does not dominate all uses!", Op, &I);
1528 // Definition must dominate use unless use is unreachable!
1529 Assert2(InstsInThisBlock.count(Op) || DT->dominates(Op, &I) ||
1530 !DT->isReachableFromEntry(BB),
1531 "Instruction does not dominate all uses!", Op, &I);
1533 } else if (isa<InlineAsm>(I.getOperand(i))) {
1534 Assert1((i + 1 == e && isa<CallInst>(I)) ||
1535 (i + 3 == e && isa<InvokeInst>(I)),
1536 "Cannot take the address of an inline asm!", &I);
1539 InstsInThisBlock.insert(&I);
1542 // Flags used by TableGen to mark intrinsic parameters with the
1543 // LLVMExtendedElementVectorType and LLVMTruncatedElementVectorType classes.
1544 static const unsigned ExtendedElementVectorType = 0x40000000;
1545 static const unsigned TruncatedElementVectorType = 0x20000000;
1547 /// visitIntrinsicFunction - Allow intrinsics to be verified in different ways.
1549 void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
1550 Function *IF = CI.getCalledFunction();
1551 Assert1(IF->isDeclaration(), "Intrinsic functions should never be defined!",
1554 #define GET_INTRINSIC_VERIFIER
1555 #include "llvm/Intrinsics.gen"
1556 #undef GET_INTRINSIC_VERIFIER
1558 // If the intrinsic takes MDNode arguments, verify that they are either global
1559 // or are local to *this* function.
1560 for (unsigned i = 0, e = CI.getNumArgOperands(); i != e; ++i)
1561 if (MDNode *MD = dyn_cast<MDNode>(CI.getArgOperand(i)))
1562 visitMDNode(*MD, CI.getParent()->getParent());
1567 case Intrinsic::dbg_declare: { // llvm.dbg.declare
1568 Assert1(CI.getArgOperand(0) && isa<MDNode>(CI.getArgOperand(0)),
1569 "invalid llvm.dbg.declare intrinsic call 1", &CI);
1570 MDNode *MD = cast<MDNode>(CI.getArgOperand(0));
1571 Assert1(MD->getNumOperands() == 1,
1572 "invalid llvm.dbg.declare intrinsic call 2", &CI);
1574 case Intrinsic::memcpy:
1575 case Intrinsic::memmove:
1576 case Intrinsic::memset:
1577 Assert1(isa<ConstantInt>(CI.getArgOperand(3)),
1578 "alignment argument of memory intrinsics must be a constant int",
1580 Assert1(isa<ConstantInt>(CI.getArgOperand(4)),
1581 "isvolatile argument of memory intrinsics must be a constant int",
1584 case Intrinsic::gcroot:
1585 case Intrinsic::gcwrite:
1586 case Intrinsic::gcread:
1587 if (ID == Intrinsic::gcroot) {
1589 dyn_cast<AllocaInst>(CI.getArgOperand(0)->stripPointerCasts());
1590 Assert1(AI, "llvm.gcroot parameter #1 must be an alloca.", &CI);
1591 Assert1(isa<Constant>(CI.getArgOperand(1)),
1592 "llvm.gcroot parameter #2 must be a constant.", &CI);
1593 if (!AI->getType()->getElementType()->isPointerTy()) {
1594 Assert1(!isa<ConstantPointerNull>(CI.getArgOperand(1)),
1595 "llvm.gcroot parameter #1 must either be a pointer alloca, "
1596 "or argument #2 must be a non-null constant.", &CI);
1600 Assert1(CI.getParent()->getParent()->hasGC(),
1601 "Enclosing function does not use GC.", &CI);
1603 case Intrinsic::init_trampoline:
1604 Assert1(isa<Function>(CI.getArgOperand(1)->stripPointerCasts()),
1605 "llvm.init_trampoline parameter #2 must resolve to a function.",
1608 case Intrinsic::prefetch:
1609 Assert1(isa<ConstantInt>(CI.getArgOperand(1)) &&
1610 isa<ConstantInt>(CI.getArgOperand(2)) &&
1611 cast<ConstantInt>(CI.getArgOperand(1))->getZExtValue() < 2 &&
1612 cast<ConstantInt>(CI.getArgOperand(2))->getZExtValue() < 4,
1613 "invalid arguments to llvm.prefetch",
1616 case Intrinsic::stackprotector:
1617 Assert1(isa<AllocaInst>(CI.getArgOperand(1)->stripPointerCasts()),
1618 "llvm.stackprotector parameter #2 must resolve to an alloca.",
1621 case Intrinsic::lifetime_start:
1622 case Intrinsic::lifetime_end:
1623 case Intrinsic::invariant_start:
1624 Assert1(isa<ConstantInt>(CI.getArgOperand(0)),
1625 "size argument of memory use markers must be a constant integer",
1628 case Intrinsic::invariant_end:
1629 Assert1(isa<ConstantInt>(CI.getArgOperand(1)),
1630 "llvm.invariant.end parameter #2 must be a constant integer", &CI);
1635 /// Produce a string to identify an intrinsic parameter or return value.
1636 /// The ArgNo value numbers the return values from 0 to NumRets-1 and the
1637 /// parameters beginning with NumRets.
1639 static std::string IntrinsicParam(unsigned ArgNo, unsigned NumRets) {
1640 if (ArgNo >= NumRets)
1641 return "Intrinsic parameter #" + utostr(ArgNo - NumRets);
1643 return "Intrinsic result type";
1644 return "Intrinsic result type #" + utostr(ArgNo);
1647 bool Verifier::PerformTypeCheck(Intrinsic::ID ID, Function *F, Type *Ty,
1648 int VT, unsigned ArgNo, std::string &Suffix) {
1649 FunctionType *FTy = F->getFunctionType();
1651 unsigned NumElts = 0;
1653 VectorType *VTy = dyn_cast<VectorType>(Ty);
1655 EltTy = VTy->getElementType();
1656 NumElts = VTy->getNumElements();
1659 Type *RetTy = FTy->getReturnType();
1660 StructType *ST = dyn_cast<StructType>(RetTy);
1661 unsigned NumRetVals;
1662 if (RetTy->isVoidTy())
1665 NumRetVals = ST->getNumElements();
1672 // Check flags that indicate a type that is an integral vector type with
1673 // elements that are larger or smaller than the elements of the matched
1675 if ((Match & (ExtendedElementVectorType |
1676 TruncatedElementVectorType)) != 0) {
1677 IntegerType *IEltTy = dyn_cast<IntegerType>(EltTy);
1678 if (!VTy || !IEltTy) {
1679 CheckFailed(IntrinsicParam(ArgNo, NumRetVals) + " is not "
1680 "an integral vector type.", F);
1683 // Adjust the current Ty (in the opposite direction) rather than
1684 // the type being matched against.
1685 if ((Match & ExtendedElementVectorType) != 0) {
1686 if ((IEltTy->getBitWidth() & 1) != 0) {
1687 CheckFailed(IntrinsicParam(ArgNo, NumRetVals) + " vector "
1688 "element bit-width is odd.", F);
1691 Ty = VectorType::getTruncatedElementVectorType(VTy);
1693 Ty = VectorType::getExtendedElementVectorType(VTy);
1694 Match &= ~(ExtendedElementVectorType | TruncatedElementVectorType);
1697 if (Match <= static_cast<int>(NumRetVals - 1)) {
1699 RetTy = ST->getElementType(Match);
1702 CheckFailed(IntrinsicParam(ArgNo, NumRetVals) + " does not "
1703 "match return type.", F);
1707 if (Ty != FTy->getParamType(Match - NumRetVals)) {
1708 CheckFailed(IntrinsicParam(ArgNo, NumRetVals) + " does not "
1709 "match parameter %" + utostr(Match - NumRetVals) + ".", F);
1713 } else if (VT == MVT::iAny) {
1714 if (!EltTy->isIntegerTy()) {
1715 CheckFailed(IntrinsicParam(ArgNo, NumRetVals) + " is not "
1716 "an integer type.", F);
1720 unsigned GotBits = cast<IntegerType>(EltTy)->getBitWidth();
1724 Suffix += "v" + utostr(NumElts);
1726 Suffix += "i" + utostr(GotBits);
1728 // Check some constraints on various intrinsics.
1730 default: break; // Not everything needs to be checked.
1731 case Intrinsic::bswap:
1732 if (GotBits < 16 || GotBits % 16 != 0) {
1733 CheckFailed("Intrinsic requires even byte width argument", F);
1738 } else if (VT == MVT::fAny) {
1739 if (!EltTy->isFloatingPointTy()) {
1740 CheckFailed(IntrinsicParam(ArgNo, NumRetVals) + " is not "
1741 "a floating-point type.", F);
1748 Suffix += "v" + utostr(NumElts);
1750 Suffix += EVT::getEVT(EltTy).getEVTString();
1751 } else if (VT == MVT::vAny) {
1753 CheckFailed(IntrinsicParam(ArgNo, NumRetVals) + " is not a vector type.",
1757 Suffix += ".v" + utostr(NumElts) + EVT::getEVT(EltTy).getEVTString();
1758 } else if (VT == MVT::iPTR) {
1759 if (!Ty->isPointerTy()) {
1760 CheckFailed(IntrinsicParam(ArgNo, NumRetVals) + " is not a "
1761 "pointer and a pointer is required.", F);
1764 } else if (VT == MVT::iPTRAny) {
1765 // Outside of TableGen, we don't distinguish iPTRAny (to any address space)
1766 // and iPTR. In the verifier, we can not distinguish which case we have so
1767 // allow either case to be legal.
1768 if (PointerType* PTyp = dyn_cast<PointerType>(Ty)) {
1769 EVT PointeeVT = EVT::getEVT(PTyp->getElementType(), true);
1770 if (PointeeVT == MVT::Other) {
1771 CheckFailed("Intrinsic has pointer to complex type.");
1774 Suffix += ".p" + utostr(PTyp->getAddressSpace()) +
1775 PointeeVT.getEVTString();
1777 CheckFailed(IntrinsicParam(ArgNo, NumRetVals) + " is not a "
1778 "pointer and a pointer is required.", F);
1781 } else if (EVT((MVT::SimpleValueType)VT).isVector()) {
1782 EVT VVT = EVT((MVT::SimpleValueType)VT);
1784 // If this is a vector argument, verify the number and type of elements.
1785 if (VVT.getVectorElementType() != EVT::getEVT(EltTy)) {
1786 CheckFailed("Intrinsic prototype has incorrect vector element type!", F);
1790 if (VVT.getVectorNumElements() != NumElts) {
1791 CheckFailed("Intrinsic prototype has incorrect number of "
1792 "vector elements!", F);
1795 } else if (EVT((MVT::SimpleValueType)VT).getTypeForEVT(Ty->getContext()) !=
1797 CheckFailed(IntrinsicParam(ArgNo, NumRetVals) + " is wrong!", F);
1799 } else if (EltTy != Ty) {
1800 CheckFailed(IntrinsicParam(ArgNo, NumRetVals) + " is a vector "
1801 "and a scalar is required.", F);
1808 /// VerifyIntrinsicPrototype - TableGen emits calls to this function into
1809 /// Intrinsics.gen. This implements a little state machine that verifies the
1810 /// prototype of intrinsics.
1811 void Verifier::VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F,
1812 unsigned NumRetVals,
1813 unsigned NumParams, ...) {
1815 va_start(VA, NumParams);
1816 FunctionType *FTy = F->getFunctionType();
1818 // For overloaded intrinsics, the Suffix of the function name must match the
1819 // types of the arguments. This variable keeps track of the expected
1820 // suffix, to be checked at the end.
1823 if (FTy->getNumParams() + FTy->isVarArg() != NumParams) {
1824 CheckFailed("Intrinsic prototype has incorrect number of arguments!", F);
1828 Type *Ty = FTy->getReturnType();
1829 StructType *ST = dyn_cast<StructType>(Ty);
1831 if (NumRetVals == 0 && !Ty->isVoidTy()) {
1832 CheckFailed("Intrinsic should return void", F);
1836 // Verify the return types.
1837 if (ST && ST->getNumElements() != NumRetVals) {
1838 CheckFailed("Intrinsic prototype has incorrect number of return types!", F);
1842 for (unsigned ArgNo = 0; ArgNo != NumRetVals; ++ArgNo) {
1843 int VT = va_arg(VA, int); // An MVT::SimpleValueType when non-negative.
1845 if (ST) Ty = ST->getElementType(ArgNo);
1846 if (!PerformTypeCheck(ID, F, Ty, VT, ArgNo, Suffix))
1850 // Verify the parameter types.
1851 for (unsigned ArgNo = 0; ArgNo != NumParams; ++ArgNo) {
1852 int VT = va_arg(VA, int); // An MVT::SimpleValueType when non-negative.
1854 if (VT == MVT::isVoid && ArgNo > 0) {
1855 if (!FTy->isVarArg())
1856 CheckFailed("Intrinsic prototype has no '...'!", F);
1860 if (!PerformTypeCheck(ID, F, FTy->getParamType(ArgNo), VT,
1861 ArgNo + NumRetVals, Suffix))
1867 // For intrinsics without pointer arguments, if we computed a Suffix then the
1868 // intrinsic is overloaded and we need to make sure that the name of the
1869 // function is correct. We add the suffix to the name of the intrinsic and
1870 // compare against the given function name. If they are not the same, the
1871 // function name is invalid. This ensures that overloading of intrinsics
1872 // uses a sane and consistent naming convention. Note that intrinsics with
1873 // pointer argument may or may not be overloaded so we will check assuming it
1874 // has a suffix and not.
1875 if (!Suffix.empty()) {
1876 std::string Name(Intrinsic::getName(ID));
1877 if (Name + Suffix != F->getName()) {
1878 CheckFailed("Overloaded intrinsic has incorrect suffix: '" +
1879 F->getName().substr(Name.length()) + "'. It should be '" +
1884 // Check parameter attributes.
1885 Assert1(F->getAttributes() == Intrinsic::getAttributes(ID),
1886 "Intrinsic has wrong parameter attributes!", F);
1890 //===----------------------------------------------------------------------===//
1891 // Implement the public interfaces to this file...
1892 //===----------------------------------------------------------------------===//
1894 FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) {
1895 return new Verifier(action);
1899 /// verifyFunction - Check a function for errors, printing messages on stderr.
1900 /// Return true if the function is corrupt.
1902 bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) {
1903 Function &F = const_cast<Function&>(f);
1904 assert(!F.isDeclaration() && "Cannot verify external functions");
1906 FunctionPassManager FPM(F.getParent());
1907 Verifier *V = new Verifier(action);
1913 /// verifyModule - Check a module for errors, printing messages on stderr.
1914 /// Return true if the module is corrupt.
1916 bool llvm::verifyModule(const Module &M, VerifierFailureAction action,
1917 std::string *ErrorInfo) {
1919 Verifier *V = new Verifier(action);
1921 PM.run(const_cast<Module&>(M));
1923 if (ErrorInfo && V->Broken)
1924 *ErrorInfo = V->MessagesStr.str();