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