blob: d305855b436ecd429d7b6de948f7db490955f8cc [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnera5c3dec2002-03-29 19:06:18 +000010// This file defines the function verifier interface, that can be used for some
Chris Lattner00950542001-06-06 20:29:01 +000011// sanity checking of input to the system.
12//
Misha Brukman5b636312004-06-24 21:47:35 +000013// Note that this does not provide full `Java style' security and verifications,
14// instead it just tries to ensure that code is well-formed.
Chris Lattner00950542001-06-06 20:29:01 +000015//
Misha Brukman5b636312004-06-24 21:47:35 +000016// * Both of a binary operator's parameters are of the same type
Chris Lattnera00409e2002-04-24 19:12:21 +000017// * Verify that the indices of mem access instructions match other operands
Misha Brukman5b636312004-06-24 21:47:35 +000018// * Verify that arithmetic and other things are only performed on first-class
Chris Lattner9ce231f2002-08-02 17:37:08 +000019// types. Verify that shifts & logicals only happen on integrals f.e.
Misha Brukman5b636312004-06-24 21:47:35 +000020// * All of the constants in a switch statement are of the correct type
Chris Lattner9ce231f2002-08-02 17:37:08 +000021// * The code is in valid SSA form
Misha Brukman5b636312004-06-24 21:47:35 +000022// * It should be illegal to put a label into any other type (like a structure)
Chris Lattner00950542001-06-06 20:29:01 +000023// or to return one. [except constant arrays!]
Chris Lattnerfdec2462002-03-14 16:53:48 +000024// * Only phi nodes can be self referential: 'add int %0, %0 ; <int>:0' is bad
Chris Lattner44d5bd92002-02-20 17:55:43 +000025// * PHI nodes must have an entry for each predecessor, with no extras.
Chris Lattner24e845f2002-06-25 15:56:27 +000026// * PHI nodes must be the first thing in a basic block, all grouped together
Chris Lattnerf6ffcb62002-10-06 21:00:31 +000027// * PHI nodes must have at least one entry
Chris Lattner24e845f2002-06-25 15:56:27 +000028// * All basic blocks should only end with terminator insts, not contain them
Chris Lattnera5c3dec2002-03-29 19:06:18 +000029// * The entry node to a function must not have predecessors
Misha Brukman6b634522003-10-10 17:54:14 +000030// * All Instructions must be embedded into a basic block
Misha Brukman5b636312004-06-24 21:47:35 +000031// * Functions cannot take a void-typed parameter
Chris Lattnerea249242002-04-13 22:48:46 +000032// * Verify that a function's argument list agrees with it's declared type.
Chris Lattneracd3cae2002-03-15 20:25:09 +000033// * It is illegal to specify a name for a void value.
Misha Brukman6b634522003-10-10 17:54:14 +000034// * It is illegal to have a internal global value with no initializer
Chris Lattner23f0ce62002-04-12 18:20:49 +000035// * It is illegal to have a ret instruction that returns a value that does not
36// agree with the function return value type.
Chris Lattner56732fb2002-05-08 19:49:50 +000037// * Function call argument types match the function prototype
Chris Lattnera00409e2002-04-24 19:12:21 +000038// * All other things that are tested by asserts spread about the code...
Chris Lattner00950542001-06-06 20:29:01 +000039//
40//===----------------------------------------------------------------------===//
41
42#include "llvm/Analysis/Verifier.h"
Brian Gaeke9cebe2d2003-11-16 23:07:42 +000043#include "llvm/Assembly/Writer.h"
Chris Lattner37c121a2005-05-08 22:27:09 +000044#include "llvm/CallingConv.h"
Chris Lattnercf899082004-02-14 02:47:17 +000045#include "llvm/Constants.h"
Chris Lattner44d5bd92002-02-20 17:55:43 +000046#include "llvm/Pass.h"
Chris Lattner00950542001-06-06 20:29:01 +000047#include "llvm/Module.h"
Chris Lattner2eff8592004-03-14 03:16:15 +000048#include "llvm/ModuleProvider.h"
Zhou Sheng5cbf3162007-06-07 06:12:03 +000049#include "llvm/ParameterAttributes.h"
Chris Lattnerea249242002-04-13 22:48:46 +000050#include "llvm/DerivedTypes.h"
Chris Lattner3188b732006-01-26 00:08:45 +000051#include "llvm/InlineAsm.h"
Gordon Henriksenb5085212007-09-18 10:14:30 +000052#include "llvm/IntrinsicInst.h"
Chris Lattnercf899082004-02-14 02:47:17 +000053#include "llvm/PassManager.h"
Chris Lattner9ce231f2002-08-02 17:37:08 +000054#include "llvm/Analysis/Dominators.h"
Chandler Carruth69940402007-08-04 01:51:18 +000055#include "llvm/CodeGen/ValueTypes.h"
Duncan Sandsd9d70392007-12-21 19:19:01 +000056#include "llvm/Support/CallSite.h"
Chris Lattner44d5bd92002-02-20 17:55:43 +000057#include "llvm/Support/CFG.h"
Chris Lattnerd231fc32002-04-18 20:37:37 +000058#include "llvm/Support/InstVisitor.h"
Bill Wendling8f487662006-11-28 02:09:03 +000059#include "llvm/Support/Streams.h"
Chris Lattner78287b42007-02-10 08:19:44 +000060#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner8552fae2007-02-10 08:30:29 +000061#include "llvm/ADT/SmallVector.h"
Chris Lattner536a9d52006-03-31 04:46:47 +000062#include "llvm/ADT/StringExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000063#include "llvm/ADT/STLExtras.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000064#include "llvm/Support/Compiler.h"
Chris Lattner44d5bd92002-02-20 17:55:43 +000065#include <algorithm>
Bill Wendling1a097e32006-12-07 23:41:45 +000066#include <sstream>
Jeff Cohen4c5701d2006-03-31 07:22:05 +000067#include <cstdarg>
Chris Lattner31f84992003-11-21 20:23:48 +000068using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000069
Chris Lattnerd231fc32002-04-18 20:37:37 +000070namespace { // Anonymous namespace for class
Owen Andersonc570e332007-10-31 21:04:18 +000071 struct VISIBILITY_HIDDEN PreVerifier : public FunctionPass {
Owen Anderson765d6452007-11-01 03:54:23 +000072 static char ID; // Pass ID, replacement for typeid
Duncan Sandsd0561902007-11-01 10:50:26 +000073
Owen Anderson765d6452007-11-01 03:54:23 +000074 PreVerifier() : FunctionPass((intptr_t)&ID) { }
Duncan Sandsd0561902007-11-01 10:50:26 +000075
76 // Check that the prerequisites for successful DominatorTree construction
77 // are satisfied.
Owen Anderson765d6452007-11-01 03:54:23 +000078 bool runOnFunction(Function &F) {
Duncan Sandsd0561902007-11-01 10:50:26 +000079 bool Broken = false;
80
81 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
82 if (I->empty() || !I->back().isTerminator()) {
83 cerr << "Basic Block does not have terminator!\n";
84 WriteAsOperand(*cerr, I, true);
85 cerr << "\n";
86 Broken = true;
87 }
88 }
89
90 if (Broken)
91 abort();
92
93 return false;
Owen Anderson765d6452007-11-01 03:54:23 +000094 }
Owen Andersonc570e332007-10-31 21:04:18 +000095 };
Duncan Sandsd0561902007-11-01 10:50:26 +000096
Owen Andersonc570e332007-10-31 21:04:18 +000097 char PreVerifier::ID = 0;
98 RegisterPass<PreVerifier> PreVer("preverify", "Preliminary module verification");
99 const PassInfo *PreVerifyID = PreVer.getPassInfo();
Duncan Sandsd0561902007-11-01 10:50:26 +0000100
Chris Lattnerf190d382006-06-28 21:38:54 +0000101 struct VISIBILITY_HIDDEN
102 Verifier : public FunctionPass, InstVisitor<Verifier> {
Devang Patel19974732007-05-03 01:11:54 +0000103 static char ID; // Pass ID, replacement for typeid
Chris Lattner9ce231f2002-08-02 17:37:08 +0000104 bool Broken; // Is this module found to be broken?
105 bool RealPass; // Are we not being run by a PassManager?
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000106 VerifierFailureAction action;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000107 // What to do if verification fails.
Misha Brukmanab5c6002004-03-02 00:22:19 +0000108 Module *Mod; // Module we are verifying right now
Devang Patel2099ff02007-06-11 15:40:48 +0000109 DominatorTree *DT; // Dominator Tree, caution can be null!
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000110 std::stringstream msgs; // A stringstream to collect messages
Chris Lattner00950542001-06-06 20:29:01 +0000111
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000112 /// InstInThisBlock - when verifying a basic block, keep track of all of the
113 /// instructions we have seen so far. This allows us to do efficient
114 /// dominance checks for the case when an instruction has an operand that is
115 /// an instruction in the same block.
Chris Lattner78287b42007-02-10 08:19:44 +0000116 SmallPtrSet<Instruction*, 16> InstsInThisBlock;
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000117
Misha Brukmanfd939082005-04-21 23:48:37 +0000118 Verifier()
Devang Patel794fd752007-05-01 21:15:47 +0000119 : FunctionPass((intptr_t)&ID),
120 Broken(false), RealPass(true), action(AbortProcessAction),
Devang Patel2099ff02007-06-11 15:40:48 +0000121 DT(0), msgs( std::ios::app | std::ios::out ) {}
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000122 Verifier( VerifierFailureAction ctn )
Devang Patel794fd752007-05-01 21:15:47 +0000123 : FunctionPass((intptr_t)&ID),
Devang Patel2099ff02007-06-11 15:40:48 +0000124 Broken(false), RealPass(true), action(ctn), DT(0),
Devang Patel794fd752007-05-01 21:15:47 +0000125 msgs( std::ios::app | std::ios::out ) {}
Misha Brukmanfd939082005-04-21 23:48:37 +0000126 Verifier(bool AB )
Devang Patel794fd752007-05-01 21:15:47 +0000127 : FunctionPass((intptr_t)&ID),
128 Broken(false), RealPass(true),
Devang Patel2099ff02007-06-11 15:40:48 +0000129 action( AB ? AbortProcessAction : PrintMessageAction), DT(0),
Devang Patel794fd752007-05-01 21:15:47 +0000130 msgs( std::ios::app | std::ios::out ) {}
Devang Patel2099ff02007-06-11 15:40:48 +0000131 Verifier(DominatorTree &dt)
Devang Patel794fd752007-05-01 21:15:47 +0000132 : FunctionPass((intptr_t)&ID),
133 Broken(false), RealPass(false), action(PrintMessageAction),
Devang Patel2099ff02007-06-11 15:40:48 +0000134 DT(&dt), msgs( std::ios::app | std::ios::out ) {}
Chris Lattner9ce231f2002-08-02 17:37:08 +0000135
Chris Lattner00950542001-06-06 20:29:01 +0000136
Chris Lattner24e845f2002-06-25 15:56:27 +0000137 bool doInitialization(Module &M) {
Brian Gaeke9cebe2d2003-11-16 23:07:42 +0000138 Mod = &M;
Reid Spencer78d033e2007-01-06 07:24:44 +0000139 verifyTypeSymbolTable(M.getTypeSymbolTable());
Chris Lattner3e1f1442002-09-19 16:12:19 +0000140
141 // If this is a real pass, in a pass manager, we must abort before
142 // returning back to the pass manager, or else the pass manager may try to
143 // run other passes on the broken module.
Chris Lattner3e1f1442002-09-19 16:12:19 +0000144 if (RealPass)
Reid Spencer7107c3b2006-07-26 16:18:00 +0000145 return abortIfBroken();
Chris Lattnerd231fc32002-04-18 20:37:37 +0000146 return false;
147 }
148
Chris Lattner24e845f2002-06-25 15:56:27 +0000149 bool runOnFunction(Function &F) {
Chris Lattner9ce231f2002-08-02 17:37:08 +0000150 // Get dominator information if we are being run by PassManager
Devang Patel2099ff02007-06-11 15:40:48 +0000151 if (RealPass) DT = &getAnalysis<DominatorTree>();
Chris Lattnercd070752007-04-20 23:59:29 +0000152
153 Mod = F.getParent();
154
Chris Lattnerd231fc32002-04-18 20:37:37 +0000155 visit(F);
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000156 InstsInThisBlock.clear();
Chris Lattner3e1f1442002-09-19 16:12:19 +0000157
158 // If this is a real pass, in a pass manager, we must abort before
159 // returning back to the pass manager, or else the pass manager may try to
160 // run other passes on the broken module.
Chris Lattner3e1f1442002-09-19 16:12:19 +0000161 if (RealPass)
Reid Spencer7107c3b2006-07-26 16:18:00 +0000162 return abortIfBroken();
Chris Lattner3e1f1442002-09-19 16:12:19 +0000163
Chris Lattnerd231fc32002-04-18 20:37:37 +0000164 return false;
165 }
166
Chris Lattner24e845f2002-06-25 15:56:27 +0000167 bool doFinalization(Module &M) {
Chris Lattner794caa12002-04-28 16:04:26 +0000168 // Scan through, checking all of the external function's linkage now...
Chris Lattner7c277b32004-06-03 06:38:43 +0000169 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner53997412003-04-16 20:42:40 +0000170 visitGlobalValue(*I);
Chris Lattner794caa12002-04-28 16:04:26 +0000171
Chris Lattner7c277b32004-06-03 06:38:43 +0000172 // Check to make sure function prototypes are okay.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000173 if (I->isDeclaration()) visitFunction(*I);
Chris Lattner7c277b32004-06-03 06:38:43 +0000174 }
175
Reid Spencer0b118202006-01-16 21:12:35 +0000176 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
177 I != E; ++I)
Chris Lattner56998b22004-12-15 20:23:49 +0000178 visitGlobalVariable(*I);
Chris Lattner61b91bc2002-10-06 22:47:32 +0000179
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000180 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
181 I != E; ++I)
182 visitGlobalAlias(*I);
183
Chris Lattner3e1f1442002-09-19 16:12:19 +0000184 // If the module is broken, abort at this time.
Reid Spencer7107c3b2006-07-26 16:18:00 +0000185 return abortIfBroken();
Chris Lattnera00409e2002-04-24 19:12:21 +0000186 }
187
Chris Lattner97e52e42002-04-28 21:27:06 +0000188 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
189 AU.setPreservesAll();
Owen Andersonc570e332007-10-31 21:04:18 +0000190 AU.addRequiredID(PreVerifyID);
Chris Lattner9ce231f2002-08-02 17:37:08 +0000191 if (RealPass)
Devang Patel2099ff02007-06-11 15:40:48 +0000192 AU.addRequired<DominatorTree>();
Chris Lattner97e52e42002-04-28 21:27:06 +0000193 }
194
Misha Brukmanab5c6002004-03-02 00:22:19 +0000195 /// abortIfBroken - If the module is broken and we are supposed to abort on
196 /// this condition, do so.
197 ///
Reid Spencer7107c3b2006-07-26 16:18:00 +0000198 bool abortIfBroken() {
Chris Lattner05ac92c2006-07-06 18:02:27 +0000199 if (Broken) {
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000200 msgs << "Broken module found, ";
Chris Lattner05ac92c2006-07-06 18:02:27 +0000201 switch (action) {
John Criswell4a9c9042004-05-04 21:46:05 +0000202 case AbortProcessAction:
203 msgs << "compilation aborted!\n";
Bill Wendlinge8156192006-12-07 01:30:32 +0000204 cerr << msgs.str();
John Criswell4a9c9042004-05-04 21:46:05 +0000205 abort();
John Criswell4a9c9042004-05-04 21:46:05 +0000206 case PrintMessageAction:
207 msgs << "verification continues.\n";
Bill Wendlinge8156192006-12-07 01:30:32 +0000208 cerr << msgs.str();
Reid Spencer7107c3b2006-07-26 16:18:00 +0000209 return false;
John Criswell4a9c9042004-05-04 21:46:05 +0000210 case ReturnStatusAction:
Reid Spencer7107c3b2006-07-26 16:18:00 +0000211 msgs << "compilation terminated.\n";
212 return Broken;
John Criswell4a9c9042004-05-04 21:46:05 +0000213 }
Chris Lattner3e1f1442002-09-19 16:12:19 +0000214 }
Reid Spencer7107c3b2006-07-26 16:18:00 +0000215 return false;
Chris Lattner3e1f1442002-09-19 16:12:19 +0000216 }
217
Chris Lattner53997412003-04-16 20:42:40 +0000218
Chris Lattnerd231fc32002-04-18 20:37:37 +0000219 // Verification methods...
Reid Spencer78d033e2007-01-06 07:24:44 +0000220 void verifyTypeSymbolTable(TypeSymbolTable &ST);
Chris Lattner53997412003-04-16 20:42:40 +0000221 void visitGlobalValue(GlobalValue &GV);
Chris Lattner56998b22004-12-15 20:23:49 +0000222 void visitGlobalVariable(GlobalVariable &GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000223 void visitGlobalAlias(GlobalAlias &GA);
Chris Lattner24e845f2002-06-25 15:56:27 +0000224 void visitFunction(Function &F);
225 void visitBasicBlock(BasicBlock &BB);
Reid Spencer3da59db2006-11-27 01:05:10 +0000226 void visitTruncInst(TruncInst &I);
227 void visitZExtInst(ZExtInst &I);
228 void visitSExtInst(SExtInst &I);
229 void visitFPTruncInst(FPTruncInst &I);
230 void visitFPExtInst(FPExtInst &I);
231 void visitFPToUIInst(FPToUIInst &I);
232 void visitFPToSIInst(FPToSIInst &I);
233 void visitUIToFPInst(UIToFPInst &I);
234 void visitSIToFPInst(SIToFPInst &I);
235 void visitIntToPtrInst(IntToPtrInst &I);
236 void visitPtrToIntInst(PtrToIntInst &I);
237 void visitBitCastInst(BitCastInst &I);
Chris Lattner24e845f2002-06-25 15:56:27 +0000238 void visitPHINode(PHINode &PN);
239 void visitBinaryOperator(BinaryOperator &B);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000240 void visitICmpInst(ICmpInst &IC);
241 void visitFCmpInst(FCmpInst &FC);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000242 void visitExtractElementInst(ExtractElementInst &EI);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000243 void visitInsertElementInst(InsertElementInst &EI);
Chris Lattner00f10232006-04-08 01:18:18 +0000244 void visitShuffleVectorInst(ShuffleVectorInst &EI);
Chris Lattner4d45bd02003-10-18 05:57:43 +0000245 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
Chris Lattner24e845f2002-06-25 15:56:27 +0000246 void visitCallInst(CallInst &CI);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000247 void visitInvokeInst(InvokeInst &II);
Chris Lattner24e845f2002-06-25 15:56:27 +0000248 void visitGetElementPtrInst(GetElementPtrInst &GEP);
249 void visitLoadInst(LoadInst &LI);
250 void visitStoreInst(StoreInst &SI);
251 void visitInstruction(Instruction &I);
252 void visitTerminatorInst(TerminatorInst &I);
253 void visitReturnInst(ReturnInst &RI);
Chris Lattner0f9e9d02004-05-21 16:47:21 +0000254 void visitSwitchInst(SwitchInst &SI);
Chris Lattner230c1a72004-03-12 05:54:31 +0000255 void visitSelectInst(SelectInst &SI);
Chris Lattner627079d2002-11-21 16:54:22 +0000256 void visitUserOp1(Instruction &I);
257 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
Brian Gaeked0fde302003-11-11 22:41:34 +0000258 void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI);
Christopher Lamb303dae92007-12-17 01:00:21 +0000259 void visitAllocationInst(AllocationInst &AI);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000260
Duncan Sandsd9d70392007-12-21 19:19:01 +0000261 void VerifyCallSite(CallSite CS);
Chandler Carruth69940402007-08-04 01:51:18 +0000262 void VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F,
263 unsigned Count, ...);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000264 void VerifyParamAttrs(const FunctionType *FT, const ParamAttrsList *Attrs,
265 const Value *V);
Chris Lattner15e87522003-11-21 17:35:51 +0000266
267 void WriteValue(const Value *V) {
268 if (!V) return;
Chris Lattner31f84992003-11-21 20:23:48 +0000269 if (isa<Instruction>(V)) {
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000270 msgs << *V;
Chris Lattner31f84992003-11-21 20:23:48 +0000271 } else {
Chris Lattner3749c9c2006-12-06 06:16:21 +0000272 WriteAsOperand(msgs, V, true, Mod);
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000273 msgs << "\n";
Chris Lattner15e87522003-11-21 17:35:51 +0000274 }
275 }
276
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000277 void WriteType(const Type* T ) {
278 if ( !T ) return;
279 WriteTypeSymbolic(msgs, T, Mod );
280 }
281
Chris Lattner15e87522003-11-21 17:35:51 +0000282
Chris Lattnerd231fc32002-04-18 20:37:37 +0000283 // CheckFailed - A check failed, so print out the condition and the message
284 // that failed. This provides a nice place to put a breakpoint if you want
285 // to see why something is not correct.
Chris Lattner15e87522003-11-21 17:35:51 +0000286 void CheckFailed(const std::string &Message,
287 const Value *V1 = 0, const Value *V2 = 0,
288 const Value *V3 = 0, const Value *V4 = 0) {
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000289 msgs << Message << "\n";
Chris Lattner15e87522003-11-21 17:35:51 +0000290 WriteValue(V1);
291 WriteValue(V2);
292 WriteValue(V3);
293 WriteValue(V4);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000294 Broken = true;
295 }
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000296
Misha Brukmanfd939082005-04-21 23:48:37 +0000297 void CheckFailed( const std::string& Message, const Value* V1,
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000298 const Type* T2, const Value* V3 = 0 ) {
299 msgs << Message << "\n";
300 WriteValue(V1);
301 WriteType(T2);
302 WriteValue(V3);
Reid Spencer5dff1582004-05-27 21:58:13 +0000303 Broken = true;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000304 }
Chris Lattnerd231fc32002-04-18 20:37:37 +0000305 };
Chris Lattnere20a5dd2002-07-23 18:08:17 +0000306
Devang Patel19974732007-05-03 01:11:54 +0000307 char Verifier::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +0000308 RegisterPass<Verifier> X("verify", "Module Verifier");
Chris Lattner31f84992003-11-21 20:23:48 +0000309} // End anonymous namespace
310
Chris Lattner00950542001-06-06 20:29:01 +0000311
Chris Lattner44d5bd92002-02-20 17:55:43 +0000312// Assert - We know that cond should be true, if not print an error message.
313#define Assert(C, M) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000314 do { if (!(C)) { CheckFailed(M); return; } } while (0)
Chris Lattner44d5bd92002-02-20 17:55:43 +0000315#define Assert1(C, M, V1) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000316 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
Chris Lattner44d5bd92002-02-20 17:55:43 +0000317#define Assert2(C, M, V1, V2) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000318 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
Chris Lattner24e845f2002-06-25 15:56:27 +0000319#define Assert3(C, M, V1, V2, V3) \
320 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
321#define Assert4(C, M, V1, V2, V3, V4) \
322 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
Chris Lattner00950542001-06-06 20:29:01 +0000323
Chris Lattner44d5bd92002-02-20 17:55:43 +0000324
Chris Lattner53997412003-04-16 20:42:40 +0000325void Verifier::visitGlobalValue(GlobalValue &GV) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000326 Assert1(!GV.isDeclaration() ||
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000327 GV.hasExternalLinkage() ||
328 GV.hasDLLImportLinkage() ||
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000329 GV.hasExternalWeakLinkage() ||
330 (isa<GlobalAlias>(GV) &&
331 (GV.hasInternalLinkage() || GV.hasWeakLinkage())),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000332 "Global is external, but doesn't have external or dllimport or weak linkage!",
333 &GV);
334
Reid Spencer5cbf9852007-01-30 20:08:39 +0000335 Assert1(!GV.hasDLLImportLinkage() || GV.isDeclaration(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000336 "Global is marked as dllimport, but not external", &GV);
337
Chris Lattner53997412003-04-16 20:42:40 +0000338 Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
339 "Only global variables can have appending linkage!", &GV);
340
341 if (GV.hasAppendingLinkage()) {
342 GlobalVariable &GVar = cast<GlobalVariable>(GV);
343 Assert1(isa<ArrayType>(GVar.getType()->getElementType()),
344 "Only global arrays can have appending linkage!", &GV);
345 }
346}
347
Chris Lattner56998b22004-12-15 20:23:49 +0000348void Verifier::visitGlobalVariable(GlobalVariable &GV) {
Chris Lattner6693da02007-09-19 17:14:45 +0000349 if (GV.hasInitializer()) {
Chris Lattner56998b22004-12-15 20:23:49 +0000350 Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(),
351 "Global variable initializer type does not match global "
352 "variable type!", &GV);
Chris Lattner6693da02007-09-19 17:14:45 +0000353 } else {
354 Assert1(GV.hasExternalLinkage() || GV.hasDLLImportLinkage() ||
355 GV.hasExternalWeakLinkage(),
356 "invalid linkage type for global declaration", &GV);
357 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000358
Chris Lattner56998b22004-12-15 20:23:49 +0000359 visitGlobalValue(GV);
360}
361
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000362void Verifier::visitGlobalAlias(GlobalAlias &GA) {
363 Assert1(!GA.getName().empty(),
364 "Alias name cannot be empty!", &GA);
365 Assert1(GA.hasExternalLinkage() || GA.hasInternalLinkage() ||
366 GA.hasWeakLinkage(),
367 "Alias should have external or external weak linkage!", &GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000368 Assert1(GA.getType() == GA.getAliasee()->getType(),
369 "Alias and aliasee types should match!", &GA);
370
Anton Korobeynikov0f53f7f2007-04-28 14:35:41 +0000371 if (!isa<GlobalValue>(GA.getAliasee())) {
372 const ConstantExpr *CE = dyn_cast<ConstantExpr>(GA.getAliasee());
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000373 Assert1(CE && CE->getOpcode() == Instruction::BitCast &&
374 isa<GlobalValue>(CE->getOperand(0)),
Anton Korobeynikov0f53f7f2007-04-28 14:35:41 +0000375 "Aliasee should be either GlobalValue or bitcast of GlobalValue",
376 &GA);
377 }
378
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000379 visitGlobalValue(GA);
380}
381
Reid Spencer78d033e2007-01-06 07:24:44 +0000382void Verifier::verifyTypeSymbolTable(TypeSymbolTable &ST) {
383}
Chris Lattner56998b22004-12-15 20:23:49 +0000384
Duncan Sandsd9d70392007-12-21 19:19:01 +0000385// VerifyParamAttrs - Check parameter attributes against a function type.
386// The value V is printed in error messages.
387void Verifier::VerifyParamAttrs(const FunctionType *FT,
388 const ParamAttrsList *Attrs,
389 const Value *V) {
390 if (!Attrs)
391 return;
392
393 // Note that when calling a varargs function, the following test disallows
394 // parameter attributes for the arguments corresponding to the varargs part.
395 Assert1(Attrs->size() &&
396 Attrs->getParamIndex(Attrs->size()-1) <= FT->getNumParams(),
397 "Attributes after end of type!", V);
398
399 bool SawNest = false;
400
401 for (unsigned Idx = 0; Idx <= FT->getNumParams(); ++Idx) {
402 uint16_t Attr = Attrs->getParamAttrs(Idx);
403
404 if (!Idx) {
405 uint16_t RetI = Attr & ParamAttr::ParameterOnly;
406 Assert1(!RetI, "Attribute " + Attrs->getParamAttrsText(RetI) +
407 "does not apply to return values!", V);
408 } else {
409 uint16_t ParmI = Attr & ParamAttr::ReturnOnly;
410 Assert1(!ParmI, "Attribute " + Attrs->getParamAttrsText(ParmI) +
411 "only applies to return values!", V);
412 }
413
414 for (unsigned i = 0;
415 i < array_lengthof(ParamAttr::MutuallyIncompatible); ++i) {
416 uint16_t MutI = Attr & ParamAttr::MutuallyIncompatible[i];
417 Assert1(!(MutI & (MutI - 1)), "Attributes " +
418 Attrs->getParamAttrsText(MutI) + "are incompatible!", V);
419 }
420
421 uint16_t IType = Attr & ParamAttr::IntegerTypeOnly;
422 Assert1(!IType || FT->getParamType(Idx-1)->isInteger(),
423 "Attribute " + Attrs->getParamAttrsText(IType) +
424 "should only apply to Integer type!", V);
425
426 uint16_t PType = Attr & ParamAttr::PointerTypeOnly;
427 Assert1(!PType || isa<PointerType>(FT->getParamType(Idx-1)),
428 "Attribute " + Attrs->getParamAttrsText(PType) +
429 "should only apply to Pointer type!", V);
430
431 if (Attr & ParamAttr::ByVal) {
432 const PointerType *Ty =
433 dyn_cast<PointerType>(FT->getParamType(Idx-1));
434 Assert1(!Ty || isa<StructType>(Ty->getElementType()),
435 "Attribute byval should only apply to pointer to structs!", V);
436 }
437
438 if (Attr & ParamAttr::Nest) {
439 Assert1(!SawNest, "More than one parameter has attribute nest!", V);
440 SawNest = true;
441 }
442
443 if (Attr & ParamAttr::StructRet) {
444 Assert1(Idx == 1, "Attribute sret not on first parameter!", V);
445 }
446 }
447}
448
Chris Lattnerd231fc32002-04-18 20:37:37 +0000449// visitFunction - Verify that a function is ok.
Chris Lattner44d5bd92002-02-20 17:55:43 +0000450//
Chris Lattner24e845f2002-06-25 15:56:27 +0000451void Verifier::visitFunction(Function &F) {
Chris Lattner37c121a2005-05-08 22:27:09 +0000452 // Check function arguments.
Chris Lattner24e845f2002-06-25 15:56:27 +0000453 const FunctionType *FT = F.getFunctionType();
Chris Lattner453eed12007-08-18 06:13:19 +0000454 unsigned NumArgs = F.arg_size();
Chris Lattnerea249242002-04-13 22:48:46 +0000455
Chris Lattner69da5cf2002-10-13 20:57:00 +0000456 Assert2(FT->getNumParams() == NumArgs,
Chris Lattnerea249242002-04-13 22:48:46 +0000457 "# formal arguments must match # of arguments for function type!",
Chris Lattner24e845f2002-06-25 15:56:27 +0000458 &F, FT);
Chris Lattnerc282f5a2003-11-21 22:32:23 +0000459 Assert1(F.getReturnType()->isFirstClassType() ||
460 F.getReturnType() == Type::VoidTy,
461 "Functions cannot return aggregate values!", &F);
Chris Lattnerea249242002-04-13 22:48:46 +0000462
Duncan Sandsdc024672007-11-27 13:23:08 +0000463 Assert1(!F.isStructReturn() || FT->getReturnType() == Type::VoidTy,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000464 "Invalid struct-return function!", &F);
465
Duncan Sandsd9d70392007-12-21 19:19:01 +0000466 // Check function attributes.
467 VerifyParamAttrs(FT, F.getParamAttrs(), &F);
Duncan Sandsfdef00f2007-07-27 15:09:54 +0000468
Chris Lattner80105dd2006-05-19 21:25:17 +0000469 // Check that this function meets the restrictions on this calling convention.
470 switch (F.getCallingConv()) {
471 default:
472 break;
473 case CallingConv::C:
474 break;
Chris Lattner80105dd2006-05-19 21:25:17 +0000475 case CallingConv::Fast:
476 case CallingConv::Cold:
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000477 case CallingConv::X86_FastCall:
Chris Lattner80105dd2006-05-19 21:25:17 +0000478 Assert1(!F.isVarArg(),
479 "Varargs functions must have C calling conventions!", &F);
480 break;
481 }
482
Chris Lattnerea249242002-04-13 22:48:46 +0000483 // Check that the argument values match the function type for this function...
Chris Lattner69da5cf2002-10-13 20:57:00 +0000484 unsigned i = 0;
Chris Lattnere3cbe032006-12-16 02:25:35 +0000485 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
486 I != E; ++I, ++i) {
Chris Lattner69da5cf2002-10-13 20:57:00 +0000487 Assert2(I->getType() == FT->getParamType(i),
488 "Argument value does not match function argument type!",
489 I, FT->getParamType(i));
Chris Lattner7c277b32004-06-03 06:38:43 +0000490 // Make sure no aggregates are passed by value.
Misha Brukmanfd939082005-04-21 23:48:37 +0000491 Assert1(I->getType()->isFirstClassType(),
Chris Lattner7c277b32004-06-03 06:38:43 +0000492 "Functions cannot take aggregates as arguments by value!", I);
493 }
Chris Lattnerea249242002-04-13 22:48:46 +0000494
Chris Lattner6693da02007-09-19 17:14:45 +0000495 if (F.isDeclaration()) {
496 Assert1(F.hasExternalLinkage() || F.hasDLLImportLinkage() ||
497 F.hasExternalWeakLinkage(),
498 "invalid linkage type for function declaration", &F);
499 } else {
Chris Lattner4d17caa2006-12-13 04:45:46 +0000500 // Verify that this function (which has a body) is not named "llvm.*". It
501 // is not legal to define intrinsics.
502 if (F.getName().size() >= 5)
503 Assert1(F.getName().substr(0, 5) != "llvm.",
504 "llvm intrinsics cannot be defined!", &F);
505
Chris Lattner69da5cf2002-10-13 20:57:00 +0000506 // Check the entry node
Chris Lattner02a3be02003-09-20 14:39:18 +0000507 BasicBlock *Entry = &F.getEntryBlock();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000508 Assert1(pred_begin(Entry) == pred_end(Entry),
509 "Entry block to function must not have predecessors!", Entry);
510 }
Chris Lattner44d5bd92002-02-20 17:55:43 +0000511}
512
513
Chris Lattnerd231fc32002-04-18 20:37:37 +0000514// verifyBasicBlock - Verify that a basic block is well formed...
515//
Chris Lattner24e845f2002-06-25 15:56:27 +0000516void Verifier::visitBasicBlock(BasicBlock &BB) {
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000517 InstsInThisBlock.clear();
518
Alkis Evlogimenos4f4cf992004-12-04 02:30:42 +0000519 // Ensure that basic blocks have terminators!
520 Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
521
Chris Lattnerbede31f2003-10-05 17:44:18 +0000522 // Check constraints that this basic block imposes on all of the PHI nodes in
523 // it.
524 if (isa<PHINode>(BB.front())) {
Chris Lattnerf8edb622007-02-10 08:33:11 +0000525 SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB));
526 SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
Chris Lattnerbede31f2003-10-05 17:44:18 +0000527 std::sort(Preds.begin(), Preds.end());
Misha Brukmanfd939082005-04-21 23:48:37 +0000528 PHINode *PN;
Chris Lattnerc70a5092004-06-05 17:44:48 +0000529 for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) {
Chris Lattnerbede31f2003-10-05 17:44:18 +0000530
531 // Ensure that PHI nodes have at least one entry!
532 Assert1(PN->getNumIncomingValues() != 0,
533 "PHI nodes must have at least one entry. If the block is dead, "
534 "the PHI should be removed!", PN);
Brian Gaeke2fea9ad2004-05-17 21:15:18 +0000535 Assert1(PN->getNumIncomingValues() == Preds.size(),
536 "PHINode should have one entry for each predecessor of its "
537 "parent basic block!", PN);
Misha Brukmanfd939082005-04-21 23:48:37 +0000538
Chris Lattnerbede31f2003-10-05 17:44:18 +0000539 // Get and sort all incoming values in the PHI node...
Chris Lattnerf8edb622007-02-10 08:33:11 +0000540 Values.clear();
Chris Lattnerbede31f2003-10-05 17:44:18 +0000541 Values.reserve(PN->getNumIncomingValues());
542 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
543 Values.push_back(std::make_pair(PN->getIncomingBlock(i),
544 PN->getIncomingValue(i)));
545 std::sort(Values.begin(), Values.end());
Misha Brukmanfd939082005-04-21 23:48:37 +0000546
Chris Lattnerbede31f2003-10-05 17:44:18 +0000547 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
548 // Check to make sure that if there is more than one entry for a
549 // particular basic block in this PHI node, that the incoming values are
550 // all identical.
551 //
552 Assert4(i == 0 || Values[i].first != Values[i-1].first ||
553 Values[i].second == Values[i-1].second,
554 "PHI node has multiple entries for the same basic block with "
555 "different incoming values!", PN, Values[i].first,
556 Values[i].second, Values[i-1].second);
Misha Brukmanfd939082005-04-21 23:48:37 +0000557
Chris Lattnerbede31f2003-10-05 17:44:18 +0000558 // Check to make sure that the predecessors and PHI node entries are
559 // matched up.
560 Assert3(Values[i].first == Preds[i],
561 "PHI node entries do not match predecessors!", PN,
Misha Brukmanfd939082005-04-21 23:48:37 +0000562 Values[i].first, Preds[i]);
Chris Lattnerbede31f2003-10-05 17:44:18 +0000563 }
564 }
565 }
Chris Lattner24e845f2002-06-25 15:56:27 +0000566}
Chris Lattneracd3cae2002-03-15 20:25:09 +0000567
Chris Lattner24e845f2002-06-25 15:56:27 +0000568void Verifier::visitTerminatorInst(TerminatorInst &I) {
569 // Ensure that terminators only exist at the end of the basic block.
570 Assert1(&I == I.getParent()->getTerminator(),
571 "Terminator found in the middle of a basic block!", I.getParent());
Chris Lattner3535c9b2002-07-18 00:13:42 +0000572 visitInstruction(I);
Chris Lattner24e845f2002-06-25 15:56:27 +0000573}
574
575void Verifier::visitReturnInst(ReturnInst &RI) {
576 Function *F = RI.getParent()->getParent();
577 if (RI.getNumOperands() == 0)
Alkis Evlogimenos8b42b432004-12-04 01:25:06 +0000578 Assert2(F->getReturnType() == Type::VoidTy,
579 "Found return instr that returns void in Function of non-void "
580 "return type!", &RI, F->getReturnType());
Chris Lattner24e845f2002-06-25 15:56:27 +0000581 else
582 Assert2(F->getReturnType() == RI.getOperand(0)->getType(),
583 "Function return type does not match operand "
584 "type of return inst!", &RI, F->getReturnType());
585
Misha Brukman5560c9d2003-08-18 14:43:39 +0000586 // Check to make sure that the return value has necessary properties for
Chris Lattner24e845f2002-06-25 15:56:27 +0000587 // terminators...
588 visitTerminatorInst(RI);
Chris Lattner44d5bd92002-02-20 17:55:43 +0000589}
590
Chris Lattner0f9e9d02004-05-21 16:47:21 +0000591void Verifier::visitSwitchInst(SwitchInst &SI) {
592 // Check to make sure that all of the constants in the switch instruction
593 // have the same type as the switched-on value.
594 const Type *SwitchTy = SI.getCondition()->getType();
595 for (unsigned i = 1, e = SI.getNumCases(); i != e; ++i)
596 Assert1(SI.getCaseValue(i)->getType() == SwitchTy,
597 "Switch constants must all be same type as switch value!", &SI);
598
599 visitTerminatorInst(SI);
600}
601
Chris Lattner230c1a72004-03-12 05:54:31 +0000602void Verifier::visitSelectInst(SelectInst &SI) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000603 Assert1(SI.getCondition()->getType() == Type::Int1Ty,
Chris Lattner230c1a72004-03-12 05:54:31 +0000604 "Select condition type must be bool!", &SI);
605 Assert1(SI.getTrueValue()->getType() == SI.getFalseValue()->getType(),
606 "Select values must have identical types!", &SI);
607 Assert1(SI.getTrueValue()->getType() == SI.getType(),
608 "Select values must have same type as select instruction!", &SI);
Chris Lattner0030e6c2004-09-29 21:19:28 +0000609 visitInstruction(SI);
Chris Lattner230c1a72004-03-12 05:54:31 +0000610}
611
612
Misha Brukmanab5c6002004-03-02 00:22:19 +0000613/// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
614/// a pass, if any exist, it's an error.
615///
Chris Lattner627079d2002-11-21 16:54:22 +0000616void Verifier::visitUserOp1(Instruction &I) {
Chris Lattner536a9d52006-03-31 04:46:47 +0000617 Assert1(0, "User-defined operators should not live outside of a pass!", &I);
Chris Lattner627079d2002-11-21 16:54:22 +0000618}
Chris Lattnerd231fc32002-04-18 20:37:37 +0000619
Reid Spencer3da59db2006-11-27 01:05:10 +0000620void Verifier::visitTruncInst(TruncInst &I) {
621 // Get the source and destination types
622 const Type *SrcTy = I.getOperand(0)->getType();
623 const Type *DestTy = I.getType();
624
625 // Get the size of the types in bits, we'll need this later
626 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
627 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
628
Chris Lattner42a75512007-01-15 02:27:26 +0000629 Assert1(SrcTy->isInteger(), "Trunc only operates on integer", &I);
630 Assert1(DestTy->isInteger(), "Trunc only produces integer", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000631 Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I);
632
633 visitInstruction(I);
634}
635
636void Verifier::visitZExtInst(ZExtInst &I) {
637 // Get the source and destination types
638 const Type *SrcTy = I.getOperand(0)->getType();
639 const Type *DestTy = I.getType();
640
641 // Get the size of the types in bits, we'll need this later
Chris Lattner42a75512007-01-15 02:27:26 +0000642 Assert1(SrcTy->isInteger(), "ZExt only operates on integer", &I);
643 Assert1(DestTy->isInteger(), "ZExt only produces an integer", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000644 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
645 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
646
Reid Spencer3da59db2006-11-27 01:05:10 +0000647 Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I);
648
649 visitInstruction(I);
650}
651
652void Verifier::visitSExtInst(SExtInst &I) {
653 // Get the source and destination types
654 const Type *SrcTy = I.getOperand(0)->getType();
655 const Type *DestTy = I.getType();
656
657 // Get the size of the types in bits, we'll need this later
658 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
659 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
660
Chris Lattner42a75512007-01-15 02:27:26 +0000661 Assert1(SrcTy->isInteger(), "SExt only operates on integer", &I);
662 Assert1(DestTy->isInteger(), "SExt only produces an integer", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000663 Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I);
664
665 visitInstruction(I);
666}
667
668void Verifier::visitFPTruncInst(FPTruncInst &I) {
669 // Get the source and destination types
670 const Type *SrcTy = I.getOperand(0)->getType();
671 const Type *DestTy = I.getType();
672 // Get the size of the types in bits, we'll need this later
673 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
674 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
675
676 Assert1(SrcTy->isFloatingPoint(),"FPTrunc only operates on FP", &I);
677 Assert1(DestTy->isFloatingPoint(),"FPTrunc only produces an FP", &I);
678 Assert1(SrcBitSize > DestBitSize,"DestTy too big for FPTrunc", &I);
679
680 visitInstruction(I);
681}
682
683void Verifier::visitFPExtInst(FPExtInst &I) {
684 // Get the source and destination types
685 const Type *SrcTy = I.getOperand(0)->getType();
686 const Type *DestTy = I.getType();
687
688 // Get the size of the types in bits, we'll need this later
689 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
690 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
691
692 Assert1(SrcTy->isFloatingPoint(),"FPExt only operates on FP", &I);
693 Assert1(DestTy->isFloatingPoint(),"FPExt only produces an FP", &I);
694 Assert1(SrcBitSize < DestBitSize,"DestTy too small for FPExt", &I);
695
696 visitInstruction(I);
697}
698
699void Verifier::visitUIToFPInst(UIToFPInst &I) {
700 // Get the source and destination types
701 const Type *SrcTy = I.getOperand(0)->getType();
702 const Type *DestTy = I.getType();
703
Nate Begemanb348d182007-11-17 03:58:34 +0000704 bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID;
705 bool DstVec = DestTy->getTypeID() == Type::VectorTyID;
706
707 Assert1(SrcVec == DstVec,"UIToFP source and dest must both be vector or scalar", &I);
708 Assert1(SrcTy->isIntOrIntVector(),"UIToFP source must be integer or integer vector", &I);
709 Assert1(DestTy->isFPOrFPVector(),"UIToFP result must be FP or FP vector", &I);
710
711 if (SrcVec && DstVec)
712 Assert1(cast<VectorType>(SrcTy)->getNumElements() == cast<VectorType>(DestTy)->getNumElements(),
713 "UIToFP source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000714
715 visitInstruction(I);
716}
717
718void Verifier::visitSIToFPInst(SIToFPInst &I) {
719 // Get the source and destination types
720 const Type *SrcTy = I.getOperand(0)->getType();
721 const Type *DestTy = I.getType();
722
Nate Begemanb348d182007-11-17 03:58:34 +0000723 bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID;
724 bool DstVec = DestTy->getTypeID() == Type::VectorTyID;
725
726 Assert1(SrcVec == DstVec,"SIToFP source and dest must both be vector or scalar", &I);
727 Assert1(SrcTy->isIntOrIntVector(),"SIToFP source must be integer or integer vector", &I);
728 Assert1(DestTy->isFPOrFPVector(),"SIToFP result must be FP or FP vector", &I);
729
730 if (SrcVec && DstVec)
731 Assert1(cast<VectorType>(SrcTy)->getNumElements() == cast<VectorType>(DestTy)->getNumElements(),
732 "SIToFP source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000733
734 visitInstruction(I);
735}
736
737void Verifier::visitFPToUIInst(FPToUIInst &I) {
738 // Get the source and destination types
739 const Type *SrcTy = I.getOperand(0)->getType();
740 const Type *DestTy = I.getType();
741
Nate Begemanb348d182007-11-17 03:58:34 +0000742 bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID;
743 bool DstVec = DestTy->getTypeID() == Type::VectorTyID;
744
745 Assert1(SrcVec == DstVec,"FPToUI source and dest must both be vector or scalar", &I);
746 Assert1(SrcTy->isFPOrFPVector(),"FPToUI source must be FP or FP vector", &I);
747 Assert1(DestTy->isIntOrIntVector(),"FPToUI result must be integer or integer vector", &I);
748
749 if (SrcVec && DstVec)
750 Assert1(cast<VectorType>(SrcTy)->getNumElements() == cast<VectorType>(DestTy)->getNumElements(),
751 "FPToUI source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000752
753 visitInstruction(I);
754}
755
756void Verifier::visitFPToSIInst(FPToSIInst &I) {
757 // Get the source and destination types
758 const Type *SrcTy = I.getOperand(0)->getType();
759 const Type *DestTy = I.getType();
760
Nate Begemanb348d182007-11-17 03:58:34 +0000761 bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID;
762 bool DstVec = DestTy->getTypeID() == Type::VectorTyID;
763
764 Assert1(SrcVec == DstVec,"FPToSI source and dest must both be vector or scalar", &I);
765 Assert1(SrcTy->isFPOrFPVector(),"FPToSI source must be FP or FP vector", &I);
766 Assert1(DestTy->isIntOrIntVector(),"FPToSI result must be integer or integer vector", &I);
767
768 if (SrcVec && DstVec)
769 Assert1(cast<VectorType>(SrcTy)->getNumElements() == cast<VectorType>(DestTy)->getNumElements(),
770 "FPToSI source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000771
772 visitInstruction(I);
773}
774
775void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
776 // Get the source and destination types
777 const Type *SrcTy = I.getOperand(0)->getType();
778 const Type *DestTy = I.getType();
779
780 Assert1(isa<PointerType>(SrcTy), "PtrToInt source must be pointer", &I);
Chris Lattner42a75512007-01-15 02:27:26 +0000781 Assert1(DestTy->isInteger(), "PtrToInt result must be integral", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000782
783 visitInstruction(I);
784}
785
786void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
787 // Get the source and destination types
788 const Type *SrcTy = I.getOperand(0)->getType();
789 const Type *DestTy = I.getType();
790
Chris Lattner42a75512007-01-15 02:27:26 +0000791 Assert1(SrcTy->isInteger(), "IntToPtr source must be an integral", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000792 Assert1(isa<PointerType>(DestTy), "IntToPtr result must be a pointer",&I);
793
794 visitInstruction(I);
795}
796
797void Verifier::visitBitCastInst(BitCastInst &I) {
798 // Get the source and destination types
799 const Type *SrcTy = I.getOperand(0)->getType();
800 const Type *DestTy = I.getType();
801
802 // Get the size of the types in bits, we'll need this later
803 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
804 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
805
806 // BitCast implies a no-op cast of type only. No bits change.
807 // However, you can't cast pointers to anything but pointers.
808 Assert1(isa<PointerType>(DestTy) == isa<PointerType>(DestTy),
809 "Bitcast requires both operands to be pointer or neither", &I);
810 Assert1(SrcBitSize == DestBitSize, "Bitcast requies types of same width", &I);
811
812 visitInstruction(I);
813}
814
Misha Brukmanab5c6002004-03-02 00:22:19 +0000815/// visitPHINode - Ensure that a PHI node is well formed.
816///
Chris Lattner24e845f2002-06-25 15:56:27 +0000817void Verifier::visitPHINode(PHINode &PN) {
818 // Ensure that the PHI nodes are all grouped together at the top of the block.
819 // This can be tested by checking whether the instruction before this is
Misha Brukman6b634522003-10-10 17:54:14 +0000820 // either nonexistent (because this is begin()) or is a PHI node. If not,
Chris Lattner24e845f2002-06-25 15:56:27 +0000821 // then there is some other instruction before a PHI.
Chris Lattner4d8c16f2007-04-17 17:36:12 +0000822 Assert2(&PN == &PN.getParent()->front() ||
823 isa<PHINode>(--BasicBlock::iterator(&PN)),
Chris Lattner24e845f2002-06-25 15:56:27 +0000824 "PHI nodes not grouped at top of basic block!",
825 &PN, PN.getParent());
826
Chris Lattner579de712003-11-12 07:13:37 +0000827 // Check that all of the operands of the PHI node have the same type as the
828 // result.
829 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
830 Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
831 "PHI node operands are not the same type as the result!", &PN);
832
Chris Lattnerbede31f2003-10-05 17:44:18 +0000833 // All other PHI node constraints are checked in the visitBasicBlock method.
Chris Lattnerd231fc32002-04-18 20:37:37 +0000834
835 visitInstruction(PN);
836}
837
Duncan Sandsd9d70392007-12-21 19:19:01 +0000838void Verifier::VerifyCallSite(CallSite CS) {
839 Instruction *I = CS.getInstruction();
840
841 Assert1(isa<PointerType>(CS.getCalledValue()->getType()),
842 "Called function must be a pointer!", I);
843 const PointerType *FPTy = cast<PointerType>(CS.getCalledValue()->getType());
Chris Lattnerefdd0a22002-04-18 22:11:52 +0000844 Assert1(isa<FunctionType>(FPTy->getElementType()),
Duncan Sandsd9d70392007-12-21 19:19:01 +0000845 "Called function is not pointer to function type!", I);
Chris Lattner56732fb2002-05-08 19:49:50 +0000846
Chris Lattner24e845f2002-06-25 15:56:27 +0000847 const FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
Chris Lattner56732fb2002-05-08 19:49:50 +0000848
849 // Verify that the correct number of arguments are being passed
850 if (FTy->isVarArg())
Duncan Sandsd9d70392007-12-21 19:19:01 +0000851 Assert1(CS.arg_size() >= FTy->getNumParams(),
852 "Called function requires more parameters than were provided!",I);
Chris Lattner56732fb2002-05-08 19:49:50 +0000853 else
Duncan Sandsd9d70392007-12-21 19:19:01 +0000854 Assert1(CS.arg_size() == FTy->getNumParams(),
855 "Incorrect number of arguments passed to called function!", I);
Chris Lattner56732fb2002-05-08 19:49:50 +0000856
857 // Verify that all arguments to the call match the function type...
858 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Duncan Sandsd9d70392007-12-21 19:19:01 +0000859 Assert3(CS.getArgument(i)->getType() == FTy->getParamType(i),
Chris Lattner56732fb2002-05-08 19:49:50 +0000860 "Call parameter type does not match function signature!",
Duncan Sandsd9d70392007-12-21 19:19:01 +0000861 CS.getArgument(i), FTy->getParamType(i), I);
862
863 // Verify call attributes.
864 VerifyParamAttrs(FTy, CS.getParamAttrs(), I);
865
866 visitInstruction(*I);
867}
868
869void Verifier::visitCallInst(CallInst &CI) {
870 VerifyCallSite(&CI);
Chris Lattner3535c9b2002-07-18 00:13:42 +0000871
Anton Korobeynikov536c64b2007-10-28 22:50:32 +0000872 if (Function *F = CI.getCalledFunction()) {
Brian Gaeked0fde302003-11-11 22:41:34 +0000873 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
Chris Lattnerdd035d12003-05-08 03:47:33 +0000874 visitIntrinsicFunctionCall(ID, CI);
Anton Korobeynikov536c64b2007-10-28 22:50:32 +0000875 }
Duncan Sandsd9d70392007-12-21 19:19:01 +0000876}
877
878void Verifier::visitInvokeInst(InvokeInst &II) {
879 VerifyCallSite(&II);
Chris Lattnerefdd0a22002-04-18 22:11:52 +0000880}
Chris Lattnerd231fc32002-04-18 20:37:37 +0000881
Misha Brukmanab5c6002004-03-02 00:22:19 +0000882/// visitBinaryOperator - Check that both arguments to the binary operator are
883/// of the same type!
884///
Chris Lattner24e845f2002-06-25 15:56:27 +0000885void Verifier::visitBinaryOperator(BinaryOperator &B) {
Chris Lattner1a143ae2002-09-09 20:26:04 +0000886 Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
887 "Both operands to a binary operator are not of the same type!", &B);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000888
Reid Spencer832254e2007-02-02 02:16:23 +0000889 switch (B.getOpcode()) {
Chris Lattner1a143ae2002-09-09 20:26:04 +0000890 // Check that logical operators are only used with integral operands.
Reid Spencer832254e2007-02-02 02:16:23 +0000891 case Instruction::And:
892 case Instruction::Or:
893 case Instruction::Xor:
Chris Lattner42a75512007-01-15 02:27:26 +0000894 Assert1(B.getType()->isInteger() ||
Reid Spencer9d6565a2007-02-15 02:26:10 +0000895 (isa<VectorType>(B.getType()) &&
896 cast<VectorType>(B.getType())->getElementType()->isInteger()),
Chris Lattner1a143ae2002-09-09 20:26:04 +0000897 "Logical operators only work with integral types!", &B);
898 Assert1(B.getType() == B.getOperand(0)->getType(),
899 "Logical operators must have same type for operands and result!",
900 &B);
Reid Spencer832254e2007-02-02 02:16:23 +0000901 break;
902 case Instruction::Shl:
903 case Instruction::LShr:
904 case Instruction::AShr:
905 Assert1(B.getType()->isInteger(),
906 "Shift must return an integer result!", &B);
907 Assert1(B.getType() == B.getOperand(0)->getType(),
908 "Shift return type must be same as operands!", &B);
909 /* FALL THROUGH */
910 default:
Chris Lattner1a143ae2002-09-09 20:26:04 +0000911 // Arithmetic operators only work on integer or fp values
912 Assert1(B.getType() == B.getOperand(0)->getType(),
913 "Arithmetic operators must have same type for operands and result!",
914 &B);
Chris Lattner42a75512007-01-15 02:27:26 +0000915 Assert1(B.getType()->isInteger() || B.getType()->isFloatingPoint() ||
Reid Spencer9d6565a2007-02-15 02:26:10 +0000916 isa<VectorType>(B.getType()),
Reid Spencerac9dcb92007-02-15 03:39:18 +0000917 "Arithmetic operators must have integer, fp, or vector type!", &B);
Reid Spencer832254e2007-02-02 02:16:23 +0000918 break;
Chris Lattner1a143ae2002-09-09 20:26:04 +0000919 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000920
Chris Lattnerd231fc32002-04-18 20:37:37 +0000921 visitInstruction(B);
922}
923
Reid Spencer45fb3f32006-11-20 01:22:35 +0000924void Verifier::visitICmpInst(ICmpInst& IC) {
925 // Check that the operands are the same type
926 const Type* Op0Ty = IC.getOperand(0)->getType();
927 const Type* Op1Ty = IC.getOperand(1)->getType();
928 Assert1(Op0Ty == Op1Ty,
929 "Both operands to ICmp instruction are not of the same type!", &IC);
930 // Check that the operands are the right type
Chris Lattner42a75512007-01-15 02:27:26 +0000931 Assert1(Op0Ty->isInteger() || isa<PointerType>(Op0Ty),
Reid Spencer45fb3f32006-11-20 01:22:35 +0000932 "Invalid operand types for ICmp instruction", &IC);
933 visitInstruction(IC);
934}
935
936void Verifier::visitFCmpInst(FCmpInst& FC) {
937 // Check that the operands are the same type
938 const Type* Op0Ty = FC.getOperand(0)->getType();
939 const Type* Op1Ty = FC.getOperand(1)->getType();
940 Assert1(Op0Ty == Op1Ty,
941 "Both operands to FCmp instruction are not of the same type!", &FC);
942 // Check that the operands are the right type
Reid Spenceraffaf072007-01-04 05:22:18 +0000943 Assert1(Op0Ty->isFloatingPoint(),
Reid Spencer45fb3f32006-11-20 01:22:35 +0000944 "Invalid operand types for FCmp instruction", &FC);
945 visitInstruction(FC);
946}
947
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000948void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
Chris Lattner1cbe05b2006-04-08 04:07:52 +0000949 Assert1(ExtractElementInst::isValidOperands(EI.getOperand(0),
950 EI.getOperand(1)),
951 "Invalid extractelement operands!", &EI);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000952 visitInstruction(EI);
953}
954
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000955void Verifier::visitInsertElementInst(InsertElementInst &IE) {
Chris Lattner1cbe05b2006-04-08 04:07:52 +0000956 Assert1(InsertElementInst::isValidOperands(IE.getOperand(0),
957 IE.getOperand(1),
958 IE.getOperand(2)),
959 "Invalid insertelement operands!", &IE);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000960 visitInstruction(IE);
961}
962
Chris Lattner00f10232006-04-08 01:18:18 +0000963void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
964 Assert1(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
965 SV.getOperand(2)),
966 "Invalid shufflevector operands!", &SV);
967 Assert1(SV.getType() == SV.getOperand(0)->getType(),
968 "Result of shufflevector must match first operand type!", &SV);
969
970 // Check to see if Mask is valid.
Reid Spencer9d6565a2007-02-15 02:26:10 +0000971 if (const ConstantVector *MV = dyn_cast<ConstantVector>(SV.getOperand(2))) {
Chris Lattner00f10232006-04-08 01:18:18 +0000972 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000973 Assert1(isa<ConstantInt>(MV->getOperand(i)) ||
Chris Lattner00f10232006-04-08 01:18:18 +0000974 isa<UndefValue>(MV->getOperand(i)),
975 "Invalid shufflevector shuffle mask!", &SV);
976 }
977 } else {
978 Assert1(isa<UndefValue>(SV.getOperand(2)) ||
979 isa<ConstantAggregateZero>(SV.getOperand(2)),
980 "Invalid shufflevector shuffle mask!", &SV);
981 }
982
983 visitInstruction(SV);
984}
985
Chris Lattner24e845f2002-06-25 15:56:27 +0000986void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattner8552fae2007-02-10 08:30:29 +0000987 SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end());
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000988 const Type *ElTy =
989 GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(),
David Greeneb8f74792007-09-04 15:46:09 +0000990 Idxs.begin(), Idxs.end(), true);
Chris Lattner24e845f2002-06-25 15:56:27 +0000991 Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
Chris Lattner8552fae2007-02-10 08:30:29 +0000992 Assert2(isa<PointerType>(GEP.getType()) &&
993 cast<PointerType>(GEP.getType())->getElementType() == ElTy,
Chris Lattner24e845f2002-06-25 15:56:27 +0000994 "GEP is not of right type for indices!", &GEP, ElTy);
Chris Lattnera00409e2002-04-24 19:12:21 +0000995 visitInstruction(GEP);
996}
997
Chris Lattner24e845f2002-06-25 15:56:27 +0000998void Verifier::visitLoadInst(LoadInst &LI) {
Chris Lattner24ea74e2002-08-22 22:49:05 +0000999 const Type *ElTy =
1000 cast<PointerType>(LI.getOperand(0)->getType())->getElementType();
Chris Lattner24e845f2002-06-25 15:56:27 +00001001 Assert2(ElTy == LI.getType(),
Chris Lattner7334f2e2003-11-21 17:06:29 +00001002 "Load result type does not match pointer operand type!", &LI, ElTy);
Chris Lattnera00409e2002-04-24 19:12:21 +00001003 visitInstruction(LI);
1004}
1005
Chris Lattner24e845f2002-06-25 15:56:27 +00001006void Verifier::visitStoreInst(StoreInst &SI) {
Chris Lattner24ea74e2002-08-22 22:49:05 +00001007 const Type *ElTy =
1008 cast<PointerType>(SI.getOperand(1)->getType())->getElementType();
Chris Lattner24e845f2002-06-25 15:56:27 +00001009 Assert2(ElTy == SI.getOperand(0)->getType(),
Chris Lattner7334f2e2003-11-21 17:06:29 +00001010 "Stored value type does not match pointer operand type!", &SI, ElTy);
Chris Lattnera00409e2002-04-24 19:12:21 +00001011 visitInstruction(SI);
1012}
1013
Christopher Lamb303dae92007-12-17 01:00:21 +00001014void Verifier::visitAllocationInst(AllocationInst &AI) {
1015 const PointerType *Ptr = AI.getType();
1016 Assert(Ptr->getAddressSpace() == 0,
1017 "Allocation instruction pointer not in the generic address space!");
1018 visitInstruction(AI);
1019}
1020
Chris Lattnerd231fc32002-04-18 20:37:37 +00001021
Misha Brukmanab5c6002004-03-02 00:22:19 +00001022/// verifyInstruction - Verify that an instruction is well formed.
1023///
Chris Lattner24e845f2002-06-25 15:56:27 +00001024void Verifier::visitInstruction(Instruction &I) {
Misha Brukmanfd939082005-04-21 23:48:37 +00001025 BasicBlock *BB = I.getParent();
Chris Lattner1a143ae2002-09-09 20:26:04 +00001026 Assert1(BB, "Instruction not embedded in basic block!", &I);
Chris Lattnerd231fc32002-04-18 20:37:37 +00001027
Chris Lattnerbede31f2003-10-05 17:44:18 +00001028 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
1029 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
1030 UI != UE; ++UI)
Chris Lattner08092532004-02-27 17:28:25 +00001031 Assert1(*UI != (User*)&I ||
Devang Patel2099ff02007-06-11 15:40:48 +00001032 !DT->dominates(&BB->getParent()->getEntryBlock(), BB),
Chris Lattnerbede31f2003-10-05 17:44:18 +00001033 "Only PHI nodes may reference their own value!", &I);
1034 }
1035
1036 // Check that void typed values don't have names
1037 Assert1(I.getType() != Type::VoidTy || !I.hasName(),
1038 "Instruction has a name, but provides a void value!", &I);
1039
Chris Lattner944cfaf2004-03-29 00:29:36 +00001040 // Check that the return value of the instruction is either void or a legal
1041 // value type.
1042 Assert1(I.getType() == Type::VoidTy || I.getType()->isFirstClassType(),
1043 "Instruction returns a non-scalar type!", &I);
1044
Chris Lattnerd231fc32002-04-18 20:37:37 +00001045 // Check that all uses of the instruction, if they are instructions
1046 // themselves, actually have parent basic blocks. If the use is not an
1047 // instruction, it is an error!
Chris Lattner24e845f2002-06-25 15:56:27 +00001048 for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattnerd231fc32002-04-18 20:37:37 +00001049 UI != UE; ++UI) {
1050 Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!",
1051 *UI);
Chris Lattnerefdd0a22002-04-18 22:11:52 +00001052 Instruction *Used = cast<Instruction>(*UI);
1053 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
Chris Lattner24e845f2002-06-25 15:56:27 +00001054 " embeded in a basic block!", &I, Used);
Chris Lattnerd231fc32002-04-18 20:37:37 +00001055 }
1056
Chris Lattnerbede31f2003-10-05 17:44:18 +00001057 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Chris Lattneraab18202005-02-24 16:58:29 +00001058 Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I);
Chris Lattnerf4ea9212006-07-11 20:29:49 +00001059
1060 // Check to make sure that only first-class-values are operands to
1061 // instructions.
1062 Assert1(I.getOperand(i)->getType()->isFirstClassType(),
1063 "Instruction operands must be first-class values!", &I);
1064
Chris Lattner59c35692004-03-14 03:23:54 +00001065 if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
Chris Lattnerf4ea9212006-07-11 20:29:49 +00001066 // Check to make sure that the "address of" an intrinsic function is never
1067 // taken.
Chris Lattnerdd035d12003-05-08 03:47:33 +00001068 Assert1(!F->isIntrinsic() || (i == 0 && isa<CallInst>(I)),
1069 "Cannot take the address of an intrinsic!", &I);
Chris Lattner19b6dcd2007-04-20 21:48:08 +00001070 Assert1(F->getParent() == Mod, "Referencing function in another module!",
1071 &I);
Chris Lattner59c35692004-03-14 03:23:54 +00001072 } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
1073 Assert1(OpBB->getParent() == BB->getParent(),
1074 "Referring to a basic block in another function!", &I);
1075 } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
1076 Assert1(OpArg->getParent() == BB->getParent(),
1077 "Referring to an argument in another function!", &I);
Chris Lattner19b6dcd2007-04-20 21:48:08 +00001078 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
1079 Assert1(GV->getParent() == Mod, "Referencing global in another module!",
1080 &I);
Chris Lattner59c35692004-03-14 03:23:54 +00001081 } else if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) {
Chris Lattner30768ac2004-01-14 04:25:59 +00001082 BasicBlock *OpBlock = Op->getParent();
Chris Lattner30768ac2004-01-14 04:25:59 +00001083
Chris Lattnerbede31f2003-10-05 17:44:18 +00001084 // Check that a definition dominates all of its uses.
Chris Lattnerbede31f2003-10-05 17:44:18 +00001085 if (!isa<PHINode>(I)) {
Chris Lattnerc9b07022004-01-14 05:42:52 +00001086 // Invoke results are only usable in the normal destination, not in the
1087 // exceptional destination.
Chris Lattnere3cbe032006-12-16 02:25:35 +00001088 if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
Chris Lattnerc9b07022004-01-14 05:42:52 +00001089 OpBlock = II->getNormalDest();
Chris Lattnere3cbe032006-12-16 02:25:35 +00001090
Chris Lattner62d75e72006-12-20 21:20:13 +00001091 Assert2(OpBlock != II->getUnwindDest(),
1092 "No uses of invoke possible due to dominance structure!",
1093 Op, II);
1094
Chris Lattnere3cbe032006-12-16 02:25:35 +00001095 // If the normal successor of an invoke instruction has multiple
1096 // predecessors, then the normal edge from the invoke is critical, so
1097 // the invoke value can only be live if the destination block
1098 // dominates all of it's predecessors (other than the invoke) or if
1099 // the invoke value is only used by a phi in the successor.
1100 if (!OpBlock->getSinglePredecessor() &&
Devang Patel2099ff02007-06-11 15:40:48 +00001101 DT->dominates(&BB->getParent()->getEntryBlock(), BB)) {
Chris Lattnere3cbe032006-12-16 02:25:35 +00001102 // The first case we allow is if the use is a PHI operand in the
1103 // normal block, and if that PHI operand corresponds to the invoke's
1104 // block.
1105 bool Bad = true;
1106 if (PHINode *PN = dyn_cast<PHINode>(&I))
1107 if (PN->getParent() == OpBlock &&
1108 PN->getIncomingBlock(i/2) == Op->getParent())
1109 Bad = false;
1110
1111 // If it is used by something non-phi, then the other case is that
1112 // 'OpBlock' dominates all of its predecessors other than the
1113 // invoke. In this case, the invoke value can still be used.
Chris Lattner19591b32006-12-20 19:50:15 +00001114 if (Bad) {
1115 Bad = false;
Chris Lattnere3cbe032006-12-16 02:25:35 +00001116 for (pred_iterator PI = pred_begin(OpBlock),
1117 E = pred_end(OpBlock); PI != E; ++PI) {
Devang Patel2099ff02007-06-11 15:40:48 +00001118 if (*PI != II->getParent() && !DT->dominates(OpBlock, *PI)) {
Chris Lattnere3cbe032006-12-16 02:25:35 +00001119 Bad = true;
1120 break;
1121 }
1122 }
1123 }
Chris Lattner62d75e72006-12-20 21:20:13 +00001124 Assert2(!Bad,
1125 "Invoke value defined on critical edge but not dead!", &I,
1126 Op);
Chris Lattnere3cbe032006-12-16 02:25:35 +00001127 }
1128 } else if (OpBlock == BB) {
Chris Lattnere1f0cf12004-04-16 05:51:47 +00001129 // If they are in the same basic block, make sure that the definition
1130 // comes before the use.
Chris Lattnera7b1c7e2004-09-29 20:07:45 +00001131 Assert2(InstsInThisBlock.count(Op) ||
Devang Patel2099ff02007-06-11 15:40:48 +00001132 !DT->dominates(&BB->getParent()->getEntryBlock(), BB),
Chris Lattnere1f0cf12004-04-16 05:51:47 +00001133 "Instruction does not dominate all uses!", Op, &I);
1134 }
Chris Lattnerc9b07022004-01-14 05:42:52 +00001135
Chris Lattnerbede31f2003-10-05 17:44:18 +00001136 // Definition must dominate use unless use is unreachable!
Devang Patel2099ff02007-06-11 15:40:48 +00001137 Assert2(DT->dominates(OpBlock, BB) ||
1138 !DT->dominates(&BB->getParent()->getEntryBlock(), BB),
Chris Lattnerbede31f2003-10-05 17:44:18 +00001139 "Instruction does not dominate all uses!", Op, &I);
1140 } else {
1141 // PHI nodes are more difficult than other nodes because they actually
1142 // "use" the value in the predecessor basic blocks they correspond to.
1143 BasicBlock *PredBB = cast<BasicBlock>(I.getOperand(i+1));
Devang Patel2099ff02007-06-11 15:40:48 +00001144 Assert2(DT->dominates(OpBlock, PredBB) ||
1145 !DT->dominates(&BB->getParent()->getEntryBlock(), PredBB),
Chris Lattnerbede31f2003-10-05 17:44:18 +00001146 "Instruction does not dominate all uses!", Op, &I);
1147 }
Chris Lattner3188b732006-01-26 00:08:45 +00001148 } else if (isa<InlineAsm>(I.getOperand(i))) {
Duncan Sandsfd7b3262007-12-17 18:08:19 +00001149 Assert1(i == 0 && (isa<CallInst>(I) || isa<InvokeInst>(I)),
Chris Lattner3188b732006-01-26 00:08:45 +00001150 "Cannot take the address of an inline asm!", &I);
Chris Lattnerbede31f2003-10-05 17:44:18 +00001151 }
1152 }
Chris Lattnera7b1c7e2004-09-29 20:07:45 +00001153 InstsInThisBlock.insert(&I);
Chris Lattnerdd035d12003-05-08 03:47:33 +00001154}
1155
1156/// visitIntrinsicFunction - Allow intrinsics to be verified in different ways.
Misha Brukmanab5c6002004-03-02 00:22:19 +00001157///
Brian Gaeked0fde302003-11-11 22:41:34 +00001158void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnerdd035d12003-05-08 03:47:33 +00001159 Function *IF = CI.getCalledFunction();
Chris Lattner19b6dcd2007-04-20 21:48:08 +00001160 Assert1(IF->isDeclaration(), "Intrinsic functions should never be defined!",
1161 IF);
Chris Lattner3b816b72006-03-09 22:06:04 +00001162
1163#define GET_INTRINSIC_VERIFIER
1164#include "llvm/Intrinsics.gen"
1165#undef GET_INTRINSIC_VERIFIER
Gordon Henriksen8c33da52007-09-17 20:30:04 +00001166
1167 switch (ID) {
1168 default:
1169 break;
Gordon Henriksene1433f22007-12-25 02:31:26 +00001170 case Intrinsic::gcroot:
1171 case Intrinsic::gcwrite:
Gordon Henriksen27acd3a2007-12-25 02:02:10 +00001172 case Intrinsic::gcread: {
1173 Type *PtrTy = PointerType::getUnqual(Type::Int8Ty),
1174 *PtrPtrTy = PointerType::getUnqual(PtrTy);
Gordon Henriksene1433f22007-12-25 02:31:26 +00001175
1176 switch (ID) {
1177 default:
1178 break;
1179 case Intrinsic::gcroot:
1180 Assert1(CI.getOperand(1)->getType() == PtrPtrTy,
1181 "Intrinsic parameter #1 is not i8**.", &CI);
1182 Assert1(CI.getOperand(2)->getType() == PtrTy,
1183 "Intrinsic parameter #2 is not i8*.", &CI);
1184 Assert1(isa<AllocaInst>(
1185 IntrinsicInst::StripPointerCasts(CI.getOperand(1))),
1186 "llvm.gcroot parameter #1 must be an alloca.", &CI);
1187 Assert1(isa<Constant>(CI.getOperand(2)),
1188 "llvm.gcroot parameter #2 must be a constant.", &CI);
1189 break;
1190 case Intrinsic::gcwrite:
1191 Assert1(CI.getOperand(1)->getType() == PtrTy,
1192 "Intrinsic parameter #1 is not a i8*.", &CI);
1193 Assert1(CI.getOperand(2)->getType() == PtrTy,
1194 "Intrinsic parameter #2 is not a i8*.", &CI);
1195 Assert1(CI.getOperand(3)->getType() == PtrPtrTy,
1196 "Intrinsic parameter #3 is not a i8**.", &CI);
1197 break;
1198 case Intrinsic::gcread:
1199 Assert1(CI.getOperand(1)->getType() == PtrTy,
1200 "Intrinsic parameter #1 is not a i8*.", &CI);
1201 Assert1(CI.getOperand(2)->getType() == PtrPtrTy,
1202 "Intrinsic parameter #2 is not a i8**.", &CI);
1203 break;
1204 }
1205
1206 Assert1(CI.getParent()->getParent()->hasCollector(),
1207 "Enclosing function does not specify a collector algorithm.",
1208 &CI);
Gordon Henriksen27acd3a2007-12-25 02:02:10 +00001209 } break;
Duncan Sandsf51edad2007-09-29 16:25:54 +00001210 case Intrinsic::init_trampoline:
1211 Assert1(isa<Function>(IntrinsicInst::StripPointerCasts(CI.getOperand(2))),
1212 "llvm.init_trampoline parameter #2 must resolve to a function.",
1213 &CI);
Gordon Henriksen27acd3a2007-12-25 02:02:10 +00001214 break;
Gordon Henriksen8c33da52007-09-17 20:30:04 +00001215 }
Chris Lattnerd231fc32002-04-18 20:37:37 +00001216}
1217
Chris Lattner536a9d52006-03-31 04:46:47 +00001218/// VerifyIntrinsicPrototype - TableGen emits calls to this function into
1219/// Intrinsics.gen. This implements a little state machine that verifies the
1220/// prototype of intrinsics.
Chandler Carruth69940402007-08-04 01:51:18 +00001221void Verifier::VerifyIntrinsicPrototype(Intrinsic::ID ID,
1222 Function *F,
1223 unsigned Count, ...) {
Chris Lattner536a9d52006-03-31 04:46:47 +00001224 va_list VA;
Chandler Carruth69940402007-08-04 01:51:18 +00001225 va_start(VA, Count);
Chris Lattner536a9d52006-03-31 04:46:47 +00001226
1227 const FunctionType *FTy = F->getFunctionType();
1228
Reid Spencer559d77a2007-04-01 07:22:57 +00001229 // For overloaded intrinsics, the Suffix of the function name must match the
1230 // types of the arguments. This variable keeps track of the expected
1231 // suffix, to be checked at the end.
1232 std::string Suffix;
1233
Chandler Carruth69940402007-08-04 01:51:18 +00001234 if (FTy->getNumParams() + FTy->isVarArg() != Count - 1) {
1235 CheckFailed("Intrinsic prototype has incorrect number of arguments!", F);
1236 return;
1237 }
1238
Chris Lattner536a9d52006-03-31 04:46:47 +00001239 // Note that "arg#0" is the return type.
Chandler Carruth69940402007-08-04 01:51:18 +00001240 for (unsigned ArgNo = 0; ArgNo < Count; ++ArgNo) {
1241 MVT::ValueType VT = va_arg(VA, MVT::ValueType);
Chris Lattner536a9d52006-03-31 04:46:47 +00001242
Chandler Carruth69940402007-08-04 01:51:18 +00001243 if (VT == MVT::isVoid && ArgNo > 0) {
1244 if (!FTy->isVarArg())
1245 CheckFailed("Intrinsic prototype has no '...'!", F);
Jim Laskeyba4cc092007-02-06 18:02:54 +00001246 break;
1247 }
1248
Chris Lattner536a9d52006-03-31 04:46:47 +00001249 const Type *Ty;
Reid Spencer559d77a2007-04-01 07:22:57 +00001250 if (ArgNo == 0)
Chris Lattner536a9d52006-03-31 04:46:47 +00001251 Ty = FTy->getReturnType();
1252 else
1253 Ty = FTy->getParamType(ArgNo-1);
Chris Lattner536a9d52006-03-31 04:46:47 +00001254
Chandler Carruth69940402007-08-04 01:51:18 +00001255 unsigned NumElts = 0;
1256 const Type *EltTy = Ty;
1257 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
1258 EltTy = VTy->getElementType();
1259 NumElts = VTy->getNumElements();
1260 }
1261
1262 if ((int)VT < 0) {
1263 int Match = ~VT;
1264 if (Match == 0) {
1265 if (Ty != FTy->getReturnType()) {
1266 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " does not "
1267 "match return type.", F);
1268 break;
1269 }
1270 } else {
1271 if (Ty != FTy->getParamType(Match-1)) {
1272 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " does not "
1273 "match parameter %" + utostr(Match-1) + ".", F);
1274 break;
1275 }
Chris Lattner536a9d52006-03-31 04:46:47 +00001276 }
Chandler Carruth69940402007-08-04 01:51:18 +00001277 } else if (VT == MVT::iAny) {
Dan Gohmanfe5b4392007-08-16 22:06:45 +00001278 if (!EltTy->isInteger()) {
1279 if (ArgNo == 0)
1280 CheckFailed("Intrinsic result type is not "
1281 "an integer type.", F);
1282 else
1283 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is not "
1284 "an integer type.", F);
1285 break;
1286 }
Chandler Carruth69940402007-08-04 01:51:18 +00001287 unsigned GotBits = cast<IntegerType>(EltTy)->getBitWidth();
1288 Suffix += ".";
1289 if (EltTy != Ty)
1290 Suffix += "v" + utostr(NumElts);
1291 Suffix += "i" + utostr(GotBits);;
Reid Spencer559d77a2007-04-01 07:22:57 +00001292 // Check some constraints on various intrinsics.
1293 switch (ID) {
1294 default: break; // Not everything needs to be checked.
1295 case Intrinsic::bswap:
1296 if (GotBits < 16 || GotBits % 16 != 0)
1297 CheckFailed("Intrinsic requires even byte width argument", F);
Reid Spencer559d77a2007-04-01 07:22:57 +00001298 break;
1299 }
Dan Gohman0fee3ff2007-08-16 21:57:19 +00001300 } else if (VT == MVT::fAny) {
Dan Gohman0fee3ff2007-08-16 21:57:19 +00001301 if (!EltTy->isFloatingPoint()) {
1302 if (ArgNo == 0)
1303 CheckFailed("Intrinsic result type is not "
1304 "a floating-point type.", F);
1305 else
1306 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is not "
1307 "a floating-point type.", F);
1308 break;
1309 }
Dan Gohmanfe5b4392007-08-16 22:06:45 +00001310 Suffix += ".";
1311 if (EltTy != Ty)
1312 Suffix += "v" + utostr(NumElts);
1313 Suffix += MVT::getValueTypeString(MVT::getValueType(EltTy));
Chandler Carruth69940402007-08-04 01:51:18 +00001314 } else if (VT == MVT::iPTR) {
1315 if (!isa<PointerType>(Ty)) {
Dan Gohmanfe5b4392007-08-16 22:06:45 +00001316 if (ArgNo == 0)
1317 CheckFailed("Intrinsic result type is not a "
1318 "pointer and a pointer is required.", F);
1319 else
1320 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is not a "
1321 "pointer and a pointer is required.", F);
Chandler Carruth69940402007-08-04 01:51:18 +00001322 break;
1323 }
1324 } else if (MVT::isVector(VT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00001325 // If this is a vector argument, verify the number and type of elements.
Chandler Carruth69940402007-08-04 01:51:18 +00001326 if (MVT::getVectorElementType(VT) != MVT::getValueType(EltTy)) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001327 CheckFailed("Intrinsic prototype has incorrect vector element type!",
1328 F);
1329 break;
1330 }
Chandler Carruth69940402007-08-04 01:51:18 +00001331 if (MVT::getVectorNumElements(VT) != NumElts) {
Chris Lattner536a9d52006-03-31 04:46:47 +00001332 CheckFailed("Intrinsic prototype has incorrect number of "
1333 "vector elements!",F);
Chandler Carruth69940402007-08-04 01:51:18 +00001334 break;
Chris Lattner536a9d52006-03-31 04:46:47 +00001335 }
Chandler Carruth69940402007-08-04 01:51:18 +00001336 } else if (MVT::getTypeForValueType(VT) != EltTy) {
1337 if (ArgNo == 0)
1338 CheckFailed("Intrinsic prototype has incorrect result type!", F);
1339 else
1340 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is wrong!",F);
1341 break;
1342 } else if (EltTy != Ty) {
1343 if (ArgNo == 0)
1344 CheckFailed("Intrinsic result type is vector "
1345 "and a scalar is required.", F);
1346 else
1347 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is vector "
1348 "and a scalar is required.", F);
Chris Lattner536a9d52006-03-31 04:46:47 +00001349 }
1350 }
1351
1352 va_end(VA);
Reid Spencer559d77a2007-04-01 07:22:57 +00001353
1354 // If we computed a Suffix then the intrinsic is overloaded and we need to
1355 // make sure that the name of the function is correct. We add the suffix to
1356 // the name of the intrinsic and compare against the given function name. If
1357 // they are not the same, the function name is invalid. This ensures that
1358 // overloading of intrinsics uses a sane and consistent naming convention.
1359 if (!Suffix.empty()) {
1360 std::string Name(Intrinsic::getName(ID));
1361 if (Name + Suffix != F->getName())
1362 CheckFailed("Overloaded intrinsic has incorrect suffix: '" +
1363 F->getName().substr(Name.length()) + "'. It should be '" +
1364 Suffix + "'", F);
1365 }
Chris Lattner536a9d52006-03-31 04:46:47 +00001366}
1367
Chris Lattnerd231fc32002-04-18 20:37:37 +00001368
1369//===----------------------------------------------------------------------===//
1370// Implement the public interfaces to this file...
1371//===----------------------------------------------------------------------===//
1372
Chris Lattnerfdc38c42004-04-02 15:45:08 +00001373FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) {
1374 return new Verifier(action);
Chris Lattnerd231fc32002-04-18 20:37:37 +00001375}
1376
Chris Lattner9ce231f2002-08-02 17:37:08 +00001377
Misha Brukmanfd939082005-04-21 23:48:37 +00001378// verifyFunction - Create
Chris Lattnerfdc38c42004-04-02 15:45:08 +00001379bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) {
Chris Lattner2eff8592004-03-14 03:16:15 +00001380 Function &F = const_cast<Function&>(f);
Reid Spencer5cbf9852007-01-30 20:08:39 +00001381 assert(!F.isDeclaration() && "Cannot verify external functions");
Misha Brukmanfd939082005-04-21 23:48:37 +00001382
Chris Lattner2eff8592004-03-14 03:16:15 +00001383 FunctionPassManager FPM(new ExistingModuleProvider(F.getParent()));
Chris Lattnerfdc38c42004-04-02 15:45:08 +00001384 Verifier *V = new Verifier(action);
Chris Lattner2eff8592004-03-14 03:16:15 +00001385 FPM.add(V);
1386 FPM.run(F);
1387 return V->Broken;
Chris Lattner44d5bd92002-02-20 17:55:43 +00001388}
1389
Misha Brukmanab5c6002004-03-02 00:22:19 +00001390/// verifyModule - Check a module for errors, printing messages on stderr.
1391/// Return true if the module is corrupt.
1392///
Chris Lattner05ac92c2006-07-06 18:02:27 +00001393bool llvm::verifyModule(const Module &M, VerifierFailureAction action,
1394 std::string *ErrorInfo) {
Chris Lattner9ce231f2002-08-02 17:37:08 +00001395 PassManager PM;
Chris Lattnerfdc38c42004-04-02 15:45:08 +00001396 Verifier *V = new Verifier(action);
Chris Lattner9ce231f2002-08-02 17:37:08 +00001397 PM.add(V);
1398 PM.run((Module&)M);
Chris Lattner05ac92c2006-07-06 18:02:27 +00001399
1400 if (ErrorInfo && V->Broken)
1401 *ErrorInfo = V->msgs.str();
Chris Lattner9ce231f2002-08-02 17:37:08 +00001402 return V->Broken;
Chris Lattner00950542001-06-06 20:29:01 +00001403}
Reid Spenceraf90b0d2004-05-25 08:53:29 +00001404
1405// vim: sw=2