Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1 | //===-- Verifier.cpp - Implement the Module Verifier -------------*- C++ -*-==// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 3e6e3e6 | 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 | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 11 | // sanity checking of input to the system. |
| 12 | // |
Misha Brukman | 1c9de46 | 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 | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 15 | // |
Misha Brukman | 1c9de46 | 2004-06-24 21:47:35 +0000 | [diff] [blame] | 16 | // * Both of a binary operator's parameters are of the same type |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 17 | // * Verify that the indices of mem access instructions match other operands |
Misha Brukman | 1c9de46 | 2004-06-24 21:47:35 +0000 | [diff] [blame] | 18 | // * Verify that arithmetic and other things are only performed on first-class |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 19 | // types. Verify that shifts & logicals only happen on integrals f.e. |
Misha Brukman | 1c9de46 | 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 | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 21 | // * The code is in valid SSA form |
Misha Brukman | 1c9de46 | 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 | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 23 | // or to return one. [except constant arrays!] |
Nick Lewycky | f88c84f | 2008-03-28 06:46:51 +0000 | [diff] [blame] | 24 | // * Only phi nodes can be self referential: 'add i32 %0, %0 ; <int>:0' is bad |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 25 | // * PHI nodes must have an entry for each predecessor, with no extras. |
Chris Lattner | 069a795 | 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 | 4cd9df8 | 2002-10-06 21:00:31 +0000 | [diff] [blame] | 27 | // * PHI nodes must have at least one entry |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 28 | // * All basic blocks should only end with terminator insts, not contain them |
Chris Lattner | 3e6e3e6 | 2002-03-29 19:06:18 +0000 | [diff] [blame] | 29 | // * The entry node to a function must not have predecessors |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 30 | // * All Instructions must be embedded into a basic block |
Misha Brukman | 1c9de46 | 2004-06-24 21:47:35 +0000 | [diff] [blame] | 31 | // * Functions cannot take a void-typed parameter |
Chris Lattner | af95e58 | 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 | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 33 | // * It is illegal to specify a name for a void value. |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 34 | // * It is illegal to have a internal global value with no initializer |
Chris Lattner | 486302a | 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 | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 37 | // * Function call argument types match the function prototype |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 38 | // * All other things that are tested by asserts spread about the code... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 39 | // |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | |
| 42 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | 2ad5aa8 | 2005-05-08 22:27:09 +0000 | [diff] [blame] | 43 | #include "llvm/CallingConv.h" |
Chris Lattner | dc63211 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 44 | #include "llvm/Constants.h" |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 45 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 41eb5cd | 2006-01-26 00:08:45 +0000 | [diff] [blame] | 46 | #include "llvm/InlineAsm.h" |
Gordon Henriksen | be6bd16 | 2007-09-18 10:14:30 +0000 | [diff] [blame] | 47 | #include "llvm/IntrinsicInst.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 48 | #include "llvm/Module.h" |
| 49 | #include "llvm/ModuleProvider.h" |
| 50 | #include "llvm/Pass.h" |
Chris Lattner | dc63211 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 51 | #include "llvm/PassManager.h" |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 52 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 53 | #include "llvm/Assembly/Writer.h" |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 54 | #include "llvm/CodeGen/ValueTypes.h" |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 55 | #include "llvm/Support/CallSite.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 56 | #include "llvm/Support/CFG.h" |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 57 | #include "llvm/Support/InstVisitor.h" |
Bill Wendling | dfc9189 | 2006-11-28 02:09:03 +0000 | [diff] [blame] | 58 | #include "llvm/Support/Streams.h" |
Chris Lattner | bc97cc2 | 2007-02-10 08:19:44 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 84d82c7 | 2007-02-10 08:30:29 +0000 | [diff] [blame] | 60 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 62 | #include "llvm/ADT/STLExtras.h" |
Chris Lattner | 3d27be1 | 2006-08-27 12:54:02 +0000 | [diff] [blame] | 63 | #include "llvm/Support/Compiler.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 64 | #include <algorithm> |
Bill Wendling | 30c0f33 | 2006-12-07 23:41:45 +0000 | [diff] [blame] | 65 | #include <sstream> |
Jeff Cohen | e453552 | 2006-03-31 07:22:05 +0000 | [diff] [blame] | 66 | #include <cstdarg> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 67 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 69 | namespace { // Anonymous namespace for class |
Owen Anderson | 9396c39 | 2007-10-31 21:04:18 +0000 | [diff] [blame] | 70 | struct VISIBILITY_HIDDEN PreVerifier : public FunctionPass { |
Owen Anderson | fe41d79 | 2007-11-01 03:54:23 +0000 | [diff] [blame] | 71 | static char ID; // Pass ID, replacement for typeid |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 72 | |
Owen Anderson | fe41d79 | 2007-11-01 03:54:23 +0000 | [diff] [blame] | 73 | PreVerifier() : FunctionPass((intptr_t)&ID) { } |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 74 | |
| 75 | // Check that the prerequisites for successful DominatorTree construction |
| 76 | // are satisfied. |
Owen Anderson | fe41d79 | 2007-11-01 03:54:23 +0000 | [diff] [blame] | 77 | bool runOnFunction(Function &F) { |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 78 | bool Broken = false; |
| 79 | |
| 80 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 81 | if (I->empty() || !I->back().isTerminator()) { |
| 82 | cerr << "Basic Block does not have terminator!\n"; |
| 83 | WriteAsOperand(*cerr, I, true); |
| 84 | cerr << "\n"; |
| 85 | Broken = true; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (Broken) |
| 90 | abort(); |
| 91 | |
| 92 | return false; |
Owen Anderson | fe41d79 | 2007-11-01 03:54:23 +0000 | [diff] [blame] | 93 | } |
Owen Anderson | 9396c39 | 2007-10-31 21:04:18 +0000 | [diff] [blame] | 94 | }; |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 95 | } |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 96 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 97 | char PreVerifier::ID = 0; |
| 98 | static RegisterPass<PreVerifier> |
| 99 | PreVer("preverify", "Preliminary module verification"); |
Dan Gohman | 4e974ab | 2008-05-14 00:42:30 +0000 | [diff] [blame] | 100 | static const PassInfo *const PreVerifyID = &PreVer; |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 101 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 102 | namespace { |
Chris Lattner | 02157b0 | 2006-06-28 21:38:54 +0000 | [diff] [blame] | 103 | struct VISIBILITY_HIDDEN |
| 104 | Verifier : public FunctionPass, InstVisitor<Verifier> { |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 105 | static char ID; // Pass ID, replacement for typeid |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 106 | bool Broken; // Is this module found to be broken? |
| 107 | bool RealPass; // Are we not being run by a PassManager? |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 108 | VerifierFailureAction action; |
Reid Spencer | 47cf71a | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 109 | // What to do if verification fails. |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 110 | Module *Mod; // Module we are verifying right now |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 111 | DominatorTree *DT; // Dominator Tree, caution can be null! |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 112 | std::stringstream msgs; // A stringstream to collect messages |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 113 | |
Chris Lattner | c9e79d0 | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 114 | /// InstInThisBlock - when verifying a basic block, keep track of all of the |
| 115 | /// instructions we have seen so far. This allows us to do efficient |
| 116 | /// dominance checks for the case when an instruction has an operand that is |
| 117 | /// an instruction in the same block. |
Chris Lattner | bc97cc2 | 2007-02-10 08:19:44 +0000 | [diff] [blame] | 118 | SmallPtrSet<Instruction*, 16> InstsInThisBlock; |
Chris Lattner | c9e79d0 | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 119 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 120 | Verifier() |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 121 | : FunctionPass((intptr_t)&ID), |
| 122 | Broken(false), RealPass(true), action(AbortProcessAction), |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 123 | DT(0), msgs( std::ios::app | std::ios::out ) {} |
Dan Gohman | c60c67f | 2008-03-25 22:06:05 +0000 | [diff] [blame] | 124 | explicit Verifier(VerifierFailureAction ctn) |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 125 | : FunctionPass((intptr_t)&ID), |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 126 | Broken(false), RealPass(true), action(ctn), DT(0), |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 127 | msgs( std::ios::app | std::ios::out ) {} |
Dan Gohman | c60c67f | 2008-03-25 22:06:05 +0000 | [diff] [blame] | 128 | explicit Verifier(bool AB) |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 129 | : FunctionPass((intptr_t)&ID), |
| 130 | Broken(false), RealPass(true), |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 131 | action( AB ? AbortProcessAction : PrintMessageAction), DT(0), |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 132 | msgs( std::ios::app | std::ios::out ) {} |
Dan Gohman | c60c67f | 2008-03-25 22:06:05 +0000 | [diff] [blame] | 133 | explicit Verifier(DominatorTree &dt) |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 134 | : FunctionPass((intptr_t)&ID), |
| 135 | Broken(false), RealPass(false), action(PrintMessageAction), |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 136 | DT(&dt), msgs( std::ios::app | std::ios::out ) {} |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 137 | |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 139 | bool doInitialization(Module &M) { |
Brian Gaeke | 9f47927 | 2003-11-16 23:07:42 +0000 | [diff] [blame] | 140 | Mod = &M; |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 141 | verifyTypeSymbolTable(M.getTypeSymbolTable()); |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 142 | |
| 143 | // If this is a real pass, in a pass manager, we must abort before |
| 144 | // returning back to the pass manager, or else the pass manager may try to |
| 145 | // run other passes on the broken module. |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 146 | if (RealPass) |
Reid Spencer | 421475c | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 147 | return abortIfBroken(); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 151 | bool runOnFunction(Function &F) { |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 152 | // Get dominator information if we are being run by PassManager |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 153 | if (RealPass) DT = &getAnalysis<DominatorTree>(); |
Chris Lattner | af332ee | 2007-04-20 23:59:29 +0000 | [diff] [blame] | 154 | |
| 155 | Mod = F.getParent(); |
| 156 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 157 | visit(F); |
Chris Lattner | c9e79d0 | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 158 | InstsInThisBlock.clear(); |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 159 | |
| 160 | // If this is a real pass, in a pass manager, we must abort before |
| 161 | // returning back to the pass manager, or else the pass manager may try to |
| 162 | // run other passes on the broken module. |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 163 | if (RealPass) |
Reid Spencer | 421475c | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 164 | return abortIfBroken(); |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 166 | return false; |
| 167 | } |
| 168 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 169 | bool doFinalization(Module &M) { |
Chris Lattner | 9713b84 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 170 | // Scan through, checking all of the external function's linkage now... |
Chris Lattner | f75832b | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 171 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
Chris Lattner | 3ac483b | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 172 | visitGlobalValue(*I); |
Chris Lattner | 9713b84 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 173 | |
Chris Lattner | f75832b | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 174 | // Check to make sure function prototypes are okay. |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 175 | if (I->isDeclaration()) visitFunction(*I); |
Chris Lattner | f75832b | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 178 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 179 | I != E; ++I) |
Chris Lattner | e340065 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 180 | visitGlobalVariable(*I); |
Chris Lattner | 78bc0fa | 2002-10-06 22:47:32 +0000 | [diff] [blame] | 181 | |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 182 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
| 183 | I != E; ++I) |
| 184 | visitGlobalAlias(*I); |
| 185 | |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 186 | // If the module is broken, abort at this time. |
Reid Spencer | 421475c | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 187 | return abortIfBroken(); |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 190 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 191 | AU.setPreservesAll(); |
Owen Anderson | 9396c39 | 2007-10-31 21:04:18 +0000 | [diff] [blame] | 192 | AU.addRequiredID(PreVerifyID); |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 193 | if (RealPass) |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 194 | AU.addRequired<DominatorTree>(); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 197 | /// abortIfBroken - If the module is broken and we are supposed to abort on |
| 198 | /// this condition, do so. |
| 199 | /// |
Reid Spencer | 421475c | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 200 | bool abortIfBroken() { |
Chris Lattner | 5734e8d | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 201 | if (Broken) { |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 202 | msgs << "Broken module found, "; |
Chris Lattner | 5734e8d | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 203 | switch (action) { |
John Criswell | 987ad19 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 204 | case AbortProcessAction: |
| 205 | msgs << "compilation aborted!\n"; |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 206 | cerr << msgs.str(); |
John Criswell | 987ad19 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 207 | abort(); |
John Criswell | 987ad19 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 208 | case PrintMessageAction: |
| 209 | msgs << "verification continues.\n"; |
Bill Wendling | f3baad3 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 210 | cerr << msgs.str(); |
Reid Spencer | 421475c | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 211 | return false; |
John Criswell | 987ad19 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 212 | case ReturnStatusAction: |
Reid Spencer | 421475c | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 213 | msgs << "compilation terminated.\n"; |
| 214 | return Broken; |
John Criswell | 987ad19 | 2004-05-04 21:46:05 +0000 | [diff] [blame] | 215 | } |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 216 | } |
Reid Spencer | 421475c | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 217 | return false; |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Chris Lattner | 3ac483b | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 220 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 221 | // Verification methods... |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 222 | void verifyTypeSymbolTable(TypeSymbolTable &ST); |
Chris Lattner | 3ac483b | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 223 | void visitGlobalValue(GlobalValue &GV); |
Chris Lattner | e340065 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 224 | void visitGlobalVariable(GlobalVariable &GV); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 225 | void visitGlobalAlias(GlobalAlias &GA); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 226 | void visitFunction(Function &F); |
| 227 | void visitBasicBlock(BasicBlock &BB); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 228 | void visitTruncInst(TruncInst &I); |
| 229 | void visitZExtInst(ZExtInst &I); |
| 230 | void visitSExtInst(SExtInst &I); |
| 231 | void visitFPTruncInst(FPTruncInst &I); |
| 232 | void visitFPExtInst(FPExtInst &I); |
| 233 | void visitFPToUIInst(FPToUIInst &I); |
| 234 | void visitFPToSIInst(FPToSIInst &I); |
| 235 | void visitUIToFPInst(UIToFPInst &I); |
| 236 | void visitSIToFPInst(SIToFPInst &I); |
| 237 | void visitIntToPtrInst(IntToPtrInst &I); |
| 238 | void visitPtrToIntInst(PtrToIntInst &I); |
| 239 | void visitBitCastInst(BitCastInst &I); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 240 | void visitPHINode(PHINode &PN); |
| 241 | void visitBinaryOperator(BinaryOperator &B); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 242 | void visitICmpInst(ICmpInst &IC); |
| 243 | void visitFCmpInst(FCmpInst &FC); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 244 | void visitExtractElementInst(ExtractElementInst &EI); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 245 | void visitInsertElementInst(InsertElementInst &EI); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 246 | void visitShuffleVectorInst(ShuffleVectorInst &EI); |
Chris Lattner | 5b33748 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 247 | void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); } |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 248 | void visitCallInst(CallInst &CI); |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 249 | void visitInvokeInst(InvokeInst &II); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 250 | void visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 251 | void visitLoadInst(LoadInst &LI); |
| 252 | void visitStoreInst(StoreInst &SI); |
| 253 | void visitInstruction(Instruction &I); |
| 254 | void visitTerminatorInst(TerminatorInst &I); |
| 255 | void visitReturnInst(ReturnInst &RI); |
Chris Lattner | ab5aa14 | 2004-05-21 16:47:21 +0000 | [diff] [blame] | 256 | void visitSwitchInst(SwitchInst &SI); |
Chris Lattner | 75648e7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 257 | void visitSelectInst(SelectInst &SI); |
Chris Lattner | 903a25d | 2002-11-21 16:54:22 +0000 | [diff] [blame] | 258 | void visitUserOp1(Instruction &I); |
| 259 | void visitUserOp2(Instruction &I) { visitUserOp1(I); } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 260 | void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI); |
Christopher Lamb | 55c6d4f | 2007-12-17 01:00:21 +0000 | [diff] [blame] | 261 | void visitAllocationInst(AllocationInst &AI); |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 262 | void visitExtractValueInst(ExtractValueInst &EVI); |
| 263 | void visitInsertValueInst(InsertValueInst &IVI); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 264 | |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 265 | void VerifyCallSite(CallSite CS); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 266 | void VerifyIntrinsicPrototype(Intrinsic::ID ID, Function *F, |
| 267 | unsigned Count, ...); |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 268 | void VerifyAttrs(ParameterAttributes Attrs, const Type *Ty, |
| 269 | bool isReturnValue, const Value *V); |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 270 | void VerifyFunctionAttrs(const FunctionType *FT, const PAListPtr &Attrs, |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 271 | const Value *V); |
Chris Lattner | 7e5e456 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 272 | |
| 273 | void WriteValue(const Value *V) { |
| 274 | if (!V) return; |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 275 | if (isa<Instruction>(V)) { |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 276 | msgs << *V; |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 277 | } else { |
Chris Lattner | edcc8c2 | 2006-12-06 06:16:21 +0000 | [diff] [blame] | 278 | WriteAsOperand(msgs, V, true, Mod); |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 279 | msgs << "\n"; |
Chris Lattner | 7e5e456 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Chris Lattner | 85ac9b1 | 2008-08-19 04:45:47 +0000 | [diff] [blame] | 283 | void WriteType(const Type *T) { |
Reid Spencer | 47cf71a | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 284 | if ( !T ) return; |
| 285 | WriteTypeSymbolic(msgs, T, Mod ); |
| 286 | } |
| 287 | |
Chris Lattner | 7e5e456 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 288 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 289 | // CheckFailed - A check failed, so print out the condition and the message |
| 290 | // that failed. This provides a nice place to put a breakpoint if you want |
| 291 | // to see why something is not correct. |
Chris Lattner | 7e5e456 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 292 | void CheckFailed(const std::string &Message, |
| 293 | const Value *V1 = 0, const Value *V2 = 0, |
| 294 | const Value *V3 = 0, const Value *V4 = 0) { |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 295 | msgs << Message << "\n"; |
Chris Lattner | 7e5e456 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 296 | WriteValue(V1); |
| 297 | WriteValue(V2); |
| 298 | WriteValue(V3); |
| 299 | WriteValue(V4); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 300 | Broken = true; |
| 301 | } |
Reid Spencer | 47cf71a | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 302 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 303 | void CheckFailed( const std::string& Message, const Value* V1, |
Reid Spencer | 47cf71a | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 304 | const Type* T2, const Value* V3 = 0 ) { |
| 305 | msgs << Message << "\n"; |
| 306 | WriteValue(V1); |
| 307 | WriteType(T2); |
| 308 | WriteValue(V3); |
Reid Spencer | 4148139 | 2004-05-27 21:58:13 +0000 | [diff] [blame] | 309 | Broken = true; |
Reid Spencer | 47cf71a | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 310 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 311 | }; |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 312 | } // End anonymous namespace |
| 313 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 314 | char Verifier::ID = 0; |
| 315 | static RegisterPass<Verifier> X("verify", "Module Verifier"); |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 316 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 317 | // Assert - We know that cond should be true, if not print an error message. |
| 318 | #define Assert(C, M) \ |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 319 | do { if (!(C)) { CheckFailed(M); return; } } while (0) |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 320 | #define Assert1(C, M, V1) \ |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 321 | do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 322 | #define Assert2(C, M, V1, V2) \ |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 323 | do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 324 | #define Assert3(C, M, V1, V2, V3) \ |
| 325 | do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0) |
| 326 | #define Assert4(C, M, V1, V2, V3, V4) \ |
| 327 | do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 328 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 329 | |
Chris Lattner | 3ac483b | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 330 | void Verifier::visitGlobalValue(GlobalValue &GV) { |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 331 | Assert1(!GV.isDeclaration() || |
Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 332 | GV.hasExternalLinkage() || |
| 333 | GV.hasDLLImportLinkage() || |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 334 | GV.hasExternalWeakLinkage() || |
Nate Begeman | fd7b2be | 2008-07-25 17:28:23 +0000 | [diff] [blame] | 335 | GV.hasGhostLinkage() || |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 336 | (isa<GlobalAlias>(GV) && |
| 337 | (GV.hasInternalLinkage() || GV.hasWeakLinkage())), |
Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 338 | "Global is external, but doesn't have external or dllimport or weak linkage!", |
| 339 | &GV); |
| 340 | |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 341 | Assert1(!GV.hasDLLImportLinkage() || GV.isDeclaration(), |
Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 342 | "Global is marked as dllimport, but not external", &GV); |
| 343 | |
Chris Lattner | 3ac483b | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 344 | Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV), |
| 345 | "Only global variables can have appending linkage!", &GV); |
| 346 | |
| 347 | if (GV.hasAppendingLinkage()) { |
| 348 | GlobalVariable &GVar = cast<GlobalVariable>(GV); |
| 349 | Assert1(isa<ArrayType>(GVar.getType()->getElementType()), |
| 350 | "Only global arrays can have appending linkage!", &GV); |
| 351 | } |
| 352 | } |
| 353 | |
Chris Lattner | e340065 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 354 | void Verifier::visitGlobalVariable(GlobalVariable &GV) { |
Chris Lattner | d79f3d5 | 2007-09-19 17:14:45 +0000 | [diff] [blame] | 355 | if (GV.hasInitializer()) { |
Chris Lattner | e340065 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 356 | Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(), |
| 357 | "Global variable initializer type does not match global " |
| 358 | "variable type!", &GV); |
Chris Lattner | d79f3d5 | 2007-09-19 17:14:45 +0000 | [diff] [blame] | 359 | } else { |
| 360 | Assert1(GV.hasExternalLinkage() || GV.hasDLLImportLinkage() || |
| 361 | GV.hasExternalWeakLinkage(), |
| 362 | "invalid linkage type for global declaration", &GV); |
| 363 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 364 | |
Chris Lattner | e340065 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 365 | visitGlobalValue(GV); |
| 366 | } |
| 367 | |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 368 | void Verifier::visitGlobalAlias(GlobalAlias &GA) { |
| 369 | Assert1(!GA.getName().empty(), |
| 370 | "Alias name cannot be empty!", &GA); |
| 371 | Assert1(GA.hasExternalLinkage() || GA.hasInternalLinkage() || |
| 372 | GA.hasWeakLinkage(), |
| 373 | "Alias should have external or external weak linkage!", &GA); |
Anton Korobeynikov | a50eed4 | 2008-05-08 23:11:06 +0000 | [diff] [blame] | 374 | Assert1(GA.getAliasee(), |
| 375 | "Aliasee cannot be NULL!", &GA); |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 376 | Assert1(GA.getType() == GA.getAliasee()->getType(), |
| 377 | "Alias and aliasee types should match!", &GA); |
Anton Korobeynikov | a50eed4 | 2008-05-08 23:11:06 +0000 | [diff] [blame] | 378 | |
Anton Korobeynikov | 7b2a2f9 | 2007-04-28 14:35:41 +0000 | [diff] [blame] | 379 | if (!isa<GlobalValue>(GA.getAliasee())) { |
| 380 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(GA.getAliasee()); |
Anton Korobeynikov | 546ea7e | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 381 | Assert1(CE && CE->getOpcode() == Instruction::BitCast && |
| 382 | isa<GlobalValue>(CE->getOperand(0)), |
Anton Korobeynikov | 7b2a2f9 | 2007-04-28 14:35:41 +0000 | [diff] [blame] | 383 | "Aliasee should be either GlobalValue or bitcast of GlobalValue", |
| 384 | &GA); |
| 385 | } |
Anton Korobeynikov | 25b2e82 | 2008-03-22 08:36:14 +0000 | [diff] [blame] | 386 | |
| 387 | const GlobalValue* Aliasee = GA.resolveAliasedGlobal(); |
| 388 | Assert1(Aliasee, |
Anton Korobeynikov | 3f7fab9 | 2008-03-22 08:37:05 +0000 | [diff] [blame] | 389 | "Aliasing chain should end with function or global variable", &GA); |
Anton Korobeynikov | 25b2e82 | 2008-03-22 08:36:14 +0000 | [diff] [blame] | 390 | |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 391 | visitGlobalValue(GA); |
| 392 | } |
| 393 | |
Reid Spencer | 32af9e8 | 2007-01-06 07:24:44 +0000 | [diff] [blame] | 394 | void Verifier::verifyTypeSymbolTable(TypeSymbolTable &ST) { |
| 395 | } |
Chris Lattner | e340065 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 396 | |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 397 | // VerifyAttrs - Check the given parameter attributes for an argument or return |
| 398 | // value of the specified type. The value V is printed in error messages. |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 399 | void Verifier::VerifyAttrs(ParameterAttributes Attrs, const Type *Ty, |
| 400 | bool isReturnValue, const Value *V) { |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 401 | if (Attrs == ParamAttr::None) |
| 402 | return; |
| 403 | |
| 404 | if (isReturnValue) { |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 405 | ParameterAttributes RetI = Attrs & ParamAttr::ParameterOnly; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 406 | Assert1(!RetI, "Attribute " + ParamAttr::getAsString(RetI) + |
Dan Gohman | 1a70bcc | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 407 | " does not apply to return values!", V); |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 408 | } else { |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 409 | ParameterAttributes ParmI = Attrs & ParamAttr::ReturnOnly; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 410 | Assert1(!ParmI, "Attribute " + ParamAttr::getAsString(ParmI) + |
Dan Gohman | 1a70bcc | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 411 | " only applies to return values!", V); |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | for (unsigned i = 0; |
| 415 | i < array_lengthof(ParamAttr::MutuallyIncompatible); ++i) { |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 416 | ParameterAttributes MutI = Attrs & ParamAttr::MutuallyIncompatible[i]; |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 417 | Assert1(!(MutI & (MutI - 1)), "Attributes " + |
Dan Gohman | 1a70bcc | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 418 | ParamAttr::getAsString(MutI) + " are incompatible!", V); |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 421 | ParameterAttributes TypeI = Attrs & ParamAttr::typeIncompatible(Ty); |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 422 | Assert1(!TypeI, "Wrong type for attribute " + |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 423 | ParamAttr::getAsString(TypeI), V); |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | // VerifyFunctionAttrs - Check parameter attributes against a function type. |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 427 | // The value V is printed in error messages. |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 428 | void Verifier::VerifyFunctionAttrs(const FunctionType *FT, |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 429 | const PAListPtr &Attrs, |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 430 | const Value *V) { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 431 | if (Attrs.isEmpty()) |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 432 | return; |
| 433 | |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 434 | bool SawNest = false; |
| 435 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 436 | for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) { |
| 437 | const ParamAttrsWithIndex &Attr = Attrs.getSlot(i); |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 439 | const Type *Ty; |
| 440 | if (Attr.Index == 0) |
| 441 | Ty = FT->getReturnType(); |
| 442 | else if (Attr.Index-1 < FT->getNumParams()) |
| 443 | Ty = FT->getParamType(Attr.Index-1); |
| 444 | else |
| 445 | break; // VarArgs attributes, don't verify. |
| 446 | |
| 447 | VerifyAttrs(Attr.Attrs, Ty, Attr.Index == 0, V); |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 448 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 449 | if (Attr.Attrs & ParamAttr::Nest) { |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 450 | Assert1(!SawNest, "More than one parameter has attribute nest!", V); |
| 451 | SawNest = true; |
| 452 | } |
| 453 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 454 | if (Attr.Attrs & ParamAttr::StructRet) |
| 455 | Assert1(Attr.Index == 1, "Attribute sret not on first parameter!", V); |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 459 | // visitFunction - Verify that a function is ok. |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 460 | // |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 461 | void Verifier::visitFunction(Function &F) { |
Chris Lattner | 2ad5aa8 | 2005-05-08 22:27:09 +0000 | [diff] [blame] | 462 | // Check function arguments. |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 463 | const FunctionType *FT = F.getFunctionType(); |
Chris Lattner | 45ffa21 | 2007-08-18 06:13:19 +0000 | [diff] [blame] | 464 | unsigned NumArgs = F.arg_size(); |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 465 | |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 466 | Assert2(FT->getNumParams() == NumArgs, |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 467 | "# formal arguments must match # of arguments for function type!", |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 468 | &F, FT); |
Chris Lattner | 3ae303c | 2003-11-21 22:32:23 +0000 | [diff] [blame] | 469 | Assert1(F.getReturnType()->isFirstClassType() || |
Devang Patel | 616f0e0 | 2008-02-20 22:36:03 +0000 | [diff] [blame] | 470 | F.getReturnType() == Type::VoidTy || |
Devang Patel | 9fea019 | 2008-02-21 22:24:17 +0000 | [diff] [blame] | 471 | isa<StructType>(F.getReturnType()), |
Chris Lattner | 3ae303c | 2003-11-21 22:32:23 +0000 | [diff] [blame] | 472 | "Functions cannot return aggregate values!", &F); |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 473 | |
Devang Patel | 9d917859 | 2008-03-03 21:46:28 +0000 | [diff] [blame] | 474 | Assert1(!F.hasStructRetAttr() || F.getReturnType() == Type::VoidTy, |
| 475 | "Invalid struct return type!", &F); |
| 476 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 477 | const PAListPtr &Attrs = F.getParamAttrs(); |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 478 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 479 | Assert1(Attrs.isEmpty() || |
| 480 | Attrs.getSlot(Attrs.getNumSlots()-1).Index <= FT->getNumParams(), |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 481 | "Attributes after last parameter!", &F); |
| 482 | |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 483 | // Check function attributes. |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 484 | VerifyFunctionAttrs(FT, Attrs, &F); |
Duncan Sands | 07c9066 | 2007-07-27 15:09:54 +0000 | [diff] [blame] | 485 | |
Chris Lattner | ef13ee3 | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 486 | // Check that this function meets the restrictions on this calling convention. |
| 487 | switch (F.getCallingConv()) { |
| 488 | default: |
| 489 | break; |
| 490 | case CallingConv::C: |
Dale Johannesen | 332dd53 | 2008-08-13 18:40:23 +0000 | [diff] [blame] | 491 | case CallingConv::X86_SSECall: |
Chris Lattner | ef13ee3 | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 492 | break; |
Chris Lattner | ef13ee3 | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 493 | case CallingConv::Fast: |
| 494 | case CallingConv::Cold: |
Anton Korobeynikov | 6f7072c | 2006-09-17 20:25:45 +0000 | [diff] [blame] | 495 | case CallingConv::X86_FastCall: |
Chris Lattner | ef13ee3 | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 496 | Assert1(!F.isVarArg(), |
| 497 | "Varargs functions must have C calling conventions!", &F); |
| 498 | break; |
| 499 | } |
| 500 | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 501 | // Check that the argument values match the function type for this function... |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 502 | unsigned i = 0; |
Chris Lattner | 6976177 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 503 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 504 | I != E; ++I, ++i) { |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 505 | Assert2(I->getType() == FT->getParamType(i), |
| 506 | "Argument value does not match function argument type!", |
| 507 | I, FT->getParamType(i)); |
Chris Lattner | f75832b | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 508 | // Make sure no aggregates are passed by value. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 509 | Assert1(I->getType()->isFirstClassType(), |
Chris Lattner | f75832b | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 510 | "Functions cannot take aggregates as arguments by value!", I); |
| 511 | } |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 512 | |
Chris Lattner | d79f3d5 | 2007-09-19 17:14:45 +0000 | [diff] [blame] | 513 | if (F.isDeclaration()) { |
| 514 | Assert1(F.hasExternalLinkage() || F.hasDLLImportLinkage() || |
Nate Begeman | fd7b2be | 2008-07-25 17:28:23 +0000 | [diff] [blame] | 515 | F.hasExternalWeakLinkage() || F.hasGhostLinkage(), |
Chris Lattner | d79f3d5 | 2007-09-19 17:14:45 +0000 | [diff] [blame] | 516 | "invalid linkage type for function declaration", &F); |
| 517 | } else { |
Chris Lattner | cb81331 | 2006-12-13 04:45:46 +0000 | [diff] [blame] | 518 | // Verify that this function (which has a body) is not named "llvm.*". It |
| 519 | // is not legal to define intrinsics. |
| 520 | if (F.getName().size() >= 5) |
| 521 | Assert1(F.getName().substr(0, 5) != "llvm.", |
| 522 | "llvm intrinsics cannot be defined!", &F); |
| 523 | |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 524 | // Check the entry node |
Chris Lattner | 5dac64f | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 525 | BasicBlock *Entry = &F.getEntryBlock(); |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 526 | Assert1(pred_begin(Entry) == pred_end(Entry), |
| 527 | "Entry block to function must not have predecessors!", Entry); |
| 528 | } |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 532 | // verifyBasicBlock - Verify that a basic block is well formed... |
| 533 | // |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 534 | void Verifier::visitBasicBlock(BasicBlock &BB) { |
Chris Lattner | c9e79d0 | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 535 | InstsInThisBlock.clear(); |
| 536 | |
Alkis Evlogimenos | be526cf | 2004-12-04 02:30:42 +0000 | [diff] [blame] | 537 | // Ensure that basic blocks have terminators! |
| 538 | Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB); |
| 539 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 540 | // Check constraints that this basic block imposes on all of the PHI nodes in |
| 541 | // it. |
| 542 | if (isa<PHINode>(BB.front())) { |
Chris Lattner | 59a8d2c | 2007-02-10 08:33:11 +0000 | [diff] [blame] | 543 | SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB)); |
| 544 | SmallVector<std::pair<BasicBlock*, Value*>, 8> Values; |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 545 | std::sort(Preds.begin(), Preds.end()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 546 | PHINode *PN; |
Chris Lattner | 307e1df | 2004-06-05 17:44:48 +0000 | [diff] [blame] | 547 | for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) { |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 548 | |
| 549 | // Ensure that PHI nodes have at least one entry! |
| 550 | Assert1(PN->getNumIncomingValues() != 0, |
| 551 | "PHI nodes must have at least one entry. If the block is dead, " |
| 552 | "the PHI should be removed!", PN); |
Brian Gaeke | e8a6bf3 | 2004-05-17 21:15:18 +0000 | [diff] [blame] | 553 | Assert1(PN->getNumIncomingValues() == Preds.size(), |
| 554 | "PHINode should have one entry for each predecessor of its " |
| 555 | "parent basic block!", PN); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 556 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 557 | // Get and sort all incoming values in the PHI node... |
Chris Lattner | 59a8d2c | 2007-02-10 08:33:11 +0000 | [diff] [blame] | 558 | Values.clear(); |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 559 | Values.reserve(PN->getNumIncomingValues()); |
| 560 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 561 | Values.push_back(std::make_pair(PN->getIncomingBlock(i), |
| 562 | PN->getIncomingValue(i))); |
| 563 | std::sort(Values.begin(), Values.end()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 564 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 565 | for (unsigned i = 0, e = Values.size(); i != e; ++i) { |
| 566 | // Check to make sure that if there is more than one entry for a |
| 567 | // particular basic block in this PHI node, that the incoming values are |
| 568 | // all identical. |
| 569 | // |
| 570 | Assert4(i == 0 || Values[i].first != Values[i-1].first || |
| 571 | Values[i].second == Values[i-1].second, |
| 572 | "PHI node has multiple entries for the same basic block with " |
| 573 | "different incoming values!", PN, Values[i].first, |
| 574 | Values[i].second, Values[i-1].second); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 575 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 576 | // Check to make sure that the predecessors and PHI node entries are |
| 577 | // matched up. |
| 578 | Assert3(Values[i].first == Preds[i], |
| 579 | "PHI node entries do not match predecessors!", PN, |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 580 | Values[i].first, Preds[i]); |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | } |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 584 | } |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 585 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 586 | void Verifier::visitTerminatorInst(TerminatorInst &I) { |
| 587 | // Ensure that terminators only exist at the end of the basic block. |
| 588 | Assert1(&I == I.getParent()->getTerminator(), |
| 589 | "Terminator found in the middle of a basic block!", I.getParent()); |
Chris Lattner | 7af3ee9 | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 590 | visitInstruction(I); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | void Verifier::visitReturnInst(ReturnInst &RI) { |
| 594 | Function *F = RI.getParent()->getParent(); |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 595 | unsigned N = RI.getNumOperands(); |
Chris Lattner | 1e31bf5 | 2008-04-23 20:33:41 +0000 | [diff] [blame] | 596 | if (F->getReturnType() == Type::VoidTy) |
| 597 | Assert2(N == 0, |
Alkis Evlogimenos | b92de19 | 2004-12-04 01:25:06 +0000 | [diff] [blame] | 598 | "Found return instr that returns void in Function of non-void " |
| 599 | "return type!", &RI, F->getReturnType()); |
Dan Gohman | 27ae953 | 2008-06-09 21:26:13 +0000 | [diff] [blame] | 600 | else if (N == 1 && F->getReturnType() == RI.getOperand(0)->getType()) { |
| 601 | // Exactly one return value and it matches the return type. Good. |
| 602 | } else if (const StructType *STy = dyn_cast<StructType>(F->getReturnType())) { |
| 603 | // The return type is a struct; check for multiple return values. |
Chris Lattner | 1e31bf5 | 2008-04-23 20:33:41 +0000 | [diff] [blame] | 604 | Assert2(STy->getNumElements() == N, |
| 605 | "Incorrect number of return values in ret instruction!", |
| 606 | &RI, F->getReturnType()); |
| 607 | for (unsigned i = 0; i != N; ++i) |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 608 | Assert2(STy->getElementType(i) == RI.getOperand(i)->getType(), |
Devang Patel | f287e7d | 2008-02-26 22:55:21 +0000 | [diff] [blame] | 609 | "Function return type does not match operand " |
| 610 | "type of return inst!", &RI, F->getReturnType()); |
Dan Gohman | 27ae953 | 2008-06-09 21:26:13 +0000 | [diff] [blame] | 611 | } else if (const ArrayType *ATy = dyn_cast<ArrayType>(F->getReturnType())) { |
| 612 | // The return type is an array; check for multiple return values. |
| 613 | Assert2(ATy->getNumElements() == N, |
| 614 | "Incorrect number of return values in ret instruction!", |
| 615 | &RI, F->getReturnType()); |
| 616 | for (unsigned i = 0; i != N; ++i) |
| 617 | Assert2(ATy->getElementType() == RI.getOperand(i)->getType(), |
| 618 | "Function return type does not match operand " |
| 619 | "type of return inst!", &RI, F->getReturnType()); |
Chris Lattner | 1e31bf5 | 2008-04-23 20:33:41 +0000 | [diff] [blame] | 620 | } else { |
Dan Gohman | 27ae953 | 2008-06-09 21:26:13 +0000 | [diff] [blame] | 621 | CheckFailed("Function return type does not match operand " |
| 622 | "type of return inst!", &RI, F->getReturnType()); |
Chris Lattner | 1e31bf5 | 2008-04-23 20:33:41 +0000 | [diff] [blame] | 623 | } |
Devang Patel | b3ca38c | 2008-03-05 00:27:05 +0000 | [diff] [blame] | 624 | |
Misha Brukman | 7eb05a1 | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 625 | // Check to make sure that the return value has necessary properties for |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 626 | // terminators... |
| 627 | visitTerminatorInst(RI); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Chris Lattner | ab5aa14 | 2004-05-21 16:47:21 +0000 | [diff] [blame] | 630 | void Verifier::visitSwitchInst(SwitchInst &SI) { |
| 631 | // Check to make sure that all of the constants in the switch instruction |
| 632 | // have the same type as the switched-on value. |
| 633 | const Type *SwitchTy = SI.getCondition()->getType(); |
| 634 | for (unsigned i = 1, e = SI.getNumCases(); i != e; ++i) |
| 635 | Assert1(SI.getCaseValue(i)->getType() == SwitchTy, |
| 636 | "Switch constants must all be same type as switch value!", &SI); |
| 637 | |
| 638 | visitTerminatorInst(SI); |
| 639 | } |
| 640 | |
Chris Lattner | 75648e7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 641 | void Verifier::visitSelectInst(SelectInst &SI) { |
Reid Spencer | 542964f | 2007-01-11 18:21:29 +0000 | [diff] [blame] | 642 | Assert1(SI.getCondition()->getType() == Type::Int1Ty, |
Chris Lattner | 75648e7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 643 | "Select condition type must be bool!", &SI); |
| 644 | Assert1(SI.getTrueValue()->getType() == SI.getFalseValue()->getType(), |
| 645 | "Select values must have identical types!", &SI); |
| 646 | Assert1(SI.getTrueValue()->getType() == SI.getType(), |
| 647 | "Select values must have same type as select instruction!", &SI); |
Chris Lattner | cde15fb | 2004-09-29 21:19:28 +0000 | [diff] [blame] | 648 | visitInstruction(SI); |
Chris Lattner | 75648e7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 652 | /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of |
| 653 | /// a pass, if any exist, it's an error. |
| 654 | /// |
Chris Lattner | 903a25d | 2002-11-21 16:54:22 +0000 | [diff] [blame] | 655 | void Verifier::visitUserOp1(Instruction &I) { |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 656 | Assert1(0, "User-defined operators should not live outside of a pass!", &I); |
Chris Lattner | 903a25d | 2002-11-21 16:54:22 +0000 | [diff] [blame] | 657 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 658 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 659 | void Verifier::visitTruncInst(TruncInst &I) { |
| 660 | // Get the source and destination types |
| 661 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 662 | const Type *DestTy = I.getType(); |
| 663 | |
| 664 | // Get the size of the types in bits, we'll need this later |
| 665 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 666 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 667 | |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 668 | Assert1(SrcTy->isIntOrIntVector(), "Trunc only operates on integer", &I); |
| 669 | Assert1(DestTy->isIntOrIntVector(), "Trunc only produces integer", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 670 | Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I); |
| 671 | |
| 672 | visitInstruction(I); |
| 673 | } |
| 674 | |
| 675 | void Verifier::visitZExtInst(ZExtInst &I) { |
| 676 | // Get the source and destination types |
| 677 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 678 | const Type *DestTy = I.getType(); |
| 679 | |
| 680 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 681 | Assert1(SrcTy->isIntOrIntVector(), "ZExt only operates on integer", &I); |
| 682 | Assert1(DestTy->isIntOrIntVector(), "ZExt only produces an integer", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 683 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 684 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 685 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 686 | Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I); |
| 687 | |
| 688 | visitInstruction(I); |
| 689 | } |
| 690 | |
| 691 | void Verifier::visitSExtInst(SExtInst &I) { |
| 692 | // Get the source and destination types |
| 693 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 694 | const Type *DestTy = I.getType(); |
| 695 | |
| 696 | // Get the size of the types in bits, we'll need this later |
| 697 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 698 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 699 | |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 700 | Assert1(SrcTy->isIntOrIntVector(), "SExt only operates on integer", &I); |
| 701 | Assert1(DestTy->isIntOrIntVector(), "SExt only produces an integer", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 702 | Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I); |
| 703 | |
| 704 | visitInstruction(I); |
| 705 | } |
| 706 | |
| 707 | void Verifier::visitFPTruncInst(FPTruncInst &I) { |
| 708 | // Get the source and destination types |
| 709 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 710 | const Type *DestTy = I.getType(); |
| 711 | // Get the size of the types in bits, we'll need this later |
| 712 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 713 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 714 | |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 715 | Assert1(SrcTy->isFPOrFPVector(),"FPTrunc only operates on FP", &I); |
| 716 | Assert1(DestTy->isFPOrFPVector(),"FPTrunc only produces an FP", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 717 | Assert1(SrcBitSize > DestBitSize,"DestTy too big for FPTrunc", &I); |
| 718 | |
| 719 | visitInstruction(I); |
| 720 | } |
| 721 | |
| 722 | void Verifier::visitFPExtInst(FPExtInst &I) { |
| 723 | // Get the source and destination types |
| 724 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 725 | const Type *DestTy = I.getType(); |
| 726 | |
| 727 | // Get the size of the types in bits, we'll need this later |
| 728 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 729 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 730 | |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 731 | Assert1(SrcTy->isFPOrFPVector(),"FPExt only operates on FP", &I); |
| 732 | Assert1(DestTy->isFPOrFPVector(),"FPExt only produces an FP", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 733 | Assert1(SrcBitSize < DestBitSize,"DestTy too small for FPExt", &I); |
| 734 | |
| 735 | visitInstruction(I); |
| 736 | } |
| 737 | |
| 738 | void Verifier::visitUIToFPInst(UIToFPInst &I) { |
| 739 | // Get the source and destination types |
| 740 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 741 | const Type *DestTy = I.getType(); |
| 742 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 743 | bool SrcVec = isa<VectorType>(SrcTy); |
| 744 | bool DstVec = isa<VectorType>(DestTy); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 745 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 746 | Assert1(SrcVec == DstVec, |
| 747 | "UIToFP source and dest must both be vector or scalar", &I); |
| 748 | Assert1(SrcTy->isIntOrIntVector(), |
| 749 | "UIToFP source must be integer or integer vector", &I); |
| 750 | Assert1(DestTy->isFPOrFPVector(), |
| 751 | "UIToFP result must be FP or FP vector", &I); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 752 | |
| 753 | if (SrcVec && DstVec) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 754 | Assert1(cast<VectorType>(SrcTy)->getNumElements() == |
| 755 | cast<VectorType>(DestTy)->getNumElements(), |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 756 | "UIToFP source and dest vector length mismatch", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 757 | |
| 758 | visitInstruction(I); |
| 759 | } |
| 760 | |
| 761 | void Verifier::visitSIToFPInst(SIToFPInst &I) { |
| 762 | // Get the source and destination types |
| 763 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 764 | const Type *DestTy = I.getType(); |
| 765 | |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 766 | bool SrcVec = SrcTy->getTypeID() == Type::VectorTyID; |
| 767 | bool DstVec = DestTy->getTypeID() == Type::VectorTyID; |
| 768 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 769 | Assert1(SrcVec == DstVec, |
| 770 | "SIToFP source and dest must both be vector or scalar", &I); |
| 771 | Assert1(SrcTy->isIntOrIntVector(), |
| 772 | "SIToFP source must be integer or integer vector", &I); |
| 773 | Assert1(DestTy->isFPOrFPVector(), |
| 774 | "SIToFP result must be FP or FP vector", &I); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 775 | |
| 776 | if (SrcVec && DstVec) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 777 | Assert1(cast<VectorType>(SrcTy)->getNumElements() == |
| 778 | cast<VectorType>(DestTy)->getNumElements(), |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 779 | "SIToFP source and dest vector length mismatch", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 780 | |
| 781 | visitInstruction(I); |
| 782 | } |
| 783 | |
| 784 | void Verifier::visitFPToUIInst(FPToUIInst &I) { |
| 785 | // Get the source and destination types |
| 786 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 787 | const Type *DestTy = I.getType(); |
| 788 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 789 | bool SrcVec = isa<VectorType>(SrcTy); |
| 790 | bool DstVec = isa<VectorType>(DestTy); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 791 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 792 | Assert1(SrcVec == DstVec, |
| 793 | "FPToUI source and dest must both be vector or scalar", &I); |
| 794 | Assert1(SrcTy->isFPOrFPVector(), "FPToUI source must be FP or FP vector", &I); |
| 795 | Assert1(DestTy->isIntOrIntVector(), |
| 796 | "FPToUI result must be integer or integer vector", &I); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 797 | |
| 798 | if (SrcVec && DstVec) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 799 | Assert1(cast<VectorType>(SrcTy)->getNumElements() == |
| 800 | cast<VectorType>(DestTy)->getNumElements(), |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 801 | "FPToUI source and dest vector length mismatch", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 802 | |
| 803 | visitInstruction(I); |
| 804 | } |
| 805 | |
| 806 | void Verifier::visitFPToSIInst(FPToSIInst &I) { |
| 807 | // Get the source and destination types |
| 808 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 809 | const Type *DestTy = I.getType(); |
| 810 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 811 | bool SrcVec = isa<VectorType>(SrcTy); |
| 812 | bool DstVec = isa<VectorType>(DestTy); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 813 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 814 | Assert1(SrcVec == DstVec, |
| 815 | "FPToSI source and dest must both be vector or scalar", &I); |
| 816 | Assert1(SrcTy->isFPOrFPVector(), |
| 817 | "FPToSI source must be FP or FP vector", &I); |
| 818 | Assert1(DestTy->isIntOrIntVector(), |
| 819 | "FPToSI result must be integer or integer vector", &I); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 820 | |
| 821 | if (SrcVec && DstVec) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 822 | Assert1(cast<VectorType>(SrcTy)->getNumElements() == |
| 823 | cast<VectorType>(DestTy)->getNumElements(), |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 824 | "FPToSI source and dest vector length mismatch", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 825 | |
| 826 | visitInstruction(I); |
| 827 | } |
| 828 | |
| 829 | void Verifier::visitPtrToIntInst(PtrToIntInst &I) { |
| 830 | // Get the source and destination types |
| 831 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 832 | const Type *DestTy = I.getType(); |
| 833 | |
| 834 | Assert1(isa<PointerType>(SrcTy), "PtrToInt source must be pointer", &I); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 835 | Assert1(DestTy->isInteger(), "PtrToInt result must be integral", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 836 | |
| 837 | visitInstruction(I); |
| 838 | } |
| 839 | |
| 840 | void Verifier::visitIntToPtrInst(IntToPtrInst &I) { |
| 841 | // Get the source and destination types |
| 842 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 843 | const Type *DestTy = I.getType(); |
| 844 | |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 845 | Assert1(SrcTy->isInteger(), "IntToPtr source must be an integral", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 846 | Assert1(isa<PointerType>(DestTy), "IntToPtr result must be a pointer",&I); |
| 847 | |
| 848 | visitInstruction(I); |
| 849 | } |
| 850 | |
| 851 | void Verifier::visitBitCastInst(BitCastInst &I) { |
| 852 | // Get the source and destination types |
| 853 | const Type *SrcTy = I.getOperand(0)->getType(); |
| 854 | const Type *DestTy = I.getType(); |
| 855 | |
| 856 | // Get the size of the types in bits, we'll need this later |
| 857 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 858 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 859 | |
| 860 | // BitCast implies a no-op cast of type only. No bits change. |
| 861 | // However, you can't cast pointers to anything but pointers. |
| 862 | Assert1(isa<PointerType>(DestTy) == isa<PointerType>(DestTy), |
| 863 | "Bitcast requires both operands to be pointer or neither", &I); |
| 864 | Assert1(SrcBitSize == DestBitSize, "Bitcast requies types of same width", &I); |
| 865 | |
| 866 | visitInstruction(I); |
| 867 | } |
| 868 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 869 | /// visitPHINode - Ensure that a PHI node is well formed. |
| 870 | /// |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 871 | void Verifier::visitPHINode(PHINode &PN) { |
| 872 | // Ensure that the PHI nodes are all grouped together at the top of the block. |
| 873 | // This can be tested by checking whether the instruction before this is |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 874 | // either nonexistent (because this is begin()) or is a PHI node. If not, |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 875 | // then there is some other instruction before a PHI. |
Chris Lattner | da1ed8d | 2007-04-17 17:36:12 +0000 | [diff] [blame] | 876 | Assert2(&PN == &PN.getParent()->front() || |
| 877 | isa<PHINode>(--BasicBlock::iterator(&PN)), |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 878 | "PHI nodes not grouped at top of basic block!", |
| 879 | &PN, PN.getParent()); |
| 880 | |
Chris Lattner | 3b93c91 | 2003-11-12 07:13:37 +0000 | [diff] [blame] | 881 | // Check that all of the operands of the PHI node have the same type as the |
| 882 | // result. |
| 883 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
| 884 | Assert1(PN.getType() == PN.getIncomingValue(i)->getType(), |
| 885 | "PHI node operands are not the same type as the result!", &PN); |
| 886 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 887 | // All other PHI node constraints are checked in the visitBasicBlock method. |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 888 | |
| 889 | visitInstruction(PN); |
| 890 | } |
| 891 | |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 892 | void Verifier::VerifyCallSite(CallSite CS) { |
| 893 | Instruction *I = CS.getInstruction(); |
| 894 | |
| 895 | Assert1(isa<PointerType>(CS.getCalledValue()->getType()), |
| 896 | "Called function must be a pointer!", I); |
| 897 | const PointerType *FPTy = cast<PointerType>(CS.getCalledValue()->getType()); |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 898 | Assert1(isa<FunctionType>(FPTy->getElementType()), |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 899 | "Called function is not pointer to function type!", I); |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 900 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 901 | const FunctionType *FTy = cast<FunctionType>(FPTy->getElementType()); |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 902 | |
| 903 | // Verify that the correct number of arguments are being passed |
| 904 | if (FTy->isVarArg()) |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 905 | Assert1(CS.arg_size() >= FTy->getNumParams(), |
| 906 | "Called function requires more parameters than were provided!",I); |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 907 | else |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 908 | Assert1(CS.arg_size() == FTy->getNumParams(), |
| 909 | "Incorrect number of arguments passed to called function!", I); |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 910 | |
| 911 | // Verify that all arguments to the call match the function type... |
| 912 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 913 | Assert3(CS.getArgument(i)->getType() == FTy->getParamType(i), |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 914 | "Call parameter type does not match function signature!", |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 915 | CS.getArgument(i), FTy->getParamType(i), I); |
| 916 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 917 | const PAListPtr &Attrs = CS.getParamAttrs(); |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 918 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 919 | Assert1(Attrs.isEmpty() || |
| 920 | Attrs.getSlot(Attrs.getNumSlots()-1).Index <= CS.arg_size(), |
| 921 | "Attributes after last parameter!", I); |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 922 | |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 923 | // Verify call attributes. |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 924 | VerifyFunctionAttrs(FTy, Attrs, I); |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 925 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 926 | if (FTy->isVarArg()) |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 927 | // Check attributes on the varargs part. |
| 928 | for (unsigned Idx = 1 + FTy->getNumParams(); Idx <= CS.arg_size(); ++Idx) { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 929 | ParameterAttributes Attr = Attrs.getParamAttrs(Idx); |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 930 | |
| 931 | VerifyAttrs(Attr, CS.getArgument(Idx-1)->getType(), false, I); |
| 932 | |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 933 | ParameterAttributes VArgI = Attr & ParamAttr::VarArgsIncompatible; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 934 | Assert1(!VArgI, "Attribute " + ParamAttr::getAsString(VArgI) + |
Dan Gohman | 1a70bcc | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 935 | " cannot be used for vararg call arguments!", I); |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 936 | } |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 937 | |
| 938 | visitInstruction(*I); |
| 939 | } |
| 940 | |
| 941 | void Verifier::visitCallInst(CallInst &CI) { |
| 942 | VerifyCallSite(&CI); |
Chris Lattner | 7af3ee9 | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 943 | |
Anton Korobeynikov | 9dced3f | 2007-10-28 22:50:32 +0000 | [diff] [blame] | 944 | if (Function *F = CI.getCalledFunction()) { |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 945 | if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 946 | visitIntrinsicFunctionCall(ID, CI); |
Anton Korobeynikov | 9dced3f | 2007-10-28 22:50:32 +0000 | [diff] [blame] | 947 | } |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | void Verifier::visitInvokeInst(InvokeInst &II) { |
| 951 | VerifyCallSite(&II); |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 952 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 953 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 954 | /// visitBinaryOperator - Check that both arguments to the binary operator are |
| 955 | /// of the same type! |
| 956 | /// |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 957 | void Verifier::visitBinaryOperator(BinaryOperator &B) { |
Chris Lattner | 1f41925 | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 958 | Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(), |
| 959 | "Both operands to a binary operator are not of the same type!", &B); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 960 | |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 961 | switch (B.getOpcode()) { |
Chris Lattner | 1f41925 | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 962 | // Check that logical operators are only used with integral operands. |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 963 | case Instruction::And: |
| 964 | case Instruction::Or: |
| 965 | case Instruction::Xor: |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 966 | Assert1(B.getType()->isInteger() || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 967 | (isa<VectorType>(B.getType()) && |
| 968 | cast<VectorType>(B.getType())->getElementType()->isInteger()), |
Chris Lattner | 1f41925 | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 969 | "Logical operators only work with integral types!", &B); |
| 970 | Assert1(B.getType() == B.getOperand(0)->getType(), |
| 971 | "Logical operators must have same type for operands and result!", |
| 972 | &B); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 973 | break; |
| 974 | case Instruction::Shl: |
| 975 | case Instruction::LShr: |
| 976 | case Instruction::AShr: |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 977 | Assert1(B.getType()->isInteger() || |
| 978 | (isa<VectorType>(B.getType()) && |
| 979 | cast<VectorType>(B.getType())->getElementType()->isInteger()), |
| 980 | "Shifts only work with integral types!", &B); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 981 | Assert1(B.getType() == B.getOperand(0)->getType(), |
| 982 | "Shift return type must be same as operands!", &B); |
| 983 | /* FALL THROUGH */ |
| 984 | default: |
Chris Lattner | 1f41925 | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 985 | // Arithmetic operators only work on integer or fp values |
| 986 | Assert1(B.getType() == B.getOperand(0)->getType(), |
| 987 | "Arithmetic operators must have same type for operands and result!", |
| 988 | &B); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 989 | Assert1(B.getType()->isInteger() || B.getType()->isFloatingPoint() || |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 990 | isa<VectorType>(B.getType()), |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 991 | "Arithmetic operators must have integer, fp, or vector type!", &B); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 992 | break; |
Chris Lattner | 1f41925 | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 993 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 994 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 995 | visitInstruction(B); |
| 996 | } |
| 997 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 998 | void Verifier::visitICmpInst(ICmpInst& IC) { |
| 999 | // Check that the operands are the same type |
| 1000 | const Type* Op0Ty = IC.getOperand(0)->getType(); |
| 1001 | const Type* Op1Ty = IC.getOperand(1)->getType(); |
| 1002 | Assert1(Op0Ty == Op1Ty, |
| 1003 | "Both operands to ICmp instruction are not of the same type!", &IC); |
| 1004 | // Check that the operands are the right type |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 1005 | Assert1(Op0Ty->isInteger() || isa<PointerType>(Op0Ty), |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1006 | "Invalid operand types for ICmp instruction", &IC); |
| 1007 | visitInstruction(IC); |
| 1008 | } |
| 1009 | |
| 1010 | void Verifier::visitFCmpInst(FCmpInst& FC) { |
| 1011 | // Check that the operands are the same type |
| 1012 | const Type* Op0Ty = FC.getOperand(0)->getType(); |
| 1013 | const Type* Op1Ty = FC.getOperand(1)->getType(); |
| 1014 | Assert1(Op0Ty == Op1Ty, |
| 1015 | "Both operands to FCmp instruction are not of the same type!", &FC); |
| 1016 | // Check that the operands are the right type |
Reid Spencer | 438f562 | 2007-01-04 05:22:18 +0000 | [diff] [blame] | 1017 | Assert1(Op0Ty->isFloatingPoint(), |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1018 | "Invalid operand types for FCmp instruction", &FC); |
| 1019 | visitInstruction(FC); |
| 1020 | } |
| 1021 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1022 | void Verifier::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 38c4cb2 | 2006-04-08 04:07:52 +0000 | [diff] [blame] | 1023 | Assert1(ExtractElementInst::isValidOperands(EI.getOperand(0), |
| 1024 | EI.getOperand(1)), |
| 1025 | "Invalid extractelement operands!", &EI); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1026 | visitInstruction(EI); |
| 1027 | } |
| 1028 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1029 | void Verifier::visitInsertElementInst(InsertElementInst &IE) { |
Chris Lattner | 38c4cb2 | 2006-04-08 04:07:52 +0000 | [diff] [blame] | 1030 | Assert1(InsertElementInst::isValidOperands(IE.getOperand(0), |
| 1031 | IE.getOperand(1), |
| 1032 | IE.getOperand(2)), |
| 1033 | "Invalid insertelement operands!", &IE); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1034 | visitInstruction(IE); |
| 1035 | } |
| 1036 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1037 | void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) { |
| 1038 | Assert1(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1), |
| 1039 | SV.getOperand(2)), |
| 1040 | "Invalid shufflevector operands!", &SV); |
| 1041 | Assert1(SV.getType() == SV.getOperand(0)->getType(), |
| 1042 | "Result of shufflevector must match first operand type!", &SV); |
| 1043 | |
| 1044 | // Check to see if Mask is valid. |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1045 | if (const ConstantVector *MV = dyn_cast<ConstantVector>(SV.getOperand(2))) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1046 | for (unsigned i = 0, e = MV->getNumOperands(); i != e; ++i) { |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1047 | if (ConstantInt* CI = dyn_cast<ConstantInt>(MV->getOperand(i))) { |
| 1048 | Assert1(!CI->uge(MV->getNumOperands()*2), |
| 1049 | "Invalid shufflevector shuffle mask!", &SV); |
| 1050 | } else { |
| 1051 | Assert1(isa<UndefValue>(MV->getOperand(i)), |
| 1052 | "Invalid shufflevector shuffle mask!", &SV); |
| 1053 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1054 | } |
| 1055 | } else { |
| 1056 | Assert1(isa<UndefValue>(SV.getOperand(2)) || |
| 1057 | isa<ConstantAggregateZero>(SV.getOperand(2)), |
| 1058 | "Invalid shufflevector shuffle mask!", &SV); |
| 1059 | } |
| 1060 | |
| 1061 | visitInstruction(SV); |
| 1062 | } |
| 1063 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1064 | void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 84d82c7 | 2007-02-10 08:30:29 +0000 | [diff] [blame] | 1065 | SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end()); |
Chris Lattner | dfb3a2c | 2002-08-22 23:37:20 +0000 | [diff] [blame] | 1066 | const Type *ElTy = |
| 1067 | GetElementPtrInst::getIndexedType(GEP.getOperand(0)->getType(), |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1068 | Idxs.begin(), Idxs.end()); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1069 | Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP); |
Chris Lattner | 84d82c7 | 2007-02-10 08:30:29 +0000 | [diff] [blame] | 1070 | Assert2(isa<PointerType>(GEP.getType()) && |
| 1071 | cast<PointerType>(GEP.getType())->getElementType() == ElTy, |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1072 | "GEP is not of right type for indices!", &GEP, ElTy); |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 1073 | visitInstruction(GEP); |
| 1074 | } |
| 1075 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1076 | void Verifier::visitLoadInst(LoadInst &LI) { |
Chris Lattner | cd709cb | 2002-08-22 22:49:05 +0000 | [diff] [blame] | 1077 | const Type *ElTy = |
| 1078 | cast<PointerType>(LI.getOperand(0)->getType())->getElementType(); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1079 | Assert2(ElTy == LI.getType(), |
Chris Lattner | 9d72c2f | 2003-11-21 17:06:29 +0000 | [diff] [blame] | 1080 | "Load result type does not match pointer operand type!", &LI, ElTy); |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 1081 | visitInstruction(LI); |
| 1082 | } |
| 1083 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1084 | void Verifier::visitStoreInst(StoreInst &SI) { |
Chris Lattner | cd709cb | 2002-08-22 22:49:05 +0000 | [diff] [blame] | 1085 | const Type *ElTy = |
| 1086 | cast<PointerType>(SI.getOperand(1)->getType())->getElementType(); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1087 | Assert2(ElTy == SI.getOperand(0)->getType(), |
Chris Lattner | 9d72c2f | 2003-11-21 17:06:29 +0000 | [diff] [blame] | 1088 | "Stored value type does not match pointer operand type!", &SI, ElTy); |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 1089 | visitInstruction(SI); |
| 1090 | } |
| 1091 | |
Christopher Lamb | 55c6d4f | 2007-12-17 01:00:21 +0000 | [diff] [blame] | 1092 | void Verifier::visitAllocationInst(AllocationInst &AI) { |
Chris Lattner | ffe0da0 | 2008-03-01 09:01:57 +0000 | [diff] [blame] | 1093 | const PointerType *PTy = AI.getType(); |
| 1094 | Assert1(PTy->getAddressSpace() == 0, |
| 1095 | "Allocation instruction pointer not in the generic address space!", |
| 1096 | &AI); |
| 1097 | Assert1(PTy->getElementType()->isSized(), "Cannot allocate unsized type", |
| 1098 | &AI); |
Christopher Lamb | 55c6d4f | 2007-12-17 01:00:21 +0000 | [diff] [blame] | 1099 | visitInstruction(AI); |
| 1100 | } |
| 1101 | |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1102 | void Verifier::visitExtractValueInst(ExtractValueInst &EVI) { |
| 1103 | Assert1(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(), |
| 1104 | EVI.idx_begin(), EVI.idx_end()) == |
| 1105 | EVI.getType(), |
| 1106 | "Invalid ExtractValueInst operands!", &EVI); |
Chris Lattner | 31abd54 | 2008-04-23 04:06:15 +0000 | [diff] [blame] | 1107 | |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1108 | visitInstruction(EVI); |
Devang Patel | 295711f | 2008-02-19 22:15:16 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1111 | void Verifier::visitInsertValueInst(InsertValueInst &IVI) { |
| 1112 | Assert1(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(), |
| 1113 | IVI.idx_begin(), IVI.idx_end()) == |
| 1114 | IVI.getOperand(1)->getType(), |
| 1115 | "Invalid InsertValueInst operands!", &IVI); |
| 1116 | |
| 1117 | visitInstruction(IVI); |
| 1118 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1119 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1120 | /// verifyInstruction - Verify that an instruction is well formed. |
| 1121 | /// |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1122 | void Verifier::visitInstruction(Instruction &I) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1123 | BasicBlock *BB = I.getParent(); |
Chris Lattner | 1f41925 | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 1124 | Assert1(BB, "Instruction not embedded in basic block!", &I); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1125 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1126 | if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential |
| 1127 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
| 1128 | UI != UE; ++UI) |
Chris Lattner | 3705370 | 2004-02-27 17:28:25 +0000 | [diff] [blame] | 1129 | Assert1(*UI != (User*)&I || |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 1130 | !DT->dominates(&BB->getParent()->getEntryBlock(), BB), |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1131 | "Only PHI nodes may reference their own value!", &I); |
| 1132 | } |
Chris Lattner | 7f5c255 | 2008-02-09 01:06:01 +0000 | [diff] [blame] | 1133 | |
| 1134 | // Verify that if this is a terminator that it is at the end of the block. |
| 1135 | if (isa<TerminatorInst>(I)) |
| 1136 | Assert1(BB->getTerminator() == &I, "Terminator not at end of block!", &I); |
| 1137 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1138 | |
| 1139 | // Check that void typed values don't have names |
| 1140 | Assert1(I.getType() != Type::VoidTy || !I.hasName(), |
| 1141 | "Instruction has a name, but provides a void value!", &I); |
| 1142 | |
Chris Lattner | 5f126b7 | 2004-03-29 00:29:36 +0000 | [diff] [blame] | 1143 | // Check that the return value of the instruction is either void or a legal |
| 1144 | // value type. |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 1145 | Assert1(I.getType() == Type::VoidTy || I.getType()->isFirstClassType() |
Devang Patel | 9fea019 | 2008-02-21 22:24:17 +0000 | [diff] [blame] | 1146 | || ((isa<CallInst>(I) || isa<InvokeInst>(I)) |
| 1147 | && isa<StructType>(I.getType())), |
Chris Lattner | 5f126b7 | 2004-03-29 00:29:36 +0000 | [diff] [blame] | 1148 | "Instruction returns a non-scalar type!", &I); |
| 1149 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1150 | // Check that all uses of the instruction, if they are instructions |
| 1151 | // themselves, actually have parent basic blocks. If the use is not an |
| 1152 | // instruction, it is an error! |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1153 | for (User::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1154 | UI != UE; ++UI) { |
| 1155 | Assert1(isa<Instruction>(*UI), "Use of instruction is not an instruction!", |
| 1156 | *UI); |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 1157 | Instruction *Used = cast<Instruction>(*UI); |
| 1158 | Assert2(Used->getParent() != 0, "Instruction referencing instruction not" |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1159 | " embeded in a basic block!", &I, Used); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1162 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
Chris Lattner | 08f7d0c | 2005-02-24 16:58:29 +0000 | [diff] [blame] | 1163 | Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I); |
Chris Lattner | b7e1ef5 | 2006-07-11 20:29:49 +0000 | [diff] [blame] | 1164 | |
| 1165 | // Check to make sure that only first-class-values are operands to |
| 1166 | // instructions. |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 1167 | if (!I.getOperand(i)->getType()->isFirstClassType()) { |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1168 | Assert1(0, "Instruction operands must be first-class values!", &I); |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 1169 | } |
| 1170 | |
Chris Lattner | 9ece94b | 2004-03-14 03:23:54 +0000 | [diff] [blame] | 1171 | if (Function *F = dyn_cast<Function>(I.getOperand(i))) { |
Chris Lattner | b7e1ef5 | 2006-07-11 20:29:49 +0000 | [diff] [blame] | 1172 | // Check to make sure that the "address of" an intrinsic function is never |
| 1173 | // taken. |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 1174 | Assert1(!F->isIntrinsic() || (i == 0 && isa<CallInst>(I)), |
| 1175 | "Cannot take the address of an intrinsic!", &I); |
Chris Lattner | 4ff0452 | 2007-04-20 21:48:08 +0000 | [diff] [blame] | 1176 | Assert1(F->getParent() == Mod, "Referencing function in another module!", |
| 1177 | &I); |
Chris Lattner | 9ece94b | 2004-03-14 03:23:54 +0000 | [diff] [blame] | 1178 | } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) { |
| 1179 | Assert1(OpBB->getParent() == BB->getParent(), |
| 1180 | "Referring to a basic block in another function!", &I); |
| 1181 | } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) { |
| 1182 | Assert1(OpArg->getParent() == BB->getParent(), |
| 1183 | "Referring to an argument in another function!", &I); |
Chris Lattner | 4ff0452 | 2007-04-20 21:48:08 +0000 | [diff] [blame] | 1184 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) { |
| 1185 | Assert1(GV->getParent() == Mod, "Referencing global in another module!", |
| 1186 | &I); |
Chris Lattner | 9ece94b | 2004-03-14 03:23:54 +0000 | [diff] [blame] | 1187 | } else if (Instruction *Op = dyn_cast<Instruction>(I.getOperand(i))) { |
Chris Lattner | ec5089a | 2004-01-14 04:25:59 +0000 | [diff] [blame] | 1188 | BasicBlock *OpBlock = Op->getParent(); |
Chris Lattner | ec5089a | 2004-01-14 04:25:59 +0000 | [diff] [blame] | 1189 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1190 | // Check that a definition dominates all of its uses. |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1191 | if (!isa<PHINode>(I)) { |
Chris Lattner | 0206282 | 2004-01-14 05:42:52 +0000 | [diff] [blame] | 1192 | // Invoke results are only usable in the normal destination, not in the |
| 1193 | // exceptional destination. |
Chris Lattner | 6976177 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1194 | if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) { |
Chris Lattner | 0206282 | 2004-01-14 05:42:52 +0000 | [diff] [blame] | 1195 | OpBlock = II->getNormalDest(); |
Chris Lattner | 6976177 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1196 | |
Chris Lattner | 6fc3c7a | 2006-12-20 21:20:13 +0000 | [diff] [blame] | 1197 | Assert2(OpBlock != II->getUnwindDest(), |
| 1198 | "No uses of invoke possible due to dominance structure!", |
| 1199 | Op, II); |
| 1200 | |
Chris Lattner | 6976177 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1201 | // If the normal successor of an invoke instruction has multiple |
| 1202 | // predecessors, then the normal edge from the invoke is critical, so |
| 1203 | // the invoke value can only be live if the destination block |
| 1204 | // dominates all of it's predecessors (other than the invoke) or if |
| 1205 | // the invoke value is only used by a phi in the successor. |
| 1206 | if (!OpBlock->getSinglePredecessor() && |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 1207 | DT->dominates(&BB->getParent()->getEntryBlock(), BB)) { |
Chris Lattner | 6976177 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1208 | // The first case we allow is if the use is a PHI operand in the |
| 1209 | // normal block, and if that PHI operand corresponds to the invoke's |
| 1210 | // block. |
| 1211 | bool Bad = true; |
| 1212 | if (PHINode *PN = dyn_cast<PHINode>(&I)) |
| 1213 | if (PN->getParent() == OpBlock && |
| 1214 | PN->getIncomingBlock(i/2) == Op->getParent()) |
| 1215 | Bad = false; |
| 1216 | |
| 1217 | // If it is used by something non-phi, then the other case is that |
| 1218 | // 'OpBlock' dominates all of its predecessors other than the |
| 1219 | // invoke. In this case, the invoke value can still be used. |
Chris Lattner | 439c25a | 2006-12-20 19:50:15 +0000 | [diff] [blame] | 1220 | if (Bad) { |
| 1221 | Bad = false; |
Chris Lattner | 6976177 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1222 | for (pred_iterator PI = pred_begin(OpBlock), |
| 1223 | E = pred_end(OpBlock); PI != E; ++PI) { |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 1224 | if (*PI != II->getParent() && !DT->dominates(OpBlock, *PI)) { |
Chris Lattner | 6976177 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1225 | Bad = true; |
| 1226 | break; |
| 1227 | } |
| 1228 | } |
| 1229 | } |
Chris Lattner | 6fc3c7a | 2006-12-20 21:20:13 +0000 | [diff] [blame] | 1230 | Assert2(!Bad, |
| 1231 | "Invoke value defined on critical edge but not dead!", &I, |
| 1232 | Op); |
Chris Lattner | 6976177 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 1233 | } |
| 1234 | } else if (OpBlock == BB) { |
Chris Lattner | 0377e43 | 2004-04-16 05:51:47 +0000 | [diff] [blame] | 1235 | // If they are in the same basic block, make sure that the definition |
| 1236 | // comes before the use. |
Chris Lattner | c9e79d0 | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 1237 | Assert2(InstsInThisBlock.count(Op) || |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 1238 | !DT->dominates(&BB->getParent()->getEntryBlock(), BB), |
Chris Lattner | 0377e43 | 2004-04-16 05:51:47 +0000 | [diff] [blame] | 1239 | "Instruction does not dominate all uses!", Op, &I); |
| 1240 | } |
Chris Lattner | 0206282 | 2004-01-14 05:42:52 +0000 | [diff] [blame] | 1241 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1242 | // Definition must dominate use unless use is unreachable! |
Chris Lattner | eee1f57 | 2008-07-18 05:23:39 +0000 | [diff] [blame] | 1243 | Assert2(InstsInThisBlock.count(Op) || DT->dominates(Op, &I) || |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 1244 | !DT->dominates(&BB->getParent()->getEntryBlock(), BB), |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1245 | "Instruction does not dominate all uses!", Op, &I); |
| 1246 | } else { |
| 1247 | // PHI nodes are more difficult than other nodes because they actually |
| 1248 | // "use" the value in the predecessor basic blocks they correspond to. |
| 1249 | BasicBlock *PredBB = cast<BasicBlock>(I.getOperand(i+1)); |
Devang Patel | c43f32b | 2007-06-11 15:40:48 +0000 | [diff] [blame] | 1250 | Assert2(DT->dominates(OpBlock, PredBB) || |
| 1251 | !DT->dominates(&BB->getParent()->getEntryBlock(), PredBB), |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1252 | "Instruction does not dominate all uses!", Op, &I); |
| 1253 | } |
Chris Lattner | 41eb5cd | 2006-01-26 00:08:45 +0000 | [diff] [blame] | 1254 | } else if (isa<InlineAsm>(I.getOperand(i))) { |
Duncan Sands | b5a79d0 | 2007-12-17 18:08:19 +0000 | [diff] [blame] | 1255 | Assert1(i == 0 && (isa<CallInst>(I) || isa<InvokeInst>(I)), |
Chris Lattner | 41eb5cd | 2006-01-26 00:08:45 +0000 | [diff] [blame] | 1256 | "Cannot take the address of an inline asm!", &I); |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1257 | } |
| 1258 | } |
Chris Lattner | c9e79d0 | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 1259 | InstsInThisBlock.insert(&I); |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /// visitIntrinsicFunction - Allow intrinsics to be verified in different ways. |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1263 | /// |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1264 | void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 1265 | Function *IF = CI.getCalledFunction(); |
Chris Lattner | 4ff0452 | 2007-04-20 21:48:08 +0000 | [diff] [blame] | 1266 | Assert1(IF->isDeclaration(), "Intrinsic functions should never be defined!", |
| 1267 | IF); |
Chris Lattner | 591693f | 2006-03-09 22:06:04 +0000 | [diff] [blame] | 1268 | |
| 1269 | #define GET_INTRINSIC_VERIFIER |
| 1270 | #include "llvm/Intrinsics.gen" |
| 1271 | #undef GET_INTRINSIC_VERIFIER |
Gordon Henriksen | a2f3e13 | 2007-09-17 20:30:04 +0000 | [diff] [blame] | 1272 | |
| 1273 | switch (ID) { |
| 1274 | default: |
| 1275 | break; |
Chris Lattner | ecded9a | 2008-08-23 05:31:10 +0000 | [diff] [blame] | 1276 | case Intrinsic::memcpy_i32: |
| 1277 | case Intrinsic::memcpy_i64: |
| 1278 | case Intrinsic::memmove_i32: |
| 1279 | case Intrinsic::memmove_i64: |
| 1280 | case Intrinsic::memset_i32: |
| 1281 | case Intrinsic::memset_i64: |
| 1282 | Assert1(isa<ConstantInt>(CI.getOperand(4)), |
| 1283 | "alignment argument of memory intrinsics must be a constant int", |
| 1284 | &CI); |
| 1285 | break; |
Bill Wendling | 05604e0 | 2008-08-23 09:46:46 +0000 | [diff] [blame] | 1286 | case Intrinsic::gcroot: |
| 1287 | case Intrinsic::gcwrite: |
Chris Lattner | 2585206 | 2008-08-24 20:46:13 +0000 | [diff] [blame^] | 1288 | case Intrinsic::gcread: |
| 1289 | if (ID == Intrinsic::gcroot) { |
| 1290 | Assert1(isa<AllocaInst>(CI.getOperand(1)->stripPointerCasts()), |
| 1291 | "llvm.gcroot parameter #1 must be an alloca.", &CI); |
| 1292 | Assert1(isa<Constant>(CI.getOperand(2)), |
| 1293 | "llvm.gcroot parameter #2 must be a constant.", &CI); |
| 1294 | } |
Bill Wendling | 05604e0 | 2008-08-23 09:46:46 +0000 | [diff] [blame] | 1295 | |
Chris Lattner | 2585206 | 2008-08-24 20:46:13 +0000 | [diff] [blame^] | 1296 | Assert1(CI.getParent()->getParent()->hasGC(), |
| 1297 | "Enclosing function does not use GC.", &CI); |
| 1298 | break; |
Duncan Sands | f72ff0c | 2007-09-29 16:25:54 +0000 | [diff] [blame] | 1299 | case Intrinsic::init_trampoline: |
Anton Korobeynikov | fc2edad | 2008-05-07 22:54:15 +0000 | [diff] [blame] | 1300 | Assert1(isa<Function>(CI.getOperand(2)->stripPointerCasts()), |
Duncan Sands | f72ff0c | 2007-09-29 16:25:54 +0000 | [diff] [blame] | 1301 | "llvm.init_trampoline parameter #2 must resolve to a function.", |
| 1302 | &CI); |
Gordon Henriksen | 9157c49 | 2007-12-25 02:02:10 +0000 | [diff] [blame] | 1303 | break; |
Gordon Henriksen | a2f3e13 | 2007-09-17 20:30:04 +0000 | [diff] [blame] | 1304 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1307 | /// VerifyIntrinsicPrototype - TableGen emits calls to this function into |
| 1308 | /// Intrinsics.gen. This implements a little state machine that verifies the |
| 1309 | /// prototype of intrinsics. |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1310 | void Verifier::VerifyIntrinsicPrototype(Intrinsic::ID ID, |
| 1311 | Function *F, |
| 1312 | unsigned Count, ...) { |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1313 | va_list VA; |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1314 | va_start(VA, Count); |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1315 | const FunctionType *FTy = F->getFunctionType(); |
| 1316 | |
Reid Spencer | 7c57b88 | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1317 | // For overloaded intrinsics, the Suffix of the function name must match the |
| 1318 | // types of the arguments. This variable keeps track of the expected |
| 1319 | // suffix, to be checked at the end. |
| 1320 | std::string Suffix; |
| 1321 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1322 | if (FTy->getNumParams() + FTy->isVarArg() != Count - 1) { |
| 1323 | CheckFailed("Intrinsic prototype has incorrect number of arguments!", F); |
| 1324 | return; |
| 1325 | } |
| 1326 | |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1327 | // Note that "arg#0" is the return type. |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1328 | for (unsigned ArgNo = 0; ArgNo < Count; ++ArgNo) { |
Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1329 | int VT = va_arg(VA, int); // An MVT::SimpleValueType when non-negative. |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1330 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1331 | if (VT == MVT::isVoid && ArgNo > 0) { |
| 1332 | if (!FTy->isVarArg()) |
| 1333 | CheckFailed("Intrinsic prototype has no '...'!", F); |
Jim Laskey | 5aed30d | 2007-02-06 18:02:54 +0000 | [diff] [blame] | 1334 | break; |
| 1335 | } |
| 1336 | |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1337 | const Type *Ty; |
Reid Spencer | 7c57b88 | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1338 | if (ArgNo == 0) |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1339 | Ty = FTy->getReturnType(); |
| 1340 | else |
| 1341 | Ty = FTy->getParamType(ArgNo-1); |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1342 | |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1343 | unsigned NumElts = 0; |
| 1344 | const Type *EltTy = Ty; |
| 1345 | if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) { |
| 1346 | EltTy = VTy->getElementType(); |
| 1347 | NumElts = VTy->getNumElements(); |
| 1348 | } |
Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1349 | |
| 1350 | if (VT < 0) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1351 | int Match = ~VT; |
| 1352 | if (Match == 0) { |
| 1353 | if (Ty != FTy->getReturnType()) { |
| 1354 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " does not " |
| 1355 | "match return type.", F); |
| 1356 | break; |
| 1357 | } |
| 1358 | } else { |
| 1359 | if (Ty != FTy->getParamType(Match-1)) { |
| 1360 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " does not " |
| 1361 | "match parameter %" + utostr(Match-1) + ".", F); |
| 1362 | break; |
| 1363 | } |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1364 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1365 | } else if (VT == MVT::iAny) { |
Dan Gohman | 88ac781 | 2007-08-16 22:06:45 +0000 | [diff] [blame] | 1366 | if (!EltTy->isInteger()) { |
| 1367 | if (ArgNo == 0) |
| 1368 | CheckFailed("Intrinsic result type is not " |
| 1369 | "an integer type.", F); |
| 1370 | else |
| 1371 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is not " |
| 1372 | "an integer type.", F); |
| 1373 | break; |
| 1374 | } |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1375 | unsigned GotBits = cast<IntegerType>(EltTy)->getBitWidth(); |
| 1376 | Suffix += "."; |
| 1377 | if (EltTy != Ty) |
| 1378 | Suffix += "v" + utostr(NumElts); |
| 1379 | Suffix += "i" + utostr(GotBits);; |
Reid Spencer | 7c57b88 | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1380 | // Check some constraints on various intrinsics. |
| 1381 | switch (ID) { |
| 1382 | default: break; // Not everything needs to be checked. |
| 1383 | case Intrinsic::bswap: |
| 1384 | if (GotBits < 16 || GotBits % 16 != 0) |
| 1385 | CheckFailed("Intrinsic requires even byte width argument", F); |
Reid Spencer | 7c57b88 | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1386 | break; |
| 1387 | } |
Dan Gohman | febf946 | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 1388 | } else if (VT == MVT::fAny) { |
Dan Gohman | febf946 | 2007-08-16 21:57:19 +0000 | [diff] [blame] | 1389 | if (!EltTy->isFloatingPoint()) { |
| 1390 | if (ArgNo == 0) |
| 1391 | CheckFailed("Intrinsic result type is not " |
| 1392 | "a floating-point type.", F); |
| 1393 | else |
| 1394 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is not " |
| 1395 | "a floating-point type.", F); |
| 1396 | break; |
| 1397 | } |
Dan Gohman | 88ac781 | 2007-08-16 22:06:45 +0000 | [diff] [blame] | 1398 | Suffix += "."; |
| 1399 | if (EltTy != Ty) |
| 1400 | Suffix += "v" + utostr(NumElts); |
Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1401 | Suffix += MVT::getMVT(EltTy).getMVTString(); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1402 | } else if (VT == MVT::iPTR) { |
| 1403 | if (!isa<PointerType>(Ty)) { |
Dan Gohman | 88ac781 | 2007-08-16 22:06:45 +0000 | [diff] [blame] | 1404 | if (ArgNo == 0) |
| 1405 | CheckFailed("Intrinsic result type is not a " |
| 1406 | "pointer and a pointer is required.", F); |
| 1407 | else |
| 1408 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is not a " |
| 1409 | "pointer and a pointer is required.", F); |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 1410 | } |
| 1411 | } else if (VT == MVT::iPTRAny) { |
| 1412 | // Outside of TableGen, we don't distinguish iPTRAny (to any address |
| 1413 | // space) and iPTR. In the verifier, we can not distinguish which case |
| 1414 | // we have so allow either case to be legal. |
| 1415 | if (const PointerType* PTyp = dyn_cast<PointerType>(Ty)) { |
| 1416 | Suffix += ".p" + utostr(PTyp->getAddressSpace()) + |
| 1417 | MVT::getMVT(PTyp->getElementType()).getMVTString(); |
| 1418 | } else { |
| 1419 | if (ArgNo == 0) |
| 1420 | CheckFailed("Intrinsic result type is not a " |
| 1421 | "pointer and a pointer is required.", F); |
| 1422 | else |
| 1423 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is not a " |
| 1424 | "pointer and a pointer is required.", F); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1425 | break; |
| 1426 | } |
Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1427 | } else if (MVT((MVT::SimpleValueType)VT).isVector()) { |
| 1428 | MVT VVT = MVT((MVT::SimpleValueType)VT); |
Dan Gohman | 06c60b6 | 2007-07-16 14:29:03 +0000 | [diff] [blame] | 1429 | // If this is a vector argument, verify the number and type of elements. |
Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1430 | if (VVT.getVectorElementType() != MVT::getMVT(EltTy)) { |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 1431 | CheckFailed("Intrinsic prototype has incorrect vector element type!", |
| 1432 | F); |
| 1433 | break; |
| 1434 | } |
Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1435 | if (VVT.getVectorNumElements() != NumElts) { |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1436 | CheckFailed("Intrinsic prototype has incorrect number of " |
| 1437 | "vector elements!",F); |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1438 | break; |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1439 | } |
Duncan Sands | 13237ac | 2008-06-06 12:08:01 +0000 | [diff] [blame] | 1440 | } else if (MVT((MVT::SimpleValueType)VT).getTypeForMVT() != EltTy) { |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 1441 | if (ArgNo == 0) |
| 1442 | CheckFailed("Intrinsic prototype has incorrect result type!", F); |
| 1443 | else |
| 1444 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is wrong!",F); |
| 1445 | break; |
| 1446 | } else if (EltTy != Ty) { |
| 1447 | if (ArgNo == 0) |
| 1448 | CheckFailed("Intrinsic result type is vector " |
| 1449 | "and a scalar is required.", F); |
| 1450 | else |
| 1451 | CheckFailed("Intrinsic parameter #" + utostr(ArgNo-1) + " is vector " |
| 1452 | "and a scalar is required.", F); |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | va_end(VA); |
Reid Spencer | 7c57b88 | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1457 | |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 1458 | // For intrinsics without pointer arguments, if we computed a Suffix then the |
| 1459 | // intrinsic is overloaded and we need to make sure that the name of the |
| 1460 | // function is correct. We add the suffix to the name of the intrinsic and |
| 1461 | // compare against the given function name. If they are not the same, the |
| 1462 | // function name is invalid. This ensures that overloading of intrinsics |
| 1463 | // uses a sane and consistent naming convention. Note that intrinsics with |
| 1464 | // pointer argument may or may not be overloaded so we will check assuming it |
| 1465 | // has a suffix and not. |
Reid Spencer | 7c57b88 | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1466 | if (!Suffix.empty()) { |
| 1467 | std::string Name(Intrinsic::getName(ID)); |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 1468 | if (Name + Suffix != F->getName()) { |
Reid Spencer | 7c57b88 | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1469 | CheckFailed("Overloaded intrinsic has incorrect suffix: '" + |
| 1470 | F->getName().substr(Name.length()) + "'. It should be '" + |
| 1471 | Suffix + "'", F); |
Mon P Wang | 2c839d4 | 2008-07-30 04:36:53 +0000 | [diff] [blame] | 1472 | } |
Reid Spencer | 7c57b88 | 2007-04-01 07:22:57 +0000 | [diff] [blame] | 1473 | } |
Duncan Sands | a8ff6ca | 2008-04-07 13:39:11 +0000 | [diff] [blame] | 1474 | |
| 1475 | // Check parameter attributes. |
| 1476 | Assert1(F->getParamAttrs() == Intrinsic::getParamAttrs(ID), |
| 1477 | "Intrinsic has wrong parameter attributes!", F); |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1480 | |
| 1481 | //===----------------------------------------------------------------------===// |
| 1482 | // Implement the public interfaces to this file... |
| 1483 | //===----------------------------------------------------------------------===// |
| 1484 | |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1485 | FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) { |
| 1486 | return new Verifier(action); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1487 | } |
| 1488 | |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1489 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1490 | // verifyFunction - Create |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1491 | bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) { |
Chris Lattner | b870ca7 | 2004-03-14 03:16:15 +0000 | [diff] [blame] | 1492 | Function &F = const_cast<Function&>(f); |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1493 | assert(!F.isDeclaration() && "Cannot verify external functions"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1494 | |
Chris Lattner | b870ca7 | 2004-03-14 03:16:15 +0000 | [diff] [blame] | 1495 | FunctionPassManager FPM(new ExistingModuleProvider(F.getParent())); |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1496 | Verifier *V = new Verifier(action); |
Chris Lattner | b870ca7 | 2004-03-14 03:16:15 +0000 | [diff] [blame] | 1497 | FPM.add(V); |
| 1498 | FPM.run(F); |
| 1499 | return V->Broken; |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1502 | /// verifyModule - Check a module for errors, printing messages on stderr. |
| 1503 | /// Return true if the module is corrupt. |
| 1504 | /// |
Chris Lattner | 5734e8d | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 1505 | bool llvm::verifyModule(const Module &M, VerifierFailureAction action, |
| 1506 | std::string *ErrorInfo) { |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1507 | PassManager PM; |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1508 | Verifier *V = new Verifier(action); |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1509 | PM.add(V); |
Dan Gohman | 6116385 | 2008-06-24 17:47:37 +0000 | [diff] [blame] | 1510 | PM.run(const_cast<Module&>(M)); |
Chris Lattner | 5734e8d | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 1511 | |
| 1512 | if (ErrorInfo && V->Broken) |
| 1513 | *ErrorInfo = V->msgs.str(); |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1514 | return V->Broken; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1515 | } |
Reid Spencer | 47cf71a | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 1516 | |
| 1517 | // vim: sw=2 |