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