blob: ed73341e25ecf8d3de298c9dbacc175d1da0280a [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)
Chris Lattnerdc09e402006-01-12 06:17:59 +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);
Chris Lattner5b337482003-10-18 05:57:43 +0000183 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
Chris Lattner069a7952002-06-25 15:56:27 +0000184 void visitCallInst(CallInst &CI);
185 void visitGetElementPtrInst(GetElementPtrInst &GEP);
186 void visitLoadInst(LoadInst &LI);
187 void visitStoreInst(StoreInst &SI);
188 void visitInstruction(Instruction &I);
189 void visitTerminatorInst(TerminatorInst &I);
190 void visitReturnInst(ReturnInst &RI);
Chris Lattnerab5aa142004-05-21 16:47:21 +0000191 void visitSwitchInst(SwitchInst &SI);
Chris Lattner75648e72004-03-12 05:54:31 +0000192 void visitSelectInst(SelectInst &SI);
Chris Lattner903a25d2002-11-21 16:54:22 +0000193 void visitUserOp1(Instruction &I);
194 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
Brian Gaeke960707c2003-11-11 22:41:34 +0000195 void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI);
Chris Lattner0e851da2002-04-18 20:37:37 +0000196
Chris Lattner7e5e4562003-11-21 17:35:51 +0000197
198 void WriteValue(const Value *V) {
199 if (!V) return;
Chris Lattner189d19f2003-11-21 20:23:48 +0000200 if (isa<Instruction>(V)) {
Chris Lattnera45a2162004-04-02 15:45:08 +0000201 msgs << *V;
Chris Lattner189d19f2003-11-21 20:23:48 +0000202 } else {
Chris Lattnera45a2162004-04-02 15:45:08 +0000203 WriteAsOperand (msgs, V, true, true, Mod);
204 msgs << "\n";
Chris Lattner7e5e4562003-11-21 17:35:51 +0000205 }
206 }
207
Reid Spencer47cf71a2004-05-25 08:53:29 +0000208 void WriteType(const Type* T ) {
209 if ( !T ) return;
210 WriteTypeSymbolic(msgs, T, Mod );
211 }
212
Chris Lattner7e5e4562003-11-21 17:35:51 +0000213
Chris Lattner0e851da2002-04-18 20:37:37 +0000214 // CheckFailed - A check failed, so print out the condition and the message
215 // that failed. This provides a nice place to put a breakpoint if you want
216 // to see why something is not correct.
Chris Lattner7e5e4562003-11-21 17:35:51 +0000217 void CheckFailed(const std::string &Message,
218 const Value *V1 = 0, const Value *V2 = 0,
219 const Value *V3 = 0, const Value *V4 = 0) {
Chris Lattnera45a2162004-04-02 15:45:08 +0000220 msgs << Message << "\n";
Chris Lattner7e5e4562003-11-21 17:35:51 +0000221 WriteValue(V1);
222 WriteValue(V2);
223 WriteValue(V3);
224 WriteValue(V4);
Chris Lattner0e851da2002-04-18 20:37:37 +0000225 Broken = true;
226 }
Reid Spencer47cf71a2004-05-25 08:53:29 +0000227
Misha Brukmanb1c93172005-04-21 23:48:37 +0000228 void CheckFailed( const std::string& Message, const Value* V1,
Reid Spencer47cf71a2004-05-25 08:53:29 +0000229 const Type* T2, const Value* V3 = 0 ) {
230 msgs << Message << "\n";
231 WriteValue(V1);
232 WriteType(T2);
233 WriteValue(V3);
Reid Spencer41481392004-05-27 21:58:13 +0000234 Broken = true;
Reid Spencer47cf71a2004-05-25 08:53:29 +0000235 }
Chris Lattner0e851da2002-04-18 20:37:37 +0000236 };
Chris Lattner00fb26c2002-07-23 18:08:17 +0000237
Chris Lattneref901292003-11-13 19:47:29 +0000238 RegisterOpt<Verifier> X("verify", "Module Verifier");
Chris Lattner189d19f2003-11-21 20:23:48 +0000239} // End anonymous namespace
240
Chris Lattner2f7c9632001-06-06 20:29:01 +0000241
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000242// Assert - We know that cond should be true, if not print an error message.
243#define Assert(C, M) \
Chris Lattner412d2772002-04-28 16:06:24 +0000244 do { if (!(C)) { CheckFailed(M); return; } } while (0)
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000245#define Assert1(C, M, V1) \
Chris Lattner412d2772002-04-28 16:06:24 +0000246 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000247#define Assert2(C, M, V1, V2) \
Chris Lattner412d2772002-04-28 16:06:24 +0000248 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
Chris Lattner069a7952002-06-25 15:56:27 +0000249#define Assert3(C, M, V1, V2, V3) \
250 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
251#define Assert4(C, M, V1, V2, V3, V4) \
252 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
Chris Lattner2f7c9632001-06-06 20:29:01 +0000253
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000254
Chris Lattner3ac483b2003-04-16 20:42:40 +0000255void Verifier::visitGlobalValue(GlobalValue &GV) {
256 Assert1(!GV.isExternal() || GV.hasExternalLinkage(),
Chris Lattnerdcdc3712003-11-21 20:33:27 +0000257 "Global is external, but doesn't have external linkage!", &GV);
Chris Lattner3ac483b2003-04-16 20:42:40 +0000258 Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
259 "Only global variables can have appending linkage!", &GV);
260
261 if (GV.hasAppendingLinkage()) {
262 GlobalVariable &GVar = cast<GlobalVariable>(GV);
263 Assert1(isa<ArrayType>(GVar.getType()->getElementType()),
264 "Only global arrays can have appending linkage!", &GV);
265 }
266}
267
Chris Lattnere3400652004-12-15 20:23:49 +0000268void Verifier::visitGlobalVariable(GlobalVariable &GV) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000269 if (GV.hasInitializer())
Chris Lattnere3400652004-12-15 20:23:49 +0000270 Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(),
271 "Global variable initializer type does not match global "
272 "variable type!", &GV);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000273
Chris Lattnere3400652004-12-15 20:23:49 +0000274 visitGlobalValue(GV);
275}
276
277
Chris Lattneraf95e582002-04-13 22:48:46 +0000278// verifySymbolTable - Verify that a function or module symbol table is ok
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000279//
Chris Lattner98cf1f52002-11-20 18:36:02 +0000280void Verifier::verifySymbolTable(SymbolTable &ST) {
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000281
Reid Spencer47cf71a2004-05-25 08:53:29 +0000282 // Loop over all of the values in all type planes in the symbol table.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000283 for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
Reid Spencer47cf71a2004-05-25 08:53:29 +0000284 PE = ST.plane_end(); PI != PE; ++PI)
285 for (SymbolTable::value_const_iterator VI = PI->second.begin(),
286 VE = PI->second.end(); VI != VE; ++VI) {
287 Value *V = VI->second;
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000288 // Check that there are no void typed values in the symbol table. Values
289 // with a void type cannot be put into symbol tables because they cannot
290 // have names!
291 Assert1(V->getType() != Type::VoidTy,
Reid Spencer47cf71a2004-05-25 08:53:29 +0000292 "Values with void type are not allowed to have names!", V);
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000293 }
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000294}
295
Chris Lattner0e851da2002-04-18 20:37:37 +0000296// visitFunction - Verify that a function is ok.
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000297//
Chris Lattner069a7952002-06-25 15:56:27 +0000298void Verifier::visitFunction(Function &F) {
Chris Lattner2ad5aa82005-05-08 22:27:09 +0000299 Assert1(!F.isVarArg() || F.getCallingConv() == CallingConv::C,
300 "Varargs functions must have C calling conventions!", &F);
301
302 // Check function arguments.
Chris Lattner069a7952002-06-25 15:56:27 +0000303 const FunctionType *FT = F.getFunctionType();
304 unsigned NumArgs = F.getArgumentList().size();
Chris Lattneraf95e582002-04-13 22:48:46 +0000305
Chris Lattner149376d2002-10-13 20:57:00 +0000306 Assert2(FT->getNumParams() == NumArgs,
Chris Lattneraf95e582002-04-13 22:48:46 +0000307 "# formal arguments must match # of arguments for function type!",
Chris Lattner069a7952002-06-25 15:56:27 +0000308 &F, FT);
Chris Lattner3ae303c2003-11-21 22:32:23 +0000309 Assert1(F.getReturnType()->isFirstClassType() ||
310 F.getReturnType() == Type::VoidTy,
311 "Functions cannot return aggregate values!", &F);
Chris Lattneraf95e582002-04-13 22:48:46 +0000312
313 // Check that the argument values match the function type for this function...
Chris Lattner149376d2002-10-13 20:57:00 +0000314 unsigned i = 0;
Chris Lattner531f9e92005-03-15 04:54:21 +0000315 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I, ++i) {
Chris Lattner149376d2002-10-13 20:57:00 +0000316 Assert2(I->getType() == FT->getParamType(i),
317 "Argument value does not match function argument type!",
318 I, FT->getParamType(i));
Chris Lattnerf75832b2004-06-03 06:38:43 +0000319 // Make sure no aggregates are passed by value.
Misha Brukmanb1c93172005-04-21 23:48:37 +0000320 Assert1(I->getType()->isFirstClassType(),
Chris Lattnerf75832b2004-06-03 06:38:43 +0000321 "Functions cannot take aggregates as arguments by value!", I);
322 }
Chris Lattneraf95e582002-04-13 22:48:46 +0000323
Chris Lattner149376d2002-10-13 20:57:00 +0000324 if (!F.isExternal()) {
325 verifySymbolTable(F.getSymbolTable());
326
327 // Check the entry node
Chris Lattner5dac64f2003-09-20 14:39:18 +0000328 BasicBlock *Entry = &F.getEntryBlock();
Chris Lattner149376d2002-10-13 20:57:00 +0000329 Assert1(pred_begin(Entry) == pred_end(Entry),
330 "Entry block to function must not have predecessors!", Entry);
331 }
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000332}
333
334
Chris Lattner0e851da2002-04-18 20:37:37 +0000335// verifyBasicBlock - Verify that a basic block is well formed...
336//
Chris Lattner069a7952002-06-25 15:56:27 +0000337void Verifier::visitBasicBlock(BasicBlock &BB) {
Chris Lattnerc9e79d02004-09-29 20:07:45 +0000338 InstsInThisBlock.clear();
339
Alkis Evlogimenosbe526cf2004-12-04 02:30:42 +0000340 // Ensure that basic blocks have terminators!
341 Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
342
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000343 // Check constraints that this basic block imposes on all of the PHI nodes in
344 // it.
345 if (isa<PHINode>(BB.front())) {
346 std::vector<BasicBlock*> Preds(pred_begin(&BB), pred_end(&BB));
347 std::sort(Preds.begin(), Preds.end());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000348 PHINode *PN;
Chris Lattner307e1df2004-06-05 17:44:48 +0000349 for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) {
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000350
351 // Ensure that PHI nodes have at least one entry!
352 Assert1(PN->getNumIncomingValues() != 0,
353 "PHI nodes must have at least one entry. If the block is dead, "
354 "the PHI should be removed!", PN);
Brian Gaekee8a6bf32004-05-17 21:15:18 +0000355 Assert1(PN->getNumIncomingValues() == Preds.size(),
356 "PHINode should have one entry for each predecessor of its "
357 "parent basic block!", PN);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000358
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000359 // Get and sort all incoming values in the PHI node...
360 std::vector<std::pair<BasicBlock*, Value*> > Values;
361 Values.reserve(PN->getNumIncomingValues());
362 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
363 Values.push_back(std::make_pair(PN->getIncomingBlock(i),
364 PN->getIncomingValue(i)));
365 std::sort(Values.begin(), Values.end());
Misha Brukmanb1c93172005-04-21 23:48:37 +0000366
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000367 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
368 // Check to make sure that if there is more than one entry for a
369 // particular basic block in this PHI node, that the incoming values are
370 // all identical.
371 //
372 Assert4(i == 0 || Values[i].first != Values[i-1].first ||
373 Values[i].second == Values[i-1].second,
374 "PHI node has multiple entries for the same basic block with "
375 "different incoming values!", PN, Values[i].first,
376 Values[i].second, Values[i-1].second);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000377
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000378 // Check to make sure that the predecessors and PHI node entries are
379 // matched up.
380 Assert3(Values[i].first == Preds[i],
381 "PHI node entries do not match predecessors!", PN,
Misha Brukmanb1c93172005-04-21 23:48:37 +0000382 Values[i].first, Preds[i]);
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000383 }
384 }
385 }
Chris Lattner069a7952002-06-25 15:56:27 +0000386}
Chris Lattnerfbf5be52002-03-15 20:25:09 +0000387
Chris Lattner069a7952002-06-25 15:56:27 +0000388void Verifier::visitTerminatorInst(TerminatorInst &I) {
389 // Ensure that terminators only exist at the end of the basic block.
390 Assert1(&I == I.getParent()->getTerminator(),
391 "Terminator found in the middle of a basic block!", I.getParent());
Chris Lattner7af3ee92002-07-18 00:13:42 +0000392 visitInstruction(I);
Chris Lattner069a7952002-06-25 15:56:27 +0000393}
394
395void Verifier::visitReturnInst(ReturnInst &RI) {
396 Function *F = RI.getParent()->getParent();
397 if (RI.getNumOperands() == 0)
Alkis Evlogimenosb92de192004-12-04 01:25:06 +0000398 Assert2(F->getReturnType() == Type::VoidTy,
399 "Found return instr that returns void in Function of non-void "
400 "return type!", &RI, F->getReturnType());
Chris Lattner069a7952002-06-25 15:56:27 +0000401 else
402 Assert2(F->getReturnType() == RI.getOperand(0)->getType(),
403 "Function return type does not match operand "
404 "type of return inst!", &RI, F->getReturnType());
405
Misha Brukman7eb05a12003-08-18 14:43:39 +0000406 // Check to make sure that the return value has necessary properties for
Chris Lattner069a7952002-06-25 15:56:27 +0000407 // terminators...
408 visitTerminatorInst(RI);
Chris Lattnerd02f08d2002-02-20 17:55:43 +0000409}
410
Chris Lattnerab5aa142004-05-21 16:47:21 +0000411void Verifier::visitSwitchInst(SwitchInst &SI) {
412 // Check to make sure that all of the constants in the switch instruction
413 // have the same type as the switched-on value.
414 const Type *SwitchTy = SI.getCondition()->getType();
415 for (unsigned i = 1, e = SI.getNumCases(); i != e; ++i)
416 Assert1(SI.getCaseValue(i)->getType() == SwitchTy,
417 "Switch constants must all be same type as switch value!", &SI);
418
419 visitTerminatorInst(SI);
420}
421
Chris Lattner75648e72004-03-12 05:54:31 +0000422void Verifier::visitSelectInst(SelectInst &SI) {
423 Assert1(SI.getCondition()->getType() == Type::BoolTy,
424 "Select condition type must be bool!", &SI);
425 Assert1(SI.getTrueValue()->getType() == SI.getFalseValue()->getType(),
426 "Select values must have identical types!", &SI);
427 Assert1(SI.getTrueValue()->getType() == SI.getType(),
428 "Select values must have same type as select instruction!", &SI);
Chris Lattnercde15fb2004-09-29 21:19:28 +0000429 visitInstruction(SI);
Chris Lattner75648e72004-03-12 05:54:31 +0000430}
431
432
Misha Brukmanc566ca362004-03-02 00:22:19 +0000433/// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
434/// a pass, if any exist, it's an error.
435///
Chris Lattner903a25d2002-11-21 16:54:22 +0000436void Verifier::visitUserOp1(Instruction &I) {
437 Assert1(0, "User-defined operators should not live outside of a pass!",
438 &I);
439}
Chris Lattner0e851da2002-04-18 20:37:37 +0000440
Misha Brukmanc566ca362004-03-02 00:22:19 +0000441/// visitPHINode - Ensure that a PHI node is well formed.
442///
Chris Lattner069a7952002-06-25 15:56:27 +0000443void Verifier::visitPHINode(PHINode &PN) {
444 // Ensure that the PHI nodes are all grouped together at the top of the block.
445 // This can be tested by checking whether the instruction before this is
Misha Brukmanfa100532003-10-10 17:54:14 +0000446 // either nonexistent (because this is begin()) or is a PHI node. If not,
Chris Lattner069a7952002-06-25 15:56:27 +0000447 // then there is some other instruction before a PHI.
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000448 Assert2(&PN.getParent()->front() == &PN || isa<PHINode>(PN.getPrev()),
Chris Lattner069a7952002-06-25 15:56:27 +0000449 "PHI nodes not grouped at top of basic block!",
450 &PN, PN.getParent());
451
Chris Lattner3b93c912003-11-12 07:13:37 +0000452 // Check that all of the operands of the PHI node have the same type as the
453 // result.
454 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
455 Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
456 "PHI node operands are not the same type as the result!", &PN);
457
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000458 // All other PHI node constraints are checked in the visitBasicBlock method.
Chris Lattner0e851da2002-04-18 20:37:37 +0000459
460 visitInstruction(PN);
461}
462
Chris Lattner069a7952002-06-25 15:56:27 +0000463void Verifier::visitCallInst(CallInst &CI) {
464 Assert1(isa<PointerType>(CI.getOperand(0)->getType()),
465 "Called function must be a pointer!", &CI);
466 const PointerType *FPTy = cast<PointerType>(CI.getOperand(0)->getType());
Chris Lattner21ea83b2002-04-18 22:11:52 +0000467 Assert1(isa<FunctionType>(FPTy->getElementType()),
Chris Lattner069a7952002-06-25 15:56:27 +0000468 "Called function is not pointer to function type!", &CI);
Chris Lattner338a4622002-05-08 19:49:50 +0000469
Chris Lattner069a7952002-06-25 15:56:27 +0000470 const FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
Chris Lattner338a4622002-05-08 19:49:50 +0000471
472 // Verify that the correct number of arguments are being passed
473 if (FTy->isVarArg())
Chris Lattner069a7952002-06-25 15:56:27 +0000474 Assert1(CI.getNumOperands()-1 >= FTy->getNumParams(),
475 "Called function requires more parameters than were provided!",&CI);
Chris Lattner338a4622002-05-08 19:49:50 +0000476 else
Chris Lattner069a7952002-06-25 15:56:27 +0000477 Assert1(CI.getNumOperands()-1 == FTy->getNumParams(),
478 "Incorrect number of arguments passed to called function!", &CI);
Chris Lattner338a4622002-05-08 19:49:50 +0000479
480 // Verify that all arguments to the call match the function type...
481 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Chris Lattnerd996e542004-02-24 22:06:07 +0000482 Assert3(CI.getOperand(i+1)->getType() == FTy->getParamType(i),
Chris Lattner338a4622002-05-08 19:49:50 +0000483 "Call parameter type does not match function signature!",
Chris Lattnerd996e542004-02-24 22:06:07 +0000484 CI.getOperand(i+1), FTy->getParamType(i), &CI);
Chris Lattner7af3ee92002-07-18 00:13:42 +0000485
Chris Lattnerbb346d02003-05-08 03:47:33 +0000486 if (Function *F = CI.getCalledFunction())
Brian Gaeke960707c2003-11-11 22:41:34 +0000487 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
Chris Lattnerbb346d02003-05-08 03:47:33 +0000488 visitIntrinsicFunctionCall(ID, CI);
489
Chris Lattner7af3ee92002-07-18 00:13:42 +0000490 visitInstruction(CI);
Chris Lattner21ea83b2002-04-18 22:11:52 +0000491}
Chris Lattner0e851da2002-04-18 20:37:37 +0000492
Misha Brukmanc566ca362004-03-02 00:22:19 +0000493/// visitBinaryOperator - Check that both arguments to the binary operator are
494/// of the same type!
495///
Chris Lattner069a7952002-06-25 15:56:27 +0000496void Verifier::visitBinaryOperator(BinaryOperator &B) {
Chris Lattner1f419252002-09-09 20:26:04 +0000497 Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
498 "Both operands to a binary operator are not of the same type!", &B);
Chris Lattner0e851da2002-04-18 20:37:37 +0000499
Chris Lattner1f419252002-09-09 20:26:04 +0000500 // Check that logical operators are only used with integral operands.
501 if (B.getOpcode() == Instruction::And || B.getOpcode() == Instruction::Or ||
502 B.getOpcode() == Instruction::Xor) {
Chris Lattnerdca56cb2005-12-21 18:22:19 +0000503 Assert1(B.getType()->isIntegral() ||
504 (isa<PackedType>(B.getType()) &&
505 cast<PackedType>(B.getType())->getElementType()->isIntegral()),
Chris Lattner1f419252002-09-09 20:26:04 +0000506 "Logical operators only work with integral types!", &B);
507 Assert1(B.getType() == B.getOperand(0)->getType(),
508 "Logical operators must have same type for operands and result!",
509 &B);
510 } else if (isa<SetCondInst>(B)) {
511 // Check that setcc instructions return bool
512 Assert1(B.getType() == Type::BoolTy,
513 "setcc instructions must return boolean values!", &B);
514 } else {
515 // Arithmetic operators only work on integer or fp values
516 Assert1(B.getType() == B.getOperand(0)->getType(),
517 "Arithmetic operators must have same type for operands and result!",
518 &B);
Misha Brukmanb1c93172005-04-21 23:48:37 +0000519 Assert1(B.getType()->isInteger() || B.getType()->isFloatingPoint() ||
Brian Gaeke02209042004-08-20 06:00:58 +0000520 isa<PackedType>(B.getType()),
521 "Arithmetic operators must have integer, fp, or packed type!", &B);
Chris Lattner1f419252002-09-09 20:26:04 +0000522 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000523
Chris Lattner0e851da2002-04-18 20:37:37 +0000524 visitInstruction(B);
525}
526
Chris Lattner1f419252002-09-09 20:26:04 +0000527void Verifier::visitShiftInst(ShiftInst &SI) {
528 Assert1(SI.getType()->isInteger(),
529 "Shift must return an integer result!", &SI);
530 Assert1(SI.getType() == SI.getOperand(0)->getType(),
531 "Shift return type must be same as first operand!", &SI);
532 Assert1(SI.getOperand(1)->getType() == Type::UByteTy,
533 "Second operand to shift must be ubyte type!", &SI);
534 visitInstruction(SI);
535}
536
Robert Bocchino23004482006-01-10 19:05:34 +0000537void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
538 Assert1(isa<PackedType>(EI.getOperand(0)->getType()),
539 "First operand to extractelement must be packed type!", &EI);
540 Assert1(EI.getOperand(1)->getType() == Type::UIntTy,
541 "Second operand to extractelement must be uint type!", &EI);
542 Assert1(EI.getType() ==
543 cast<PackedType>(EI.getOperand(0)->getType())->getElementType(),
544 "Extractelement return type must be same as "
545 "first operand element type!", &EI);
546 visitInstruction(EI);
547}
548
Chris Lattner069a7952002-06-25 15:56:27 +0000549void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattnerdfb3a2c2002-08-22 23:37:20 +0000550 const Type *ElTy =
551 GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(),
552 std::vector<Value*>(GEP.idx_begin(), GEP.idx_end()), true);
Chris Lattner069a7952002-06-25 15:56:27 +0000553 Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
554 Assert2(PointerType::get(ElTy) == GEP.getType(),
555 "GEP is not of right type for indices!", &GEP, ElTy);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000556 visitInstruction(GEP);
557}
558
Chris Lattner069a7952002-06-25 15:56:27 +0000559void Verifier::visitLoadInst(LoadInst &LI) {
Chris Lattnercd709cb2002-08-22 22:49:05 +0000560 const Type *ElTy =
561 cast<PointerType>(LI.getOperand(0)->getType())->getElementType();
Chris Lattner069a7952002-06-25 15:56:27 +0000562 Assert2(ElTy == LI.getType(),
Chris Lattner9d72c2f2003-11-21 17:06:29 +0000563 "Load result type does not match pointer operand type!", &LI, ElTy);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000564 visitInstruction(LI);
565}
566
Chris Lattner069a7952002-06-25 15:56:27 +0000567void Verifier::visitStoreInst(StoreInst &SI) {
Chris Lattnercd709cb2002-08-22 22:49:05 +0000568 const Type *ElTy =
569 cast<PointerType>(SI.getOperand(1)->getType())->getElementType();
Chris Lattner069a7952002-06-25 15:56:27 +0000570 Assert2(ElTy == SI.getOperand(0)->getType(),
Chris Lattner9d72c2f2003-11-21 17:06:29 +0000571 "Stored value type does not match pointer operand type!", &SI, ElTy);
Chris Lattnerd46bb6e2002-04-24 19:12:21 +0000572 visitInstruction(SI);
573}
574
Chris Lattner0e851da2002-04-18 20:37:37 +0000575
Misha Brukmanc566ca362004-03-02 00:22:19 +0000576/// verifyInstruction - Verify that an instruction is well formed.
577///
Chris Lattner069a7952002-06-25 15:56:27 +0000578void Verifier::visitInstruction(Instruction &I) {
Misha Brukmanb1c93172005-04-21 23:48:37 +0000579 BasicBlock *BB = I.getParent();
Chris Lattner1f419252002-09-09 20:26:04 +0000580 Assert1(BB, "Instruction not embedded in basic block!", &I);
Chris Lattner0e851da2002-04-18 20:37:37 +0000581
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000582 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
583 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
584 UI != UE; ++UI)
Chris Lattner37053702004-02-27 17:28:25 +0000585 Assert1(*UI != (User*)&I ||
Chris Lattnerdc09e402006-01-12 06:17:59 +0000586 !EF->dominates(&BB->getParent()->getEntryBlock(), BB),
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000587 "Only PHI nodes may reference their own value!", &I);
588 }
589
590 // Check that void typed values don't have names
591 Assert1(I.getType() != Type::VoidTy || !I.hasName(),
592 "Instruction has a name, but provides a void value!", &I);
593
Chris Lattner5f126b72004-03-29 00:29:36 +0000594 // Check that the return value of the instruction is either void or a legal
595 // value type.
596 Assert1(I.getType() == Type::VoidTy || I.getType()->isFirstClassType(),
597 "Instruction returns a non-scalar type!", &I);
598
Chris Lattner0e851da2002-04-18 20:37:37 +0000599 // Check that all uses of the instruction, if they are instructions
600 // themselves, actually have parent basic blocks. If the use is not an
601 // instruction, it is an error!
Chris Lattner069a7952002-06-25 15:56:27 +0000602 for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattner0e851da2002-04-18 20:37:37 +0000603 UI != UE; ++UI) {
604 Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!",
605 *UI);
Chris Lattner21ea83b2002-04-18 22:11:52 +0000606 Instruction *Used = cast<Instruction>(*UI);
607 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
Chris Lattner069a7952002-06-25 15:56:27 +0000608 " embeded in a basic block!", &I, Used);
Chris Lattner0e851da2002-04-18 20:37:37 +0000609 }
610
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000611 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
612 // Check to make sure that the "address of" an intrinsic function is never
613 // taken.
Chris Lattner08f7d0c2005-02-24 16:58:29 +0000614 Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I);
Chris Lattner9ece94b2004-03-14 03:23:54 +0000615 if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
Chris Lattnerbb346d02003-05-08 03:47:33 +0000616 Assert1(!F->isIntrinsic() || (i == 0 && isa<CallInst>(I)),
617 "Cannot take the address of an intrinsic!", &I);
Chris Lattner9ece94b2004-03-14 03:23:54 +0000618 } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
619 Assert1(OpBB->getParent() == BB->getParent(),
620 "Referring to a basic block in another function!", &I);
621 } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
622 Assert1(OpArg->getParent() == BB->getParent(),
623 "Referring to an argument in another function!", &I);
624 } else if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) {
Chris Lattnerec5089a2004-01-14 04:25:59 +0000625 BasicBlock *OpBlock = Op->getParent();
Chris Lattnerec5089a2004-01-14 04:25:59 +0000626
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000627 // Check that a definition dominates all of its uses.
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000628 if (!isa<PHINode>(I)) {
Chris Lattner02062822004-01-14 05:42:52 +0000629 // Invoke results are only usable in the normal destination, not in the
630 // exceptional destination.
631 if (InvokeInst *II = dyn_cast<InvokeInst>(Op))
632 OpBlock = II->getNormalDest();
Chris Lattner0377e432004-04-16 05:51:47 +0000633 else if (OpBlock == BB) {
634 // If they are in the same basic block, make sure that the definition
635 // comes before the use.
Chris Lattnerc9e79d02004-09-29 20:07:45 +0000636 Assert2(InstsInThisBlock.count(Op) ||
Chris Lattnerdc09e402006-01-12 06:17:59 +0000637 !EF->dominates(&BB->getParent()->getEntryBlock(), BB),
Chris Lattner0377e432004-04-16 05:51:47 +0000638 "Instruction does not dominate all uses!", Op, &I);
639 }
Chris Lattner02062822004-01-14 05:42:52 +0000640
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000641 // Definition must dominate use unless use is unreachable!
Chris Lattnerdc09e402006-01-12 06:17:59 +0000642 Assert2(EF->dominates(OpBlock, BB) ||
643 !EF->dominates(&BB->getParent()->getEntryBlock(), BB),
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000644 "Instruction does not dominate all uses!", Op, &I);
645 } else {
646 // PHI nodes are more difficult than other nodes because they actually
647 // "use" the value in the predecessor basic blocks they correspond to.
648 BasicBlock *PredBB = cast<BasicBlock>(I.getOperand(i+1));
Chris Lattnerdc09e402006-01-12 06:17:59 +0000649 Assert2(EF->dominates(OpBlock, PredBB) ||
650 !EF->dominates(&BB->getParent()->getEntryBlock(), PredBB),
Chris Lattnerdf9779c2003-10-05 17:44:18 +0000651 "Instruction does not dominate all uses!", Op, &I);
652 }
653 }
654 }
Chris Lattnerc9e79d02004-09-29 20:07:45 +0000655 InstsInThisBlock.insert(&I);
Chris Lattnerbb346d02003-05-08 03:47:33 +0000656}
657
658/// visitIntrinsicFunction - Allow intrinsics to be verified in different ways.
Misha Brukmanc566ca362004-03-02 00:22:19 +0000659///
Brian Gaeke960707c2003-11-11 22:41:34 +0000660void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnerbb346d02003-05-08 03:47:33 +0000661 Function *IF = CI.getCalledFunction();
662 const FunctionType *FT = IF->getFunctionType();
663 Assert1(IF->isExternal(), "Intrinsic functions should never be defined!", IF);
Chris Lattnerd295d992003-06-05 21:01:26 +0000664 unsigned NumArgs = 0;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000665
Chris Lattnerade94102003-08-24 05:30:29 +0000666 // FIXME: this should check the return type of each intrinsic as well, also
667 // arguments!
Chris Lattnerbb346d02003-05-08 03:47:33 +0000668 switch (ID) {
Chris Lattner071a5e52004-03-13 00:24:00 +0000669 case Intrinsic::vastart:
Chris Lattnerbad4b4a2003-05-08 15:55:31 +0000670 Assert1(CI.getParent()->getParent()->getFunctionType()->isVarArg(),
671 "llvm.va_start intrinsic may only occur in function with variable"
672 " args!", &CI);
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000673 NumArgs = 1;
Chris Lattnerbb346d02003-05-08 03:47:33 +0000674 break;
Chris Lattner071a5e52004-03-13 00:24:00 +0000675 case Intrinsic::vaend: NumArgs = 1; break;
Andrew Lenharth9144ec42005-06-18 18:34:52 +0000676 case Intrinsic::vacopy: NumArgs = 2; break;
Chris Lattnerade94102003-08-24 05:30:29 +0000677
Chris Lattnerdc632112004-02-14 02:47:17 +0000678 case Intrinsic::returnaddress:
679 case Intrinsic::frameaddress:
680 Assert1(isa<PointerType>(FT->getReturnType()),
681 "llvm.(frame|return)address must return pointers", IF);
682 Assert1(FT->getNumParams() == 1 && isa<ConstantInt>(CI.getOperand(1)),
683 "llvm.(frame|return)address require a single constant integer argument",
684 &CI);
685 NumArgs = 1;
686 break;
687
John Criswell52010042004-04-08 20:27:38 +0000688 // Verify that read and write port have integral parameters of the correct
689 // signed-ness.
690 case Intrinsic::writeport:
691 Assert1(FT->getNumParams() == 2,
692 "Illegal # arguments for intrinsic function!", IF);
John Criswell2b4c96e2004-04-09 19:09:14 +0000693 Assert1(FT->getParamType(0)->isIntegral(),
John Criswell52010042004-04-08 20:27:38 +0000694 "First argument not unsigned int!", IF);
John Criswell2b4c96e2004-04-09 19:09:14 +0000695 Assert1(FT->getParamType(1)->isUnsigned(),
John Criswell52010042004-04-08 20:27:38 +0000696 "First argument not unsigned int!", IF);
697 NumArgs = 2;
698 break;
699
John Criswell23c48d62004-04-14 13:46:52 +0000700 case Intrinsic::writeio:
701 Assert1(FT->getNumParams() == 2,
702 "Illegal # arguments for intrinsic function!", IF);
703 Assert1(FT->getParamType(0)->isFirstClassType(),
704 "First argument not a first class type!", IF);
Chris Lattner395e2192004-06-17 17:56:54 +0000705 Assert1(isa<PointerType>(FT->getParamType(1)),
John Criswell23c48d62004-04-14 13:46:52 +0000706 "Second argument not a pointer!", IF);
707 NumArgs = 2;
708 break;
709
John Criswell52010042004-04-08 20:27:38 +0000710 case Intrinsic::readport:
711 Assert1(FT->getNumParams() == 1,
712 "Illegal # arguments for intrinsic function!", IF);
John Criswell23c48d62004-04-14 13:46:52 +0000713 Assert1(FT->getReturnType()->isFirstClassType(),
714 "Return type is not a first class type!", IF);
John Criswell52010042004-04-08 20:27:38 +0000715 Assert1(FT->getParamType(0)->isUnsigned(),
716 "First argument not unsigned int!", IF);
717 NumArgs = 1;
718 break;
719
Chris Lattner9c251eb2004-05-23 21:16:51 +0000720 case Intrinsic::readio: {
721 const PointerType *ParamType = dyn_cast<PointerType>(FT->getParamType(0));
722 const Type *ReturnType = FT->getReturnType();
John Criswell0c654c62004-04-14 14:49:36 +0000723
John Criswell23c48d62004-04-14 13:46:52 +0000724 Assert1(FT->getNumParams() == 1,
725 "Illegal # arguments for intrinsic function!", IF);
Chris Lattner9c251eb2004-05-23 21:16:51 +0000726 Assert1(ParamType, "First argument not a pointer!", IF);
727 Assert1(ParamType->getElementType() == ReturnType,
John Criswellc4e72c92004-04-14 15:06:48 +0000728 "Pointer type doesn't match return type!", IF);
John Criswell23c48d62004-04-14 13:46:52 +0000729 NumArgs = 1;
730 break;
John Criswell0c654c62004-04-14 14:49:36 +0000731 }
John Criswell23c48d62004-04-14 13:46:52 +0000732
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000733 case Intrinsic::isunordered_f32:
Alkis Evlogimenoscf9f8f12004-06-13 00:55:26 +0000734 Assert1(FT->getNumParams() == 2,
735 "Illegal # arguments for intrinsic function!", IF);
Alkis Evlogimenos9d740622004-06-12 19:19:14 +0000736 Assert1(FT->getReturnType() == Type::BoolTy,
Alkis Evlogimenoscf9f8f12004-06-13 00:55:26 +0000737 "Return type is not bool!", IF);
738 Assert1(FT->getParamType(0) == FT->getParamType(1),
739 "Arguments must be of the same type!", IF);
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000740 Assert1(FT->getParamType(0) == Type::FloatTy,
741 "Arguments must be a 32-bit floating point type!", IF);
742 NumArgs = 2;
743 break;
744
745 case Intrinsic::isunordered_f64:
746 Assert1(FT->getNumParams() == 2,
747 "Illegal # arguments for intrinsic function!", IF);
748 Assert1(FT->getReturnType() == Type::BoolTy,
749 "Return type is not bool!", IF);
750 Assert1(FT->getParamType(0) == FT->getParamType(1),
751 "Arguments must be of the same type!", IF);
752 Assert1(FT->getParamType(0) == Type::DoubleTy,
753 "Argument is not a 64-bit floating point type!", IF);
Alkis Evlogimenos9d740622004-06-12 19:19:14 +0000754 NumArgs = 2;
755 break;
756
Andrew Lenharth01aa5632005-11-11 16:47:30 +0000757 case Intrinsic::readcyclecounter:
758 Assert1(FT->getNumParams() == 0,
759 "Illegal # arguments for intrinsic function!", IF);
760 Assert1(FT->getReturnType() == Type::ULongTy,
761 "Return type is not ulong!", IF);
762 NumArgs = 0;
763 break;
764
Nate Begeman82049eb2006-01-14 01:25:24 +0000765 case Intrinsic::bswap_i16:
766 Assert1(FT->getNumParams() == 1,
767 "Illegal # arguments for intrinsic function!", IF);
768 Assert1(FT->getReturnType() == FT->getParamType(0),
769 "Return type does not match source type", IF);
770 Assert1(FT->getReturnType() == Type::UShortTy,
771 "Return type is not ushort!", IF);
772 NumArgs = 1;
773 break;
774
775 case Intrinsic::bswap_i32:
776 Assert1(FT->getNumParams() == 1,
777 "Illegal # arguments for intrinsic function!", IF);
778 Assert1(FT->getReturnType() == FT->getParamType(0),
779 "Return type does not match source type", IF);
780 Assert1(FT->getReturnType() == Type::UIntTy,
781 "Return type is not uint!", IF);
782 NumArgs = 1;
783 break;
784
785 case Intrinsic::bswap_i64:
786 Assert1(FT->getNumParams() == 1,
787 "Illegal # arguments for intrinsic function!", IF);
788 Assert1(FT->getReturnType() == FT->getParamType(0),
789 "Return type does not match source type", IF);
790 Assert1(FT->getReturnType() == Type::ULongTy,
791 "Return type is not ulong!", IF);
792 NumArgs = 1;
793 break;
794
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000795 case Intrinsic::ctpop_i8:
Andrew Lenharth5e177822005-05-03 17:19:30 +0000796 Assert1(FT->getNumParams() == 1,
797 "Illegal # arguments for intrinsic function!", IF);
798 Assert1(FT->getReturnType() == FT->getParamType(0),
799 "Return type does not match source type", IF);
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000800 Assert1(FT->getParamType(0) == Type::UByteTy
801 || FT->getParamType(0) == Type::SByteTy,
802 "Argument must be a byte type!", IF);
Andrew Lenharth5e177822005-05-03 17:19:30 +0000803 NumArgs = 1;
804 break;
805
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000806 case Intrinsic::ctpop_i16:
Chris Lattner1c636f12005-04-30 03:44:07 +0000807 Assert1(FT->getNumParams() == 1,
808 "Illegal # arguments for intrinsic function!", IF);
Reid Spencerb4f9a6f2006-01-16 21:12:35 +0000809 Assert1(FT->getReturnType() == FT->getParamType(0),
810 "Return type does not match source type", IF);
811 Assert1(FT->getParamType(0) == Type::UShortTy
812 || FT->getParamType(0) == Type::ShortTy,
813 "Argument must be a short type!", IF);
814 NumArgs = 1;
815 break;
816
817 case Intrinsic::ctpop_i32:
818 Assert1(FT->getNumParams() == 1,
819 "Illegal # arguments for intrinsic function!", IF);
820 Assert1(FT->getReturnType() == FT->getParamType(0),
821 "Return type does not match source type", IF);
822 Assert1(FT->getParamType(0) == Type::UIntTy
823 || FT->getParamType(0) == Type::IntTy,
824 "Argument must be an int type!", IF);
825 NumArgs = 1;
826 break;
827
828 case Intrinsic::ctpop_i64:
829 Assert1(FT->getNumParams() == 1,
830 "Illegal # arguments for intrinsic function!", IF);
831 Assert1(FT->getReturnType() == FT->getParamType(0),
832 "Return type does not match source type", IF);
833 Assert1(FT->getParamType(0) == Type::ULongTy
834 || FT->getParamType(0) == Type::LongTy,
835 "Argument must be a long type!", IF);
836 NumArgs = 1;
837 break;
838
839 case Intrinsic::ctlz_i8:
840 Assert1(FT->getNumParams() == 1,
841 "Illegal # arguments for intrinsic function!", IF);
842 Assert1(FT->getReturnType() == FT->getParamType(0),
843 "Return type does not match source type", IF);
844 Assert1(FT->getParamType(0) == Type::UByteTy
845 || FT->getParamType(0) == Type::SByteTy,
846 "Argument must be a byte type!", IF);
847 NumArgs = 1;
848 break;
849
850 case Intrinsic::ctlz_i16:
851 Assert1(FT->getNumParams() == 1,
852 "Illegal # arguments for intrinsic function!", IF);
853 Assert1(FT->getReturnType() == FT->getParamType(0),
854 "Return type does not match source type", IF);
855 Assert1(FT->getParamType(0) == Type::UShortTy
856 || FT->getParamType(0) == Type::ShortTy,
857 "Argument must be a short type!", IF);
858 NumArgs = 1;
859 break;
860 case Intrinsic::ctlz_i32:
861 Assert1(FT->getNumParams() == 1,
862 "Illegal # arguments for intrinsic function!", IF);
863 Assert1(FT->getReturnType() == FT->getParamType(0),
864 "Return type does not match source type", IF);
865 Assert1(FT->getParamType(0) == Type::UIntTy
866 || FT->getParamType(0) == Type::IntTy,
867 "Argument must be an int type!", IF);
868 NumArgs = 1;
869 break;
870 case Intrinsic::ctlz_i64:
871 Assert1(FT->getNumParams() == 1,
872 "Illegal # arguments for intrinsic function!", IF);
873 Assert1(FT->getReturnType() == FT->getParamType(0),
874 "Return type does not match source type", IF);
875 Assert1(FT->getParamType(0) == Type::ULongTy
876 || FT->getParamType(0) == Type::LongTy,
877 "Argument must be a long type!", IF);
878 NumArgs = 1;
879 break;
880 case Intrinsic::cttz_i8:
881 Assert1(FT->getNumParams() == 1,
882 "Illegal # arguments for intrinsic function!", IF);
883 Assert1(FT->getReturnType() == FT->getParamType(0),
884 "Return type does not match source type", IF);
885 Assert1(FT->getParamType(0) == Type::UByteTy
886 || FT->getParamType(0) == Type::SByteTy,
887 "Argument must be a byte type!", IF);
888 NumArgs = 1;
889 break;
890 case Intrinsic::cttz_i16:
891 Assert1(FT->getNumParams() == 1,
892 "Illegal # arguments for intrinsic function!", IF);
893 Assert1(FT->getReturnType() == FT->getParamType(0),
894 "Return type does not match source type", IF);
895 Assert1(FT->getParamType(0) == Type::UShortTy
896 || FT->getParamType(0) == Type::ShortTy,
897 "Argument must be a short type!", IF);
898 NumArgs = 1;
899 break;
900 case Intrinsic::cttz_i32:
901 Assert1(FT->getNumParams() == 1,
902 "Illegal # arguments for intrinsic function!", IF);
903 Assert1(FT->getReturnType() == FT->getParamType(0),
904 "Return type does not match source type", IF);
905 Assert1(FT->getParamType(0) == Type::UIntTy
906 || FT->getParamType(0) == Type::IntTy,
907 "Argument must be an int type!", IF);
908 NumArgs = 1;
909 break;
910 case Intrinsic::cttz_i64:
911 Assert1(FT->getNumParams() == 1,
912 "Illegal # arguments for intrinsic function!", IF);
913 Assert1(FT->getReturnType() == FT->getParamType(0),
914 "Return type does not match source type", IF);
915 Assert1(FT->getParamType(0) == Type::ULongTy
916 || FT->getParamType(0) == Type::LongTy,
917 "Argument must be a long type!", IF);
918 NumArgs = 1;
919 break;
920
921 case Intrinsic::sqrt_f32:
922 Assert1(FT->getNumParams() == 1,
923 "Illegal # arguments for intrinsic function!", IF);
924 Assert1(FT->getParamType(0) == Type::FloatTy,
925 "Argument is not a 32-bit floating point type!", IF);
926 Assert1(FT->getReturnType() == FT->getParamType(0),
927 "Return type is not the same as argument type!", IF);
928 NumArgs = 1;
929 break;
930
931 case Intrinsic::sqrt_f64:
932 Assert1(FT->getNumParams() == 1,
933 "Illegal # arguments for intrinsic function!", IF);
934 Assert1(FT->getParamType(0) == Type::DoubleTy,
935 "Argument is not a 64-bit floating point type!", IF);
Chris Lattner1c636f12005-04-30 03:44:07 +0000936 Assert1(FT->getReturnType() == FT->getParamType(0),
937 "Return type is not the same as argument type!", IF);
938 NumArgs = 1;
939 break;
940
Brian Gaeke960707c2003-11-11 22:41:34 +0000941 case Intrinsic::setjmp: NumArgs = 1; break;
942 case Intrinsic::longjmp: NumArgs = 2; break;
943 case Intrinsic::sigsetjmp: NumArgs = 2; break;
944 case Intrinsic::siglongjmp: NumArgs = 2; break;
Chris Lattner3d903f02004-01-05 05:36:30 +0000945
Chris Lattner9c251eb2004-05-23 21:16:51 +0000946 case Intrinsic::gcroot:
947 Assert1(FT->getNumParams() == 2,
948 "Illegal # arguments for intrinsic function!", IF);
Reid Spencer49fc8a72004-07-18 00:02:41 +0000949 Assert1(isa<Constant>(CI.getOperand(2)),
Chris Lattner9c251eb2004-05-23 21:16:51 +0000950 "Second argument to llvm.gcroot must be a constant!", &CI);
951 NumArgs = 2;
952 break;
Chris Lattner001aba72004-07-22 05:50:01 +0000953 case Intrinsic::gcread: NumArgs = 2; break;
954 case Intrinsic::gcwrite: NumArgs = 3; break;
Chris Lattner9c251eb2004-05-23 21:16:51 +0000955
Chris Lattner3d903f02004-01-05 05:36:30 +0000956 case Intrinsic::dbg_stoppoint: NumArgs = 4; break;
957 case Intrinsic::dbg_region_start:NumArgs = 1; break;
958 case Intrinsic::dbg_region_end: NumArgs = 1; break;
959 case Intrinsic::dbg_func_start: NumArgs = 1; break;
Chris Lattner59f1ef42004-01-06 05:33:02 +0000960 case Intrinsic::dbg_declare: NumArgs = 1; break;
Chris Lattner17d028d2004-02-12 17:01:09 +0000961
962 case Intrinsic::memcpy: NumArgs = 4; break;
Chris Lattner5ed171e2004-02-12 18:11:20 +0000963 case Intrinsic::memmove: NumArgs = 4; break;
Chris Lattnerdc632112004-02-14 02:47:17 +0000964 case Intrinsic::memset: NumArgs = 4; break;
Chris Lattner39637ef2005-02-28 19:27:42 +0000965
Chris Lattnerc482f162006-01-13 02:15:39 +0000966 case Intrinsic::stacksave:
967 NumArgs = 0;
968 Assert1(CI.getType() == PointerType::get(Type::SByteTy),
969 "llvm.stacksave must return an sbyte*", &CI);
970 break;
971 case Intrinsic::stackrestore:
972 NumArgs = 1;
973 Assert1(CI.getOperand(1)->getType() == PointerType::get(Type::SByteTy),
974 "llvm.stackrestore must take an sbyte*", &CI);
975 Assert1(CI.getType() == Type::VoidTy,
976 "llvm.stackrestore return void", &CI);
977 break;
Chris Lattner39637ef2005-02-28 19:27:42 +0000978 case Intrinsic::prefetch: NumArgs = 3; break;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000979 case Intrinsic::pcmarker:
980 NumArgs = 1;
Andrew Lenharthb4427912005-03-28 20:05:49 +0000981 Assert1(isa<Constant>(CI.getOperand(1)),
982 "First argument to llvm.pcmarker must be a constant!", &CI);
983 break;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000984
985 case Intrinsic::not_intrinsic:
Chris Lattnerbb346d02003-05-08 03:47:33 +0000986 assert(0 && "Invalid intrinsic!"); NumArgs = 0; break;
987 }
988
989 Assert1(FT->getNumParams() == NumArgs || (FT->getNumParams() < NumArgs &&
990 FT->isVarArg()),
991 "Illegal # arguments for intrinsic function!", IF);
Chris Lattner0e851da2002-04-18 20:37:37 +0000992}
993
994
995//===----------------------------------------------------------------------===//
996// Implement the public interfaces to this file...
997//===----------------------------------------------------------------------===//
998
Chris Lattnera45a2162004-04-02 15:45:08 +0000999FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) {
1000 return new Verifier(action);
Chris Lattner0e851da2002-04-18 20:37:37 +00001001}
1002
Chris Lattner8e72d6f2002-08-02 17:37:08 +00001003
Misha Brukmanb1c93172005-04-21 23:48:37 +00001004// verifyFunction - Create
Chris Lattnera45a2162004-04-02 15:45:08 +00001005bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) {
Chris Lattnerb870ca72004-03-14 03:16:15 +00001006 Function &F = const_cast<Function&>(f);
Chris Lattner8e72d6f2002-08-02 17:37:08 +00001007 assert(!F.isExternal() && "Cannot verify external functions");
Misha Brukmanb1c93172005-04-21 23:48:37 +00001008
Chris Lattnerb870ca72004-03-14 03:16:15 +00001009 FunctionPassManager FPM(new ExistingModuleProvider(F.getParent()));
Chris Lattnera45a2162004-04-02 15:45:08 +00001010 Verifier *V = new Verifier(action);
Chris Lattnerb870ca72004-03-14 03:16:15 +00001011 FPM.add(V);
1012 FPM.run(F);
1013 return V->Broken;
Chris Lattnerd02f08d2002-02-20 17:55:43 +00001014}
1015
Misha Brukmanc566ca362004-03-02 00:22:19 +00001016/// verifyModule - Check a module for errors, printing messages on stderr.
1017/// Return true if the module is corrupt.
1018///
Chris Lattnera45a2162004-04-02 15:45:08 +00001019bool llvm::verifyModule(const Module &M, VerifierFailureAction action) {
Chris Lattner8e72d6f2002-08-02 17:37:08 +00001020 PassManager PM;
Chris Lattnera45a2162004-04-02 15:45:08 +00001021 Verifier *V = new Verifier(action);
Chris Lattner8e72d6f2002-08-02 17:37:08 +00001022 PM.add(V);
1023 PM.run((Module&)M);
1024 return V->Broken;
Chris Lattner2f7c9632001-06-06 20:29:01 +00001025}
Reid Spencer47cf71a2004-05-25 08:53:29 +00001026
1027// vim: sw=2