Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // 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 Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | a5c3dec | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 10 | // This file defines the function verifier interface, that can be used for some |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // sanity checking of input to the system. |
| 12 | // |
Misha Brukman | 5b63631 | 2004-06-24 21:47:35 +0000 | [diff] [blame] | 13 | // Note that this does not provide full `Java style' security and verifications, |
| 14 | // instead it just tries to ensure that code is well-formed. |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | // |
Misha Brukman | 5b63631 | 2004-06-24 21:47:35 +0000 | [diff] [blame] | 16 | // * Both of a binary operator's parameters are of the same type |
Chris Lattner | a00409e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 17 | // * Verify that the indices of mem access instructions match other operands |
Misha Brukman | 5b63631 | 2004-06-24 21:47:35 +0000 | [diff] [blame] | 18 | // * Verify that arithmetic and other things are only performed on first-class |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 19 | // types. Verify that shifts & logicals only happen on integrals f.e. |
Misha Brukman | 5b63631 | 2004-06-24 21:47:35 +0000 | [diff] [blame] | 20 | // * All of the constants in a switch statement are of the correct type |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 21 | // * The code is in valid SSA form |
Misha Brukman | 5b63631 | 2004-06-24 21:47:35 +0000 | [diff] [blame] | 22 | // * It should be illegal to put a label into any other type (like a structure) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 23 | // or to return one. [except constant arrays!] |
Chris Lattner | fdec246 | 2002-03-14 16:53:48 +0000 | [diff] [blame] | 24 | // * Only phi nodes can be self referential: 'add int %0, %0 ; <int>:0' is bad |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 25 | // * PHI nodes must have an entry for each predecessor, with no extras. |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 26 | // * PHI nodes must be the first thing in a basic block, all grouped together |
Chris Lattner | f6ffcb6 | 2002-10-06 21:00:31 +0000 | [diff] [blame] | 27 | // * PHI nodes must have at least one entry |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 28 | // * All basic blocks should only end with terminator insts, not contain them |
Chris Lattner | a5c3dec | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 29 | // * The entry node to a function must not have predecessors |
Misha Brukman | 6b63452 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 30 | // * All Instructions must be embedded into a basic block |
Misha Brukman | 5b63631 | 2004-06-24 21:47:35 +0000 | [diff] [blame] | 31 | // * Functions cannot take a void-typed parameter |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 32 | // * Verify that a function's argument list agrees with it's declared type. |
Chris Lattner | acd3cae | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 33 | // * It is illegal to specify a name for a void value. |
Misha Brukman | 6b63452 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 34 | // * It is illegal to have a internal global value with no initializer |
Chris Lattner | 23f0ce6 | 2002-04-12 18:20:49 +0000 | [diff] [blame] | 35 | // * It is illegal to have a ret instruction that returns a value that does not |
| 36 | // agree with the function return value type. |
Chris Lattner | 56732fb | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 37 | // * Function call argument types match the function prototype |
Chris Lattner | a00409e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 38 | // * All other things that are tested by asserts spread about the code... |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | // |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
| 42 | #include "llvm/Analysis/Verifier.h" |
Brian Gaeke | 9cebe2d | 2003-11-16 23:07:42 +0000 | [diff] [blame] | 43 | #include "llvm/Assembly/Writer.h" |
Chris Lattner | 37c121a | 2005-05-08 22:27:09 +0000 | [diff] [blame] | 44 | #include "llvm/CallingConv.h" |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 45 | #include "llvm/Constants.h" |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 46 | #include "llvm/Pass.h" |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 47 | #include "llvm/Module.h" |
Chris Lattner | 2eff859 | 2004-03-14 03:16:15 +0000 | [diff] [blame] | 48 | #include "llvm/ModuleProvider.h" |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 49 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 3188b73 | 2006-01-26 00:08:45 +0000 | [diff] [blame] | 50 | #include "llvm/InlineAsm.h" |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 51 | #include "llvm/Instructions.h" |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 52 | #include "llvm/Intrinsics.h" |
Chris Lattner | cf89908 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 53 | #include "llvm/PassManager.h" |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 54 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 55 | #include "llvm/Support/CFG.h" |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 56 | #include "llvm/Support/InstVisitor.h" |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 57 | #include "llvm/Support/Streams.h" |
Chris Lattner | 78287b4 | 2007-02-10 08:19:44 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 8552fae | 2007-02-10 08:30:29 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 60 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 62 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 63 | #include <algorithm> |
Bill Wendling | 1a097e3 | 2006-12-07 23:41:45 +0000 | [diff] [blame] | 64 | #include <sstream> |
Jeff Cohen | 4c5701d | 2006-03-31 07:22:05 +0000 | [diff] [blame] | 65 | #include <cstdarg> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 66 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 67 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 68 | namespace { // Anonymous namespace for class |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 69 | |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 70 | struct VISIBILITY_HIDDEN |
| 71 | Verifier : public FunctionPass, InstVisitor<Verifier> { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 72 | static char ID; // Pass ID, replacement for typeid |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 73 | bool Broken; // Is this module found to be broken? |
| 74 | bool RealPass; // Are we not being run by a PassManager? |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 75 | VerifierFailureAction action; |
Reid Spencer | af90b0d | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 76 | // What to do if verification fails. |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 77 | Module *Mod; // Module we are verifying right now |
Chris Lattner | 0b2192c | 2006-01-12 06:17:59 +0000 | [diff] [blame] | 78 | ETForest *EF; // ET-Forest, caution can be null! |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 79 | std::stringstream msgs; // A stringstream to collect messages |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 80 | |
Chris Lattner | a7b1c7e | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 81 | /// 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 Lattner | 78287b4 | 2007-02-10 08:19:44 +0000 | [diff] [blame] | 85 | SmallPtrSet<Instruction*, 16> InstsInThisBlock; |
Chris Lattner | a7b1c7e | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 86 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 87 | Verifier() |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 88 | : FunctionPass((intptr_t)&ID), |
| 89 | Broken(false), RealPass(true), action(AbortProcessAction), |
| 90 | EF(0), msgs( std::ios::app | std::ios::out ) {} |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 91 | Verifier( VerifierFailureAction ctn ) |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 92 | : FunctionPass((intptr_t)&ID), |
| 93 | Broken(false), RealPass(true), action(ctn), EF(0), |
| 94 | msgs( std::ios::app | std::ios::out ) {} |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 95 | Verifier(bool AB ) |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 96 | : 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 Lattner | 0b2192c | 2006-01-12 06:17:59 +0000 | [diff] [blame] | 100 | Verifier(ETForest &ef) |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 101 | : FunctionPass((intptr_t)&ID), |
| 102 | Broken(false), RealPass(false), action(PrintMessageAction), |
| 103 | EF(&ef), msgs( std::ios::app | std::ios::out ) {} |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 105 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 106 | bool doInitialization(Module &M) { |
Brian Gaeke | 9cebe2d | 2003-11-16 23:07:42 +0000 | [diff] [blame] | 107 | Mod = &M; |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 108 | verifyTypeSymbolTable(M.getTypeSymbolTable()); |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 109 | |
| 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 Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 113 | if (RealPass) |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 114 | return abortIfBroken(); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 118 | bool runOnFunction(Function &F) { |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 119 | // Get dominator information if we are being run by PassManager |
Chris Lattner | 0b2192c | 2006-01-12 06:17:59 +0000 | [diff] [blame] | 120 | if (RealPass) EF = &getAnalysis<ETForest>(); |
Chris Lattner | cd07075 | 2007-04-20 23:59:29 +0000 | [diff] [blame] | 121 | |
| 122 | Mod = F.getParent(); |
| 123 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 124 | visit(F); |
Chris Lattner | a7b1c7e | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 125 | InstsInThisBlock.clear(); |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 126 | |
| 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 Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 130 | if (RealPass) |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 131 | return abortIfBroken(); |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 132 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 136 | bool doFinalization(Module &M) { |
Chris Lattner | 794caa1 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 137 | // Scan through, checking all of the external function's linkage now... |
Chris Lattner | 7c277b3 | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 138 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
Chris Lattner | 5399741 | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 139 | visitGlobalValue(*I); |
Chris Lattner | 794caa1 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 140 | |
Chris Lattner | 7c277b3 | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 141 | // Check to make sure function prototypes are okay. |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 142 | if (I->isDeclaration()) visitFunction(*I); |
Chris Lattner | 7c277b3 | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 145 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 146 | I != E; ++I) |
Chris Lattner | 56998b2 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 147 | visitGlobalVariable(*I); |
Chris Lattner | 61b91bc | 2002-10-06 22:47:32 +0000 | [diff] [blame] | 148 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 149 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
| 150 | I != E; ++I) |
| 151 | visitGlobalAlias(*I); |
| 152 | |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 153 | // If the module is broken, abort at this time. |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 154 | return abortIfBroken(); |
Chris Lattner | a00409e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 157 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 158 | AU.setPreservesAll(); |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 159 | if (RealPass) |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 160 | AU.addRequired<ETForest>(); |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 163 | /// abortIfBroken - If the module is broken and we are supposed to abort on |
| 164 | /// this condition, do so. |
| 165 | /// |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 166 | bool abortIfBroken() { |
Chris Lattner | 05ac92c | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 167 | if (Broken) { |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 168 | msgs << "Broken module found, "; |
Chris Lattner | 05ac92c | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 169 | switch (action) { |
John Criswell | 4a9c904 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 170 | case AbortProcessAction: |
| 171 | msgs << "compilation aborted!\n"; |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 172 | cerr << msgs.str(); |
John Criswell | 4a9c904 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 173 | abort(); |
John Criswell | 4a9c904 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 174 | case PrintMessageAction: |
| 175 | msgs << "verification continues.\n"; |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 176 | cerr << msgs.str(); |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 177 | return false; |
John Criswell | 4a9c904 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 178 | case ReturnStatusAction: |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 179 | msgs << "compilation terminated.\n"; |
| 180 | return Broken; |
John Criswell | 4a9c904 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 181 | } |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 182 | } |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 183 | return false; |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Chris Lattner | 5399741 | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 186 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 187 | // Verification methods... |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 188 | void verifyTypeSymbolTable(TypeSymbolTable &ST); |
Chris Lattner | 5399741 | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 189 | void visitGlobalValue(GlobalValue &GV); |
Chris Lattner | 56998b2 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 190 | void visitGlobalVariable(GlobalVariable &GV); |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 191 | void visitGlobalAlias(GlobalAlias &GA); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 192 | void visitFunction(Function &F); |
| 193 | void visitBasicBlock(BasicBlock &BB); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 194 | 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 Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 206 | void visitPHINode(PHINode &PN); |
| 207 | void visitBinaryOperator(BinaryOperator &B); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 208 | void visitICmpInst(ICmpInst &IC); |
| 209 | void visitFCmpInst(FCmpInst &FC); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 210 | void visitExtractElementInst(ExtractElementInst &EI); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 211 | void visitInsertElementInst(InsertElementInst &EI); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 212 | void visitShuffleVectorInst(ShuffleVectorInst &EI); |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 213 | void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); } |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 214 | 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 Lattner | 0f9e9d0 | 2004-05-21 16:47:21 +0000 | [diff] [blame] | 221 | void visitSwitchInst(SwitchInst &SI); |
Chris Lattner | 230c1a7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 222 | void visitSelectInst(SelectInst &SI); |
Chris Lattner | 627079d | 2002-11-21 16:54:22 +0000 | [diff] [blame] | 223 | void visitUserOp1(Instruction &I); |
| 224 | void visitUserOp2(Instruction &I) { visitUserOp1(I); } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 225 | void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 226 | |
Reid Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 227 | void VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F, ...); |
Chris Lattner | 15e8752 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 228 | |
| 229 | void WriteValue(const Value *V) { |
| 230 | if (!V) return; |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 231 | if (isa<Instruction>(V)) { |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 232 | msgs << *V; |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 233 | } else { |
Chris Lattner | 3749c9c | 2006-12-06 06:16:21 +0000 | [diff] [blame] | 234 | WriteAsOperand(msgs, V, true, Mod); |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 235 | msgs << "\n"; |
Chris Lattner | 15e8752 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Reid Spencer | af90b0d | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 239 | void WriteType(const Type* T ) { |
| 240 | if ( !T ) return; |
| 241 | WriteTypeSymbolic(msgs, T, Mod ); |
| 242 | } |
| 243 | |
Chris Lattner | 15e8752 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 244 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 245 | // 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 Lattner | 15e8752 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 248 | 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 Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 251 | msgs << Message << "\n"; |
Chris Lattner | 15e8752 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 252 | WriteValue(V1); |
| 253 | WriteValue(V2); |
| 254 | WriteValue(V3); |
| 255 | WriteValue(V4); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 256 | Broken = true; |
| 257 | } |
Reid Spencer | af90b0d | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 258 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 259 | void CheckFailed( const std::string& Message, const Value* V1, |
Reid Spencer | af90b0d | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 260 | const Type* T2, const Value* V3 = 0 ) { |
| 261 | msgs << Message << "\n"; |
| 262 | WriteValue(V1); |
| 263 | WriteType(T2); |
| 264 | WriteValue(V3); |
Reid Spencer | 5dff158 | 2004-05-27 21:58:13 +0000 | [diff] [blame] | 265 | Broken = true; |
Reid Spencer | af90b0d | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 266 | } |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 267 | }; |
Chris Lattner | e20a5dd | 2002-07-23 18:08:17 +0000 | [diff] [blame] | 268 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame^] | 269 | char Verifier::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 270 | RegisterPass<Verifier> X("verify", "Module Verifier"); |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 271 | } // End anonymous namespace |
| 272 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 274 | // Assert - We know that cond should be true, if not print an error message. |
| 275 | #define Assert(C, M) \ |
Chris Lattner | 39dd024 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 276 | do { if (!(C)) { CheckFailed(M); return; } } while (0) |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 277 | #define Assert1(C, M, V1) \ |
Chris Lattner | 39dd024 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 278 | do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 279 | #define Assert2(C, M, V1, V2) \ |
Chris Lattner | 39dd024 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 280 | do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 281 | #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 Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 285 | |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 5399741 | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 287 | void Verifier::visitGlobalValue(GlobalValue &GV) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 288 | Assert1(!GV.isDeclaration() || |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 289 | GV.hasExternalLinkage() || |
| 290 | GV.hasDLLImportLinkage() || |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 291 | GV.hasExternalWeakLinkage() || |
| 292 | (isa<GlobalAlias>(GV) && |
| 293 | (GV.hasInternalLinkage() || GV.hasWeakLinkage())), |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 294 | "Global is external, but doesn't have external or dllimport or weak linkage!", |
| 295 | &GV); |
| 296 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 297 | Assert1(!GV.hasDLLImportLinkage() || GV.isDeclaration(), |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 298 | "Global is marked as dllimport, but not external", &GV); |
| 299 | |
Chris Lattner | 5399741 | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 300 | 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 Lattner | 56998b2 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 310 | void Verifier::visitGlobalVariable(GlobalVariable &GV) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 311 | if (GV.hasInitializer()) |
Chris Lattner | 56998b2 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 312 | Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(), |
| 313 | "Global variable initializer type does not match global " |
| 314 | "variable type!", &GV); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 315 | |
Chris Lattner | 56998b2 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 316 | visitGlobalValue(GV); |
| 317 | } |
| 318 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 319 | void 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 Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 325 | Assert1(GA.getType() == GA.getAliasee()->getType(), |
| 326 | "Alias and aliasee types should match!", &GA); |
| 327 | |
Anton Korobeynikov | 0f53f7f | 2007-04-28 14:35:41 +0000 | [diff] [blame] | 328 | if (!isa<GlobalValue>(GA.getAliasee())) { |
| 329 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(GA.getAliasee()); |
Anton Korobeynikov | c6c98af | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 330 | Assert1(CE && CE->getOpcode() == Instruction::BitCast && |
| 331 | isa<GlobalValue>(CE->getOperand(0)), |
Anton Korobeynikov | 0f53f7f | 2007-04-28 14:35:41 +0000 | [diff] [blame] | 332 | "Aliasee should be either GlobalValue or bitcast of GlobalValue", |
| 333 | &GA); |
| 334 | } |
| 335 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 336 | visitGlobalValue(GA); |
| 337 | } |
| 338 | |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 339 | void Verifier::verifyTypeSymbolTable(TypeSymbolTable &ST) { |
| 340 | } |
Chris Lattner | 56998b2 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 341 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 342 | // visitFunction - Verify that a function is ok. |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 343 | // |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 344 | void Verifier::visitFunction(Function &F) { |
Chris Lattner | 37c121a | 2005-05-08 22:27:09 +0000 | [diff] [blame] | 345 | // Check function arguments. |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 346 | const FunctionType *FT = F.getFunctionType(); |
| 347 | unsigned NumArgs = F.getArgumentList().size(); |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 349 | Assert2(FT->getNumParams() == NumArgs, |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 350 | "# formal arguments must match # of arguments for function type!", |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 351 | &F, FT); |
Chris Lattner | c282f5a | 2003-11-21 22:32:23 +0000 | [diff] [blame] | 352 | Assert1(F.getReturnType()->isFirstClassType() || |
| 353 | F.getReturnType() == Type::VoidTy, |
| 354 | "Functions cannot return aggregate values!", &F); |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 355 | |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 356 | 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 Lattner | 80105dd | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 361 | // 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 Lattner | 80105dd | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 367 | case CallingConv::Fast: |
| 368 | case CallingConv::Cold: |
Anton Korobeynikov | bcb9770 | 2006-09-17 20:25:45 +0000 | [diff] [blame] | 369 | case CallingConv::X86_FastCall: |
Chris Lattner | 80105dd | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 370 | Assert1(!F.isVarArg(), |
| 371 | "Varargs functions must have C calling conventions!", &F); |
| 372 | break; |
| 373 | } |
| 374 | |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 375 | // Check that the argument values match the function type for this function... |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 376 | unsigned i = 0; |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 377 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 378 | I != E; ++I, ++i) { |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 379 | Assert2(I->getType() == FT->getParamType(i), |
| 380 | "Argument value does not match function argument type!", |
| 381 | I, FT->getParamType(i)); |
Chris Lattner | 7c277b3 | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 382 | // Make sure no aggregates are passed by value. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 383 | Assert1(I->getType()->isFirstClassType(), |
Chris Lattner | 7c277b3 | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 384 | "Functions cannot take aggregates as arguments by value!", I); |
| 385 | } |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 386 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 387 | if (!F.isDeclaration()) { |
Chris Lattner | 4d17caa | 2006-12-13 04:45:46 +0000 | [diff] [blame] | 388 | // 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 Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 394 | // Check the entry node |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 395 | BasicBlock *Entry = &F.getEntryBlock(); |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 396 | Assert1(pred_begin(Entry) == pred_end(Entry), |
| 397 | "Entry block to function must not have predecessors!", Entry); |
| 398 | } |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 402 | // verifyBasicBlock - Verify that a basic block is well formed... |
| 403 | // |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 404 | void Verifier::visitBasicBlock(BasicBlock &BB) { |
Chris Lattner | a7b1c7e | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 405 | InstsInThisBlock.clear(); |
| 406 | |
Alkis Evlogimenos | 4f4cf99 | 2004-12-04 02:30:42 +0000 | [diff] [blame] | 407 | // Ensure that basic blocks have terminators! |
| 408 | Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB); |
| 409 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 410 | // Check constraints that this basic block imposes on all of the PHI nodes in |
| 411 | // it. |
| 412 | if (isa<PHINode>(BB.front())) { |
Chris Lattner | f8edb62 | 2007-02-10 08:33:11 +0000 | [diff] [blame] | 413 | SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB)); |
| 414 | SmallVector<std::pair<BasicBlock*, Value*>, 8> Values; |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 415 | std::sort(Preds.begin(), Preds.end()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 416 | PHINode *PN; |
Chris Lattner | c70a509 | 2004-06-05 17:44:48 +0000 | [diff] [blame] | 417 | for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) { |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 418 | |
| 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 Gaeke | 2fea9ad | 2004-05-17 21:15:18 +0000 | [diff] [blame] | 423 | Assert1(PN->getNumIncomingValues() == Preds.size(), |
| 424 | "PHINode should have one entry for each predecessor of its " |
| 425 | "parent basic block!", PN); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 426 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 427 | // Get and sort all incoming values in the PHI node... |
Chris Lattner | f8edb62 | 2007-02-10 08:33:11 +0000 | [diff] [blame] | 428 | Values.clear(); |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 429 | 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 Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 434 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 435 | 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 Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 445 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 446 | // 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 Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 450 | Values[i].first, Preds[i]); |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 451 | } |
| 452 | } |
| 453 | } |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 454 | } |
Chris Lattner | acd3cae | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 456 | void 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 Lattner | 3535c9b | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 460 | visitInstruction(I); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | void Verifier::visitReturnInst(ReturnInst &RI) { |
| 464 | Function *F = RI.getParent()->getParent(); |
| 465 | if (RI.getNumOperands() == 0) |
Alkis Evlogimenos | 8b42b43 | 2004-12-04 01:25:06 +0000 | [diff] [blame] | 466 | Assert2(F->getReturnType() == Type::VoidTy, |
| 467 | "Found return instr that returns void in Function of non-void " |
| 468 | "return type!", &RI, F->getReturnType()); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 469 | 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 Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 474 | // Check to make sure that the return value has necessary properties for |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 475 | // terminators... |
| 476 | visitTerminatorInst(RI); |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Chris Lattner | 0f9e9d0 | 2004-05-21 16:47:21 +0000 | [diff] [blame] | 479 | void 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 Lattner | 230c1a7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 490 | void Verifier::visitSelectInst(SelectInst &SI) { |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 491 | Assert1(SI.getCondition()->getType() == Type::Int1Ty, |
Chris Lattner | 230c1a7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 492 | "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 Lattner | 0030e6c | 2004-09-29 21:19:28 +0000 | [diff] [blame] | 497 | visitInstruction(SI); |
Chris Lattner | 230c1a7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 501 | /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of |
| 502 | /// a pass, if any exist, it's an error. |
| 503 | /// |
Chris Lattner | 627079d | 2002-11-21 16:54:22 +0000 | [diff] [blame] | 504 | void Verifier::visitUserOp1(Instruction &I) { |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 505 | Assert1(0, "User-defined operators should not live outside of a pass!", &I); |
Chris Lattner | 627079d | 2002-11-21 16:54:22 +0000 | [diff] [blame] | 506 | } |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 507 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 508 | void 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 Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 517 | Assert1(SrcTy->isInteger(), "Trunc only operates on integer", &I); |
| 518 | Assert1(DestTy->isInteger(), "Trunc only produces integer", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 519 | Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I); |
| 520 | |
| 521 | visitInstruction(I); |
| 522 | } |
| 523 | |
| 524 | void 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 Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 530 | Assert1(SrcTy->isInteger(), "ZExt only operates on integer", &I); |
| 531 | Assert1(DestTy->isInteger(), "ZExt only produces an integer", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 532 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 533 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 534 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 535 | Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I); |
| 536 | |
| 537 | visitInstruction(I); |
| 538 | } |
| 539 | |
| 540 | void 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 Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 549 | Assert1(SrcTy->isInteger(), "SExt only operates on integer", &I); |
| 550 | Assert1(DestTy->isInteger(), "SExt only produces an integer", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 551 | Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I); |
| 552 | |
| 553 | visitInstruction(I); |
| 554 | } |
| 555 | |
| 556 | void 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 | |
| 571 | void 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 | |
| 587 | void 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 Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 592 | Assert1(SrcTy->isInteger(),"UInt2FP source must be integral", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 593 | Assert1(DestTy->isFloatingPoint(),"UInt2FP result must be FP", &I); |
| 594 | |
| 595 | visitInstruction(I); |
| 596 | } |
| 597 | |
| 598 | void 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 Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 603 | Assert1(SrcTy->isInteger(),"SInt2FP source must be integral", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 604 | Assert1(DestTy->isFloatingPoint(),"SInt2FP result must be FP", &I); |
| 605 | |
| 606 | visitInstruction(I); |
| 607 | } |
| 608 | |
| 609 | void 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 Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 615 | Assert1(DestTy->isInteger(),"FP2UInt result must be integral", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 616 | |
| 617 | visitInstruction(I); |
| 618 | } |
| 619 | |
| 620 | void 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 Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 626 | Assert1(DestTy->isInteger(),"FP2ToI result must be integral", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 627 | |
| 628 | visitInstruction(I); |
| 629 | } |
| 630 | |
| 631 | void 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 Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 637 | Assert1(DestTy->isInteger(), "PtrToInt result must be integral", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 638 | |
| 639 | visitInstruction(I); |
| 640 | } |
| 641 | |
| 642 | void 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 Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 647 | Assert1(SrcTy->isInteger(), "IntToPtr source must be an integral", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 648 | Assert1(isa<PointerType>(DestTy), "IntToPtr result must be a pointer",&I); |
| 649 | |
| 650 | visitInstruction(I); |
| 651 | } |
| 652 | |
| 653 | void 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 Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 671 | /// visitPHINode - Ensure that a PHI node is well formed. |
| 672 | /// |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 673 | void 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 Brukman | 6b63452 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 676 | // either nonexistent (because this is begin()) or is a PHI node. If not, |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 677 | // then there is some other instruction before a PHI. |
Chris Lattner | 4d8c16f | 2007-04-17 17:36:12 +0000 | [diff] [blame] | 678 | Assert2(&PN == &PN.getParent()->front() || |
| 679 | isa<PHINode>(--BasicBlock::iterator(&PN)), |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 680 | "PHI nodes not grouped at top of basic block!", |
| 681 | &PN, PN.getParent()); |
| 682 | |
Chris Lattner | 579de71 | 2003-11-12 07:13:37 +0000 | [diff] [blame] | 683 | // 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 Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 689 | // All other PHI node constraints are checked in the visitBasicBlock method. |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 690 | |
| 691 | visitInstruction(PN); |
| 692 | } |
| 693 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 694 | void 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 Lattner | efdd0a2 | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 698 | Assert1(isa<FunctionType>(FPTy->getElementType()), |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 699 | "Called function is not pointer to function type!", &CI); |
Chris Lattner | 56732fb | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 700 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 701 | const FunctionType *FTy = cast<FunctionType>(FPTy->getElementType()); |
Chris Lattner | 56732fb | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 702 | |
| 703 | // Verify that the correct number of arguments are being passed |
| 704 | if (FTy->isVarArg()) |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 705 | Assert1(CI.getNumOperands()-1 >= FTy->getNumParams(), |
| 706 | "Called function requires more parameters than were provided!",&CI); |
Chris Lattner | 56732fb | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 707 | else |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 708 | Assert1(CI.getNumOperands()-1 == FTy->getNumParams(), |
| 709 | "Incorrect number of arguments passed to called function!", &CI); |
Chris Lattner | 56732fb | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 710 | |
| 711 | // Verify that all arguments to the call match the function type... |
| 712 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
Chris Lattner | 2a11653 | 2004-02-24 22:06:07 +0000 | [diff] [blame] | 713 | Assert3(CI.getOperand(i+1)->getType() == FTy->getParamType(i), |
Chris Lattner | 56732fb | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 714 | "Call parameter type does not match function signature!", |
Chris Lattner | 2a11653 | 2004-02-24 22:06:07 +0000 | [diff] [blame] | 715 | CI.getOperand(i+1), FTy->getParamType(i), &CI); |
Chris Lattner | 3535c9b | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 716 | |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 717 | if (Function *F = CI.getCalledFunction()) |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 718 | if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 719 | visitIntrinsicFunctionCall(ID, CI); |
| 720 | |
Chris Lattner | 3535c9b | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 721 | visitInstruction(CI); |
Chris Lattner | efdd0a2 | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 722 | } |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 723 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 724 | /// visitBinaryOperator - Check that both arguments to the binary operator are |
| 725 | /// of the same type! |
| 726 | /// |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 727 | void Verifier::visitBinaryOperator(BinaryOperator &B) { |
Chris Lattner | 1a143ae | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 728 | Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(), |
| 729 | "Both operands to a binary operator are not of the same type!", &B); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 730 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 731 | switch (B.getOpcode()) { |
Chris Lattner | 1a143ae | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 732 | // Check that logical operators are only used with integral operands. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 733 | case Instruction::And: |
| 734 | case Instruction::Or: |
| 735 | case Instruction::Xor: |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 736 | Assert1(B.getType()->isInteger() || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 737 | (isa<VectorType>(B.getType()) && |
| 738 | cast<VectorType>(B.getType())->getElementType()->isInteger()), |
Chris Lattner | 1a143ae | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 739 | "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 Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 743 | 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 Lattner | 1a143ae | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 753 | // 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 Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 757 | Assert1(B.getType()->isInteger() || B.getType()->isFloatingPoint() || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 758 | isa<VectorType>(B.getType()), |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 759 | "Arithmetic operators must have integer, fp, or vector type!", &B); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 760 | break; |
Chris Lattner | 1a143ae | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 761 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 762 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 763 | visitInstruction(B); |
| 764 | } |
| 765 | |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 766 | void 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 Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 773 | Assert1(Op0Ty->isInteger() || isa<PointerType>(Op0Ty), |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 774 | "Invalid operand types for ICmp instruction", &IC); |
| 775 | visitInstruction(IC); |
| 776 | } |
| 777 | |
| 778 | void 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 Spencer | affaf07 | 2007-01-04 05:22:18 +0000 | [diff] [blame] | 785 | Assert1(Op0Ty->isFloatingPoint(), |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 786 | "Invalid operand types for FCmp instruction", &FC); |
| 787 | visitInstruction(FC); |
| 788 | } |
| 789 | |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 790 | void Verifier::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 1cbe05b | 2006-04-08 04:07:52 +0000 | [diff] [blame] | 791 | Assert1(ExtractElementInst::isValidOperands(EI.getOperand(0), |
| 792 | EI.getOperand(1)), |
| 793 | "Invalid extractelement operands!", &EI); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 794 | visitInstruction(EI); |
| 795 | } |
| 796 | |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 797 | void Verifier::visitInsertElementInst(InsertElementInst &IE) { |
Chris Lattner | 1cbe05b | 2006-04-08 04:07:52 +0000 | [diff] [blame] | 798 | Assert1(InsertElementInst::isValidOperands(IE.getOperand(0), |
| 799 | IE.getOperand(1), |
| 800 | IE.getOperand(2)), |
| 801 | "Invalid insertelement operands!", &IE); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 802 | visitInstruction(IE); |
| 803 | } |
| 804 | |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 805 | void 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 Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 813 | if (const ConstantVector *MV = dyn_cast<ConstantVector>(SV.getOperand(2))) { |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 814 | for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 815 | Assert1(isa<ConstantInt>(MV->getOperand(i)) || |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 816 | 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 Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 828 | void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 8552fae | 2007-02-10 08:30:29 +0000 | [diff] [blame] | 829 | SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end()); |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 830 | const Type *ElTy = |
| 831 | GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(), |
Chris Lattner | 8552fae | 2007-02-10 08:30:29 +0000 | [diff] [blame] | 832 | &Idxs[0], Idxs.size(), true); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 833 | Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP); |
Chris Lattner | 8552fae | 2007-02-10 08:30:29 +0000 | [diff] [blame] | 834 | Assert2(isa<PointerType>(GEP.getType()) && |
| 835 | cast<PointerType>(GEP.getType())->getElementType() == ElTy, |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 836 | "GEP is not of right type for indices!", &GEP, ElTy); |
Chris Lattner | a00409e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 837 | visitInstruction(GEP); |
| 838 | } |
| 839 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 840 | void Verifier::visitLoadInst(LoadInst &LI) { |
Chris Lattner | 24ea74e | 2002-08-22 22:49:05 +0000 | [diff] [blame] | 841 | const Type *ElTy = |
| 842 | cast<PointerType>(LI.getOperand(0)->getType())->getElementType(); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 843 | Assert2(ElTy == LI.getType(), |
Chris Lattner | 7334f2e | 2003-11-21 17:06:29 +0000 | [diff] [blame] | 844 | "Load result type does not match pointer operand type!", &LI, ElTy); |
Chris Lattner | a00409e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 845 | visitInstruction(LI); |
| 846 | } |
| 847 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 848 | void Verifier::visitStoreInst(StoreInst &SI) { |
Chris Lattner | 24ea74e | 2002-08-22 22:49:05 +0000 | [diff] [blame] | 849 | const Type *ElTy = |
| 850 | cast<PointerType>(SI.getOperand(1)->getType())->getElementType(); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 851 | Assert2(ElTy == SI.getOperand(0)->getType(), |
Chris Lattner | 7334f2e | 2003-11-21 17:06:29 +0000 | [diff] [blame] | 852 | "Stored value type does not match pointer operand type!", &SI, ElTy); |
Chris Lattner | a00409e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 853 | visitInstruction(SI); |
| 854 | } |
| 855 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 856 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 857 | /// verifyInstruction - Verify that an instruction is well formed. |
| 858 | /// |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 859 | void Verifier::visitInstruction(Instruction &I) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 860 | BasicBlock *BB = I.getParent(); |
Chris Lattner | 1a143ae | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 861 | Assert1(BB, "Instruction not embedded in basic block!", &I); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 862 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 863 | 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 Lattner | 0809253 | 2004-02-27 17:28:25 +0000 | [diff] [blame] | 866 | Assert1(*UI != (User*)&I || |
Chris Lattner | 0b2192c | 2006-01-12 06:17:59 +0000 | [diff] [blame] | 867 | !EF->dominates(&BB->getParent()->getEntryBlock(), BB), |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 868 | "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 Lattner | 944cfaf | 2004-03-29 00:29:36 +0000 | [diff] [blame] | 875 | // 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 Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 880 | // 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 Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 883 | for (User::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 884 | UI != UE; ++UI) { |
| 885 | Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!", |
| 886 | *UI); |
Chris Lattner | efdd0a2 | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 887 | Instruction *Used = cast<Instruction>(*UI); |
| 888 | Assert2(Used->getParent() != 0, "Instruction referencing instruction not" |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 889 | " embeded in a basic block!", &I, Used); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 892 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
Chris Lattner | aab1820 | 2005-02-24 16:58:29 +0000 | [diff] [blame] | 893 | Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I); |
Chris Lattner | f4ea921 | 2006-07-11 20:29:49 +0000 | [diff] [blame] | 894 | |
| 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 Lattner | 59c3569 | 2004-03-14 03:23:54 +0000 | [diff] [blame] | 900 | if (Function *F = dyn_cast<Function>(I.getOperand(i))) { |
Chris Lattner | f4ea921 | 2006-07-11 20:29:49 +0000 | [diff] [blame] | 901 | // Check to make sure that the "address of" an intrinsic function is never |
| 902 | // taken. |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 903 | Assert1(!F->isIntrinsic() || (i == 0 && isa<CallInst>(I)), |
| 904 | "Cannot take the address of an intrinsic!", &I); |
Chris Lattner | 19b6dcd | 2007-04-20 21:48:08 +0000 | [diff] [blame] | 905 | Assert1(F->getParent() == Mod, "Referencing function in another module!", |
| 906 | &I); |
Chris Lattner | 59c3569 | 2004-03-14 03:23:54 +0000 | [diff] [blame] | 907 | } 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 Lattner | 19b6dcd | 2007-04-20 21:48:08 +0000 | [diff] [blame] | 913 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) { |
| 914 | Assert1(GV->getParent() == Mod, "Referencing global in another module!", |
| 915 | &I); |
Chris Lattner | 59c3569 | 2004-03-14 03:23:54 +0000 | [diff] [blame] | 916 | } else if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) { |
Chris Lattner | 30768ac | 2004-01-14 04:25:59 +0000 | [diff] [blame] | 917 | BasicBlock *OpBlock = Op->getParent(); |
Chris Lattner | 30768ac | 2004-01-14 04:25:59 +0000 | [diff] [blame] | 918 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 919 | // Check that a definition dominates all of its uses. |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 920 | if (!isa<PHINode>(I)) { |
Chris Lattner | c9b0702 | 2004-01-14 05:42:52 +0000 | [diff] [blame] | 921 | // Invoke results are only usable in the normal destination, not in the |
| 922 | // exceptional destination. |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 923 | if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) { |
Chris Lattner | c9b0702 | 2004-01-14 05:42:52 +0000 | [diff] [blame] | 924 | OpBlock = II->getNormalDest(); |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 925 | |
Chris Lattner | 62d75e7 | 2006-12-20 21:20:13 +0000 | [diff] [blame] | 926 | Assert2(OpBlock != II->getUnwindDest(), |
| 927 | "No uses of invoke possible due to dominance structure!", |
| 928 | Op, II); |
| 929 | |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 930 | // 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 Lattner | 19591b3 | 2006-12-20 19:50:15 +0000 | [diff] [blame] | 949 | if (Bad) { |
| 950 | Bad = false; |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 951 | 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 Lattner | 62d75e7 | 2006-12-20 21:20:13 +0000 | [diff] [blame] | 959 | Assert2(!Bad, |
| 960 | "Invoke value defined on critical edge but not dead!", &I, |
| 961 | Op); |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 962 | } |
| 963 | } else if (OpBlock == BB) { |
Chris Lattner | e1f0cf1 | 2004-04-16 05:51:47 +0000 | [diff] [blame] | 964 | // If they are in the same basic block, make sure that the definition |
| 965 | // comes before the use. |
Chris Lattner | a7b1c7e | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 966 | Assert2(InstsInThisBlock.count(Op) || |
Chris Lattner | 0b2192c | 2006-01-12 06:17:59 +0000 | [diff] [blame] | 967 | !EF->dominates(&BB->getParent()->getEntryBlock(), BB), |
Chris Lattner | e1f0cf1 | 2004-04-16 05:51:47 +0000 | [diff] [blame] | 968 | "Instruction does not dominate all uses!", Op, &I); |
| 969 | } |
Chris Lattner | c9b0702 | 2004-01-14 05:42:52 +0000 | [diff] [blame] | 970 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 971 | // Definition must dominate use unless use is unreachable! |
Chris Lattner | 0b2192c | 2006-01-12 06:17:59 +0000 | [diff] [blame] | 972 | Assert2(EF->dominates(OpBlock, BB) || |
| 973 | !EF->dominates(&BB->getParent()->getEntryBlock(), BB), |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 974 | "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 Lattner | 0b2192c | 2006-01-12 06:17:59 +0000 | [diff] [blame] | 979 | Assert2(EF->dominates(OpBlock, PredBB) || |
| 980 | !EF->dominates(&BB->getParent()->getEntryBlock(), PredBB), |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 981 | "Instruction does not dominate all uses!", Op, &I); |
| 982 | } |
Chris Lattner | 3188b73 | 2006-01-26 00:08:45 +0000 | [diff] [blame] | 983 | } 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 Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 986 | } |
| 987 | } |
Chris Lattner | a7b1c7e | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 988 | InstsInThisBlock.insert(&I); |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | /// visitIntrinsicFunction - Allow intrinsics to be verified in different ways. |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 992 | /// |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 993 | void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 994 | Function *IF = CI.getCalledFunction(); |
Chris Lattner | 19b6dcd | 2007-04-20 21:48:08 +0000 | [diff] [blame] | 995 | Assert1(IF->isDeclaration(), "Intrinsic functions should never be defined!", |
| 996 | IF); |
Chris Lattner | 3b816b7 | 2006-03-09 22:06:04 +0000 | [diff] [blame] | 997 | |
| 998 | #define GET_INTRINSIC_VERIFIER |
| 999 | #include "llvm/Intrinsics.gen" |
| 1000 | #undef GET_INTRINSIC_VERIFIER |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1001 | } |
| 1002 | |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1003 | /// 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 Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1006 | void Verifier::VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F, ...) { |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1007 | va_list VA; |
| 1008 | va_start(VA, F); |
| 1009 | |
| 1010 | const FunctionType *FTy = F->getFunctionType(); |
| 1011 | |
Reid Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1012 | // 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 Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1017 | // Note that "arg#0" is the return type. |
| 1018 | for (unsigned ArgNo = 0; 1; ++ArgNo) { |
| 1019 | int TypeID = va_arg(VA, int); |
| 1020 | |
Jim Laskey | ba4cc09 | 2007-02-06 18:02:54 +0000 | [diff] [blame] | 1021 | if (TypeID == -2) { |
| 1022 | break; |
| 1023 | } |
| 1024 | |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1025 | 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 Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1037 | if (ArgNo == 0) |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1038 | Ty = FTy->getReturnType(); |
| 1039 | else |
| 1040 | Ty = FTy->getParamType(ArgNo-1); |
| 1041 | |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1042 | if (TypeID != Ty->getTypeID()) { |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1043 | 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 Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1050 | if (TypeID == Type::IntegerTyID) { |
Reid Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1051 | 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 Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1057 | 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 Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1064 | break; |
| 1065 | } |
Reid Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1066 | // 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 Spencer | addd11d | 2007-04-04 23:48:25 +0000 | [diff] [blame] | 1072 | /* FALL THROUGH */ |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 1073 | case Intrinsic::part_set: |
Chris Lattner | 466b9bd | 2007-04-10 03:18:19 +0000 | [diff] [blame] | 1074 | case Intrinsic::part_select: |
Reid Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1075 | if (ArgNo == 1) { |
| 1076 | unsigned ResultBits = |
| 1077 | cast<IntegerType>(FTy->getReturnType())->getBitWidth(); |
| 1078 | if (GotBits != ResultBits) |
Reid Spencer | f75b874 | 2007-04-12 02:48:46 +0000 | [diff] [blame] | 1079 | CheckFailed("Intrinsic requires the bit widths of the first " |
| 1080 | "parameter and the result to match", F); |
Reid Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1081 | } |
| 1082 | break; |
| 1083 | } |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1084 | } else if (TypeID == Type::VectorTyID) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1085 | // If this is a packed argument, verify the number and type of elements. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1086 | const VectorType *PTy = cast<VectorType>(Ty); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1087 | 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 Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1103 | if ((unsigned)va_arg(VA, int) != PTy->getNumElements()) { |
| 1104 | CheckFailed("Intrinsic prototype has incorrect number of " |
| 1105 | "vector elements!",F); |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1106 | break; |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | va_end(VA); |
Reid Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1112 | |
| 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 Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1127 | |
| 1128 | //===----------------------------------------------------------------------===// |
| 1129 | // Implement the public interfaces to this file... |
| 1130 | //===----------------------------------------------------------------------===// |
| 1131 | |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1132 | FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) { |
| 1133 | return new Verifier(action); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1134 | } |
| 1135 | |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1136 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1137 | // verifyFunction - Create |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1138 | bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) { |
Chris Lattner | 2eff859 | 2004-03-14 03:16:15 +0000 | [diff] [blame] | 1139 | Function &F = const_cast<Function&>(f); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1140 | assert(!F.isDeclaration() && "Cannot verify external functions"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1141 | |
Chris Lattner | 2eff859 | 2004-03-14 03:16:15 +0000 | [diff] [blame] | 1142 | FunctionPassManager FPM(new ExistingModuleProvider(F.getParent())); |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1143 | Verifier *V = new Verifier(action); |
Chris Lattner | 2eff859 | 2004-03-14 03:16:15 +0000 | [diff] [blame] | 1144 | FPM.add(V); |
| 1145 | FPM.run(F); |
| 1146 | return V->Broken; |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1149 | /// verifyModule - Check a module for errors, printing messages on stderr. |
| 1150 | /// Return true if the module is corrupt. |
| 1151 | /// |
Chris Lattner | 05ac92c | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 1152 | bool llvm::verifyModule(const Module &M, VerifierFailureAction action, |
| 1153 | std::string *ErrorInfo) { |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1154 | PassManager PM; |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1155 | Verifier *V = new Verifier(action); |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1156 | PM.add(V); |
| 1157 | PM.run((Module&)M); |
Chris Lattner | 05ac92c | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 1158 | |
| 1159 | if (ErrorInfo && V->Broken) |
| 1160 | *ErrorInfo = V->msgs.str(); |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1161 | return V->Broken; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1162 | } |
Reid Spencer | af90b0d | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 1163 | |
| 1164 | // vim: sw=2 |