blob: 7e92eb303d781d42f6e2d63a56ce077f55c610d4 [file] [log] [blame]
Chris Lattner00950542001-06-06 20:29:01 +00001//===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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 Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnera5c3dec2002-03-29 19:06:18 +000010// This file defines the function verifier interface, that can be used for some
Chris Lattner00950542001-06-06 20:29:01 +000011// sanity checking of input to the system.
12//
Misha Brukman5b636312004-06-24 21:47:35 +000013// Note that this does not provide full `Java style' security and verifications,
14// instead it just tries to ensure that code is well-formed.
Chris Lattner00950542001-06-06 20:29:01 +000015//
Misha Brukman5b636312004-06-24 21:47:35 +000016// * Both of a binary operator's parameters are of the same type
Chris Lattnera00409e2002-04-24 19:12:21 +000017// * Verify that the indices of mem access instructions match other operands
Misha Brukman5b636312004-06-24 21:47:35 +000018// * Verify that arithmetic and other things are only performed on first-class
Chris Lattner9ce231f2002-08-02 17:37:08 +000019// types. Verify that shifts & logicals only happen on integrals f.e.
Misha Brukman5b636312004-06-24 21:47:35 +000020// * All of the constants in a switch statement are of the correct type
Chris Lattner9ce231f2002-08-02 17:37:08 +000021// * The code is in valid SSA form
Misha Brukman5b636312004-06-24 21:47:35 +000022// * It should be illegal to put a label into any other type (like a structure)
Chris Lattner00950542001-06-06 20:29:01 +000023// or to return one. [except constant arrays!]
Chris Lattnerfdec2462002-03-14 16:53:48 +000024// * Only phi nodes can be self referential: 'add int %0, %0 ; <int>:0' is bad
Chris Lattner44d5bd92002-02-20 17:55:43 +000025// * PHI nodes must have an entry for each predecessor, with no extras.
Chris Lattner24e845f2002-06-25 15:56:27 +000026// * PHI nodes must be the first thing in a basic block, all grouped together
Chris Lattnerf6ffcb62002-10-06 21:00:31 +000027// * PHI nodes must have at least one entry
Chris Lattner24e845f2002-06-25 15:56:27 +000028// * All basic blocks should only end with terminator insts, not contain them
Chris Lattnera5c3dec2002-03-29 19:06:18 +000029// * The entry node to a function must not have predecessors
Misha Brukman6b634522003-10-10 17:54:14 +000030// * All Instructions must be embedded into a basic block
Misha Brukman5b636312004-06-24 21:47:35 +000031// * Functions cannot take a void-typed parameter
Chris Lattnerea249242002-04-13 22:48:46 +000032// * Verify that a function's argument list agrees with it's declared type.
Chris Lattneracd3cae2002-03-15 20:25:09 +000033// * It is illegal to specify a name for a void value.
Misha Brukman6b634522003-10-10 17:54:14 +000034// * It is illegal to have a internal global value with no initializer
Chris Lattner23f0ce62002-04-12 18:20:49 +000035// * It is illegal to have a ret instruction that returns a value that does not
36// agree with the function return value type.
Chris Lattner56732fb2002-05-08 19:49:50 +000037// * Function call argument types match the function prototype
Chris Lattnera00409e2002-04-24 19:12:21 +000038// * All other things that are tested by asserts spread about the code...
Chris Lattner00950542001-06-06 20:29:01 +000039//
40//===----------------------------------------------------------------------===//
41
42#include "llvm/Analysis/Verifier.h"
Brian Gaeke9cebe2d2003-11-16 23:07:42 +000043#include "llvm/Assembly/Writer.h"
Chris Lattner37c121a2005-05-08 22:27:09 +000044#include "llvm/CallingConv.h"
Chris Lattnercf899082004-02-14 02:47:17 +000045#include "llvm/Constants.h"
Chris Lattner44d5bd92002-02-20 17:55:43 +000046#include "llvm/Pass.h"
Chris Lattner00950542001-06-06 20:29:01 +000047#include "llvm/Module.h"
Chris Lattner2eff8592004-03-14 03:16:15 +000048#include "llvm/ModuleProvider.h"
Chris Lattnerea249242002-04-13 22:48:46 +000049#include "llvm/DerivedTypes.h"
Chris Lattner3188b732006-01-26 00:08:45 +000050#include "llvm/InlineAsm.h"
Chris Lattnercf899082004-02-14 02:47:17 +000051#include "llvm/Instructions.h"
Chris Lattnerdd035d12003-05-08 03:47:33 +000052#include "llvm/Intrinsics.h"
Chris Lattnercf899082004-02-14 02:47:17 +000053#include "llvm/PassManager.h"
Chris Lattner9ce231f2002-08-02 17:37:08 +000054#include "llvm/Analysis/Dominators.h"
Chris Lattner44d5bd92002-02-20 17:55:43 +000055#include "llvm/Support/CFG.h"
Chris Lattnerd231fc32002-04-18 20:37:37 +000056#include "llvm/Support/InstVisitor.h"
Bill Wendling8f487662006-11-28 02:09:03 +000057#include "llvm/Support/Streams.h"
Chris Lattner78287b42007-02-10 08:19:44 +000058#include "llvm/ADT/SmallPtrSet.h"
Chris Lattner8552fae2007-02-10 08:30:29 +000059#include "llvm/ADT/SmallVector.h"
Chris Lattner536a9d52006-03-31 04:46:47 +000060#include "llvm/ADT/StringExtras.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000061#include "llvm/ADT/STLExtras.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000062#include "llvm/Support/Compiler.h"
Chris Lattner44d5bd92002-02-20 17:55:43 +000063#include <algorithm>
Bill Wendling1a097e32006-12-07 23:41:45 +000064#include <sstream>
Jeff Cohen4c5701d2006-03-31 07:22:05 +000065#include <cstdarg>
Chris Lattner31f84992003-11-21 20:23:48 +000066using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000067
Chris Lattnerd231fc32002-04-18 20:37:37 +000068namespace { // Anonymous namespace for class
Chris Lattner00950542001-06-06 20:29:01 +000069
Chris Lattnerf190d382006-06-28 21:38:54 +000070 struct VISIBILITY_HIDDEN
71 Verifier : public FunctionPass, InstVisitor<Verifier> {
Devang Patel19974732007-05-03 01:11:54 +000072 static char ID; // Pass ID, replacement for typeid
Chris Lattner9ce231f2002-08-02 17:37:08 +000073 bool Broken; // Is this module found to be broken?
74 bool RealPass; // Are we not being run by a PassManager?
Chris Lattnerfdc38c42004-04-02 15:45:08 +000075 VerifierFailureAction action;
Reid Spenceraf90b0d2004-05-25 08:53:29 +000076 // What to do if verification fails.
Misha Brukmanab5c6002004-03-02 00:22:19 +000077 Module *Mod; // Module we are verifying right now
Chris Lattner0b2192c2006-01-12 06:17:59 +000078 ETForest *EF; // ET-Forest, caution can be null!
Chris Lattnerfdc38c42004-04-02 15:45:08 +000079 std::stringstream msgs; // A stringstream to collect messages
Chris Lattner00950542001-06-06 20:29:01 +000080
Chris Lattnera7b1c7e2004-09-29 20:07:45 +000081 /// InstInThisBlock - when verifying a basic block, keep track of all of the
82 /// instructions we have seen so far. This allows us to do efficient
83 /// dominance checks for the case when an instruction has an operand that is
84 /// an instruction in the same block.
Chris Lattner78287b42007-02-10 08:19:44 +000085 SmallPtrSet<Instruction*, 16> InstsInThisBlock;
Chris Lattnera7b1c7e2004-09-29 20:07:45 +000086
Misha Brukmanfd939082005-04-21 23:48:37 +000087 Verifier()
Devang Patel794fd752007-05-01 21:15:47 +000088 : FunctionPass((intptr_t)&ID),
89 Broken(false), RealPass(true), action(AbortProcessAction),
90 EF(0), msgs( std::ios::app | std::ios::out ) {}
Chris Lattnerfdc38c42004-04-02 15:45:08 +000091 Verifier( VerifierFailureAction ctn )
Devang Patel794fd752007-05-01 21:15:47 +000092 : FunctionPass((intptr_t)&ID),
93 Broken(false), RealPass(true), action(ctn), EF(0),
94 msgs( std::ios::app | std::ios::out ) {}
Misha Brukmanfd939082005-04-21 23:48:37 +000095 Verifier(bool AB )
Devang Patel794fd752007-05-01 21:15:47 +000096 : FunctionPass((intptr_t)&ID),
97 Broken(false), RealPass(true),
98 action( AB ? AbortProcessAction : PrintMessageAction), EF(0),
99 msgs( std::ios::app | std::ios::out ) {}
Chris Lattner0b2192c2006-01-12 06:17:59 +0000100 Verifier(ETForest &ef)
Devang Patel794fd752007-05-01 21:15:47 +0000101 : FunctionPass((intptr_t)&ID),
102 Broken(false), RealPass(false), action(PrintMessageAction),
103 EF(&ef), msgs( std::ios::app | std::ios::out ) {}
Chris Lattner9ce231f2002-08-02 17:37:08 +0000104
Chris Lattner00950542001-06-06 20:29:01 +0000105
Chris Lattner24e845f2002-06-25 15:56:27 +0000106 bool doInitialization(Module &M) {
Brian Gaeke9cebe2d2003-11-16 23:07:42 +0000107 Mod = &M;
Reid Spencer78d033e2007-01-06 07:24:44 +0000108 verifyTypeSymbolTable(M.getTypeSymbolTable());
Chris Lattner3e1f1442002-09-19 16:12:19 +0000109
110 // If this is a real pass, in a pass manager, we must abort before
111 // returning back to the pass manager, or else the pass manager may try to
112 // run other passes on the broken module.
Chris Lattner3e1f1442002-09-19 16:12:19 +0000113 if (RealPass)
Reid Spencer7107c3b2006-07-26 16:18:00 +0000114 return abortIfBroken();
Chris Lattnerd231fc32002-04-18 20:37:37 +0000115 return false;
116 }
117
Chris Lattner24e845f2002-06-25 15:56:27 +0000118 bool runOnFunction(Function &F) {
Chris Lattner9ce231f2002-08-02 17:37:08 +0000119 // Get dominator information if we are being run by PassManager
Chris Lattner0b2192c2006-01-12 06:17:59 +0000120 if (RealPass) EF = &getAnalysis<ETForest>();
Chris Lattnercd070752007-04-20 23:59:29 +0000121
122 Mod = F.getParent();
123
Chris Lattnerd231fc32002-04-18 20:37:37 +0000124 visit(F);
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000125 InstsInThisBlock.clear();
Chris Lattner3e1f1442002-09-19 16:12:19 +0000126
127 // If this is a real pass, in a pass manager, we must abort before
128 // returning back to the pass manager, or else the pass manager may try to
129 // run other passes on the broken module.
Chris Lattner3e1f1442002-09-19 16:12:19 +0000130 if (RealPass)
Reid Spencer7107c3b2006-07-26 16:18:00 +0000131 return abortIfBroken();
Chris Lattner3e1f1442002-09-19 16:12:19 +0000132
Chris Lattnerd231fc32002-04-18 20:37:37 +0000133 return false;
134 }
135
Chris Lattner24e845f2002-06-25 15:56:27 +0000136 bool doFinalization(Module &M) {
Chris Lattner794caa12002-04-28 16:04:26 +0000137 // Scan through, checking all of the external function's linkage now...
Chris Lattner7c277b32004-06-03 06:38:43 +0000138 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner53997412003-04-16 20:42:40 +0000139 visitGlobalValue(*I);
Chris Lattner794caa12002-04-28 16:04:26 +0000140
Chris Lattner7c277b32004-06-03 06:38:43 +0000141 // Check to make sure function prototypes are okay.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000142 if (I->isDeclaration()) visitFunction(*I);
Chris Lattner7c277b32004-06-03 06:38:43 +0000143 }
144
Reid Spencer0b118202006-01-16 21:12:35 +0000145 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
146 I != E; ++I)
Chris Lattner56998b22004-12-15 20:23:49 +0000147 visitGlobalVariable(*I);
Chris Lattner61b91bc2002-10-06 22:47:32 +0000148
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000149 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
150 I != E; ++I)
151 visitGlobalAlias(*I);
152
Chris Lattner3e1f1442002-09-19 16:12:19 +0000153 // If the module is broken, abort at this time.
Reid Spencer7107c3b2006-07-26 16:18:00 +0000154 return abortIfBroken();
Chris Lattnera00409e2002-04-24 19:12:21 +0000155 }
156
Chris Lattner97e52e42002-04-28 21:27:06 +0000157 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
158 AU.setPreservesAll();
Chris Lattner9ce231f2002-08-02 17:37:08 +0000159 if (RealPass)
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000160 AU.addRequired<ETForest>();
Chris Lattner97e52e42002-04-28 21:27:06 +0000161 }
162
Misha Brukmanab5c6002004-03-02 00:22:19 +0000163 /// abortIfBroken - If the module is broken and we are supposed to abort on
164 /// this condition, do so.
165 ///
Reid Spencer7107c3b2006-07-26 16:18:00 +0000166 bool abortIfBroken() {
Chris Lattner05ac92c2006-07-06 18:02:27 +0000167 if (Broken) {
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000168 msgs << "Broken module found, ";
Chris Lattner05ac92c2006-07-06 18:02:27 +0000169 switch (action) {
John Criswell4a9c9042004-05-04 21:46:05 +0000170 case AbortProcessAction:
171 msgs << "compilation aborted!\n";
Bill Wendlinge8156192006-12-07 01:30:32 +0000172 cerr << msgs.str();
John Criswell4a9c9042004-05-04 21:46:05 +0000173 abort();
John Criswell4a9c9042004-05-04 21:46:05 +0000174 case PrintMessageAction:
175 msgs << "verification continues.\n";
Bill Wendlinge8156192006-12-07 01:30:32 +0000176 cerr << msgs.str();
Reid Spencer7107c3b2006-07-26 16:18:00 +0000177 return false;
John Criswell4a9c9042004-05-04 21:46:05 +0000178 case ReturnStatusAction:
Reid Spencer7107c3b2006-07-26 16:18:00 +0000179 msgs << "compilation terminated.\n";
180 return Broken;
John Criswell4a9c9042004-05-04 21:46:05 +0000181 }
Chris Lattner3e1f1442002-09-19 16:12:19 +0000182 }
Reid Spencer7107c3b2006-07-26 16:18:00 +0000183 return false;
Chris Lattner3e1f1442002-09-19 16:12:19 +0000184 }
185
Chris Lattner53997412003-04-16 20:42:40 +0000186
Chris Lattnerd231fc32002-04-18 20:37:37 +0000187 // Verification methods...
Reid Spencer78d033e2007-01-06 07:24:44 +0000188 void verifyTypeSymbolTable(TypeSymbolTable &ST);
Chris Lattner53997412003-04-16 20:42:40 +0000189 void visitGlobalValue(GlobalValue &GV);
Chris Lattner56998b22004-12-15 20:23:49 +0000190 void visitGlobalVariable(GlobalVariable &GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000191 void visitGlobalAlias(GlobalAlias &GA);
Chris Lattner24e845f2002-06-25 15:56:27 +0000192 void visitFunction(Function &F);
193 void visitBasicBlock(BasicBlock &BB);
Reid Spencer3da59db2006-11-27 01:05:10 +0000194 void visitTruncInst(TruncInst &I);
195 void visitZExtInst(ZExtInst &I);
196 void visitSExtInst(SExtInst &I);
197 void visitFPTruncInst(FPTruncInst &I);
198 void visitFPExtInst(FPExtInst &I);
199 void visitFPToUIInst(FPToUIInst &I);
200 void visitFPToSIInst(FPToSIInst &I);
201 void visitUIToFPInst(UIToFPInst &I);
202 void visitSIToFPInst(SIToFPInst &I);
203 void visitIntToPtrInst(IntToPtrInst &I);
204 void visitPtrToIntInst(PtrToIntInst &I);
205 void visitBitCastInst(BitCastInst &I);
Chris Lattner24e845f2002-06-25 15:56:27 +0000206 void visitPHINode(PHINode &PN);
207 void visitBinaryOperator(BinaryOperator &B);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000208 void visitICmpInst(ICmpInst &IC);
209 void visitFCmpInst(FCmpInst &FC);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000210 void visitExtractElementInst(ExtractElementInst &EI);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000211 void visitInsertElementInst(InsertElementInst &EI);
Chris Lattner00f10232006-04-08 01:18:18 +0000212 void visitShuffleVectorInst(ShuffleVectorInst &EI);
Chris Lattner4d45bd02003-10-18 05:57:43 +0000213 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
Chris Lattner24e845f2002-06-25 15:56:27 +0000214 void visitCallInst(CallInst &CI);
215 void visitGetElementPtrInst(GetElementPtrInst &GEP);
216 void visitLoadInst(LoadInst &LI);
217 void visitStoreInst(StoreInst &SI);
218 void visitInstruction(Instruction &I);
219 void visitTerminatorInst(TerminatorInst &I);
220 void visitReturnInst(ReturnInst &RI);
Chris Lattner0f9e9d02004-05-21 16:47:21 +0000221 void visitSwitchInst(SwitchInst &SI);
Chris Lattner230c1a72004-03-12 05:54:31 +0000222 void visitSelectInst(SelectInst &SI);
Chris Lattner627079d2002-11-21 16:54:22 +0000223 void visitUserOp1(Instruction &I);
224 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
Brian Gaeked0fde302003-11-11 22:41:34 +0000225 void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000226
Reid Spencer559d77a2007-04-01 07:22:57 +0000227 void VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F, ...);
Chris Lattner15e87522003-11-21 17:35:51 +0000228
229 void WriteValue(const Value *V) {
230 if (!V) return;
Chris Lattner31f84992003-11-21 20:23:48 +0000231 if (isa<Instruction>(V)) {
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000232 msgs << *V;
Chris Lattner31f84992003-11-21 20:23:48 +0000233 } else {
Chris Lattner3749c9c2006-12-06 06:16:21 +0000234 WriteAsOperand(msgs, V, true, Mod);
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000235 msgs << "\n";
Chris Lattner15e87522003-11-21 17:35:51 +0000236 }
237 }
238
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000239 void WriteType(const Type* T ) {
240 if ( !T ) return;
241 WriteTypeSymbolic(msgs, T, Mod );
242 }
243
Chris Lattner15e87522003-11-21 17:35:51 +0000244
Chris Lattnerd231fc32002-04-18 20:37:37 +0000245 // CheckFailed - A check failed, so print out the condition and the message
246 // that failed. This provides a nice place to put a breakpoint if you want
247 // to see why something is not correct.
Chris Lattner15e87522003-11-21 17:35:51 +0000248 void CheckFailed(const std::string &Message,
249 const Value *V1 = 0, const Value *V2 = 0,
250 const Value *V3 = 0, const Value *V4 = 0) {
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000251 msgs << Message << "\n";
Chris Lattner15e87522003-11-21 17:35:51 +0000252 WriteValue(V1);
253 WriteValue(V2);
254 WriteValue(V3);
255 WriteValue(V4);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000256 Broken = true;
257 }
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000258
Misha Brukmanfd939082005-04-21 23:48:37 +0000259 void CheckFailed( const std::string& Message, const Value* V1,
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000260 const Type* T2, const Value* V3 = 0 ) {
261 msgs << Message << "\n";
262 WriteValue(V1);
263 WriteType(T2);
264 WriteValue(V3);
Reid Spencer5dff1582004-05-27 21:58:13 +0000265 Broken = true;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000266 }
Chris Lattnerd231fc32002-04-18 20:37:37 +0000267 };
Chris Lattnere20a5dd2002-07-23 18:08:17 +0000268
Devang Patel19974732007-05-03 01:11:54 +0000269 char Verifier::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +0000270 RegisterPass<Verifier> X("verify", "Module Verifier");
Chris Lattner31f84992003-11-21 20:23:48 +0000271} // End anonymous namespace
272
Chris Lattner00950542001-06-06 20:29:01 +0000273
Chris Lattner44d5bd92002-02-20 17:55:43 +0000274// Assert - We know that cond should be true, if not print an error message.
275#define Assert(C, M) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000276 do { if (!(C)) { CheckFailed(M); return; } } while (0)
Chris Lattner44d5bd92002-02-20 17:55:43 +0000277#define Assert1(C, M, V1) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000278 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
Chris Lattner44d5bd92002-02-20 17:55:43 +0000279#define Assert2(C, M, V1, V2) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000280 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
Chris Lattner24e845f2002-06-25 15:56:27 +0000281#define Assert3(C, M, V1, V2, V3) \
282 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
283#define Assert4(C, M, V1, V2, V3, V4) \
284 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
Chris Lattner00950542001-06-06 20:29:01 +0000285
Chris Lattner44d5bd92002-02-20 17:55:43 +0000286
Chris Lattner53997412003-04-16 20:42:40 +0000287void Verifier::visitGlobalValue(GlobalValue &GV) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000288 Assert1(!GV.isDeclaration() ||
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000289 GV.hasExternalLinkage() ||
290 GV.hasDLLImportLinkage() ||
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000291 GV.hasExternalWeakLinkage() ||
292 (isa<GlobalAlias>(GV) &&
293 (GV.hasInternalLinkage() || GV.hasWeakLinkage())),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000294 "Global is external, but doesn't have external or dllimport or weak linkage!",
295 &GV);
296
Reid Spencer5cbf9852007-01-30 20:08:39 +0000297 Assert1(!GV.hasDLLImportLinkage() || GV.isDeclaration(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000298 "Global is marked as dllimport, but not external", &GV);
299
Chris Lattner53997412003-04-16 20:42:40 +0000300 Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
301 "Only global variables can have appending linkage!", &GV);
302
303 if (GV.hasAppendingLinkage()) {
304 GlobalVariable &GVar = cast<GlobalVariable>(GV);
305 Assert1(isa<ArrayType>(GVar.getType()->getElementType()),
306 "Only global arrays can have appending linkage!", &GV);
307 }
308}
309
Chris Lattner56998b22004-12-15 20:23:49 +0000310void Verifier::visitGlobalVariable(GlobalVariable &GV) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000311 if (GV.hasInitializer())
Chris Lattner56998b22004-12-15 20:23:49 +0000312 Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(),
313 "Global variable initializer type does not match global "
314 "variable type!", &GV);
Misha Brukmanfd939082005-04-21 23:48:37 +0000315
Chris Lattner56998b22004-12-15 20:23:49 +0000316 visitGlobalValue(GV);
317}
318
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000319void Verifier::visitGlobalAlias(GlobalAlias &GA) {
320 Assert1(!GA.getName().empty(),
321 "Alias name cannot be empty!", &GA);
322 Assert1(GA.hasExternalLinkage() || GA.hasInternalLinkage() ||
323 GA.hasWeakLinkage(),
324 "Alias should have external or external weak linkage!", &GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000325 Assert1(GA.getType() == GA.getAliasee()->getType(),
326 "Alias and aliasee types should match!", &GA);
327
Anton Korobeynikov0f53f7f2007-04-28 14:35:41 +0000328 if (!isa<GlobalValue>(GA.getAliasee())) {
329 const ConstantExpr *CE = dyn_cast<ConstantExpr>(GA.getAliasee());
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000330 Assert1(CE && CE->getOpcode() == Instruction::BitCast &&
331 isa<GlobalValue>(CE->getOperand(0)),
Anton Korobeynikov0f53f7f2007-04-28 14:35:41 +0000332 "Aliasee should be either GlobalValue or bitcast of GlobalValue",
333 &GA);
334 }
335
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000336 visitGlobalValue(GA);
337}
338
Reid Spencer78d033e2007-01-06 07:24:44 +0000339void Verifier::verifyTypeSymbolTable(TypeSymbolTable &ST) {
340}
Chris Lattner56998b22004-12-15 20:23:49 +0000341
Chris Lattnerd231fc32002-04-18 20:37:37 +0000342// visitFunction - Verify that a function is ok.
Chris Lattner44d5bd92002-02-20 17:55:43 +0000343//
Chris Lattner24e845f2002-06-25 15:56:27 +0000344void Verifier::visitFunction(Function &F) {
Chris Lattner37c121a2005-05-08 22:27:09 +0000345 // Check function arguments.
Chris Lattner24e845f2002-06-25 15:56:27 +0000346 const FunctionType *FT = F.getFunctionType();
347 unsigned NumArgs = F.getArgumentList().size();
Chris Lattnerea249242002-04-13 22:48:46 +0000348
Chris Lattner69da5cf2002-10-13 20:57:00 +0000349 Assert2(FT->getNumParams() == NumArgs,
Chris Lattnerea249242002-04-13 22:48:46 +0000350 "# formal arguments must match # of arguments for function type!",
Chris Lattner24e845f2002-06-25 15:56:27 +0000351 &F, FT);
Chris Lattnerc282f5a2003-11-21 22:32:23 +0000352 Assert1(F.getReturnType()->isFirstClassType() ||
353 F.getReturnType() == Type::VoidTy,
354 "Functions cannot return aggregate values!", &F);
Chris Lattnerea249242002-04-13 22:48:46 +0000355
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000356 Assert1(!FT->isStructReturn() ||
357 (FT->getReturnType() == Type::VoidTy &&
358 FT->getNumParams() > 0 && isa<PointerType>(FT->getParamType(0))),
359 "Invalid struct-return function!", &F);
360
Chris Lattner80105dd2006-05-19 21:25:17 +0000361 // Check that this function meets the restrictions on this calling convention.
362 switch (F.getCallingConv()) {
363 default:
364 break;
365 case CallingConv::C:
366 break;
Chris Lattner80105dd2006-05-19 21:25:17 +0000367 case CallingConv::Fast:
368 case CallingConv::Cold:
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000369 case CallingConv::X86_FastCall:
Chris Lattner80105dd2006-05-19 21:25:17 +0000370 Assert1(!F.isVarArg(),
371 "Varargs functions must have C calling conventions!", &F);
372 break;
373 }
374
Chris Lattnerea249242002-04-13 22:48:46 +0000375 // Check that the argument values match the function type for this function...
Chris Lattner69da5cf2002-10-13 20:57:00 +0000376 unsigned i = 0;
Chris Lattnere3cbe032006-12-16 02:25:35 +0000377 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
378 I != E; ++I, ++i) {
Chris Lattner69da5cf2002-10-13 20:57:00 +0000379 Assert2(I->getType() == FT->getParamType(i),
380 "Argument value does not match function argument type!",
381 I, FT->getParamType(i));
Chris Lattner7c277b32004-06-03 06:38:43 +0000382 // Make sure no aggregates are passed by value.
Misha Brukmanfd939082005-04-21 23:48:37 +0000383 Assert1(I->getType()->isFirstClassType(),
Chris Lattner7c277b32004-06-03 06:38:43 +0000384 "Functions cannot take aggregates as arguments by value!", I);
385 }
Chris Lattnerea249242002-04-13 22:48:46 +0000386
Reid Spencer5cbf9852007-01-30 20:08:39 +0000387 if (!F.isDeclaration()) {
Chris Lattner4d17caa2006-12-13 04:45:46 +0000388 // Verify that this function (which has a body) is not named "llvm.*". It
389 // is not legal to define intrinsics.
390 if (F.getName().size() >= 5)
391 Assert1(F.getName().substr(0, 5) != "llvm.",
392 "llvm intrinsics cannot be defined!", &F);
393
Chris Lattner69da5cf2002-10-13 20:57:00 +0000394 // Check the entry node
Chris Lattner02a3be02003-09-20 14:39:18 +0000395 BasicBlock *Entry = &F.getEntryBlock();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000396 Assert1(pred_begin(Entry) == pred_end(Entry),
397 "Entry block to function must not have predecessors!", Entry);
398 }
Chris Lattner44d5bd92002-02-20 17:55:43 +0000399}
400
401
Chris Lattnerd231fc32002-04-18 20:37:37 +0000402// verifyBasicBlock - Verify that a basic block is well formed...
403//
Chris Lattner24e845f2002-06-25 15:56:27 +0000404void Verifier::visitBasicBlock(BasicBlock &BB) {
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000405 InstsInThisBlock.clear();
406
Alkis Evlogimenos4f4cf992004-12-04 02:30:42 +0000407 // Ensure that basic blocks have terminators!
408 Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
409
Chris Lattnerbede31f2003-10-05 17:44:18 +0000410 // Check constraints that this basic block imposes on all of the PHI nodes in
411 // it.
412 if (isa<PHINode>(BB.front())) {
Chris Lattnerf8edb622007-02-10 08:33:11 +0000413 SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB));
414 SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
Chris Lattnerbede31f2003-10-05 17:44:18 +0000415 std::sort(Preds.begin(), Preds.end());
Misha Brukmanfd939082005-04-21 23:48:37 +0000416 PHINode *PN;
Chris Lattnerc70a5092004-06-05 17:44:48 +0000417 for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) {
Chris Lattnerbede31f2003-10-05 17:44:18 +0000418
419 // Ensure that PHI nodes have at least one entry!
420 Assert1(PN->getNumIncomingValues() != 0,
421 "PHI nodes must have at least one entry. If the block is dead, "
422 "the PHI should be removed!", PN);
Brian Gaeke2fea9ad2004-05-17 21:15:18 +0000423 Assert1(PN->getNumIncomingValues() == Preds.size(),
424 "PHINode should have one entry for each predecessor of its "
425 "parent basic block!", PN);
Misha Brukmanfd939082005-04-21 23:48:37 +0000426
Chris Lattnerbede31f2003-10-05 17:44:18 +0000427 // Get and sort all incoming values in the PHI node...
Chris Lattnerf8edb622007-02-10 08:33:11 +0000428 Values.clear();
Chris Lattnerbede31f2003-10-05 17:44:18 +0000429 Values.reserve(PN->getNumIncomingValues());
430 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
431 Values.push_back(std::make_pair(PN->getIncomingBlock(i),
432 PN->getIncomingValue(i)));
433 std::sort(Values.begin(), Values.end());
Misha Brukmanfd939082005-04-21 23:48:37 +0000434
Chris Lattnerbede31f2003-10-05 17:44:18 +0000435 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
436 // Check to make sure that if there is more than one entry for a
437 // particular basic block in this PHI node, that the incoming values are
438 // all identical.
439 //
440 Assert4(i == 0 || Values[i].first != Values[i-1].first ||
441 Values[i].second == Values[i-1].second,
442 "PHI node has multiple entries for the same basic block with "
443 "different incoming values!", PN, Values[i].first,
444 Values[i].second, Values[i-1].second);
Misha Brukmanfd939082005-04-21 23:48:37 +0000445
Chris Lattnerbede31f2003-10-05 17:44:18 +0000446 // Check to make sure that the predecessors and PHI node entries are
447 // matched up.
448 Assert3(Values[i].first == Preds[i],
449 "PHI node entries do not match predecessors!", PN,
Misha Brukmanfd939082005-04-21 23:48:37 +0000450 Values[i].first, Preds[i]);
Chris Lattnerbede31f2003-10-05 17:44:18 +0000451 }
452 }
453 }
Chris Lattner24e845f2002-06-25 15:56:27 +0000454}
Chris Lattneracd3cae2002-03-15 20:25:09 +0000455
Chris Lattner24e845f2002-06-25 15:56:27 +0000456void Verifier::visitTerminatorInst(TerminatorInst &I) {
457 // Ensure that terminators only exist at the end of the basic block.
458 Assert1(&I == I.getParent()->getTerminator(),
459 "Terminator found in the middle of a basic block!", I.getParent());
Chris Lattner3535c9b2002-07-18 00:13:42 +0000460 visitInstruction(I);
Chris Lattner24e845f2002-06-25 15:56:27 +0000461}
462
463void Verifier::visitReturnInst(ReturnInst &RI) {
464 Function *F = RI.getParent()->getParent();
465 if (RI.getNumOperands() == 0)
Alkis Evlogimenos8b42b432004-12-04 01:25:06 +0000466 Assert2(F->getReturnType() == Type::VoidTy,
467 "Found return instr that returns void in Function of non-void "
468 "return type!", &RI, F->getReturnType());
Chris Lattner24e845f2002-06-25 15:56:27 +0000469 else
470 Assert2(F->getReturnType() == RI.getOperand(0)->getType(),
471 "Function return type does not match operand "
472 "type of return inst!", &RI, F->getReturnType());
473
Misha Brukman5560c9d2003-08-18 14:43:39 +0000474 // Check to make sure that the return value has necessary properties for
Chris Lattner24e845f2002-06-25 15:56:27 +0000475 // terminators...
476 visitTerminatorInst(RI);
Chris Lattner44d5bd92002-02-20 17:55:43 +0000477}
478
Chris Lattner0f9e9d02004-05-21 16:47:21 +0000479void Verifier::visitSwitchInst(SwitchInst &SI) {
480 // Check to make sure that all of the constants in the switch instruction
481 // have the same type as the switched-on value.
482 const Type *SwitchTy = SI.getCondition()->getType();
483 for (unsigned i = 1, e = SI.getNumCases(); i != e; ++i)
484 Assert1(SI.getCaseValue(i)->getType() == SwitchTy,
485 "Switch constants must all be same type as switch value!", &SI);
486
487 visitTerminatorInst(SI);
488}
489
Chris Lattner230c1a72004-03-12 05:54:31 +0000490void Verifier::visitSelectInst(SelectInst &SI) {
Reid Spencer4fe16d62007-01-11 18:21:29 +0000491 Assert1(SI.getCondition()->getType() == Type::Int1Ty,
Chris Lattner230c1a72004-03-12 05:54:31 +0000492 "Select condition type must be bool!", &SI);
493 Assert1(SI.getTrueValue()->getType() == SI.getFalseValue()->getType(),
494 "Select values must have identical types!", &SI);
495 Assert1(SI.getTrueValue()->getType() == SI.getType(),
496 "Select values must have same type as select instruction!", &SI);
Chris Lattner0030e6c2004-09-29 21:19:28 +0000497 visitInstruction(SI);
Chris Lattner230c1a72004-03-12 05:54:31 +0000498}
499
500
Misha Brukmanab5c6002004-03-02 00:22:19 +0000501/// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
502/// a pass, if any exist, it's an error.
503///
Chris Lattner627079d2002-11-21 16:54:22 +0000504void Verifier::visitUserOp1(Instruction &I) {
Chris Lattner536a9d52006-03-31 04:46:47 +0000505 Assert1(0, "User-defined operators should not live outside of a pass!", &I);
Chris Lattner627079d2002-11-21 16:54:22 +0000506}
Chris Lattnerd231fc32002-04-18 20:37:37 +0000507
Reid Spencer3da59db2006-11-27 01:05:10 +0000508void Verifier::visitTruncInst(TruncInst &I) {
509 // Get the source and destination types
510 const Type *SrcTy = I.getOperand(0)->getType();
511 const Type *DestTy = I.getType();
512
513 // Get the size of the types in bits, we'll need this later
514 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
515 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
516
Chris Lattner42a75512007-01-15 02:27:26 +0000517 Assert1(SrcTy->isInteger(), "Trunc only operates on integer", &I);
518 Assert1(DestTy->isInteger(), "Trunc only produces integer", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000519 Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I);
520
521 visitInstruction(I);
522}
523
524void Verifier::visitZExtInst(ZExtInst &I) {
525 // Get the source and destination types
526 const Type *SrcTy = I.getOperand(0)->getType();
527 const Type *DestTy = I.getType();
528
529 // Get the size of the types in bits, we'll need this later
Chris Lattner42a75512007-01-15 02:27:26 +0000530 Assert1(SrcTy->isInteger(), "ZExt only operates on integer", &I);
531 Assert1(DestTy->isInteger(), "ZExt only produces an integer", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000532 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
533 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
534
Reid Spencer3da59db2006-11-27 01:05:10 +0000535 Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I);
536
537 visitInstruction(I);
538}
539
540void Verifier::visitSExtInst(SExtInst &I) {
541 // Get the source and destination types
542 const Type *SrcTy = I.getOperand(0)->getType();
543 const Type *DestTy = I.getType();
544
545 // Get the size of the types in bits, we'll need this later
546 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
547 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
548
Chris Lattner42a75512007-01-15 02:27:26 +0000549 Assert1(SrcTy->isInteger(), "SExt only operates on integer", &I);
550 Assert1(DestTy->isInteger(), "SExt only produces an integer", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000551 Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I);
552
553 visitInstruction(I);
554}
555
556void Verifier::visitFPTruncInst(FPTruncInst &I) {
557 // Get the source and destination types
558 const Type *SrcTy = I.getOperand(0)->getType();
559 const Type *DestTy = I.getType();
560 // Get the size of the types in bits, we'll need this later
561 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
562 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
563
564 Assert1(SrcTy->isFloatingPoint(),"FPTrunc only operates on FP", &I);
565 Assert1(DestTy->isFloatingPoint(),"FPTrunc only produces an FP", &I);
566 Assert1(SrcBitSize > DestBitSize,"DestTy too big for FPTrunc", &I);
567
568 visitInstruction(I);
569}
570
571void Verifier::visitFPExtInst(FPExtInst &I) {
572 // Get the source and destination types
573 const Type *SrcTy = I.getOperand(0)->getType();
574 const Type *DestTy = I.getType();
575
576 // Get the size of the types in bits, we'll need this later
577 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
578 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
579
580 Assert1(SrcTy->isFloatingPoint(),"FPExt only operates on FP", &I);
581 Assert1(DestTy->isFloatingPoint(),"FPExt only produces an FP", &I);
582 Assert1(SrcBitSize < DestBitSize,"DestTy too small for FPExt", &I);
583
584 visitInstruction(I);
585}
586
587void Verifier::visitUIToFPInst(UIToFPInst &I) {
588 // Get the source and destination types
589 const Type *SrcTy = I.getOperand(0)->getType();
590 const Type *DestTy = I.getType();
591
Chris Lattner42a75512007-01-15 02:27:26 +0000592 Assert1(SrcTy->isInteger(),"UInt2FP source must be integral", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000593 Assert1(DestTy->isFloatingPoint(),"UInt2FP result must be FP", &I);
594
595 visitInstruction(I);
596}
597
598void Verifier::visitSIToFPInst(SIToFPInst &I) {
599 // Get the source and destination types
600 const Type *SrcTy = I.getOperand(0)->getType();
601 const Type *DestTy = I.getType();
602
Chris Lattner42a75512007-01-15 02:27:26 +0000603 Assert1(SrcTy->isInteger(),"SInt2FP source must be integral", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000604 Assert1(DestTy->isFloatingPoint(),"SInt2FP result must be FP", &I);
605
606 visitInstruction(I);
607}
608
609void Verifier::visitFPToUIInst(FPToUIInst &I) {
610 // Get the source and destination types
611 const Type *SrcTy = I.getOperand(0)->getType();
612 const Type *DestTy = I.getType();
613
614 Assert1(SrcTy->isFloatingPoint(),"FP2UInt source must be FP", &I);
Chris Lattner42a75512007-01-15 02:27:26 +0000615 Assert1(DestTy->isInteger(),"FP2UInt result must be integral", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000616
617 visitInstruction(I);
618}
619
620void Verifier::visitFPToSIInst(FPToSIInst &I) {
621 // Get the source and destination types
622 const Type *SrcTy = I.getOperand(0)->getType();
623 const Type *DestTy = I.getType();
624
625 Assert1(SrcTy->isFloatingPoint(),"FPToSI source must be FP", &I);
Chris Lattner42a75512007-01-15 02:27:26 +0000626 Assert1(DestTy->isInteger(),"FP2ToI result must be integral", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000627
628 visitInstruction(I);
629}
630
631void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
632 // Get the source and destination types
633 const Type *SrcTy = I.getOperand(0)->getType();
634 const Type *DestTy = I.getType();
635
636 Assert1(isa<PointerType>(SrcTy), "PtrToInt source must be pointer", &I);
Chris Lattner42a75512007-01-15 02:27:26 +0000637 Assert1(DestTy->isInteger(), "PtrToInt result must be integral", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000638
639 visitInstruction(I);
640}
641
642void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
643 // Get the source and destination types
644 const Type *SrcTy = I.getOperand(0)->getType();
645 const Type *DestTy = I.getType();
646
Chris Lattner42a75512007-01-15 02:27:26 +0000647 Assert1(SrcTy->isInteger(), "IntToPtr source must be an integral", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000648 Assert1(isa<PointerType>(DestTy), "IntToPtr result must be a pointer",&I);
649
650 visitInstruction(I);
651}
652
653void Verifier::visitBitCastInst(BitCastInst &I) {
654 // Get the source and destination types
655 const Type *SrcTy = I.getOperand(0)->getType();
656 const Type *DestTy = I.getType();
657
658 // Get the size of the types in bits, we'll need this later
659 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
660 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
661
662 // BitCast implies a no-op cast of type only. No bits change.
663 // However, you can't cast pointers to anything but pointers.
664 Assert1(isa<PointerType>(DestTy) == isa<PointerType>(DestTy),
665 "Bitcast requires both operands to be pointer or neither", &I);
666 Assert1(SrcBitSize == DestBitSize, "Bitcast requies types of same width", &I);
667
668 visitInstruction(I);
669}
670
Misha Brukmanab5c6002004-03-02 00:22:19 +0000671/// visitPHINode - Ensure that a PHI node is well formed.
672///
Chris Lattner24e845f2002-06-25 15:56:27 +0000673void Verifier::visitPHINode(PHINode &PN) {
674 // Ensure that the PHI nodes are all grouped together at the top of the block.
675 // This can be tested by checking whether the instruction before this is
Misha Brukman6b634522003-10-10 17:54:14 +0000676 // either nonexistent (because this is begin()) or is a PHI node. If not,
Chris Lattner24e845f2002-06-25 15:56:27 +0000677 // then there is some other instruction before a PHI.
Chris Lattner4d8c16f2007-04-17 17:36:12 +0000678 Assert2(&PN == &PN.getParent()->front() ||
679 isa<PHINode>(--BasicBlock::iterator(&PN)),
Chris Lattner24e845f2002-06-25 15:56:27 +0000680 "PHI nodes not grouped at top of basic block!",
681 &PN, PN.getParent());
682
Chris Lattner579de712003-11-12 07:13:37 +0000683 // Check that all of the operands of the PHI node have the same type as the
684 // result.
685 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i)
686 Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
687 "PHI node operands are not the same type as the result!", &PN);
688
Chris Lattnerbede31f2003-10-05 17:44:18 +0000689 // All other PHI node constraints are checked in the visitBasicBlock method.
Chris Lattnerd231fc32002-04-18 20:37:37 +0000690
691 visitInstruction(PN);
692}
693
Chris Lattner24e845f2002-06-25 15:56:27 +0000694void Verifier::visitCallInst(CallInst &CI) {
695 Assert1(isa<PointerType>(CI.getOperand(0)->getType()),
696 "Called function must be a pointer!", &CI);
697 const PointerType *FPTy = cast<PointerType>(CI.getOperand(0)->getType());
Chris Lattnerefdd0a22002-04-18 22:11:52 +0000698 Assert1(isa<FunctionType>(FPTy->getElementType()),
Chris Lattner24e845f2002-06-25 15:56:27 +0000699 "Called function is not pointer to function type!", &CI);
Chris Lattner56732fb2002-05-08 19:49:50 +0000700
Chris Lattner24e845f2002-06-25 15:56:27 +0000701 const FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
Chris Lattner56732fb2002-05-08 19:49:50 +0000702
703 // Verify that the correct number of arguments are being passed
704 if (FTy->isVarArg())
Chris Lattner24e845f2002-06-25 15:56:27 +0000705 Assert1(CI.getNumOperands()-1 >= FTy->getNumParams(),
706 "Called function requires more parameters than were provided!",&CI);
Chris Lattner56732fb2002-05-08 19:49:50 +0000707 else
Chris Lattner24e845f2002-06-25 15:56:27 +0000708 Assert1(CI.getNumOperands()-1 == FTy->getNumParams(),
709 "Incorrect number of arguments passed to called function!", &CI);
Chris Lattner56732fb2002-05-08 19:49:50 +0000710
711 // Verify that all arguments to the call match the function type...
712 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Chris Lattner2a116532004-02-24 22:06:07 +0000713 Assert3(CI.getOperand(i+1)->getType() == FTy->getParamType(i),
Chris Lattner56732fb2002-05-08 19:49:50 +0000714 "Call parameter type does not match function signature!",
Chris Lattner2a116532004-02-24 22:06:07 +0000715 CI.getOperand(i+1), FTy->getParamType(i), &CI);
Chris Lattner3535c9b2002-07-18 00:13:42 +0000716
Chris Lattnerdd035d12003-05-08 03:47:33 +0000717 if (Function *F = CI.getCalledFunction())
Brian Gaeked0fde302003-11-11 22:41:34 +0000718 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
Chris Lattnerdd035d12003-05-08 03:47:33 +0000719 visitIntrinsicFunctionCall(ID, CI);
720
Chris Lattner3535c9b2002-07-18 00:13:42 +0000721 visitInstruction(CI);
Chris Lattnerefdd0a22002-04-18 22:11:52 +0000722}
Chris Lattnerd231fc32002-04-18 20:37:37 +0000723
Misha Brukmanab5c6002004-03-02 00:22:19 +0000724/// visitBinaryOperator - Check that both arguments to the binary operator are
725/// of the same type!
726///
Chris Lattner24e845f2002-06-25 15:56:27 +0000727void Verifier::visitBinaryOperator(BinaryOperator &B) {
Chris Lattner1a143ae2002-09-09 20:26:04 +0000728 Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
729 "Both operands to a binary operator are not of the same type!", &B);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000730
Reid Spencer832254e2007-02-02 02:16:23 +0000731 switch (B.getOpcode()) {
Chris Lattner1a143ae2002-09-09 20:26:04 +0000732 // Check that logical operators are only used with integral operands.
Reid Spencer832254e2007-02-02 02:16:23 +0000733 case Instruction::And:
734 case Instruction::Or:
735 case Instruction::Xor:
Chris Lattner42a75512007-01-15 02:27:26 +0000736 Assert1(B.getType()->isInteger() ||
Reid Spencer9d6565a2007-02-15 02:26:10 +0000737 (isa<VectorType>(B.getType()) &&
738 cast<VectorType>(B.getType())->getElementType()->isInteger()),
Chris Lattner1a143ae2002-09-09 20:26:04 +0000739 "Logical operators only work with integral types!", &B);
740 Assert1(B.getType() == B.getOperand(0)->getType(),
741 "Logical operators must have same type for operands and result!",
742 &B);
Reid Spencer832254e2007-02-02 02:16:23 +0000743 break;
744 case Instruction::Shl:
745 case Instruction::LShr:
746 case Instruction::AShr:
747 Assert1(B.getType()->isInteger(),
748 "Shift must return an integer result!", &B);
749 Assert1(B.getType() == B.getOperand(0)->getType(),
750 "Shift return type must be same as operands!", &B);
751 /* FALL THROUGH */
752 default:
Chris Lattner1a143ae2002-09-09 20:26:04 +0000753 // Arithmetic operators only work on integer or fp values
754 Assert1(B.getType() == B.getOperand(0)->getType(),
755 "Arithmetic operators must have same type for operands and result!",
756 &B);
Chris Lattner42a75512007-01-15 02:27:26 +0000757 Assert1(B.getType()->isInteger() || B.getType()->isFloatingPoint() ||
Reid Spencer9d6565a2007-02-15 02:26:10 +0000758 isa<VectorType>(B.getType()),
Reid Spencerac9dcb92007-02-15 03:39:18 +0000759 "Arithmetic operators must have integer, fp, or vector type!", &B);
Reid Spencer832254e2007-02-02 02:16:23 +0000760 break;
Chris Lattner1a143ae2002-09-09 20:26:04 +0000761 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000762
Chris Lattnerd231fc32002-04-18 20:37:37 +0000763 visitInstruction(B);
764}
765
Reid Spencer45fb3f32006-11-20 01:22:35 +0000766void Verifier::visitICmpInst(ICmpInst& IC) {
767 // Check that the operands are the same type
768 const Type* Op0Ty = IC.getOperand(0)->getType();
769 const Type* Op1Ty = IC.getOperand(1)->getType();
770 Assert1(Op0Ty == Op1Ty,
771 "Both operands to ICmp instruction are not of the same type!", &IC);
772 // Check that the operands are the right type
Chris Lattner42a75512007-01-15 02:27:26 +0000773 Assert1(Op0Ty->isInteger() || isa<PointerType>(Op0Ty),
Reid Spencer45fb3f32006-11-20 01:22:35 +0000774 "Invalid operand types for ICmp instruction", &IC);
775 visitInstruction(IC);
776}
777
778void Verifier::visitFCmpInst(FCmpInst& FC) {
779 // Check that the operands are the same type
780 const Type* Op0Ty = FC.getOperand(0)->getType();
781 const Type* Op1Ty = FC.getOperand(1)->getType();
782 Assert1(Op0Ty == Op1Ty,
783 "Both operands to FCmp instruction are not of the same type!", &FC);
784 // Check that the operands are the right type
Reid Spenceraffaf072007-01-04 05:22:18 +0000785 Assert1(Op0Ty->isFloatingPoint(),
Reid Spencer45fb3f32006-11-20 01:22:35 +0000786 "Invalid operand types for FCmp instruction", &FC);
787 visitInstruction(FC);
788}
789
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000790void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
Chris Lattner1cbe05b2006-04-08 04:07:52 +0000791 Assert1(ExtractElementInst::isValidOperands(EI.getOperand(0),
792 EI.getOperand(1)),
793 "Invalid extractelement operands!", &EI);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000794 visitInstruction(EI);
795}
796
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000797void Verifier::visitInsertElementInst(InsertElementInst &IE) {
Chris Lattner1cbe05b2006-04-08 04:07:52 +0000798 Assert1(InsertElementInst::isValidOperands(IE.getOperand(0),
799 IE.getOperand(1),
800 IE.getOperand(2)),
801 "Invalid insertelement operands!", &IE);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000802 visitInstruction(IE);
803}
804
Chris Lattner00f10232006-04-08 01:18:18 +0000805void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
806 Assert1(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
807 SV.getOperand(2)),
808 "Invalid shufflevector operands!", &SV);
809 Assert1(SV.getType() == SV.getOperand(0)->getType(),
810 "Result of shufflevector must match first operand type!", &SV);
811
812 // Check to see if Mask is valid.
Reid Spencer9d6565a2007-02-15 02:26:10 +0000813 if (const ConstantVector *MV = dyn_cast<ConstantVector>(SV.getOperand(2))) {
Chris Lattner00f10232006-04-08 01:18:18 +0000814 for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) {
Reid Spencerb83eb642006-10-20 07:07:24 +0000815 Assert1(isa<ConstantInt>(MV->getOperand(i)) ||
Chris Lattner00f10232006-04-08 01:18:18 +0000816 isa<UndefValue>(MV->getOperand(i)),
817 "Invalid shufflevector shuffle mask!", &SV);
818 }
819 } else {
820 Assert1(isa<UndefValue>(SV.getOperand(2)) ||
821 isa<ConstantAggregateZero>(SV.getOperand(2)),
822 "Invalid shufflevector shuffle mask!", &SV);
823 }
824
825 visitInstruction(SV);
826}
827
Chris Lattner24e845f2002-06-25 15:56:27 +0000828void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Chris Lattner8552fae2007-02-10 08:30:29 +0000829 SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end());
Chris Lattnercc63f1c2002-08-22 23:37:20 +0000830 const Type *ElTy =
831 GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(),
Chris Lattner8552fae2007-02-10 08:30:29 +0000832 &Idxs[0], Idxs.size(), true);
Chris Lattner24e845f2002-06-25 15:56:27 +0000833 Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
Chris Lattner8552fae2007-02-10 08:30:29 +0000834 Assert2(isa<PointerType>(GEP.getType()) &&
835 cast<PointerType>(GEP.getType())->getElementType() == ElTy,
Chris Lattner24e845f2002-06-25 15:56:27 +0000836 "GEP is not of right type for indices!", &GEP, ElTy);
Chris Lattnera00409e2002-04-24 19:12:21 +0000837 visitInstruction(GEP);
838}
839
Chris Lattner24e845f2002-06-25 15:56:27 +0000840void Verifier::visitLoadInst(LoadInst &LI) {
Chris Lattner24ea74e2002-08-22 22:49:05 +0000841 const Type *ElTy =
842 cast<PointerType>(LI.getOperand(0)->getType())->getElementType();
Chris Lattner24e845f2002-06-25 15:56:27 +0000843 Assert2(ElTy == LI.getType(),
Chris Lattner7334f2e2003-11-21 17:06:29 +0000844 "Load result type does not match pointer operand type!", &LI, ElTy);
Chris Lattnera00409e2002-04-24 19:12:21 +0000845 visitInstruction(LI);
846}
847
Chris Lattner24e845f2002-06-25 15:56:27 +0000848void Verifier::visitStoreInst(StoreInst &SI) {
Chris Lattner24ea74e2002-08-22 22:49:05 +0000849 const Type *ElTy =
850 cast<PointerType>(SI.getOperand(1)->getType())->getElementType();
Chris Lattner24e845f2002-06-25 15:56:27 +0000851 Assert2(ElTy == SI.getOperand(0)->getType(),
Chris Lattner7334f2e2003-11-21 17:06:29 +0000852 "Stored value type does not match pointer operand type!", &SI, ElTy);
Chris Lattnera00409e2002-04-24 19:12:21 +0000853 visitInstruction(SI);
854}
855
Chris Lattnerd231fc32002-04-18 20:37:37 +0000856
Misha Brukmanab5c6002004-03-02 00:22:19 +0000857/// verifyInstruction - Verify that an instruction is well formed.
858///
Chris Lattner24e845f2002-06-25 15:56:27 +0000859void Verifier::visitInstruction(Instruction &I) {
Misha Brukmanfd939082005-04-21 23:48:37 +0000860 BasicBlock *BB = I.getParent();
Chris Lattner1a143ae2002-09-09 20:26:04 +0000861 Assert1(BB, "Instruction not embedded in basic block!", &I);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000862
Chris Lattnerbede31f2003-10-05 17:44:18 +0000863 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
864 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
865 UI != UE; ++UI)
Chris Lattner08092532004-02-27 17:28:25 +0000866 Assert1(*UI != (User*)&I ||
Chris Lattner0b2192c2006-01-12 06:17:59 +0000867 !EF->dominates(&BB->getParent()->getEntryBlock(), BB),
Chris Lattnerbede31f2003-10-05 17:44:18 +0000868 "Only PHI nodes may reference their own value!", &I);
869 }
870
871 // Check that void typed values don't have names
872 Assert1(I.getType() != Type::VoidTy || !I.hasName(),
873 "Instruction has a name, but provides a void value!", &I);
874
Chris Lattner944cfaf2004-03-29 00:29:36 +0000875 // Check that the return value of the instruction is either void or a legal
876 // value type.
877 Assert1(I.getType() == Type::VoidTy || I.getType()->isFirstClassType(),
878 "Instruction returns a non-scalar type!", &I);
879
Chris Lattnerd231fc32002-04-18 20:37:37 +0000880 // Check that all uses of the instruction, if they are instructions
881 // themselves, actually have parent basic blocks. If the use is not an
882 // instruction, it is an error!
Chris Lattner24e845f2002-06-25 15:56:27 +0000883 for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattnerd231fc32002-04-18 20:37:37 +0000884 UI != UE; ++UI) {
885 Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!",
886 *UI);
Chris Lattnerefdd0a22002-04-18 22:11:52 +0000887 Instruction *Used = cast<Instruction>(*UI);
888 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
Chris Lattner24e845f2002-06-25 15:56:27 +0000889 " embeded in a basic block!", &I, Used);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000890 }
891
Chris Lattnerbede31f2003-10-05 17:44:18 +0000892 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Chris Lattneraab18202005-02-24 16:58:29 +0000893 Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I);
Chris Lattnerf4ea9212006-07-11 20:29:49 +0000894
895 // Check to make sure that only first-class-values are operands to
896 // instructions.
897 Assert1(I.getOperand(i)->getType()->isFirstClassType(),
898 "Instruction operands must be first-class values!", &I);
899
Chris Lattner59c35692004-03-14 03:23:54 +0000900 if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
Chris Lattnerf4ea9212006-07-11 20:29:49 +0000901 // Check to make sure that the "address of" an intrinsic function is never
902 // taken.
Chris Lattnerdd035d12003-05-08 03:47:33 +0000903 Assert1(!F->isIntrinsic() || (i == 0 && isa<CallInst>(I)),
904 "Cannot take the address of an intrinsic!", &I);
Chris Lattner19b6dcd2007-04-20 21:48:08 +0000905 Assert1(F->getParent() == Mod, "Referencing function in another module!",
906 &I);
Chris Lattner59c35692004-03-14 03:23:54 +0000907 } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
908 Assert1(OpBB->getParent() == BB->getParent(),
909 "Referring to a basic block in another function!", &I);
910 } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
911 Assert1(OpArg->getParent() == BB->getParent(),
912 "Referring to an argument in another function!", &I);
Chris Lattner19b6dcd2007-04-20 21:48:08 +0000913 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
914 Assert1(GV->getParent() == Mod, "Referencing global in another module!",
915 &I);
Chris Lattner59c35692004-03-14 03:23:54 +0000916 } else if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) {
Chris Lattner30768ac2004-01-14 04:25:59 +0000917 BasicBlock *OpBlock = Op->getParent();
Chris Lattner30768ac2004-01-14 04:25:59 +0000918
Chris Lattnerbede31f2003-10-05 17:44:18 +0000919 // Check that a definition dominates all of its uses.
Chris Lattnerbede31f2003-10-05 17:44:18 +0000920 if (!isa<PHINode>(I)) {
Chris Lattnerc9b07022004-01-14 05:42:52 +0000921 // Invoke results are only usable in the normal destination, not in the
922 // exceptional destination.
Chris Lattnere3cbe032006-12-16 02:25:35 +0000923 if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
Chris Lattnerc9b07022004-01-14 05:42:52 +0000924 OpBlock = II->getNormalDest();
Chris Lattnere3cbe032006-12-16 02:25:35 +0000925
Chris Lattner62d75e72006-12-20 21:20:13 +0000926 Assert2(OpBlock != II->getUnwindDest(),
927 "No uses of invoke possible due to dominance structure!",
928 Op, II);
929
Chris Lattnere3cbe032006-12-16 02:25:35 +0000930 // If the normal successor of an invoke instruction has multiple
931 // predecessors, then the normal edge from the invoke is critical, so
932 // the invoke value can only be live if the destination block
933 // dominates all of it's predecessors (other than the invoke) or if
934 // the invoke value is only used by a phi in the successor.
935 if (!OpBlock->getSinglePredecessor() &&
936 EF->dominates(&BB->getParent()->getEntryBlock(), BB)) {
937 // The first case we allow is if the use is a PHI operand in the
938 // normal block, and if that PHI operand corresponds to the invoke's
939 // block.
940 bool Bad = true;
941 if (PHINode *PN = dyn_cast<PHINode>(&I))
942 if (PN->getParent() == OpBlock &&
943 PN->getIncomingBlock(i/2) == Op->getParent())
944 Bad = false;
945
946 // If it is used by something non-phi, then the other case is that
947 // 'OpBlock' dominates all of its predecessors other than the
948 // invoke. In this case, the invoke value can still be used.
Chris Lattner19591b32006-12-20 19:50:15 +0000949 if (Bad) {
950 Bad = false;
Chris Lattnere3cbe032006-12-16 02:25:35 +0000951 for (pred_iterator PI = pred_begin(OpBlock),
952 E = pred_end(OpBlock); PI != E; ++PI) {
953 if (*PI != II->getParent() && !EF->dominates(OpBlock, *PI)) {
954 Bad = true;
955 break;
956 }
957 }
958 }
Chris Lattner62d75e72006-12-20 21:20:13 +0000959 Assert2(!Bad,
960 "Invoke value defined on critical edge but not dead!", &I,
961 Op);
Chris Lattnere3cbe032006-12-16 02:25:35 +0000962 }
963 } else if (OpBlock == BB) {
Chris Lattnere1f0cf12004-04-16 05:51:47 +0000964 // If they are in the same basic block, make sure that the definition
965 // comes before the use.
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000966 Assert2(InstsInThisBlock.count(Op) ||
Chris Lattner0b2192c2006-01-12 06:17:59 +0000967 !EF->dominates(&BB->getParent()->getEntryBlock(), BB),
Chris Lattnere1f0cf12004-04-16 05:51:47 +0000968 "Instruction does not dominate all uses!", Op, &I);
969 }
Chris Lattnerc9b07022004-01-14 05:42:52 +0000970
Chris Lattnerbede31f2003-10-05 17:44:18 +0000971 // Definition must dominate use unless use is unreachable!
Chris Lattner0b2192c2006-01-12 06:17:59 +0000972 Assert2(EF->dominates(OpBlock, BB) ||
973 !EF->dominates(&BB->getParent()->getEntryBlock(), BB),
Chris Lattnerbede31f2003-10-05 17:44:18 +0000974 "Instruction does not dominate all uses!", Op, &I);
975 } else {
976 // PHI nodes are more difficult than other nodes because they actually
977 // "use" the value in the predecessor basic blocks they correspond to.
978 BasicBlock *PredBB = cast<BasicBlock>(I.getOperand(i+1));
Chris Lattner0b2192c2006-01-12 06:17:59 +0000979 Assert2(EF->dominates(OpBlock, PredBB) ||
980 !EF->dominates(&BB->getParent()->getEntryBlock(), PredBB),
Chris Lattnerbede31f2003-10-05 17:44:18 +0000981 "Instruction does not dominate all uses!", Op, &I);
982 }
Chris Lattner3188b732006-01-26 00:08:45 +0000983 } else if (isa<InlineAsm>(I.getOperand(i))) {
984 Assert1(i == 0 && isa<CallInst>(I),
985 "Cannot take the address of an inline asm!", &I);
Chris Lattnerbede31f2003-10-05 17:44:18 +0000986 }
987 }
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000988 InstsInThisBlock.insert(&I);
Chris Lattnerdd035d12003-05-08 03:47:33 +0000989}
990
991/// visitIntrinsicFunction - Allow intrinsics to be verified in different ways.
Misha Brukmanab5c6002004-03-02 00:22:19 +0000992///
Brian Gaeked0fde302003-11-11 22:41:34 +0000993void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnerdd035d12003-05-08 03:47:33 +0000994 Function *IF = CI.getCalledFunction();
Chris Lattner19b6dcd2007-04-20 21:48:08 +0000995 Assert1(IF->isDeclaration(), "Intrinsic functions should never be defined!",
996 IF);
Chris Lattner3b816b72006-03-09 22:06:04 +0000997
998#define GET_INTRINSIC_VERIFIER
999#include "llvm/Intrinsics.gen"
1000#undef GET_INTRINSIC_VERIFIER
Chris Lattnerd231fc32002-04-18 20:37:37 +00001001}
1002
Chris Lattner536a9d52006-03-31 04:46:47 +00001003/// VerifyIntrinsicPrototype - TableGen emits calls to this function into
1004/// Intrinsics.gen. This implements a little state machine that verifies the
1005/// prototype of intrinsics.
Reid Spencer559d77a2007-04-01 07:22:57 +00001006void Verifier::VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F, ...) {
Chris Lattner536a9d52006-03-31 04:46:47 +00001007 va_list VA;
1008 va_start(VA, F);
1009
1010 const FunctionType *FTy = F->getFunctionType();
1011
Reid Spencer559d77a2007-04-01 07:22:57 +00001012 // For overloaded intrinsics, the Suffix of the function name must match the
1013 // types of the arguments. This variable keeps track of the expected
1014 // suffix, to be checked at the end.
1015 std::string Suffix;
1016
Chris Lattner536a9d52006-03-31 04:46:47 +00001017 // Note that "arg#0" is the return type.
1018 for (unsigned ArgNo = 0; 1; ++ArgNo) {
1019 int TypeID = va_arg(VA, int);
1020
Jim Laskeyba4cc092007-02-06 18:02:54 +00001021 if (TypeID == -2) {
1022 break;
1023 }
1024
Chris Lattner536a9d52006-03-31 04:46:47 +00001025 if (TypeID == -1) {
1026 if (ArgNo != FTy->getNumParams()+1)
1027 CheckFailed("Intrinsic prototype has too many arguments!", F);
1028 break;
1029 }
1030
1031 if (ArgNo == FTy->getNumParams()+1) {
1032 CheckFailed("Intrinsic prototype has too few arguments!", F);
1033 break;
1034 }
1035
1036 const Type *Ty;
Reid Spencer559d77a2007-04-01 07:22:57 +00001037 if (ArgNo == 0)
Chris Lattner536a9d52006-03-31 04:46:47 +00001038 Ty = FTy->getReturnType();
1039 else
1040 Ty = FTy->getParamType(ArgNo-1);
1041
Reid Spencera54b7cb2007-01-12 07:05:14 +00001042 if (TypeID != Ty->getTypeID()) {
Chris Lattner536a9d52006-03-31 04:46:47 +00001043 if (ArgNo == 0)
1044 CheckFailed("Intrinsic prototype has incorrect result type!", F);
1045 else
1046 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is wrong!",F);
1047 break;
1048 }
1049
Reid Spencera54b7cb2007-01-12 07:05:14 +00001050 if (TypeID == Type::IntegerTyID) {
Reid Spencer559d77a2007-04-01 07:22:57 +00001051 unsigned ExpectedBits = (unsigned) va_arg(VA, int);
1052 unsigned GotBits = cast<IntegerType>(Ty)->getBitWidth();
1053 if (ExpectedBits == 0) {
1054 Suffix += ".i" + utostr(GotBits);
1055 } else if (GotBits != ExpectedBits) {
1056 std::string bitmsg = " Expected " + utostr(ExpectedBits) + " but got "+
Reid Spencera54b7cb2007-01-12 07:05:14 +00001057 utostr(GotBits) + " bits.";
1058 if (ArgNo == 0)
1059 CheckFailed("Intrinsic prototype has incorrect integer result width!"
1060 + bitmsg, F);
1061 else
1062 CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " has "
1063 "incorrect integer width!" + bitmsg, F);
Chris Lattner536a9d52006-03-31 04:46:47 +00001064 break;
1065 }
Reid Spencer559d77a2007-04-01 07:22:57 +00001066 // Check some constraints on various intrinsics.
1067 switch (ID) {
1068 default: break; // Not everything needs to be checked.
1069 case Intrinsic::bswap:
1070 if (GotBits < 16 || GotBits % 16 != 0)
1071 CheckFailed("Intrinsic requires even byte width argument", F);
Reid Spenceraddd11d2007-04-04 23:48:25 +00001072 /* FALL THROUGH */
Reid Spencerf75b8742007-04-12 02:48:46 +00001073 case Intrinsic::part_set:
Chris Lattner466b9bd2007-04-10 03:18:19 +00001074 case Intrinsic::part_select:
Reid Spencer559d77a2007-04-01 07:22:57 +00001075 if (ArgNo == 1) {
1076 unsigned ResultBits =
1077 cast<IntegerType>(FTy->getReturnType())->getBitWidth();
1078 if (GotBits != ResultBits)
Reid Spencerf75b8742007-04-12 02:48:46 +00001079 CheckFailed("Intrinsic requires the bit widths of the first "
1080 "parameter and the result to match", F);
Reid Spencer559d77a2007-04-01 07:22:57 +00001081 }
1082 break;
1083 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00001084 } else if (TypeID == Type::VectorTyID) {
Reid Spencera54b7cb2007-01-12 07:05:14 +00001085 // If this is a packed argument, verify the number and type of elements.
Reid Spencer9d6565a2007-02-15 02:26:10 +00001086 const VectorType *PTy = cast<VectorType>(Ty);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001087 int ElemTy = va_arg(VA, int);
1088 if (ElemTy != PTy->getElementType()->getTypeID()) {
1089 CheckFailed("Intrinsic prototype has incorrect vector element type!",
1090 F);
1091 break;
1092 }
1093 if (ElemTy == Type::IntegerTyID) {
1094 unsigned NumBits = (unsigned)va_arg(VA, int);
1095 unsigned ExpectedBits =
1096 cast<IntegerType>(PTy->getElementType())->getBitWidth();
1097 if (NumBits != ExpectedBits) {
1098 CheckFailed("Intrinsic prototype has incorrect vector element type!",
1099 F);
1100 break;
1101 }
1102 }
Chris Lattner536a9d52006-03-31 04:46:47 +00001103 if ((unsigned)va_arg(VA, int) != PTy->getNumElements()) {
1104 CheckFailed("Intrinsic prototype has incorrect number of "
1105 "vector elements!",F);
Reid Spencera54b7cb2007-01-12 07:05:14 +00001106 break;
Chris Lattner536a9d52006-03-31 04:46:47 +00001107 }
1108 }
1109 }
1110
1111 va_end(VA);
Reid Spencer559d77a2007-04-01 07:22:57 +00001112
1113 // If we computed a Suffix then the intrinsic is overloaded and we need to
1114 // make sure that the name of the function is correct. We add the suffix to
1115 // the name of the intrinsic and compare against the given function name. If
1116 // they are not the same, the function name is invalid. This ensures that
1117 // overloading of intrinsics uses a sane and consistent naming convention.
1118 if (!Suffix.empty()) {
1119 std::string Name(Intrinsic::getName(ID));
1120 if (Name + Suffix != F->getName())
1121 CheckFailed("Overloaded intrinsic has incorrect suffix: '" +
1122 F->getName().substr(Name.length()) + "'. It should be '" +
1123 Suffix + "'", F);
1124 }
Chris Lattner536a9d52006-03-31 04:46:47 +00001125}
1126
Chris Lattnerd231fc32002-04-18 20:37:37 +00001127
1128//===----------------------------------------------------------------------===//
1129// Implement the public interfaces to this file...
1130//===----------------------------------------------------------------------===//
1131
Chris Lattnerfdc38c42004-04-02 15:45:08 +00001132FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) {
1133 return new Verifier(action);
Chris Lattnerd231fc32002-04-18 20:37:37 +00001134}
1135
Chris Lattner9ce231f2002-08-02 17:37:08 +00001136
Misha Brukmanfd939082005-04-21 23:48:37 +00001137// verifyFunction - Create
Chris Lattnerfdc38c42004-04-02 15:45:08 +00001138bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) {
Chris Lattner2eff8592004-03-14 03:16:15 +00001139 Function &F = const_cast<Function&>(f);
Reid Spencer5cbf9852007-01-30 20:08:39 +00001140 assert(!F.isDeclaration() && "Cannot verify external functions");
Misha Brukmanfd939082005-04-21 23:48:37 +00001141
Chris Lattner2eff8592004-03-14 03:16:15 +00001142 FunctionPassManager FPM(new ExistingModuleProvider(F.getParent()));
Chris Lattnerfdc38c42004-04-02 15:45:08 +00001143 Verifier *V = new Verifier(action);
Chris Lattner2eff8592004-03-14 03:16:15 +00001144 FPM.add(V);
1145 FPM.run(F);
1146 return V->Broken;
Chris Lattner44d5bd92002-02-20 17:55:43 +00001147}
1148
Misha Brukmanab5c6002004-03-02 00:22:19 +00001149/// verifyModule - Check a module for errors, printing messages on stderr.
1150/// Return true if the module is corrupt.
1151///
Chris Lattner05ac92c2006-07-06 18:02:27 +00001152bool llvm::verifyModule(const Module &M, VerifierFailureAction action,
1153 std::string *ErrorInfo) {
Chris Lattner9ce231f2002-08-02 17:37:08 +00001154 PassManager PM;
Chris Lattnerfdc38c42004-04-02 15:45:08 +00001155 Verifier *V = new Verifier(action);
Chris Lattner9ce231f2002-08-02 17:37:08 +00001156 PM.add(V);
1157 PM.run((Module&)M);
Chris Lattner05ac92c2006-07-06 18:02:27 +00001158
1159 if (ErrorInfo && V->Broken)
1160 *ErrorInfo = V->msgs.str();
Chris Lattner9ce231f2002-08-02 17:37:08 +00001161 return V->Broken;
Chris Lattner00950542001-06-06 20:29:01 +00001162}
Reid Spenceraf90b0d2004-05-25 08:53:29 +00001163
1164// vim: sw=2