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