blob: d523e42823f87f6db3ecf42a8c0a50c431e8b96f [file] [log] [blame]
Nick Lewycky1d05c212012-02-25 07:20:06 +00001//===-- Verifier.cpp - Implement the Module Verifier -----------------------==//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
Chris Lattnera5c3dec2002-03-29 19:06:18 +000010// This file defines the function verifier interface, that can be used for some
Chris Lattner00950542001-06-06 20:29:01 +000011// sanity checking of input to the system.
12//
Misha Brukman5b636312004-06-24 21:47:35 +000013// 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 Lattner00950542001-06-06 20:29:01 +000015//
Misha Brukman5b636312004-06-24 21:47:35 +000016// * Both of a binary operator's parameters are of the same type
Chris Lattnera00409e2002-04-24 19:12:21 +000017// * Verify that the indices of mem access instructions match other operands
Misha Brukman5b636312004-06-24 21:47:35 +000018// * Verify that arithmetic and other things are only performed on first-class
Chris Lattner9ce231f2002-08-02 17:37:08 +000019// types. Verify that shifts & logicals only happen on integrals f.e.
Misha Brukman5b636312004-06-24 21:47:35 +000020// * All of the constants in a switch statement are of the correct type
Chris Lattner9ce231f2002-08-02 17:37:08 +000021// * The code is in valid SSA form
Misha Brukman5b636312004-06-24 21:47:35 +000022// * It should be illegal to put a label into any other type (like a structure)
Chris Lattner00950542001-06-06 20:29:01 +000023// or to return one. [except constant arrays!]
Nick Lewycky0c78ac12008-03-28 06:46:51 +000024// * Only phi nodes can be self referential: 'add i32 %0, %0 ; <int>:0' is bad
Chris Lattner44d5bd92002-02-20 17:55:43 +000025// * PHI nodes must have an entry for each predecessor, with no extras.
Chris Lattner24e845f2002-06-25 15:56:27 +000026// * PHI nodes must be the first thing in a basic block, all grouped together
Chris Lattnerf6ffcb62002-10-06 21:00:31 +000027// * PHI nodes must have at least one entry
Chris Lattner24e845f2002-06-25 15:56:27 +000028// * All basic blocks should only end with terminator insts, not contain them
Chris Lattnera5c3dec2002-03-29 19:06:18 +000029// * The entry node to a function must not have predecessors
Misha Brukman6b634522003-10-10 17:54:14 +000030// * All Instructions must be embedded into a basic block
Misha Brukman5b636312004-06-24 21:47:35 +000031// * Functions cannot take a void-typed parameter
Chris Lattnerea249242002-04-13 22:48:46 +000032// * Verify that a function's argument list agrees with it's declared type.
Chris Lattneracd3cae2002-03-15 20:25:09 +000033// * It is illegal to specify a name for a void value.
Misha Brukman6b634522003-10-10 17:54:14 +000034// * It is illegal to have a internal global value with no initializer
Chris Lattner23f0ce62002-04-12 18:20:49 +000035// * It is illegal to have a ret instruction that returns a value that does not
36// agree with the function return value type.
Chris Lattner56732fb2002-05-08 19:49:50 +000037// * Function call argument types match the function prototype
Bill Wendlinge6e88262011-08-12 20:24:12 +000038// * 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 Lattnera00409e2002-04-24 19:12:21 +000044// * All other things that are tested by asserts spread about the code...
Chris Lattner00950542001-06-06 20:29:01 +000045//
46//===----------------------------------------------------------------------===//
47
48#include "llvm/Analysis/Verifier.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000049#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 Rencd262572013-07-19 00:31:03 +000056#include "llvm/DebugInfo.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000057#include "llvm/IR/CallingConv.h"
58#include "llvm/IR/Constants.h"
Matt Arsenault16e4ed52013-07-31 17:49:08 +000059#include "llvm/IR/DataLayout.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000060#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 Carruth84bcf932012-11-30 03:08:41 +000066#include "llvm/InstVisitor.h"
Chris Lattner58d74912008-03-12 17:45:29 +000067#include "llvm/Pass.h"
Chris Lattnercf899082004-02-14 02:47:17 +000068#include "llvm/PassManager.h"
Chris Lattner44d5bd92002-02-20 17:55:43 +000069#include "llvm/Support/CFG.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000070#include "llvm/Support/CallSite.h"
Manman Rencd262572013-07-19 00:31:03 +000071#include "llvm/Support/CommandLine.h"
Rafael Espindolaa1b95f52012-05-31 16:04:26 +000072#include "llvm/Support/ConstantRange.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000073#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000074#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc2871372009-02-28 21:05:51 +000075#include "llvm/Support/raw_ostream.h"
Chris Lattner44d5bd92002-02-20 17:55:43 +000076#include <algorithm>
Jeff Cohen4c5701d2006-03-31 07:22:05 +000077#include <cstdarg>
Chris Lattner31f84992003-11-21 20:23:48 +000078using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000079
Manman Rencd262572013-07-19 00:31:03 +000080static cl::opt<bool> DisableDebugInfoVerifier("disable-debug-info-verifier",
81 cl::init(false));
82
Chris Lattnerd231fc32002-04-18 20:37:37 +000083namespace { // Anonymous namespace for class
Nick Lewycky6726b6d2009-10-25 06:33:48 +000084 struct PreVerifier : public FunctionPass {
Owen Anderson765d6452007-11-01 03:54:23 +000085 static char ID; // Pass ID, replacement for typeid
Duncan Sandsd0561902007-11-01 10:50:26 +000086
Owen Anderson081c34b2010-10-19 17:21:58 +000087 PreVerifier() : FunctionPass(ID) {
88 initializePreVerifierPass(*PassRegistry::getPassRegistry());
89 }
Duncan Sandsd0561902007-11-01 10:50:26 +000090
Chris Lattner11240d02008-12-01 03:58:38 +000091 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
92 AU.setPreservesAll();
93 }
94
Duncan Sandsd0561902007-11-01 10:50:26 +000095 // Check that the prerequisites for successful DominatorTree construction
96 // are satisfied.
Owen Anderson765d6452007-11-01 03:54:23 +000097 bool runOnFunction(Function &F) {
Duncan Sandsd0561902007-11-01 10:50:26 +000098 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 Arsenault3f493222013-07-20 17:46:00 +0000102 dbgs() << "Basic Block in function '" << F.getName()
Chris Lattner2a9a8ae2010-06-12 15:50:24 +0000103 << "' does not have terminator!\n";
David Greene4b121682010-01-05 01:29:14 +0000104 WriteAsOperand(dbgs(), I, true);
105 dbgs() << "\n";
Duncan Sandsd0561902007-11-01 10:50:26 +0000106 Broken = true;
107 }
108 }
109
110 if (Broken)
Chris Lattner75361b62010-04-07 22:58:41 +0000111 report_fatal_error("Broken module, no Basic Block terminator!");
Duncan Sandsd0561902007-11-01 10:50:26 +0000112
113 return false;
Owen Anderson765d6452007-11-01 03:54:23 +0000114 }
Owen Andersonc570e332007-10-31 21:04:18 +0000115 };
Dan Gohman844731a2008-05-13 00:00:25 +0000116}
Duncan Sandsd0561902007-11-01 10:50:26 +0000117
Dan Gohman844731a2008-05-13 00:00:25 +0000118char PreVerifier::ID = 0;
Matt Arsenault3f493222013-07-20 17:46:00 +0000119INITIALIZE_PASS(PreVerifier, "preverify", "Preliminary module verification",
Owen Andersonce665bd2010-10-07 22:25:06 +0000120 false, false)
Benjamin Kramera3ac4272010-10-22 17:35:07 +0000121static char &PreVerifyID = PreVerifier::ID;
Duncan Sandsd0561902007-11-01 10:50:26 +0000122
Dan Gohman844731a2008-05-13 00:00:25 +0000123namespace {
Nick Lewyckybc6836b2009-09-13 23:45:39 +0000124 struct Verifier : public FunctionPass, public InstVisitor<Verifier> {
Devang Patel19974732007-05-03 01:11:54 +0000125 static char ID; // Pass ID, replacement for typeid
Chris Lattner9ce231f2002-08-02 17:37:08 +0000126 bool Broken; // Is this module found to be broken?
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000127 VerifierFailureAction action;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000128 // What to do if verification fails.
Misha Brukmanab5c6002004-03-02 00:22:19 +0000129 Module *Mod; // Module we are verifying right now
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000130 LLVMContext *Context; // Context within which we are verifying
131 DominatorTree *DT; // Dominator Tree, caution can be null!
Matt Arsenault16e4ed52013-07-31 17:49:08 +0000132 const DataLayout *DL;
Nick Lewycky29ef6592009-09-07 20:44:51 +0000133
Chris Lattner37f077a2009-08-23 04:02:03 +0000134 std::string Messages;
135 raw_string_ostream MessagesStr;
Chris Lattner00950542001-06-06 20:29:01 +0000136
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000137 /// 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 Lattner78287b42007-02-10 08:19:44 +0000141 SmallPtrSet<Instruction*, 16> InstsInThisBlock;
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000142
Duncan Sandse704d9d2010-04-29 16:10:30 +0000143 /// MDNodes - keep track of the metadata nodes that have been checked
144 /// already.
145 SmallPtrSet<MDNode *, 32> MDNodes;
146
Bill Wendlinge6e88262011-08-12 20:24:12 +0000147 /// 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 Ren0e29eee2013-07-23 00:22:51 +0000152 /// Finder keeps track of all debug info MDNodes in a Module.
153 DebugInfoFinder Finder;
154
Misha Brukmanfd939082005-04-21 23:48:37 +0000155 Verifier()
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000156 : FunctionPass(ID), Broken(false),
Matt Arsenault16e4ed52013-07-31 17:49:08 +0000157 action(AbortProcessAction), Mod(0), Context(0), DT(0), DL(0),
Bill Wendlinge6e88262011-08-12 20:24:12 +0000158 MessagesStr(Messages), PersonalityFn(0) {
159 initializeVerifierPass(*PassRegistry::getPassRegistry());
160 }
Dan Gohman950a4c42008-03-25 22:06:05 +0000161 explicit Verifier(VerifierFailureAction ctn)
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000162 : FunctionPass(ID), Broken(false), action(ctn), Mod(0),
Matt Arsenault16e4ed52013-07-31 17:49:08 +0000163 Context(0), DT(0), DL(0), MessagesStr(Messages), PersonalityFn(0) {
Bill Wendlinge6e88262011-08-12 20:24:12 +0000164 initializeVerifierPass(*PassRegistry::getPassRegistry());
165 }
Chris Lattner00950542001-06-06 20:29:01 +0000166
Chris Lattner24e845f2002-06-25 15:56:27 +0000167 bool doInitialization(Module &M) {
Brian Gaeke9cebe2d2003-11-16 23:07:42 +0000168 Mod = &M;
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000169 Context = &M.getContext();
Manman Ren0e29eee2013-07-23 00:22:51 +0000170 Finder.reset();
Chris Lattner3e1f1442002-09-19 16:12:19 +0000171
Matt Arsenault16e4ed52013-07-31 17:49:08 +0000172 DL = getAnalysisIfAvailable<DataLayout>();
173
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000174 // We must abort before returning back to the pass manager, or else the
175 // pass manager may try to run other passes on the broken module.
176 return abortIfBroken();
Chris Lattnerd231fc32002-04-18 20:37:37 +0000177 }
178
Chris Lattner24e845f2002-06-25 15:56:27 +0000179 bool runOnFunction(Function &F) {
Chris Lattner9ce231f2002-08-02 17:37:08 +0000180 // Get dominator information if we are being run by PassManager
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000181 DT = &getAnalysis<DominatorTree>();
Chris Lattnercd070752007-04-20 23:59:29 +0000182
183 Mod = F.getParent();
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000184 if (!Context) Context = &F.getContext();
Chris Lattnercd070752007-04-20 23:59:29 +0000185
Chris Lattnerd231fc32002-04-18 20:37:37 +0000186 visit(F);
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000187 InstsInThisBlock.clear();
Bill Wendlinge6e88262011-08-12 20:24:12 +0000188 PersonalityFn = 0;
Chris Lattner3e1f1442002-09-19 16:12:19 +0000189
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000190 // We must abort before returning back to the pass manager, or else the
191 // pass manager may try to run other passes on the broken module.
192 return abortIfBroken();
Chris Lattnerd231fc32002-04-18 20:37:37 +0000193 }
194
Chris Lattner24e845f2002-06-25 15:56:27 +0000195 bool doFinalization(Module &M) {
Chris Lattner794caa12002-04-28 16:04:26 +0000196 // Scan through, checking all of the external function's linkage now...
Chris Lattner7c277b32004-06-03 06:38:43 +0000197 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner53997412003-04-16 20:42:40 +0000198 visitGlobalValue(*I);
Chris Lattner794caa12002-04-28 16:04:26 +0000199
Chris Lattner7c277b32004-06-03 06:38:43 +0000200 // Check to make sure function prototypes are okay.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000201 if (I->isDeclaration()) visitFunction(*I);
Chris Lattner7c277b32004-06-03 06:38:43 +0000202 }
203
Matt Arsenault3f493222013-07-20 17:46:00 +0000204 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
Reid Spencer0b118202006-01-16 21:12:35 +0000205 I != E; ++I)
Chris Lattner56998b22004-12-15 20:23:49 +0000206 visitGlobalVariable(*I);
Chris Lattner61b91bc2002-10-06 22:47:32 +0000207
Matt Arsenault3f493222013-07-20 17:46:00 +0000208 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000209 I != E; ++I)
210 visitGlobalAlias(*I);
211
Duncan Sandse704d9d2010-04-29 16:10:30 +0000212 for (Module::named_metadata_iterator I = M.named_metadata_begin(),
213 E = M.named_metadata_end(); I != E; ++I)
214 visitNamedMDNode(*I);
215
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000216 visitModuleFlags(M);
217
Manman Rencd262572013-07-19 00:31:03 +0000218 // Verify Debug Info.
219 verifyDebugInfo(M);
220
Chris Lattner3e1f1442002-09-19 16:12:19 +0000221 // If the module is broken, abort at this time.
Reid Spencer7107c3b2006-07-26 16:18:00 +0000222 return abortIfBroken();
Chris Lattnera00409e2002-04-24 19:12:21 +0000223 }
224
Chris Lattner97e52e42002-04-28 21:27:06 +0000225 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
226 AU.setPreservesAll();
Owen Andersonc570e332007-10-31 21:04:18 +0000227 AU.addRequiredID(PreVerifyID);
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000228 AU.addRequired<DominatorTree>();
Chris Lattner97e52e42002-04-28 21:27:06 +0000229 }
230
Misha Brukmanab5c6002004-03-02 00:22:19 +0000231 /// abortIfBroken - If the module is broken and we are supposed to abort on
232 /// this condition, do so.
233 ///
Reid Spencer7107c3b2006-07-26 16:18:00 +0000234 bool abortIfBroken() {
Chris Lattner505569d2008-08-27 17:36:58 +0000235 if (!Broken) return false;
Chris Lattner37f077a2009-08-23 04:02:03 +0000236 MessagesStr << "Broken module found, ";
Chris Lattner505569d2008-08-27 17:36:58 +0000237 switch (action) {
Chris Lattner505569d2008-08-27 17:36:58 +0000238 case AbortProcessAction:
Chris Lattner37f077a2009-08-23 04:02:03 +0000239 MessagesStr << "compilation aborted!\n";
David Greene4b121682010-01-05 01:29:14 +0000240 dbgs() << MessagesStr.str();
Torok Edwindac237e2009-07-08 20:53:28 +0000241 // Client should choose different reaction if abort is not desired
242 abort();
Chris Lattner505569d2008-08-27 17:36:58 +0000243 case PrintMessageAction:
Chris Lattner37f077a2009-08-23 04:02:03 +0000244 MessagesStr << "verification continues.\n";
David Greene4b121682010-01-05 01:29:14 +0000245 dbgs() << MessagesStr.str();
Chris Lattner505569d2008-08-27 17:36:58 +0000246 return false;
247 case ReturnStatusAction:
Chris Lattner37f077a2009-08-23 04:02:03 +0000248 MessagesStr << "compilation terminated.\n";
Nick Lewycky57174882009-03-15 06:40:32 +0000249 return true;
Chris Lattner3e1f1442002-09-19 16:12:19 +0000250 }
Chandler Carruth732f05c2012-01-10 18:08:01 +0000251 llvm_unreachable("Invalid action");
Chris Lattner3e1f1442002-09-19 16:12:19 +0000252 }
253
Chris Lattner53997412003-04-16 20:42:40 +0000254
Chris Lattnerd231fc32002-04-18 20:37:37 +0000255 // Verification methods...
Chris Lattner53997412003-04-16 20:42:40 +0000256 void visitGlobalValue(GlobalValue &GV);
Chris Lattner56998b22004-12-15 20:23:49 +0000257 void visitGlobalVariable(GlobalVariable &GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000258 void visitGlobalAlias(GlobalAlias &GA);
Duncan Sandse704d9d2010-04-29 16:10:30 +0000259 void visitNamedMDNode(NamedMDNode &NMD);
260 void visitMDNode(MDNode &MD, Function *F);
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000261 void visitModuleFlags(Module &M);
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000262 void visitModuleFlag(MDNode *Op, DenseMap<MDString*, MDNode*> &SeenIDs,
263 SmallVectorImpl<MDNode*> &Requirements);
Chris Lattner24e845f2002-06-25 15:56:27 +0000264 void visitFunction(Function &F);
265 void visitBasicBlock(BasicBlock &BB);
Chris Lattnercad208b2008-08-28 04:02:44 +0000266 using InstVisitor<Verifier>::visit;
Nick Lewycky29ef6592009-09-07 20:44:51 +0000267
Chris Lattnercad208b2008-08-28 04:02:44 +0000268 void visit(Instruction &I);
Nick Lewycky29ef6592009-09-07 20:44:51 +0000269
Reid Spencer3da59db2006-11-27 01:05:10 +0000270 void visitTruncInst(TruncInst &I);
271 void visitZExtInst(ZExtInst &I);
272 void visitSExtInst(SExtInst &I);
273 void visitFPTruncInst(FPTruncInst &I);
274 void visitFPExtInst(FPExtInst &I);
275 void visitFPToUIInst(FPToUIInst &I);
276 void visitFPToSIInst(FPToSIInst &I);
277 void visitUIToFPInst(UIToFPInst &I);
278 void visitSIToFPInst(SIToFPInst &I);
279 void visitIntToPtrInst(IntToPtrInst &I);
280 void visitPtrToIntInst(PtrToIntInst &I);
281 void visitBitCastInst(BitCastInst &I);
Chris Lattner24e845f2002-06-25 15:56:27 +0000282 void visitPHINode(PHINode &PN);
283 void visitBinaryOperator(BinaryOperator &B);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000284 void visitICmpInst(ICmpInst &IC);
285 void visitFCmpInst(FCmpInst &FC);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000286 void visitExtractElementInst(ExtractElementInst &EI);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000287 void visitInsertElementInst(InsertElementInst &EI);
Chris Lattner00f10232006-04-08 01:18:18 +0000288 void visitShuffleVectorInst(ShuffleVectorInst &EI);
Chris Lattner4d45bd02003-10-18 05:57:43 +0000289 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
Chris Lattner24e845f2002-06-25 15:56:27 +0000290 void visitCallInst(CallInst &CI);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000291 void visitInvokeInst(InvokeInst &II);
Chris Lattner24e845f2002-06-25 15:56:27 +0000292 void visitGetElementPtrInst(GetElementPtrInst &GEP);
293 void visitLoadInst(LoadInst &LI);
294 void visitStoreInst(StoreInst &SI);
Rafael Espindolac987f4c2012-02-26 02:23:37 +0000295 void verifyDominatesUse(Instruction &I, unsigned i);
Chris Lattner24e845f2002-06-25 15:56:27 +0000296 void visitInstruction(Instruction &I);
297 void visitTerminatorInst(TerminatorInst &I);
Nick Lewycky6a7cb632010-02-15 22:09:09 +0000298 void visitBranchInst(BranchInst &BI);
Chris Lattner24e845f2002-06-25 15:56:27 +0000299 void visitReturnInst(ReturnInst &RI);
Chris Lattner0f9e9d02004-05-21 16:47:21 +0000300 void visitSwitchInst(SwitchInst &SI);
Dan Gohman37680912010-08-02 23:08:33 +0000301 void visitIndirectBrInst(IndirectBrInst &BI);
Chris Lattner230c1a72004-03-12 05:54:31 +0000302 void visitSelectInst(SelectInst &SI);
Chris Lattner627079d2002-11-21 16:54:22 +0000303 void visitUserOp1(Instruction &I);
304 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
Brian Gaeked0fde302003-11-11 22:41:34 +0000305 void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI);
Eli Friedmanff030482011-07-28 21:48:00 +0000306 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI);
307 void visitAtomicRMWInst(AtomicRMWInst &RMWI);
Eli Friedman47f35132011-07-25 23:16:38 +0000308 void visitFenceInst(FenceInst &FI);
Victor Hernandez7b929da2009-10-23 21:09:37 +0000309 void visitAllocaInst(AllocaInst &AI);
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000310 void visitExtractValueInst(ExtractValueInst &EVI);
311 void visitInsertValueInst(InsertValueInst &IVI);
Bill Wendlinge6e88262011-08-12 20:24:12 +0000312 void visitLandingPadInst(LandingPadInst &LPI);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000313
Duncan Sandsd9d70392007-12-21 19:19:01 +0000314 void VerifyCallSite(CallSite CS);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000315 bool PerformTypeCheck(Intrinsic::ID ID, Function *F, Type *Ty,
Bill Wendlinga6ce05f2008-11-13 07:11:27 +0000316 int VT, unsigned ArgNo, std::string &Suffix);
Chris Lattner55dc5c72012-05-27 19:37:05 +0000317 bool VerifyIntrinsicType(Type *Ty,
318 ArrayRef<Intrinsic::IITDescriptor> &Infos,
319 SmallVectorImpl<Type*> &ArgTys);
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000320 bool VerifyAttributeCount(AttributeSet Attrs, unsigned Params);
321 void VerifyAttributeTypes(AttributeSet Attrs, unsigned Idx,
322 bool isFunction, const Value *V);
323 void VerifyParameterAttrs(AttributeSet Attrs, unsigned Idx, Type *Ty,
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000324 bool isReturnValue, const Value *V);
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000325 void VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs,
Duncan Sandscfad1b42008-01-12 16:42:01 +0000326 const Value *V);
Chris Lattner15e87522003-11-21 17:35:51 +0000327
Matt Arsenault16e4ed52013-07-31 17:49:08 +0000328 void VerifyBitcastType(const Value *V, Type *DestTy, Type *SrcTy);
329 void VerifyConstantExprBitcastType(const ConstantExpr *CE);
330
Manman Rencd262572013-07-19 00:31:03 +0000331 void verifyDebugInfo(Module &M);
332
Chris Lattner15e87522003-11-21 17:35:51 +0000333 void WriteValue(const Value *V) {
334 if (!V) return;
Chris Lattner31f84992003-11-21 20:23:48 +0000335 if (isa<Instruction>(V)) {
Nick Lewycky2bb6f6a2009-10-17 19:43:45 +0000336 MessagesStr << *V << '\n';
Chris Lattner31f84992003-11-21 20:23:48 +0000337 } else {
Chris Lattner37f077a2009-08-23 04:02:03 +0000338 WriteAsOperand(MessagesStr, V, true, Mod);
Nick Lewycky2bb6f6a2009-10-17 19:43:45 +0000339 MessagesStr << '\n';
Chris Lattner15e87522003-11-21 17:35:51 +0000340 }
341 }
342
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000343 void WriteType(Type *T) {
Chris Lattnerc2871372009-02-28 21:05:51 +0000344 if (!T) return;
Chris Lattner1afcace2011-07-09 17:41:24 +0000345 MessagesStr << ' ' << *T;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000346 }
347
Chris Lattner15e87522003-11-21 17:35:51 +0000348
Chris Lattnerd231fc32002-04-18 20:37:37 +0000349 // CheckFailed - A check failed, so print out the condition and the message
350 // that failed. This provides a nice place to put a breakpoint if you want
351 // to see why something is not correct.
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000352 void CheckFailed(const Twine &Message,
Chris Lattner15e87522003-11-21 17:35:51 +0000353 const Value *V1 = 0, const Value *V2 = 0,
354 const Value *V3 = 0, const Value *V4 = 0) {
Chris Lattner37f077a2009-08-23 04:02:03 +0000355 MessagesStr << Message.str() << "\n";
Chris Lattner15e87522003-11-21 17:35:51 +0000356 WriteValue(V1);
357 WriteValue(V2);
358 WriteValue(V3);
359 WriteValue(V4);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000360 Broken = true;
361 }
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000362
Nick Lewycky4b6af8a2009-09-08 02:02:39 +0000363 void CheckFailed(const Twine &Message, const Value *V1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000364 Type *T2, const Value *V3 = 0) {
Chris Lattner37f077a2009-08-23 04:02:03 +0000365 MessagesStr << Message.str() << "\n";
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000366 WriteValue(V1);
367 WriteType(T2);
368 WriteValue(V3);
Reid Spencer5dff1582004-05-27 21:58:13 +0000369 Broken = true;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000370 }
Nick Lewycky49072472009-09-08 01:23:52 +0000371
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000372 void CheckFailed(const Twine &Message, Type *T1,
373 Type *T2 = 0, Type *T3 = 0) {
Nick Lewycky49072472009-09-08 01:23:52 +0000374 MessagesStr << Message.str() << "\n";
375 WriteType(T1);
376 WriteType(T2);
377 WriteType(T3);
378 Broken = true;
379 }
Chris Lattnerd231fc32002-04-18 20:37:37 +0000380 };
Chris Lattner31f84992003-11-21 20:23:48 +0000381} // End anonymous namespace
382
Dan Gohman844731a2008-05-13 00:00:25 +0000383char Verifier::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +0000384INITIALIZE_PASS_BEGIN(Verifier, "verify", "Module Verifier", false, false)
385INITIALIZE_PASS_DEPENDENCY(PreVerifier)
386INITIALIZE_PASS_DEPENDENCY(DominatorTree)
387INITIALIZE_PASS_END(Verifier, "verify", "Module Verifier", false, false)
Chris Lattner00950542001-06-06 20:29:01 +0000388
Chris Lattner44d5bd92002-02-20 17:55:43 +0000389// Assert - We know that cond should be true, if not print an error message.
390#define Assert(C, M) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000391 do { if (!(C)) { CheckFailed(M); return; } } while (0)
Chris Lattner44d5bd92002-02-20 17:55:43 +0000392#define Assert1(C, M, V1) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000393 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
Chris Lattner44d5bd92002-02-20 17:55:43 +0000394#define Assert2(C, M, V1, V2) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000395 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
Chris Lattner24e845f2002-06-25 15:56:27 +0000396#define Assert3(C, M, V1, V2, V3) \
397 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
398#define Assert4(C, M, V1, V2, V3, V4) \
399 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
Chris Lattner00950542001-06-06 20:29:01 +0000400
Chris Lattnercad208b2008-08-28 04:02:44 +0000401void Verifier::visit(Instruction &I) {
402 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
403 Assert1(I.getOperand(i) != 0, "Operand is null", &I);
404 InstVisitor<Verifier>::visit(I);
405}
406
407
Chris Lattner53997412003-04-16 20:42:40 +0000408void Verifier::visitGlobalValue(GlobalValue &GV) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000409 Assert1(!GV.isDeclaration() ||
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000410 GV.isMaterializable() ||
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000411 GV.hasExternalLinkage() ||
412 GV.hasDLLImportLinkage() ||
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000413 GV.hasExternalWeakLinkage() ||
414 (isa<GlobalAlias>(GV) &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000415 (GV.hasLocalLinkage() || GV.hasWeakLinkage())),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000416 "Global is external, but doesn't have external or dllimport or weak linkage!",
417 &GV);
418
Reid Spencer5cbf9852007-01-30 20:08:39 +0000419 Assert1(!GV.hasDLLImportLinkage() || GV.isDeclaration(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000420 "Global is marked as dllimport, but not external", &GV);
Nick Lewycky49072472009-09-08 01:23:52 +0000421
Chris Lattner53997412003-04-16 20:42:40 +0000422 Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
423 "Only global variables can have appending linkage!", &GV);
424
425 if (GV.hasAppendingLinkage()) {
Nick Lewycky49072472009-09-08 01:23:52 +0000426 GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
Duncan Sands1df98592010-02-16 11:11:14 +0000427 Assert1(GVar && GVar->getType()->getElementType()->isArrayTy(),
Nick Lewycky49072472009-09-08 01:23:52 +0000428 "Only global arrays can have appending linkage!", GVar);
Chris Lattner53997412003-04-16 20:42:40 +0000429 }
Bill Wendling55ae5152010-08-20 22:05:50 +0000430
Bill Wendling32811be2012-08-17 18:33:14 +0000431 Assert1(!GV.hasLinkOnceODRAutoHideLinkage() || GV.hasDefaultVisibility(),
432 "linkonce_odr_auto_hide can only have default visibility!",
Bill Wendling55ae5152010-08-20 22:05:50 +0000433 &GV);
Chris Lattner53997412003-04-16 20:42:40 +0000434}
435
Chris Lattner56998b22004-12-15 20:23:49 +0000436void Verifier::visitGlobalVariable(GlobalVariable &GV) {
Chris Lattner6693da02007-09-19 17:14:45 +0000437 if (GV.hasInitializer()) {
Chris Lattner56998b22004-12-15 20:23:49 +0000438 Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(),
439 "Global variable initializer type does not match global "
440 "variable type!", &GV);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000441
Chris Lattnercd81f5d2009-08-05 05:41:44 +0000442 // If the global has common linkage, it must have a zero initializer and
443 // cannot be constant.
444 if (GV.hasCommonLinkage()) {
Chris Lattner26d054d2009-08-05 05:21:07 +0000445 Assert1(GV.getInitializer()->isNullValue(),
446 "'common' global must have a zero initializer!", &GV);
Chris Lattnercd81f5d2009-08-05 05:41:44 +0000447 Assert1(!GV.isConstant(), "'common' global may not be marked constant!",
448 &GV);
449 }
Chris Lattner6693da02007-09-19 17:14:45 +0000450 } else {
451 Assert1(GV.hasExternalLinkage() || GV.hasDLLImportLinkage() ||
452 GV.hasExternalWeakLinkage(),
453 "invalid linkage type for global declaration", &GV);
454 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000455
Nick Lewycky2c44a802011-04-08 07:30:21 +0000456 if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
457 GV.getName() == "llvm.global_dtors")) {
458 Assert1(!GV.hasInitializer() || GV.hasAppendingLinkage(),
459 "invalid linkage for intrinsic global variable", &GV);
460 // Don't worry about emitting an error for it not being an array,
461 // visitGlobalValue will complain on appending non-array.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000462 if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getType())) {
463 StructType *STy = dyn_cast<StructType>(ATy->getElementType());
464 PointerType *FuncPtrTy =
Micah Villmowb8bce922012-10-24 17:25:11 +0000465 FunctionType::get(Type::getVoidTy(*Context), false)->getPointerTo();
Nick Lewycky2c44a802011-04-08 07:30:21 +0000466 Assert1(STy && STy->getNumElements() == 2 &&
467 STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
468 STy->getTypeAtIndex(1) == FuncPtrTy,
469 "wrong type for intrinsic global variable", &GV);
470 }
471 }
472
Rafael Espindola97bf57d2013-04-22 15:16:51 +0000473 if (GV.hasName() && (GV.getName() == "llvm.used" ||
Rafael Espindola70968312013-07-19 18:44:51 +0000474 GV.getName() == "llvm.compiler.used")) {
Rafael Espindolacde25b42013-04-22 14:58:02 +0000475 Assert1(!GV.hasInitializer() || GV.hasAppendingLinkage(),
476 "invalid linkage for intrinsic global variable", &GV);
477 Type *GVType = GV.getType()->getElementType();
478 if (ArrayType *ATy = dyn_cast<ArrayType>(GVType)) {
479 PointerType *PTy = dyn_cast<PointerType>(ATy->getElementType());
480 Assert1(PTy, "wrong type for intrinsic global variable", &GV);
481 if (GV.hasInitializer()) {
482 Constant *Init = GV.getInitializer();
483 ConstantArray *InitArray = dyn_cast<ConstantArray>(Init);
484 Assert1(InitArray, "wrong initalizer for intrinsic global variable",
485 Init);
486 for (unsigned i = 0, e = InitArray->getNumOperands(); i != e; ++i) {
Rafael Espindolaaf10fe62013-05-27 22:47:09 +0000487 Value *V = Init->getOperand(i)->stripPointerCastsNoFollowAliases();
488 Assert1(
489 isa<GlobalVariable>(V) || isa<Function>(V) || isa<GlobalAlias>(V),
490 "invalid llvm.used member", V);
Rafael Espindola9f8e6da2013-06-11 13:18:13 +0000491 Assert1(V->hasName(), "members of llvm.used must be named", V);
Rafael Espindolacde25b42013-04-22 14:58:02 +0000492 }
493 }
494 }
495 }
496
Matt Arsenault16e4ed52013-07-31 17:49:08 +0000497 if (!GV.hasInitializer()) {
498 visitGlobalValue(GV);
499 return;
500 }
501
502 // Walk any aggregate initializers looking for bitcasts between address spaces
503 SmallPtrSet<const Value *, 4> Visited;
504 SmallVector<const Value *, 4> WorkStack;
505 WorkStack.push_back(cast<Value>(GV.getInitializer()));
506
507 while (!WorkStack.empty()) {
508 const Value *V = WorkStack.pop_back_val();
509 if (!Visited.insert(V))
510 continue;
511
512 if (const User *U = dyn_cast<User>(V)) {
513 for (unsigned I = 0, N = U->getNumOperands(); I != N; ++I)
514 WorkStack.push_back(U->getOperand(I));
515 }
516
517 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
518 VerifyConstantExprBitcastType(CE);
519 if (Broken)
520 return;
521 }
522 }
523
Chris Lattner56998b22004-12-15 20:23:49 +0000524 visitGlobalValue(GV);
525}
526
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000527void Verifier::visitGlobalAlias(GlobalAlias &GA) {
528 Assert1(!GA.getName().empty(),
529 "Alias name cannot be empty!", &GA);
Rafael Espindolabb46f522009-01-15 20:18:42 +0000530 Assert1(GA.hasExternalLinkage() || GA.hasLocalLinkage() ||
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000531 GA.hasWeakLinkage(),
532 "Alias should have external or external weak linkage!", &GA);
Anton Korobeynikov018f7712008-05-08 23:11:06 +0000533 Assert1(GA.getAliasee(),
534 "Aliasee cannot be NULL!", &GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000535 Assert1(GA.getType() == GA.getAliasee()->getType(),
536 "Alias and aliasee types should match!", &GA);
Rafael Espindolabea46262011-01-08 16:42:36 +0000537 Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA);
Anton Korobeynikov018f7712008-05-08 23:11:06 +0000538
Matt Arsenault5f53c3b2013-07-20 17:46:05 +0000539 Constant *Aliasee = GA.getAliasee();
540
541 if (!isa<GlobalValue>(Aliasee)) {
542 ConstantExpr *CE = dyn_cast<ConstantExpr>(Aliasee);
Matt Arsenault3f493222013-07-20 17:46:00 +0000543 Assert1(CE &&
Chris Lattnera2165ed2009-04-25 21:23:19 +0000544 (CE->getOpcode() == Instruction::BitCast ||
545 CE->getOpcode() == Instruction::GetElementPtr) &&
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000546 isa<GlobalValue>(CE->getOperand(0)),
Anton Korobeynikov0f53f7f2007-04-28 14:35:41 +0000547 "Aliasee should be either GlobalValue or bitcast of GlobalValue",
548 &GA);
Matt Arsenault5f53c3b2013-07-20 17:46:05 +0000549
550 if (CE->getOpcode() == Instruction::BitCast) {
551 unsigned SrcAS = CE->getOperand(0)->getType()->getPointerAddressSpace();
552 unsigned DstAS = CE->getType()->getPointerAddressSpace();
553
554 Assert1(SrcAS == DstAS,
555 "Alias bitcasts cannot be between different address spaces",
556 &GA);
557 }
Anton Korobeynikov0f53f7f2007-04-28 14:35:41 +0000558 }
Anton Korobeynikov726d45c2008-03-22 08:36:14 +0000559
Matt Arsenault5f53c3b2013-07-20 17:46:05 +0000560 const GlobalValue* Resolved = GA.resolveAliasedGlobal(/*stopOnWeak*/ false);
561 Assert1(Resolved,
Anton Korobeynikovef30c1d2008-03-22 08:37:05 +0000562 "Aliasing chain should end with function or global variable", &GA);
Anton Korobeynikov726d45c2008-03-22 08:36:14 +0000563
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000564 visitGlobalValue(GA);
565}
566
Duncan Sandse704d9d2010-04-29 16:10:30 +0000567void Verifier::visitNamedMDNode(NamedMDNode &NMD) {
568 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) {
569 MDNode *MD = NMD.getOperand(i);
570 if (!MD)
571 continue;
572
Dan Gohman17aa92c2010-07-21 23:38:33 +0000573 Assert1(!MD->isFunctionLocal(),
574 "Named metadata operand cannot be function local!", MD);
Duncan Sandse704d9d2010-04-29 16:10:30 +0000575 visitMDNode(*MD, 0);
576 }
577}
578
579void Verifier::visitMDNode(MDNode &MD, Function *F) {
580 // Only visit each node once. Metadata can be mutually recursive, so this
581 // avoids infinite recursion here, as well as being an optimization.
582 if (!MDNodes.insert(&MD))
583 return;
584
585 for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) {
586 Value *Op = MD.getOperand(i);
587 if (!Op)
588 continue;
Dan Gohman557c83c2010-07-21 20:25:43 +0000589 if (isa<Constant>(Op) || isa<MDString>(Op))
Duncan Sandse704d9d2010-04-29 16:10:30 +0000590 continue;
591 if (MDNode *N = dyn_cast<MDNode>(Op)) {
592 Assert2(MD.isFunctionLocal() || !N->isFunctionLocal(),
593 "Global metadata operand cannot be function local!", &MD, N);
594 visitMDNode(*N, F);
595 continue;
596 }
597 Assert2(MD.isFunctionLocal(), "Invalid operand for global metadata!", &MD, Op);
598
599 // If this was an instruction, bb, or argument, verify that it is in the
600 // function that we expect.
601 Function *ActualF = 0;
602 if (Instruction *I = dyn_cast<Instruction>(Op))
603 ActualF = I->getParent()->getParent();
604 else if (BasicBlock *BB = dyn_cast<BasicBlock>(Op))
605 ActualF = BB->getParent();
606 else if (Argument *A = dyn_cast<Argument>(Op))
607 ActualF = A->getParent();
608 assert(ActualF && "Unimplemented function local metadata case!");
609
610 Assert2(ActualF == F, "function-local metadata used in wrong function",
611 &MD, Op);
612 }
613}
614
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000615void Verifier::visitModuleFlags(Module &M) {
616 const NamedMDNode *Flags = M.getModuleFlagsMetadata();
617 if (!Flags) return;
618
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000619 // Scan each flag, and track the flags and requirements.
620 DenseMap<MDString*, MDNode*> SeenIDs;
621 SmallVector<MDNode*, 16> Requirements;
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000622 for (unsigned I = 0, E = Flags->getNumOperands(); I != E; ++I) {
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000623 visitModuleFlag(Flags->getOperand(I), SeenIDs, Requirements);
624 }
625
626 // Validate that the requirements in the module are valid.
627 for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
628 MDNode *Requirement = Requirements[I];
629 MDString *Flag = cast<MDString>(Requirement->getOperand(0));
630 Value *ReqValue = Requirement->getOperand(1);
631
632 MDNode *Op = SeenIDs.lookup(Flag);
633 if (!Op) {
634 CheckFailed("invalid requirement on flag, flag is not present in module",
635 Flag);
636 continue;
637 }
638
639 if (Op->getOperand(2) != ReqValue) {
640 CheckFailed(("invalid requirement on flag, "
641 "flag does not have the required value"),
642 Flag);
643 continue;
644 }
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000645 }
646}
647
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000648void Verifier::visitModuleFlag(MDNode *Op, DenseMap<MDString*, MDNode*>&SeenIDs,
649 SmallVectorImpl<MDNode*> &Requirements) {
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000650 // Each module flag should have three arguments, the merge behavior (a
651 // constant int), the flag ID (an MDString), and the value.
652 Assert1(Op->getNumOperands() == 3,
653 "incorrect number of operands in module flag", Op);
654 ConstantInt *Behavior = dyn_cast<ConstantInt>(Op->getOperand(0));
655 MDString *ID = dyn_cast<MDString>(Op->getOperand(1));
656 Assert1(Behavior,
657 "invalid behavior operand in module flag (expected constant integer)",
658 Op->getOperand(0));
659 unsigned BehaviorValue = Behavior->getZExtValue();
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000660 Assert1(ID,
661 "invalid ID operand in module flag (expected metadata string)",
662 Op->getOperand(1));
663
Daniel Dunbar5db391c2013-01-16 21:38:56 +0000664 // Sanity check the values for behaviors with additional requirements.
665 switch (BehaviorValue) {
666 default:
667 Assert1(false,
668 "invalid behavior operand in module flag (unexpected constant)",
669 Op->getOperand(0));
670 break;
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000671
Daniel Dunbar5db391c2013-01-16 21:38:56 +0000672 case Module::Error:
673 case Module::Warning:
674 case Module::Override:
675 // These behavior types accept any value.
676 break;
677
678 case Module::Require: {
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000679 // The value should itself be an MDNode with two operands, a flag ID (an
680 // MDString), and a value.
681 MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2));
682 Assert1(Value && Value->getNumOperands() == 2,
683 "invalid value for 'require' module flag (expected metadata pair)",
684 Op->getOperand(2));
685 Assert1(isa<MDString>(Value->getOperand(0)),
686 ("invalid value for 'require' module flag "
687 "(first value operand should be a string)"),
688 Value->getOperand(0));
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000689
690 // Append it to the list of requirements, to check once all module flags are
691 // scanned.
692 Requirements.push_back(Value);
Daniel Dunbar5db391c2013-01-16 21:38:56 +0000693 break;
694 }
695
696 case Module::Append:
697 case Module::AppendUnique: {
698 // These behavior types require the operand be an MDNode.
699 Assert1(isa<MDNode>(Op->getOperand(2)),
700 "invalid value for 'append'-type module flag "
701 "(expected a metadata node)", Op->getOperand(2));
702 break;
703 }
704 }
705
706 // Unless this is a "requires" flag, check the ID is unique.
707 if (BehaviorValue != Module::Require) {
708 bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
709 Assert1(Inserted,
710 "module flag identifiers must be unique (or of 'require' type)",
711 ID);
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000712 }
713}
714
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000715void Verifier::VerifyAttributeTypes(AttributeSet Attrs, unsigned Idx,
Nick Lewyckydc897372013-07-06 00:29:58 +0000716 bool isFunction, const Value *V) {
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000717 unsigned Slot = ~0U;
718 for (unsigned I = 0, E = Attrs.getNumSlots(); I != E; ++I)
719 if (Attrs.getSlotIndex(I) == Idx) {
720 Slot = I;
721 break;
722 }
723
724 assert(Slot != ~0U && "Attribute set inconsistency!");
725
726 for (AttributeSet::iterator I = Attrs.begin(Slot), E = Attrs.end(Slot);
727 I != E; ++I) {
728 if (I->isStringAttribute())
729 continue;
730
731 if (I->getKindAsEnum() == Attribute::NoReturn ||
732 I->getKindAsEnum() == Attribute::NoUnwind ||
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000733 I->getKindAsEnum() == Attribute::NoInline ||
734 I->getKindAsEnum() == Attribute::AlwaysInline ||
735 I->getKindAsEnum() == Attribute::OptimizeForSize ||
736 I->getKindAsEnum() == Attribute::StackProtect ||
737 I->getKindAsEnum() == Attribute::StackProtectReq ||
738 I->getKindAsEnum() == Attribute::StackProtectStrong ||
739 I->getKindAsEnum() == Attribute::NoRedZone ||
740 I->getKindAsEnum() == Attribute::NoImplicitFloat ||
741 I->getKindAsEnum() == Attribute::Naked ||
742 I->getKindAsEnum() == Attribute::InlineHint ||
743 I->getKindAsEnum() == Attribute::StackAlignment ||
744 I->getKindAsEnum() == Attribute::UWTable ||
745 I->getKindAsEnum() == Attribute::NonLazyBind ||
746 I->getKindAsEnum() == Attribute::ReturnsTwice ||
747 I->getKindAsEnum() == Attribute::SanitizeAddress ||
748 I->getKindAsEnum() == Attribute::SanitizeThread ||
749 I->getKindAsEnum() == Attribute::SanitizeMemory ||
750 I->getKindAsEnum() == Attribute::MinSize ||
751 I->getKindAsEnum() == Attribute::NoDuplicate ||
Michael Gottesman2253a2f2013-06-27 00:25:01 +0000752 I->getKindAsEnum() == Attribute::Builtin ||
Diego Novillo77226a02013-05-24 12:26:52 +0000753 I->getKindAsEnum() == Attribute::NoBuiltin ||
754 I->getKindAsEnum() == Attribute::Cold) {
Tobias Grosser068acc52013-07-02 03:28:10 +0000755 if (!isFunction) {
Nick Lewyckydc897372013-07-06 00:29:58 +0000756 CheckFailed("Attribute '" + I->getAsString() +
757 "' only applies to functions!", V);
758 return;
759 }
760 } else if (I->getKindAsEnum() == Attribute::ReadOnly ||
761 I->getKindAsEnum() == Attribute::ReadNone) {
762 if (Idx == 0) {
763 CheckFailed("Attribute '" + I->getAsString() +
764 "' does not apply to function returns");
765 return;
Tobias Grosser068acc52013-07-02 03:28:10 +0000766 }
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000767 } else if (isFunction) {
Nick Lewyckydc897372013-07-06 00:29:58 +0000768 CheckFailed("Attribute '" + I->getAsString() +
769 "' does not apply to functions!", V);
770 return;
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000771 }
772 }
773}
774
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000775// VerifyParameterAttrs - Check the given attributes for an argument or return
Duncan Sandscfad1b42008-01-12 16:42:01 +0000776// value of the specified type. The value V is printed in error messages.
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000777void Verifier::VerifyParameterAttrs(AttributeSet Attrs, unsigned Idx, Type *Ty,
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000778 bool isReturnValue, const Value *V) {
Bill Wendlingbed80592013-01-21 23:03:18 +0000779 if (!Attrs.hasAttributes(Idx))
Duncan Sandscfad1b42008-01-12 16:42:01 +0000780 return;
781
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000782 VerifyAttributeTypes(Attrs, Idx, false, V);
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000783
Bill Wendling943c2912012-10-09 09:51:10 +0000784 if (isReturnValue)
Bill Wendlingbed80592013-01-21 23:03:18 +0000785 Assert1(!Attrs.hasAttribute(Idx, Attribute::ByVal) &&
786 !Attrs.hasAttribute(Idx, Attribute::Nest) &&
787 !Attrs.hasAttribute(Idx, Attribute::StructRet) &&
Stephen Lin456ca042013-04-20 05:14:40 +0000788 !Attrs.hasAttribute(Idx, Attribute::NoCapture) &&
789 !Attrs.hasAttribute(Idx, Attribute::Returned),
790 "Attribute 'byval', 'nest', 'sret', 'nocapture', and 'returned' "
Bill Wendling943c2912012-10-09 09:51:10 +0000791 "do not apply to return values!", V);
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000792
Bill Wendling28d1c602012-10-09 20:11:19 +0000793 // Check for mutually incompatible attributes.
Bill Wendlingbed80592013-01-21 23:03:18 +0000794 Assert1(!((Attrs.hasAttribute(Idx, Attribute::ByVal) &&
795 Attrs.hasAttribute(Idx, Attribute::Nest)) ||
796 (Attrs.hasAttribute(Idx, Attribute::ByVal) &&
797 Attrs.hasAttribute(Idx, Attribute::StructRet)) ||
798 (Attrs.hasAttribute(Idx, Attribute::Nest) &&
799 Attrs.hasAttribute(Idx, Attribute::StructRet))), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000800 "'byval, nest, and sret' are incompatible!", V);
801
Bill Wendlingbed80592013-01-21 23:03:18 +0000802 Assert1(!((Attrs.hasAttribute(Idx, Attribute::ByVal) &&
803 Attrs.hasAttribute(Idx, Attribute::Nest)) ||
804 (Attrs.hasAttribute(Idx, Attribute::ByVal) &&
805 Attrs.hasAttribute(Idx, Attribute::InReg)) ||
806 (Attrs.hasAttribute(Idx, Attribute::Nest) &&
807 Attrs.hasAttribute(Idx, Attribute::InReg))), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000808 "'byval, nest, and inreg' are incompatible!", V);
809
Stephen Lin13aba142013-04-23 16:31:56 +0000810 Assert1(!(Attrs.hasAttribute(Idx, Attribute::StructRet) &&
811 Attrs.hasAttribute(Idx, Attribute::Returned)), "Attributes "
812 "'sret and returned' are incompatible!", V);
813
Bill Wendlingbed80592013-01-21 23:03:18 +0000814 Assert1(!(Attrs.hasAttribute(Idx, Attribute::ZExt) &&
815 Attrs.hasAttribute(Idx, Attribute::SExt)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000816 "'zeroext and signext' are incompatible!", V);
817
Bill Wendlingbed80592013-01-21 23:03:18 +0000818 Assert1(!(Attrs.hasAttribute(Idx, Attribute::ReadNone) &&
819 Attrs.hasAttribute(Idx, Attribute::ReadOnly)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000820 "'readnone and readonly' are incompatible!", V);
821
Bill Wendlingbed80592013-01-21 23:03:18 +0000822 Assert1(!(Attrs.hasAttribute(Idx, Attribute::NoInline) &&
823 Attrs.hasAttribute(Idx, Attribute::AlwaysInline)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000824 "'noinline and alwaysinline' are incompatible!", V);
Duncan Sandscfad1b42008-01-12 16:42:01 +0000825
Bill Wendlingbed80592013-01-21 23:03:18 +0000826 Assert1(!AttrBuilder(Attrs, Idx).
Bill Wendlinge7436542013-01-30 23:07:40 +0000827 hasAttributes(AttributeFuncs::typeIncompatible(Ty, Idx), Idx),
Bill Wendling1feacad2012-10-14 07:52:48 +0000828 "Wrong types for attribute: " +
Bill Wendlinge7436542013-01-30 23:07:40 +0000829 AttributeFuncs::typeIncompatible(Ty, Idx).getAsString(Idx), V);
Dan Gohman39dfc2c2008-08-27 14:48:06 +0000830
Bill Wendling943c2912012-10-09 09:51:10 +0000831 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
Bill Wendlingbed80592013-01-21 23:03:18 +0000832 Assert1(!Attrs.hasAttribute(Idx, Attribute::ByVal) ||
Bill Wendling943c2912012-10-09 09:51:10 +0000833 PTy->getElementType()->isSized(),
834 "Attribute 'byval' does not support unsized types!", V);
835 else
Bill Wendlingbed80592013-01-21 23:03:18 +0000836 Assert1(!Attrs.hasAttribute(Idx, Attribute::ByVal),
Bill Wendling943c2912012-10-09 09:51:10 +0000837 "Attribute 'byval' only applies to parameters with pointer type!",
838 V);
Duncan Sandscfad1b42008-01-12 16:42:01 +0000839}
840
841// VerifyFunctionAttrs - Check parameter attributes against a function type.
Duncan Sandsd9d70392007-12-21 19:19:01 +0000842// The value V is printed in error messages.
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000843void Verifier::VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs,
Duncan Sandscfad1b42008-01-12 16:42:01 +0000844 const Value *V) {
Chris Lattner58d74912008-03-12 17:45:29 +0000845 if (Attrs.isEmpty())
Duncan Sandsd9d70392007-12-21 19:19:01 +0000846 return;
847
Duncan Sandsd9d70392007-12-21 19:19:01 +0000848 bool SawNest = false;
Stephen Lin456ca042013-04-20 05:14:40 +0000849 bool SawReturned = false;
Duncan Sandsd9d70392007-12-21 19:19:01 +0000850
Chris Lattner58d74912008-03-12 17:45:29 +0000851 for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000852 unsigned Idx = Attrs.getSlotIndex(i);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000853
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000854 Type *Ty;
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000855 if (Idx == 0)
Chris Lattner58d74912008-03-12 17:45:29 +0000856 Ty = FT->getReturnType();
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000857 else if (Idx-1 < FT->getNumParams())
858 Ty = FT->getParamType(Idx-1);
Chris Lattner58d74912008-03-12 17:45:29 +0000859 else
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000860 break; // VarArgs attributes, verified elsewhere.
861
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000862 VerifyParameterAttrs(Attrs, Idx, Ty, Idx == 0, V);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000863
Stephen Lin456ca042013-04-20 05:14:40 +0000864 if (Idx == 0)
865 continue;
866
867 if (Attrs.hasAttribute(Idx, Attribute::Nest)) {
Duncan Sandsd9d70392007-12-21 19:19:01 +0000868 Assert1(!SawNest, "More than one parameter has attribute nest!", V);
869 SawNest = true;
870 }
871
Stephen Lin456ca042013-04-20 05:14:40 +0000872 if (Attrs.hasAttribute(Idx, Attribute::Returned)) {
873 Assert1(!SawReturned, "More than one parameter has attribute returned!",
874 V);
875 Assert1(Ty->canLosslesslyBitCastTo(FT->getReturnType()), "Incompatible "
876 "argument and return types for 'returned' attribute", V);
877 SawReturned = true;
878 }
879
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000880 if (Attrs.hasAttribute(Idx, Attribute::StructRet))
881 Assert1(Idx == 1, "Attribute sret is not on first parameter!", V);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000882 }
Devang Patel7c310852008-10-01 23:41:25 +0000883
Bill Wendling956f1342013-01-18 21:11:39 +0000884 if (!Attrs.hasAttributes(AttributeSet::FunctionIndex))
885 return;
886
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000887 VerifyAttributeTypes(Attrs, AttributeSet::FunctionIndex, true, V);
Bill Wendling28d1c602012-10-09 20:11:19 +0000888
Bill Wendling956f1342013-01-18 21:11:39 +0000889 Assert1(!(Attrs.hasAttribute(AttributeSet::FunctionIndex,
890 Attribute::ReadNone) &&
891 Attrs.hasAttribute(AttributeSet::FunctionIndex,
892 Attribute::ReadOnly)),
893 "Attributes 'readnone and readonly' are incompatible!", V);
Bill Wendling28d1c602012-10-09 20:11:19 +0000894
Bill Wendling956f1342013-01-18 21:11:39 +0000895 Assert1(!(Attrs.hasAttribute(AttributeSet::FunctionIndex,
896 Attribute::NoInline) &&
897 Attrs.hasAttribute(AttributeSet::FunctionIndex,
898 Attribute::AlwaysInline)),
899 "Attributes 'noinline and alwaysinline' are incompatible!", V);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000900}
901
Matt Arsenault16e4ed52013-07-31 17:49:08 +0000902void Verifier::VerifyBitcastType(const Value *V, Type *DestTy, Type *SrcTy) {
903 // Get the size of the types in bits, we'll need this later
904 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
905 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
906
907 // BitCast implies a no-op cast of type only. No bits change.
908 // However, you can't cast pointers to anything but pointers.
909 Assert1(SrcTy->isPointerTy() == DestTy->isPointerTy(),
910 "Bitcast requires both operands to be pointer or neither", V);
911 Assert1(SrcBitSize == DestBitSize,
912 "Bitcast requires types of same width", V);
913
914 // Disallow aggregates.
915 Assert1(!SrcTy->isAggregateType(),
916 "Bitcast operand must not be aggregate", V);
917 Assert1(!DestTy->isAggregateType(),
918 "Bitcast type must not be aggregate", V);
919
920 // Without datalayout, assume all address spaces are the same size.
921 // Don't check if both types are not pointers.
922 // Skip casts between scalars and vectors.
923 if (!DL ||
924 !SrcTy->isPtrOrPtrVectorTy() ||
925 !DestTy->isPtrOrPtrVectorTy() ||
926 SrcTy->isVectorTy() != DestTy->isVectorTy()) {
927 return;
928 }
929
930 unsigned SrcAS = SrcTy->getPointerAddressSpace();
931 unsigned DstAS = DestTy->getPointerAddressSpace();
932
933 unsigned SrcASSize = DL->getPointerSizeInBits(SrcAS);
934 unsigned DstASSize = DL->getPointerSizeInBits(DstAS);
935 Assert1(SrcASSize == DstASSize,
936 "Bitcasts between pointers of different address spaces must have "
937 "the same size pointers, otherwise use PtrToInt/IntToPtr.", V);
938}
939
940void Verifier::VerifyConstantExprBitcastType(const ConstantExpr *CE) {
941 if (CE->getOpcode() == Instruction::BitCast) {
942 Type *SrcTy = CE->getOperand(0)->getType();
943 Type *DstTy = CE->getType();
944 VerifyBitcastType(CE, DstTy, SrcTy);
945 }
946}
947
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000948bool Verifier::VerifyAttributeCount(AttributeSet Attrs, unsigned Params) {
Bill Wendling254aed52013-01-30 06:54:41 +0000949 if (Attrs.getNumSlots() == 0)
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000950 return true;
Nick Lewycky29ef6592009-09-07 20:44:51 +0000951
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000952 unsigned LastSlot = Attrs.getNumSlots() - 1;
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000953 unsigned LastIndex = Attrs.getSlotIndex(LastSlot);
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000954 if (LastIndex <= Params
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000955 || (LastIndex == AttributeSet::FunctionIndex
956 && (LastSlot == 0 || Attrs.getSlotIndex(LastSlot - 1) <= Params)))
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000957 return true;
Matt Arsenault3f493222013-07-20 17:46:00 +0000958
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000959 return false;
960}
Nick Lewycky29ef6592009-09-07 20:44:51 +0000961
Chris Lattnerd231fc32002-04-18 20:37:37 +0000962// visitFunction - Verify that a function is ok.
Chris Lattner44d5bd92002-02-20 17:55:43 +0000963//
Chris Lattner24e845f2002-06-25 15:56:27 +0000964void Verifier::visitFunction(Function &F) {
Chris Lattner37c121a2005-05-08 22:27:09 +0000965 // Check function arguments.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000966 FunctionType *FT = F.getFunctionType();
Chris Lattner453eed12007-08-18 06:13:19 +0000967 unsigned NumArgs = F.arg_size();
Chris Lattnerea249242002-04-13 22:48:46 +0000968
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000969 Assert1(Context == &F.getContext(),
970 "Function context does not match Module context!", &F);
971
Chris Lattner26d054d2009-08-05 05:21:07 +0000972 Assert1(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
Chris Lattner69da5cf2002-10-13 20:57:00 +0000973 Assert2(FT->getNumParams() == NumArgs,
Chris Lattnerea249242002-04-13 22:48:46 +0000974 "# formal arguments must match # of arguments for function type!",
Chris Lattner24e845f2002-06-25 15:56:27 +0000975 &F, FT);
Chris Lattnerc282f5a2003-11-21 22:32:23 +0000976 Assert1(F.getReturnType()->isFirstClassType() ||
Matt Arsenault3f493222013-07-20 17:46:00 +0000977 F.getReturnType()->isVoidTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000978 F.getReturnType()->isStructTy(),
Chris Lattnerc282f5a2003-11-21 22:32:23 +0000979 "Functions cannot return aggregate values!", &F);
Chris Lattnerea249242002-04-13 22:48:46 +0000980
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000981 Assert1(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
Devang Patel41e23972008-03-03 21:46:28 +0000982 "Invalid struct return type!", &F);
983
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000984 AttributeSet Attrs = F.getAttributes();
Duncan Sands623a3892008-01-11 22:36:48 +0000985
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000986 Assert1(VerifyAttributeCount(Attrs, FT->getNumParams()),
Bill Wendling034b94b2012-12-19 07:18:57 +0000987 "Attribute after last parameter!", &F);
Duncan Sands623a3892008-01-11 22:36:48 +0000988
Duncan Sandsd9d70392007-12-21 19:19:01 +0000989 // Check function attributes.
Duncan Sandscfad1b42008-01-12 16:42:01 +0000990 VerifyFunctionAttrs(FT, Attrs, &F);
Duncan Sandsfdef00f2007-07-27 15:09:54 +0000991
Michael Gottesman2253a2f2013-06-27 00:25:01 +0000992 // On function declarations/definitions, we do not support the builtin
993 // attribute. We do not check this in VerifyFunctionAttrs since that is
994 // checking for Attributes that can/can not ever be on functions.
995 Assert1(!Attrs.hasAttribute(AttributeSet::FunctionIndex,
996 Attribute::Builtin),
997 "Attribute 'builtin' can only be applied to a callsite.", &F);
998
Chris Lattner80105dd2006-05-19 21:25:17 +0000999 // Check that this function meets the restrictions on this calling convention.
1000 switch (F.getCallingConv()) {
1001 default:
1002 break;
1003 case CallingConv::C:
1004 break;
Chris Lattner80105dd2006-05-19 21:25:17 +00001005 case CallingConv::Fast:
1006 case CallingConv::Cold:
Anton Korobeynikovbcb97702006-09-17 20:25:45 +00001007 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +00001008 case CallingConv::X86_ThisCall:
Elena Demikhovsky35752222012-10-24 14:46:16 +00001009 case CallingConv::Intel_OCL_BI:
Che-Liang Chiouf9930da2010-09-25 07:46:17 +00001010 case CallingConv::PTX_Kernel:
1011 case CallingConv::PTX_Device:
Chris Lattner80105dd2006-05-19 21:25:17 +00001012 Assert1(!F.isVarArg(),
1013 "Varargs functions must have C calling conventions!", &F);
1014 break;
1015 }
Nick Lewycky29ef6592009-09-07 20:44:51 +00001016
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001017 bool isLLVMdotName = F.getName().size() >= 5 &&
1018 F.getName().substr(0, 5) == "llvm.";
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001019
Chris Lattnerea249242002-04-13 22:48:46 +00001020 // Check that the argument values match the function type for this function...
Chris Lattner69da5cf2002-10-13 20:57:00 +00001021 unsigned i = 0;
Chris Lattnere3cbe032006-12-16 02:25:35 +00001022 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
1023 I != E; ++I, ++i) {
Chris Lattner69da5cf2002-10-13 20:57:00 +00001024 Assert2(I->getType() == FT->getParamType(i),
1025 "Argument value does not match function argument type!",
1026 I, FT->getParamType(i));
Misha Brukmanfd939082005-04-21 23:48:37 +00001027 Assert1(I->getType()->isFirstClassType(),
Dan Gohman9f50eee2008-08-27 14:44:57 +00001028 "Function arguments must have first-class types!", I);
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001029 if (!isLLVMdotName)
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001030 Assert2(!I->getType()->isMetadataTy(),
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001031 "Function takes metadata but isn't an intrinsic", I, &F);
Dan Gohman9f50eee2008-08-27 14:44:57 +00001032 }
Chris Lattnerea249242002-04-13 22:48:46 +00001033
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001034 if (F.isMaterializable()) {
1035 // Function has a body somewhere we can't see.
1036 } else if (F.isDeclaration()) {
Chris Lattner6693da02007-09-19 17:14:45 +00001037 Assert1(F.hasExternalLinkage() || F.hasDLLImportLinkage() ||
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00001038 F.hasExternalWeakLinkage(),
Chris Lattner6693da02007-09-19 17:14:45 +00001039 "invalid linkage type for function declaration", &F);
1040 } else {
Chris Lattner4d17caa2006-12-13 04:45:46 +00001041 // Verify that this function (which has a body) is not named "llvm.*". It
1042 // is not legal to define intrinsics.
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001043 Assert1(!isLLVMdotName, "llvm intrinsics cannot be defined!", &F);
Matt Arsenault3f493222013-07-20 17:46:00 +00001044
Chris Lattner69da5cf2002-10-13 20:57:00 +00001045 // Check the entry node
Chris Lattner02a3be02003-09-20 14:39:18 +00001046 BasicBlock *Entry = &F.getEntryBlock();
Chris Lattner69da5cf2002-10-13 20:57:00 +00001047 Assert1(pred_begin(Entry) == pred_end(Entry),
1048 "Entry block to function must not have predecessors!", Entry);
Matt Arsenault3f493222013-07-20 17:46:00 +00001049
Chris Lattner660a4f32009-11-01 04:08:01 +00001050 // The address of the entry block cannot be taken, unless it is dead.
1051 if (Entry->hasAddressTaken()) {
Chris Lattner4a7642e2009-11-01 18:11:50 +00001052 Assert1(!BlockAddress::get(Entry)->isConstantUsed(),
Chris Lattner660a4f32009-11-01 04:08:01 +00001053 "blockaddress may not be used with the entry block!", Entry);
1054 }
Chris Lattner69da5cf2002-10-13 20:57:00 +00001055 }
Matt Arsenault3f493222013-07-20 17:46:00 +00001056
Chris Lattner13202de2009-09-11 17:05:29 +00001057 // If this function is actually an intrinsic, verify that it is only used in
1058 // direct call/invokes, never having its "address taken".
1059 if (F.getIntrinsicID()) {
Gabor Greifc9f75002010-03-24 13:21:49 +00001060 const User *U;
1061 if (F.hasAddressTaken(&U))
Matt Arsenault3f493222013-07-20 17:46:00 +00001062 Assert1(0, "Invalid user of intrinsic instruction!", U);
Chris Lattner13202de2009-09-11 17:05:29 +00001063 }
Chris Lattner44d5bd92002-02-20 17:55:43 +00001064}
1065
Chris Lattnerd231fc32002-04-18 20:37:37 +00001066// verifyBasicBlock - Verify that a basic block is well formed...
1067//
Chris Lattner24e845f2002-06-25 15:56:27 +00001068void Verifier::visitBasicBlock(BasicBlock &BB) {
Chris Lattnera7b1c7e2004-09-29 20:07:45 +00001069 InstsInThisBlock.clear();
1070
Alkis Evlogimenos4f4cf992004-12-04 02:30:42 +00001071 // Ensure that basic blocks have terminators!
1072 Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
1073
Chris Lattnerbede31f2003-10-05 17:44:18 +00001074 // Check constraints that this basic block imposes on all of the PHI nodes in
1075 // it.
1076 if (isa<PHINode>(BB.front())) {
Chris Lattnerf8edb622007-02-10 08:33:11 +00001077 SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB));
1078 SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
Chris Lattnerbede31f2003-10-05 17:44:18 +00001079 std::sort(Preds.begin(), Preds.end());
Misha Brukmanfd939082005-04-21 23:48:37 +00001080 PHINode *PN;
Chris Lattnerc70a5092004-06-05 17:44:48 +00001081 for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) {
Chris Lattnerbede31f2003-10-05 17:44:18 +00001082 // Ensure that PHI nodes have at least one entry!
1083 Assert1(PN->getNumIncomingValues() != 0,
1084 "PHI nodes must have at least one entry. If the block is dead, "
1085 "the PHI should be removed!", PN);
Brian Gaeke2fea9ad2004-05-17 21:15:18 +00001086 Assert1(PN->getNumIncomingValues() == Preds.size(),
1087 "PHINode should have one entry for each predecessor of its "
1088 "parent basic block!", PN);
Misha Brukmanfd939082005-04-21 23:48:37 +00001089
Chris Lattnerbede31f2003-10-05 17:44:18 +00001090 // Get and sort all incoming values in the PHI node...
Chris Lattnerf8edb622007-02-10 08:33:11 +00001091 Values.clear();
Chris Lattnerbede31f2003-10-05 17:44:18 +00001092 Values.reserve(PN->getNumIncomingValues());
1093 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1094 Values.push_back(std::make_pair(PN->getIncomingBlock(i),
1095 PN->getIncomingValue(i)));
1096 std::sort(Values.begin(), Values.end());
Misha Brukmanfd939082005-04-21 23:48:37 +00001097
Chris Lattnerbede31f2003-10-05 17:44:18 +00001098 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1099 // Check to make sure that if there is more than one entry for a
1100 // particular basic block in this PHI node, that the incoming values are
1101 // all identical.
1102 //
1103 Assert4(i == 0 || Values[i].first != Values[i-1].first ||
1104 Values[i].second == Values[i-1].second,
1105 "PHI node has multiple entries for the same basic block with "
1106 "different incoming values!", PN, Values[i].first,
1107 Values[i].second, Values[i-1].second);
Misha Brukmanfd939082005-04-21 23:48:37 +00001108
Chris Lattnerbede31f2003-10-05 17:44:18 +00001109 // Check to make sure that the predecessors and PHI node entries are
1110 // matched up.
1111 Assert3(Values[i].first == Preds[i],
1112 "PHI node entries do not match predecessors!", PN,
Misha Brukmanfd939082005-04-21 23:48:37 +00001113 Values[i].first, Preds[i]);
Chris Lattnerbede31f2003-10-05 17:44:18 +00001114 }
1115 }
1116 }
Chris Lattner24e845f2002-06-25 15:56:27 +00001117}
Chris Lattneracd3cae2002-03-15 20:25:09 +00001118
Chris Lattner24e845f2002-06-25 15:56:27 +00001119void Verifier::visitTerminatorInst(TerminatorInst &I) {
1120 // Ensure that terminators only exist at the end of the basic block.
1121 Assert1(&I == I.getParent()->getTerminator(),
1122 "Terminator found in the middle of a basic block!", I.getParent());
Chris Lattner3535c9b2002-07-18 00:13:42 +00001123 visitInstruction(I);
Chris Lattner24e845f2002-06-25 15:56:27 +00001124}
1125
Nick Lewycky6a7cb632010-02-15 22:09:09 +00001126void Verifier::visitBranchInst(BranchInst &BI) {
1127 if (BI.isConditional()) {
1128 Assert2(BI.getCondition()->getType()->isIntegerTy(1),
1129 "Branch condition is not 'i1' type!", &BI, BI.getCondition());
1130 }
1131 visitTerminatorInst(BI);
1132}
1133
Chris Lattner24e845f2002-06-25 15:56:27 +00001134void Verifier::visitReturnInst(ReturnInst &RI) {
1135 Function *F = RI.getParent()->getParent();
Devang Patel57ef4f42008-02-23 00:35:18 +00001136 unsigned N = RI.getNumOperands();
Matt Arsenault3f493222013-07-20 17:46:00 +00001137 if (F->getReturnType()->isVoidTy())
Chris Lattner80b8f5d2008-04-23 20:33:41 +00001138 Assert2(N == 0,
Nick Lewycky70c44f02008-11-15 17:50:47 +00001139 "Found return instr that returns non-void in Function of void "
Alkis Evlogimenos8b42b432004-12-04 01:25:06 +00001140 "return type!", &RI, F->getReturnType());
Jay Foad3e2f74e2011-04-04 07:44:02 +00001141 else
1142 Assert2(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
1143 "Function return type does not match operand "
1144 "type of return inst!", &RI, F->getReturnType());
Nick Lewycky29ef6592009-09-07 20:44:51 +00001145
Misha Brukman5560c9d2003-08-18 14:43:39 +00001146 // Check to make sure that the return value has necessary properties for
Chris Lattner24e845f2002-06-25 15:56:27 +00001147 // terminators...
1148 visitTerminatorInst(RI);
Chris Lattner44d5bd92002-02-20 17:55:43 +00001149}
1150
Chris Lattner0f9e9d02004-05-21 16:47:21 +00001151void Verifier::visitSwitchInst(SwitchInst &SI) {
1152 // Check to make sure that all of the constants in the switch instruction
1153 // have the same type as the switched-on value.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001154 Type *SwitchTy = SI.getCondition()->getType();
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00001155 IntegerType *IntTy = cast<IntegerType>(SwitchTy);
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00001156 IntegersSubsetToBB Mapping;
1157 std::map<IntegersSubset::Range, unsigned> RangeSetMap;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00001158 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); i != e; ++i) {
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00001159 IntegersSubset CaseRanges = i.getCaseValueEx();
1160 for (unsigned ri = 0, rie = CaseRanges.getNumItems(); ri < rie; ++ri) {
1161 IntegersSubset::Range r = CaseRanges.getItem(ri);
Stepan Dyatkovskiy43eb31b2012-06-02 09:42:43 +00001162 Assert1(((const APInt&)r.getLow()).getBitWidth() == IntTy->getBitWidth(),
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001163 "Switch constants must all be same type as switch value!", &SI);
Stepan Dyatkovskiy43eb31b2012-06-02 09:42:43 +00001164 Assert1(((const APInt&)r.getHigh()).getBitWidth() == IntTy->getBitWidth(),
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001165 "Switch constants must all be same type as switch value!", &SI);
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00001166 Mapping.add(r);
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001167 RangeSetMap[r] = i.getCaseIndex();
1168 }
Chris Lattnerd682a602009-11-11 17:37:02 +00001169 }
Matt Arsenault3f493222013-07-20 17:46:00 +00001170
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00001171 IntegersSubsetToBB::RangeIterator errItem;
1172 if (!Mapping.verify(errItem)) {
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001173 unsigned CaseIndex = RangeSetMap[errItem->first];
1174 SwitchInst::CaseIt i(&SI, CaseIndex);
1175 Assert2(false, "Duplicate integer as switch case", &SI, i.getCaseValueEx());
1176 }
Matt Arsenault3f493222013-07-20 17:46:00 +00001177
Chris Lattner0f9e9d02004-05-21 16:47:21 +00001178 visitTerminatorInst(SI);
1179}
1180
Dan Gohman37680912010-08-02 23:08:33 +00001181void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
1182 Assert1(BI.getAddress()->getType()->isPointerTy(),
1183 "Indirectbr operand must have pointer type!", &BI);
1184 for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
1185 Assert1(BI.getDestination(i)->getType()->isLabelTy(),
1186 "Indirectbr destinations must all have pointer type!", &BI);
1187
1188 visitTerminatorInst(BI);
1189}
1190
Chris Lattner230c1a72004-03-12 05:54:31 +00001191void Verifier::visitSelectInst(SelectInst &SI) {
Chris Lattnerb76ec322008-12-29 00:12:50 +00001192 Assert1(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
1193 SI.getOperand(2)),
1194 "Invalid operands for select instruction!", &SI);
1195
Chris Lattner230c1a72004-03-12 05:54:31 +00001196 Assert1(SI.getTrueValue()->getType() == SI.getType(),
1197 "Select values must have same type as select instruction!", &SI);
Chris Lattner0030e6c2004-09-29 21:19:28 +00001198 visitInstruction(SI);
Chris Lattner230c1a72004-03-12 05:54:31 +00001199}
1200
Misha Brukmanab5c6002004-03-02 00:22:19 +00001201/// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
1202/// a pass, if any exist, it's an error.
1203///
Chris Lattner627079d2002-11-21 16:54:22 +00001204void Verifier::visitUserOp1(Instruction &I) {
Chris Lattner536a9d52006-03-31 04:46:47 +00001205 Assert1(0, "User-defined operators should not live outside of a pass!", &I);
Chris Lattner627079d2002-11-21 16:54:22 +00001206}
Chris Lattnerd231fc32002-04-18 20:37:37 +00001207
Reid Spencer3da59db2006-11-27 01:05:10 +00001208void Verifier::visitTruncInst(TruncInst &I) {
1209 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001210 Type *SrcTy = I.getOperand(0)->getType();
1211 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001212
1213 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001214 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1215 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001216
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001217 Assert1(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
1218 Assert1(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001219 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001220 "trunc source and destination must both be a vector or neither", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001221 Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I);
1222
1223 visitInstruction(I);
1224}
1225
1226void Verifier::visitZExtInst(ZExtInst &I) {
1227 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001228 Type *SrcTy = I.getOperand(0)->getType();
1229 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001230
1231 // Get the size of the types in bits, we'll need this later
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001232 Assert1(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
1233 Assert1(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001234 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001235 "zext source and destination must both be a vector or neither", &I);
Dan Gohman6de29f82009-06-15 22:12:54 +00001236 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1237 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001238
Reid Spencer3da59db2006-11-27 01:05:10 +00001239 Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I);
1240
1241 visitInstruction(I);
1242}
1243
1244void Verifier::visitSExtInst(SExtInst &I) {
1245 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001246 Type *SrcTy = I.getOperand(0)->getType();
1247 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001248
1249 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001250 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1251 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001252
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001253 Assert1(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
1254 Assert1(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001255 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001256 "sext source and destination must both be a vector or neither", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001257 Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I);
1258
1259 visitInstruction(I);
1260}
1261
1262void Verifier::visitFPTruncInst(FPTruncInst &I) {
1263 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001264 Type *SrcTy = I.getOperand(0)->getType();
1265 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001266 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001267 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1268 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001269
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001270 Assert1(SrcTy->isFPOrFPVectorTy(),"FPTrunc only operates on FP", &I);
1271 Assert1(DestTy->isFPOrFPVectorTy(),"FPTrunc only produces an FP", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001272 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001273 "fptrunc source and destination must both be a vector or neither",&I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001274 Assert1(SrcBitSize > DestBitSize,"DestTy too big for FPTrunc", &I);
1275
1276 visitInstruction(I);
1277}
1278
1279void Verifier::visitFPExtInst(FPExtInst &I) {
1280 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001281 Type *SrcTy = I.getOperand(0)->getType();
1282 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001283
1284 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001285 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1286 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001287
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001288 Assert1(SrcTy->isFPOrFPVectorTy(),"FPExt only operates on FP", &I);
1289 Assert1(DestTy->isFPOrFPVectorTy(),"FPExt only produces an FP", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001290 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001291 "fpext source and destination must both be a vector or neither", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001292 Assert1(SrcBitSize < DestBitSize,"DestTy too small for FPExt", &I);
1293
1294 visitInstruction(I);
1295}
1296
1297void Verifier::visitUIToFPInst(UIToFPInst &I) {
1298 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001299 Type *SrcTy = I.getOperand(0)->getType();
1300 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001301
Duncan Sands1df98592010-02-16 11:11:14 +00001302 bool SrcVec = SrcTy->isVectorTy();
1303 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001304
Chris Lattner58d74912008-03-12 17:45:29 +00001305 Assert1(SrcVec == DstVec,
1306 "UIToFP source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001307 Assert1(SrcTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001308 "UIToFP source must be integer or integer vector", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001309 Assert1(DestTy->isFPOrFPVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001310 "UIToFP result must be FP or FP vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001311
1312 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001313 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1314 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001315 "UIToFP source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001316
1317 visitInstruction(I);
1318}
1319
1320void Verifier::visitSIToFPInst(SIToFPInst &I) {
1321 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001322 Type *SrcTy = I.getOperand(0)->getType();
1323 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001324
Duncan Sands1df98592010-02-16 11:11:14 +00001325 bool SrcVec = SrcTy->isVectorTy();
1326 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001327
Chris Lattner58d74912008-03-12 17:45:29 +00001328 Assert1(SrcVec == DstVec,
1329 "SIToFP source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001330 Assert1(SrcTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001331 "SIToFP source must be integer or integer vector", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001332 Assert1(DestTy->isFPOrFPVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001333 "SIToFP result must be FP or FP vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001334
1335 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001336 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1337 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001338 "SIToFP source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001339
1340 visitInstruction(I);
1341}
1342
1343void Verifier::visitFPToUIInst(FPToUIInst &I) {
1344 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001345 Type *SrcTy = I.getOperand(0)->getType();
1346 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001347
Duncan Sands1df98592010-02-16 11:11:14 +00001348 bool SrcVec = SrcTy->isVectorTy();
1349 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001350
Chris Lattner58d74912008-03-12 17:45:29 +00001351 Assert1(SrcVec == DstVec,
1352 "FPToUI source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001353 Assert1(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector",
1354 &I);
1355 Assert1(DestTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001356 "FPToUI result must be integer or integer vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001357
1358 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001359 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1360 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001361 "FPToUI source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001362
1363 visitInstruction(I);
1364}
1365
1366void Verifier::visitFPToSIInst(FPToSIInst &I) {
1367 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001368 Type *SrcTy = I.getOperand(0)->getType();
1369 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001370
Duncan Sands1df98592010-02-16 11:11:14 +00001371 bool SrcVec = SrcTy->isVectorTy();
1372 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001373
Chris Lattner58d74912008-03-12 17:45:29 +00001374 Assert1(SrcVec == DstVec,
1375 "FPToSI source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001376 Assert1(SrcTy->isFPOrFPVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001377 "FPToSI source must be FP or FP vector", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001378 Assert1(DestTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001379 "FPToSI result must be integer or integer vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001380
1381 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001382 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1383 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001384 "FPToSI source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001385
1386 visitInstruction(I);
1387}
1388
1389void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
1390 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001391 Type *SrcTy = I.getOperand(0)->getType();
1392 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001393
Nadav Rotem16087692011-12-05 06:29:09 +00001394 Assert1(SrcTy->getScalarType()->isPointerTy(),
1395 "PtrToInt source must be pointer", &I);
1396 Assert1(DestTy->getScalarType()->isIntegerTy(),
1397 "PtrToInt result must be integral", &I);
1398 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
1399 "PtrToInt type mismatch", &I);
1400
1401 if (SrcTy->isVectorTy()) {
1402 VectorType *VSrc = dyn_cast<VectorType>(SrcTy);
1403 VectorType *VDest = dyn_cast<VectorType>(DestTy);
1404 Assert1(VSrc->getNumElements() == VDest->getNumElements(),
1405 "PtrToInt Vector width mismatch", &I);
1406 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001407
1408 visitInstruction(I);
1409}
1410
1411void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
1412 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001413 Type *SrcTy = I.getOperand(0)->getType();
1414 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001415
Nadav Rotem16087692011-12-05 06:29:09 +00001416 Assert1(SrcTy->getScalarType()->isIntegerTy(),
1417 "IntToPtr source must be an integral", &I);
1418 Assert1(DestTy->getScalarType()->isPointerTy(),
1419 "IntToPtr result must be a pointer",&I);
1420 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
1421 "IntToPtr type mismatch", &I);
1422 if (SrcTy->isVectorTy()) {
1423 VectorType *VSrc = dyn_cast<VectorType>(SrcTy);
1424 VectorType *VDest = dyn_cast<VectorType>(DestTy);
1425 Assert1(VSrc->getNumElements() == VDest->getNumElements(),
1426 "IntToPtr Vector width mismatch", &I);
1427 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001428 visitInstruction(I);
1429}
1430
1431void Verifier::visitBitCastInst(BitCastInst &I) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001432 Type *SrcTy = I.getOperand(0)->getType();
1433 Type *DestTy = I.getType();
Matt Arsenault16e4ed52013-07-31 17:49:08 +00001434 VerifyBitcastType(&I, DestTy, SrcTy);
Reid Spencer3da59db2006-11-27 01:05:10 +00001435 visitInstruction(I);
1436}
1437
Misha Brukmanab5c6002004-03-02 00:22:19 +00001438/// visitPHINode - Ensure that a PHI node is well formed.
1439///
Chris Lattner24e845f2002-06-25 15:56:27 +00001440void Verifier::visitPHINode(PHINode &PN) {
1441 // Ensure that the PHI nodes are all grouped together at the top of the block.
1442 // This can be tested by checking whether the instruction before this is
Misha Brukman6b634522003-10-10 17:54:14 +00001443 // either nonexistent (because this is begin()) or is a PHI node. If not,
Chris Lattner24e845f2002-06-25 15:56:27 +00001444 // then there is some other instruction before a PHI.
Matt Arsenault3f493222013-07-20 17:46:00 +00001445 Assert2(&PN == &PN.getParent()->front() ||
Chris Lattner4d8c16f2007-04-17 17:36:12 +00001446 isa<PHINode>(--BasicBlock::iterator(&PN)),
Chris Lattner24e845f2002-06-25 15:56:27 +00001447 "PHI nodes not grouped at top of basic block!",
1448 &PN, PN.getParent());
1449
Nick Lewycky49072472009-09-08 01:23:52 +00001450 // Check that all of the values of the PHI node have the same type as the
1451 // result, and that the incoming blocks are really basic blocks.
1452 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Chris Lattner579de712003-11-12 07:13:37 +00001453 Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
1454 "PHI node operands are not the same type as the result!", &PN);
Nick Lewycky49072472009-09-08 01:23:52 +00001455 }
Chris Lattner579de712003-11-12 07:13:37 +00001456
Chris Lattnerbede31f2003-10-05 17:44:18 +00001457 // All other PHI node constraints are checked in the visitBasicBlock method.
Chris Lattnerd231fc32002-04-18 20:37:37 +00001458
1459 visitInstruction(PN);
1460}
1461
Duncan Sandsd9d70392007-12-21 19:19:01 +00001462void Verifier::VerifyCallSite(CallSite CS) {
1463 Instruction *I = CS.getInstruction();
1464
Duncan Sands1df98592010-02-16 11:11:14 +00001465 Assert1(CS.getCalledValue()->getType()->isPointerTy(),
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001466 "Called function must be a pointer!", I);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001467 PointerType *FPTy = cast<PointerType>(CS.getCalledValue()->getType());
Chris Lattner56732fb2002-05-08 19:49:50 +00001468
Duncan Sands1df98592010-02-16 11:11:14 +00001469 Assert1(FPTy->getElementType()->isFunctionTy(),
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001470 "Called function is not pointer to function type!", I);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001471 FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
Chris Lattner56732fb2002-05-08 19:49:50 +00001472
1473 // Verify that the correct number of arguments are being passed
1474 if (FTy->isVarArg())
Duncan Sandsd9d70392007-12-21 19:19:01 +00001475 Assert1(CS.arg_size() >= FTy->getNumParams(),
1476 "Called function requires more parameters than were provided!",I);
Chris Lattner56732fb2002-05-08 19:49:50 +00001477 else
Duncan Sandsd9d70392007-12-21 19:19:01 +00001478 Assert1(CS.arg_size() == FTy->getNumParams(),
1479 "Incorrect number of arguments passed to called function!", I);
Chris Lattner56732fb2002-05-08 19:49:50 +00001480
Chris Lattner775aba22010-05-10 20:58:42 +00001481 // Verify that all arguments to the call match the function type.
Chris Lattner56732fb2002-05-08 19:49:50 +00001482 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Duncan Sandsd9d70392007-12-21 19:19:01 +00001483 Assert3(CS.getArgument(i)->getType() == FTy->getParamType(i),
Chris Lattner56732fb2002-05-08 19:49:50 +00001484 "Call parameter type does not match function signature!",
Duncan Sandsd9d70392007-12-21 19:19:01 +00001485 CS.getArgument(i), FTy->getParamType(i), I);
1486
Bill Wendlingbb1b63c2013-04-18 20:15:25 +00001487 AttributeSet Attrs = CS.getAttributes();
Duncan Sands623a3892008-01-11 22:36:48 +00001488
Devang Pateld9b4a5f2008-09-23 22:35:17 +00001489 Assert1(VerifyAttributeCount(Attrs, CS.arg_size()),
Bill Wendling034b94b2012-12-19 07:18:57 +00001490 "Attribute after last parameter!", I);
Duncan Sands623a3892008-01-11 22:36:48 +00001491
Duncan Sandsd9d70392007-12-21 19:19:01 +00001492 // Verify call attributes.
Duncan Sandscfad1b42008-01-12 16:42:01 +00001493 VerifyFunctionAttrs(FTy, Attrs, I);
Duncan Sands623a3892008-01-11 22:36:48 +00001494
Stephen Lin456ca042013-04-20 05:14:40 +00001495 if (FTy->isVarArg()) {
1496 // FIXME? is 'nest' even legal here?
1497 bool SawNest = false;
1498 bool SawReturned = false;
1499
1500 for (unsigned Idx = 1; Idx < 1 + FTy->getNumParams(); ++Idx) {
1501 if (Attrs.hasAttribute(Idx, Attribute::Nest))
1502 SawNest = true;
1503 if (Attrs.hasAttribute(Idx, Attribute::Returned))
1504 SawReturned = true;
1505 }
1506
Duncan Sands623a3892008-01-11 22:36:48 +00001507 // Check attributes on the varargs part.
1508 for (unsigned Idx = 1 + FTy->getNumParams(); Idx <= CS.arg_size(); ++Idx) {
Matt Arsenault3f493222013-07-20 17:46:00 +00001509 Type *Ty = CS.getArgument(Idx-1)->getType();
Stephen Lin456ca042013-04-20 05:14:40 +00001510 VerifyParameterAttrs(Attrs, Idx, Ty, false, I);
Matt Arsenault3f493222013-07-20 17:46:00 +00001511
Stephen Lin456ca042013-04-20 05:14:40 +00001512 if (Attrs.hasAttribute(Idx, Attribute::Nest)) {
1513 Assert1(!SawNest, "More than one parameter has attribute nest!", I);
1514 SawNest = true;
1515 }
1516
1517 if (Attrs.hasAttribute(Idx, Attribute::Returned)) {
1518 Assert1(!SawReturned, "More than one parameter has attribute returned!",
1519 I);
1520 Assert1(Ty->canLosslesslyBitCastTo(FTy->getReturnType()),
1521 "Incompatible argument and return types for 'returned' "
1522 "attribute", I);
1523 SawReturned = true;
1524 }
Duncan Sandscfad1b42008-01-12 16:42:01 +00001525
Bill Wendlingbed80592013-01-21 23:03:18 +00001526 Assert1(!Attrs.hasAttribute(Idx, Attribute::StructRet),
Bill Wendling15c37892012-10-09 09:33:01 +00001527 "Attribute 'sret' cannot be used for vararg call arguments!", I);
Duncan Sands623a3892008-01-11 22:36:48 +00001528 }
Stephen Lin456ca042013-04-20 05:14:40 +00001529 }
Duncan Sandsd9d70392007-12-21 19:19:01 +00001530
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001531 // Verify that there's no metadata unless it's a direct call to an intrinsic.
Chris Lattner1afcace2011-07-09 17:41:24 +00001532 if (CS.getCalledFunction() == 0 ||
Chris Lattner775aba22010-05-10 20:58:42 +00001533 !CS.getCalledFunction()->getName().startswith("llvm.")) {
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001534 for (FunctionType::param_iterator PI = FTy->param_begin(),
1535 PE = FTy->param_end(); PI != PE; ++PI)
Chris Lattner1afcace2011-07-09 17:41:24 +00001536 Assert1(!(*PI)->isMetadataTy(),
Owen Anderson1d0be152009-08-13 21:58:54 +00001537 "Function has metadata parameter but isn't an intrinsic", I);
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001538 }
1539
Michael Gottesman2253a2f2013-06-27 00:25:01 +00001540 // If the call site has the 'builtin' attribute, verify that it's applied to a
1541 // direct call to a function with the 'nobuiltin' attribute.
1542 if (CS.hasFnAttr(Attribute::Builtin))
1543 Assert1(CS.getCalledFunction() &&
1544 CS.getCalledFunction()->hasFnAttribute(Attribute::NoBuiltin),
1545 "Attribute 'builtin' can only be used in a call to a function with "
1546 "the 'nobuiltin' attribute.", I);
1547
Duncan Sandsd9d70392007-12-21 19:19:01 +00001548 visitInstruction(*I);
1549}
1550
1551void Verifier::visitCallInst(CallInst &CI) {
1552 VerifyCallSite(&CI);
Chris Lattner3535c9b2002-07-18 00:13:42 +00001553
Dale Johannesen49de9822009-02-05 01:49:45 +00001554 if (Function *F = CI.getCalledFunction())
Brian Gaeked0fde302003-11-11 22:41:34 +00001555 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
Chris Lattnerdd035d12003-05-08 03:47:33 +00001556 visitIntrinsicFunctionCall(ID, CI);
Duncan Sandsd9d70392007-12-21 19:19:01 +00001557}
1558
1559void Verifier::visitInvokeInst(InvokeInst &II) {
1560 VerifyCallSite(&II);
Bill Wendlingcccfd192011-09-21 22:57:02 +00001561
1562 // Verify that there is a landingpad instruction as the first non-PHI
1563 // instruction of the 'unwind' destination.
1564 Assert1(II.getUnwindDest()->isLandingPad(),
1565 "The unwind destination does not have a landingpad instruction!",&II);
1566
Dan Gohmandacfc5d2010-08-02 23:09:14 +00001567 visitTerminatorInst(II);
Chris Lattnerefdd0a22002-04-18 22:11:52 +00001568}
Chris Lattnerd231fc32002-04-18 20:37:37 +00001569
Misha Brukmanab5c6002004-03-02 00:22:19 +00001570/// visitBinaryOperator - Check that both arguments to the binary operator are
1571/// of the same type!
1572///
Chris Lattner24e845f2002-06-25 15:56:27 +00001573void Verifier::visitBinaryOperator(BinaryOperator &B) {
Chris Lattner1a143ae2002-09-09 20:26:04 +00001574 Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
1575 "Both operands to a binary operator are not of the same type!", &B);
Chris Lattnerd231fc32002-04-18 20:37:37 +00001576
Reid Spencer832254e2007-02-02 02:16:23 +00001577 switch (B.getOpcode()) {
Dan Gohman11cc35d2009-06-05 16:10:00 +00001578 // Check that integer arithmetic operators are only used with
1579 // integral operands.
1580 case Instruction::Add:
1581 case Instruction::Sub:
1582 case Instruction::Mul:
1583 case Instruction::SDiv:
1584 case Instruction::UDiv:
1585 case Instruction::SRem:
1586 case Instruction::URem:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001587 Assert1(B.getType()->isIntOrIntVectorTy(),
Dan Gohman11cc35d2009-06-05 16:10:00 +00001588 "Integer arithmetic operators only work with integral types!", &B);
1589 Assert1(B.getType() == B.getOperand(0)->getType(),
1590 "Integer arithmetic operators must have same type "
1591 "for operands and result!", &B);
1592 break;
1593 // Check that floating-point arithmetic operators are only used with
1594 // floating-point operands.
1595 case Instruction::FAdd:
1596 case Instruction::FSub:
1597 case Instruction::FMul:
1598 case Instruction::FDiv:
1599 case Instruction::FRem:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001600 Assert1(B.getType()->isFPOrFPVectorTy(),
Dan Gohman11cc35d2009-06-05 16:10:00 +00001601 "Floating-point arithmetic operators only work with "
Dan Gohmanf38fd692009-06-05 18:34:16 +00001602 "floating-point types!", &B);
Dan Gohman11cc35d2009-06-05 16:10:00 +00001603 Assert1(B.getType() == B.getOperand(0)->getType(),
1604 "Floating-point arithmetic operators must have same type "
1605 "for operands and result!", &B);
1606 break;
Chris Lattner1a143ae2002-09-09 20:26:04 +00001607 // Check that logical operators are only used with integral operands.
Reid Spencer832254e2007-02-02 02:16:23 +00001608 case Instruction::And:
1609 case Instruction::Or:
1610 case Instruction::Xor:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001611 Assert1(B.getType()->isIntOrIntVectorTy(),
Chris Lattner1a143ae2002-09-09 20:26:04 +00001612 "Logical operators only work with integral types!", &B);
1613 Assert1(B.getType() == B.getOperand(0)->getType(),
1614 "Logical operators must have same type for operands and result!",
1615 &B);
Reid Spencer832254e2007-02-02 02:16:23 +00001616 break;
1617 case Instruction::Shl:
1618 case Instruction::LShr:
1619 case Instruction::AShr:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001620 Assert1(B.getType()->isIntOrIntVectorTy(),
Nate Begeman5bc1ea02008-07-29 15:49:41 +00001621 "Shifts only work with integral types!", &B);
Reid Spencer832254e2007-02-02 02:16:23 +00001622 Assert1(B.getType() == B.getOperand(0)->getType(),
1623 "Shift return type must be same as operands!", &B);
Reid Spencer832254e2007-02-02 02:16:23 +00001624 break;
Dan Gohman11cc35d2009-06-05 16:10:00 +00001625 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001626 llvm_unreachable("Unknown BinaryOperator opcode!");
Chris Lattner1a143ae2002-09-09 20:26:04 +00001627 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001628
Chris Lattnerd231fc32002-04-18 20:37:37 +00001629 visitInstruction(B);
1630}
1631
Nick Lewycky55e97d42010-08-22 23:45:14 +00001632void Verifier::visitICmpInst(ICmpInst &IC) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00001633 // Check that the operands are the same type
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001634 Type *Op0Ty = IC.getOperand(0)->getType();
1635 Type *Op1Ty = IC.getOperand(1)->getType();
Reid Spencer45fb3f32006-11-20 01:22:35 +00001636 Assert1(Op0Ty == Op1Ty,
1637 "Both operands to ICmp instruction are not of the same type!", &IC);
1638 // Check that the operands are the right type
Nadav Rotem16087692011-12-05 06:29:09 +00001639 Assert1(Op0Ty->isIntOrIntVectorTy() || Op0Ty->getScalarType()->isPointerTy(),
Reid Spencer45fb3f32006-11-20 01:22:35 +00001640 "Invalid operand types for ICmp instruction", &IC);
Nick Lewycky55e97d42010-08-22 23:45:14 +00001641 // Check that the predicate is valid.
1642 Assert1(IC.getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1643 IC.getPredicate() <= CmpInst::LAST_ICMP_PREDICATE,
1644 "Invalid predicate in ICmp instruction!", &IC);
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001645
Reid Spencer45fb3f32006-11-20 01:22:35 +00001646 visitInstruction(IC);
1647}
1648
Nick Lewycky55e97d42010-08-22 23:45:14 +00001649void Verifier::visitFCmpInst(FCmpInst &FC) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00001650 // Check that the operands are the same type
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001651 Type *Op0Ty = FC.getOperand(0)->getType();
1652 Type *Op1Ty = FC.getOperand(1)->getType();
Reid Spencer45fb3f32006-11-20 01:22:35 +00001653 Assert1(Op0Ty == Op1Ty,
1654 "Both operands to FCmp instruction are not of the same type!", &FC);
1655 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001656 Assert1(Op0Ty->isFPOrFPVectorTy(),
Reid Spencer45fb3f32006-11-20 01:22:35 +00001657 "Invalid operand types for FCmp instruction", &FC);
Nick Lewycky55e97d42010-08-22 23:45:14 +00001658 // Check that the predicate is valid.
1659 Assert1(FC.getPredicate() >= CmpInst::FIRST_FCMP_PREDICATE &&
1660 FC.getPredicate() <= CmpInst::LAST_FCMP_PREDICATE,
1661 "Invalid predicate in FCmp instruction!", &FC);
1662
Reid Spencer45fb3f32006-11-20 01:22:35 +00001663 visitInstruction(FC);
1664}
1665
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001666void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
Chris Lattner1cbe05b2006-04-08 04:07:52 +00001667 Assert1(ExtractElementInst::isValidOperands(EI.getOperand(0),
1668 EI.getOperand(1)),
1669 "Invalid extractelement operands!", &EI);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001670 visitInstruction(EI);
1671}
1672
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001673void Verifier::visitInsertElementInst(InsertElementInst &IE) {
Chris Lattner1cbe05b2006-04-08 04:07:52 +00001674 Assert1(InsertElementInst::isValidOperands(IE.getOperand(0),
1675 IE.getOperand(1),
1676 IE.getOperand(2)),
1677 "Invalid insertelement operands!", &IE);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001678 visitInstruction(IE);
1679}
1680
Chris Lattner00f10232006-04-08 01:18:18 +00001681void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
1682 Assert1(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
1683 SV.getOperand(2)),
1684 "Invalid shufflevector operands!", &SV);
Chris Lattner00f10232006-04-08 01:18:18 +00001685 visitInstruction(SV);
1686}
1687
Chris Lattner24e845f2002-06-25 15:56:27 +00001688void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Duncan Sandsb95d1ff2012-02-03 17:28:51 +00001689 Type *TargetTy = GEP.getPointerOperandType()->getScalarType();
Nadav Rotem16087692011-12-05 06:29:09 +00001690
Duncan Sandsb95d1ff2012-02-03 17:28:51 +00001691 Assert1(isa<PointerType>(TargetTy),
Bill Wendling6ebddd22012-10-17 23:56:05 +00001692 "GEP base pointer is not a vector or a vector of pointers", &GEP);
Nadav Rotem16087692011-12-05 06:29:09 +00001693 Assert1(cast<PointerType>(TargetTy)->getElementType()->isSized(),
Chris Lattner4cea5ba2011-07-29 20:32:28 +00001694 "GEP into unsized type!", &GEP);
Duncan Sands2333e292012-11-13 12:59:33 +00001695 Assert1(GEP.getPointerOperandType()->isVectorTy() ==
1696 GEP.getType()->isVectorTy(), "Vector GEP must return a vector value",
1697 &GEP);
Nadav Rotem16087692011-12-05 06:29:09 +00001698
Chris Lattner8552fae2007-02-10 08:30:29 +00001699 SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end());
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001700 Type *ElTy =
Nadav Rotem16087692011-12-05 06:29:09 +00001701 GetElementPtrInst::getIndexedType(GEP.getPointerOperandType(), Idxs);
Chris Lattner24e845f2002-06-25 15:56:27 +00001702 Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
Nadav Rotem16087692011-12-05 06:29:09 +00001703
Duncan Sands2333e292012-11-13 12:59:33 +00001704 Assert2(GEP.getType()->getScalarType()->isPointerTy() &&
1705 cast<PointerType>(GEP.getType()->getScalarType())->getElementType()
1706 == ElTy, "GEP is not of right type for indices!", &GEP, ElTy);
1707
1708 if (GEP.getPointerOperandType()->isVectorTy()) {
1709 // Additional checks for vector GEPs.
1710 unsigned GepWidth = GEP.getPointerOperandType()->getVectorNumElements();
1711 Assert1(GepWidth == GEP.getType()->getVectorNumElements(),
1712 "Vector GEP result width doesn't match operand's", &GEP);
1713 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
1714 Type *IndexTy = Idxs[i]->getType();
1715 Assert1(IndexTy->isVectorTy(),
1716 "Vector GEP must have vector indices!", &GEP);
1717 unsigned IndexWidth = IndexTy->getVectorNumElements();
1718 Assert1(IndexWidth == GepWidth, "Invalid GEP index vector width", &GEP);
1719 }
Nadav Rotem16087692011-12-05 06:29:09 +00001720 }
Chris Lattnera00409e2002-04-24 19:12:21 +00001721 visitInstruction(GEP);
1722}
1723
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001724static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
1725 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
1726}
1727
Chris Lattner24e845f2002-06-25 15:56:27 +00001728void Verifier::visitLoadInst(LoadInst &LI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001729 PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
Nick Lewycky49072472009-09-08 01:23:52 +00001730 Assert1(PTy, "Load operand must be a pointer.", &LI);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001731 Type *ElTy = PTy->getElementType();
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001732 Assert2(ElTy == LI.getType(),
1733 "Load result type does not match pointer operand type!", &LI, ElTy);
Eli Friedman21006d42011-08-09 23:02:53 +00001734 if (LI.isAtomic()) {
1735 Assert1(LI.getOrdering() != Release && LI.getOrdering() != AcquireRelease,
1736 "Load cannot have Release ordering", &LI);
1737 Assert1(LI.getAlignment() != 0,
1738 "Atomic load must specify explicit alignment", &LI);
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001739 if (!ElTy->isPointerTy()) {
1740 Assert2(ElTy->isIntegerTy(),
1741 "atomic store operand must have integer type!",
1742 &LI, ElTy);
1743 unsigned Size = ElTy->getPrimitiveSizeInBits();
1744 Assert2(Size >= 8 && !(Size & (Size - 1)),
1745 "atomic store operand must be power-of-two byte-sized integer",
1746 &LI, ElTy);
1747 }
Eli Friedman21006d42011-08-09 23:02:53 +00001748 } else {
1749 Assert1(LI.getSynchScope() == CrossThread,
1750 "Non-atomic load cannot have SynchronizationScope specified", &LI);
1751 }
Rafael Espindola39dd3282012-03-24 00:14:51 +00001752
1753 if (MDNode *Range = LI.getMetadata(LLVMContext::MD_range)) {
1754 unsigned NumOperands = Range->getNumOperands();
1755 Assert1(NumOperands % 2 == 0, "Unfinished range!", Range);
1756 unsigned NumRanges = NumOperands / 2;
1757 Assert1(NumRanges >= 1, "It should have at least one range!", Range);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001758
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001759 ConstantRange LastRange(1); // Dummy initial value
Rafael Espindola39dd3282012-03-24 00:14:51 +00001760 for (unsigned i = 0; i < NumRanges; ++i) {
1761 ConstantInt *Low = dyn_cast<ConstantInt>(Range->getOperand(2*i));
1762 Assert1(Low, "The lower limit must be an integer!", Low);
1763 ConstantInt *High = dyn_cast<ConstantInt>(Range->getOperand(2*i + 1));
1764 Assert1(High, "The upper limit must be an integer!", High);
1765 Assert1(High->getType() == Low->getType() &&
1766 High->getType() == ElTy, "Range types must match load type!",
1767 &LI);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001768
1769 APInt HighV = High->getValue();
1770 APInt LowV = Low->getValue();
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001771 ConstantRange CurRange(LowV, HighV);
1772 Assert1(!CurRange.isEmptySet() && !CurRange.isFullSet(),
1773 "Range must not be empty!", Range);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001774 if (i != 0) {
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001775 Assert1(CurRange.intersectWith(LastRange).isEmptySet(),
1776 "Intervals are overlapping", Range);
1777 Assert1(LowV.sgt(LastRange.getLower()), "Intervals are not in order",
1778 Range);
1779 Assert1(!isContiguous(CurRange, LastRange), "Intervals are contiguous",
1780 Range);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001781 }
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001782 LastRange = ConstantRange(LowV, HighV);
Rafael Espindola39dd3282012-03-24 00:14:51 +00001783 }
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001784 if (NumRanges > 2) {
1785 APInt FirstLow =
1786 dyn_cast<ConstantInt>(Range->getOperand(0))->getValue();
1787 APInt FirstHigh =
1788 dyn_cast<ConstantInt>(Range->getOperand(1))->getValue();
1789 ConstantRange FirstRange(FirstLow, FirstHigh);
1790 Assert1(FirstRange.intersectWith(LastRange).isEmptySet(),
1791 "Intervals are overlapping", Range);
1792 Assert1(!isContiguous(FirstRange, LastRange), "Intervals are contiguous",
1793 Range);
1794 }
1795
1796
Rafael Espindola39dd3282012-03-24 00:14:51 +00001797 }
1798
Chris Lattnera00409e2002-04-24 19:12:21 +00001799 visitInstruction(LI);
1800}
1801
Chris Lattner24e845f2002-06-25 15:56:27 +00001802void Verifier::visitStoreInst(StoreInst &SI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001803 PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
Chris Lattnerb4a96312010-07-11 19:42:53 +00001804 Assert1(PTy, "Store operand must be a pointer.", &SI);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001805 Type *ElTy = PTy->getElementType();
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001806 Assert2(ElTy == SI.getOperand(0)->getType(),
1807 "Stored value type does not match pointer operand type!",
1808 &SI, ElTy);
Eli Friedman21006d42011-08-09 23:02:53 +00001809 if (SI.isAtomic()) {
1810 Assert1(SI.getOrdering() != Acquire && SI.getOrdering() != AcquireRelease,
1811 "Store cannot have Acquire ordering", &SI);
1812 Assert1(SI.getAlignment() != 0,
1813 "Atomic store must specify explicit alignment", &SI);
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001814 if (!ElTy->isPointerTy()) {
1815 Assert2(ElTy->isIntegerTy(),
1816 "atomic store operand must have integer type!",
1817 &SI, ElTy);
1818 unsigned Size = ElTy->getPrimitiveSizeInBits();
1819 Assert2(Size >= 8 && !(Size & (Size - 1)),
1820 "atomic store operand must be power-of-two byte-sized integer",
1821 &SI, ElTy);
1822 }
Eli Friedman21006d42011-08-09 23:02:53 +00001823 } else {
1824 Assert1(SI.getSynchScope() == CrossThread,
1825 "Non-atomic store cannot have SynchronizationScope specified", &SI);
1826 }
Chris Lattnera00409e2002-04-24 19:12:21 +00001827 visitInstruction(SI);
1828}
1829
Victor Hernandez7b929da2009-10-23 21:09:37 +00001830void Verifier::visitAllocaInst(AllocaInst &AI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001831 PointerType *PTy = AI.getType();
Matt Arsenault3f493222013-07-20 17:46:00 +00001832 Assert1(PTy->getAddressSpace() == 0,
Chris Lattnerab3b7782008-03-01 09:01:57 +00001833 "Allocation instruction pointer not in the generic address space!",
1834 &AI);
1835 Assert1(PTy->getElementType()->isSized(), "Cannot allocate unsized type",
1836 &AI);
Dan Gohmanf75a7d32010-05-28 01:14:11 +00001837 Assert1(AI.getArraySize()->getType()->isIntegerTy(),
1838 "Alloca array size must have integer type", &AI);
Christopher Lamb303dae92007-12-17 01:00:21 +00001839 visitInstruction(AI);
1840}
1841
Eli Friedmanff030482011-07-28 21:48:00 +00001842void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) {
1843 Assert1(CXI.getOrdering() != NotAtomic,
1844 "cmpxchg instructions must be atomic.", &CXI);
1845 Assert1(CXI.getOrdering() != Unordered,
1846 "cmpxchg instructions cannot be unordered.", &CXI);
1847 PointerType *PTy = dyn_cast<PointerType>(CXI.getOperand(0)->getType());
1848 Assert1(PTy, "First cmpxchg operand must be a pointer.", &CXI);
1849 Type *ElTy = PTy->getElementType();
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001850 Assert2(ElTy->isIntegerTy(),
1851 "cmpxchg operand must have integer type!",
1852 &CXI, ElTy);
1853 unsigned Size = ElTy->getPrimitiveSizeInBits();
1854 Assert2(Size >= 8 && !(Size & (Size - 1)),
1855 "cmpxchg operand must be power-of-two byte-sized integer",
1856 &CXI, ElTy);
Eli Friedmanff030482011-07-28 21:48:00 +00001857 Assert2(ElTy == CXI.getOperand(1)->getType(),
1858 "Expected value type does not match pointer operand type!",
1859 &CXI, ElTy);
1860 Assert2(ElTy == CXI.getOperand(2)->getType(),
1861 "Stored value type does not match pointer operand type!",
1862 &CXI, ElTy);
1863 visitInstruction(CXI);
1864}
1865
1866void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
1867 Assert1(RMWI.getOrdering() != NotAtomic,
1868 "atomicrmw instructions must be atomic.", &RMWI);
1869 Assert1(RMWI.getOrdering() != Unordered,
1870 "atomicrmw instructions cannot be unordered.", &RMWI);
1871 PointerType *PTy = dyn_cast<PointerType>(RMWI.getOperand(0)->getType());
1872 Assert1(PTy, "First atomicrmw operand must be a pointer.", &RMWI);
1873 Type *ElTy = PTy->getElementType();
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001874 Assert2(ElTy->isIntegerTy(),
1875 "atomicrmw operand must have integer type!",
1876 &RMWI, ElTy);
1877 unsigned Size = ElTy->getPrimitiveSizeInBits();
1878 Assert2(Size >= 8 && !(Size & (Size - 1)),
1879 "atomicrmw operand must be power-of-two byte-sized integer",
1880 &RMWI, ElTy);
Eli Friedmanff030482011-07-28 21:48:00 +00001881 Assert2(ElTy == RMWI.getOperand(1)->getType(),
1882 "Argument value type does not match pointer operand type!",
1883 &RMWI, ElTy);
1884 Assert1(AtomicRMWInst::FIRST_BINOP <= RMWI.getOperation() &&
1885 RMWI.getOperation() <= AtomicRMWInst::LAST_BINOP,
1886 "Invalid binary operation!", &RMWI);
1887 visitInstruction(RMWI);
1888}
1889
Eli Friedman47f35132011-07-25 23:16:38 +00001890void Verifier::visitFenceInst(FenceInst &FI) {
1891 const AtomicOrdering Ordering = FI.getOrdering();
1892 Assert1(Ordering == Acquire || Ordering == Release ||
1893 Ordering == AcquireRelease || Ordering == SequentiallyConsistent,
1894 "fence instructions may only have "
Bill Wendling63012202011-08-08 08:02:48 +00001895 "acquire, release, acq_rel, or seq_cst ordering.", &FI);
Eli Friedman47f35132011-07-25 23:16:38 +00001896 visitInstruction(FI);
1897}
1898
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001899void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
1900 Assert1(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001901 EVI.getIndices()) ==
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001902 EVI.getType(),
1903 "Invalid ExtractValueInst operands!", &EVI);
Matt Arsenault3f493222013-07-20 17:46:00 +00001904
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001905 visitInstruction(EVI);
Devang Patel40a04212008-02-19 22:15:16 +00001906}
1907
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001908void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
1909 Assert1(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001910 IVI.getIndices()) ==
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001911 IVI.getOperand(1)->getType(),
1912 "Invalid InsertValueInst operands!", &IVI);
Matt Arsenault3f493222013-07-20 17:46:00 +00001913
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001914 visitInstruction(IVI);
1915}
Chris Lattnerd231fc32002-04-18 20:37:37 +00001916
Bill Wendlinge6e88262011-08-12 20:24:12 +00001917void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
1918 BasicBlock *BB = LPI.getParent();
1919
1920 // The landingpad instruction is ill-formed if it doesn't have any clauses and
1921 // isn't a cleanup.
1922 Assert1(LPI.getNumClauses() > 0 || LPI.isCleanup(),
1923 "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
1924
1925 // The landingpad instruction defines its parent as a landing pad block. The
1926 // landing pad block may be branched to only by the unwind edge of an invoke.
1927 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
1928 const InvokeInst *II = dyn_cast<InvokeInst>((*I)->getTerminator());
Eli Friedman6b951b22012-08-10 20:55:20 +00001929 Assert1(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
Bill Wendlinge6e88262011-08-12 20:24:12 +00001930 "Block containing LandingPadInst must be jumped to "
1931 "only by the unwind edge of an invoke.", &LPI);
1932 }
1933
1934 // The landingpad instruction must be the first non-PHI instruction in the
1935 // block.
1936 Assert1(LPI.getParent()->getLandingPadInst() == &LPI,
1937 "LandingPadInst not the first non-PHI instruction in the block.",
1938 &LPI);
1939
1940 // The personality functions for all landingpad instructions within the same
1941 // function should match.
1942 if (PersonalityFn)
1943 Assert1(LPI.getPersonalityFn() == PersonalityFn,
1944 "Personality function doesn't match others in function", &LPI);
1945 PersonalityFn = LPI.getPersonalityFn();
1946
Duncan Sands00418ea2011-09-27 16:43:19 +00001947 // All operands must be constants.
1948 Assert1(isa<Constant>(PersonalityFn), "Personality function is not constant!",
1949 &LPI);
1950 for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) {
1951 Value *Clause = LPI.getClause(i);
1952 Assert1(isa<Constant>(Clause), "Clause is not constant!", &LPI);
Duncan Sands040bff02011-09-27 19:34:22 +00001953 if (LPI.isCatch(i)) {
1954 Assert1(isa<PointerType>(Clause->getType()),
1955 "Catch operand does not have pointer type!", &LPI);
1956 } else {
1957 Assert1(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI);
Duncan Sands00418ea2011-09-27 16:43:19 +00001958 Assert1(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause),
Duncan Sands040bff02011-09-27 19:34:22 +00001959 "Filter operand is not an array of constants!", &LPI);
1960 }
Duncan Sands00418ea2011-09-27 16:43:19 +00001961 }
1962
Bill Wendlinge6e88262011-08-12 20:24:12 +00001963 visitInstruction(LPI);
1964}
1965
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001966void Verifier::verifyDominatesUse(Instruction &I, unsigned i) {
1967 Instruction *Op = cast<Instruction>(I.getOperand(i));
Rafael Espindolad5118c82012-08-17 18:21:28 +00001968 // If the we have an invalid invoke, don't try to compute the dominance.
1969 // We already reject it in the invoke specific checks and the dominance
1970 // computation doesn't handle multiple edges.
1971 if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
1972 if (II->getNormalDest() == II->getUnwindDest())
1973 return;
1974 }
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001975
Rafael Espindola20907662012-06-01 21:56:26 +00001976 const Use &U = I.getOperandUse(i);
1977 Assert2(InstsInThisBlock.count(Op) || DT->dominates(Op, U),
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001978 "Instruction does not dominate all uses!", Op, &I);
1979}
1980
Misha Brukmanab5c6002004-03-02 00:22:19 +00001981/// verifyInstruction - Verify that an instruction is well formed.
1982///
Chris Lattner24e845f2002-06-25 15:56:27 +00001983void Verifier::visitInstruction(Instruction &I) {
Misha Brukmanfd939082005-04-21 23:48:37 +00001984 BasicBlock *BB = I.getParent();
Chris Lattner1a143ae2002-09-09 20:26:04 +00001985 Assert1(BB, "Instruction not embedded in basic block!", &I);
Chris Lattnerd231fc32002-04-18 20:37:37 +00001986
Chris Lattnerbede31f2003-10-05 17:44:18 +00001987 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
1988 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
1989 UI != UE; ++UI)
Duncan Sands44008252009-05-29 19:39:36 +00001990 Assert1(*UI != (User*)&I || !DT->isReachableFromEntry(BB),
Chris Lattnerbede31f2003-10-05 17:44:18 +00001991 "Only PHI nodes may reference their own value!", &I);
1992 }
Nick Lewycky29ef6592009-09-07 20:44:51 +00001993
Chris Lattnerbede31f2003-10-05 17:44:18 +00001994 // Check that void typed values don't have names
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001995 Assert1(!I.getType()->isVoidTy() || !I.hasName(),
Chris Lattnerbede31f2003-10-05 17:44:18 +00001996 "Instruction has a name, but provides a void value!", &I);
1997
Chris Lattner944cfaf2004-03-29 00:29:36 +00001998 // Check that the return value of the instruction is either void or a legal
1999 // value type.
Matt Arsenault3f493222013-07-20 17:46:00 +00002000 Assert1(I.getType()->isVoidTy() ||
Nick Lewyckyc261df92009-09-27 23:27:42 +00002001 I.getType()->isFirstClassType(),
Chris Lattner944cfaf2004-03-29 00:29:36 +00002002 "Instruction returns a non-scalar type!", &I);
2003
Nick Lewyckyc261df92009-09-27 23:27:42 +00002004 // Check that the instruction doesn't produce metadata. Calls are already
2005 // checked against the callee type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00002006 Assert1(!I.getType()->isMetadataTy() ||
Nick Lewycky7a0370f2009-05-30 05:06:04 +00002007 isa<CallInst>(I) || isa<InvokeInst>(I),
2008 "Invalid use of metadata!", &I);
2009
Chris Lattnerd231fc32002-04-18 20:37:37 +00002010 // Check that all uses of the instruction, if they are instructions
2011 // themselves, actually have parent basic blocks. If the use is not an
2012 // instruction, it is an error!
Chris Lattner24e845f2002-06-25 15:56:27 +00002013 for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattnerd231fc32002-04-18 20:37:37 +00002014 UI != UE; ++UI) {
Nick Lewycky49072472009-09-08 01:23:52 +00002015 if (Instruction *Used = dyn_cast<Instruction>(*UI))
2016 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
2017 " embedded in a basic block!", &I, Used);
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00002018 else {
Nick Lewycky49072472009-09-08 01:23:52 +00002019 CheckFailed("Use of instruction is not an instruction!", *UI);
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00002020 return;
2021 }
Chris Lattnerd231fc32002-04-18 20:37:37 +00002022 }
2023
Chris Lattnerbede31f2003-10-05 17:44:18 +00002024 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Chris Lattneraab18202005-02-24 16:58:29 +00002025 Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I);
Chris Lattnerf4ea9212006-07-11 20:29:49 +00002026
2027 // Check to make sure that only first-class-values are operands to
2028 // instructions.
Devang Patelbb4f8d42008-02-21 01:54:02 +00002029 if (!I.getOperand(i)->getType()->isFirstClassType()) {
Dan Gohmanfc74abf2008-07-23 00:34:11 +00002030 Assert1(0, "Instruction operands must be first-class values!", &I);
Devang Patelbb4f8d42008-02-21 01:54:02 +00002031 }
Nick Lewycky7a0370f2009-05-30 05:06:04 +00002032
Chris Lattner59c35692004-03-14 03:23:54 +00002033 if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
Chris Lattnerf4ea9212006-07-11 20:29:49 +00002034 // Check to make sure that the "address of" an intrinsic function is never
2035 // taken.
Nuno Lopes917f97c2012-06-28 22:57:00 +00002036 Assert1(!F->isIntrinsic() || i == (isa<CallInst>(I) ? e-1 : 0),
Chris Lattnerdd035d12003-05-08 03:47:33 +00002037 "Cannot take the address of an intrinsic!", &I);
Nuno Lopes917f97c2012-06-28 22:57:00 +00002038 Assert1(!F->isIntrinsic() || isa<CallInst>(I) ||
2039 F->getIntrinsicID() == Intrinsic::donothing,
2040 "Cannot invoke an intrinsinc other than donothing", &I);
Chris Lattner19b6dcd2007-04-20 21:48:08 +00002041 Assert1(F->getParent() == Mod, "Referencing function in another module!",
2042 &I);
Chris Lattner59c35692004-03-14 03:23:54 +00002043 } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
2044 Assert1(OpBB->getParent() == BB->getParent(),
2045 "Referring to a basic block in another function!", &I);
2046 } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
2047 Assert1(OpArg->getParent() == BB->getParent(),
2048 "Referring to an argument in another function!", &I);
Chris Lattner19b6dcd2007-04-20 21:48:08 +00002049 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
2050 Assert1(GV->getParent() == Mod, "Referencing global in another module!",
2051 &I);
Rafael Espindolac987f4c2012-02-26 02:23:37 +00002052 } else if (isa<Instruction>(I.getOperand(i))) {
2053 verifyDominatesUse(I, i);
Chris Lattner3188b732006-01-26 00:08:45 +00002054 } else if (isa<InlineAsm>(I.getOperand(i))) {
Gabor Greif63d024f2010-07-13 15:31:36 +00002055 Assert1((i + 1 == e && isa<CallInst>(I)) ||
2056 (i + 3 == e && isa<InvokeInst>(I)),
Chris Lattner3188b732006-01-26 00:08:45 +00002057 "Cannot take the address of an inline asm!", &I);
Matt Arsenault16e4ed52013-07-31 17:49:08 +00002058 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(I.getOperand(i))) {
2059 if (CE->getType()->isPtrOrPtrVectorTy()) {
2060 // If we have a ConstantExpr pointer, we need to see if it came from an
2061 // illegal bitcast (inttoptr <constant int> )
2062 SmallVector<const ConstantExpr *, 4> Stack;
2063 SmallPtrSet<const ConstantExpr *, 4> Visited;
2064 Stack.push_back(CE);
2065
2066 while (!Stack.empty()) {
2067 const ConstantExpr *V = Stack.pop_back_val();
2068 if (!Visited.insert(V))
2069 continue;
2070
2071 VerifyConstantExprBitcastType(V);
2072
2073 for (unsigned I = 0, N = V->getNumOperands(); I != N; ++I) {
2074 if (ConstantExpr *Op = dyn_cast<ConstantExpr>(V->getOperand(I)))
2075 Stack.push_back(Op);
2076 }
2077 }
2078 }
Chris Lattnerbede31f2003-10-05 17:44:18 +00002079 }
2080 }
Rafael Espindola39dd3282012-03-24 00:14:51 +00002081
Duncan Sands5e5c5f82012-04-14 12:36:06 +00002082 if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) {
Duncan Sands1fd63df2012-04-10 08:22:43 +00002083 Assert1(I.getType()->isFPOrFPVectorTy(),
Duncan Sands5e5c5f82012-04-14 12:36:06 +00002084 "fpmath requires a floating point result!", &I);
2085 Assert1(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
Duncan Sands8883c432012-04-16 16:28:59 +00002086 Value *Op0 = MD->getOperand(0);
2087 if (ConstantFP *CFP0 = dyn_cast_or_null<ConstantFP>(Op0)) {
2088 APFloat Accuracy = CFP0->getValueAPF();
Michael Gottesman07969dc2013-06-19 21:23:18 +00002089 Assert1(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(),
Duncan Sands8883c432012-04-16 16:28:59 +00002090 "fpmath accuracy not a positive number!", &I);
Duncan Sands8883c432012-04-16 16:28:59 +00002091 } else {
2092 Assert1(false, "invalid fpmath accuracy!", &I);
2093 }
Duncan Sands1fd63df2012-04-10 08:22:43 +00002094 }
2095
Rafael Espindola39dd3282012-03-24 00:14:51 +00002096 MDNode *MD = I.getMetadata(LLVMContext::MD_range);
2097 Assert1(!MD || isa<LoadInst>(I), "Ranges are only for loads!", &I);
2098
Chris Lattnera7b1c7e2004-09-29 20:07:45 +00002099 InstsInThisBlock.insert(&I);
Chris Lattnerdd035d12003-05-08 03:47:33 +00002100}
2101
Chris Lattner55dc5c72012-05-27 19:37:05 +00002102/// VerifyIntrinsicType - Verify that the specified type (which comes from an
2103/// intrinsic argument or return value) matches the type constraints specified
2104/// by the .td file (e.g. an "any integer" argument really is an integer).
2105///
2106/// This return true on error but does not print a message.
2107bool Verifier::VerifyIntrinsicType(Type *Ty,
2108 ArrayRef<Intrinsic::IITDescriptor> &Infos,
2109 SmallVectorImpl<Type*> &ArgTys) {
2110 using namespace Intrinsic;
2111
2112 // If we ran out of descriptors, there are too many arguments.
Matt Arsenault3f493222013-07-20 17:46:00 +00002113 if (Infos.empty()) return true;
Chris Lattner55dc5c72012-05-27 19:37:05 +00002114 IITDescriptor D = Infos.front();
2115 Infos = Infos.slice(1);
Matt Arsenault3f493222013-07-20 17:46:00 +00002116
Chris Lattner55dc5c72012-05-27 19:37:05 +00002117 switch (D.Kind) {
2118 case IITDescriptor::Void: return !Ty->isVoidTy();
2119 case IITDescriptor::MMX: return !Ty->isX86_MMXTy();
2120 case IITDescriptor::Metadata: return !Ty->isMetadataTy();
Michael Ilseman4d0b4a42013-01-11 01:45:05 +00002121 case IITDescriptor::Half: return !Ty->isHalfTy();
Chris Lattner55dc5c72012-05-27 19:37:05 +00002122 case IITDescriptor::Float: return !Ty->isFloatTy();
2123 case IITDescriptor::Double: return !Ty->isDoubleTy();
2124 case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
2125 case IITDescriptor::Vector: {
2126 VectorType *VT = dyn_cast<VectorType>(Ty);
2127 return VT == 0 || VT->getNumElements() != D.Vector_Width ||
2128 VerifyIntrinsicType(VT->getElementType(), Infos, ArgTys);
2129 }
2130 case IITDescriptor::Pointer: {
2131 PointerType *PT = dyn_cast<PointerType>(Ty);
2132 return PT == 0 || PT->getAddressSpace() != D.Pointer_AddressSpace ||
2133 VerifyIntrinsicType(PT->getElementType(), Infos, ArgTys);
2134 }
Matt Arsenault3f493222013-07-20 17:46:00 +00002135
Chris Lattner55dc5c72012-05-27 19:37:05 +00002136 case IITDescriptor::Struct: {
2137 StructType *ST = dyn_cast<StructType>(Ty);
2138 if (ST == 0 || ST->getNumElements() != D.Struct_NumElements)
2139 return true;
Matt Arsenault3f493222013-07-20 17:46:00 +00002140
Chris Lattner55dc5c72012-05-27 19:37:05 +00002141 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
2142 if (VerifyIntrinsicType(ST->getElementType(i), Infos, ArgTys))
2143 return true;
2144 return false;
2145 }
Matt Arsenault3f493222013-07-20 17:46:00 +00002146
Chris Lattner55dc5c72012-05-27 19:37:05 +00002147 case IITDescriptor::Argument:
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002148 // Two cases here - If this is the second occurrence of an argument, verify
Matt Arsenault3f493222013-07-20 17:46:00 +00002149 // that the later instance matches the previous instance.
Chris Lattner55dc5c72012-05-27 19:37:05 +00002150 if (D.getArgumentNumber() < ArgTys.size())
Matt Arsenault3f493222013-07-20 17:46:00 +00002151 return Ty != ArgTys[D.getArgumentNumber()];
2152
Chris Lattner55dc5c72012-05-27 19:37:05 +00002153 // Otherwise, if this is the first instance of an argument, record it and
2154 // verify the "Any" kind.
2155 assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error");
2156 ArgTys.push_back(Ty);
Matt Arsenault3f493222013-07-20 17:46:00 +00002157
Chris Lattner55dc5c72012-05-27 19:37:05 +00002158 switch (D.getArgumentKind()) {
2159 case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
2160 case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy();
2161 case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty);
2162 case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
2163 }
2164 llvm_unreachable("all argument kinds not covered");
Matt Arsenault3f493222013-07-20 17:46:00 +00002165
Chris Lattner55dc5c72012-05-27 19:37:05 +00002166 case IITDescriptor::ExtendVecArgument:
2167 // This may only be used when referring to a previous vector argument.
2168 return D.getArgumentNumber() >= ArgTys.size() ||
2169 !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
2170 VectorType::getExtendedElementVectorType(
2171 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
2172
2173 case IITDescriptor::TruncVecArgument:
2174 // This may only be used when referring to a previous vector argument.
2175 return D.getArgumentNumber() >= ArgTys.size() ||
2176 !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
2177 VectorType::getTruncatedElementVectorType(
2178 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
2179 }
2180 llvm_unreachable("unhandled");
2181}
Bob Wilsonbc039792009-01-07 00:09:01 +00002182
Chris Lattnerdd035d12003-05-08 03:47:33 +00002183/// visitIntrinsicFunction - Allow intrinsics to be verified in different ways.
Misha Brukmanab5c6002004-03-02 00:22:19 +00002184///
Brian Gaeked0fde302003-11-11 22:41:34 +00002185void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnerdd035d12003-05-08 03:47:33 +00002186 Function *IF = CI.getCalledFunction();
Chris Lattner19b6dcd2007-04-20 21:48:08 +00002187 Assert1(IF->isDeclaration(), "Intrinsic functions should never be defined!",
2188 IF);
Nick Lewycky29ef6592009-09-07 20:44:51 +00002189
Chris Lattner55dc5c72012-05-27 19:37:05 +00002190 // Verify that the intrinsic prototype lines up with what the .td files
2191 // describe.
2192 FunctionType *IFTy = IF->getFunctionType();
2193 Assert1(!IFTy->isVarArg(), "Intrinsic prototypes are not varargs", IF);
Matt Arsenault3f493222013-07-20 17:46:00 +00002194
Chris Lattner55dc5c72012-05-27 19:37:05 +00002195 SmallVector<Intrinsic::IITDescriptor, 8> Table;
2196 getIntrinsicInfoTableEntries(ID, Table);
2197 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
Nick Lewycky29ef6592009-09-07 20:44:51 +00002198
Chris Lattner55dc5c72012-05-27 19:37:05 +00002199 SmallVector<Type *, 4> ArgTys;
2200 Assert1(!VerifyIntrinsicType(IFTy->getReturnType(), TableRef, ArgTys),
2201 "Intrinsic has incorrect return type!", IF);
2202 for (unsigned i = 0, e = IFTy->getNumParams(); i != e; ++i)
2203 Assert1(!VerifyIntrinsicType(IFTy->getParamType(i), TableRef, ArgTys),
2204 "Intrinsic has incorrect argument type!", IF);
2205 Assert1(TableRef.empty(), "Intrinsic has too few arguments!", IF);
2206
2207 // Now that we have the intrinsic ID and the actual argument types (and we
2208 // know they are legal for the intrinsic!) get the intrinsic name through the
2209 // usual means. This allows us to verify the mangling of argument types into
2210 // the name.
2211 Assert1(Intrinsic::getName(ID, ArgTys) == IF->getName(),
2212 "Intrinsic name not mangled correctly for type arguments!", IF);
Matt Arsenault3f493222013-07-20 17:46:00 +00002213
Chris Lattnerb241b372009-12-28 09:07:21 +00002214 // If the intrinsic takes MDNode arguments, verify that they are either global
2215 // or are local to *this* function.
Bill Wendlingd942df22010-06-07 19:18:58 +00002216 for (unsigned i = 0, e = CI.getNumArgOperands(); i != e; ++i)
2217 if (MDNode *MD = dyn_cast<MDNode>(CI.getArgOperand(i)))
Duncan Sandse704d9d2010-04-29 16:10:30 +00002218 visitMDNode(*MD, CI.getParent()->getParent());
Victor Hernandez5d301622009-12-18 20:09:14 +00002219
Gordon Henriksen8c33da52007-09-17 20:30:04 +00002220 switch (ID) {
2221 default:
2222 break;
Chandler Carruthc4eab902011-12-12 04:36:02 +00002223 case Intrinsic::ctlz: // llvm.ctlz
2224 case Intrinsic::cttz: // llvm.cttz
2225 Assert1(isa<ConstantInt>(CI.getArgOperand(1)),
2226 "is_zero_undef argument of bit counting intrinsics must be a "
2227 "constant int", &CI);
2228 break;
Victor Hernandez02d574d2010-01-22 19:06:12 +00002229 case Intrinsic::dbg_declare: { // llvm.dbg.declare
Gabor Greifb37a64a2010-06-23 13:56:57 +00002230 Assert1(CI.getArgOperand(0) && isa<MDNode>(CI.getArgOperand(0)),
Victor Hernandez02d574d2010-01-22 19:06:12 +00002231 "invalid llvm.dbg.declare intrinsic call 1", &CI);
Gabor Greifb37a64a2010-06-23 13:56:57 +00002232 MDNode *MD = cast<MDNode>(CI.getArgOperand(0));
Victor Hernandez02d574d2010-01-22 19:06:12 +00002233 Assert1(MD->getNumOperands() == 1,
2234 "invalid llvm.dbg.declare intrinsic call 2", &CI);
Manman Ren0e29eee2013-07-23 00:22:51 +00002235 if (!DisableDebugInfoVerifier)
2236 Finder.processDeclare(cast<DbgDeclareInst>(&CI));
Victor Hernandez02d574d2010-01-22 19:06:12 +00002237 } break;
Manman Ren0e29eee2013-07-23 00:22:51 +00002238 case Intrinsic::dbg_value: { //llvm.dbg.value
2239 if (!DisableDebugInfoVerifier) {
2240 Assert1(CI.getArgOperand(0) && isa<MDNode>(CI.getArgOperand(0)),
2241 "invalid llvm.dbg.value intrinsic call 1", &CI);
2242 Finder.processValue(cast<DbgValueInst>(&CI));
2243 }
2244 break;
2245 }
Chris Lattner824b9582008-11-21 16:42:48 +00002246 case Intrinsic::memcpy:
2247 case Intrinsic::memmove:
2248 case Intrinsic::memset:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002249 Assert1(isa<ConstantInt>(CI.getArgOperand(3)),
Chris Lattner259f88e2008-08-23 05:31:10 +00002250 "alignment argument of memory intrinsics must be a constant int",
2251 &CI);
Eli Friedmane65e9ee2011-05-31 20:12:07 +00002252 Assert1(isa<ConstantInt>(CI.getArgOperand(4)),
2253 "isvolatile argument of memory intrinsics must be a constant int",
2254 &CI);
Chris Lattner259f88e2008-08-23 05:31:10 +00002255 break;
Bill Wendling955fdeb2008-08-23 09:46:46 +00002256 case Intrinsic::gcroot:
2257 case Intrinsic::gcwrite:
Chris Lattner415b4142008-08-24 20:46:13 +00002258 case Intrinsic::gcread:
2259 if (ID == Intrinsic::gcroot) {
Gordon Henriksena2cbe6c2008-10-25 16:28:35 +00002260 AllocaInst *AI =
Gabor Greifb37a64a2010-06-23 13:56:57 +00002261 dyn_cast<AllocaInst>(CI.getArgOperand(0)->stripPointerCasts());
Talinc87cfb62010-09-30 20:23:47 +00002262 Assert1(AI, "llvm.gcroot parameter #1 must be an alloca.", &CI);
Gabor Greifb37a64a2010-06-23 13:56:57 +00002263 Assert1(isa<Constant>(CI.getArgOperand(1)),
Chris Lattner415b4142008-08-24 20:46:13 +00002264 "llvm.gcroot parameter #2 must be a constant.", &CI);
Talinc87cfb62010-09-30 20:23:47 +00002265 if (!AI->getType()->getElementType()->isPointerTy()) {
2266 Assert1(!isa<ConstantPointerNull>(CI.getArgOperand(1)),
2267 "llvm.gcroot parameter #1 must either be a pointer alloca, "
2268 "or argument #2 must be a non-null constant.", &CI);
2269 }
Chris Lattner415b4142008-08-24 20:46:13 +00002270 }
Nick Lewycky29ef6592009-09-07 20:44:51 +00002271
Chris Lattner415b4142008-08-24 20:46:13 +00002272 Assert1(CI.getParent()->getParent()->hasGC(),
2273 "Enclosing function does not use GC.", &CI);
2274 break;
Duncan Sandsf51edad2007-09-29 16:25:54 +00002275 case Intrinsic::init_trampoline:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002276 Assert1(isa<Function>(CI.getArgOperand(1)->stripPointerCasts()),
Duncan Sandsf51edad2007-09-29 16:25:54 +00002277 "llvm.init_trampoline parameter #2 must resolve to a function.",
2278 &CI);
Gordon Henriksen27acd3a2007-12-25 02:02:10 +00002279 break;
Chris Lattnerd3745472008-10-16 06:00:36 +00002280 case Intrinsic::prefetch:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002281 Assert1(isa<ConstantInt>(CI.getArgOperand(1)) &&
2282 isa<ConstantInt>(CI.getArgOperand(2)) &&
2283 cast<ConstantInt>(CI.getArgOperand(1))->getZExtValue() < 2 &&
2284 cast<ConstantInt>(CI.getArgOperand(2))->getZExtValue() < 4,
Chris Lattnerd3745472008-10-16 06:00:36 +00002285 "invalid arguments to llvm.prefetch",
2286 &CI);
2287 break;
Bill Wendlingc5b795e2008-11-18 23:09:31 +00002288 case Intrinsic::stackprotector:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002289 Assert1(isa<AllocaInst>(CI.getArgOperand(1)->stripPointerCasts()),
Bill Wendlingc5b795e2008-11-18 23:09:31 +00002290 "llvm.stackprotector parameter #2 must resolve to an alloca.",
2291 &CI);
2292 break;
Nick Lewycky321333e2009-10-13 07:57:33 +00002293 case Intrinsic::lifetime_start:
2294 case Intrinsic::lifetime_end:
2295 case Intrinsic::invariant_start:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002296 Assert1(isa<ConstantInt>(CI.getArgOperand(0)),
Nick Lewycky321333e2009-10-13 07:57:33 +00002297 "size argument of memory use markers must be a constant integer",
2298 &CI);
2299 break;
2300 case Intrinsic::invariant_end:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002301 Assert1(isa<ConstantInt>(CI.getArgOperand(1)),
Nick Lewycky321333e2009-10-13 07:57:33 +00002302 "llvm.invariant.end parameter #2 must be a constant integer", &CI);
2303 break;
Gordon Henriksen8c33da52007-09-17 20:30:04 +00002304 }
Chris Lattnerd231fc32002-04-18 20:37:37 +00002305}
2306
Manman Rencd262572013-07-19 00:31:03 +00002307void Verifier::verifyDebugInfo(Module &M) {
2308 // Verify Debug Info.
2309 if (!DisableDebugInfoVerifier) {
Manman Rencd262572013-07-19 00:31:03 +00002310 Finder.processModule(M);
2311
2312 for (DebugInfoFinder::iterator I = Finder.compile_unit_begin(),
2313 E = Finder.compile_unit_end(); I != E; ++I)
2314 Assert1(DICompileUnit(*I).Verify(), "DICompileUnit does not Verify!", *I);
2315 for (DebugInfoFinder::iterator I = Finder.subprogram_begin(),
2316 E = Finder.subprogram_end(); I != E; ++I)
2317 Assert1(DISubprogram(*I).Verify(), "DISubprogram does not Verify!", *I);
2318 for (DebugInfoFinder::iterator I = Finder.global_variable_begin(),
2319 E = Finder.global_variable_end(); I != E; ++I)
2320 Assert1(DIGlobalVariable(*I).Verify(),
2321 "DIGlobalVariable does not Verify!", *I);
2322 for (DebugInfoFinder::iterator I = Finder.type_begin(),
2323 E = Finder.type_end(); I != E; ++I)
2324 Assert1(DIType(*I).Verify(), "DIType does not Verify!", *I);
Manman Ren96ea0382013-07-20 00:38:46 +00002325 for (DebugInfoFinder::iterator I = Finder.scope_begin(),
2326 E = Finder.scope_end(); I != E; ++I)
2327 Assert1(DIScope(*I).Verify(), "DIScope does not Verify!", *I);
Manman Rencd262572013-07-19 00:31:03 +00002328 }
2329}
2330
Chris Lattnerd231fc32002-04-18 20:37:37 +00002331//===----------------------------------------------------------------------===//
2332// Implement the public interfaces to this file...
2333//===----------------------------------------------------------------------===//
2334
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002335FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) {
2336 return new Verifier(action);
Chris Lattnerd231fc32002-04-18 20:37:37 +00002337}
2338
Chris Lattner9ce231f2002-08-02 17:37:08 +00002339
Dan Gohmanbcf9f002010-04-08 15:57:10 +00002340/// verifyFunction - Check a function for errors, printing messages on stderr.
2341/// Return true if the function is corrupt.
2342///
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002343bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) {
Chris Lattner2eff8592004-03-14 03:16:15 +00002344 Function &F = const_cast<Function&>(f);
Reid Spencer5cbf9852007-01-30 20:08:39 +00002345 assert(!F.isDeclaration() && "Cannot verify external functions");
Misha Brukmanfd939082005-04-21 23:48:37 +00002346
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002347 FunctionPassManager FPM(F.getParent());
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002348 Verifier *V = new Verifier(action);
Chris Lattner2eff8592004-03-14 03:16:15 +00002349 FPM.add(V);
2350 FPM.run(F);
2351 return V->Broken;
Chris Lattner44d5bd92002-02-20 17:55:43 +00002352}
2353
Misha Brukmanab5c6002004-03-02 00:22:19 +00002354/// verifyModule - Check a module for errors, printing messages on stderr.
2355/// Return true if the module is corrupt.
2356///
Chris Lattner05ac92c2006-07-06 18:02:27 +00002357bool llvm::verifyModule(const Module &M, VerifierFailureAction action,
2358 std::string *ErrorInfo) {
Chris Lattner9ce231f2002-08-02 17:37:08 +00002359 PassManager PM;
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002360 Verifier *V = new Verifier(action);
Chris Lattner9ce231f2002-08-02 17:37:08 +00002361 PM.add(V);
Dan Gohman78f39da2008-06-24 17:47:37 +00002362 PM.run(const_cast<Module&>(M));
Nick Lewycky29ef6592009-09-07 20:44:51 +00002363
Chris Lattner05ac92c2006-07-06 18:02:27 +00002364 if (ErrorInfo && V->Broken)
Chris Lattner37f077a2009-08-23 04:02:03 +00002365 *ErrorInfo = V->MessagesStr.str();
Chris Lattner9ce231f2002-08-02 17:37:08 +00002366 return V->Broken;
Chris Lattner00950542001-06-06 20:29:01 +00002367}