Nick Lewycky | d489edf | 2012-02-25 07:20:06 +0000 | [diff] [blame] | 1 | //===-- Verifier.cpp - Implement the Module Verifier -----------------------==// |
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 |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 38 | // * A landing pad is defined by a landingpad instruction, and can be jumped to |
| 39 | // only by the unwind edge of an invoke instruction. |
| 40 | // * A landingpad instruction must be the first non-PHI instruction in the |
| 41 | // block. |
| 42 | // * All landingpad instructions must use the same personality function with |
| 43 | // the same function. |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 44 | // * All other things that are tested by asserts spread about the code... |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 45 | // |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | |
| 48 | #include "llvm/Analysis/Verifier.h" |
Chris Lattner | 2ad5aa8 | 2005-05-08 22:27:09 +0000 | [diff] [blame] | 49 | #include "llvm/CallingConv.h" |
Chris Lattner | dc63211 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 50 | #include "llvm/Constants.h" |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 51 | #include "llvm/DerivedTypes.h" |
Chris Lattner | 41eb5cd | 2006-01-26 00:08:45 +0000 | [diff] [blame] | 52 | #include "llvm/InlineAsm.h" |
Gordon Henriksen | be6bd16 | 2007-09-18 10:14:30 +0000 | [diff] [blame] | 53 | #include "llvm/IntrinsicInst.h" |
Rafael Espindola | ef9f550 | 2012-03-24 00:14:51 +0000 | [diff] [blame] | 54 | #include "llvm/LLVMContext.h" |
Devang Patel | a4f43fb | 2009-07-28 21:49:47 +0000 | [diff] [blame] | 55 | #include "llvm/Metadata.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 56 | #include "llvm/Module.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 57 | #include "llvm/Pass.h" |
Chris Lattner | dc63211 | 2004-02-14 02:47:17 +0000 | [diff] [blame] | 58 | #include "llvm/PassManager.h" |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 59 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 60 | #include "llvm/Assembly/Writer.h" |
Chandler Carruth | 7132e00 | 2007-08-04 01:51:18 +0000 | [diff] [blame] | 61 | #include "llvm/CodeGen/ValueTypes.h" |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 62 | #include "llvm/Support/CallSite.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 63 | #include "llvm/Support/CFG.h" |
David Greene | 7749903 | 2010-01-05 01:29:14 +0000 | [diff] [blame] | 64 | #include "llvm/Support/Debug.h" |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 65 | #include "llvm/Support/InstVisitor.h" |
Nick Lewycky | c9f9367 | 2009-09-14 00:36:52 +0000 | [diff] [blame] | 66 | #include "llvm/ADT/SetVector.h" |
Chris Lattner | bc97cc2 | 2007-02-10 08:19:44 +0000 | [diff] [blame] | 67 | #include "llvm/ADT/SmallPtrSet.h" |
Chris Lattner | 84d82c7 | 2007-02-10 08:30:29 +0000 | [diff] [blame] | 68 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 69 | #include "llvm/ADT/StringExtras.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 70 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | e3c5f3e | 2012-05-31 16:04:26 +0000 | [diff] [blame] | 71 | #include "llvm/Support/ConstantRange.h" |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 72 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 2402526 | 2009-02-28 21:05:51 +0000 | [diff] [blame] | 73 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 74 | #include <algorithm> |
Jeff Cohen | e453552 | 2006-03-31 07:22:05 +0000 | [diff] [blame] | 75 | #include <cstdarg> |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 76 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 78 | namespace { // Anonymous namespace for class |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 79 | struct PreVerifier : public FunctionPass { |
Owen Anderson | fe41d79 | 2007-11-01 03:54:23 +0000 | [diff] [blame] | 80 | static char ID; // Pass ID, replacement for typeid |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 81 | |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 82 | PreVerifier() : FunctionPass(ID) { |
| 83 | initializePreVerifierPass(*PassRegistry::getPassRegistry()); |
| 84 | } |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 856684d | 2008-12-01 03:58:38 +0000 | [diff] [blame] | 86 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 87 | AU.setPreservesAll(); |
| 88 | } |
| 89 | |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 90 | // Check that the prerequisites for successful DominatorTree construction |
| 91 | // are satisfied. |
Owen Anderson | fe41d79 | 2007-11-01 03:54:23 +0000 | [diff] [blame] | 92 | bool runOnFunction(Function &F) { |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 93 | bool Broken = false; |
| 94 | |
| 95 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 96 | if (I->empty() || !I->back().isTerminator()) { |
Chris Lattner | 2ed3955 | 2010-06-12 15:50:24 +0000 | [diff] [blame] | 97 | dbgs() << "Basic Block in function '" << F.getName() |
| 98 | << "' does not have terminator!\n"; |
David Greene | 7749903 | 2010-01-05 01:29:14 +0000 | [diff] [blame] | 99 | WriteAsOperand(dbgs(), I, true); |
| 100 | dbgs() << "\n"; |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 101 | Broken = true; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | if (Broken) |
Chris Lattner | 2104b8d | 2010-04-07 22:58:41 +0000 | [diff] [blame] | 106 | report_fatal_error("Broken module, no Basic Block terminator!"); |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 107 | |
| 108 | return false; |
Owen Anderson | fe41d79 | 2007-11-01 03:54:23 +0000 | [diff] [blame] | 109 | } |
Owen Anderson | 9396c39 | 2007-10-31 21:04:18 +0000 | [diff] [blame] | 110 | }; |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 111 | } |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 112 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 113 | char PreVerifier::ID = 0; |
Owen Anderson | d31d82d | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 114 | INITIALIZE_PASS(PreVerifier, "preverify", "Preliminary module verification", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 115 | false, false) |
Benjamin Kramer | 9192e7a | 2010-10-22 17:35:07 +0000 | [diff] [blame] | 116 | static char &PreVerifyID = PreVerifier::ID; |
Duncan Sands | e0e774b | 2007-11-01 10:50:26 +0000 | [diff] [blame] | 117 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 118 | namespace { |
Nick Lewycky | 64f18ec | 2009-09-13 23:45:39 +0000 | [diff] [blame] | 119 | struct Verifier : public FunctionPass, public InstVisitor<Verifier> { |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 120 | static char ID; // Pass ID, replacement for typeid |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 121 | bool Broken; // Is this module found to be broken? |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 122 | VerifierFailureAction action; |
Reid Spencer | 47cf71a | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 123 | // What to do if verification fails. |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 124 | Module *Mod; // Module we are verifying right now |
Nick Lewycky | 62f864d | 2010-02-15 21:52:04 +0000 | [diff] [blame] | 125 | LLVMContext *Context; // Context within which we are verifying |
| 126 | DominatorTree *DT; // Dominator Tree, caution can be null! |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 128 | std::string Messages; |
| 129 | raw_string_ostream MessagesStr; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 130 | |
Chris Lattner | c9e79d0 | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 131 | /// InstInThisBlock - when verifying a basic block, keep track of all of the |
| 132 | /// instructions we have seen so far. This allows us to do efficient |
| 133 | /// dominance checks for the case when an instruction has an operand that is |
| 134 | /// an instruction in the same block. |
Chris Lattner | bc97cc2 | 2007-02-10 08:19:44 +0000 | [diff] [blame] | 135 | SmallPtrSet<Instruction*, 16> InstsInThisBlock; |
Chris Lattner | c9e79d0 | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 136 | |
Duncan Sands | 76d6217 | 2010-04-29 16:10:30 +0000 | [diff] [blame] | 137 | /// MDNodes - keep track of the metadata nodes that have been checked |
| 138 | /// already. |
| 139 | SmallPtrSet<MDNode *, 32> MDNodes; |
| 140 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 141 | /// PersonalityFn - The personality function referenced by the |
| 142 | /// LandingPadInsts. All LandingPadInsts within the same function must use |
| 143 | /// the same personality function. |
| 144 | const Value *PersonalityFn; |
| 145 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 146 | Verifier() |
Rafael Espindola | 8e5b40e | 2012-03-24 20:02:25 +0000 | [diff] [blame] | 147 | : FunctionPass(ID), Broken(false), |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 148 | action(AbortProcessAction), Mod(0), Context(0), DT(0), |
| 149 | MessagesStr(Messages), PersonalityFn(0) { |
| 150 | initializeVerifierPass(*PassRegistry::getPassRegistry()); |
| 151 | } |
Dan Gohman | c60c67f | 2008-03-25 22:06:05 +0000 | [diff] [blame] | 152 | explicit Verifier(VerifierFailureAction ctn) |
Rafael Espindola | 8e5b40e | 2012-03-24 20:02:25 +0000 | [diff] [blame] | 153 | : FunctionPass(ID), Broken(false), action(ctn), Mod(0), |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 154 | Context(0), DT(0), MessagesStr(Messages), PersonalityFn(0) { |
| 155 | initializeVerifierPass(*PassRegistry::getPassRegistry()); |
| 156 | } |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 157 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 158 | bool doInitialization(Module &M) { |
Brian Gaeke | 9f47927 | 2003-11-16 23:07:42 +0000 | [diff] [blame] | 159 | Mod = &M; |
Nick Lewycky | 62f864d | 2010-02-15 21:52:04 +0000 | [diff] [blame] | 160 | Context = &M.getContext(); |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 161 | |
Rafael Espindola | 8e5b40e | 2012-03-24 20:02:25 +0000 | [diff] [blame] | 162 | // We must abort before returning back to the pass manager, or else the |
| 163 | // pass manager may try to run other passes on the broken module. |
| 164 | return abortIfBroken(); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 167 | bool runOnFunction(Function &F) { |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 168 | // Get dominator information if we are being run by PassManager |
Rafael Espindola | 8e5b40e | 2012-03-24 20:02:25 +0000 | [diff] [blame] | 169 | DT = &getAnalysis<DominatorTree>(); |
Chris Lattner | af332ee | 2007-04-20 23:59:29 +0000 | [diff] [blame] | 170 | |
| 171 | Mod = F.getParent(); |
Nick Lewycky | 62f864d | 2010-02-15 21:52:04 +0000 | [diff] [blame] | 172 | if (!Context) Context = &F.getContext(); |
Chris Lattner | af332ee | 2007-04-20 23:59:29 +0000 | [diff] [blame] | 173 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 174 | visit(F); |
Chris Lattner | c9e79d0 | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 175 | InstsInThisBlock.clear(); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 176 | PersonalityFn = 0; |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 177 | |
Rafael Espindola | 8e5b40e | 2012-03-24 20:02:25 +0000 | [diff] [blame] | 178 | // We must abort before returning back to the pass manager, or else the |
| 179 | // pass manager may try to run other passes on the broken module. |
| 180 | return abortIfBroken(); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 183 | bool doFinalization(Module &M) { |
Chris Lattner | 9713b84 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 184 | // Scan through, checking all of the external function's linkage now... |
Chris Lattner | f75832b | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 185 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
Chris Lattner | 3ac483b | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 186 | visitGlobalValue(*I); |
Chris Lattner | 9713b84 | 2002-04-28 16:04:26 +0000 | [diff] [blame] | 187 | |
Chris Lattner | f75832b | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 188 | // Check to make sure function prototypes are okay. |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 189 | if (I->isDeclaration()) visitFunction(*I); |
Chris Lattner | f75832b | 2004-06-03 06:38:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Reid Spencer | b4f9a6f | 2006-01-16 21:12:35 +0000 | [diff] [blame] | 192 | for (Module::global_iterator I = M.global_begin(), E = M.global_end(); |
| 193 | I != E; ++I) |
Chris Lattner | e340065 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 194 | visitGlobalVariable(*I); |
Chris Lattner | 78bc0fa | 2002-10-06 22:47:32 +0000 | [diff] [blame] | 195 | |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 196 | for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); |
| 197 | I != E; ++I) |
| 198 | visitGlobalAlias(*I); |
| 199 | |
Duncan Sands | 76d6217 | 2010-04-29 16:10:30 +0000 | [diff] [blame] | 200 | for (Module::named_metadata_iterator I = M.named_metadata_begin(), |
| 201 | E = M.named_metadata_end(); I != E; ++I) |
| 202 | visitNamedMDNode(*I); |
| 203 | |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 204 | // If the module is broken, abort at this time. |
Reid Spencer | 421475c | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 205 | return abortIfBroken(); |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 208 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 209 | AU.setPreservesAll(); |
Owen Anderson | 9396c39 | 2007-10-31 21:04:18 +0000 | [diff] [blame] | 210 | AU.addRequiredID(PreVerifyID); |
Rafael Espindola | 8e5b40e | 2012-03-24 20:02:25 +0000 | [diff] [blame] | 211 | AU.addRequired<DominatorTree>(); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 212 | } |
| 213 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 214 | /// abortIfBroken - If the module is broken and we are supposed to abort on |
| 215 | /// this condition, do so. |
| 216 | /// |
Reid Spencer | 421475c | 2006-07-26 16:18:00 +0000 | [diff] [blame] | 217 | bool abortIfBroken() { |
Chris Lattner | 81e2309 | 2008-08-27 17:36:58 +0000 | [diff] [blame] | 218 | if (!Broken) return false; |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 219 | MessagesStr << "Broken module found, "; |
Chris Lattner | 81e2309 | 2008-08-27 17:36:58 +0000 | [diff] [blame] | 220 | switch (action) { |
Chris Lattner | 81e2309 | 2008-08-27 17:36:58 +0000 | [diff] [blame] | 221 | case AbortProcessAction: |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 222 | MessagesStr << "compilation aborted!\n"; |
David Greene | 7749903 | 2010-01-05 01:29:14 +0000 | [diff] [blame] | 223 | dbgs() << MessagesStr.str(); |
Torok Edwin | fb8d6d5 | 2009-07-08 20:53:28 +0000 | [diff] [blame] | 224 | // Client should choose different reaction if abort is not desired |
| 225 | abort(); |
Chris Lattner | 81e2309 | 2008-08-27 17:36:58 +0000 | [diff] [blame] | 226 | case PrintMessageAction: |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 227 | MessagesStr << "verification continues.\n"; |
David Greene | 7749903 | 2010-01-05 01:29:14 +0000 | [diff] [blame] | 228 | dbgs() << MessagesStr.str(); |
Chris Lattner | 81e2309 | 2008-08-27 17:36:58 +0000 | [diff] [blame] | 229 | return false; |
| 230 | case ReturnStatusAction: |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 231 | MessagesStr << "compilation terminated.\n"; |
Nick Lewycky | edbb0e2 | 2009-03-15 06:40:32 +0000 | [diff] [blame] | 232 | return true; |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 233 | } |
Chandler Carruth | f3e8502 | 2012-01-10 18:08:01 +0000 | [diff] [blame] | 234 | llvm_unreachable("Invalid action"); |
Chris Lattner | a450397 | 2002-09-19 16:12:19 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Chris Lattner | 3ac483b | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 238 | // Verification methods... |
Chris Lattner | 3ac483b | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 239 | void visitGlobalValue(GlobalValue &GV); |
Chris Lattner | e340065 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 240 | void visitGlobalVariable(GlobalVariable &GV); |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 241 | void visitGlobalAlias(GlobalAlias &GA); |
Duncan Sands | 76d6217 | 2010-04-29 16:10:30 +0000 | [diff] [blame] | 242 | void visitNamedMDNode(NamedMDNode &NMD); |
| 243 | void visitMDNode(MDNode &MD, Function *F); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 244 | void visitFunction(Function &F); |
| 245 | void visitBasicBlock(BasicBlock &BB); |
Chris Lattner | e5a22c0 | 2008-08-28 04:02:44 +0000 | [diff] [blame] | 246 | using InstVisitor<Verifier>::visit; |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 247 | |
Chris Lattner | e5a22c0 | 2008-08-28 04:02:44 +0000 | [diff] [blame] | 248 | void visit(Instruction &I); |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 249 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 250 | void visitTruncInst(TruncInst &I); |
| 251 | void visitZExtInst(ZExtInst &I); |
| 252 | void visitSExtInst(SExtInst &I); |
| 253 | void visitFPTruncInst(FPTruncInst &I); |
| 254 | void visitFPExtInst(FPExtInst &I); |
| 255 | void visitFPToUIInst(FPToUIInst &I); |
| 256 | void visitFPToSIInst(FPToSIInst &I); |
| 257 | void visitUIToFPInst(UIToFPInst &I); |
| 258 | void visitSIToFPInst(SIToFPInst &I); |
| 259 | void visitIntToPtrInst(IntToPtrInst &I); |
| 260 | void visitPtrToIntInst(PtrToIntInst &I); |
| 261 | void visitBitCastInst(BitCastInst &I); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 262 | void visitPHINode(PHINode &PN); |
| 263 | void visitBinaryOperator(BinaryOperator &B); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 264 | void visitICmpInst(ICmpInst &IC); |
| 265 | void visitFCmpInst(FCmpInst &FC); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 266 | void visitExtractElementInst(ExtractElementInst &EI); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 267 | void visitInsertElementInst(InsertElementInst &EI); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 268 | void visitShuffleVectorInst(ShuffleVectorInst &EI); |
Chris Lattner | 5b33748 | 2003-10-18 05:57:43 +0000 | [diff] [blame] | 269 | void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); } |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 270 | void visitCallInst(CallInst &CI); |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 271 | void visitInvokeInst(InvokeInst &II); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 272 | void visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 273 | void visitLoadInst(LoadInst &LI); |
| 274 | void visitStoreInst(StoreInst &SI); |
Rafael Espindola | 654320a | 2012-02-26 02:23:37 +0000 | [diff] [blame] | 275 | void verifyDominatesUse(Instruction &I, unsigned i); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 276 | void visitInstruction(Instruction &I); |
| 277 | void visitTerminatorInst(TerminatorInst &I); |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 278 | void visitBranchInst(BranchInst &BI); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 279 | void visitReturnInst(ReturnInst &RI); |
Chris Lattner | ab5aa14 | 2004-05-21 16:47:21 +0000 | [diff] [blame] | 280 | void visitSwitchInst(SwitchInst &SI); |
Dan Gohman | d0a1e3d | 2010-08-02 23:08:33 +0000 | [diff] [blame] | 281 | void visitIndirectBrInst(IndirectBrInst &BI); |
Chris Lattner | 75648e7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 282 | void visitSelectInst(SelectInst &SI); |
Chris Lattner | 903a25d | 2002-11-21 16:54:22 +0000 | [diff] [blame] | 283 | void visitUserOp1(Instruction &I); |
| 284 | void visitUserOp2(Instruction &I) { visitUserOp1(I); } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 285 | void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 286 | void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI); |
| 287 | void visitAtomicRMWInst(AtomicRMWInst &RMWI); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 288 | void visitFenceInst(FenceInst &FI); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 289 | void visitAllocaInst(AllocaInst &AI); |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 290 | void visitExtractValueInst(ExtractValueInst &EVI); |
| 291 | void visitInsertValueInst(InsertValueInst &IVI); |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 292 | void visitLandingPadInst(LandingPadInst &LPI); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 293 | |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 294 | void VerifyCallSite(CallSite CS); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 295 | bool PerformTypeCheck(Intrinsic::ID ID, Function *F, Type *Ty, |
Bill Wendling | 9a04b9d | 2008-11-13 07:11:27 +0000 | [diff] [blame] | 296 | int VT, unsigned ArgNo, std::string &Suffix); |
Chris Lattner | 144b619 | 2012-05-27 19:37:05 +0000 | [diff] [blame] | 297 | bool VerifyIntrinsicType(Type *Ty, |
| 298 | ArrayRef<Intrinsic::IITDescriptor> &Infos, |
| 299 | SmallVectorImpl<Type*> &ArgTys); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 300 | void VerifyParameterAttrs(Attributes Attrs, Type *Ty, |
Duncan Sands | c3a7992 | 2009-06-11 08:11:03 +0000 | [diff] [blame] | 301 | bool isReturnValue, const Value *V); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 302 | void VerifyFunctionAttrs(FunctionType *FT, const AttrListPtr &Attrs, |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 303 | const Value *V); |
Chris Lattner | 7e5e456 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 304 | |
| 305 | void WriteValue(const Value *V) { |
| 306 | if (!V) return; |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 307 | if (isa<Instruction>(V)) { |
Nick Lewycky | e63c2cb | 2009-10-17 19:43:45 +0000 | [diff] [blame] | 308 | MessagesStr << *V << '\n'; |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 309 | } else { |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 310 | WriteAsOperand(MessagesStr, V, true, Mod); |
Nick Lewycky | e63c2cb | 2009-10-17 19:43:45 +0000 | [diff] [blame] | 311 | MessagesStr << '\n'; |
Chris Lattner | 7e5e456 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 315 | void WriteType(Type *T) { |
Chris Lattner | 2402526 | 2009-02-28 21:05:51 +0000 | [diff] [blame] | 316 | if (!T) return; |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 317 | MessagesStr << ' ' << *T; |
Reid Spencer | 47cf71a | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Chris Lattner | 7e5e456 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 320 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 321 | // CheckFailed - A check failed, so print out the condition and the message |
| 322 | // that failed. This provides a nice place to put a breakpoint if you want |
| 323 | // to see why something is not correct. |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 324 | void CheckFailed(const Twine &Message, |
Chris Lattner | 7e5e456 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 325 | const Value *V1 = 0, const Value *V2 = 0, |
| 326 | const Value *V3 = 0, const Value *V4 = 0) { |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 327 | MessagesStr << Message.str() << "\n"; |
Chris Lattner | 7e5e456 | 2003-11-21 17:35:51 +0000 | [diff] [blame] | 328 | WriteValue(V1); |
| 329 | WriteValue(V2); |
| 330 | WriteValue(V3); |
| 331 | WriteValue(V4); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 332 | Broken = true; |
| 333 | } |
Reid Spencer | 47cf71a | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 334 | |
Nick Lewycky | 984161a | 2009-09-08 02:02:39 +0000 | [diff] [blame] | 335 | void CheckFailed(const Twine &Message, const Value *V1, |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 336 | Type *T2, const Value *V3 = 0) { |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 337 | MessagesStr << Message.str() << "\n"; |
Reid Spencer | 47cf71a | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 338 | WriteValue(V1); |
| 339 | WriteType(T2); |
| 340 | WriteValue(V3); |
Reid Spencer | 4148139 | 2004-05-27 21:58:13 +0000 | [diff] [blame] | 341 | Broken = true; |
Reid Spencer | 47cf71a | 2004-05-25 08:53:29 +0000 | [diff] [blame] | 342 | } |
Nick Lewycky | b2b0467 | 2009-09-08 01:23:52 +0000 | [diff] [blame] | 343 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 344 | void CheckFailed(const Twine &Message, Type *T1, |
| 345 | Type *T2 = 0, Type *T3 = 0) { |
Nick Lewycky | b2b0467 | 2009-09-08 01:23:52 +0000 | [diff] [blame] | 346 | MessagesStr << Message.str() << "\n"; |
| 347 | WriteType(T1); |
| 348 | WriteType(T2); |
| 349 | WriteType(T3); |
| 350 | Broken = true; |
| 351 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 352 | }; |
Chris Lattner | 189d19f | 2003-11-21 20:23:48 +0000 | [diff] [blame] | 353 | } // End anonymous namespace |
| 354 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 355 | char Verifier::ID = 0; |
Owen Anderson | 8ac477f | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 356 | INITIALIZE_PASS_BEGIN(Verifier, "verify", "Module Verifier", false, false) |
| 357 | INITIALIZE_PASS_DEPENDENCY(PreVerifier) |
| 358 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 359 | INITIALIZE_PASS_END(Verifier, "verify", "Module Verifier", false, false) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 360 | |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 361 | // Assert - We know that cond should be true, if not print an error message. |
| 362 | #define Assert(C, M) \ |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 363 | do { if (!(C)) { CheckFailed(M); return; } } while (0) |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 364 | #define Assert1(C, M, V1) \ |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 365 | do { if (!(C)) { CheckFailed(M, V1); return; } } while (0) |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 366 | #define Assert2(C, M, V1, V2) \ |
Chris Lattner | 412d277 | 2002-04-28 16:06:24 +0000 | [diff] [blame] | 367 | do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0) |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 368 | #define Assert3(C, M, V1, V2, V3) \ |
| 369 | do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0) |
| 370 | #define Assert4(C, M, V1, V2, V3, V4) \ |
| 371 | do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0) |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 372 | |
Chris Lattner | e5a22c0 | 2008-08-28 04:02:44 +0000 | [diff] [blame] | 373 | void Verifier::visit(Instruction &I) { |
| 374 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) |
| 375 | Assert1(I.getOperand(i) != 0, "Operand is null", &I); |
| 376 | InstVisitor<Verifier>::visit(I); |
| 377 | } |
| 378 | |
| 379 | |
Chris Lattner | 3ac483b | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 380 | void Verifier::visitGlobalValue(GlobalValue &GV) { |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 381 | Assert1(!GV.isDeclaration() || |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 382 | GV.isMaterializable() || |
Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 383 | GV.hasExternalLinkage() || |
| 384 | GV.hasDLLImportLinkage() || |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 385 | GV.hasExternalWeakLinkage() || |
| 386 | (isa<GlobalAlias>(GV) && |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 387 | (GV.hasLocalLinkage() || GV.hasWeakLinkage())), |
Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 388 | "Global is external, but doesn't have external or dllimport or weak linkage!", |
| 389 | &GV); |
| 390 | |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 391 | Assert1(!GV.hasDLLImportLinkage() || GV.isDeclaration(), |
Anton Korobeynikov | d61d39e | 2006-09-14 18:23:27 +0000 | [diff] [blame] | 392 | "Global is marked as dllimport, but not external", &GV); |
Nick Lewycky | b2b0467 | 2009-09-08 01:23:52 +0000 | [diff] [blame] | 393 | |
Chris Lattner | 3ac483b | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 394 | Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV), |
| 395 | "Only global variables can have appending linkage!", &GV); |
| 396 | |
| 397 | if (GV.hasAppendingLinkage()) { |
Nick Lewycky | b2b0467 | 2009-09-08 01:23:52 +0000 | [diff] [blame] | 398 | GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 399 | Assert1(GVar && GVar->getType()->getElementType()->isArrayTy(), |
Nick Lewycky | b2b0467 | 2009-09-08 01:23:52 +0000 | [diff] [blame] | 400 | "Only global arrays can have appending linkage!", GVar); |
Chris Lattner | 3ac483b | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 401 | } |
Bill Wendling | 578ee40 | 2010-08-20 22:05:50 +0000 | [diff] [blame] | 402 | |
Bill Wendling | 34bc34e | 2012-08-17 18:33:14 +0000 | [diff] [blame] | 403 | Assert1(!GV.hasLinkOnceODRAutoHideLinkage() || GV.hasDefaultVisibility(), |
| 404 | "linkonce_odr_auto_hide can only have default visibility!", |
Bill Wendling | 578ee40 | 2010-08-20 22:05:50 +0000 | [diff] [blame] | 405 | &GV); |
Chris Lattner | 3ac483b | 2003-04-16 20:42:40 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Chris Lattner | e340065 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 408 | void Verifier::visitGlobalVariable(GlobalVariable &GV) { |
Chris Lattner | d79f3d5 | 2007-09-19 17:14:45 +0000 | [diff] [blame] | 409 | if (GV.hasInitializer()) { |
Chris Lattner | e340065 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 410 | Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(), |
| 411 | "Global variable initializer type does not match global " |
| 412 | "variable type!", &GV); |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 413 | |
Chris Lattner | 0aff0b2 | 2009-08-05 05:41:44 +0000 | [diff] [blame] | 414 | // If the global has common linkage, it must have a zero initializer and |
| 415 | // cannot be constant. |
| 416 | if (GV.hasCommonLinkage()) { |
Chris Lattner | d055488 | 2009-08-05 05:21:07 +0000 | [diff] [blame] | 417 | Assert1(GV.getInitializer()->isNullValue(), |
| 418 | "'common' global must have a zero initializer!", &GV); |
Chris Lattner | 0aff0b2 | 2009-08-05 05:41:44 +0000 | [diff] [blame] | 419 | Assert1(!GV.isConstant(), "'common' global may not be marked constant!", |
| 420 | &GV); |
| 421 | } |
Chris Lattner | d79f3d5 | 2007-09-19 17:14:45 +0000 | [diff] [blame] | 422 | } else { |
| 423 | Assert1(GV.hasExternalLinkage() || GV.hasDLLImportLinkage() || |
| 424 | GV.hasExternalWeakLinkage(), |
| 425 | "invalid linkage type for global declaration", &GV); |
| 426 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 427 | |
Nick Lewycky | 466d0c1 | 2011-04-08 07:30:21 +0000 | [diff] [blame] | 428 | if (GV.hasName() && (GV.getName() == "llvm.global_ctors" || |
| 429 | GV.getName() == "llvm.global_dtors")) { |
| 430 | Assert1(!GV.hasInitializer() || GV.hasAppendingLinkage(), |
| 431 | "invalid linkage for intrinsic global variable", &GV); |
| 432 | // Don't worry about emitting an error for it not being an array, |
| 433 | // visitGlobalValue will complain on appending non-array. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 434 | if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getType())) { |
| 435 | StructType *STy = dyn_cast<StructType>(ATy->getElementType()); |
| 436 | PointerType *FuncPtrTy = |
Nick Lewycky | 466d0c1 | 2011-04-08 07:30:21 +0000 | [diff] [blame] | 437 | FunctionType::get(Type::getVoidTy(*Context), false)->getPointerTo(); |
| 438 | Assert1(STy && STy->getNumElements() == 2 && |
| 439 | STy->getTypeAtIndex(0u)->isIntegerTy(32) && |
| 440 | STy->getTypeAtIndex(1) == FuncPtrTy, |
| 441 | "wrong type for intrinsic global variable", &GV); |
| 442 | } |
| 443 | } |
| 444 | |
Chris Lattner | e340065 | 2004-12-15 20:23:49 +0000 | [diff] [blame] | 445 | visitGlobalValue(GV); |
| 446 | } |
| 447 | |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 448 | void Verifier::visitGlobalAlias(GlobalAlias &GA) { |
| 449 | Assert1(!GA.getName().empty(), |
| 450 | "Alias name cannot be empty!", &GA); |
Rafael Espindola | 6de96a1 | 2009-01-15 20:18:42 +0000 | [diff] [blame] | 451 | Assert1(GA.hasExternalLinkage() || GA.hasLocalLinkage() || |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 452 | GA.hasWeakLinkage(), |
| 453 | "Alias should have external or external weak linkage!", &GA); |
Anton Korobeynikov | a50eed4 | 2008-05-08 23:11:06 +0000 | [diff] [blame] | 454 | Assert1(GA.getAliasee(), |
| 455 | "Aliasee cannot be NULL!", &GA); |
Anton Korobeynikov | b18f8f8 | 2007-04-28 13:45:00 +0000 | [diff] [blame] | 456 | Assert1(GA.getType() == GA.getAliasee()->getType(), |
| 457 | "Alias and aliasee types should match!", &GA); |
Rafael Espindola | 45e6c19 | 2011-01-08 16:42:36 +0000 | [diff] [blame] | 458 | Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA); |
Anton Korobeynikov | a50eed4 | 2008-05-08 23:11:06 +0000 | [diff] [blame] | 459 | |
Anton Korobeynikov | 7b2a2f9 | 2007-04-28 14:35:41 +0000 | [diff] [blame] | 460 | if (!isa<GlobalValue>(GA.getAliasee())) { |
| 461 | const ConstantExpr *CE = dyn_cast<ConstantExpr>(GA.getAliasee()); |
Chris Lattner | cde89e4 | 2009-04-25 21:23:19 +0000 | [diff] [blame] | 462 | Assert1(CE && |
| 463 | (CE->getOpcode() == Instruction::BitCast || |
| 464 | CE->getOpcode() == Instruction::GetElementPtr) && |
Anton Korobeynikov | 546ea7e | 2007-04-29 18:02:48 +0000 | [diff] [blame] | 465 | isa<GlobalValue>(CE->getOperand(0)), |
Anton Korobeynikov | 7b2a2f9 | 2007-04-28 14:35:41 +0000 | [diff] [blame] | 466 | "Aliasee should be either GlobalValue or bitcast of GlobalValue", |
| 467 | &GA); |
| 468 | } |
Anton Korobeynikov | 25b2e82 | 2008-03-22 08:36:14 +0000 | [diff] [blame] | 469 | |
Anton Korobeynikov | 1a11404 | 2008-09-09 20:05:04 +0000 | [diff] [blame] | 470 | const GlobalValue* Aliasee = GA.resolveAliasedGlobal(/*stopOnWeak*/ false); |
Anton Korobeynikov | 25b2e82 | 2008-03-22 08:36:14 +0000 | [diff] [blame] | 471 | Assert1(Aliasee, |
Anton Korobeynikov | 3f7fab9 | 2008-03-22 08:37:05 +0000 | [diff] [blame] | 472 | "Aliasing chain should end with function or global variable", &GA); |
Anton Korobeynikov | 25b2e82 | 2008-03-22 08:36:14 +0000 | [diff] [blame] | 473 | |
Anton Korobeynikov | a97b694 | 2007-04-25 14:27:10 +0000 | [diff] [blame] | 474 | visitGlobalValue(GA); |
| 475 | } |
| 476 | |
Duncan Sands | 76d6217 | 2010-04-29 16:10:30 +0000 | [diff] [blame] | 477 | void Verifier::visitNamedMDNode(NamedMDNode &NMD) { |
| 478 | for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) { |
| 479 | MDNode *MD = NMD.getOperand(i); |
| 480 | if (!MD) |
| 481 | continue; |
| 482 | |
Dan Gohman | 2637cc1 | 2010-07-21 23:38:33 +0000 | [diff] [blame] | 483 | Assert1(!MD->isFunctionLocal(), |
| 484 | "Named metadata operand cannot be function local!", MD); |
Duncan Sands | 76d6217 | 2010-04-29 16:10:30 +0000 | [diff] [blame] | 485 | visitMDNode(*MD, 0); |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | void Verifier::visitMDNode(MDNode &MD, Function *F) { |
| 490 | // Only visit each node once. Metadata can be mutually recursive, so this |
| 491 | // avoids infinite recursion here, as well as being an optimization. |
| 492 | if (!MDNodes.insert(&MD)) |
| 493 | return; |
| 494 | |
| 495 | for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) { |
| 496 | Value *Op = MD.getOperand(i); |
| 497 | if (!Op) |
| 498 | continue; |
Dan Gohman | 477498f | 2010-07-21 20:25:43 +0000 | [diff] [blame] | 499 | if (isa<Constant>(Op) || isa<MDString>(Op)) |
Duncan Sands | 76d6217 | 2010-04-29 16:10:30 +0000 | [diff] [blame] | 500 | continue; |
| 501 | if (MDNode *N = dyn_cast<MDNode>(Op)) { |
| 502 | Assert2(MD.isFunctionLocal() || !N->isFunctionLocal(), |
| 503 | "Global metadata operand cannot be function local!", &MD, N); |
| 504 | visitMDNode(*N, F); |
| 505 | continue; |
| 506 | } |
| 507 | Assert2(MD.isFunctionLocal(), "Invalid operand for global metadata!", &MD, Op); |
| 508 | |
| 509 | // If this was an instruction, bb, or argument, verify that it is in the |
| 510 | // function that we expect. |
| 511 | Function *ActualF = 0; |
| 512 | if (Instruction *I = dyn_cast<Instruction>(Op)) |
| 513 | ActualF = I->getParent()->getParent(); |
| 514 | else if (BasicBlock *BB = dyn_cast<BasicBlock>(Op)) |
| 515 | ActualF = BB->getParent(); |
| 516 | else if (Argument *A = dyn_cast<Argument>(Op)) |
| 517 | ActualF = A->getParent(); |
| 518 | assert(ActualF && "Unimplemented function local metadata case!"); |
| 519 | |
| 520 | Assert2(ActualF == F, "function-local metadata used in wrong function", |
| 521 | &MD, Op); |
| 522 | } |
| 523 | } |
| 524 | |
Duncan Sands | c3a7992 | 2009-06-11 08:11:03 +0000 | [diff] [blame] | 525 | // VerifyParameterAttrs - Check the given attributes for an argument or return |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 526 | // value of the specified type. The value V is printed in error messages. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 527 | void Verifier::VerifyParameterAttrs(Attributes Attrs, Type *Ty, |
Duncan Sands | c3a7992 | 2009-06-11 08:11:03 +0000 | [diff] [blame] | 528 | bool isReturnValue, const Value *V) { |
Bill Wendling | f3216e9 | 2012-10-05 06:18:50 +0000 | [diff] [blame^] | 529 | if (!Attrs.hasAttributes()) |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 530 | return; |
| 531 | |
Duncan Sands | c3a7992 | 2009-06-11 08:11:03 +0000 | [diff] [blame] | 532 | Attributes FnCheckAttr = Attrs & Attribute::FunctionOnly; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 533 | Assert1(!FnCheckAttr, "Attribute " + FnCheckAttr.getAsString() + |
Duncan Sands | c3a7992 | 2009-06-11 08:11:03 +0000 | [diff] [blame] | 534 | " only applies to the function!", V); |
| 535 | |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 536 | if (isReturnValue) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 537 | Attributes RetI = Attrs & Attribute::ParameterOnly; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 538 | Assert1(!RetI, "Attribute " + RetI.getAsString() + |
Dan Gohman | 1a70bcc | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 539 | " does not apply to return values!", V); |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 540 | } |
Duncan Sands | c3a7992 | 2009-06-11 08:11:03 +0000 | [diff] [blame] | 541 | |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 542 | for (unsigned i = 0; |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 543 | i < array_lengthof(Attribute::MutuallyIncompatible); ++i) { |
| 544 | Attributes MutI = Attrs & Attribute::MutuallyIncompatible[i]; |
Kostya Serebryany | a5054ad | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 545 | Assert1(MutI.isEmptyOrSingleton(), "Attributes " + |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 546 | MutI.getAsString() + " are incompatible!", V); |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Bill Wendling | eb33723 | 2012-09-25 20:38:59 +0000 | [diff] [blame] | 549 | Attributes TypeI = Attrs & Attributes::typeIncompatible(Ty); |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 550 | Assert1(!TypeI, "Wrong type for attribute " + |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 551 | TypeI.getAsString(), V); |
Dan Gohman | 6d61872 | 2008-08-27 14:48:06 +0000 | [diff] [blame] | 552 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 553 | Attributes ByValI = Attrs & Attribute::ByVal; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 554 | if (PointerType *PTy = dyn_cast<PointerType>(Ty)) { |
Dan Gohman | 6d61872 | 2008-08-27 14:48:06 +0000 | [diff] [blame] | 555 | Assert1(!ByValI || PTy->getElementType()->isSized(), |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 556 | "Attribute " + ByValI.getAsString() + |
Dan Gohman | 6d61872 | 2008-08-27 14:48:06 +0000 | [diff] [blame] | 557 | " does not support unsized types!", V); |
| 558 | } else { |
| 559 | Assert1(!ByValI, |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 560 | "Attribute " + ByValI.getAsString() + |
Dan Gohman | 6d61872 | 2008-08-27 14:48:06 +0000 | [diff] [blame] | 561 | " only applies to parameters with pointer type!", V); |
| 562 | } |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | // VerifyFunctionAttrs - Check parameter attributes against a function type. |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 566 | // The value V is printed in error messages. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 567 | void Verifier::VerifyFunctionAttrs(FunctionType *FT, |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 568 | const AttrListPtr &Attrs, |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 569 | const Value *V) { |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 570 | if (Attrs.isEmpty()) |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 571 | return; |
| 572 | |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 573 | bool SawNest = false; |
| 574 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 575 | for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 576 | const AttributeWithIndex &Attr = Attrs.getSlot(i); |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 577 | |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 578 | Type *Ty; |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 579 | if (Attr.Index == 0) |
| 580 | Ty = FT->getReturnType(); |
| 581 | else if (Attr.Index-1 < FT->getNumParams()) |
| 582 | Ty = FT->getParamType(Attr.Index-1); |
| 583 | else |
Duncan Sands | c3a7992 | 2009-06-11 08:11:03 +0000 | [diff] [blame] | 584 | break; // VarArgs attributes, verified elsewhere. |
| 585 | |
| 586 | VerifyParameterAttrs(Attr.Attrs, Ty, Attr.Index == 0, V); |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 587 | |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 588 | if (Attr.Attrs.hasNestAttr()) { |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 589 | Assert1(!SawNest, "More than one parameter has attribute nest!", V); |
| 590 | SawNest = true; |
| 591 | } |
| 592 | |
Bill Wendling | 3bef2dd | 2012-09-19 23:54:18 +0000 | [diff] [blame] | 593 | if (Attr.Attrs.hasStructRetAttr()) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 594 | Assert1(Attr.Index == 1, "Attribute sret not on first parameter!", V); |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 595 | } |
Devang Patel | 9cc9812 | 2008-10-01 23:41:25 +0000 | [diff] [blame] | 596 | |
| 597 | Attributes FAttrs = Attrs.getFnAttributes(); |
Duncan Sands | c3a7992 | 2009-06-11 08:11:03 +0000 | [diff] [blame] | 598 | Attributes NotFn = FAttrs & (~Attribute::FunctionOnly); |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 599 | Assert1(!NotFn, "Attribute " + NotFn.getAsString() + |
Duncan Sands | c3a7992 | 2009-06-11 08:11:03 +0000 | [diff] [blame] | 600 | " does not apply to the function!", V); |
| 601 | |
Devang Patel | 9cc9812 | 2008-10-01 23:41:25 +0000 | [diff] [blame] | 602 | for (unsigned i = 0; |
| 603 | i < array_lengthof(Attribute::MutuallyIncompatible); ++i) { |
| 604 | Attributes MutI = FAttrs & Attribute::MutuallyIncompatible[i]; |
Kostya Serebryany | a5054ad | 2012-01-20 17:56:17 +0000 | [diff] [blame] | 605 | Assert1(MutI.isEmptyOrSingleton(), "Attributes " + |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 606 | MutI.getAsString() + " are incompatible!", V); |
Devang Patel | 9cc9812 | 2008-10-01 23:41:25 +0000 | [diff] [blame] | 607 | } |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 610 | static bool VerifyAttributeCount(const AttrListPtr &Attrs, unsigned Params) { |
Devang Patel | 82fed67 | 2008-09-23 22:35:17 +0000 | [diff] [blame] | 611 | if (Attrs.isEmpty()) |
| 612 | return true; |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 613 | |
Devang Patel | 82fed67 | 2008-09-23 22:35:17 +0000 | [diff] [blame] | 614 | unsigned LastSlot = Attrs.getNumSlots() - 1; |
| 615 | unsigned LastIndex = Attrs.getSlot(LastSlot).Index; |
| 616 | if (LastIndex <= Params |
| 617 | || (LastIndex == (unsigned)~0 |
| 618 | && (LastSlot == 0 || Attrs.getSlot(LastSlot - 1).Index <= Params))) |
| 619 | return true; |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 620 | |
Devang Patel | 82fed67 | 2008-09-23 22:35:17 +0000 | [diff] [blame] | 621 | return false; |
| 622 | } |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 623 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 624 | // visitFunction - Verify that a function is ok. |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 625 | // |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 626 | void Verifier::visitFunction(Function &F) { |
Chris Lattner | 2ad5aa8 | 2005-05-08 22:27:09 +0000 | [diff] [blame] | 627 | // Check function arguments. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 628 | FunctionType *FT = F.getFunctionType(); |
Chris Lattner | 45ffa21 | 2007-08-18 06:13:19 +0000 | [diff] [blame] | 629 | unsigned NumArgs = F.arg_size(); |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 630 | |
Nick Lewycky | 62f864d | 2010-02-15 21:52:04 +0000 | [diff] [blame] | 631 | Assert1(Context == &F.getContext(), |
| 632 | "Function context does not match Module context!", &F); |
| 633 | |
Chris Lattner | d055488 | 2009-08-05 05:21:07 +0000 | [diff] [blame] | 634 | Assert1(!F.hasCommonLinkage(), "Functions may not have common linkage", &F); |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 635 | Assert2(FT->getNumParams() == NumArgs, |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 636 | "# formal arguments must match # of arguments for function type!", |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 637 | &F, FT); |
Chris Lattner | 3ae303c | 2003-11-21 22:32:23 +0000 | [diff] [blame] | 638 | Assert1(F.getReturnType()->isFirstClassType() || |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 639 | F.getReturnType()->isVoidTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 640 | F.getReturnType()->isStructTy(), |
Chris Lattner | 3ae303c | 2003-11-21 22:32:23 +0000 | [diff] [blame] | 641 | "Functions cannot return aggregate values!", &F); |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 642 | |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 643 | Assert1(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(), |
Devang Patel | 9d917859 | 2008-03-03 21:46:28 +0000 | [diff] [blame] | 644 | "Invalid struct return type!", &F); |
| 645 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 646 | const AttrListPtr &Attrs = F.getAttributes(); |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 647 | |
Devang Patel | 82fed67 | 2008-09-23 22:35:17 +0000 | [diff] [blame] | 648 | Assert1(VerifyAttributeCount(Attrs, FT->getNumParams()), |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 649 | "Attributes after last parameter!", &F); |
| 650 | |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 651 | // Check function attributes. |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 652 | VerifyFunctionAttrs(FT, Attrs, &F); |
Duncan Sands | 07c9066 | 2007-07-27 15:09:54 +0000 | [diff] [blame] | 653 | |
Chris Lattner | ef13ee3 | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 654 | // Check that this function meets the restrictions on this calling convention. |
| 655 | switch (F.getCallingConv()) { |
| 656 | default: |
| 657 | break; |
| 658 | case CallingConv::C: |
| 659 | break; |
Chris Lattner | ef13ee3 | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 660 | case CallingConv::Fast: |
| 661 | case CallingConv::Cold: |
Anton Korobeynikov | 6f7072c | 2006-09-17 20:25:45 +0000 | [diff] [blame] | 662 | case CallingConv::X86_FastCall: |
Anton Korobeynikov | 8f35fab | 2010-05-16 09:08:45 +0000 | [diff] [blame] | 663 | case CallingConv::X86_ThisCall: |
Che-Liang Chiou | 2994790 | 2010-09-25 07:46:17 +0000 | [diff] [blame] | 664 | case CallingConv::PTX_Kernel: |
| 665 | case CallingConv::PTX_Device: |
Chris Lattner | ef13ee3 | 2006-05-19 21:25:17 +0000 | [diff] [blame] | 666 | Assert1(!F.isVarArg(), |
| 667 | "Varargs functions must have C calling conventions!", &F); |
| 668 | break; |
| 669 | } |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 670 | |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 671 | bool isLLVMdotName = F.getName().size() >= 5 && |
| 672 | F.getName().substr(0, 5) == "llvm."; |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 673 | |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 674 | // Check that the argument values match the function type for this function... |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 675 | unsigned i = 0; |
Chris Lattner | 6976177 | 2006-12-16 02:25:35 +0000 | [diff] [blame] | 676 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); |
| 677 | I != E; ++I, ++i) { |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 678 | Assert2(I->getType() == FT->getParamType(i), |
| 679 | "Argument value does not match function argument type!", |
| 680 | I, FT->getParamType(i)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 681 | Assert1(I->getType()->isFirstClassType(), |
Dan Gohman | 4051bf4 | 2008-08-27 14:44:57 +0000 | [diff] [blame] | 682 | "Function arguments must have first-class types!", I); |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 683 | if (!isLLVMdotName) |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 684 | Assert2(!I->getType()->isMetadataTy(), |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 685 | "Function takes metadata but isn't an intrinsic", I, &F); |
Dan Gohman | 4051bf4 | 2008-08-27 14:44:57 +0000 | [diff] [blame] | 686 | } |
Chris Lattner | af95e58 | 2002-04-13 22:48:46 +0000 | [diff] [blame] | 687 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 688 | if (F.isMaterializable()) { |
| 689 | // Function has a body somewhere we can't see. |
| 690 | } else if (F.isDeclaration()) { |
Chris Lattner | d79f3d5 | 2007-09-19 17:14:45 +0000 | [diff] [blame] | 691 | Assert1(F.hasExternalLinkage() || F.hasDLLImportLinkage() || |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 692 | F.hasExternalWeakLinkage(), |
Chris Lattner | d79f3d5 | 2007-09-19 17:14:45 +0000 | [diff] [blame] | 693 | "invalid linkage type for function declaration", &F); |
| 694 | } else { |
Chris Lattner | cb81331 | 2006-12-13 04:45:46 +0000 | [diff] [blame] | 695 | // Verify that this function (which has a body) is not named "llvm.*". It |
| 696 | // is not legal to define intrinsics. |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 697 | Assert1(!isLLVMdotName, "llvm intrinsics cannot be defined!", &F); |
Chris Lattner | cb81331 | 2006-12-13 04:45:46 +0000 | [diff] [blame] | 698 | |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 699 | // Check the entry node |
Chris Lattner | 5dac64f | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 700 | BasicBlock *Entry = &F.getEntryBlock(); |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 701 | Assert1(pred_begin(Entry) == pred_end(Entry), |
| 702 | "Entry block to function must not have predecessors!", Entry); |
Chris Lattner | 2747174 | 2009-11-01 04:08:01 +0000 | [diff] [blame] | 703 | |
| 704 | // The address of the entry block cannot be taken, unless it is dead. |
| 705 | if (Entry->hasAddressTaken()) { |
Chris Lattner | 253bc77 | 2009-11-01 18:11:50 +0000 | [diff] [blame] | 706 | Assert1(!BlockAddress::get(Entry)->isConstantUsed(), |
Chris Lattner | 2747174 | 2009-11-01 04:08:01 +0000 | [diff] [blame] | 707 | "blockaddress may not be used with the entry block!", Entry); |
| 708 | } |
Chris Lattner | 149376d | 2002-10-13 20:57:00 +0000 | [diff] [blame] | 709 | } |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 710 | |
Chris Lattner | 7730dcc | 2009-09-11 17:05:29 +0000 | [diff] [blame] | 711 | // If this function is actually an intrinsic, verify that it is only used in |
| 712 | // direct call/invokes, never having its "address taken". |
| 713 | if (F.getIntrinsicID()) { |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 714 | const User *U; |
| 715 | if (F.hasAddressTaken(&U)) |
Chris Lattner | 7730dcc | 2009-09-11 17:05:29 +0000 | [diff] [blame] | 716 | Assert1(0, "Invalid user of intrinsic instruction!", U); |
Chris Lattner | 7730dcc | 2009-09-11 17:05:29 +0000 | [diff] [blame] | 717 | } |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 720 | // verifyBasicBlock - Verify that a basic block is well formed... |
| 721 | // |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 722 | void Verifier::visitBasicBlock(BasicBlock &BB) { |
Chris Lattner | c9e79d0 | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 723 | InstsInThisBlock.clear(); |
| 724 | |
Alkis Evlogimenos | be526cf | 2004-12-04 02:30:42 +0000 | [diff] [blame] | 725 | // Ensure that basic blocks have terminators! |
| 726 | Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB); |
| 727 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 728 | // Check constraints that this basic block imposes on all of the PHI nodes in |
| 729 | // it. |
| 730 | if (isa<PHINode>(BB.front())) { |
Chris Lattner | 59a8d2c | 2007-02-10 08:33:11 +0000 | [diff] [blame] | 731 | SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB)); |
| 732 | SmallVector<std::pair<BasicBlock*, Value*>, 8> Values; |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 733 | std::sort(Preds.begin(), Preds.end()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 734 | PHINode *PN; |
Chris Lattner | 307e1df | 2004-06-05 17:44:48 +0000 | [diff] [blame] | 735 | for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) { |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 736 | // Ensure that PHI nodes have at least one entry! |
| 737 | Assert1(PN->getNumIncomingValues() != 0, |
| 738 | "PHI nodes must have at least one entry. If the block is dead, " |
| 739 | "the PHI should be removed!", PN); |
Brian Gaeke | e8a6bf3 | 2004-05-17 21:15:18 +0000 | [diff] [blame] | 740 | Assert1(PN->getNumIncomingValues() == Preds.size(), |
| 741 | "PHINode should have one entry for each predecessor of its " |
| 742 | "parent basic block!", PN); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 743 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 744 | // Get and sort all incoming values in the PHI node... |
Chris Lattner | 59a8d2c | 2007-02-10 08:33:11 +0000 | [diff] [blame] | 745 | Values.clear(); |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 746 | Values.reserve(PN->getNumIncomingValues()); |
| 747 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 748 | Values.push_back(std::make_pair(PN->getIncomingBlock(i), |
| 749 | PN->getIncomingValue(i))); |
| 750 | std::sort(Values.begin(), Values.end()); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 751 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 752 | for (unsigned i = 0, e = Values.size(); i != e; ++i) { |
| 753 | // Check to make sure that if there is more than one entry for a |
| 754 | // particular basic block in this PHI node, that the incoming values are |
| 755 | // all identical. |
| 756 | // |
| 757 | Assert4(i == 0 || Values[i].first != Values[i-1].first || |
| 758 | Values[i].second == Values[i-1].second, |
| 759 | "PHI node has multiple entries for the same basic block with " |
| 760 | "different incoming values!", PN, Values[i].first, |
| 761 | Values[i].second, Values[i-1].second); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 762 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 763 | // Check to make sure that the predecessors and PHI node entries are |
| 764 | // matched up. |
| 765 | Assert3(Values[i].first == Preds[i], |
| 766 | "PHI node entries do not match predecessors!", PN, |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 767 | Values[i].first, Preds[i]); |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
| 770 | } |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 771 | } |
Chris Lattner | fbf5be5 | 2002-03-15 20:25:09 +0000 | [diff] [blame] | 772 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 773 | void Verifier::visitTerminatorInst(TerminatorInst &I) { |
| 774 | // Ensure that terminators only exist at the end of the basic block. |
| 775 | Assert1(&I == I.getParent()->getTerminator(), |
| 776 | "Terminator found in the middle of a basic block!", I.getParent()); |
Chris Lattner | 7af3ee9 | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 777 | visitInstruction(I); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Nick Lewycky | 1d9a815 | 2010-02-15 22:09:09 +0000 | [diff] [blame] | 780 | void Verifier::visitBranchInst(BranchInst &BI) { |
| 781 | if (BI.isConditional()) { |
| 782 | Assert2(BI.getCondition()->getType()->isIntegerTy(1), |
| 783 | "Branch condition is not 'i1' type!", &BI, BI.getCondition()); |
| 784 | } |
| 785 | visitTerminatorInst(BI); |
| 786 | } |
| 787 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 788 | void Verifier::visitReturnInst(ReturnInst &RI) { |
| 789 | Function *F = RI.getParent()->getParent(); |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 790 | unsigned N = RI.getNumOperands(); |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 791 | if (F->getReturnType()->isVoidTy()) |
Chris Lattner | 1e31bf5 | 2008-04-23 20:33:41 +0000 | [diff] [blame] | 792 | Assert2(N == 0, |
Nick Lewycky | 967aeeb | 2008-11-15 17:50:47 +0000 | [diff] [blame] | 793 | "Found return instr that returns non-void in Function of void " |
Alkis Evlogimenos | b92de19 | 2004-12-04 01:25:06 +0000 | [diff] [blame] | 794 | "return type!", &RI, F->getReturnType()); |
Jay Foad | 1152209 | 2011-04-04 07:44:02 +0000 | [diff] [blame] | 795 | else |
| 796 | Assert2(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(), |
| 797 | "Function return type does not match operand " |
| 798 | "type of return inst!", &RI, F->getReturnType()); |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 799 | |
Misha Brukman | 7eb05a1 | 2003-08-18 14:43:39 +0000 | [diff] [blame] | 800 | // Check to make sure that the return value has necessary properties for |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 801 | // terminators... |
| 802 | visitTerminatorInst(RI); |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 803 | } |
| 804 | |
Chris Lattner | ab5aa14 | 2004-05-21 16:47:21 +0000 | [diff] [blame] | 805 | void Verifier::visitSwitchInst(SwitchInst &SI) { |
| 806 | // Check to make sure that all of the constants in the switch instruction |
| 807 | // have the same type as the switched-on value. |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 808 | Type *SwitchTy = SI.getCondition()->getType(); |
Stepan Dyatkovskiy | e3e19cb | 2012-05-28 12:39:09 +0000 | [diff] [blame] | 809 | IntegerType *IntTy = cast<IntegerType>(SwitchTy); |
Stepan Dyatkovskiy | 58107dd | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 810 | IntegersSubsetToBB Mapping; |
| 811 | std::map<IntegersSubset::Range, unsigned> RangeSetMap; |
Stepan Dyatkovskiy | 97b02fc | 2012-03-11 06:09:17 +0000 | [diff] [blame] | 812 | for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); i != e; ++i) { |
Stepan Dyatkovskiy | 58107dd | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 813 | IntegersSubset CaseRanges = i.getCaseValueEx(); |
| 814 | for (unsigned ri = 0, rie = CaseRanges.getNumItems(); ri < rie; ++ri) { |
| 815 | IntegersSubset::Range r = CaseRanges.getItem(ri); |
Stepan Dyatkovskiy | 0e46d8a | 2012-06-02 09:42:43 +0000 | [diff] [blame] | 816 | Assert1(((const APInt&)r.getLow()).getBitWidth() == IntTy->getBitWidth(), |
Stepan Dyatkovskiy | e89dafd | 2012-05-21 10:44:40 +0000 | [diff] [blame] | 817 | "Switch constants must all be same type as switch value!", &SI); |
Stepan Dyatkovskiy | 0e46d8a | 2012-06-02 09:42:43 +0000 | [diff] [blame] | 818 | Assert1(((const APInt&)r.getHigh()).getBitWidth() == IntTy->getBitWidth(), |
Stepan Dyatkovskiy | e89dafd | 2012-05-21 10:44:40 +0000 | [diff] [blame] | 819 | "Switch constants must all be same type as switch value!", &SI); |
Stepan Dyatkovskiy | 58107dd | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 820 | Mapping.add(r); |
Stepan Dyatkovskiy | e89dafd | 2012-05-21 10:44:40 +0000 | [diff] [blame] | 821 | RangeSetMap[r] = i.getCaseIndex(); |
| 822 | } |
Chris Lattner | 7eb8415 | 2009-11-11 17:37:02 +0000 | [diff] [blame] | 823 | } |
Stepan Dyatkovskiy | e89dafd | 2012-05-21 10:44:40 +0000 | [diff] [blame] | 824 | |
Stepan Dyatkovskiy | 58107dd | 2012-05-29 12:26:47 +0000 | [diff] [blame] | 825 | IntegersSubsetToBB::RangeIterator errItem; |
| 826 | if (!Mapping.verify(errItem)) { |
Stepan Dyatkovskiy | e89dafd | 2012-05-21 10:44:40 +0000 | [diff] [blame] | 827 | unsigned CaseIndex = RangeSetMap[errItem->first]; |
| 828 | SwitchInst::CaseIt i(&SI, CaseIndex); |
| 829 | Assert2(false, "Duplicate integer as switch case", &SI, i.getCaseValueEx()); |
| 830 | } |
| 831 | |
Chris Lattner | ab5aa14 | 2004-05-21 16:47:21 +0000 | [diff] [blame] | 832 | visitTerminatorInst(SI); |
| 833 | } |
| 834 | |
Dan Gohman | d0a1e3d | 2010-08-02 23:08:33 +0000 | [diff] [blame] | 835 | void Verifier::visitIndirectBrInst(IndirectBrInst &BI) { |
| 836 | Assert1(BI.getAddress()->getType()->isPointerTy(), |
| 837 | "Indirectbr operand must have pointer type!", &BI); |
| 838 | for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i) |
| 839 | Assert1(BI.getDestination(i)->getType()->isLabelTy(), |
| 840 | "Indirectbr destinations must all have pointer type!", &BI); |
| 841 | |
| 842 | visitTerminatorInst(BI); |
| 843 | } |
| 844 | |
Chris Lattner | 75648e7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 845 | void Verifier::visitSelectInst(SelectInst &SI) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 846 | Assert1(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1), |
| 847 | SI.getOperand(2)), |
| 848 | "Invalid operands for select instruction!", &SI); |
| 849 | |
Chris Lattner | 75648e7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 850 | Assert1(SI.getTrueValue()->getType() == SI.getType(), |
| 851 | "Select values must have same type as select instruction!", &SI); |
Chris Lattner | cde15fb | 2004-09-29 21:19:28 +0000 | [diff] [blame] | 852 | visitInstruction(SI); |
Chris Lattner | 75648e7 | 2004-03-12 05:54:31 +0000 | [diff] [blame] | 853 | } |
| 854 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 855 | /// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of |
| 856 | /// a pass, if any exist, it's an error. |
| 857 | /// |
Chris Lattner | 903a25d | 2002-11-21 16:54:22 +0000 | [diff] [blame] | 858 | void Verifier::visitUserOp1(Instruction &I) { |
Chris Lattner | b37dfd6 | 2006-03-31 04:46:47 +0000 | [diff] [blame] | 859 | 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] | 860 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 861 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 862 | void Verifier::visitTruncInst(TruncInst &I) { |
| 863 | // Get the source and destination types |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 864 | Type *SrcTy = I.getOperand(0)->getType(); |
| 865 | Type *DestTy = I.getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 866 | |
| 867 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 868 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 869 | unsigned DestBitSize = DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 870 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 871 | Assert1(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I); |
| 872 | Assert1(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 873 | Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(), |
Chris Lattner | b2b1773 | 2009-02-02 07:40:17 +0000 | [diff] [blame] | 874 | "trunc source and destination must both be a vector or neither", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 875 | Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I); |
| 876 | |
| 877 | visitInstruction(I); |
| 878 | } |
| 879 | |
| 880 | void Verifier::visitZExtInst(ZExtInst &I) { |
| 881 | // Get the source and destination types |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 882 | Type *SrcTy = I.getOperand(0)->getType(); |
| 883 | Type *DestTy = I.getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 884 | |
| 885 | // Get the size of the types in bits, we'll need this later |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 886 | Assert1(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I); |
| 887 | Assert1(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 888 | Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(), |
Chris Lattner | b2b1773 | 2009-02-02 07:40:17 +0000 | [diff] [blame] | 889 | "zext source and destination must both be a vector or neither", &I); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 890 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 891 | unsigned DestBitSize = DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 892 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 893 | Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I); |
| 894 | |
| 895 | visitInstruction(I); |
| 896 | } |
| 897 | |
| 898 | void Verifier::visitSExtInst(SExtInst &I) { |
| 899 | // Get the source and destination types |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 900 | Type *SrcTy = I.getOperand(0)->getType(); |
| 901 | Type *DestTy = I.getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 902 | |
| 903 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 904 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 905 | unsigned DestBitSize = DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 906 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 907 | Assert1(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I); |
| 908 | Assert1(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 909 | Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(), |
Chris Lattner | b2b1773 | 2009-02-02 07:40:17 +0000 | [diff] [blame] | 910 | "sext source and destination must both be a vector or neither", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 911 | Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I); |
| 912 | |
| 913 | visitInstruction(I); |
| 914 | } |
| 915 | |
| 916 | void Verifier::visitFPTruncInst(FPTruncInst &I) { |
| 917 | // Get the source and destination types |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 918 | Type *SrcTy = I.getOperand(0)->getType(); |
| 919 | Type *DestTy = I.getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 920 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 921 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 922 | unsigned DestBitSize = DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 923 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 924 | Assert1(SrcTy->isFPOrFPVectorTy(),"FPTrunc only operates on FP", &I); |
| 925 | Assert1(DestTy->isFPOrFPVectorTy(),"FPTrunc only produces an FP", &I); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 926 | Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(), |
Chris Lattner | b2b1773 | 2009-02-02 07:40:17 +0000 | [diff] [blame] | 927 | "fptrunc source and destination must both be a vector or neither",&I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 928 | Assert1(SrcBitSize > DestBitSize,"DestTy too big for FPTrunc", &I); |
| 929 | |
| 930 | visitInstruction(I); |
| 931 | } |
| 932 | |
| 933 | void Verifier::visitFPExtInst(FPExtInst &I) { |
| 934 | // Get the source and destination types |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 935 | Type *SrcTy = I.getOperand(0)->getType(); |
| 936 | Type *DestTy = I.getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 937 | |
| 938 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 939 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 940 | unsigned DestBitSize = DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 941 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 942 | Assert1(SrcTy->isFPOrFPVectorTy(),"FPExt only operates on FP", &I); |
| 943 | Assert1(DestTy->isFPOrFPVectorTy(),"FPExt only produces an FP", &I); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 944 | Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(), |
Chris Lattner | b2b1773 | 2009-02-02 07:40:17 +0000 | [diff] [blame] | 945 | "fpext source and destination must both be a vector or neither", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 946 | Assert1(SrcBitSize < DestBitSize,"DestTy too small for FPExt", &I); |
| 947 | |
| 948 | visitInstruction(I); |
| 949 | } |
| 950 | |
| 951 | void Verifier::visitUIToFPInst(UIToFPInst &I) { |
| 952 | // Get the source and destination types |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 953 | Type *SrcTy = I.getOperand(0)->getType(); |
| 954 | Type *DestTy = I.getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 955 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 956 | bool SrcVec = SrcTy->isVectorTy(); |
| 957 | bool DstVec = DestTy->isVectorTy(); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 958 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 959 | Assert1(SrcVec == DstVec, |
| 960 | "UIToFP source and dest must both be vector or scalar", &I); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 961 | Assert1(SrcTy->isIntOrIntVectorTy(), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 962 | "UIToFP source must be integer or integer vector", &I); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 963 | Assert1(DestTy->isFPOrFPVectorTy(), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 964 | "UIToFP result must be FP or FP vector", &I); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 965 | |
| 966 | if (SrcVec && DstVec) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 967 | Assert1(cast<VectorType>(SrcTy)->getNumElements() == |
| 968 | cast<VectorType>(DestTy)->getNumElements(), |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 969 | "UIToFP source and dest vector length mismatch", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 970 | |
| 971 | visitInstruction(I); |
| 972 | } |
| 973 | |
| 974 | void Verifier::visitSIToFPInst(SIToFPInst &I) { |
| 975 | // Get the source and destination types |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 976 | Type *SrcTy = I.getOperand(0)->getType(); |
| 977 | Type *DestTy = I.getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 978 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 979 | bool SrcVec = SrcTy->isVectorTy(); |
| 980 | bool DstVec = DestTy->isVectorTy(); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 981 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 982 | Assert1(SrcVec == DstVec, |
| 983 | "SIToFP source and dest must both be vector or scalar", &I); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 984 | Assert1(SrcTy->isIntOrIntVectorTy(), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 985 | "SIToFP source must be integer or integer vector", &I); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 986 | Assert1(DestTy->isFPOrFPVectorTy(), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 987 | "SIToFP result must be FP or FP vector", &I); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 988 | |
| 989 | if (SrcVec && DstVec) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 990 | Assert1(cast<VectorType>(SrcTy)->getNumElements() == |
| 991 | cast<VectorType>(DestTy)->getNumElements(), |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 992 | "SIToFP source and dest vector length mismatch", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 993 | |
| 994 | visitInstruction(I); |
| 995 | } |
| 996 | |
| 997 | void Verifier::visitFPToUIInst(FPToUIInst &I) { |
| 998 | // Get the source and destination types |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 999 | Type *SrcTy = I.getOperand(0)->getType(); |
| 1000 | Type *DestTy = I.getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1001 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1002 | bool SrcVec = SrcTy->isVectorTy(); |
| 1003 | bool DstVec = DestTy->isVectorTy(); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1005 | Assert1(SrcVec == DstVec, |
| 1006 | "FPToUI source and dest must both be vector or scalar", &I); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1007 | Assert1(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector", |
| 1008 | &I); |
| 1009 | Assert1(DestTy->isIntOrIntVectorTy(), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1010 | "FPToUI result must be integer or integer vector", &I); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1011 | |
| 1012 | if (SrcVec && DstVec) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1013 | Assert1(cast<VectorType>(SrcTy)->getNumElements() == |
| 1014 | cast<VectorType>(DestTy)->getNumElements(), |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1015 | "FPToUI source and dest vector length mismatch", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1016 | |
| 1017 | visitInstruction(I); |
| 1018 | } |
| 1019 | |
| 1020 | void Verifier::visitFPToSIInst(FPToSIInst &I) { |
| 1021 | // Get the source and destination types |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1022 | Type *SrcTy = I.getOperand(0)->getType(); |
| 1023 | Type *DestTy = I.getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1024 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1025 | bool SrcVec = SrcTy->isVectorTy(); |
| 1026 | bool DstVec = DestTy->isVectorTy(); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1027 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1028 | Assert1(SrcVec == DstVec, |
| 1029 | "FPToSI source and dest must both be vector or scalar", &I); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1030 | Assert1(SrcTy->isFPOrFPVectorTy(), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1031 | "FPToSI source must be FP or FP vector", &I); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1032 | Assert1(DestTy->isIntOrIntVectorTy(), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1033 | "FPToSI result must be integer or integer vector", &I); |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1034 | |
| 1035 | if (SrcVec && DstVec) |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1036 | Assert1(cast<VectorType>(SrcTy)->getNumElements() == |
| 1037 | cast<VectorType>(DestTy)->getNumElements(), |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 1038 | "FPToSI source and dest vector length mismatch", &I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1039 | |
| 1040 | visitInstruction(I); |
| 1041 | } |
| 1042 | |
| 1043 | void Verifier::visitPtrToIntInst(PtrToIntInst &I) { |
| 1044 | // Get the source and destination types |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1045 | Type *SrcTy = I.getOperand(0)->getType(); |
| 1046 | Type *DestTy = I.getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1047 | |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1048 | Assert1(SrcTy->getScalarType()->isPointerTy(), |
| 1049 | "PtrToInt source must be pointer", &I); |
| 1050 | Assert1(DestTy->getScalarType()->isIntegerTy(), |
| 1051 | "PtrToInt result must be integral", &I); |
| 1052 | Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(), |
| 1053 | "PtrToInt type mismatch", &I); |
| 1054 | |
| 1055 | if (SrcTy->isVectorTy()) { |
| 1056 | VectorType *VSrc = dyn_cast<VectorType>(SrcTy); |
| 1057 | VectorType *VDest = dyn_cast<VectorType>(DestTy); |
| 1058 | Assert1(VSrc->getNumElements() == VDest->getNumElements(), |
| 1059 | "PtrToInt Vector width mismatch", &I); |
| 1060 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1061 | |
| 1062 | visitInstruction(I); |
| 1063 | } |
| 1064 | |
| 1065 | void Verifier::visitIntToPtrInst(IntToPtrInst &I) { |
| 1066 | // Get the source and destination types |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1067 | Type *SrcTy = I.getOperand(0)->getType(); |
| 1068 | Type *DestTy = I.getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1069 | |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1070 | Assert1(SrcTy->getScalarType()->isIntegerTy(), |
| 1071 | "IntToPtr source must be an integral", &I); |
| 1072 | Assert1(DestTy->getScalarType()->isPointerTy(), |
| 1073 | "IntToPtr result must be a pointer",&I); |
| 1074 | Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(), |
| 1075 | "IntToPtr type mismatch", &I); |
| 1076 | if (SrcTy->isVectorTy()) { |
| 1077 | VectorType *VSrc = dyn_cast<VectorType>(SrcTy); |
| 1078 | VectorType *VDest = dyn_cast<VectorType>(DestTy); |
| 1079 | Assert1(VSrc->getNumElements() == VDest->getNumElements(), |
| 1080 | "IntToPtr Vector width mismatch", &I); |
| 1081 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1082 | visitInstruction(I); |
| 1083 | } |
| 1084 | |
| 1085 | void Verifier::visitBitCastInst(BitCastInst &I) { |
| 1086 | // Get the source and destination types |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1087 | Type *SrcTy = I.getOperand(0)->getType(); |
| 1088 | Type *DestTy = I.getType(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1089 | |
| 1090 | // Get the size of the types in bits, we'll need this later |
| 1091 | unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); |
| 1092 | unsigned DestBitSize = DestTy->getPrimitiveSizeInBits(); |
| 1093 | |
| 1094 | // BitCast implies a no-op cast of type only. No bits change. |
| 1095 | // However, you can't cast pointers to anything but pointers. |
Nick Lewycky | 58564d5 | 2012-08-15 02:37:07 +0000 | [diff] [blame] | 1096 | Assert1(SrcTy->isPointerTy() == DestTy->isPointerTy(), |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1097 | "Bitcast requires both operands to be pointer or neither", &I); |
Nate Begeman | 50b69ea | 2009-07-30 02:00:06 +0000 | [diff] [blame] | 1098 | Assert1(SrcBitSize == DestBitSize, "Bitcast requires types of same width",&I); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1099 | |
Dan Gohman | c05dca9 | 2008-09-08 16:45:59 +0000 | [diff] [blame] | 1100 | // Disallow aggregates. |
| 1101 | Assert1(!SrcTy->isAggregateType(), |
| 1102 | "Bitcast operand must not be aggregate", &I); |
| 1103 | Assert1(!DestTy->isAggregateType(), |
| 1104 | "Bitcast type must not be aggregate", &I); |
| 1105 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1106 | visitInstruction(I); |
| 1107 | } |
| 1108 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1109 | /// visitPHINode - Ensure that a PHI node is well formed. |
| 1110 | /// |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1111 | void Verifier::visitPHINode(PHINode &PN) { |
| 1112 | // Ensure that the PHI nodes are all grouped together at the top of the block. |
| 1113 | // This can be tested by checking whether the instruction before this is |
Misha Brukman | fa10053 | 2003-10-10 17:54:14 +0000 | [diff] [blame] | 1114 | // 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] | 1115 | // then there is some other instruction before a PHI. |
Chris Lattner | da1ed8d | 2007-04-17 17:36:12 +0000 | [diff] [blame] | 1116 | Assert2(&PN == &PN.getParent()->front() || |
| 1117 | isa<PHINode>(--BasicBlock::iterator(&PN)), |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1118 | "PHI nodes not grouped at top of basic block!", |
| 1119 | &PN, PN.getParent()); |
| 1120 | |
Nick Lewycky | b2b0467 | 2009-09-08 01:23:52 +0000 | [diff] [blame] | 1121 | // Check that all of the values of the PHI node have the same type as the |
| 1122 | // result, and that the incoming blocks are really basic blocks. |
| 1123 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) { |
Chris Lattner | 3b93c91 | 2003-11-12 07:13:37 +0000 | [diff] [blame] | 1124 | Assert1(PN.getType() == PN.getIncomingValue(i)->getType(), |
| 1125 | "PHI node operands are not the same type as the result!", &PN); |
Nick Lewycky | b2b0467 | 2009-09-08 01:23:52 +0000 | [diff] [blame] | 1126 | } |
Chris Lattner | 3b93c91 | 2003-11-12 07:13:37 +0000 | [diff] [blame] | 1127 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1128 | // All other PHI node constraints are checked in the visitBasicBlock method. |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1129 | |
| 1130 | visitInstruction(PN); |
| 1131 | } |
| 1132 | |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 1133 | void Verifier::VerifyCallSite(CallSite CS) { |
| 1134 | Instruction *I = CS.getInstruction(); |
| 1135 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1136 | Assert1(CS.getCalledValue()->getType()->isPointerTy(), |
Nick Lewycky | 984161a | 2009-09-08 02:02:39 +0000 | [diff] [blame] | 1137 | "Called function must be a pointer!", I); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1138 | PointerType *FPTy = cast<PointerType>(CS.getCalledValue()->getType()); |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 1139 | |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1140 | Assert1(FPTy->getElementType()->isFunctionTy(), |
Nick Lewycky | 984161a | 2009-09-08 02:02:39 +0000 | [diff] [blame] | 1141 | "Called function is not pointer to function type!", I); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1142 | FunctionType *FTy = cast<FunctionType>(FPTy->getElementType()); |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 1143 | |
| 1144 | // Verify that the correct number of arguments are being passed |
| 1145 | if (FTy->isVarArg()) |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 1146 | Assert1(CS.arg_size() >= FTy->getNumParams(), |
| 1147 | "Called function requires more parameters than were provided!",I); |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 1148 | else |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 1149 | Assert1(CS.arg_size() == FTy->getNumParams(), |
| 1150 | "Incorrect number of arguments passed to called function!", I); |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 1151 | |
Chris Lattner | 609de00 | 2010-05-10 20:58:42 +0000 | [diff] [blame] | 1152 | // Verify that all arguments to the call match the function type. |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 1153 | for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i) |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 1154 | Assert3(CS.getArgument(i)->getType() == FTy->getParamType(i), |
Chris Lattner | 338a462 | 2002-05-08 19:49:50 +0000 | [diff] [blame] | 1155 | "Call parameter type does not match function signature!", |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 1156 | CS.getArgument(i), FTy->getParamType(i), I); |
| 1157 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1158 | const AttrListPtr &Attrs = CS.getAttributes(); |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 1159 | |
Devang Patel | 82fed67 | 2008-09-23 22:35:17 +0000 | [diff] [blame] | 1160 | Assert1(VerifyAttributeCount(Attrs, CS.arg_size()), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1161 | "Attributes after last parameter!", I); |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 1162 | |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 1163 | // Verify call attributes. |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 1164 | VerifyFunctionAttrs(FTy, Attrs, I); |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 1165 | |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 1166 | if (FTy->isVarArg()) |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 1167 | // Check attributes on the varargs part. |
| 1168 | for (unsigned Idx = 1 + FTy->getNumParams(); Idx <= CS.arg_size(); ++Idx) { |
Devang Patel | a05633e | 2008-09-26 22:53:05 +0000 | [diff] [blame] | 1169 | Attributes Attr = Attrs.getParamAttributes(Idx); |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 1170 | |
Duncan Sands | c3a7992 | 2009-06-11 08:11:03 +0000 | [diff] [blame] | 1171 | VerifyParameterAttrs(Attr, CS.getArgument(Idx-1)->getType(), false, I); |
Duncan Sands | 0009c44 | 2008-01-12 16:42:01 +0000 | [diff] [blame] | 1172 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 1173 | Attributes VArgI = Attr & Attribute::VarArgsIncompatible; |
Bill Wendling | de74cf5 | 2012-09-20 14:44:42 +0000 | [diff] [blame] | 1174 | Assert1(!VArgI, "Attribute " + VArgI.getAsString() + |
Dan Gohman | 1a70bcc | 2008-08-05 15:51:44 +0000 | [diff] [blame] | 1175 | " cannot be used for vararg call arguments!", I); |
Duncan Sands | b99f44a | 2008-01-11 22:36:48 +0000 | [diff] [blame] | 1176 | } |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 1177 | |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1178 | // Verify that there's no metadata unless it's a direct call to an intrinsic. |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1179 | if (CS.getCalledFunction() == 0 || |
Chris Lattner | 609de00 | 2010-05-10 20:58:42 +0000 | [diff] [blame] | 1180 | !CS.getCalledFunction()->getName().startswith("llvm.")) { |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1181 | for (FunctionType::param_iterator PI = FTy->param_begin(), |
| 1182 | PE = FTy->param_end(); PI != PE; ++PI) |
Chris Lattner | b1ed91f | 2011-07-09 17:41:24 +0000 | [diff] [blame] | 1183 | Assert1(!(*PI)->isMetadataTy(), |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1184 | "Function has metadata parameter but isn't an intrinsic", I); |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 1187 | visitInstruction(*I); |
| 1188 | } |
| 1189 | |
| 1190 | void Verifier::visitCallInst(CallInst &CI) { |
| 1191 | VerifyCallSite(&CI); |
Chris Lattner | 7af3ee9 | 2002-07-18 00:13:42 +0000 | [diff] [blame] | 1192 | |
Dale Johannesen | b842d52 | 2009-02-05 01:49:45 +0000 | [diff] [blame] | 1193 | if (Function *F = CI.getCalledFunction()) |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1194 | if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID()) |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 1195 | visitIntrinsicFunctionCall(ID, CI); |
Duncan Sands | 8c58228 | 2007-12-21 19:19:01 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | void Verifier::visitInvokeInst(InvokeInst &II) { |
| 1199 | VerifyCallSite(&II); |
Bill Wendling | f4bbc04 | 2011-09-21 22:57:02 +0000 | [diff] [blame] | 1200 | |
| 1201 | // Verify that there is a landingpad instruction as the first non-PHI |
| 1202 | // instruction of the 'unwind' destination. |
| 1203 | Assert1(II.getUnwindDest()->isLandingPad(), |
| 1204 | "The unwind destination does not have a landingpad instruction!",&II); |
| 1205 | |
Dan Gohman | 9c6e188 | 2010-08-02 23:09:14 +0000 | [diff] [blame] | 1206 | visitTerminatorInst(II); |
Chris Lattner | 21ea83b | 2002-04-18 22:11:52 +0000 | [diff] [blame] | 1207 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1208 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1209 | /// visitBinaryOperator - Check that both arguments to the binary operator are |
| 1210 | /// of the same type! |
| 1211 | /// |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1212 | void Verifier::visitBinaryOperator(BinaryOperator &B) { |
Chris Lattner | 1f41925 | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 1213 | Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(), |
| 1214 | "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] | 1215 | |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1216 | switch (B.getOpcode()) { |
Dan Gohman | 5208dd8 | 2009-06-05 16:10:00 +0000 | [diff] [blame] | 1217 | // Check that integer arithmetic operators are only used with |
| 1218 | // integral operands. |
| 1219 | case Instruction::Add: |
| 1220 | case Instruction::Sub: |
| 1221 | case Instruction::Mul: |
| 1222 | case Instruction::SDiv: |
| 1223 | case Instruction::UDiv: |
| 1224 | case Instruction::SRem: |
| 1225 | case Instruction::URem: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1226 | Assert1(B.getType()->isIntOrIntVectorTy(), |
Dan Gohman | 5208dd8 | 2009-06-05 16:10:00 +0000 | [diff] [blame] | 1227 | "Integer arithmetic operators only work with integral types!", &B); |
| 1228 | Assert1(B.getType() == B.getOperand(0)->getType(), |
| 1229 | "Integer arithmetic operators must have same type " |
| 1230 | "for operands and result!", &B); |
| 1231 | break; |
| 1232 | // Check that floating-point arithmetic operators are only used with |
| 1233 | // floating-point operands. |
| 1234 | case Instruction::FAdd: |
| 1235 | case Instruction::FSub: |
| 1236 | case Instruction::FMul: |
| 1237 | case Instruction::FDiv: |
| 1238 | case Instruction::FRem: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1239 | Assert1(B.getType()->isFPOrFPVectorTy(), |
Dan Gohman | 5208dd8 | 2009-06-05 16:10:00 +0000 | [diff] [blame] | 1240 | "Floating-point arithmetic operators only work with " |
Dan Gohman | 2bde90b | 2009-06-05 18:34:16 +0000 | [diff] [blame] | 1241 | "floating-point types!", &B); |
Dan Gohman | 5208dd8 | 2009-06-05 16:10:00 +0000 | [diff] [blame] | 1242 | Assert1(B.getType() == B.getOperand(0)->getType(), |
| 1243 | "Floating-point arithmetic operators must have same type " |
| 1244 | "for operands and result!", &B); |
| 1245 | break; |
Chris Lattner | 1f41925 | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 1246 | // Check that logical operators are only used with integral operands. |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1247 | case Instruction::And: |
| 1248 | case Instruction::Or: |
| 1249 | case Instruction::Xor: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1250 | Assert1(B.getType()->isIntOrIntVectorTy(), |
Chris Lattner | 1f41925 | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 1251 | "Logical operators only work with integral types!", &B); |
| 1252 | Assert1(B.getType() == B.getOperand(0)->getType(), |
| 1253 | "Logical operators must have same type for operands and result!", |
| 1254 | &B); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1255 | break; |
| 1256 | case Instruction::Shl: |
| 1257 | case Instruction::LShr: |
| 1258 | case Instruction::AShr: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1259 | Assert1(B.getType()->isIntOrIntVectorTy(), |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1260 | "Shifts only work with integral types!", &B); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1261 | Assert1(B.getType() == B.getOperand(0)->getType(), |
| 1262 | "Shift return type must be same as operands!", &B); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1263 | break; |
Dan Gohman | 5208dd8 | 2009-06-05 16:10:00 +0000 | [diff] [blame] | 1264 | default: |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 1265 | llvm_unreachable("Unknown BinaryOperator opcode!"); |
Chris Lattner | 1f41925 | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 1266 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1267 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1268 | visitInstruction(B); |
| 1269 | } |
| 1270 | |
Nick Lewycky | c72d285 | 2010-08-22 23:45:14 +0000 | [diff] [blame] | 1271 | void Verifier::visitICmpInst(ICmpInst &IC) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1272 | // Check that the operands are the same type |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1273 | Type *Op0Ty = IC.getOperand(0)->getType(); |
| 1274 | Type *Op1Ty = IC.getOperand(1)->getType(); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1275 | Assert1(Op0Ty == Op1Ty, |
| 1276 | "Both operands to ICmp instruction are not of the same type!", &IC); |
| 1277 | // Check that the operands are the right type |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1278 | Assert1(Op0Ty->isIntOrIntVectorTy() || Op0Ty->getScalarType()->isPointerTy(), |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1279 | "Invalid operand types for ICmp instruction", &IC); |
Nick Lewycky | c72d285 | 2010-08-22 23:45:14 +0000 | [diff] [blame] | 1280 | // Check that the predicate is valid. |
| 1281 | Assert1(IC.getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE && |
| 1282 | IC.getPredicate() <= CmpInst::LAST_ICMP_PREDICATE, |
| 1283 | "Invalid predicate in ICmp instruction!", &IC); |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1284 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1285 | visitInstruction(IC); |
| 1286 | } |
| 1287 | |
Nick Lewycky | c72d285 | 2010-08-22 23:45:14 +0000 | [diff] [blame] | 1288 | void Verifier::visitFCmpInst(FCmpInst &FC) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1289 | // Check that the operands are the same type |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1290 | Type *Op0Ty = FC.getOperand(0)->getType(); |
| 1291 | Type *Op1Ty = FC.getOperand(1)->getType(); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1292 | Assert1(Op0Ty == Op1Ty, |
| 1293 | "Both operands to FCmp instruction are not of the same type!", &FC); |
| 1294 | // Check that the operands are the right type |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1295 | Assert1(Op0Ty->isFPOrFPVectorTy(), |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1296 | "Invalid operand types for FCmp instruction", &FC); |
Nick Lewycky | c72d285 | 2010-08-22 23:45:14 +0000 | [diff] [blame] | 1297 | // Check that the predicate is valid. |
| 1298 | Assert1(FC.getPredicate() >= CmpInst::FIRST_FCMP_PREDICATE && |
| 1299 | FC.getPredicate() <= CmpInst::LAST_FCMP_PREDICATE, |
| 1300 | "Invalid predicate in FCmp instruction!", &FC); |
| 1301 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 1302 | visitInstruction(FC); |
| 1303 | } |
| 1304 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1305 | void Verifier::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 38c4cb2 | 2006-04-08 04:07:52 +0000 | [diff] [blame] | 1306 | Assert1(ExtractElementInst::isValidOperands(EI.getOperand(0), |
| 1307 | EI.getOperand(1)), |
| 1308 | "Invalid extractelement operands!", &EI); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1309 | visitInstruction(EI); |
| 1310 | } |
| 1311 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1312 | void Verifier::visitInsertElementInst(InsertElementInst &IE) { |
Chris Lattner | 38c4cb2 | 2006-04-08 04:07:52 +0000 | [diff] [blame] | 1313 | Assert1(InsertElementInst::isValidOperands(IE.getOperand(0), |
| 1314 | IE.getOperand(1), |
| 1315 | IE.getOperand(2)), |
| 1316 | "Invalid insertelement operands!", &IE); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1317 | visitInstruction(IE); |
| 1318 | } |
| 1319 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1320 | void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) { |
| 1321 | Assert1(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1), |
| 1322 | SV.getOperand(2)), |
| 1323 | "Invalid shufflevector operands!", &SV); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1324 | visitInstruction(SV); |
| 1325 | } |
| 1326 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1327 | void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Duncan Sands | a71ae96 | 2012-02-03 17:28:51 +0000 | [diff] [blame] | 1328 | Type *TargetTy = GEP.getPointerOperandType()->getScalarType(); |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1329 | |
Duncan Sands | a71ae96 | 2012-02-03 17:28:51 +0000 | [diff] [blame] | 1330 | Assert1(isa<PointerType>(TargetTy), |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1331 | "GEP base pointer is not a vector or a vector of pointers", &GEP); |
| 1332 | Assert1(cast<PointerType>(TargetTy)->getElementType()->isSized(), |
Chris Lattner | 34a7db7 | 2011-07-29 20:32:28 +0000 | [diff] [blame] | 1333 | "GEP into unsized type!", &GEP); |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1334 | |
Chris Lattner | 84d82c7 | 2007-02-10 08:30:29 +0000 | [diff] [blame] | 1335 | SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end()); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1336 | Type *ElTy = |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1337 | GetElementPtrInst::getIndexedType(GEP.getPointerOperandType(), Idxs); |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1338 | Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP); |
Nadav Rotem | 3924cb0 | 2011-12-05 06:29:09 +0000 | [diff] [blame] | 1339 | |
| 1340 | if (GEP.getPointerOperandType()->isPointerTy()) { |
| 1341 | // Validate GEPs with scalar indices. |
| 1342 | Assert2(GEP.getType()->isPointerTy() && |
| 1343 | cast<PointerType>(GEP.getType())->getElementType() == ElTy, |
| 1344 | "GEP is not of right type for indices!", &GEP, ElTy); |
| 1345 | } else { |
| 1346 | // Validate GEPs with a vector index. |
| 1347 | Assert1(Idxs.size() == 1, "Invalid number of indices!", &GEP); |
| 1348 | Value *Index = Idxs[0]; |
| 1349 | Type *IndexTy = Index->getType(); |
| 1350 | Assert1(IndexTy->isVectorTy(), |
| 1351 | "Vector GEP must have vector indices!", &GEP); |
| 1352 | Assert1(GEP.getType()->isVectorTy(), |
| 1353 | "Vector GEP must return a vector value", &GEP); |
| 1354 | Type *ElemPtr = cast<VectorType>(GEP.getType())->getElementType(); |
| 1355 | Assert1(ElemPtr->isPointerTy(), |
| 1356 | "Vector GEP pointer operand is not a pointer!", &GEP); |
| 1357 | unsigned IndexWidth = cast<VectorType>(IndexTy)->getNumElements(); |
| 1358 | unsigned GepWidth = cast<VectorType>(GEP.getType())->getNumElements(); |
| 1359 | Assert1(IndexWidth == GepWidth, "Invalid GEP index vector width", &GEP); |
| 1360 | Assert1(ElTy == cast<PointerType>(ElemPtr)->getElementType(), |
| 1361 | "Vector GEP type does not match pointer type!", &GEP); |
| 1362 | } |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 1363 | visitInstruction(GEP); |
| 1364 | } |
| 1365 | |
Rafael Espindola | e3c5f3e | 2012-05-31 16:04:26 +0000 | [diff] [blame] | 1366 | static bool isContiguous(const ConstantRange &A, const ConstantRange &B) { |
| 1367 | return A.getUpper() == B.getLower() || A.getLower() == B.getUpper(); |
| 1368 | } |
| 1369 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1370 | void Verifier::visitLoadInst(LoadInst &LI) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1371 | PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType()); |
Nick Lewycky | b2b0467 | 2009-09-08 01:23:52 +0000 | [diff] [blame] | 1372 | Assert1(PTy, "Load operand must be a pointer.", &LI); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1373 | Type *ElTy = PTy->getElementType(); |
Nick Lewycky | 984161a | 2009-09-08 02:02:39 +0000 | [diff] [blame] | 1374 | Assert2(ElTy == LI.getType(), |
| 1375 | "Load result type does not match pointer operand type!", &LI, ElTy); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1376 | if (LI.isAtomic()) { |
| 1377 | Assert1(LI.getOrdering() != Release && LI.getOrdering() != AcquireRelease, |
| 1378 | "Load cannot have Release ordering", &LI); |
| 1379 | Assert1(LI.getAlignment() != 0, |
| 1380 | "Atomic load must specify explicit alignment", &LI); |
Eli Friedman | 79a6b30 | 2012-08-17 23:24:29 +0000 | [diff] [blame] | 1381 | if (!ElTy->isPointerTy()) { |
| 1382 | Assert2(ElTy->isIntegerTy(), |
| 1383 | "atomic store operand must have integer type!", |
| 1384 | &LI, ElTy); |
| 1385 | unsigned Size = ElTy->getPrimitiveSizeInBits(); |
| 1386 | Assert2(Size >= 8 && !(Size & (Size - 1)), |
| 1387 | "atomic store operand must be power-of-two byte-sized integer", |
| 1388 | &LI, ElTy); |
| 1389 | } |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1390 | } else { |
| 1391 | Assert1(LI.getSynchScope() == CrossThread, |
| 1392 | "Non-atomic load cannot have SynchronizationScope specified", &LI); |
| 1393 | } |
Rafael Espindola | ef9f550 | 2012-03-24 00:14:51 +0000 | [diff] [blame] | 1394 | |
| 1395 | if (MDNode *Range = LI.getMetadata(LLVMContext::MD_range)) { |
| 1396 | unsigned NumOperands = Range->getNumOperands(); |
| 1397 | Assert1(NumOperands % 2 == 0, "Unfinished range!", Range); |
| 1398 | unsigned NumRanges = NumOperands / 2; |
| 1399 | Assert1(NumRanges >= 1, "It should have at least one range!", Range); |
Rafael Espindola | 97d7787 | 2012-05-31 13:45:46 +0000 | [diff] [blame] | 1400 | |
Rafael Espindola | e3c5f3e | 2012-05-31 16:04:26 +0000 | [diff] [blame] | 1401 | ConstantRange LastRange(1); // Dummy initial value |
Rafael Espindola | ef9f550 | 2012-03-24 00:14:51 +0000 | [diff] [blame] | 1402 | for (unsigned i = 0; i < NumRanges; ++i) { |
| 1403 | ConstantInt *Low = dyn_cast<ConstantInt>(Range->getOperand(2*i)); |
| 1404 | Assert1(Low, "The lower limit must be an integer!", Low); |
| 1405 | ConstantInt *High = dyn_cast<ConstantInt>(Range->getOperand(2*i + 1)); |
| 1406 | Assert1(High, "The upper limit must be an integer!", High); |
| 1407 | Assert1(High->getType() == Low->getType() && |
| 1408 | High->getType() == ElTy, "Range types must match load type!", |
| 1409 | &LI); |
Rafael Espindola | 97d7787 | 2012-05-31 13:45:46 +0000 | [diff] [blame] | 1410 | |
| 1411 | APInt HighV = High->getValue(); |
| 1412 | APInt LowV = Low->getValue(); |
Rafael Espindola | e3c5f3e | 2012-05-31 16:04:26 +0000 | [diff] [blame] | 1413 | ConstantRange CurRange(LowV, HighV); |
| 1414 | Assert1(!CurRange.isEmptySet() && !CurRange.isFullSet(), |
| 1415 | "Range must not be empty!", Range); |
Rafael Espindola | 97d7787 | 2012-05-31 13:45:46 +0000 | [diff] [blame] | 1416 | if (i != 0) { |
Rafael Espindola | e3c5f3e | 2012-05-31 16:04:26 +0000 | [diff] [blame] | 1417 | Assert1(CurRange.intersectWith(LastRange).isEmptySet(), |
| 1418 | "Intervals are overlapping", Range); |
| 1419 | Assert1(LowV.sgt(LastRange.getLower()), "Intervals are not in order", |
| 1420 | Range); |
| 1421 | Assert1(!isContiguous(CurRange, LastRange), "Intervals are contiguous", |
| 1422 | Range); |
Rafael Espindola | 97d7787 | 2012-05-31 13:45:46 +0000 | [diff] [blame] | 1423 | } |
Rafael Espindola | e3c5f3e | 2012-05-31 16:04:26 +0000 | [diff] [blame] | 1424 | LastRange = ConstantRange(LowV, HighV); |
Rafael Espindola | ef9f550 | 2012-03-24 00:14:51 +0000 | [diff] [blame] | 1425 | } |
Rafael Espindola | e3c5f3e | 2012-05-31 16:04:26 +0000 | [diff] [blame] | 1426 | if (NumRanges > 2) { |
| 1427 | APInt FirstLow = |
| 1428 | dyn_cast<ConstantInt>(Range->getOperand(0))->getValue(); |
| 1429 | APInt FirstHigh = |
| 1430 | dyn_cast<ConstantInt>(Range->getOperand(1))->getValue(); |
| 1431 | ConstantRange FirstRange(FirstLow, FirstHigh); |
| 1432 | Assert1(FirstRange.intersectWith(LastRange).isEmptySet(), |
| 1433 | "Intervals are overlapping", Range); |
| 1434 | Assert1(!isContiguous(FirstRange, LastRange), "Intervals are contiguous", |
| 1435 | Range); |
| 1436 | } |
| 1437 | |
| 1438 | |
Rafael Espindola | ef9f550 | 2012-03-24 00:14:51 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 1441 | visitInstruction(LI); |
| 1442 | } |
| 1443 | |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1444 | void Verifier::visitStoreInst(StoreInst &SI) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1445 | PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType()); |
Chris Lattner | 31bd2de | 2010-07-11 19:42:53 +0000 | [diff] [blame] | 1446 | Assert1(PTy, "Store operand must be a pointer.", &SI); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1447 | Type *ElTy = PTy->getElementType(); |
Nick Lewycky | 984161a | 2009-09-08 02:02:39 +0000 | [diff] [blame] | 1448 | Assert2(ElTy == SI.getOperand(0)->getType(), |
| 1449 | "Stored value type does not match pointer operand type!", |
| 1450 | &SI, ElTy); |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1451 | if (SI.isAtomic()) { |
| 1452 | Assert1(SI.getOrdering() != Acquire && SI.getOrdering() != AcquireRelease, |
| 1453 | "Store cannot have Acquire ordering", &SI); |
| 1454 | Assert1(SI.getAlignment() != 0, |
| 1455 | "Atomic store must specify explicit alignment", &SI); |
Eli Friedman | 79a6b30 | 2012-08-17 23:24:29 +0000 | [diff] [blame] | 1456 | if (!ElTy->isPointerTy()) { |
| 1457 | Assert2(ElTy->isIntegerTy(), |
| 1458 | "atomic store operand must have integer type!", |
| 1459 | &SI, ElTy); |
| 1460 | unsigned Size = ElTy->getPrimitiveSizeInBits(); |
| 1461 | Assert2(Size >= 8 && !(Size & (Size - 1)), |
| 1462 | "atomic store operand must be power-of-two byte-sized integer", |
| 1463 | &SI, ElTy); |
| 1464 | } |
Eli Friedman | 59b6688 | 2011-08-09 23:02:53 +0000 | [diff] [blame] | 1465 | } else { |
| 1466 | Assert1(SI.getSynchScope() == CrossThread, |
| 1467 | "Non-atomic store cannot have SynchronizationScope specified", &SI); |
| 1468 | } |
Chris Lattner | d46bb6e | 2002-04-24 19:12:21 +0000 | [diff] [blame] | 1469 | visitInstruction(SI); |
| 1470 | } |
| 1471 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 1472 | void Verifier::visitAllocaInst(AllocaInst &AI) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1473 | PointerType *PTy = AI.getType(); |
Chris Lattner | ffe0da0 | 2008-03-01 09:01:57 +0000 | [diff] [blame] | 1474 | Assert1(PTy->getAddressSpace() == 0, |
| 1475 | "Allocation instruction pointer not in the generic address space!", |
| 1476 | &AI); |
| 1477 | Assert1(PTy->getElementType()->isSized(), "Cannot allocate unsized type", |
| 1478 | &AI); |
Dan Gohman | 2140a74 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 1479 | Assert1(AI.getArraySize()->getType()->isIntegerTy(), |
| 1480 | "Alloca array size must have integer type", &AI); |
Christopher Lamb | 55c6d4f | 2007-12-17 01:00:21 +0000 | [diff] [blame] | 1481 | visitInstruction(AI); |
| 1482 | } |
| 1483 | |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1484 | void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) { |
| 1485 | Assert1(CXI.getOrdering() != NotAtomic, |
| 1486 | "cmpxchg instructions must be atomic.", &CXI); |
| 1487 | Assert1(CXI.getOrdering() != Unordered, |
| 1488 | "cmpxchg instructions cannot be unordered.", &CXI); |
| 1489 | PointerType *PTy = dyn_cast<PointerType>(CXI.getOperand(0)->getType()); |
| 1490 | Assert1(PTy, "First cmpxchg operand must be a pointer.", &CXI); |
| 1491 | Type *ElTy = PTy->getElementType(); |
Eli Friedman | 79a6b30 | 2012-08-17 23:24:29 +0000 | [diff] [blame] | 1492 | Assert2(ElTy->isIntegerTy(), |
| 1493 | "cmpxchg operand must have integer type!", |
| 1494 | &CXI, ElTy); |
| 1495 | unsigned Size = ElTy->getPrimitiveSizeInBits(); |
| 1496 | Assert2(Size >= 8 && !(Size & (Size - 1)), |
| 1497 | "cmpxchg operand must be power-of-two byte-sized integer", |
| 1498 | &CXI, ElTy); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1499 | Assert2(ElTy == CXI.getOperand(1)->getType(), |
| 1500 | "Expected value type does not match pointer operand type!", |
| 1501 | &CXI, ElTy); |
| 1502 | Assert2(ElTy == CXI.getOperand(2)->getType(), |
| 1503 | "Stored value type does not match pointer operand type!", |
| 1504 | &CXI, ElTy); |
| 1505 | visitInstruction(CXI); |
| 1506 | } |
| 1507 | |
| 1508 | void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) { |
| 1509 | Assert1(RMWI.getOrdering() != NotAtomic, |
| 1510 | "atomicrmw instructions must be atomic.", &RMWI); |
| 1511 | Assert1(RMWI.getOrdering() != Unordered, |
| 1512 | "atomicrmw instructions cannot be unordered.", &RMWI); |
| 1513 | PointerType *PTy = dyn_cast<PointerType>(RMWI.getOperand(0)->getType()); |
| 1514 | Assert1(PTy, "First atomicrmw operand must be a pointer.", &RMWI); |
| 1515 | Type *ElTy = PTy->getElementType(); |
Eli Friedman | 79a6b30 | 2012-08-17 23:24:29 +0000 | [diff] [blame] | 1516 | Assert2(ElTy->isIntegerTy(), |
| 1517 | "atomicrmw operand must have integer type!", |
| 1518 | &RMWI, ElTy); |
| 1519 | unsigned Size = ElTy->getPrimitiveSizeInBits(); |
| 1520 | Assert2(Size >= 8 && !(Size & (Size - 1)), |
| 1521 | "atomicrmw operand must be power-of-two byte-sized integer", |
| 1522 | &RMWI, ElTy); |
Eli Friedman | c9a551e | 2011-07-28 21:48:00 +0000 | [diff] [blame] | 1523 | Assert2(ElTy == RMWI.getOperand(1)->getType(), |
| 1524 | "Argument value type does not match pointer operand type!", |
| 1525 | &RMWI, ElTy); |
| 1526 | Assert1(AtomicRMWInst::FIRST_BINOP <= RMWI.getOperation() && |
| 1527 | RMWI.getOperation() <= AtomicRMWInst::LAST_BINOP, |
| 1528 | "Invalid binary operation!", &RMWI); |
| 1529 | visitInstruction(RMWI); |
| 1530 | } |
| 1531 | |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1532 | void Verifier::visitFenceInst(FenceInst &FI) { |
| 1533 | const AtomicOrdering Ordering = FI.getOrdering(); |
| 1534 | Assert1(Ordering == Acquire || Ordering == Release || |
| 1535 | Ordering == AcquireRelease || Ordering == SequentiallyConsistent, |
| 1536 | "fence instructions may only have " |
Bill Wendling | e632cb3 | 2011-08-08 08:02:48 +0000 | [diff] [blame] | 1537 | "acquire, release, acq_rel, or seq_cst ordering.", &FI); |
Eli Friedman | fee02c6 | 2011-07-25 23:16:38 +0000 | [diff] [blame] | 1538 | visitInstruction(FI); |
| 1539 | } |
| 1540 | |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1541 | void Verifier::visitExtractValueInst(ExtractValueInst &EVI) { |
| 1542 | Assert1(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(), |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1543 | EVI.getIndices()) == |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1544 | EVI.getType(), |
| 1545 | "Invalid ExtractValueInst operands!", &EVI); |
Chris Lattner | 31abd54 | 2008-04-23 04:06:15 +0000 | [diff] [blame] | 1546 | |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1547 | visitInstruction(EVI); |
Devang Patel | 295711f | 2008-02-19 22:15:16 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1550 | void Verifier::visitInsertValueInst(InsertValueInst &IVI) { |
| 1551 | Assert1(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(), |
Jay Foad | 57aa636 | 2011-07-13 10:26:04 +0000 | [diff] [blame] | 1552 | IVI.getIndices()) == |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1553 | IVI.getOperand(1)->getType(), |
| 1554 | "Invalid InsertValueInst operands!", &IVI); |
| 1555 | |
| 1556 | visitInstruction(IVI); |
| 1557 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1558 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 1559 | void Verifier::visitLandingPadInst(LandingPadInst &LPI) { |
| 1560 | BasicBlock *BB = LPI.getParent(); |
| 1561 | |
| 1562 | // The landingpad instruction is ill-formed if it doesn't have any clauses and |
| 1563 | // isn't a cleanup. |
| 1564 | Assert1(LPI.getNumClauses() > 0 || LPI.isCleanup(), |
| 1565 | "LandingPadInst needs at least one clause or to be a cleanup.", &LPI); |
| 1566 | |
| 1567 | // The landingpad instruction defines its parent as a landing pad block. The |
| 1568 | // landing pad block may be branched to only by the unwind edge of an invoke. |
| 1569 | for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) { |
| 1570 | const InvokeInst *II = dyn_cast<InvokeInst>((*I)->getTerminator()); |
Eli Friedman | 4c923b3 | 2012-08-10 20:55:20 +0000 | [diff] [blame] | 1571 | Assert1(II && II->getUnwindDest() == BB && II->getNormalDest() != BB, |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 1572 | "Block containing LandingPadInst must be jumped to " |
| 1573 | "only by the unwind edge of an invoke.", &LPI); |
| 1574 | } |
| 1575 | |
| 1576 | // The landingpad instruction must be the first non-PHI instruction in the |
| 1577 | // block. |
| 1578 | Assert1(LPI.getParent()->getLandingPadInst() == &LPI, |
| 1579 | "LandingPadInst not the first non-PHI instruction in the block.", |
| 1580 | &LPI); |
| 1581 | |
| 1582 | // The personality functions for all landingpad instructions within the same |
| 1583 | // function should match. |
| 1584 | if (PersonalityFn) |
| 1585 | Assert1(LPI.getPersonalityFn() == PersonalityFn, |
| 1586 | "Personality function doesn't match others in function", &LPI); |
| 1587 | PersonalityFn = LPI.getPersonalityFn(); |
| 1588 | |
Duncan Sands | 86de1a6 | 2011-09-27 16:43:19 +0000 | [diff] [blame] | 1589 | // All operands must be constants. |
| 1590 | Assert1(isa<Constant>(PersonalityFn), "Personality function is not constant!", |
| 1591 | &LPI); |
| 1592 | for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) { |
| 1593 | Value *Clause = LPI.getClause(i); |
| 1594 | Assert1(isa<Constant>(Clause), "Clause is not constant!", &LPI); |
Duncan Sands | 68ba813 | 2011-09-27 19:34:22 +0000 | [diff] [blame] | 1595 | if (LPI.isCatch(i)) { |
| 1596 | Assert1(isa<PointerType>(Clause->getType()), |
| 1597 | "Catch operand does not have pointer type!", &LPI); |
| 1598 | } else { |
| 1599 | Assert1(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI); |
Duncan Sands | 86de1a6 | 2011-09-27 16:43:19 +0000 | [diff] [blame] | 1600 | Assert1(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause), |
Duncan Sands | 68ba813 | 2011-09-27 19:34:22 +0000 | [diff] [blame] | 1601 | "Filter operand is not an array of constants!", &LPI); |
| 1602 | } |
Duncan Sands | 86de1a6 | 2011-09-27 16:43:19 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Bill Wendling | fae1475 | 2011-08-12 20:24:12 +0000 | [diff] [blame] | 1605 | visitInstruction(LPI); |
| 1606 | } |
| 1607 | |
Rafael Espindola | 654320a | 2012-02-26 02:23:37 +0000 | [diff] [blame] | 1608 | void Verifier::verifyDominatesUse(Instruction &I, unsigned i) { |
| 1609 | Instruction *Op = cast<Instruction>(I.getOperand(i)); |
Rafael Espindola | 9a16735 | 2012-08-17 18:21:28 +0000 | [diff] [blame] | 1610 | // If the we have an invalid invoke, don't try to compute the dominance. |
| 1611 | // We already reject it in the invoke specific checks and the dominance |
| 1612 | // computation doesn't handle multiple edges. |
| 1613 | if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) { |
| 1614 | if (II->getNormalDest() == II->getUnwindDest()) |
| 1615 | return; |
| 1616 | } |
Rafael Espindola | 654320a | 2012-02-26 02:23:37 +0000 | [diff] [blame] | 1617 | |
Rafael Espindola | 103c2cf | 2012-06-01 21:56:26 +0000 | [diff] [blame] | 1618 | const Use &U = I.getOperandUse(i); |
| 1619 | Assert2(InstsInThisBlock.count(Op) || DT->dominates(Op, U), |
Rafael Espindola | 654320a | 2012-02-26 02:23:37 +0000 | [diff] [blame] | 1620 | "Instruction does not dominate all uses!", Op, &I); |
| 1621 | } |
| 1622 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1623 | /// verifyInstruction - Verify that an instruction is well formed. |
| 1624 | /// |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1625 | void Verifier::visitInstruction(Instruction &I) { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1626 | BasicBlock *BB = I.getParent(); |
Chris Lattner | 1f41925 | 2002-09-09 20:26:04 +0000 | [diff] [blame] | 1627 | Assert1(BB, "Instruction not embedded in basic block!", &I); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1628 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1629 | if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential |
| 1630 | for (Value::use_iterator UI = I.use_begin(), UE = I.use_end(); |
| 1631 | UI != UE; ++UI) |
Duncan Sands | 15de591 | 2009-05-29 19:39:36 +0000 | [diff] [blame] | 1632 | Assert1(*UI != (User*)&I || !DT->isReachableFromEntry(BB), |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1633 | "Only PHI nodes may reference their own value!", &I); |
| 1634 | } |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 1635 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1636 | // Check that void typed values don't have names |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1637 | Assert1(!I.getType()->isVoidTy() || !I.hasName(), |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1638 | "Instruction has a name, but provides a void value!", &I); |
| 1639 | |
Chris Lattner | 5f126b7 | 2004-03-29 00:29:36 +0000 | [diff] [blame] | 1640 | // Check that the return value of the instruction is either void or a legal |
| 1641 | // value type. |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1642 | Assert1(I.getType()->isVoidTy() || |
Nick Lewycky | 93e06a5 | 2009-09-27 23:27:42 +0000 | [diff] [blame] | 1643 | I.getType()->isFirstClassType(), |
Chris Lattner | 5f126b7 | 2004-03-29 00:29:36 +0000 | [diff] [blame] | 1644 | "Instruction returns a non-scalar type!", &I); |
| 1645 | |
Nick Lewycky | 93e06a5 | 2009-09-27 23:27:42 +0000 | [diff] [blame] | 1646 | // Check that the instruction doesn't produce metadata. Calls are already |
| 1647 | // checked against the callee type. |
Chris Lattner | fdd8790 | 2009-10-05 05:54:46 +0000 | [diff] [blame] | 1648 | Assert1(!I.getType()->isMetadataTy() || |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1649 | isa<CallInst>(I) || isa<InvokeInst>(I), |
| 1650 | "Invalid use of metadata!", &I); |
| 1651 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1652 | // Check that all uses of the instruction, if they are instructions |
| 1653 | // themselves, actually have parent basic blocks. If the use is not an |
| 1654 | // instruction, it is an error! |
Chris Lattner | 069a795 | 2002-06-25 15:56:27 +0000 | [diff] [blame] | 1655 | for (User::use_iterator UI = I.use_begin(), UE = I.use_end(); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1656 | UI != UE; ++UI) { |
Nick Lewycky | b2b0467 | 2009-09-08 01:23:52 +0000 | [diff] [blame] | 1657 | if (Instruction *Used = dyn_cast<Instruction>(*UI)) |
| 1658 | Assert2(Used->getParent() != 0, "Instruction referencing instruction not" |
| 1659 | " embedded in a basic block!", &I, Used); |
Nick Lewycky | 984161a | 2009-09-08 02:02:39 +0000 | [diff] [blame] | 1660 | else { |
Nick Lewycky | b2b0467 | 2009-09-08 01:23:52 +0000 | [diff] [blame] | 1661 | CheckFailed("Use of instruction is not an instruction!", *UI); |
Nick Lewycky | 984161a | 2009-09-08 02:02:39 +0000 | [diff] [blame] | 1662 | return; |
| 1663 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1664 | } |
| 1665 | |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1666 | for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) { |
Chris Lattner | 08f7d0c | 2005-02-24 16:58:29 +0000 | [diff] [blame] | 1667 | Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I); |
Chris Lattner | b7e1ef5 | 2006-07-11 20:29:49 +0000 | [diff] [blame] | 1668 | |
| 1669 | // Check to make sure that only first-class-values are operands to |
| 1670 | // instructions. |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 1671 | if (!I.getOperand(i)->getType()->isFirstClassType()) { |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 1672 | Assert1(0, "Instruction operands must be first-class values!", &I); |
Devang Patel | 1f00b53 | 2008-02-21 01:54:02 +0000 | [diff] [blame] | 1673 | } |
Nick Lewycky | adbc284 | 2009-05-30 05:06:04 +0000 | [diff] [blame] | 1674 | |
Chris Lattner | 9ece94b | 2004-03-14 03:23:54 +0000 | [diff] [blame] | 1675 | if (Function *F = dyn_cast<Function>(I.getOperand(i))) { |
Chris Lattner | b7e1ef5 | 2006-07-11 20:29:49 +0000 | [diff] [blame] | 1676 | // Check to make sure that the "address of" an intrinsic function is never |
| 1677 | // taken. |
Nuno Lopes | 2f49284 | 2012-06-28 22:57:00 +0000 | [diff] [blame] | 1678 | Assert1(!F->isIntrinsic() || i == (isa<CallInst>(I) ? e-1 : 0), |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 1679 | "Cannot take the address of an intrinsic!", &I); |
Nuno Lopes | 2f49284 | 2012-06-28 22:57:00 +0000 | [diff] [blame] | 1680 | Assert1(!F->isIntrinsic() || isa<CallInst>(I) || |
| 1681 | F->getIntrinsicID() == Intrinsic::donothing, |
| 1682 | "Cannot invoke an intrinsinc other than donothing", &I); |
Chris Lattner | 4ff0452 | 2007-04-20 21:48:08 +0000 | [diff] [blame] | 1683 | Assert1(F->getParent() == Mod, "Referencing function in another module!", |
| 1684 | &I); |
Chris Lattner | 9ece94b | 2004-03-14 03:23:54 +0000 | [diff] [blame] | 1685 | } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) { |
| 1686 | Assert1(OpBB->getParent() == BB->getParent(), |
| 1687 | "Referring to a basic block in another function!", &I); |
| 1688 | } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) { |
| 1689 | Assert1(OpArg->getParent() == BB->getParent(), |
| 1690 | "Referring to an argument in another function!", &I); |
Chris Lattner | 4ff0452 | 2007-04-20 21:48:08 +0000 | [diff] [blame] | 1691 | } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) { |
| 1692 | Assert1(GV->getParent() == Mod, "Referencing global in another module!", |
| 1693 | &I); |
Rafael Espindola | 654320a | 2012-02-26 02:23:37 +0000 | [diff] [blame] | 1694 | } else if (isa<Instruction>(I.getOperand(i))) { |
| 1695 | verifyDominatesUse(I, i); |
Chris Lattner | 41eb5cd | 2006-01-26 00:08:45 +0000 | [diff] [blame] | 1696 | } else if (isa<InlineAsm>(I.getOperand(i))) { |
Gabor Greif | 03e7e68 | 2010-07-13 15:31:36 +0000 | [diff] [blame] | 1697 | Assert1((i + 1 == e && isa<CallInst>(I)) || |
| 1698 | (i + 3 == e && isa<InvokeInst>(I)), |
Chris Lattner | 41eb5cd | 2006-01-26 00:08:45 +0000 | [diff] [blame] | 1699 | "Cannot take the address of an inline asm!", &I); |
Chris Lattner | df9779c | 2003-10-05 17:44:18 +0000 | [diff] [blame] | 1700 | } |
| 1701 | } |
Rafael Espindola | ef9f550 | 2012-03-24 00:14:51 +0000 | [diff] [blame] | 1702 | |
Duncan Sands | 34bd91a | 2012-04-14 12:36:06 +0000 | [diff] [blame] | 1703 | if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) { |
Duncan Sands | af06b26 | 2012-04-10 08:22:43 +0000 | [diff] [blame] | 1704 | Assert1(I.getType()->isFPOrFPVectorTy(), |
Duncan Sands | 34bd91a | 2012-04-14 12:36:06 +0000 | [diff] [blame] | 1705 | "fpmath requires a floating point result!", &I); |
| 1706 | Assert1(MD->getNumOperands() == 1, "fpmath takes one operand!", &I); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 1707 | Value *Op0 = MD->getOperand(0); |
| 1708 | if (ConstantFP *CFP0 = dyn_cast_or_null<ConstantFP>(Op0)) { |
| 1709 | APFloat Accuracy = CFP0->getValueAPF(); |
| 1710 | Assert1(Accuracy.isNormal() && !Accuracy.isNegative(), |
| 1711 | "fpmath accuracy not a positive number!", &I); |
Duncan Sands | 05f4df8 | 2012-04-16 16:28:59 +0000 | [diff] [blame] | 1712 | } else { |
| 1713 | Assert1(false, "invalid fpmath accuracy!", &I); |
| 1714 | } |
Duncan Sands | af06b26 | 2012-04-10 08:22:43 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Rafael Espindola | ef9f550 | 2012-03-24 00:14:51 +0000 | [diff] [blame] | 1717 | MDNode *MD = I.getMetadata(LLVMContext::MD_range); |
| 1718 | Assert1(!MD || isa<LoadInst>(I), "Ranges are only for loads!", &I); |
| 1719 | |
Chris Lattner | c9e79d0 | 2004-09-29 20:07:45 +0000 | [diff] [blame] | 1720 | InstsInThisBlock.insert(&I); |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Chris Lattner | 144b619 | 2012-05-27 19:37:05 +0000 | [diff] [blame] | 1723 | /// VerifyIntrinsicType - Verify that the specified type (which comes from an |
| 1724 | /// intrinsic argument or return value) matches the type constraints specified |
| 1725 | /// by the .td file (e.g. an "any integer" argument really is an integer). |
| 1726 | /// |
| 1727 | /// This return true on error but does not print a message. |
| 1728 | bool Verifier::VerifyIntrinsicType(Type *Ty, |
| 1729 | ArrayRef<Intrinsic::IITDescriptor> &Infos, |
| 1730 | SmallVectorImpl<Type*> &ArgTys) { |
| 1731 | using namespace Intrinsic; |
| 1732 | |
| 1733 | // If we ran out of descriptors, there are too many arguments. |
| 1734 | if (Infos.empty()) return true; |
| 1735 | IITDescriptor D = Infos.front(); |
| 1736 | Infos = Infos.slice(1); |
| 1737 | |
| 1738 | switch (D.Kind) { |
| 1739 | case IITDescriptor::Void: return !Ty->isVoidTy(); |
| 1740 | case IITDescriptor::MMX: return !Ty->isX86_MMXTy(); |
| 1741 | case IITDescriptor::Metadata: return !Ty->isMetadataTy(); |
| 1742 | case IITDescriptor::Float: return !Ty->isFloatTy(); |
| 1743 | case IITDescriptor::Double: return !Ty->isDoubleTy(); |
| 1744 | case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width); |
| 1745 | case IITDescriptor::Vector: { |
| 1746 | VectorType *VT = dyn_cast<VectorType>(Ty); |
| 1747 | return VT == 0 || VT->getNumElements() != D.Vector_Width || |
| 1748 | VerifyIntrinsicType(VT->getElementType(), Infos, ArgTys); |
| 1749 | } |
| 1750 | case IITDescriptor::Pointer: { |
| 1751 | PointerType *PT = dyn_cast<PointerType>(Ty); |
| 1752 | return PT == 0 || PT->getAddressSpace() != D.Pointer_AddressSpace || |
| 1753 | VerifyIntrinsicType(PT->getElementType(), Infos, ArgTys); |
| 1754 | } |
| 1755 | |
| 1756 | case IITDescriptor::Struct: { |
| 1757 | StructType *ST = dyn_cast<StructType>(Ty); |
| 1758 | if (ST == 0 || ST->getNumElements() != D.Struct_NumElements) |
| 1759 | return true; |
| 1760 | |
| 1761 | for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i) |
| 1762 | if (VerifyIntrinsicType(ST->getElementType(i), Infos, ArgTys)) |
| 1763 | return true; |
| 1764 | return false; |
| 1765 | } |
| 1766 | |
| 1767 | case IITDescriptor::Argument: |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 1768 | // Two cases here - If this is the second occurrence of an argument, verify |
Chris Lattner | 144b619 | 2012-05-27 19:37:05 +0000 | [diff] [blame] | 1769 | // that the later instance matches the previous instance. |
| 1770 | if (D.getArgumentNumber() < ArgTys.size()) |
| 1771 | return Ty != ArgTys[D.getArgumentNumber()]; |
| 1772 | |
| 1773 | // Otherwise, if this is the first instance of an argument, record it and |
| 1774 | // verify the "Any" kind. |
| 1775 | assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error"); |
| 1776 | ArgTys.push_back(Ty); |
| 1777 | |
| 1778 | switch (D.getArgumentKind()) { |
| 1779 | case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy(); |
| 1780 | case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy(); |
| 1781 | case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty); |
| 1782 | case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty); |
| 1783 | } |
| 1784 | llvm_unreachable("all argument kinds not covered"); |
| 1785 | |
| 1786 | case IITDescriptor::ExtendVecArgument: |
| 1787 | // This may only be used when referring to a previous vector argument. |
| 1788 | return D.getArgumentNumber() >= ArgTys.size() || |
| 1789 | !isa<VectorType>(ArgTys[D.getArgumentNumber()]) || |
| 1790 | VectorType::getExtendedElementVectorType( |
| 1791 | cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty; |
| 1792 | |
| 1793 | case IITDescriptor::TruncVecArgument: |
| 1794 | // This may only be used when referring to a previous vector argument. |
| 1795 | return D.getArgumentNumber() >= ArgTys.size() || |
| 1796 | !isa<VectorType>(ArgTys[D.getArgumentNumber()]) || |
| 1797 | VectorType::getTruncatedElementVectorType( |
| 1798 | cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty; |
| 1799 | } |
| 1800 | llvm_unreachable("unhandled"); |
| 1801 | } |
Bob Wilson | f76486a | 2009-01-07 00:09:01 +0000 | [diff] [blame] | 1802 | |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 1803 | /// visitIntrinsicFunction - Allow intrinsics to be verified in different ways. |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1804 | /// |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 1805 | void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) { |
Chris Lattner | bb346d0 | 2003-05-08 03:47:33 +0000 | [diff] [blame] | 1806 | Function *IF = CI.getCalledFunction(); |
Chris Lattner | 4ff0452 | 2007-04-20 21:48:08 +0000 | [diff] [blame] | 1807 | Assert1(IF->isDeclaration(), "Intrinsic functions should never be defined!", |
| 1808 | IF); |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 1809 | |
Chris Lattner | 144b619 | 2012-05-27 19:37:05 +0000 | [diff] [blame] | 1810 | // Verify that the intrinsic prototype lines up with what the .td files |
| 1811 | // describe. |
| 1812 | FunctionType *IFTy = IF->getFunctionType(); |
| 1813 | Assert1(!IFTy->isVarArg(), "Intrinsic prototypes are not varargs", IF); |
| 1814 | |
| 1815 | SmallVector<Intrinsic::IITDescriptor, 8> Table; |
| 1816 | getIntrinsicInfoTableEntries(ID, Table); |
| 1817 | ArrayRef<Intrinsic::IITDescriptor> TableRef = Table; |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 1818 | |
Chris Lattner | 144b619 | 2012-05-27 19:37:05 +0000 | [diff] [blame] | 1819 | SmallVector<Type *, 4> ArgTys; |
| 1820 | Assert1(!VerifyIntrinsicType(IFTy->getReturnType(), TableRef, ArgTys), |
| 1821 | "Intrinsic has incorrect return type!", IF); |
| 1822 | for (unsigned i = 0, e = IFTy->getNumParams(); i != e; ++i) |
| 1823 | Assert1(!VerifyIntrinsicType(IFTy->getParamType(i), TableRef, ArgTys), |
| 1824 | "Intrinsic has incorrect argument type!", IF); |
| 1825 | Assert1(TableRef.empty(), "Intrinsic has too few arguments!", IF); |
| 1826 | |
| 1827 | // Now that we have the intrinsic ID and the actual argument types (and we |
| 1828 | // know they are legal for the intrinsic!) get the intrinsic name through the |
| 1829 | // usual means. This allows us to verify the mangling of argument types into |
| 1830 | // the name. |
| 1831 | Assert1(Intrinsic::getName(ID, ArgTys) == IF->getName(), |
| 1832 | "Intrinsic name not mangled correctly for type arguments!", IF); |
| 1833 | |
Chris Lattner | 588096e | 2009-12-28 09:07:21 +0000 | [diff] [blame] | 1834 | // If the intrinsic takes MDNode arguments, verify that they are either global |
| 1835 | // or are local to *this* function. |
Bill Wendling | cfcd0e1 | 2010-06-07 19:18:58 +0000 | [diff] [blame] | 1836 | for (unsigned i = 0, e = CI.getNumArgOperands(); i != e; ++i) |
| 1837 | if (MDNode *MD = dyn_cast<MDNode>(CI.getArgOperand(i))) |
Duncan Sands | 76d6217 | 2010-04-29 16:10:30 +0000 | [diff] [blame] | 1838 | visitMDNode(*MD, CI.getParent()->getParent()); |
Victor Hernandez | 0471abd | 2009-12-18 20:09:14 +0000 | [diff] [blame] | 1839 | |
Gordon Henriksen | a2f3e13 | 2007-09-17 20:30:04 +0000 | [diff] [blame] | 1840 | switch (ID) { |
| 1841 | default: |
| 1842 | break; |
Chandler Carruth | 026cc37 | 2011-12-12 04:36:02 +0000 | [diff] [blame] | 1843 | case Intrinsic::ctlz: // llvm.ctlz |
| 1844 | case Intrinsic::cttz: // llvm.cttz |
| 1845 | Assert1(isa<ConstantInt>(CI.getArgOperand(1)), |
| 1846 | "is_zero_undef argument of bit counting intrinsics must be a " |
| 1847 | "constant int", &CI); |
| 1848 | break; |
Victor Hernandez | 016b286 | 2010-01-22 19:06:12 +0000 | [diff] [blame] | 1849 | case Intrinsic::dbg_declare: { // llvm.dbg.declare |
Gabor Greif | 4d18165 | 2010-06-23 13:56:57 +0000 | [diff] [blame] | 1850 | Assert1(CI.getArgOperand(0) && isa<MDNode>(CI.getArgOperand(0)), |
Victor Hernandez | 016b286 | 2010-01-22 19:06:12 +0000 | [diff] [blame] | 1851 | "invalid llvm.dbg.declare intrinsic call 1", &CI); |
Gabor Greif | 4d18165 | 2010-06-23 13:56:57 +0000 | [diff] [blame] | 1852 | MDNode *MD = cast<MDNode>(CI.getArgOperand(0)); |
Victor Hernandez | 016b286 | 2010-01-22 19:06:12 +0000 | [diff] [blame] | 1853 | Assert1(MD->getNumOperands() == 1, |
| 1854 | "invalid llvm.dbg.declare intrinsic call 2", &CI); |
Victor Hernandez | 016b286 | 2010-01-22 19:06:12 +0000 | [diff] [blame] | 1855 | } break; |
Chris Lattner | dd70834 | 2008-11-21 16:42:48 +0000 | [diff] [blame] | 1856 | case Intrinsic::memcpy: |
| 1857 | case Intrinsic::memmove: |
| 1858 | case Intrinsic::memset: |
Gabor Greif | 4d18165 | 2010-06-23 13:56:57 +0000 | [diff] [blame] | 1859 | Assert1(isa<ConstantInt>(CI.getArgOperand(3)), |
Chris Lattner | ecded9a | 2008-08-23 05:31:10 +0000 | [diff] [blame] | 1860 | "alignment argument of memory intrinsics must be a constant int", |
| 1861 | &CI); |
Eli Friedman | af5a895 | 2011-05-31 20:12:07 +0000 | [diff] [blame] | 1862 | Assert1(isa<ConstantInt>(CI.getArgOperand(4)), |
| 1863 | "isvolatile argument of memory intrinsics must be a constant int", |
| 1864 | &CI); |
Chris Lattner | ecded9a | 2008-08-23 05:31:10 +0000 | [diff] [blame] | 1865 | break; |
Bill Wendling | 05604e0 | 2008-08-23 09:46:46 +0000 | [diff] [blame] | 1866 | case Intrinsic::gcroot: |
| 1867 | case Intrinsic::gcwrite: |
Chris Lattner | 2585206 | 2008-08-24 20:46:13 +0000 | [diff] [blame] | 1868 | case Intrinsic::gcread: |
| 1869 | if (ID == Intrinsic::gcroot) { |
Gordon Henriksen | bf40eee | 2008-10-25 16:28:35 +0000 | [diff] [blame] | 1870 | AllocaInst *AI = |
Gabor Greif | 4d18165 | 2010-06-23 13:56:57 +0000 | [diff] [blame] | 1871 | dyn_cast<AllocaInst>(CI.getArgOperand(0)->stripPointerCasts()); |
Talin | 2e59f14 | 2010-09-30 20:23:47 +0000 | [diff] [blame] | 1872 | Assert1(AI, "llvm.gcroot parameter #1 must be an alloca.", &CI); |
Gabor Greif | 4d18165 | 2010-06-23 13:56:57 +0000 | [diff] [blame] | 1873 | Assert1(isa<Constant>(CI.getArgOperand(1)), |
Chris Lattner | 2585206 | 2008-08-24 20:46:13 +0000 | [diff] [blame] | 1874 | "llvm.gcroot parameter #2 must be a constant.", &CI); |
Talin | 2e59f14 | 2010-09-30 20:23:47 +0000 | [diff] [blame] | 1875 | if (!AI->getType()->getElementType()->isPointerTy()) { |
| 1876 | Assert1(!isa<ConstantPointerNull>(CI.getArgOperand(1)), |
| 1877 | "llvm.gcroot parameter #1 must either be a pointer alloca, " |
| 1878 | "or argument #2 must be a non-null constant.", &CI); |
| 1879 | } |
Chris Lattner | 2585206 | 2008-08-24 20:46:13 +0000 | [diff] [blame] | 1880 | } |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 1881 | |
Chris Lattner | 2585206 | 2008-08-24 20:46:13 +0000 | [diff] [blame] | 1882 | Assert1(CI.getParent()->getParent()->hasGC(), |
| 1883 | "Enclosing function does not use GC.", &CI); |
| 1884 | break; |
Duncan Sands | f72ff0c | 2007-09-29 16:25:54 +0000 | [diff] [blame] | 1885 | case Intrinsic::init_trampoline: |
Gabor Greif | 4d18165 | 2010-06-23 13:56:57 +0000 | [diff] [blame] | 1886 | Assert1(isa<Function>(CI.getArgOperand(1)->stripPointerCasts()), |
Duncan Sands | f72ff0c | 2007-09-29 16:25:54 +0000 | [diff] [blame] | 1887 | "llvm.init_trampoline parameter #2 must resolve to a function.", |
| 1888 | &CI); |
Gordon Henriksen | 9157c49 | 2007-12-25 02:02:10 +0000 | [diff] [blame] | 1889 | break; |
Chris Lattner | 229f765 | 2008-10-16 06:00:36 +0000 | [diff] [blame] | 1890 | case Intrinsic::prefetch: |
Gabor Greif | 4d18165 | 2010-06-23 13:56:57 +0000 | [diff] [blame] | 1891 | Assert1(isa<ConstantInt>(CI.getArgOperand(1)) && |
| 1892 | isa<ConstantInt>(CI.getArgOperand(2)) && |
| 1893 | cast<ConstantInt>(CI.getArgOperand(1))->getZExtValue() < 2 && |
| 1894 | cast<ConstantInt>(CI.getArgOperand(2))->getZExtValue() < 4, |
Chris Lattner | 229f765 | 2008-10-16 06:00:36 +0000 | [diff] [blame] | 1895 | "invalid arguments to llvm.prefetch", |
| 1896 | &CI); |
| 1897 | break; |
Bill Wendling | d8e312d | 2008-11-18 23:09:31 +0000 | [diff] [blame] | 1898 | case Intrinsic::stackprotector: |
Gabor Greif | 4d18165 | 2010-06-23 13:56:57 +0000 | [diff] [blame] | 1899 | Assert1(isa<AllocaInst>(CI.getArgOperand(1)->stripPointerCasts()), |
Bill Wendling | d8e312d | 2008-11-18 23:09:31 +0000 | [diff] [blame] | 1900 | "llvm.stackprotector parameter #2 must resolve to an alloca.", |
| 1901 | &CI); |
| 1902 | break; |
Nick Lewycky | 9bc8904 | 2009-10-13 07:57:33 +0000 | [diff] [blame] | 1903 | case Intrinsic::lifetime_start: |
| 1904 | case Intrinsic::lifetime_end: |
| 1905 | case Intrinsic::invariant_start: |
Gabor Greif | 4d18165 | 2010-06-23 13:56:57 +0000 | [diff] [blame] | 1906 | Assert1(isa<ConstantInt>(CI.getArgOperand(0)), |
Nick Lewycky | 9bc8904 | 2009-10-13 07:57:33 +0000 | [diff] [blame] | 1907 | "size argument of memory use markers must be a constant integer", |
| 1908 | &CI); |
| 1909 | break; |
| 1910 | case Intrinsic::invariant_end: |
Gabor Greif | 4d18165 | 2010-06-23 13:56:57 +0000 | [diff] [blame] | 1911 | Assert1(isa<ConstantInt>(CI.getArgOperand(1)), |
Nick Lewycky | 9bc8904 | 2009-10-13 07:57:33 +0000 | [diff] [blame] | 1912 | "llvm.invariant.end parameter #2 must be a constant integer", &CI); |
| 1913 | break; |
Gordon Henriksen | a2f3e13 | 2007-09-17 20:30:04 +0000 | [diff] [blame] | 1914 | } |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1915 | } |
| 1916 | |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1917 | //===----------------------------------------------------------------------===// |
| 1918 | // Implement the public interfaces to this file... |
| 1919 | //===----------------------------------------------------------------------===// |
| 1920 | |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1921 | FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) { |
| 1922 | return new Verifier(action); |
Chris Lattner | 0e851da | 2002-04-18 20:37:37 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1925 | |
Dan Gohman | bc04830 | 2010-04-08 15:57:10 +0000 | [diff] [blame] | 1926 | /// verifyFunction - Check a function for errors, printing messages on stderr. |
| 1927 | /// Return true if the function is corrupt. |
| 1928 | /// |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1929 | bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) { |
Chris Lattner | b870ca7 | 2004-03-14 03:16:15 +0000 | [diff] [blame] | 1930 | Function &F = const_cast<Function&>(f); |
Reid Spencer | 5301e7c | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 1931 | assert(!F.isDeclaration() && "Cannot verify external functions"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1932 | |
Jeffrey Yasskin | 091217b | 2010-01-27 20:34:15 +0000 | [diff] [blame] | 1933 | FunctionPassManager FPM(F.getParent()); |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1934 | Verifier *V = new Verifier(action); |
Chris Lattner | b870ca7 | 2004-03-14 03:16:15 +0000 | [diff] [blame] | 1935 | FPM.add(V); |
| 1936 | FPM.run(F); |
| 1937 | return V->Broken; |
Chris Lattner | d02f08d | 2002-02-20 17:55:43 +0000 | [diff] [blame] | 1938 | } |
| 1939 | |
Misha Brukman | c566ca36 | 2004-03-02 00:22:19 +0000 | [diff] [blame] | 1940 | /// verifyModule - Check a module for errors, printing messages on stderr. |
| 1941 | /// Return true if the module is corrupt. |
| 1942 | /// |
Chris Lattner | 5734e8d | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 1943 | bool llvm::verifyModule(const Module &M, VerifierFailureAction action, |
| 1944 | std::string *ErrorInfo) { |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1945 | PassManager PM; |
Chris Lattner | a45a216 | 2004-04-02 15:45:08 +0000 | [diff] [blame] | 1946 | Verifier *V = new Verifier(action); |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1947 | PM.add(V); |
Dan Gohman | 6116385 | 2008-06-24 17:47:37 +0000 | [diff] [blame] | 1948 | PM.run(const_cast<Module&>(M)); |
Nick Lewycky | 3fc8980 | 2009-09-07 20:44:51 +0000 | [diff] [blame] | 1949 | |
Chris Lattner | 5734e8d | 2006-07-06 18:02:27 +0000 | [diff] [blame] | 1950 | if (ErrorInfo && V->Broken) |
Chris Lattner | 78683a7 | 2009-08-23 04:02:03 +0000 | [diff] [blame] | 1951 | *ErrorInfo = V->MessagesStr.str(); |
Chris Lattner | 8e72d6f | 2002-08-02 17:37:08 +0000 | [diff] [blame] | 1952 | return V->Broken; |
Chris Lattner | 2f7c963 | 2001-06-06 20:29:01 +0000 | [diff] [blame] | 1953 | } |