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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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" |
Zhou Sheng | 5cbf316 | 2007-06-07 06:12:03 +0000 | [diff] [blame] | 49 | #include "llvm/ParameterAttributes.h" |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 50 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 3188b73 | 2006-01-26 00:08:45 +0000 | [diff] [blame] | 51 | #include "llvm/InlineAsm.h" |
Gordon Henriksen | b508521 | 2007-09-18 10:14:30 +0000 | [diff] [blame] | 52 | #include "llvm/IntrinsicInst.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" |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 55 | #include "llvm/CodeGen/ValueTypes.h" |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 56 | #include "llvm/Support/CallSite.h" |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 57 | #include "llvm/Support/CFG.h" |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 58 | #include "llvm/Support/InstVisitor.h" |
Bill Wendling | 8f48766 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 59 | #include "llvm/Support/Streams.h" |
Chris Lattner | 78287b4 | 2007-02-10 08:19:44 +0000 | [diff] [blame] | 60 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 8552fae | 2007-02-10 08:30:29 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 62 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 63 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | a4f0b3a | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 64 | #include "llvm/Support/Compiler.h" |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 65 | #include <algorithm> |
Bill Wendling | 1a097e3 | 2006-12-07 23:41:45 +0000 | [diff] [blame] | 66 | #include <sstream> |
Jeff Cohen | 4c5701d | 2006-03-31 07:22:05 +0000 | [diff] [blame] | 67 | #include <cstdarg> |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 68 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 69 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 70 | namespace { // Anonymous namespace for class |
Owen Anderson | c570e33 | 2007-10-31 21:04:18 +0000 | [diff] [blame] | 71 | struct VISIBILITY_HIDDEN PreVerifier : public FunctionPass { |
Owen Anderson | 765d645 | 2007-11-01 03:54:23 +0000 | [diff] [blame] | 72 | static char ID; // Pass ID, replacement for typeid |
Duncan Sands | d056190 | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 73 | |
Owen Anderson | 765d645 | 2007-11-01 03:54:23 +0000 | [diff] [blame] | 74 | PreVerifier() : FunctionPass((intptr_t)&ID) { } |
Duncan Sands | d056190 | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 75 | |
| 76 | // Check that the prerequisites for successful DominatorTree construction |
| 77 | // are satisfied. |
Owen Anderson | 765d645 | 2007-11-01 03:54:23 +0000 | [diff] [blame] | 78 | bool runOnFunction(Function &F) { |
Duncan Sands | d056190 | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 79 | bool Broken = false; |
| 80 | |
| 81 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 82 | if (I->empty() || !I->back().isTerminator()) { |
| 83 | cerr << "Basic Block does not have terminator!\n"; |
| 84 | WriteAsOperand(*cerr, I, true); |
| 85 | cerr << "\n"; |
| 86 | Broken = true; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (Broken) |
| 91 | abort(); |
| 92 | |
| 93 | return false; |
Owen Anderson | 765d645 | 2007-11-01 03:54:23 +0000 | [diff] [blame] | 94 | } |
Owen Anderson | c570e33 | 2007-10-31 21:04:18 +0000 | [diff] [blame] | 95 | }; |
Duncan Sands | d056190 | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 96 | |
Owen Anderson | c570e33 | 2007-10-31 21:04:18 +0000 | [diff] [blame] | 97 | char PreVerifier::ID = 0; |
| 98 | RegisterPass<PreVerifier> PreVer("preverify", "Preliminary module verification"); |
| 99 | const PassInfo *PreVerifyID = PreVer.getPassInfo(); |
Duncan Sands | d056190 | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 100 | |
Chris Lattner | f190d38 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 101 | struct VISIBILITY_HIDDEN |
| 102 | Verifier : public FunctionPass, InstVisitor<Verifier> { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 103 | static char ID; // Pass ID, replacement for typeid |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 104 | bool Broken; // Is this module found to be broken? |
| 105 | bool RealPass; // Are we not being run by a PassManager? |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 106 | VerifierFailureAction action; |
Reid Spencer | af90b0d | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 107 | // What to do if verification fails. |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 108 | Module *Mod; // Module we are verifying right now |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 109 | DominatorTree *DT; // Dominator Tree, caution can be null! |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 110 | std::stringstream msgs; // A stringstream to collect messages |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 111 | |
Chris Lattner | a7b1c7e | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 112 | /// InstInThisBlock - when verifying a basic block, keep track of all of the |
| 113 | /// instructions we have seen so far. This allows us to do efficient |
| 114 | /// dominance checks for the case when an instruction has an operand that is |
| 115 | /// an instruction in the same block. |
Chris Lattner | 78287b4 | 2007-02-10 08:19:44 +0000 | [diff] [blame] | 116 | SmallPtrSet<Instruction*, 16> InstsInThisBlock; |
Chris Lattner | a7b1c7e | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 117 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 118 | Verifier() |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 119 | : FunctionPass((intptr_t)&ID), |
| 120 | Broken(false), RealPass(true), action(AbortProcessAction), |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 121 | DT(0), msgs( std::ios::app | std::ios::out ) {} |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 122 | Verifier( VerifierFailureAction ctn ) |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 123 | : FunctionPass((intptr_t)&ID), |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 124 | Broken(false), RealPass(true), action(ctn), DT(0), |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 125 | msgs( std::ios::app | std::ios::out ) {} |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 126 | Verifier(bool AB ) |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 127 | : FunctionPass((intptr_t)&ID), |
| 128 | Broken(false), RealPass(true), |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 129 | action( AB ? AbortProcessAction : PrintMessageAction), DT(0), |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 130 | msgs( std::ios::app | std::ios::out ) {} |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 131 | Verifier(DominatorTree &dt) |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 132 | : FunctionPass((intptr_t)&ID), |
| 133 | Broken(false), RealPass(false), action(PrintMessageAction), |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 134 | DT(&dt), msgs( std::ios::app | std::ios::out ) {} |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 136 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 137 | bool doInitialization(Module &M) { |
Brian Gaeke | 9cebe2d | 2003-11-16 23:07:42 +0000 | [diff] [blame] | 138 | Mod = &M; |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 139 | verifyTypeSymbolTable(M.getTypeSymbolTable()); |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 140 | |
| 141 | // If this is a real pass, in a pass manager, we must abort before |
| 142 | // returning back to the pass manager, or else the pass manager may try to |
| 143 | // run other passes on the broken module. |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 144 | if (RealPass) |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 145 | return abortIfBroken(); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 146 | return false; |
| 147 | } |
| 148 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 149 | bool runOnFunction(Function &F) { |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 150 | // Get dominator information if we are being run by PassManager |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 151 | if (RealPass) DT = &getAnalysis<DominatorTree>(); |
Chris Lattner | cd07075 | 2007-04-20 23:59:29 +0000 | [diff] [blame] | 152 | |
| 153 | Mod = F.getParent(); |
| 154 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 155 | visit(F); |
Chris Lattner | a7b1c7e | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 156 | InstsInThisBlock.clear(); |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 157 | |
| 158 | // If this is a real pass, in a pass manager, we must abort before |
| 159 | // returning back to the pass manager, or else the pass manager may try to |
| 160 | // run other passes on the broken module. |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 161 | if (RealPass) |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 162 | return abortIfBroken(); |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 163 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 167 | bool doFinalization(Module &M) { |
Chris Lattner | 794caa1 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 168 | // Scan through, checking all of the external function's linkage now... |
Chris Lattner | 7c277b3 | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 169 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
Chris Lattner | 5399741 | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 170 | visitGlobalValue(*I); |
Chris Lattner | 794caa1 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 7c277b3 | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 172 | // Check to make sure function prototypes are okay. |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 173 | if (I->isDeclaration()) visitFunction(*I); |
Chris Lattner | 7c277b3 | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Reid Spencer | 0b11820 | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 176 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 177 | I != E; ++I) |
Chris Lattner | 56998b2 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 178 | visitGlobalVariable(*I); |
Chris Lattner | 61b91bc | 2002-10-06 22:47:32 +0000 | [diff] [blame] | 179 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 180 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
| 181 | I != E; ++I) |
| 182 | visitGlobalAlias(*I); |
| 183 | |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 184 | // If the module is broken, abort at this time. |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 185 | return abortIfBroken(); |
Chris Lattner | a00409e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 188 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 189 | AU.setPreservesAll(); |
Owen Anderson | c570e33 | 2007-10-31 21:04:18 +0000 | [diff] [blame] | 190 | AU.addRequiredID(PreVerifyID); |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 191 | if (RealPass) |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 192 | AU.addRequired<DominatorTree>(); |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 195 | /// abortIfBroken - If the module is broken and we are supposed to abort on |
| 196 | /// this condition, do so. |
| 197 | /// |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 198 | bool abortIfBroken() { |
Chris Lattner | 05ac92c | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 199 | if (Broken) { |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 200 | msgs << "Broken module found, "; |
Chris Lattner | 05ac92c | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 201 | switch (action) { |
John Criswell | 4a9c904 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 202 | case AbortProcessAction: |
| 203 | msgs << "compilation aborted!\n"; |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 204 | cerr << msgs.str(); |
John Criswell | 4a9c904 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 205 | abort(); |
John Criswell | 4a9c904 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 206 | case PrintMessageAction: |
| 207 | msgs << "verification continues.\n"; |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 208 | cerr << msgs.str(); |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 209 | return false; |
John Criswell | 4a9c904 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 210 | case ReturnStatusAction: |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 211 | msgs << "compilation terminated.\n"; |
| 212 | return Broken; |
John Criswell | 4a9c904 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 213 | } |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 214 | } |
Reid Spencer | 7107c3b | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 215 | return false; |
Chris Lattner | 3e1f144 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Chris Lattner | 5399741 | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 218 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 219 | // Verification methods... |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 220 | void verifyTypeSymbolTable(TypeSymbolTable &ST); |
Chris Lattner | 5399741 | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 221 | void visitGlobalValue(GlobalValue &GV); |
Chris Lattner | 56998b2 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 222 | void visitGlobalVariable(GlobalVariable &GV); |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 223 | void visitGlobalAlias(GlobalAlias &GA); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 224 | void visitFunction(Function &F); |
| 225 | void visitBasicBlock(BasicBlock &BB); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 226 | void visitTruncInst(TruncInst &I); |
| 227 | void visitZExtInst(ZExtInst &I); |
| 228 | void visitSExtInst(SExtInst &I); |
| 229 | void visitFPTruncInst(FPTruncInst &I); |
| 230 | void visitFPExtInst(FPExtInst &I); |
| 231 | void visitFPToUIInst(FPToUIInst &I); |
| 232 | void visitFPToSIInst(FPToSIInst &I); |
| 233 | void visitUIToFPInst(UIToFPInst &I); |
| 234 | void visitSIToFPInst(SIToFPInst &I); |
| 235 | void visitIntToPtrInst(IntToPtrInst &I); |
| 236 | void visitPtrToIntInst(PtrToIntInst &I); |
| 237 | void visitBitCastInst(BitCastInst &I); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 238 | void visitPHINode(PHINode &PN); |
| 239 | void visitBinaryOperator(BinaryOperator &B); |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 240 | void visitICmpInst(ICmpInst &IC); |
| 241 | void visitFCmpInst(FCmpInst &FC); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 242 | void visitExtractElementInst(ExtractElementInst &EI); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 243 | void visitInsertElementInst(InsertElementInst &EI); |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 244 | void visitShuffleVectorInst(ShuffleVectorInst &EI); |
Chris Lattner | 4d45bd0 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 245 | void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); } |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 246 | void visitCallInst(CallInst &CI); |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 247 | void visitInvokeInst(InvokeInst &II); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 248 | void visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 249 | void visitLoadInst(LoadInst &LI); |
| 250 | void visitStoreInst(StoreInst &SI); |
| 251 | void visitInstruction(Instruction &I); |
| 252 | void visitTerminatorInst(TerminatorInst &I); |
| 253 | void visitReturnInst(ReturnInst &RI); |
Chris Lattner | 0f9e9d0 | 2004-05-21 16:47:21 +0000 | [diff] [blame] | 254 | void visitSwitchInst(SwitchInst &SI); |
Chris Lattner | 230c1a7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 255 | void visitSelectInst(SelectInst &SI); |
Chris Lattner | 627079d | 2002-11-21 16:54:22 +0000 | [diff] [blame] | 256 | void visitUserOp1(Instruction &I); |
| 257 | void visitUserOp2(Instruction &I) { visitUserOp1(I); } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 258 | void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI); |
Christopher Lamb | 303dae9 | 2007-12-17 01:00:21 +0000 | [diff] [blame] | 259 | void visitAllocationInst(AllocationInst &AI); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 260 | |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 261 | void VerifyCallSite(CallSite CS); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 262 | void VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F, |
| 263 | unsigned Count, ...); |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 264 | void VerifyParamAttrs(const FunctionType *FT, const ParamAttrsList *Attrs, |
| 265 | const Value *V); |
Chris Lattner | 15e8752 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 266 | |
| 267 | void WriteValue(const Value *V) { |
| 268 | if (!V) return; |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 269 | if (isa<Instruction>(V)) { |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 270 | msgs << *V; |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 271 | } else { |
Chris Lattner | 3749c9c | 2006-12-06 06:16:21 +0000 | [diff] [blame] | 272 | WriteAsOperand(msgs, V, true, Mod); |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 273 | msgs << "\n"; |
Chris Lattner | 15e8752 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Reid Spencer | af90b0d | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 277 | void WriteType(const Type* T ) { |
| 278 | if ( !T ) return; |
| 279 | WriteTypeSymbolic(msgs, T, Mod ); |
| 280 | } |
| 281 | |
Chris Lattner | 15e8752 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 282 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 283 | // CheckFailed - A check failed, so print out the condition and the message |
| 284 | // that failed. This provides a nice place to put a breakpoint if you want |
| 285 | // to see why something is not correct. |
Chris Lattner | 15e8752 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 286 | void CheckFailed(const std::string &Message, |
| 287 | const Value *V1 = 0, const Value *V2 = 0, |
| 288 | const Value *V3 = 0, const Value *V4 = 0) { |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 289 | msgs << Message << "\n"; |
Chris Lattner | 15e8752 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 290 | WriteValue(V1); |
| 291 | WriteValue(V2); |
| 292 | WriteValue(V3); |
| 293 | WriteValue(V4); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 294 | Broken = true; |
| 295 | } |
Reid Spencer | af90b0d | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 296 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 297 | void CheckFailed( const std::string& Message, const Value* V1, |
Reid Spencer | af90b0d | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 298 | const Type* T2, const Value* V3 = 0 ) { |
| 299 | msgs << Message << "\n"; |
| 300 | WriteValue(V1); |
| 301 | WriteType(T2); |
| 302 | WriteValue(V3); |
Reid Spencer | 5dff158 | 2004-05-27 21:58:13 +0000 | [diff] [blame] | 303 | Broken = true; |
Reid Spencer | af90b0d | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 304 | } |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 305 | }; |
Chris Lattner | e20a5dd | 2002-07-23 18:08:17 +0000 | [diff] [blame] | 306 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 307 | char Verifier::ID = 0; |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 308 | RegisterPass<Verifier> X("verify", "Module Verifier"); |
Chris Lattner | 31f8499 | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 309 | } // End anonymous namespace |
| 310 | |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 311 | |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 312 | // Assert - We know that cond should be true, if not print an error message. |
| 313 | #define Assert(C, M) \ |
Chris Lattner | 39dd024 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 314 | do { if (!(C)) { CheckFailed(M); return; } } while (0) |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 315 | #define Assert1(C, M, V1) \ |
Chris Lattner | 39dd024 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 316 | do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 317 | #define Assert2(C, M, V1, V2) \ |
Chris Lattner | 39dd024 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 318 | do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 319 | #define Assert3(C, M, V1, V2, V3) \ |
| 320 | do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0) |
| 321 | #define Assert4(C, M, V1, V2, V3, V4) \ |
| 322 | do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0) |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 323 | |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 324 | |
Chris Lattner | 5399741 | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 325 | void Verifier::visitGlobalValue(GlobalValue &GV) { |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 326 | Assert1(!GV.isDeclaration() || |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 327 | GV.hasExternalLinkage() || |
| 328 | GV.hasDLLImportLinkage() || |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 329 | GV.hasExternalWeakLinkage() || |
| 330 | (isa<GlobalAlias>(GV) && |
| 331 | (GV.hasInternalLinkage() || GV.hasWeakLinkage())), |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 332 | "Global is external, but doesn't have external or dllimport or weak linkage!", |
| 333 | &GV); |
| 334 | |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 335 | Assert1(!GV.hasDLLImportLinkage() || GV.isDeclaration(), |
Anton Korobeynikov | b74ed07 | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 336 | "Global is marked as dllimport, but not external", &GV); |
| 337 | |
Chris Lattner | 5399741 | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 338 | Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV), |
| 339 | "Only global variables can have appending linkage!", &GV); |
| 340 | |
| 341 | if (GV.hasAppendingLinkage()) { |
| 342 | GlobalVariable &GVar = cast<GlobalVariable>(GV); |
| 343 | Assert1(isa<ArrayType>(GVar.getType()->getElementType()), |
| 344 | "Only global arrays can have appending linkage!", &GV); |
| 345 | } |
| 346 | } |
| 347 | |
Chris Lattner | 56998b2 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 348 | void Verifier::visitGlobalVariable(GlobalVariable &GV) { |
Chris Lattner | 6693da0 | 2007-09-19 17:14:45 +0000 | [diff] [blame] | 349 | if (GV.hasInitializer()) { |
Chris Lattner | 56998b2 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 350 | Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(), |
| 351 | "Global variable initializer type does not match global " |
| 352 | "variable type!", &GV); |
Chris Lattner | 6693da0 | 2007-09-19 17:14:45 +0000 | [diff] [blame] | 353 | } else { |
| 354 | Assert1(GV.hasExternalLinkage() || GV.hasDLLImportLinkage() || |
| 355 | GV.hasExternalWeakLinkage(), |
| 356 | "invalid linkage type for global declaration", &GV); |
| 357 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 358 | |
Chris Lattner | 56998b2 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 359 | visitGlobalValue(GV); |
| 360 | } |
| 361 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 362 | void Verifier::visitGlobalAlias(GlobalAlias &GA) { |
| 363 | Assert1(!GA.getName().empty(), |
| 364 | "Alias name cannot be empty!", &GA); |
| 365 | Assert1(GA.hasExternalLinkage() || GA.hasInternalLinkage() || |
| 366 | GA.hasWeakLinkage(), |
| 367 | "Alias should have external or external weak linkage!", &GA); |
Anton Korobeynikov | a80e118 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 368 | Assert1(GA.getType() == GA.getAliasee()->getType(), |
| 369 | "Alias and aliasee types should match!", &GA); |
| 370 | |
Anton Korobeynikov | 0f53f7f | 2007-04-28 14:35:41 +0000 | [diff] [blame] | 371 | if (!isa<GlobalValue>(GA.getAliasee())) { |
| 372 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(GA.getAliasee()); |
Anton Korobeynikov | c6c98af | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 373 | Assert1(CE && CE->getOpcode() == Instruction::BitCast && |
| 374 | isa<GlobalValue>(CE->getOperand(0)), |
Anton Korobeynikov | 0f53f7f | 2007-04-28 14:35:41 +0000 | [diff] [blame] | 375 | "Aliasee should be either GlobalValue or bitcast of GlobalValue", |
| 376 | &GA); |
| 377 | } |
| 378 | |
Anton Korobeynikov | 8b0a8c8 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 379 | visitGlobalValue(GA); |
| 380 | } |
| 381 | |
Reid Spencer | 78d033e | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 382 | void Verifier::verifyTypeSymbolTable(TypeSymbolTable &ST) { |
| 383 | } |
Chris Lattner | 56998b2 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 384 | |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 385 | // VerifyParamAttrs - Check parameter attributes against a function type. |
| 386 | // The value V is printed in error messages. |
| 387 | void Verifier::VerifyParamAttrs(const FunctionType *FT, |
| 388 | const ParamAttrsList *Attrs, |
| 389 | const Value *V) { |
| 390 | if (!Attrs) |
| 391 | return; |
| 392 | |
| 393 | // Note that when calling a varargs function, the following test disallows |
| 394 | // parameter attributes for the arguments corresponding to the varargs part. |
| 395 | Assert1(Attrs->size() && |
| 396 | Attrs->getParamIndex(Attrs->size()-1) <= FT->getNumParams(), |
| 397 | "Attributes after end of type!", V); |
| 398 | |
| 399 | bool SawNest = false; |
| 400 | |
| 401 | for (unsigned Idx = 0; Idx <= FT->getNumParams(); ++Idx) { |
| 402 | uint16_t Attr = Attrs->getParamAttrs(Idx); |
| 403 | |
| 404 | if (!Idx) { |
| 405 | uint16_t RetI = Attr & ParamAttr::ParameterOnly; |
| 406 | Assert1(!RetI, "Attribute " + Attrs->getParamAttrsText(RetI) + |
| 407 | "does not apply to return values!", V); |
| 408 | } else { |
| 409 | uint16_t ParmI = Attr & ParamAttr::ReturnOnly; |
| 410 | Assert1(!ParmI, "Attribute " + Attrs->getParamAttrsText(ParmI) + |
| 411 | "only applies to return values!", V); |
| 412 | } |
| 413 | |
| 414 | for (unsigned i = 0; |
| 415 | i < array_lengthof(ParamAttr::MutuallyIncompatible); ++i) { |
| 416 | uint16_t MutI = Attr & ParamAttr::MutuallyIncompatible[i]; |
| 417 | Assert1(!(MutI & (MutI - 1)), "Attributes " + |
| 418 | Attrs->getParamAttrsText(MutI) + "are incompatible!", V); |
| 419 | } |
| 420 | |
| 421 | uint16_t IType = Attr & ParamAttr::IntegerTypeOnly; |
| 422 | Assert1(!IType || FT->getParamType(Idx-1)->isInteger(), |
| 423 | "Attribute " + Attrs->getParamAttrsText(IType) + |
| 424 | "should only apply to Integer type!", V); |
| 425 | |
| 426 | uint16_t PType = Attr & ParamAttr::PointerTypeOnly; |
| 427 | Assert1(!PType || isa<PointerType>(FT->getParamType(Idx-1)), |
| 428 | "Attribute " + Attrs->getParamAttrsText(PType) + |
| 429 | "should only apply to Pointer type!", V); |
| 430 | |
| 431 | if (Attr & ParamAttr::ByVal) { |
| 432 | const PointerType *Ty = |
| 433 | dyn_cast<PointerType>(FT->getParamType(Idx-1)); |
| 434 | Assert1(!Ty || isa<StructType>(Ty->getElementType()), |
| 435 | "Attribute byval should only apply to pointer to structs!", V); |
| 436 | } |
| 437 | |
| 438 | if (Attr & ParamAttr::Nest) { |
| 439 | Assert1(!SawNest, "More than one parameter has attribute nest!", V); |
| 440 | SawNest = true; |
| 441 | } |
| 442 | |
| 443 | if (Attr & ParamAttr::StructRet) { |
| 444 | Assert1(Idx == 1, "Attribute sret not on first parameter!", V); |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 449 | // visitFunction - Verify that a function is ok. |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 450 | // |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 451 | void Verifier::visitFunction(Function &F) { |
Chris Lattner | 37c121a | 2005-05-08 22:27:09 +0000 | [diff] [blame] | 452 | // Check function arguments. |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 453 | const FunctionType *FT = F.getFunctionType(); |
Chris Lattner | 453eed1 | 2007-08-18 06:13:19 +0000 | [diff] [blame] | 454 | unsigned NumArgs = F.arg_size(); |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 455 | |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 456 | Assert2(FT->getNumParams() == NumArgs, |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 457 | "# formal arguments must match # of arguments for function type!", |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 458 | &F, FT); |
Chris Lattner | c282f5a | 2003-11-21 22:32:23 +0000 | [diff] [blame] | 459 | Assert1(F.getReturnType()->isFirstClassType() || |
| 460 | F.getReturnType() == Type::VoidTy, |
| 461 | "Functions cannot return aggregate values!", &F); |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 462 | |
Duncan Sands | dc02467 | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 463 | Assert1(!F.isStructReturn() || FT->getReturnType() == Type::VoidTy, |
Anton Korobeynikov | b10308e | 2007-01-28 13:31:35 +0000 | [diff] [blame] | 464 | "Invalid struct-return function!", &F); |
| 465 | |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 466 | // Check function attributes. |
| 467 | VerifyParamAttrs(FT, F.getParamAttrs(), &F); |
Duncan Sands | fdef00f | 2007-07-27 15:09:54 +0000 | [diff] [blame] | 468 | |
Chris Lattner | 80105dd | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 469 | // Check that this function meets the restrictions on this calling convention. |
| 470 | switch (F.getCallingConv()) { |
| 471 | default: |
| 472 | break; |
| 473 | case CallingConv::C: |
| 474 | break; |
Chris Lattner | 80105dd | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 475 | case CallingConv::Fast: |
| 476 | case CallingConv::Cold: |
Anton Korobeynikov | bcb9770 | 2006-09-17 20:25:45 +0000 | [diff] [blame] | 477 | case CallingConv::X86_FastCall: |
Chris Lattner | 80105dd | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 478 | Assert1(!F.isVarArg(), |
| 479 | "Varargs functions must have C calling conventions!", &F); |
| 480 | break; |
| 481 | } |
| 482 | |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 483 | // Check that the argument values match the function type for this function... |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 484 | unsigned i = 0; |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 485 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 486 | I != E; ++I, ++i) { |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 487 | Assert2(I->getType() == FT->getParamType(i), |
| 488 | "Argument value does not match function argument type!", |
| 489 | I, FT->getParamType(i)); |
Chris Lattner | 7c277b3 | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 490 | // Make sure no aggregates are passed by value. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 491 | Assert1(I->getType()->isFirstClassType(), |
Chris Lattner | 7c277b3 | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 492 | "Functions cannot take aggregates as arguments by value!", I); |
| 493 | } |
Chris Lattner | ea24924 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 494 | |
Chris Lattner | 6693da0 | 2007-09-19 17:14:45 +0000 | [diff] [blame] | 495 | if (F.isDeclaration()) { |
| 496 | Assert1(F.hasExternalLinkage() || F.hasDLLImportLinkage() || |
| 497 | F.hasExternalWeakLinkage(), |
| 498 | "invalid linkage type for function declaration", &F); |
| 499 | } else { |
Chris Lattner | 4d17caa | 2006-12-13 04:45:46 +0000 | [diff] [blame] | 500 | // Verify that this function (which has a body) is not named "llvm.*". It |
| 501 | // is not legal to define intrinsics. |
| 502 | if (F.getName().size() >= 5) |
| 503 | Assert1(F.getName().substr(0, 5) != "llvm.", |
| 504 | "llvm intrinsics cannot be defined!", &F); |
| 505 | |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 506 | // Check the entry node |
Chris Lattner | 02a3be0 | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 507 | BasicBlock *Entry = &F.getEntryBlock(); |
Chris Lattner | 69da5cf | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 508 | Assert1(pred_begin(Entry) == pred_end(Entry), |
| 509 | "Entry block to function must not have predecessors!", Entry); |
| 510 | } |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 514 | // verifyBasicBlock - Verify that a basic block is well formed... |
| 515 | // |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 516 | void Verifier::visitBasicBlock(BasicBlock &BB) { |
Chris Lattner | a7b1c7e | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 517 | InstsInThisBlock.clear(); |
| 518 | |
Alkis Evlogimenos | 4f4cf99 | 2004-12-04 02:30:42 +0000 | [diff] [blame] | 519 | // Ensure that basic blocks have terminators! |
| 520 | Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB); |
| 521 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 522 | // Check constraints that this basic block imposes on all of the PHI nodes in |
| 523 | // it. |
| 524 | if (isa<PHINode>(BB.front())) { |
Chris Lattner | f8edb62 | 2007-02-10 08:33:11 +0000 | [diff] [blame] | 525 | SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB)); |
| 526 | SmallVector<std::pair<BasicBlock*, Value*>, 8> Values; |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 527 | std::sort(Preds.begin(), Preds.end()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 528 | PHINode *PN; |
Chris Lattner | c70a509 | 2004-06-05 17:44:48 +0000 | [diff] [blame] | 529 | for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) { |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 530 | |
| 531 | // Ensure that PHI nodes have at least one entry! |
| 532 | Assert1(PN->getNumIncomingValues() != 0, |
| 533 | "PHI nodes must have at least one entry. If the block is dead, " |
| 534 | "the PHI should be removed!", PN); |
Brian Gaeke | 2fea9ad | 2004-05-17 21:15:18 +0000 | [diff] [blame] | 535 | Assert1(PN->getNumIncomingValues() == Preds.size(), |
| 536 | "PHINode should have one entry for each predecessor of its " |
| 537 | "parent basic block!", PN); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 538 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 539 | // Get and sort all incoming values in the PHI node... |
Chris Lattner | f8edb62 | 2007-02-10 08:33:11 +0000 | [diff] [blame] | 540 | Values.clear(); |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 541 | Values.reserve(PN->getNumIncomingValues()); |
| 542 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 543 | Values.push_back(std::make_pair(PN->getIncomingBlock(i), |
| 544 | PN->getIncomingValue(i))); |
| 545 | std::sort(Values.begin(), Values.end()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 546 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 547 | for (unsigned i = 0, e = Values.size(); i != e; ++i) { |
| 548 | // Check to make sure that if there is more than one entry for a |
| 549 | // particular basic block in this PHI node, that the incoming values are |
| 550 | // all identical. |
| 551 | // |
| 552 | Assert4(i == 0 || Values[i].first != Values[i-1].first || |
| 553 | Values[i].second == Values[i-1].second, |
| 554 | "PHI node has multiple entries for the same basic block with " |
| 555 | "different incoming values!", PN, Values[i].first, |
| 556 | Values[i].second, Values[i-1].second); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 557 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 558 | // Check to make sure that the predecessors and PHI node entries are |
| 559 | // matched up. |
| 560 | Assert3(Values[i].first == Preds[i], |
| 561 | "PHI node entries do not match predecessors!", PN, |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 562 | Values[i].first, Preds[i]); |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 563 | } |
| 564 | } |
| 565 | } |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 566 | } |
Chris Lattner | acd3cae | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 567 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 568 | void Verifier::visitTerminatorInst(TerminatorInst &I) { |
| 569 | // Ensure that terminators only exist at the end of the basic block. |
| 570 | Assert1(&I == I.getParent()->getTerminator(), |
| 571 | "Terminator found in the middle of a basic block!", I.getParent()); |
Chris Lattner | 3535c9b | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 572 | visitInstruction(I); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | void Verifier::visitReturnInst(ReturnInst &RI) { |
| 576 | Function *F = RI.getParent()->getParent(); |
| 577 | if (RI.getNumOperands() == 0) |
Alkis Evlogimenos | 8b42b43 | 2004-12-04 01:25:06 +0000 | [diff] [blame] | 578 | Assert2(F->getReturnType() == Type::VoidTy, |
| 579 | "Found return instr that returns void in Function of non-void " |
| 580 | "return type!", &RI, F->getReturnType()); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 581 | else |
| 582 | Assert2(F->getReturnType() == RI.getOperand(0)->getType(), |
| 583 | "Function return type does not match operand " |
| 584 | "type of return inst!", &RI, F->getReturnType()); |
| 585 | |
Misha Brukman | 5560c9d | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 586 | // Check to make sure that the return value has necessary properties for |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 587 | // terminators... |
| 588 | visitTerminatorInst(RI); |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Chris Lattner | 0f9e9d0 | 2004-05-21 16:47:21 +0000 | [diff] [blame] | 591 | void Verifier::visitSwitchInst(SwitchInst &SI) { |
| 592 | // Check to make sure that all of the constants in the switch instruction |
| 593 | // have the same type as the switched-on value. |
| 594 | const Type *SwitchTy = SI.getCondition()->getType(); |
| 595 | for (unsigned i = 1, e = SI.getNumCases(); i != e; ++i) |
| 596 | Assert1(SI.getCaseValue(i)->getType() == SwitchTy, |
| 597 | "Switch constants must all be same type as switch value!", &SI); |
| 598 | |
| 599 | visitTerminatorInst(SI); |
| 600 | } |
| 601 | |
Chris Lattner | 230c1a7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 602 | void Verifier::visitSelectInst(SelectInst &SI) { |
Reid Spencer | 4fe16d6 | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 603 | Assert1(SI.getCondition()->getType() == Type::Int1Ty, |
Chris Lattner | 230c1a7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 604 | "Select condition type must be bool!", &SI); |
| 605 | Assert1(SI.getTrueValue()->getType() == SI.getFalseValue()->getType(), |
| 606 | "Select values must have identical types!", &SI); |
| 607 | Assert1(SI.getTrueValue()->getType() == SI.getType(), |
| 608 | "Select values must have same type as select instruction!", &SI); |
Chris Lattner | 0030e6c | 2004-09-29 21:19:28 +0000 | [diff] [blame] | 609 | visitInstruction(SI); |
Chris Lattner | 230c1a7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 613 | /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of |
| 614 | /// a pass, if any exist, it's an error. |
| 615 | /// |
Chris Lattner | 627079d | 2002-11-21 16:54:22 +0000 | [diff] [blame] | 616 | void Verifier::visitUserOp1(Instruction &I) { |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 617 | 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] | 618 | } |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 619 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 620 | void Verifier::visitTruncInst(TruncInst &I) { |
| 621 | // Get the source and destination types |
| 622 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 623 | const Type *DestTy = I.getType(); |
| 624 | |
| 625 | // Get the size of the types in bits, we'll need this later |
| 626 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 627 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 628 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 629 | Assert1(SrcTy->isInteger(), "Trunc only operates on integer", &I); |
| 630 | Assert1(DestTy->isInteger(), "Trunc only produces integer", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 631 | Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I); |
| 632 | |
| 633 | visitInstruction(I); |
| 634 | } |
| 635 | |
| 636 | void Verifier::visitZExtInst(ZExtInst &I) { |
| 637 | // Get the source and destination types |
| 638 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 639 | const Type *DestTy = I.getType(); |
| 640 | |
| 641 | // Get the size of the types in bits, we'll need this later |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 642 | Assert1(SrcTy->isInteger(), "ZExt only operates on integer", &I); |
| 643 | Assert1(DestTy->isInteger(), "ZExt only produces an integer", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 644 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 645 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 646 | |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 647 | Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I); |
| 648 | |
| 649 | visitInstruction(I); |
| 650 | } |
| 651 | |
| 652 | void Verifier::visitSExtInst(SExtInst &I) { |
| 653 | // Get the source and destination types |
| 654 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 655 | const Type *DestTy = I.getType(); |
| 656 | |
| 657 | // Get the size of the types in bits, we'll need this later |
| 658 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 659 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 660 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 661 | Assert1(SrcTy->isInteger(), "SExt only operates on integer", &I); |
| 662 | Assert1(DestTy->isInteger(), "SExt only produces an integer", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 663 | Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I); |
| 664 | |
| 665 | visitInstruction(I); |
| 666 | } |
| 667 | |
| 668 | void Verifier::visitFPTruncInst(FPTruncInst &I) { |
| 669 | // Get the source and destination types |
| 670 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 671 | const Type *DestTy = I.getType(); |
| 672 | // Get the size of the types in bits, we'll need this later |
| 673 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 674 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 675 | |
| 676 | Assert1(SrcTy->isFloatingPoint(),"FPTrunc only operates on FP", &I); |
| 677 | Assert1(DestTy->isFloatingPoint(),"FPTrunc only produces an FP", &I); |
| 678 | Assert1(SrcBitSize > DestBitSize,"DestTy too big for FPTrunc", &I); |
| 679 | |
| 680 | visitInstruction(I); |
| 681 | } |
| 682 | |
| 683 | void Verifier::visitFPExtInst(FPExtInst &I) { |
| 684 | // Get the source and destination types |
| 685 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 686 | const Type *DestTy = I.getType(); |
| 687 | |
| 688 | // Get the size of the types in bits, we'll need this later |
| 689 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 690 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 691 | |
| 692 | Assert1(SrcTy->isFloatingPoint(),"FPExt only operates on FP", &I); |
| 693 | Assert1(DestTy->isFloatingPoint(),"FPExt only produces an FP", &I); |
| 694 | Assert1(SrcBitSize < DestBitSize,"DestTy too small for FPExt", &I); |
| 695 | |
| 696 | visitInstruction(I); |
| 697 | } |
| 698 | |
| 699 | void Verifier::visitUIToFPInst(UIToFPInst &I) { |
| 700 | // Get the source and destination types |
| 701 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 702 | const Type *DestTy = I.getType(); |
| 703 | |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 704 | bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID; |
| 705 | bool DstVec = DestTy->getTypeID() == Type::VectorTyID; |
| 706 | |
| 707 | Assert1(SrcVec == DstVec,"UIToFP source and dest must both be vector or scalar", &I); |
| 708 | Assert1(SrcTy->isIntOrIntVector(),"UIToFP source must be integer or integer vector", &I); |
| 709 | Assert1(DestTy->isFPOrFPVector(),"UIToFP result must be FP or FP vector", &I); |
| 710 | |
| 711 | if (SrcVec && DstVec) |
| 712 | Assert1(cast<VectorType>(SrcTy)->getNumElements() == cast<VectorType>(DestTy)->getNumElements(), |
| 713 | "UIToFP source and dest vector length mismatch", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 714 | |
| 715 | visitInstruction(I); |
| 716 | } |
| 717 | |
| 718 | void Verifier::visitSIToFPInst(SIToFPInst &I) { |
| 719 | // Get the source and destination types |
| 720 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 721 | const Type *DestTy = I.getType(); |
| 722 | |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 723 | bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID; |
| 724 | bool DstVec = DestTy->getTypeID() == Type::VectorTyID; |
| 725 | |
| 726 | Assert1(SrcVec == DstVec,"SIToFP source and dest must both be vector or scalar", &I); |
| 727 | Assert1(SrcTy->isIntOrIntVector(),"SIToFP source must be integer or integer vector", &I); |
| 728 | Assert1(DestTy->isFPOrFPVector(),"SIToFP result must be FP or FP vector", &I); |
| 729 | |
| 730 | if (SrcVec && DstVec) |
| 731 | Assert1(cast<VectorType>(SrcTy)->getNumElements() == cast<VectorType>(DestTy)->getNumElements(), |
| 732 | "SIToFP source and dest vector length mismatch", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 733 | |
| 734 | visitInstruction(I); |
| 735 | } |
| 736 | |
| 737 | void Verifier::visitFPToUIInst(FPToUIInst &I) { |
| 738 | // Get the source and destination types |
| 739 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 740 | const Type *DestTy = I.getType(); |
| 741 | |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 742 | bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID; |
| 743 | bool DstVec = DestTy->getTypeID() == Type::VectorTyID; |
| 744 | |
| 745 | Assert1(SrcVec == DstVec,"FPToUI source and dest must both be vector or scalar", &I); |
| 746 | Assert1(SrcTy->isFPOrFPVector(),"FPToUI source must be FP or FP vector", &I); |
| 747 | Assert1(DestTy->isIntOrIntVector(),"FPToUI result must be integer or integer vector", &I); |
| 748 | |
| 749 | if (SrcVec && DstVec) |
| 750 | Assert1(cast<VectorType>(SrcTy)->getNumElements() == cast<VectorType>(DestTy)->getNumElements(), |
| 751 | "FPToUI source and dest vector length mismatch", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 752 | |
| 753 | visitInstruction(I); |
| 754 | } |
| 755 | |
| 756 | void Verifier::visitFPToSIInst(FPToSIInst &I) { |
| 757 | // Get the source and destination types |
| 758 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 759 | const Type *DestTy = I.getType(); |
| 760 | |
Nate Begeman | b348d18 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 761 | bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID; |
| 762 | bool DstVec = DestTy->getTypeID() == Type::VectorTyID; |
| 763 | |
| 764 | Assert1(SrcVec == DstVec,"FPToSI source and dest must both be vector or scalar", &I); |
| 765 | Assert1(SrcTy->isFPOrFPVector(),"FPToSI source must be FP or FP vector", &I); |
| 766 | Assert1(DestTy->isIntOrIntVector(),"FPToSI result must be integer or integer vector", &I); |
| 767 | |
| 768 | if (SrcVec && DstVec) |
| 769 | Assert1(cast<VectorType>(SrcTy)->getNumElements() == cast<VectorType>(DestTy)->getNumElements(), |
| 770 | "FPToSI source and dest vector length mismatch", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 771 | |
| 772 | visitInstruction(I); |
| 773 | } |
| 774 | |
| 775 | void Verifier::visitPtrToIntInst(PtrToIntInst &I) { |
| 776 | // Get the source and destination types |
| 777 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 778 | const Type *DestTy = I.getType(); |
| 779 | |
| 780 | Assert1(isa<PointerType>(SrcTy), "PtrToInt source must be pointer", &I); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 781 | Assert1(DestTy->isInteger(), "PtrToInt result must be integral", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 782 | |
| 783 | visitInstruction(I); |
| 784 | } |
| 785 | |
| 786 | void Verifier::visitIntToPtrInst(IntToPtrInst &I) { |
| 787 | // Get the source and destination types |
| 788 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 789 | const Type *DestTy = I.getType(); |
| 790 | |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 791 | Assert1(SrcTy->isInteger(), "IntToPtr source must be an integral", &I); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 792 | Assert1(isa<PointerType>(DestTy), "IntToPtr result must be a pointer",&I); |
| 793 | |
| 794 | visitInstruction(I); |
| 795 | } |
| 796 | |
| 797 | void Verifier::visitBitCastInst(BitCastInst &I) { |
| 798 | // Get the source and destination types |
| 799 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 800 | const Type *DestTy = I.getType(); |
| 801 | |
| 802 | // Get the size of the types in bits, we'll need this later |
| 803 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 804 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 805 | |
| 806 | // BitCast implies a no-op cast of type only. No bits change. |
| 807 | // However, you can't cast pointers to anything but pointers. |
| 808 | Assert1(isa<PointerType>(DestTy) == isa<PointerType>(DestTy), |
| 809 | "Bitcast requires both operands to be pointer or neither", &I); |
| 810 | Assert1(SrcBitSize == DestBitSize, "Bitcast requies types of same width", &I); |
| 811 | |
| 812 | visitInstruction(I); |
| 813 | } |
| 814 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 815 | /// visitPHINode - Ensure that a PHI node is well formed. |
| 816 | /// |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 817 | void Verifier::visitPHINode(PHINode &PN) { |
| 818 | // Ensure that the PHI nodes are all grouped together at the top of the block. |
| 819 | // This can be tested by checking whether the instruction before this is |
Misha Brukman | 6b63452 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 820 | // 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] | 821 | // then there is some other instruction before a PHI. |
Chris Lattner | 4d8c16f | 2007-04-17 17:36:12 +0000 | [diff] [blame] | 822 | Assert2(&PN == &PN.getParent()->front() || |
| 823 | isa<PHINode>(--BasicBlock::iterator(&PN)), |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 824 | "PHI nodes not grouped at top of basic block!", |
| 825 | &PN, PN.getParent()); |
| 826 | |
Chris Lattner | 579de71 | 2003-11-12 07:13:37 +0000 | [diff] [blame] | 827 | // Check that all of the operands of the PHI node have the same type as the |
| 828 | // result. |
| 829 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 830 | Assert1(PN.getType() == PN.getIncomingValue(i)->getType(), |
| 831 | "PHI node operands are not the same type as the result!", &PN); |
| 832 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 833 | // All other PHI node constraints are checked in the visitBasicBlock method. |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 834 | |
| 835 | visitInstruction(PN); |
| 836 | } |
| 837 | |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 838 | void Verifier::VerifyCallSite(CallSite CS) { |
| 839 | Instruction *I = CS.getInstruction(); |
| 840 | |
| 841 | Assert1(isa<PointerType>(CS.getCalledValue()->getType()), |
| 842 | "Called function must be a pointer!", I); |
| 843 | const PointerType *FPTy = cast<PointerType>(CS.getCalledValue()->getType()); |
Chris Lattner | efdd0a2 | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 844 | Assert1(isa<FunctionType>(FPTy->getElementType()), |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 845 | "Called function is not pointer to function type!", I); |
Chris Lattner | 56732fb | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 846 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 847 | const FunctionType *FTy = cast<FunctionType>(FPTy->getElementType()); |
Chris Lattner | 56732fb | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 848 | |
| 849 | // Verify that the correct number of arguments are being passed |
| 850 | if (FTy->isVarArg()) |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 851 | Assert1(CS.arg_size() >= FTy->getNumParams(), |
| 852 | "Called function requires more parameters than were provided!",I); |
Chris Lattner | 56732fb | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 853 | else |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 854 | Assert1(CS.arg_size() == FTy->getNumParams(), |
| 855 | "Incorrect number of arguments passed to called function!", I); |
Chris Lattner | 56732fb | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 856 | |
| 857 | // Verify that all arguments to the call match the function type... |
| 858 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 859 | Assert3(CS.getArgument(i)->getType() == FTy->getParamType(i), |
Chris Lattner | 56732fb | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 860 | "Call parameter type does not match function signature!", |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 861 | CS.getArgument(i), FTy->getParamType(i), I); |
| 862 | |
| 863 | // Verify call attributes. |
| 864 | VerifyParamAttrs(FTy, CS.getParamAttrs(), I); |
| 865 | |
| 866 | visitInstruction(*I); |
| 867 | } |
| 868 | |
| 869 | void Verifier::visitCallInst(CallInst &CI) { |
| 870 | VerifyCallSite(&CI); |
Chris Lattner | 3535c9b | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 871 | |
Anton Korobeynikov | 536c64b | 2007-10-28 22:50:32 +0000 | [diff] [blame] | 872 | if (Function *F = CI.getCalledFunction()) { |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 873 | if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 874 | visitIntrinsicFunctionCall(ID, CI); |
Anton Korobeynikov | 536c64b | 2007-10-28 22:50:32 +0000 | [diff] [blame] | 875 | } |
Duncan Sands | d9d7039 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | void Verifier::visitInvokeInst(InvokeInst &II) { |
| 879 | VerifyCallSite(&II); |
Chris Lattner | efdd0a2 | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 880 | } |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 881 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 882 | /// visitBinaryOperator - Check that both arguments to the binary operator are |
| 883 | /// of the same type! |
| 884 | /// |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 885 | void Verifier::visitBinaryOperator(BinaryOperator &B) { |
Chris Lattner | 1a143ae | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 886 | Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(), |
| 887 | "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] | 888 | |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 889 | switch (B.getOpcode()) { |
Chris Lattner | 1a143ae | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 890 | // Check that logical operators are only used with integral operands. |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 891 | case Instruction::And: |
| 892 | case Instruction::Or: |
| 893 | case Instruction::Xor: |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 894 | Assert1(B.getType()->isInteger() || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 895 | (isa<VectorType>(B.getType()) && |
| 896 | cast<VectorType>(B.getType())->getElementType()->isInteger()), |
Chris Lattner | 1a143ae | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 897 | "Logical operators only work with integral types!", &B); |
| 898 | Assert1(B.getType() == B.getOperand(0)->getType(), |
| 899 | "Logical operators must have same type for operands and result!", |
| 900 | &B); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 901 | break; |
| 902 | case Instruction::Shl: |
| 903 | case Instruction::LShr: |
| 904 | case Instruction::AShr: |
| 905 | Assert1(B.getType()->isInteger(), |
| 906 | "Shift must return an integer result!", &B); |
| 907 | Assert1(B.getType() == B.getOperand(0)->getType(), |
| 908 | "Shift return type must be same as operands!", &B); |
| 909 | /* FALL THROUGH */ |
| 910 | default: |
Chris Lattner | 1a143ae | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 911 | // Arithmetic operators only work on integer or fp values |
| 912 | Assert1(B.getType() == B.getOperand(0)->getType(), |
| 913 | "Arithmetic operators must have same type for operands and result!", |
| 914 | &B); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 915 | Assert1(B.getType()->isInteger() || B.getType()->isFloatingPoint() || |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 916 | isa<VectorType>(B.getType()), |
Reid Spencer | ac9dcb9 | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 917 | "Arithmetic operators must have integer, fp, or vector type!", &B); |
Reid Spencer | 832254e | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 918 | break; |
Chris Lattner | 1a143ae | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 919 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 920 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 921 | visitInstruction(B); |
| 922 | } |
| 923 | |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 924 | void Verifier::visitICmpInst(ICmpInst& IC) { |
| 925 | // Check that the operands are the same type |
| 926 | const Type* Op0Ty = IC.getOperand(0)->getType(); |
| 927 | const Type* Op1Ty = IC.getOperand(1)->getType(); |
| 928 | Assert1(Op0Ty == Op1Ty, |
| 929 | "Both operands to ICmp instruction are not of the same type!", &IC); |
| 930 | // Check that the operands are the right type |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 931 | Assert1(Op0Ty->isInteger() || isa<PointerType>(Op0Ty), |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 932 | "Invalid operand types for ICmp instruction", &IC); |
| 933 | visitInstruction(IC); |
| 934 | } |
| 935 | |
| 936 | void Verifier::visitFCmpInst(FCmpInst& FC) { |
| 937 | // Check that the operands are the same type |
| 938 | const Type* Op0Ty = FC.getOperand(0)->getType(); |
| 939 | const Type* Op1Ty = FC.getOperand(1)->getType(); |
| 940 | Assert1(Op0Ty == Op1Ty, |
| 941 | "Both operands to FCmp instruction are not of the same type!", &FC); |
| 942 | // Check that the operands are the right type |
Reid Spencer | affaf07 | 2007-01-04 05:22:18 +0000 | [diff] [blame] | 943 | Assert1(Op0Ty->isFloatingPoint(), |
Reid Spencer | 45fb3f3 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 944 | "Invalid operand types for FCmp instruction", &FC); |
| 945 | visitInstruction(FC); |
| 946 | } |
| 947 | |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 948 | void Verifier::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 1cbe05b | 2006-04-08 04:07:52 +0000 | [diff] [blame] | 949 | Assert1(ExtractElementInst::isValidOperands(EI.getOperand(0), |
| 950 | EI.getOperand(1)), |
| 951 | "Invalid extractelement operands!", &EI); |
Robert Bocchino | b52ee7f | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 952 | visitInstruction(EI); |
| 953 | } |
| 954 | |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 955 | void Verifier::visitInsertElementInst(InsertElementInst &IE) { |
Chris Lattner | 1cbe05b | 2006-04-08 04:07:52 +0000 | [diff] [blame] | 956 | Assert1(InsertElementInst::isValidOperands(IE.getOperand(0), |
| 957 | IE.getOperand(1), |
| 958 | IE.getOperand(2)), |
| 959 | "Invalid insertelement operands!", &IE); |
Robert Bocchino | c152f9c | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 960 | visitInstruction(IE); |
| 961 | } |
| 962 | |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 963 | void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) { |
| 964 | Assert1(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1), |
| 965 | SV.getOperand(2)), |
| 966 | "Invalid shufflevector operands!", &SV); |
| 967 | Assert1(SV.getType() == SV.getOperand(0)->getType(), |
| 968 | "Result of shufflevector must match first operand type!", &SV); |
| 969 | |
| 970 | // Check to see if Mask is valid. |
Reid Spencer | 9d6565a | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 971 | if (const ConstantVector *MV = dyn_cast<ConstantVector>(SV.getOperand(2))) { |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 972 | for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) { |
Reid Spencer | b83eb64 | 2006-10-20 07:07:24 +0000 | [diff] [blame] | 973 | Assert1(isa<ConstantInt>(MV->getOperand(i)) || |
Chris Lattner | 00f1023 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 974 | isa<UndefValue>(MV->getOperand(i)), |
| 975 | "Invalid shufflevector shuffle mask!", &SV); |
| 976 | } |
| 977 | } else { |
| 978 | Assert1(isa<UndefValue>(SV.getOperand(2)) || |
| 979 | isa<ConstantAggregateZero>(SV.getOperand(2)), |
| 980 | "Invalid shufflevector shuffle mask!", &SV); |
| 981 | } |
| 982 | |
| 983 | visitInstruction(SV); |
| 984 | } |
| 985 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 986 | void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 8552fae | 2007-02-10 08:30:29 +0000 | [diff] [blame] | 987 | SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end()); |
Chris Lattner | cc63f1c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 988 | const Type *ElTy = |
| 989 | GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(), |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 990 | Idxs.begin(), Idxs.end(), true); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 991 | Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP); |
Chris Lattner | 8552fae | 2007-02-10 08:30:29 +0000 | [diff] [blame] | 992 | Assert2(isa<PointerType>(GEP.getType()) && |
| 993 | cast<PointerType>(GEP.getType())->getElementType() == ElTy, |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 994 | "GEP is not of right type for indices!", &GEP, ElTy); |
Chris Lattner | a00409e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 995 | visitInstruction(GEP); |
| 996 | } |
| 997 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 998 | void Verifier::visitLoadInst(LoadInst &LI) { |
Chris Lattner | 24ea74e | 2002-08-22 22:49:05 +0000 | [diff] [blame] | 999 | const Type *ElTy = |
| 1000 | cast<PointerType>(LI.getOperand(0)->getType())->getElementType(); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1001 | Assert2(ElTy == LI.getType(), |
Chris Lattner | 7334f2e | 2003-11-21 17:06:29 +0000 | [diff] [blame] | 1002 | "Load result type does not match pointer operand type!", &LI, ElTy); |
Chris Lattner | a00409e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 1003 | visitInstruction(LI); |
| 1004 | } |
| 1005 | |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1006 | void Verifier::visitStoreInst(StoreInst &SI) { |
Chris Lattner | 24ea74e | 2002-08-22 22:49:05 +0000 | [diff] [blame] | 1007 | const Type *ElTy = |
| 1008 | cast<PointerType>(SI.getOperand(1)->getType())->getElementType(); |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1009 | Assert2(ElTy == SI.getOperand(0)->getType(), |
Chris Lattner | 7334f2e | 2003-11-21 17:06:29 +0000 | [diff] [blame] | 1010 | "Stored value type does not match pointer operand type!", &SI, ElTy); |
Chris Lattner | a00409e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 1011 | visitInstruction(SI); |
| 1012 | } |
| 1013 | |
Christopher Lamb | 303dae9 | 2007-12-17 01:00:21 +0000 | [diff] [blame] | 1014 | void Verifier::visitAllocationInst(AllocationInst &AI) { |
| 1015 | const PointerType *Ptr = AI.getType(); |
| 1016 | Assert(Ptr->getAddressSpace() == 0, |
| 1017 | "Allocation instruction pointer not in the generic address space!"); |
| 1018 | visitInstruction(AI); |
| 1019 | } |
| 1020 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1021 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1022 | /// verifyInstruction - Verify that an instruction is well formed. |
| 1023 | /// |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1024 | void Verifier::visitInstruction(Instruction &I) { |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1025 | BasicBlock *BB = I.getParent(); |
Chris Lattner | 1a143ae | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 1026 | Assert1(BB, "Instruction not embedded in basic block!", &I); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1027 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1028 | if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential |
| 1029 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
| 1030 | UI != UE; ++UI) |
Chris Lattner | 0809253 | 2004-02-27 17:28:25 +0000 | [diff] [blame] | 1031 | Assert1(*UI != (User*)&I || |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 1032 | !DT->dominates(&BB->getParent()->getEntryBlock(), BB), |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1033 | "Only PHI nodes may reference their own value!", &I); |
| 1034 | } |
| 1035 | |
| 1036 | // Check that void typed values don't have names |
| 1037 | Assert1(I.getType() != Type::VoidTy || !I.hasName(), |
| 1038 | "Instruction has a name, but provides a void value!", &I); |
| 1039 | |
Chris Lattner | 944cfaf | 2004-03-29 00:29:36 +0000 | [diff] [blame] | 1040 | // Check that the return value of the instruction is either void or a legal |
| 1041 | // value type. |
| 1042 | Assert1(I.getType() == Type::VoidTy || I.getType()->isFirstClassType(), |
| 1043 | "Instruction returns a non-scalar type!", &I); |
| 1044 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1045 | // Check that all uses of the instruction, if they are instructions |
| 1046 | // themselves, actually have parent basic blocks. If the use is not an |
| 1047 | // instruction, it is an error! |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1048 | for (User::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1049 | UI != UE; ++UI) { |
| 1050 | Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!", |
| 1051 | *UI); |
Chris Lattner | efdd0a2 | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 1052 | Instruction *Used = cast<Instruction>(*UI); |
| 1053 | Assert2(Used->getParent() != 0, "Instruction referencing instruction not" |
Chris Lattner | 24e845f | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1054 | " embeded in a basic block!", &I, Used); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1057 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
Chris Lattner | aab1820 | 2005-02-24 16:58:29 +0000 | [diff] [blame] | 1058 | Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I); |
Chris Lattner | f4ea921 | 2006-07-11 20:29:49 +0000 | [diff] [blame] | 1059 | |
| 1060 | // Check to make sure that only first-class-values are operands to |
| 1061 | // instructions. |
| 1062 | Assert1(I.getOperand(i)->getType()->isFirstClassType(), |
| 1063 | "Instruction operands must be first-class values!", &I); |
| 1064 | |
Chris Lattner | 59c3569 | 2004-03-14 03:23:54 +0000 | [diff] [blame] | 1065 | if (Function *F = dyn_cast<Function>(I.getOperand(i))) { |
Chris Lattner | f4ea921 | 2006-07-11 20:29:49 +0000 | [diff] [blame] | 1066 | // Check to make sure that the "address of" an intrinsic function is never |
| 1067 | // taken. |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 1068 | Assert1(!F->isIntrinsic() || (i == 0 && isa<CallInst>(I)), |
| 1069 | "Cannot take the address of an intrinsic!", &I); |
Chris Lattner | 19b6dcd | 2007-04-20 21:48:08 +0000 | [diff] [blame] | 1070 | Assert1(F->getParent() == Mod, "Referencing function in another module!", |
| 1071 | &I); |
Chris Lattner | 59c3569 | 2004-03-14 03:23:54 +0000 | [diff] [blame] | 1072 | } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) { |
| 1073 | Assert1(OpBB->getParent() == BB->getParent(), |
| 1074 | "Referring to a basic block in another function!", &I); |
| 1075 | } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) { |
| 1076 | Assert1(OpArg->getParent() == BB->getParent(), |
| 1077 | "Referring to an argument in another function!", &I); |
Chris Lattner | 19b6dcd | 2007-04-20 21:48:08 +0000 | [diff] [blame] | 1078 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) { |
| 1079 | Assert1(GV->getParent() == Mod, "Referencing global in another module!", |
| 1080 | &I); |
Chris Lattner | 59c3569 | 2004-03-14 03:23:54 +0000 | [diff] [blame] | 1081 | } else if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) { |
Chris Lattner | 30768ac | 2004-01-14 04:25:59 +0000 | [diff] [blame] | 1082 | BasicBlock *OpBlock = Op->getParent(); |
Chris Lattner | 30768ac | 2004-01-14 04:25:59 +0000 | [diff] [blame] | 1083 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1084 | // Check that a definition dominates all of its uses. |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1085 | if (!isa<PHINode>(I)) { |
Chris Lattner | c9b0702 | 2004-01-14 05:42:52 +0000 | [diff] [blame] | 1086 | // Invoke results are only usable in the normal destination, not in the |
| 1087 | // exceptional destination. |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1088 | if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) { |
Chris Lattner | c9b0702 | 2004-01-14 05:42:52 +0000 | [diff] [blame] | 1089 | OpBlock = II->getNormalDest(); |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1090 | |
Chris Lattner | 62d75e7 | 2006-12-20 21:20:13 +0000 | [diff] [blame] | 1091 | Assert2(OpBlock != II->getUnwindDest(), |
| 1092 | "No uses of invoke possible due to dominance structure!", |
| 1093 | Op, II); |
| 1094 | |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1095 | // If the normal successor of an invoke instruction has multiple |
| 1096 | // predecessors, then the normal edge from the invoke is critical, so |
| 1097 | // the invoke value can only be live if the destination block |
| 1098 | // dominates all of it's predecessors (other than the invoke) or if |
| 1099 | // the invoke value is only used by a phi in the successor. |
| 1100 | if (!OpBlock->getSinglePredecessor() && |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 1101 | DT->dominates(&BB->getParent()->getEntryBlock(), BB)) { |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1102 | // The first case we allow is if the use is a PHI operand in the |
| 1103 | // normal block, and if that PHI operand corresponds to the invoke's |
| 1104 | // block. |
| 1105 | bool Bad = true; |
| 1106 | if (PHINode *PN = dyn_cast<PHINode>(&I)) |
| 1107 | if (PN->getParent() == OpBlock && |
| 1108 | PN->getIncomingBlock(i/2) == Op->getParent()) |
| 1109 | Bad = false; |
| 1110 | |
| 1111 | // If it is used by something non-phi, then the other case is that |
| 1112 | // 'OpBlock' dominates all of its predecessors other than the |
| 1113 | // invoke. In this case, the invoke value can still be used. |
Chris Lattner | 19591b3 | 2006-12-20 19:50:15 +0000 | [diff] [blame] | 1114 | if (Bad) { |
| 1115 | Bad = false; |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1116 | for (pred_iterator PI = pred_begin(OpBlock), |
| 1117 | E = pred_end(OpBlock); PI != E; ++PI) { |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 1118 | if (*PI != II->getParent() && !DT->dominates(OpBlock, *PI)) { |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1119 | Bad = true; |
| 1120 | break; |
| 1121 | } |
| 1122 | } |
| 1123 | } |
Chris Lattner | 62d75e7 | 2006-12-20 21:20:13 +0000 | [diff] [blame] | 1124 | Assert2(!Bad, |
| 1125 | "Invoke value defined on critical edge but not dead!", &I, |
| 1126 | Op); |
Chris Lattner | e3cbe03 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1127 | } |
| 1128 | } else if (OpBlock == BB) { |
Chris Lattner | e1f0cf1 | 2004-04-16 05:51:47 +0000 | [diff] [blame] | 1129 | // If they are in the same basic block, make sure that the definition |
| 1130 | // comes before the use. |
Chris Lattner | a7b1c7e | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 1131 | Assert2(InstsInThisBlock.count(Op) || |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 1132 | !DT->dominates(&BB->getParent()->getEntryBlock(), BB), |
Chris Lattner | e1f0cf1 | 2004-04-16 05:51:47 +0000 | [diff] [blame] | 1133 | "Instruction does not dominate all uses!", Op, &I); |
| 1134 | } |
Chris Lattner | c9b0702 | 2004-01-14 05:42:52 +0000 | [diff] [blame] | 1135 | |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1136 | // Definition must dominate use unless use is unreachable! |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 1137 | Assert2(DT->dominates(OpBlock, BB) || |
| 1138 | !DT->dominates(&BB->getParent()->getEntryBlock(), BB), |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1139 | "Instruction does not dominate all uses!", Op, &I); |
| 1140 | } else { |
| 1141 | // PHI nodes are more difficult than other nodes because they actually |
| 1142 | // "use" the value in the predecessor basic blocks they correspond to. |
| 1143 | BasicBlock *PredBB = cast<BasicBlock>(I.getOperand(i+1)); |
Devang Patel | 2099ff0 | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 1144 | Assert2(DT->dominates(OpBlock, PredBB) || |
| 1145 | !DT->dominates(&BB->getParent()->getEntryBlock(), PredBB), |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1146 | "Instruction does not dominate all uses!", Op, &I); |
| 1147 | } |
Chris Lattner | 3188b73 | 2006-01-26 00:08:45 +0000 | [diff] [blame] | 1148 | } else if (isa<InlineAsm>(I.getOperand(i))) { |
Duncan Sands | fd7b326 | 2007-12-17 18:08:19 +0000 | [diff] [blame] | 1149 | Assert1(i == 0 && (isa<CallInst>(I) || isa<InvokeInst>(I)), |
Chris Lattner | 3188b73 | 2006-01-26 00:08:45 +0000 | [diff] [blame] | 1150 | "Cannot take the address of an inline asm!", &I); |
Chris Lattner | bede31f | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1151 | } |
| 1152 | } |
Chris Lattner | a7b1c7e | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 1153 | InstsInThisBlock.insert(&I); |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | /// visitIntrinsicFunction - Allow intrinsics to be verified in different ways. |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1157 | /// |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1158 | void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { |
Chris Lattner | dd035d1 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 1159 | Function *IF = CI.getCalledFunction(); |
Chris Lattner | 19b6dcd | 2007-04-20 21:48:08 +0000 | [diff] [blame] | 1160 | Assert1(IF->isDeclaration(), "Intrinsic functions should never be defined!", |
| 1161 | IF); |
Chris Lattner | 3b816b7 | 2006-03-09 22:06:04 +0000 | [diff] [blame] | 1162 | |
| 1163 | #define GET_INTRINSIC_VERIFIER |
| 1164 | #include "llvm/Intrinsics.gen" |
| 1165 | #undef GET_INTRINSIC_VERIFIER |
Gordon Henriksen | 8c33da5 | 2007-09-17 20:30:04 +0000 | [diff] [blame] | 1166 | |
| 1167 | switch (ID) { |
| 1168 | default: |
| 1169 | break; |
Gordon Henriksen | e1433f2 | 2007-12-25 02:31:26 +0000 | [diff] [blame] | 1170 | case Intrinsic::gcroot: |
| 1171 | case Intrinsic::gcwrite: |
Gordon Henriksen | 27acd3a | 2007-12-25 02:02:10 +0000 | [diff] [blame] | 1172 | case Intrinsic::gcread: { |
| 1173 | Type *PtrTy = PointerType::getUnqual(Type::Int8Ty), |
| 1174 | *PtrPtrTy = PointerType::getUnqual(PtrTy); |
Gordon Henriksen | e1433f2 | 2007-12-25 02:31:26 +0000 | [diff] [blame] | 1175 | |
| 1176 | switch (ID) { |
| 1177 | default: |
| 1178 | break; |
| 1179 | case Intrinsic::gcroot: |
| 1180 | Assert1(CI.getOperand(1)->getType() == PtrPtrTy, |
| 1181 | "Intrinsic parameter #1 is not i8**.", &CI); |
| 1182 | Assert1(CI.getOperand(2)->getType() == PtrTy, |
| 1183 | "Intrinsic parameter #2 is not i8*.", &CI); |
| 1184 | Assert1(isa<AllocaInst>( |
| 1185 | IntrinsicInst::StripPointerCasts(CI.getOperand(1))), |
| 1186 | "llvm.gcroot parameter #1 must be an alloca.", &CI); |
| 1187 | Assert1(isa<Constant>(CI.getOperand(2)), |
| 1188 | "llvm.gcroot parameter #2 must be a constant.", &CI); |
| 1189 | break; |
| 1190 | case Intrinsic::gcwrite: |
| 1191 | Assert1(CI.getOperand(1)->getType() == PtrTy, |
| 1192 | "Intrinsic parameter #1 is not a i8*.", &CI); |
| 1193 | Assert1(CI.getOperand(2)->getType() == PtrTy, |
| 1194 | "Intrinsic parameter #2 is not a i8*.", &CI); |
| 1195 | Assert1(CI.getOperand(3)->getType() == PtrPtrTy, |
| 1196 | "Intrinsic parameter #3 is not a i8**.", &CI); |
| 1197 | break; |
| 1198 | case Intrinsic::gcread: |
| 1199 | Assert1(CI.getOperand(1)->getType() == PtrTy, |
| 1200 | "Intrinsic parameter #1 is not a i8*.", &CI); |
| 1201 | Assert1(CI.getOperand(2)->getType() == PtrPtrTy, |
| 1202 | "Intrinsic parameter #2 is not a i8**.", &CI); |
| 1203 | break; |
| 1204 | } |
| 1205 | |
| 1206 | Assert1(CI.getParent()->getParent()->hasCollector(), |
| 1207 | "Enclosing function does not specify a collector algorithm.", |
| 1208 | &CI); |
Gordon Henriksen | 27acd3a | 2007-12-25 02:02:10 +0000 | [diff] [blame] | 1209 | } break; |
Duncan Sands | f51edad | 2007-09-29 16:25:54 +0000 | [diff] [blame] | 1210 | case Intrinsic::init_trampoline: |
| 1211 | Assert1(isa<Function>(IntrinsicInst::StripPointerCasts(CI.getOperand(2))), |
| 1212 | "llvm.init_trampoline parameter #2 must resolve to a function.", |
| 1213 | &CI); |
Gordon Henriksen | 27acd3a | 2007-12-25 02:02:10 +0000 | [diff] [blame] | 1214 | break; |
Gordon Henriksen | 8c33da5 | 2007-09-17 20:30:04 +0000 | [diff] [blame] | 1215 | } |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1218 | /// VerifyIntrinsicPrototype - TableGen emits calls to this function into |
| 1219 | /// Intrinsics.gen. This implements a little state machine that verifies the |
| 1220 | /// prototype of intrinsics. |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1221 | void Verifier::VerifyIntrinsicPrototype(Intrinsic::ID ID, |
| 1222 | Function *F, |
| 1223 | unsigned Count, ...) { |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1224 | va_list VA; |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1225 | va_start(VA, Count); |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1226 | |
| 1227 | const FunctionType *FTy = F->getFunctionType(); |
| 1228 | |
Reid Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1229 | // For overloaded intrinsics, the Suffix of the function name must match the |
| 1230 | // types of the arguments. This variable keeps track of the expected |
| 1231 | // suffix, to be checked at the end. |
| 1232 | std::string Suffix; |
| 1233 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1234 | if (FTy->getNumParams() + FTy->isVarArg() != Count - 1) { |
| 1235 | CheckFailed("Intrinsic prototype has incorrect number of arguments!", F); |
| 1236 | return; |
| 1237 | } |
| 1238 | |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1239 | // Note that "arg#0" is the return type. |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1240 | for (unsigned ArgNo = 0; ArgNo < Count; ++ArgNo) { |
| 1241 | MVT::ValueType VT = va_arg(VA, MVT::ValueType); |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1242 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1243 | if (VT == MVT::isVoid && ArgNo > 0) { |
| 1244 | if (!FTy->isVarArg()) |
| 1245 | CheckFailed("Intrinsic prototype has no '...'!", F); |
Jim Laskey | ba4cc09 | 2007-02-06 18:02:54 +0000 | [diff] [blame] | 1246 | break; |
| 1247 | } |
| 1248 | |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1249 | const Type *Ty; |
Reid Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1250 | if (ArgNo == 0) |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1251 | Ty = FTy->getReturnType(); |
| 1252 | else |
| 1253 | Ty = FTy->getParamType(ArgNo-1); |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1254 | |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1255 | unsigned NumElts = 0; |
| 1256 | const Type *EltTy = Ty; |
| 1257 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) { |
| 1258 | EltTy = VTy->getElementType(); |
| 1259 | NumElts = VTy->getNumElements(); |
| 1260 | } |
| 1261 | |
| 1262 | if ((int)VT < 0) { |
| 1263 | int Match = ~VT; |
| 1264 | if (Match == 0) { |
| 1265 | if (Ty != FTy->getReturnType()) { |
| 1266 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " does not " |
| 1267 | "match return type.", F); |
| 1268 | break; |
| 1269 | } |
| 1270 | } else { |
| 1271 | if (Ty != FTy->getParamType(Match-1)) { |
| 1272 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " does not " |
| 1273 | "match parameter %" + utostr(Match-1) + ".", F); |
| 1274 | break; |
| 1275 | } |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1276 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1277 | } else if (VT == MVT::iAny) { |
Dan Gohman | fe5b439 | 2007-08-16 22:06:45 +0000 | [diff] [blame] | 1278 | if (!EltTy->isInteger()) { |
| 1279 | if (ArgNo == 0) |
| 1280 | CheckFailed("Intrinsic result type is not " |
| 1281 | "an integer type.", F); |
| 1282 | else |
| 1283 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is not " |
| 1284 | "an integer type.", F); |
| 1285 | break; |
| 1286 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1287 | unsigned GotBits = cast<IntegerType>(EltTy)->getBitWidth(); |
| 1288 | Suffix += "."; |
| 1289 | if (EltTy != Ty) |
| 1290 | Suffix += "v" + utostr(NumElts); |
| 1291 | Suffix += "i" + utostr(GotBits);; |
Reid Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1292 | // Check some constraints on various intrinsics. |
| 1293 | switch (ID) { |
| 1294 | default: break; // Not everything needs to be checked. |
| 1295 | case Intrinsic::bswap: |
| 1296 | if (GotBits < 16 || GotBits % 16 != 0) |
| 1297 | CheckFailed("Intrinsic requires even byte width argument", F); |
Reid Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1298 | break; |
| 1299 | } |
Dan Gohman | 0fee3ff | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 1300 | } else if (VT == MVT::fAny) { |
Dan Gohman | 0fee3ff | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 1301 | if (!EltTy->isFloatingPoint()) { |
| 1302 | if (ArgNo == 0) |
| 1303 | CheckFailed("Intrinsic result type is not " |
| 1304 | "a floating-point type.", F); |
| 1305 | else |
| 1306 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is not " |
| 1307 | "a floating-point type.", F); |
| 1308 | break; |
| 1309 | } |
Dan Gohman | fe5b439 | 2007-08-16 22:06:45 +0000 | [diff] [blame] | 1310 | Suffix += "."; |
| 1311 | if (EltTy != Ty) |
| 1312 | Suffix += "v" + utostr(NumElts); |
| 1313 | Suffix += MVT::getValueTypeString(MVT::getValueType(EltTy)); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1314 | } else if (VT == MVT::iPTR) { |
| 1315 | if (!isa<PointerType>(Ty)) { |
Dan Gohman | fe5b439 | 2007-08-16 22:06:45 +0000 | [diff] [blame] | 1316 | if (ArgNo == 0) |
| 1317 | CheckFailed("Intrinsic result type is not a " |
| 1318 | "pointer and a pointer is required.", F); |
| 1319 | else |
| 1320 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is not a " |
| 1321 | "pointer and a pointer is required.", F); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1322 | break; |
| 1323 | } |
| 1324 | } else if (MVT::isVector(VT)) { |
Dan Gohman | 07a9676 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1325 | // If this is a vector argument, verify the number and type of elements. |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1326 | if (MVT::getVectorElementType(VT) != MVT::getValueType(EltTy)) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1327 | CheckFailed("Intrinsic prototype has incorrect vector element type!", |
| 1328 | F); |
| 1329 | break; |
| 1330 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1331 | if (MVT::getVectorNumElements(VT) != NumElts) { |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1332 | CheckFailed("Intrinsic prototype has incorrect number of " |
| 1333 | "vector elements!",F); |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1334 | break; |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1335 | } |
Chandler Carruth | 6994040 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1336 | } else if (MVT::getTypeForValueType(VT) != EltTy) { |
| 1337 | if (ArgNo == 0) |
| 1338 | CheckFailed("Intrinsic prototype has incorrect result type!", F); |
| 1339 | else |
| 1340 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is wrong!",F); |
| 1341 | break; |
| 1342 | } else if (EltTy != Ty) { |
| 1343 | if (ArgNo == 0) |
| 1344 | CheckFailed("Intrinsic result type is vector " |
| 1345 | "and a scalar is required.", F); |
| 1346 | else |
| 1347 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is vector " |
| 1348 | "and a scalar is required.", F); |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1349 | } |
| 1350 | } |
| 1351 | |
| 1352 | va_end(VA); |
Reid Spencer | 559d77a | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1353 | |
| 1354 | // If we computed a Suffix then the intrinsic is overloaded and we need to |
| 1355 | // make sure that the name of the function is correct. We add the suffix to |
| 1356 | // the name of the intrinsic and compare against the given function name. If |
| 1357 | // they are not the same, the function name is invalid. This ensures that |
| 1358 | // overloading of intrinsics uses a sane and consistent naming convention. |
| 1359 | if (!Suffix.empty()) { |
| 1360 | std::string Name(Intrinsic::getName(ID)); |
| 1361 | if (Name + Suffix != F->getName()) |
| 1362 | CheckFailed("Overloaded intrinsic has incorrect suffix: '" + |
| 1363 | F->getName().substr(Name.length()) + "'. It should be '" + |
| 1364 | Suffix + "'", F); |
| 1365 | } |
Chris Lattner | 536a9d5 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1368 | |
| 1369 | //===----------------------------------------------------------------------===// |
| 1370 | // Implement the public interfaces to this file... |
| 1371 | //===----------------------------------------------------------------------===// |
| 1372 | |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1373 | FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) { |
| 1374 | return new Verifier(action); |
Chris Lattner | d231fc3 | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1377 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1378 | // verifyFunction - Create |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1379 | bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) { |
Chris Lattner | 2eff859 | 2004-03-14 03:16:15 +0000 | [diff] [blame] | 1380 | Function &F = const_cast<Function&>(f); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1381 | assert(!F.isDeclaration() && "Cannot verify external functions"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1382 | |
Chris Lattner | 2eff859 | 2004-03-14 03:16:15 +0000 | [diff] [blame] | 1383 | FunctionPassManager FPM(new ExistingModuleProvider(F.getParent())); |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1384 | Verifier *V = new Verifier(action); |
Chris Lattner | 2eff859 | 2004-03-14 03:16:15 +0000 | [diff] [blame] | 1385 | FPM.add(V); |
| 1386 | FPM.run(F); |
| 1387 | return V->Broken; |
Chris Lattner | 44d5bd9 | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 1388 | } |
| 1389 | |
Misha Brukman | ab5c600 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1390 | /// verifyModule - Check a module for errors, printing messages on stderr. |
| 1391 | /// Return true if the module is corrupt. |
| 1392 | /// |
Chris Lattner | 05ac92c | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 1393 | bool llvm::verifyModule(const Module &M, VerifierFailureAction action, |
| 1394 | std::string *ErrorInfo) { |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1395 | PassManager PM; |
Chris Lattner | fdc38c4 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1396 | Verifier *V = new Verifier(action); |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1397 | PM.add(V); |
| 1398 | PM.run((Module&)M); |
Chris Lattner | 05ac92c | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 1399 | |
| 1400 | if (ErrorInfo && V->Broken) |
| 1401 | *ErrorInfo = V->msgs.str(); |
Chris Lattner | 9ce231f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1402 | return V->Broken; |
Chris Lattner | 0095054 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1403 | } |
Reid Spencer | af90b0d | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 1404 | |
| 1405 | // vim: sw=2 |