blob: 9f4a6c894d922a0d83498dd44489530f94c42ee3 [file] [log] [blame]
Chris Lattner2f7c9632001-06-06 20:29:01 +00001//===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner2f7c9632001-06-06 20:29:01 +00009//
Chris Lattner3e6e3e62002-03-29 19:06:18 +000010// This file defines the function verifier interface, that can be used for some
Chris Lattner2f7c9632001-06-06 20:29:01 +000011// sanity checking of input to the system.
12//
Misha Brukman1c9de462004-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 Lattner2f7c9632001-06-06 20:29:01 +000015//
Misha Brukman1c9de462004-06-24 21:47:35 +000016// * Both of a binary operator's parameters are of the same type
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000017// * Verify that the indices of mem access instructions match other operands
Misha Brukman1c9de462004-06-24 21:47:35 +000018// * Verify that arithmetic and other things are only performed on first-class
Chris Lattner8e72d6f2002-08-02 17:37:08 +000019// types. Verify that shifts & logicals only happen on integrals f.e.
Misha Brukman1c9de462004-06-24 21:47:35 +000020// * All of the constants in a switch statement are of the correct type
Chris Lattner8e72d6f2002-08-02 17:37:08 +000021// * The code is in valid SSA form
Misha Brukman1c9de462004-06-24 21:47:35 +000022// * It should be illegal to put a label into any other type (like a structure)
Chris Lattner2f7c9632001-06-06 20:29:01 +000023// or to return one. [except constant arrays!]
Chris Lattner7704e9f2002-03-14 16:53:48 +000024// * Only phi nodes can be self referential: 'add int %0, %0 ; <int>:0' is bad
Chris Lattnerd02f08d2002-02-20 17:55:43 +000025// * PHI nodes must have an entry for each predecessor, with no extras.
Chris Lattner069a7952002-06-25 15:56:27 +000026// * PHI nodes must be the first thing in a basic block, all grouped together
Chris Lattner4cd9df82002-10-06 21:00:31 +000027// * PHI nodes must have at least one entry
Chris Lattner069a7952002-06-25 15:56:27 +000028// * All basic blocks should only end with terminator insts, not contain them
Chris Lattner3e6e3e62002-03-29 19:06:18 +000029// * The entry node to a function must not have predecessors
Misha Brukmanfa100532003-10-10 17:54:14 +000030// * All Instructions must be embedded into a basic block
Misha Brukman1c9de462004-06-24 21:47:35 +000031// * Functions cannot take a void-typed parameter
Chris Lattneraf95e582002-04-13 22:48:46 +000032// * Verify that a function's argument list agrees with it's declared type.
Chris Lattnerfbf5be52002-03-15 20:25:09 +000033// * It is illegal to specify a name for a void value.
Misha Brukmanfa100532003-10-10 17:54:14 +000034// * It is illegal to have a internal global value with no initializer
Chris Lattner486302a2002-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 Lattner338a4622002-05-08 19:49:50 +000037// * Function call argument types match the function prototype
Chris Lattnerd46bb6e2002-04-24 19:12:21 +000038// * All other things that are tested by asserts spread about the code...
Chris Lattner2f7c9632001-06-06 20:29:01 +000039//
40//===----------------------------------------------------------------------===//
41
42#include "llvm/Analysis/Verifier.h"
Brian Gaeke9f479272003-11-16 23:07:42 +000043#include "llvm/Assembly/Writer.h"
Chris Lattner2ad5aa82005-05-08 22:27:09 +000044#include "llvm/CallingConv.h"
Chris Lattnerdc632112004-02-14 02:47:17 +000045#include "llvm/Constants.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000046#include "llvm/Pass.h"
Chris Lattner2f7c9632001-06-06 20:29:01 +000047#include "llvm/Module.h"
Chris Lattnerb870ca72004-03-14 03:16:15 +000048#include "llvm/ModuleProvider.h"
Chris Lattneraf95e582002-04-13 22:48:46 +000049#include "llvm/DerivedTypes.h"
Chris Lattnerdc632112004-02-14 02:47:17 +000050#include "llvm/Instructions.h"
Chris Lattnerbb346d02003-05-08 03:47:33 +000051#include "llvm/Intrinsics.h"
Chris Lattnerdc632112004-02-14 02:47:17 +000052#include "llvm/PassManager.h"
53#include "llvm/SymbolTable.h"
Chris Lattner8e72d6f2002-08-02 17:37:08 +000054#include "llvm/Analysis/Dominators.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000055#include "llvm/Support/CFG.h"
Chris Lattner0e851da2002-04-18 20:37:37 +000056#include "llvm/Support/InstVisitor.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000057#include "llvm/ADT/STLExtras.h"
Chris Lattnerd02f08d2002-02-20 17:55:43 +000058#include <algorithm>
Reid Spencer8baf8e22004-07-04 11:55:37 +000059#include <iostream>
Chris Lattnera45a2162004-04-02 15:45:08 +000060#include <sstream>
Chris Lattner189d19f2003-11-21 20:23:48 +000061using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000062
Chris Lattner0e851da2002-04-18 20:37:37 +000063namespace { // Anonymous namespace for class
Chris Lattner2f7c9632001-06-06 20:29:01 +000064
Chris Lattnerc8e66542002-04-27 06:56:12 +000065 struct Verifier : public FunctionPass, InstVisitor<Verifier> {
Chris Lattner8e72d6f2002-08-02 17:37:08 +000066 bool Broken; // Is this module found to be broken?
67 bool RealPass; // Are we not being run by a PassManager?
Chris Lattnera45a2162004-04-02 15:45:08 +000068 VerifierFailureAction action;
Reid Spencer47cf71a2004-05-25 08:53:29 +000069 // What to do if verification fails.
Misha Brukmanc566ca362004-03-02 00:22:19 +000070 Module *Mod; // Module we are verifying right now
Chris Lattnerdc09e402006-01-12 06:17:59 +000071 ETForest *EF; // ET-Forest, caution can be null!
Chris Lattnera45a2162004-04-02 15:45:08 +000072 std::stringstream msgs; // A stringstream to collect messages
Chris Lattner2f7c9632001-06-06 20:29:01 +000073
Chris Lattnerc9e79d02004-09-29 20:07:45 +000074 /// InstInThisBlock - when verifying a basic block, keep track of all of the
75 /// instructions we have seen so far. This allows us to do efficient
76 /// dominance checks for the case when an instruction has an operand that is
77 /// an instruction in the same block.
78 std::set<Instruction*> InstsInThisBlock;
79
Misha Brukmanb1c93172005-04-21 23:48:37 +000080 Verifier()
Reid Spencer47cf71a2004-05-25 08:53:29 +000081 : Broken(false), RealPass(true), action(AbortProcessAction),
Chris Lattnerdc09e402006-01-12 06:17:59 +000082 EF(0), msgs( std::ios::app | std::ios::out ) {}
Chris Lattnera45a2162004-04-02 15:45:08 +000083 Verifier( VerifierFailureAction ctn )
Chris Lattnerdc09e402006-01-12 06:17:59 +000084 : Broken(false), RealPass(true), action(ctn), EF(0),
Jeff Cohenc8f1f4b2005-01-22 17:36:17 +000085 msgs( std::ios::app | std::ios::out ) {}
Misha Brukmanb1c93172005-04-21 23:48:37 +000086 Verifier(bool AB )
87 : Broken(false), RealPass(true),
Chris Lattnerdc09e402006-01-12 06:17:59 +000088 action( AB ? AbortProcessAction : PrintMessageAction), EF(0),
Jeff Cohenc8f1f4b2005-01-22 17:36:17 +000089 msgs( std::ios::app | std::ios::out ) {}
Chris Lattnerdc09e402006-01-12 06:17:59 +000090 Verifier(ETForest &ef)
Chris Lattnera45a2162004-04-02 15:45:08 +000091 : Broken(false), RealPass(false), action(PrintMessageAction),
Chris Lattnerdc09e402006-01-12 06:17:59 +000092 EF(&ef), msgs( std::ios::app | std::ios::out ) {}
Chris Lattner8e72d6f2002-08-02 17:37:08 +000093
Chris Lattner2f7c9632001-06-06 20:29:01 +000094
Chris Lattner069a7952002-06-25 15:56:27 +000095 bool doInitialization(Module &M) {
Brian Gaeke9f479272003-11-16 23:07:42 +000096 Mod = &M;
Chris Lattner069a7952002-06-25 15:56:27 +000097 verifySymbolTable(M.getSymbolTable());
Chris Lattnera4503972002-09-19 16:12:19 +000098
99 // If this is a real pass, in a pass manager, we must abort before
100 // returning back to the pass manager, or else the pass manager may try to
101 // run other passes on the broken module.
Chris Lattnera4503972002-09-19 16:12:19 +0000102 if (RealPass)
103 abortIfBroken();
Chris Lattner0e851da2002-04-18 20:37:37 +0000104 return false;
105 }
106
Chris Lattner069a7952002-06-25 15:56:27 +0000107 bool runOnFunction(Function &F) {
Chris Lattner8e72d6f2002-08-02 17:37:08 +0000108 // Get dominator information if we are being run by PassManager
Chris Lattnerdc09e402006-01-12 06:17:59 +0000109 if (RealPass) EF = &getAnalysis<ETForest>();
Chris Lattner0e851da2002-04-18 20:37:37 +0000110 visit(F);
Chris Lattnerc9e79d02004-09-29 20:07:45 +0000111 InstsInThisBlock.clear();
Chris Lattnera4503972002-09-19 16:12:19 +0000112
113 // If this is a real pass, in a pass manager, we must abort before
114 // returning back to the pass manager, or else the pass manager may try to
115 // run other passes on the broken module.
Chris Lattnera4503972002-09-19 16:12:19 +0000116 if (RealPass)
117 abortIfBroken();
118
Chris Lattner0e851da2002-04-18 20:37:37 +0000119 return false;
120 }
121
Chris Lattner069a7952002-06-25 15:56:27 +0000122 bool doFinalization(Module &M) {
Chris Lattner9713b842002-04-28 16:04:26 +0000123 // Scan through, checking all of the external function's linkage now...
Chris Lattnerf75832b2004-06-03 06:38:43 +0000124 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner3ac483b2003-04-16 20:42:40 +0000125 visitGlobalValue(*I);
Chris Lattner9713b842002-04-28 16:04:26 +0000126
Chris Lattnerf75832b2004-06-03 06:38:43 +0000127 // Check to make sure function prototypes are okay.
128 if (I->isExternal()) visitFunction(*I);
129 }
130
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000131 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
132 I != E; ++I)
Chris Lattnere3400652004-12-15 20:23:49 +0000133 visitGlobalVariable(*I);
Chris Lattner78bc0fa2002-10-06 22:47:32 +0000134
Chris Lattnera4503972002-09-19 16:12:19 +0000135 // If the module is broken, abort at this time.
136 abortIfBroken();
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000137 return false;
138 }
139
Chris Lattnerf12cc842002-04-28 21:27:06 +0000140 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
141 AU.setPreservesAll();
Chris Lattner8e72d6f2002-08-02 17:37:08 +0000142 if (RealPass)
Robert Bocchinoca27f032006-01-17 20:07:22 +0000143 AU.addRequired<ETForest>();
Chris Lattnerf12cc842002-04-28 21:27:06 +0000144 }
145
Misha Brukmanc566ca362004-03-02 00:22:19 +0000146 /// abortIfBroken - If the module is broken and we are supposed to abort on
147 /// this condition, do so.
148 ///
Chris Lattnera45a2162004-04-02 15:45:08 +0000149 void abortIfBroken() {
150 if (Broken)
151 {
152 msgs << "Broken module found, ";
John Criswell987ad192004-05-04 21:46:05 +0000153 switch (action)
154 {
155 case AbortProcessAction:
156 msgs << "compilation aborted!\n";
157 std::cerr << msgs.str();
158 abort();
159 case ThrowExceptionAction:
160 msgs << "verification terminated.\n";
161 throw msgs.str();
162 case PrintMessageAction:
163 msgs << "verification continues.\n";
164 std::cerr << msgs.str();
165 break;
166 case ReturnStatusAction:
167 break;
168 }
Chris Lattnera4503972002-09-19 16:12:19 +0000169 }
170 }
171
Chris Lattner3ac483b2003-04-16 20:42:40 +0000172
Chris Lattner0e851da2002-04-18 20:37:37 +0000173 // Verification methods...
Chris Lattner98cf1f52002-11-20 18:36:02 +0000174 void verifySymbolTable(SymbolTable &ST);
Chris Lattner3ac483b2003-04-16 20:42:40 +0000175 void visitGlobalValue(GlobalValue &GV);
Chris Lattnere3400652004-12-15 20:23:49 +0000176 void visitGlobalVariable(GlobalVariable &GV);
Chris Lattner069a7952002-06-25 15:56:27 +0000177 void visitFunction(Function &F);
178 void visitBasicBlock(BasicBlock &BB);
179 void visitPHINode(PHINode &PN);
180 void visitBinaryOperator(BinaryOperator &B);
Chris Lattner1f419252002-09-09 20:26:04 +0000181 void visitShiftInst(ShiftInst &SI);
Robert Bocchino23004482006-01-10 19:05:34 +0000182 void visitExtractElementInst(ExtractElementInst &EI);
Robert Bocchinoca27f032006-01-17 20:07:22 +0000183 void visitInsertElementInst(InsertElementInst &EI);
Chris Lattner5b337482003-10-18 05:57:43 +0000184 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
Chris Lattner069a7952002-06-25 15:56:27 +0000185 void visitCallInst(CallInst &CI);
186 void visitGetElementPtrInst(GetElementPtrInst &GEP);
187 void visitLoadInst(LoadInst &LI);
188 void visitStoreInst(StoreInst &SI);
189 void visitInstruction(Instruction &I);
190 void visitTerminatorInst(TerminatorInst &I);
191 void visitReturnInst(ReturnInst &RI);
Chris Lattnerab5aa142004-05-21 16:47:21 +0000192 void visitSwitchInst(SwitchInst &SI);
Chris Lattner75648e72004-03-12 05:54:31 +0000193 void visitSelectInst(SelectInst &SI);
Chris Lattner903a25d2002-11-21 16:54:22 +0000194 void visitUserOp1(Instruction &I);
195 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
Brian Gaeke960707c2003-11-11 22:41:34 +0000196 void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI);
Chris Lattner0e851da2002-04-18 20:37:37 +0000197
Chris Lattner7e5e4562003-11-21 17:35:51 +0000198
199 void WriteValue(const Value *V) {
200 if (!V) return;
Chris Lattner189d19f2003-11-21 20:23:48 +0000201 if (isa<Instruction>(V)) {
Chris Lattnera45a2162004-04-02 15:45:08 +0000202 msgs << *V;
Chris Lattner189d19f2003-11-21 20:23:48 +0000203 } else {
Chris Lattnera45a2162004-04-02 15:45:08 +0000204 WriteAsOperand (msgs, V, true, true, Mod);
205 msgs << "\n";
Chris Lattner7e5e4562003-11-21 17:35:51 +0000206 }
207 }
208
Reid Spencer47cf71a2004-05-25 08:53:29 +0000209 void WriteType(const Type* T ) {
210 if ( !T ) return;
211 WriteTypeSymbolic(msgs, T, Mod );
212 }
213
Chris Lattner7e5e4562003-11-21 17:35:51 +0000214
Chris Lattner0e851da2002-04-18 20:37:37 +0000215 // CheckFailed - A check failed, so print out the condition and the message
216 // that failed. This provides a nice place to put a breakpoint if you want
217 // to see why something is not correct.
Chris Lattner7e5e4562003-11-21 17:35:51 +0000218 void CheckFailed(const std::string &Message,
219 const Value *V1 = 0, const Value *V2 = 0,
220 const Value *V3 = 0, const Value *V4 = 0) {
Chris Lattnera45a2162004-04-02 15:45:08 +0000221 msgs << Message << "\n";
Chris Lattner7e5e4562003-11-21 17:35:51 +0000222 WriteValue(V1);
223 WriteValue(V2);
224 WriteValue(V3);
225 WriteValue(V4);
Chris Lattner0e851da2002-04-18 20:37:37 +0000226 Broken = true;
227 }
Reid Spencer47cf71a2004-05-25 08:53:29 +0000228
Misha Brukmanb1c93172005-04-21 23:48:37 +0000229 void CheckFailed( const std::string& Message, const Value* V1,
Reid Spencer47cf71a2004-05-25 08:53:29 +0000230 const Type* T2, const Value* V3 = 0 ) {
231 msgs << Message << "\n";
232 WriteValue(V1);
233 WriteType(T2);
234 WriteValue(V3);
Reid Spencer41481392004-05-27 21:58:13 +0000235 Broken = true;
Reid Spencer47cf71a2004-05-25 08:53:29 +0000236 }
Chris Lattner0e851da2002-04-18 20:37:37 +0000237 };
Chris Lattner00fb26c2002-07-23 18:08:17 +0000238
Chris Lattneref901292003-11-13 19:47:29 +0000239 RegisterOpt<Verifier> X("verify", "Module Verifier");
Chris Lattner189d19f2003-11-21 20:23:48 +0000240} // End anonymous namespace
241
Chris Lattner2f7c9632001-06-06 20:29:01 +0000242
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000243// Assert - We know that cond should be true, if not print an error message.
244#define Assert(C, M) \
Chris Lattner412d2772002-04-28 16:06:24 +0000245 do { if (!(C)) { CheckFailed(M); return; } } while (0)
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000246#define Assert1(C, M, V1) \
Chris Lattner412d2772002-04-28 16:06:24 +0000247 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000248#define Assert2(C, M, V1, V2) \
Chris Lattner412d2772002-04-28 16:06:24 +0000249 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
Chris Lattner069a7952002-06-25 15:56:27 +0000250#define Assert3(C, M, V1, V2, V3) \
251 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
252#define Assert4(C, M, V1, V2, V3, V4) \
253 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000254
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000255
Chris Lattner3ac483b2003-04-16 20:42:40 +0000256void Verifier::visitGlobalValue(GlobalValue &GV) {
257 Assert1(!GV.isExternal() || GV.hasExternalLinkage(),
Chris Lattnerdcdc3712003-11-21 20:33:27 +0000258 "Global is external, but doesn't have external linkage!", &GV);
Chris Lattner3ac483b2003-04-16 20:42:40 +0000259 Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
260 "Only global variables can have appending linkage!", &GV);
261
262 if (GV.hasAppendingLinkage()) {
263 GlobalVariable &GVar = cast<GlobalVariable>(GV);
264 Assert1(isa<ArrayType>(GVar.getType()->getElementType()),
265 "Only global arrays can have appending linkage!", &GV);
266 }
267}
268
Chris Lattnere3400652004-12-15 20:23:49 +0000269void Verifier::visitGlobalVariable(GlobalVariable &GV) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000270 if (GV.hasInitializer())
Chris Lattnere3400652004-12-15 20:23:49 +0000271 Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(),
272 "Global variable initializer type does not match global "
273 "variable type!", &GV);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000274
Chris Lattnere3400652004-12-15 20:23:49 +0000275 visitGlobalValue(GV);
276}
277
278
Chris Lattneraf95e582002-04-13 22:48:46 +0000279// verifySymbolTable - Verify that a function or module symbol table is ok
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000280//
Chris Lattner98cf1f52002-11-20 18:36:02 +0000281void Verifier::verifySymbolTable(SymbolTable &ST) {
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000282
Reid Spencer47cf71a2004-05-25 08:53:29 +0000283 // Loop over all of the values in all type planes in the symbol table.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000284 for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
Reid Spencer47cf71a2004-05-25 08:53:29 +0000285 PE = ST.plane_end(); PI != PE; ++PI)
286 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
287 VE = PI->second.end(); VI != VE; ++VI) {
288 Value *V = VI->second;
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000289 // Check that there are no void typed values in the symbol table. Values
290 // with a void type cannot be put into symbol tables because they cannot
291 // have names!
292 Assert1(V->getType() != Type::VoidTy,
Reid Spencer47cf71a2004-05-25 08:53:29 +0000293 "Values with void type are not allowed to have names!", V);
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000294 }
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000295}
296
Chris Lattner0e851da2002-04-18 20:37:37 +0000297// visitFunction - Verify that a function is ok.
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000298//
Chris Lattner069a7952002-06-25 15:56:27 +0000299void Verifier::visitFunction(Function &F) {
Chris Lattner2ad5aa82005-05-08 22:27:09 +0000300 Assert1(!F.isVarArg() || F.getCallingConv() == CallingConv::C,
301 "Varargs functions must have C calling conventions!", &F);
302
303 // Check function arguments.
Chris Lattner069a7952002-06-25 15:56:27 +0000304 const FunctionType *FT = F.getFunctionType();
305 unsigned NumArgs = F.getArgumentList().size();
Chris Lattneraf95e582002-04-13 22:48:46 +0000306
Chris Lattner149376d2002-10-13 20:57:00 +0000307 Assert2(FT->getNumParams() == NumArgs,
Chris Lattneraf95e582002-04-13 22:48:46 +0000308 "# formal arguments must match # of arguments for function type!",
Chris Lattner069a7952002-06-25 15:56:27 +0000309 &F, FT);
Chris Lattner3ae303c2003-11-21 22:32:23 +0000310 Assert1(F.getReturnType()->isFirstClassType() ||
311 F.getReturnType() == Type::VoidTy,
312 "Functions cannot return aggregate values!", &F);
Chris Lattneraf95e582002-04-13 22:48:46 +0000313
314 // Check that the argument values match the function type for this function...
Chris Lattner149376d2002-10-13 20:57:00 +0000315 unsigned i = 0;
Chris Lattner531f9e92005-03-15 04:54:21 +0000316 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I, ++i) {
Chris Lattner149376d2002-10-13 20:57:00 +0000317 Assert2(I->getType() == FT->getParamType(i),
318 "Argument value does not match function argument type!",
319 I, FT->getParamType(i));
Chris Lattnerf75832b2004-06-03 06:38:43 +0000320 // Make sure no aggregates are passed by value.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000321 Assert1(I->getType()->isFirstClassType(),
Chris Lattnerf75832b2004-06-03 06:38:43 +0000322 "Functions cannot take aggregates as arguments by value!", I);
323 }
Chris Lattneraf95e582002-04-13 22:48:46 +0000324
Chris Lattner149376d2002-10-13 20:57:00 +0000325 if (!F.isExternal()) {
326 verifySymbolTable(F.getSymbolTable());
327
328 // Check the entry node
Chris Lattner5dac64f2003-09-20 14:39:18 +0000329 BasicBlock *Entry = &F.getEntryBlock();
Chris Lattner149376d2002-10-13 20:57:00 +0000330 Assert1(pred_begin(Entry) == pred_end(Entry),
331 "Entry block to function must not have predecessors!", Entry);
332 }
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000333}
334
335
Chris Lattner0e851da2002-04-18 20:37:37 +0000336// verifyBasicBlock - Verify that a basic block is well formed...
337//
Chris Lattner069a7952002-06-25 15:56:27 +0000338void Verifier::visitBasicBlock(BasicBlock &BB) {
Chris Lattnerc9e79d02004-09-29 20:07:45 +0000339 InstsInThisBlock.clear();
340
Alkis Evlogimenosbe526cf2004-12-04 02:30:42 +0000341 // Ensure that basic blocks have terminators!
342 Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
343
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000344 // Check constraints that this basic block imposes on all of the PHI nodes in
345 // it.
346 if (isa<PHINode>(BB.front())) {
347 std::vector<BasicBlock*> Preds(pred_begin(&BB), pred_end(&BB));
348 std::sort(Preds.begin(), Preds.end());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000349 PHINode *PN;
Chris Lattner307e1df2004-06-05 17:44:48 +0000350 for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) {
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000351
352 // Ensure that PHI nodes have at least one entry!
353 Assert1(PN->getNumIncomingValues() != 0,
354 "PHI nodes must have at least one entry. If the block is dead, "
355 "the PHI should be removed!", PN);
Brian Gaekee8a6bf32004-05-17 21:15:18 +0000356 Assert1(PN->getNumIncomingValues() == Preds.size(),
357 "PHINode should have one entry for each predecessor of its "
358 "parent basic block!", PN);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000359
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000360 // Get and sort all incoming values in the PHI node...
361 std::vector<std::pair<BasicBlock*, Value*> > Values;
362 Values.reserve(PN->getNumIncomingValues());
363 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
364 Values.push_back(std::make_pair(PN->getIncomingBlock(i),
365 PN->getIncomingValue(i)));
366 std::sort(Values.begin(), Values.end());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000367
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000368 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
369 // Check to make sure that if there is more than one entry for a
370 // particular basic block in this PHI node, that the incoming values are
371 // all identical.
372 //
373 Assert4(i == 0 || Values[i].first != Values[i-1].first ||
374 Values[i].second == Values[i-1].second,
375 "PHI node has multiple entries for the same basic block with "
376 "different incoming values!", PN, Values[i].first,
377 Values[i].second, Values[i-1].second);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000378
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000379 // Check to make sure that the predecessors and PHI node entries are
380 // matched up.
381 Assert3(Values[i].first == Preds[i],
382 "PHI node entries do not match predecessors!", PN,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000383 Values[i].first, Preds[i]);
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000384 }
385 }
386 }
Chris Lattner069a7952002-06-25 15:56:27 +0000387}
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000388
Chris Lattner069a7952002-06-25 15:56:27 +0000389void Verifier::visitTerminatorInst(TerminatorInst &I) {
390 // Ensure that terminators only exist at the end of the basic block.
391 Assert1(&I == I.getParent()->getTerminator(),
392 "Terminator found in the middle of a basic block!", I.getParent());
Chris Lattner7af3ee92002-07-18 00:13:42 +0000393 visitInstruction(I);
Chris Lattner069a7952002-06-25 15:56:27 +0000394}
395
396void Verifier::visitReturnInst(ReturnInst &RI) {
397 Function *F = RI.getParent()->getParent();
398 if (RI.getNumOperands() == 0)
Alkis Evlogimenosb92de192004-12-04 01:25:06 +0000399 Assert2(F->getReturnType() == Type::VoidTy,
400 "Found return instr that returns void in Function of non-void "
401 "return type!", &RI, F->getReturnType());
Chris Lattner069a7952002-06-25 15:56:27 +0000402 else
403 Assert2(F->getReturnType() == RI.getOperand(0)->getType(),
404 "Function return type does not match operand "
405 "type of return inst!", &RI, F->getReturnType());
406
Misha Brukman7eb05a12003-08-18 14:43:39 +0000407 // Check to make sure that the return value has necessary properties for
Chris Lattner069a7952002-06-25 15:56:27 +0000408 // terminators...
409 visitTerminatorInst(RI);
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000410}
411
Chris Lattnerab5aa142004-05-21 16:47:21 +0000412void Verifier::visitSwitchInst(SwitchInst &SI) {
413 // Check to make sure that all of the constants in the switch instruction
414 // have the same type as the switched-on value.
415 const Type *SwitchTy = SI.getCondition()->getType();
416 for (unsigned i = 1, e = SI.getNumCases(); i != e; ++i)
417 Assert1(SI.getCaseValue(i)->getType() == SwitchTy,
418 "Switch constants must all be same type as switch value!", &SI);
419
420 visitTerminatorInst(SI);
421}
422
Chris Lattner75648e72004-03-12 05:54:31 +0000423void Verifier::visitSelectInst(SelectInst &SI) {
424 Assert1(SI.getCondition()->getType() == Type::BoolTy,
425 "Select condition type must be bool!", &SI);
426 Assert1(SI.getTrueValue()->getType() == SI.getFalseValue()->getType(),
427 "Select values must have identical types!", &SI);
428 Assert1(SI.getTrueValue()->getType() == SI.getType(),
429 "Select values must have same type as select instruction!", &SI);
Chris Lattnercde15fb2004-09-29 21:19:28 +0000430 visitInstruction(SI);
Chris Lattner75648e72004-03-12 05:54:31 +0000431}
432
433
Misha Brukmanc566ca362004-03-02 00:22:19 +0000434/// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
435/// a pass, if any exist, it's an error.
436///
Chris Lattner903a25d2002-11-21 16:54:22 +0000437void Verifier::visitUserOp1(Instruction &I) {
438 Assert1(0, "User-defined operators should not live outside of a pass!",
439 &I);
440}
Chris Lattner0e851da2002-04-18 20:37:37 +0000441
Misha Brukmanc566ca362004-03-02 00:22:19 +0000442/// visitPHINode - Ensure that a PHI node is well formed.
443///
Chris Lattner069a7952002-06-25 15:56:27 +0000444void Verifier::visitPHINode(PHINode &PN) {
445 // Ensure that the PHI nodes are all grouped together at the top of the block.
446 // This can be tested by checking whether the instruction before this is
Misha Brukmanfa100532003-10-10 17:54:14 +0000447 // either nonexistent (because this is begin()) or is a PHI node. If not,
Chris Lattner069a7952002-06-25 15:56:27 +0000448 // then there is some other instruction before a PHI.
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000449 Assert2(&PN.getParent()->front() == &PN || isa<PHINode>(PN.getPrev()),
Chris Lattner069a7952002-06-25 15:56:27 +0000450 "PHI nodes not grouped at top of basic block!",
451 &PN, PN.getParent());
452
Chris Lattner3b93c912003-11-12 07:13:37 +0000453 // Check that all of the operands of the PHI node have the same type as the
454 // result.
455 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
456 Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
457 "PHI node operands are not the same type as the result!", &PN);
458
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000459 // All other PHI node constraints are checked in the visitBasicBlock method.
Chris Lattner0e851da2002-04-18 20:37:37 +0000460
461 visitInstruction(PN);
462}
463
Chris Lattner069a7952002-06-25 15:56:27 +0000464void Verifier::visitCallInst(CallInst &CI) {
465 Assert1(isa<PointerType>(CI.getOperand(0)->getType()),
466 "Called function must be a pointer!", &CI);
467 const PointerType *FPTy = cast<PointerType>(CI.getOperand(0)->getType());
Chris Lattner21ea83b2002-04-18 22:11:52 +0000468 Assert1(isa<FunctionType>(FPTy->getElementType()),
Chris Lattner069a7952002-06-25 15:56:27 +0000469 "Called function is not pointer to function type!", &CI);
Chris Lattner338a4622002-05-08 19:49:50 +0000470
Chris Lattner069a7952002-06-25 15:56:27 +0000471 const FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
Chris Lattner338a4622002-05-08 19:49:50 +0000472
473 // Verify that the correct number of arguments are being passed
474 if (FTy->isVarArg())
Chris Lattner069a7952002-06-25 15:56:27 +0000475 Assert1(CI.getNumOperands()-1 >= FTy->getNumParams(),
476 "Called function requires more parameters than were provided!",&CI);
Chris Lattner338a4622002-05-08 19:49:50 +0000477 else
Chris Lattner069a7952002-06-25 15:56:27 +0000478 Assert1(CI.getNumOperands()-1 == FTy->getNumParams(),
479 "Incorrect number of arguments passed to called function!", &CI);
Chris Lattner338a4622002-05-08 19:49:50 +0000480
481 // Verify that all arguments to the call match the function type...
482 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Chris Lattnerd996e542004-02-24 22:06:07 +0000483 Assert3(CI.getOperand(i+1)->getType() == FTy->getParamType(i),
Chris Lattner338a4622002-05-08 19:49:50 +0000484 "Call parameter type does not match function signature!",
Chris Lattnerd996e542004-02-24 22:06:07 +0000485 CI.getOperand(i+1), FTy->getParamType(i), &CI);
Chris Lattner7af3ee92002-07-18 00:13:42 +0000486
Chris Lattnerbb346d02003-05-08 03:47:33 +0000487 if (Function *F = CI.getCalledFunction())
Brian Gaeke960707c2003-11-11 22:41:34 +0000488 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
Chris Lattnerbb346d02003-05-08 03:47:33 +0000489 visitIntrinsicFunctionCall(ID, CI);
490
Chris Lattner7af3ee92002-07-18 00:13:42 +0000491 visitInstruction(CI);
Chris Lattner21ea83b2002-04-18 22:11:52 +0000492}
Chris Lattner0e851da2002-04-18 20:37:37 +0000493
Misha Brukmanc566ca362004-03-02 00:22:19 +0000494/// visitBinaryOperator - Check that both arguments to the binary operator are
495/// of the same type!
496///
Chris Lattner069a7952002-06-25 15:56:27 +0000497void Verifier::visitBinaryOperator(BinaryOperator &B) {
Chris Lattner1f419252002-09-09 20:26:04 +0000498 Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
499 "Both operands to a binary operator are not of the same type!", &B);
Chris Lattner0e851da2002-04-18 20:37:37 +0000500
Chris Lattner1f419252002-09-09 20:26:04 +0000501 // Check that logical operators are only used with integral operands.
502 if (B.getOpcode() == Instruction::And || B.getOpcode() == Instruction::Or ||
503 B.getOpcode() == Instruction::Xor) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +0000504 Assert1(B.getType()->isIntegral() ||
505 (isa<PackedType>(B.getType()) &&
506 cast<PackedType>(B.getType())->getElementType()->isIntegral()),
Chris Lattner1f419252002-09-09 20:26:04 +0000507 "Logical operators only work with integral types!", &B);
508 Assert1(B.getType() == B.getOperand(0)->getType(),
509 "Logical operators must have same type for operands and result!",
510 &B);
511 } else if (isa<SetCondInst>(B)) {
512 // Check that setcc instructions return bool
513 Assert1(B.getType() == Type::BoolTy,
514 "setcc instructions must return boolean values!", &B);
515 } else {
516 // Arithmetic operators only work on integer or fp values
517 Assert1(B.getType() == B.getOperand(0)->getType(),
518 "Arithmetic operators must have same type for operands and result!",
519 &B);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000520 Assert1(B.getType()->isInteger() || B.getType()->isFloatingPoint() ||
Brian Gaeke02209042004-08-20 06:00:58 +0000521 isa<PackedType>(B.getType()),
522 "Arithmetic operators must have integer, fp, or packed type!", &B);
Chris Lattner1f419252002-09-09 20:26:04 +0000523 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000524
Chris Lattner0e851da2002-04-18 20:37:37 +0000525 visitInstruction(B);
526}
527
Chris Lattner1f419252002-09-09 20:26:04 +0000528void Verifier::visitShiftInst(ShiftInst &SI) {
529 Assert1(SI.getType()->isInteger(),
530 "Shift must return an integer result!", &SI);
531 Assert1(SI.getType() == SI.getOperand(0)->getType(),
532 "Shift return type must be same as first operand!", &SI);
533 Assert1(SI.getOperand(1)->getType() == Type::UByteTy,
534 "Second operand to shift must be ubyte type!", &SI);
535 visitInstruction(SI);
536}
537
Robert Bocchino23004482006-01-10 19:05:34 +0000538void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
539 Assert1(isa<PackedType>(EI.getOperand(0)->getType()),
540 "First operand to extractelement must be packed type!", &EI);
541 Assert1(EI.getOperand(1)->getType() == Type::UIntTy,
542 "Second operand to extractelement must be uint type!", &EI);
543 Assert1(EI.getType() ==
Robert Bocchinoca27f032006-01-17 20:07:22 +0000544 cast<PackedType>(EI.getOperand(0)->getType())->getElementType(),
545 "Extractelement return type must match "
546 "first operand element type!", &EI);
Robert Bocchino23004482006-01-10 19:05:34 +0000547 visitInstruction(EI);
548}
549
Robert Bocchinoca27f032006-01-17 20:07:22 +0000550void Verifier::visitInsertElementInst(InsertElementInst &IE) {
551 Assert1(isa<PackedType>(IE.getOperand(0)->getType()),
552 "First operand to insertelement must be packed type!", &IE);
553 Assert1(IE.getOperand(1)->getType() ==
554 cast<PackedType>(IE.getOperand(0)->getType())->getElementType(),
555 "Second operand to insertelement must match "
556 "first operand element type!", &IE);
557 Assert1(IE.getOperand(2)->getType() == Type::UIntTy,
558 "Third operand to insertelement must be uint type!", &IE);
559 visitInstruction(IE);
560}
561
Chris Lattner069a7952002-06-25 15:56:27 +0000562void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattnerdfb3a2c2002-08-22 23:37:20 +0000563 const Type *ElTy =
564 GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(),
565 std::vector<Value*>(GEP.idx_begin(), GEP.idx_end()), true);
Chris Lattner069a7952002-06-25 15:56:27 +0000566 Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
567 Assert2(PointerType::get(ElTy) == GEP.getType(),
568 "GEP is not of right type for indices!", &GEP, ElTy);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000569 visitInstruction(GEP);
570}
571
Chris Lattner069a7952002-06-25 15:56:27 +0000572void Verifier::visitLoadInst(LoadInst &LI) {
Chris Lattnercd709cb2002-08-22 22:49:05 +0000573 const Type *ElTy =
574 cast<PointerType>(LI.getOperand(0)->getType())->getElementType();
Chris Lattner069a7952002-06-25 15:56:27 +0000575 Assert2(ElTy == LI.getType(),
Chris Lattner9d72c2f2003-11-21 17:06:29 +0000576 "Load result type does not match pointer operand type!", &LI, ElTy);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000577 visitInstruction(LI);
578}
579
Chris Lattner069a7952002-06-25 15:56:27 +0000580void Verifier::visitStoreInst(StoreInst &SI) {
Chris Lattnercd709cb2002-08-22 22:49:05 +0000581 const Type *ElTy =
582 cast<PointerType>(SI.getOperand(1)->getType())->getElementType();
Chris Lattner069a7952002-06-25 15:56:27 +0000583 Assert2(ElTy == SI.getOperand(0)->getType(),
Chris Lattner9d72c2f2003-11-21 17:06:29 +0000584 "Stored value type does not match pointer operand type!", &SI, ElTy);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000585 visitInstruction(SI);
586}
587
Chris Lattner0e851da2002-04-18 20:37:37 +0000588
Misha Brukmanc566ca362004-03-02 00:22:19 +0000589/// verifyInstruction - Verify that an instruction is well formed.
590///
Chris Lattner069a7952002-06-25 15:56:27 +0000591void Verifier::visitInstruction(Instruction &I) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000592 BasicBlock *BB = I.getParent();
Chris Lattner1f419252002-09-09 20:26:04 +0000593 Assert1(BB, "Instruction not embedded in basic block!", &I);
Chris Lattner0e851da2002-04-18 20:37:37 +0000594
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000595 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
596 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
597 UI != UE; ++UI)
Chris Lattner37053702004-02-27 17:28:25 +0000598 Assert1(*UI != (User*)&I ||
Chris Lattnerdc09e402006-01-12 06:17:59 +0000599 !EF->dominates(&BB->getParent()->getEntryBlock(), BB),
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000600 "Only PHI nodes may reference their own value!", &I);
601 }
602
603 // Check that void typed values don't have names
604 Assert1(I.getType() != Type::VoidTy || !I.hasName(),
605 "Instruction has a name, but provides a void value!", &I);
606
Chris Lattner5f126b72004-03-29 00:29:36 +0000607 // Check that the return value of the instruction is either void or a legal
608 // value type.
609 Assert1(I.getType() == Type::VoidTy || I.getType()->isFirstClassType(),
610 "Instruction returns a non-scalar type!", &I);
611
Chris Lattner0e851da2002-04-18 20:37:37 +0000612 // Check that all uses of the instruction, if they are instructions
613 // themselves, actually have parent basic blocks. If the use is not an
614 // instruction, it is an error!
Chris Lattner069a7952002-06-25 15:56:27 +0000615 for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattner0e851da2002-04-18 20:37:37 +0000616 UI != UE; ++UI) {
617 Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!",
618 *UI);
Chris Lattner21ea83b2002-04-18 22:11:52 +0000619 Instruction *Used = cast<Instruction>(*UI);
620 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
Chris Lattner069a7952002-06-25 15:56:27 +0000621 " embeded in a basic block!", &I, Used);
Chris Lattner0e851da2002-04-18 20:37:37 +0000622 }
623
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000624 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
625 // Check to make sure that the "address of" an intrinsic function is never
626 // taken.
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000627 Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I);
Chris Lattner9ece94b2004-03-14 03:23:54 +0000628 if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
Chris Lattnerbb346d02003-05-08 03:47:33 +0000629 Assert1(!F->isIntrinsic() || (i == 0 && isa<CallInst>(I)),
630 "Cannot take the address of an intrinsic!", &I);
Chris Lattner9ece94b2004-03-14 03:23:54 +0000631 } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
632 Assert1(OpBB->getParent() == BB->getParent(),
633 "Referring to a basic block in another function!", &I);
634 } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
635 Assert1(OpArg->getParent() == BB->getParent(),
636 "Referring to an argument in another function!", &I);
637 } else if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) {
Chris Lattnerec5089a2004-01-14 04:25:59 +0000638 BasicBlock *OpBlock = Op->getParent();
Chris Lattnerec5089a2004-01-14 04:25:59 +0000639
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000640 // Check that a definition dominates all of its uses.
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000641 if (!isa<PHINode>(I)) {
Chris Lattner02062822004-01-14 05:42:52 +0000642 // Invoke results are only usable in the normal destination, not in the
643 // exceptional destination.
644 if (InvokeInst *II = dyn_cast<InvokeInst>(Op))
645 OpBlock = II->getNormalDest();
Chris Lattner0377e432004-04-16 05:51:47 +0000646 else if (OpBlock == BB) {
647 // If they are in the same basic block, make sure that the definition
648 // comes before the use.
Chris Lattnerc9e79d02004-09-29 20:07:45 +0000649 Assert2(InstsInThisBlock.count(Op) ||
Chris Lattnerdc09e402006-01-12 06:17:59 +0000650 !EF->dominates(&BB->getParent()->getEntryBlock(), BB),
Chris Lattner0377e432004-04-16 05:51:47 +0000651 "Instruction does not dominate all uses!", Op, &I);
652 }
Chris Lattner02062822004-01-14 05:42:52 +0000653
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000654 // Definition must dominate use unless use is unreachable!
Chris Lattnerdc09e402006-01-12 06:17:59 +0000655 Assert2(EF->dominates(OpBlock, BB) ||
656 !EF->dominates(&BB->getParent()->getEntryBlock(), BB),
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000657 "Instruction does not dominate all uses!", Op, &I);
658 } else {
659 // PHI nodes are more difficult than other nodes because they actually
660 // "use" the value in the predecessor basic blocks they correspond to.
661 BasicBlock *PredBB = cast<BasicBlock>(I.getOperand(i+1));
Chris Lattnerdc09e402006-01-12 06:17:59 +0000662 Assert2(EF->dominates(OpBlock, PredBB) ||
663 !EF->dominates(&BB->getParent()->getEntryBlock(), PredBB),
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000664 "Instruction does not dominate all uses!", Op, &I);
665 }
666 }
667 }
Chris Lattnerc9e79d02004-09-29 20:07:45 +0000668 InstsInThisBlock.insert(&I);
Chris Lattnerbb346d02003-05-08 03:47:33 +0000669}
670
671/// visitIntrinsicFunction - Allow intrinsics to be verified in different ways.
Misha Brukmanc566ca362004-03-02 00:22:19 +0000672///
Brian Gaeke960707c2003-11-11 22:41:34 +0000673void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnerbb346d02003-05-08 03:47:33 +0000674 Function *IF = CI.getCalledFunction();
675 const FunctionType *FT = IF->getFunctionType();
676 Assert1(IF->isExternal(), "Intrinsic functions should never be defined!", IF);
Chris Lattnerd295d992003-06-05 21:01:26 +0000677 unsigned NumArgs = 0;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000678
Chris Lattnerade94102003-08-24 05:30:29 +0000679 // FIXME: this should check the return type of each intrinsic as well, also
680 // arguments!
Chris Lattnerbb346d02003-05-08 03:47:33 +0000681 switch (ID) {
Chris Lattner071a5e52004-03-13 00:24:00 +0000682 case Intrinsic::vastart:
Chris Lattnerbad4b4a2003-05-08 15:55:31 +0000683 Assert1(CI.getParent()->getParent()->getFunctionType()->isVarArg(),
684 "llvm.va_start intrinsic may only occur in function with variable"
685 " args!", &CI);
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000686 NumArgs = 1;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000687 break;
Chris Lattner071a5e52004-03-13 00:24:00 +0000688 case Intrinsic::vaend: NumArgs = 1; break;
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000689 case Intrinsic::vacopy: NumArgs = 2; break;
Chris Lattnerade94102003-08-24 05:30:29 +0000690
Chris Lattnerdc632112004-02-14 02:47:17 +0000691 case Intrinsic::returnaddress:
692 case Intrinsic::frameaddress:
693 Assert1(isa<PointerType>(FT->getReturnType()),
694 "llvm.(frame|return)address must return pointers", IF);
695 Assert1(FT->getNumParams() == 1 && isa<ConstantInt>(CI.getOperand(1)),
696 "llvm.(frame|return)address require a single constant integer argument",
697 &CI);
698 NumArgs = 1;
699 break;
700
John Criswell52010042004-04-08 20:27:38 +0000701 // Verify that read and write port have integral parameters of the correct
702 // signed-ness.
703 case Intrinsic::writeport:
704 Assert1(FT->getNumParams() == 2,
705 "Illegal # arguments for intrinsic function!", IF);
John Criswell2b4c96e2004-04-09 19:09:14 +0000706 Assert1(FT->getParamType(0)->isIntegral(),
John Criswell52010042004-04-08 20:27:38 +0000707 "First argument not unsigned int!", IF);
John Criswell2b4c96e2004-04-09 19:09:14 +0000708 Assert1(FT->getParamType(1)->isUnsigned(),
John Criswell52010042004-04-08 20:27:38 +0000709 "First argument not unsigned int!", IF);
710 NumArgs = 2;
711 break;
712
John Criswell23c48d62004-04-14 13:46:52 +0000713 case Intrinsic::writeio:
714 Assert1(FT->getNumParams() == 2,
715 "Illegal # arguments for intrinsic function!", IF);
716 Assert1(FT->getParamType(0)->isFirstClassType(),
717 "First argument not a first class type!", IF);
Chris Lattner395e2192004-06-17 17:56:54 +0000718 Assert1(isa<PointerType>(FT->getParamType(1)),
John Criswell23c48d62004-04-14 13:46:52 +0000719 "Second argument not a pointer!", IF);
720 NumArgs = 2;
721 break;
722
John Criswell52010042004-04-08 20:27:38 +0000723 case Intrinsic::readport:
724 Assert1(FT->getNumParams() == 1,
725 "Illegal # arguments for intrinsic function!", IF);
John Criswell23c48d62004-04-14 13:46:52 +0000726 Assert1(FT->getReturnType()->isFirstClassType(),
727 "Return type is not a first class type!", IF);
John Criswell52010042004-04-08 20:27:38 +0000728 Assert1(FT->getParamType(0)->isUnsigned(),
729 "First argument not unsigned int!", IF);
730 NumArgs = 1;
731 break;
732
Chris Lattner9c251eb2004-05-23 21:16:51 +0000733 case Intrinsic::readio: {
734 const PointerType *ParamType = dyn_cast<PointerType>(FT->getParamType(0));
735 const Type *ReturnType = FT->getReturnType();
John Criswell0c654c62004-04-14 14:49:36 +0000736
John Criswell23c48d62004-04-14 13:46:52 +0000737 Assert1(FT->getNumParams() == 1,
738 "Illegal # arguments for intrinsic function!", IF);
Chris Lattner9c251eb2004-05-23 21:16:51 +0000739 Assert1(ParamType, "First argument not a pointer!", IF);
740 Assert1(ParamType->getElementType() == ReturnType,
John Criswellc4e72c92004-04-14 15:06:48 +0000741 "Pointer type doesn't match return type!", IF);
John Criswell23c48d62004-04-14 13:46:52 +0000742 NumArgs = 1;
743 break;
John Criswell0c654c62004-04-14 14:49:36 +0000744 }
John Criswell23c48d62004-04-14 13:46:52 +0000745
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000746 case Intrinsic::isunordered_f32:
Alkis Evlogimenoscf9f8f12004-06-13 00:55:26 +0000747 Assert1(FT->getNumParams() == 2,
748 "Illegal # arguments for intrinsic function!", IF);
Alkis Evlogimenos9d740622004-06-12 19:19:14 +0000749 Assert1(FT->getReturnType() == Type::BoolTy,
Alkis Evlogimenoscf9f8f12004-06-13 00:55:26 +0000750 "Return type is not bool!", IF);
751 Assert1(FT->getParamType(0) == FT->getParamType(1),
752 "Arguments must be of the same type!", IF);
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000753 Assert1(FT->getParamType(0) == Type::FloatTy,
754 "Arguments must be a 32-bit floating point type!", IF);
755 NumArgs = 2;
756 break;
757
758 case Intrinsic::isunordered_f64:
759 Assert1(FT->getNumParams() == 2,
760 "Illegal # arguments for intrinsic function!", IF);
761 Assert1(FT->getReturnType() == Type::BoolTy,
762 "Return type is not bool!", IF);
763 Assert1(FT->getParamType(0) == FT->getParamType(1),
764 "Arguments must be of the same type!", IF);
765 Assert1(FT->getParamType(0) == Type::DoubleTy,
766 "Argument is not a 64-bit floating point type!", IF);
Alkis Evlogimenos9d740622004-06-12 19:19:14 +0000767 NumArgs = 2;
768 break;
769
Andrew Lenharth01aa5632005-11-11 16:47:30 +0000770 case Intrinsic::readcyclecounter:
771 Assert1(FT->getNumParams() == 0,
772 "Illegal # arguments for intrinsic function!", IF);
773 Assert1(FT->getReturnType() == Type::ULongTy,
774 "Return type is not ulong!", IF);
775 NumArgs = 0;
776 break;
777
Nate Begeman82049eb2006-01-14 01:25:24 +0000778 case Intrinsic::bswap_i16:
779 Assert1(FT->getNumParams() == 1,
780 "Illegal # arguments for intrinsic function!", IF);
781 Assert1(FT->getReturnType() == FT->getParamType(0),
782 "Return type does not match source type", IF);
783 Assert1(FT->getReturnType() == Type::UShortTy,
784 "Return type is not ushort!", IF);
785 NumArgs = 1;
786 break;
787
788 case Intrinsic::bswap_i32:
789 Assert1(FT->getNumParams() == 1,
790 "Illegal # arguments for intrinsic function!", IF);
791 Assert1(FT->getReturnType() == FT->getParamType(0),
792 "Return type does not match source type", IF);
793 Assert1(FT->getReturnType() == Type::UIntTy,
794 "Return type is not uint!", IF);
795 NumArgs = 1;
796 break;
797
798 case Intrinsic::bswap_i64:
799 Assert1(FT->getNumParams() == 1,
800 "Illegal # arguments for intrinsic function!", IF);
801 Assert1(FT->getReturnType() == FT->getParamType(0),
802 "Return type does not match source type", IF);
803 Assert1(FT->getReturnType() == Type::ULongTy,
804 "Return type is not ulong!", IF);
805 NumArgs = 1;
806 break;
807
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000808 case Intrinsic::ctpop_i8:
Andrew Lenharth5e177822005-05-03 17:19:30 +0000809 Assert1(FT->getNumParams() == 1,
810 "Illegal # arguments for intrinsic function!", IF);
811 Assert1(FT->getReturnType() == FT->getParamType(0),
812 "Return type does not match source type", IF);
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000813 Assert1(FT->getParamType(0) == Type::UByteTy
814 || FT->getParamType(0) == Type::SByteTy,
815 "Argument must be a byte type!", IF);
Andrew Lenharth5e177822005-05-03 17:19:30 +0000816 NumArgs = 1;
817 break;
818
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000819 case Intrinsic::ctpop_i16:
Chris Lattner1c636f12005-04-30 03:44:07 +0000820 Assert1(FT->getNumParams() == 1,
821 "Illegal # arguments for intrinsic function!", IF);
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000822 Assert1(FT->getReturnType() == FT->getParamType(0),
823 "Return type does not match source type", IF);
824 Assert1(FT->getParamType(0) == Type::UShortTy
825 || FT->getParamType(0) == Type::ShortTy,
826 "Argument must be a short type!", IF);
827 NumArgs = 1;
828 break;
829
830 case Intrinsic::ctpop_i32:
831 Assert1(FT->getNumParams() == 1,
832 "Illegal # arguments for intrinsic function!", IF);
833 Assert1(FT->getReturnType() == FT->getParamType(0),
834 "Return type does not match source type", IF);
835 Assert1(FT->getParamType(0) == Type::UIntTy
836 || FT->getParamType(0) == Type::IntTy,
837 "Argument must be an int type!", IF);
838 NumArgs = 1;
839 break;
840
841 case Intrinsic::ctpop_i64:
842 Assert1(FT->getNumParams() == 1,
843 "Illegal # arguments for intrinsic function!", IF);
844 Assert1(FT->getReturnType() == FT->getParamType(0),
845 "Return type does not match source type", IF);
846 Assert1(FT->getParamType(0) == Type::ULongTy
847 || FT->getParamType(0) == Type::LongTy,
848 "Argument must be a long type!", IF);
849 NumArgs = 1;
850 break;
851
852 case Intrinsic::ctlz_i8:
853 Assert1(FT->getNumParams() == 1,
854 "Illegal # arguments for intrinsic function!", IF);
855 Assert1(FT->getReturnType() == FT->getParamType(0),
856 "Return type does not match source type", IF);
857 Assert1(FT->getParamType(0) == Type::UByteTy
858 || FT->getParamType(0) == Type::SByteTy,
859 "Argument must be a byte type!", IF);
860 NumArgs = 1;
861 break;
862
863 case Intrinsic::ctlz_i16:
864 Assert1(FT->getNumParams() == 1,
865 "Illegal # arguments for intrinsic function!", IF);
866 Assert1(FT->getReturnType() == FT->getParamType(0),
867 "Return type does not match source type", IF);
868 Assert1(FT->getParamType(0) == Type::UShortTy
869 || FT->getParamType(0) == Type::ShortTy,
870 "Argument must be a short type!", IF);
871 NumArgs = 1;
872 break;
873 case Intrinsic::ctlz_i32:
874 Assert1(FT->getNumParams() == 1,
875 "Illegal # arguments for intrinsic function!", IF);
876 Assert1(FT->getReturnType() == FT->getParamType(0),
877 "Return type does not match source type", IF);
878 Assert1(FT->getParamType(0) == Type::UIntTy
879 || FT->getParamType(0) == Type::IntTy,
880 "Argument must be an int type!", IF);
881 NumArgs = 1;
882 break;
883 case Intrinsic::ctlz_i64:
884 Assert1(FT->getNumParams() == 1,
885 "Illegal # arguments for intrinsic function!", IF);
886 Assert1(FT->getReturnType() == FT->getParamType(0),
887 "Return type does not match source type", IF);
888 Assert1(FT->getParamType(0) == Type::ULongTy
889 || FT->getParamType(0) == Type::LongTy,
890 "Argument must be a long type!", IF);
891 NumArgs = 1;
892 break;
893 case Intrinsic::cttz_i8:
894 Assert1(FT->getNumParams() == 1,
895 "Illegal # arguments for intrinsic function!", IF);
896 Assert1(FT->getReturnType() == FT->getParamType(0),
897 "Return type does not match source type", IF);
898 Assert1(FT->getParamType(0) == Type::UByteTy
899 || FT->getParamType(0) == Type::SByteTy,
900 "Argument must be a byte type!", IF);
901 NumArgs = 1;
902 break;
903 case Intrinsic::cttz_i16:
904 Assert1(FT->getNumParams() == 1,
905 "Illegal # arguments for intrinsic function!", IF);
906 Assert1(FT->getReturnType() == FT->getParamType(0),
907 "Return type does not match source type", IF);
908 Assert1(FT->getParamType(0) == Type::UShortTy
909 || FT->getParamType(0) == Type::ShortTy,
910 "Argument must be a short type!", IF);
911 NumArgs = 1;
912 break;
913 case Intrinsic::cttz_i32:
914 Assert1(FT->getNumParams() == 1,
915 "Illegal # arguments for intrinsic function!", IF);
916 Assert1(FT->getReturnType() == FT->getParamType(0),
917 "Return type does not match source type", IF);
918 Assert1(FT->getParamType(0) == Type::UIntTy
919 || FT->getParamType(0) == Type::IntTy,
920 "Argument must be an int type!", IF);
921 NumArgs = 1;
922 break;
923 case Intrinsic::cttz_i64:
924 Assert1(FT->getNumParams() == 1,
925 "Illegal # arguments for intrinsic function!", IF);
926 Assert1(FT->getReturnType() == FT->getParamType(0),
927 "Return type does not match source type", IF);
928 Assert1(FT->getParamType(0) == Type::ULongTy
929 || FT->getParamType(0) == Type::LongTy,
930 "Argument must be a long type!", IF);
931 NumArgs = 1;
932 break;
933
934 case Intrinsic::sqrt_f32:
935 Assert1(FT->getNumParams() == 1,
936 "Illegal # arguments for intrinsic function!", IF);
937 Assert1(FT->getParamType(0) == Type::FloatTy,
938 "Argument is not a 32-bit floating point type!", IF);
939 Assert1(FT->getReturnType() == FT->getParamType(0),
940 "Return type is not the same as argument type!", IF);
941 NumArgs = 1;
942 break;
943
944 case Intrinsic::sqrt_f64:
945 Assert1(FT->getNumParams() == 1,
946 "Illegal # arguments for intrinsic function!", IF);
947 Assert1(FT->getParamType(0) == Type::DoubleTy,
948 "Argument is not a 64-bit floating point type!", IF);
Chris Lattner1c636f12005-04-30 03:44:07 +0000949 Assert1(FT->getReturnType() == FT->getParamType(0),
950 "Return type is not the same as argument type!", IF);
951 NumArgs = 1;
952 break;
953
Brian Gaeke960707c2003-11-11 22:41:34 +0000954 case Intrinsic::setjmp: NumArgs = 1; break;
955 case Intrinsic::longjmp: NumArgs = 2; break;
956 case Intrinsic::sigsetjmp: NumArgs = 2; break;
957 case Intrinsic::siglongjmp: NumArgs = 2; break;
Chris Lattner3d903f02004-01-05 05:36:30 +0000958
Chris Lattner9c251eb2004-05-23 21:16:51 +0000959 case Intrinsic::gcroot:
960 Assert1(FT->getNumParams() == 2,
961 "Illegal # arguments for intrinsic function!", IF);
Reid Spencer49fc8a72004-07-18 00:02:41 +0000962 Assert1(isa<Constant>(CI.getOperand(2)),
Chris Lattner9c251eb2004-05-23 21:16:51 +0000963 "Second argument to llvm.gcroot must be a constant!", &CI);
964 NumArgs = 2;
965 break;
Chris Lattner001aba72004-07-22 05:50:01 +0000966 case Intrinsic::gcread: NumArgs = 2; break;
967 case Intrinsic::gcwrite: NumArgs = 3; break;
Chris Lattner9c251eb2004-05-23 21:16:51 +0000968
Chris Lattner3d903f02004-01-05 05:36:30 +0000969 case Intrinsic::dbg_stoppoint: NumArgs = 4; break;
970 case Intrinsic::dbg_region_start:NumArgs = 1; break;
971 case Intrinsic::dbg_region_end: NumArgs = 1; break;
972 case Intrinsic::dbg_func_start: NumArgs = 1; break;
Chris Lattner59f1ef42004-01-06 05:33:02 +0000973 case Intrinsic::dbg_declare: NumArgs = 1; break;
Chris Lattner17d028d2004-02-12 17:01:09 +0000974
975 case Intrinsic::memcpy: NumArgs = 4; break;
Chris Lattner5ed171e2004-02-12 18:11:20 +0000976 case Intrinsic::memmove: NumArgs = 4; break;
Chris Lattnerdc632112004-02-14 02:47:17 +0000977 case Intrinsic::memset: NumArgs = 4; break;
Chris Lattner39637ef2005-02-28 19:27:42 +0000978
Chris Lattnerc482f162006-01-13 02:15:39 +0000979 case Intrinsic::stacksave:
980 NumArgs = 0;
981 Assert1(CI.getType() == PointerType::get(Type::SByteTy),
982 "llvm.stacksave must return an sbyte*", &CI);
983 break;
984 case Intrinsic::stackrestore:
985 NumArgs = 1;
986 Assert1(CI.getOperand(1)->getType() == PointerType::get(Type::SByteTy),
987 "llvm.stackrestore must take an sbyte*", &CI);
988 Assert1(CI.getType() == Type::VoidTy,
989 "llvm.stackrestore return void", &CI);
990 break;
Chris Lattner39637ef2005-02-28 19:27:42 +0000991 case Intrinsic::prefetch: NumArgs = 3; break;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000992 case Intrinsic::pcmarker:
993 NumArgs = 1;
Andrew Lenharthb4427912005-03-28 20:05:49 +0000994 Assert1(isa<Constant>(CI.getOperand(1)),
995 "First argument to llvm.pcmarker must be a constant!", &CI);
996 break;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000997
998 case Intrinsic::not_intrinsic:
Chris Lattnerbb346d02003-05-08 03:47:33 +0000999 assert(0 && "Invalid intrinsic!"); NumArgs = 0; break;
1000 }
1001
1002 Assert1(FT->getNumParams() == NumArgs || (FT->getNumParams() < NumArgs &&
1003 FT->isVarArg()),
1004 "Illegal # arguments for intrinsic function!", IF);
Chris Lattner0e851da2002-04-18 20:37:37 +00001005}
1006
1007
1008//===----------------------------------------------------------------------===//
1009// Implement the public interfaces to this file...
1010//===----------------------------------------------------------------------===//
1011
Chris Lattnera45a2162004-04-02 15:45:08 +00001012FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) {
1013 return new Verifier(action);
Chris Lattner0e851da2002-04-18 20:37:37 +00001014}
1015
Chris Lattner8e72d6f2002-08-02 17:37:08 +00001016
Misha Brukmanb1c93172005-04-21 23:48:37 +00001017// verifyFunction - Create
Chris Lattnera45a2162004-04-02 15:45:08 +00001018bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) {
Chris Lattnerb870ca72004-03-14 03:16:15 +00001019 Function &F = const_cast<Function&>(f);
Chris Lattner8e72d6f2002-08-02 17:37:08 +00001020 assert(!F.isExternal() && "Cannot verify external functions");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001021
Chris Lattnerb870ca72004-03-14 03:16:15 +00001022 FunctionPassManager FPM(new ExistingModuleProvider(F.getParent()));
Chris Lattnera45a2162004-04-02 15:45:08 +00001023 Verifier *V = new Verifier(action);
Chris Lattnerb870ca72004-03-14 03:16:15 +00001024 FPM.add(V);
1025 FPM.run(F);
1026 return V->Broken;
Chris Lattnerd02f08d2002-02-20 17:55:43 +00001027}
1028
Misha Brukmanc566ca362004-03-02 00:22:19 +00001029/// verifyModule - Check a module for errors, printing messages on stderr.
1030/// Return true if the module is corrupt.
1031///
Chris Lattnera45a2162004-04-02 15:45:08 +00001032bool llvm::verifyModule(const Module &M, VerifierFailureAction action) {
Chris Lattner8e72d6f2002-08-02 17:37:08 +00001033 PassManager PM;
Chris Lattnera45a2162004-04-02 15:45:08 +00001034 Verifier *V = new Verifier(action);
Chris Lattner8e72d6f2002-08-02 17:37:08 +00001035 PM.add(V);
1036 PM.run((Module&)M);
1037 return V->Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001038}
Reid Spencer47cf71a2004-05-25 08:53:29 +00001039
1040// vim: sw=2