blob: 1d495b7ed46f4dda169df0c7617c442024527b46 [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"
59#include "llvm/IR/DerivedTypes.h"
60#include "llvm/IR/InlineAsm.h"
61#include "llvm/IR/IntrinsicInst.h"
62#include "llvm/IR/LLVMContext.h"
63#include "llvm/IR/Metadata.h"
64#include "llvm/IR/Module.h"
Chandler Carruth84bcf932012-11-30 03:08:41 +000065#include "llvm/InstVisitor.h"
Chris Lattner58d74912008-03-12 17:45:29 +000066#include "llvm/Pass.h"
Chris Lattnercf899082004-02-14 02:47:17 +000067#include "llvm/PassManager.h"
Chris Lattner44d5bd92002-02-20 17:55:43 +000068#include "llvm/Support/CFG.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000069#include "llvm/Support/CallSite.h"
Manman Rencd262572013-07-19 00:31:03 +000070#include "llvm/Support/CommandLine.h"
Rafael Espindolaa1b95f52012-05-31 16:04:26 +000071#include "llvm/Support/ConstantRange.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000072#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000073#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc2871372009-02-28 21:05:51 +000074#include "llvm/Support/raw_ostream.h"
Chris Lattner44d5bd92002-02-20 17:55:43 +000075#include <algorithm>
Jeff Cohen4c5701d2006-03-31 07:22:05 +000076#include <cstdarg>
Chris Lattner31f84992003-11-21 20:23:48 +000077using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000078
Manman Rencd262572013-07-19 00:31:03 +000079static cl::opt<bool> DisableDebugInfoVerifier("disable-debug-info-verifier",
80 cl::init(false));
81
Chris Lattnerd231fc32002-04-18 20:37:37 +000082namespace { // Anonymous namespace for class
Nick Lewycky6726b6d2009-10-25 06:33:48 +000083 struct PreVerifier : public FunctionPass {
Owen Anderson765d6452007-11-01 03:54:23 +000084 static char ID; // Pass ID, replacement for typeid
Duncan Sandsd0561902007-11-01 10:50:26 +000085
Owen Anderson081c34b2010-10-19 17:21:58 +000086 PreVerifier() : FunctionPass(ID) {
87 initializePreVerifierPass(*PassRegistry::getPassRegistry());
88 }
Duncan Sandsd0561902007-11-01 10:50:26 +000089
Chris Lattner11240d02008-12-01 03:58:38 +000090 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
91 AU.setPreservesAll();
92 }
93
Duncan Sandsd0561902007-11-01 10:50:26 +000094 // Check that the prerequisites for successful DominatorTree construction
95 // are satisfied.
Owen Anderson765d6452007-11-01 03:54:23 +000096 bool runOnFunction(Function &F) {
Duncan Sandsd0561902007-11-01 10:50:26 +000097 bool Broken = false;
98
99 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
100 if (I->empty() || !I->back().isTerminator()) {
Chris Lattner2a9a8ae2010-06-12 15:50:24 +0000101 dbgs() << "Basic Block in function '" << F.getName()
102 << "' does not have terminator!\n";
David Greene4b121682010-01-05 01:29:14 +0000103 WriteAsOperand(dbgs(), I, true);
104 dbgs() << "\n";
Duncan Sandsd0561902007-11-01 10:50:26 +0000105 Broken = true;
106 }
107 }
108
109 if (Broken)
Chris Lattner75361b62010-04-07 22:58:41 +0000110 report_fatal_error("Broken module, no Basic Block terminator!");
Duncan Sandsd0561902007-11-01 10:50:26 +0000111
112 return false;
Owen Anderson765d6452007-11-01 03:54:23 +0000113 }
Owen Andersonc570e332007-10-31 21:04:18 +0000114 };
Dan Gohman844731a2008-05-13 00:00:25 +0000115}
Duncan Sandsd0561902007-11-01 10:50:26 +0000116
Dan Gohman844731a2008-05-13 00:00:25 +0000117char PreVerifier::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000118INITIALIZE_PASS(PreVerifier, "preverify", "Preliminary module verification",
Owen Andersonce665bd2010-10-07 22:25:06 +0000119 false, false)
Benjamin Kramera3ac4272010-10-22 17:35:07 +0000120static char &PreVerifyID = PreVerifier::ID;
Duncan Sandsd0561902007-11-01 10:50:26 +0000121
Dan Gohman844731a2008-05-13 00:00:25 +0000122namespace {
Nick Lewyckybc6836b2009-09-13 23:45:39 +0000123 struct Verifier : public FunctionPass, public InstVisitor<Verifier> {
Devang Patel19974732007-05-03 01:11:54 +0000124 static char ID; // Pass ID, replacement for typeid
Chris Lattner9ce231f2002-08-02 17:37:08 +0000125 bool Broken; // Is this module found to be broken?
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000126 VerifierFailureAction action;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000127 // What to do if verification fails.
Misha Brukmanab5c6002004-03-02 00:22:19 +0000128 Module *Mod; // Module we are verifying right now
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000129 LLVMContext *Context; // Context within which we are verifying
130 DominatorTree *DT; // Dominator Tree, caution can be null!
Nick Lewycky29ef6592009-09-07 20:44:51 +0000131
Chris Lattner37f077a2009-08-23 04:02:03 +0000132 std::string Messages;
133 raw_string_ostream MessagesStr;
Chris Lattner00950542001-06-06 20:29:01 +0000134
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000135 /// InstInThisBlock - when verifying a basic block, keep track of all of the
136 /// instructions we have seen so far. This allows us to do efficient
137 /// dominance checks for the case when an instruction has an operand that is
138 /// an instruction in the same block.
Chris Lattner78287b42007-02-10 08:19:44 +0000139 SmallPtrSet<Instruction*, 16> InstsInThisBlock;
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000140
Duncan Sandse704d9d2010-04-29 16:10:30 +0000141 /// MDNodes - keep track of the metadata nodes that have been checked
142 /// already.
143 SmallPtrSet<MDNode *, 32> MDNodes;
144
Bill Wendlinge6e88262011-08-12 20:24:12 +0000145 /// PersonalityFn - The personality function referenced by the
146 /// LandingPadInsts. All LandingPadInsts within the same function must use
147 /// the same personality function.
148 const Value *PersonalityFn;
149
Misha Brukmanfd939082005-04-21 23:48:37 +0000150 Verifier()
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000151 : FunctionPass(ID), Broken(false),
Bill Wendlinge6e88262011-08-12 20:24:12 +0000152 action(AbortProcessAction), Mod(0), Context(0), DT(0),
153 MessagesStr(Messages), PersonalityFn(0) {
154 initializeVerifierPass(*PassRegistry::getPassRegistry());
155 }
Dan Gohman950a4c42008-03-25 22:06:05 +0000156 explicit Verifier(VerifierFailureAction ctn)
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000157 : FunctionPass(ID), Broken(false), action(ctn), Mod(0),
Bill Wendlinge6e88262011-08-12 20:24:12 +0000158 Context(0), DT(0), MessagesStr(Messages), PersonalityFn(0) {
159 initializeVerifierPass(*PassRegistry::getPassRegistry());
160 }
Chris Lattner00950542001-06-06 20:29:01 +0000161
Chris Lattner24e845f2002-06-25 15:56:27 +0000162 bool doInitialization(Module &M) {
Brian Gaeke9cebe2d2003-11-16 23:07:42 +0000163 Mod = &M;
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000164 Context = &M.getContext();
Chris Lattner3e1f1442002-09-19 16:12:19 +0000165
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000166 // We must abort before returning back to the pass manager, or else the
167 // pass manager may try to run other passes on the broken module.
168 return abortIfBroken();
Chris Lattnerd231fc32002-04-18 20:37:37 +0000169 }
170
Chris Lattner24e845f2002-06-25 15:56:27 +0000171 bool runOnFunction(Function &F) {
Chris Lattner9ce231f2002-08-02 17:37:08 +0000172 // Get dominator information if we are being run by PassManager
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000173 DT = &getAnalysis<DominatorTree>();
Chris Lattnercd070752007-04-20 23:59:29 +0000174
175 Mod = F.getParent();
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000176 if (!Context) Context = &F.getContext();
Chris Lattnercd070752007-04-20 23:59:29 +0000177
Chris Lattnerd231fc32002-04-18 20:37:37 +0000178 visit(F);
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000179 InstsInThisBlock.clear();
Bill Wendlinge6e88262011-08-12 20:24:12 +0000180 PersonalityFn = 0;
Chris Lattner3e1f1442002-09-19 16:12:19 +0000181
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000182 // We must abort before returning back to the pass manager, or else the
183 // pass manager may try to run other passes on the broken module.
184 return abortIfBroken();
Chris Lattnerd231fc32002-04-18 20:37:37 +0000185 }
186
Chris Lattner24e845f2002-06-25 15:56:27 +0000187 bool doFinalization(Module &M) {
Chris Lattner794caa12002-04-28 16:04:26 +0000188 // Scan through, checking all of the external function's linkage now...
Chris Lattner7c277b32004-06-03 06:38:43 +0000189 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner53997412003-04-16 20:42:40 +0000190 visitGlobalValue(*I);
Chris Lattner794caa12002-04-28 16:04:26 +0000191
Chris Lattner7c277b32004-06-03 06:38:43 +0000192 // Check to make sure function prototypes are okay.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000193 if (I->isDeclaration()) visitFunction(*I);
Chris Lattner7c277b32004-06-03 06:38:43 +0000194 }
195
Reid Spencer0b118202006-01-16 21:12:35 +0000196 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
197 I != E; ++I)
Chris Lattner56998b22004-12-15 20:23:49 +0000198 visitGlobalVariable(*I);
Chris Lattner61b91bc2002-10-06 22:47:32 +0000199
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000200 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
201 I != E; ++I)
202 visitGlobalAlias(*I);
203
Duncan Sandse704d9d2010-04-29 16:10:30 +0000204 for (Module::named_metadata_iterator I = M.named_metadata_begin(),
205 E = M.named_metadata_end(); I != E; ++I)
206 visitNamedMDNode(*I);
207
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000208 visitModuleFlags(M);
209
Manman Rencd262572013-07-19 00:31:03 +0000210 // Verify Debug Info.
211 verifyDebugInfo(M);
212
Chris Lattner3e1f1442002-09-19 16:12:19 +0000213 // If the module is broken, abort at this time.
Reid Spencer7107c3b2006-07-26 16:18:00 +0000214 return abortIfBroken();
Chris Lattnera00409e2002-04-24 19:12:21 +0000215 }
216
Chris Lattner97e52e42002-04-28 21:27:06 +0000217 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
218 AU.setPreservesAll();
Owen Andersonc570e332007-10-31 21:04:18 +0000219 AU.addRequiredID(PreVerifyID);
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000220 AU.addRequired<DominatorTree>();
Chris Lattner97e52e42002-04-28 21:27:06 +0000221 }
222
Misha Brukmanab5c6002004-03-02 00:22:19 +0000223 /// abortIfBroken - If the module is broken and we are supposed to abort on
224 /// this condition, do so.
225 ///
Reid Spencer7107c3b2006-07-26 16:18:00 +0000226 bool abortIfBroken() {
Chris Lattner505569d2008-08-27 17:36:58 +0000227 if (!Broken) return false;
Chris Lattner37f077a2009-08-23 04:02:03 +0000228 MessagesStr << "Broken module found, ";
Chris Lattner505569d2008-08-27 17:36:58 +0000229 switch (action) {
Chris Lattner505569d2008-08-27 17:36:58 +0000230 case AbortProcessAction:
Chris Lattner37f077a2009-08-23 04:02:03 +0000231 MessagesStr << "compilation aborted!\n";
David Greene4b121682010-01-05 01:29:14 +0000232 dbgs() << MessagesStr.str();
Torok Edwindac237e2009-07-08 20:53:28 +0000233 // Client should choose different reaction if abort is not desired
234 abort();
Chris Lattner505569d2008-08-27 17:36:58 +0000235 case PrintMessageAction:
Chris Lattner37f077a2009-08-23 04:02:03 +0000236 MessagesStr << "verification continues.\n";
David Greene4b121682010-01-05 01:29:14 +0000237 dbgs() << MessagesStr.str();
Chris Lattner505569d2008-08-27 17:36:58 +0000238 return false;
239 case ReturnStatusAction:
Chris Lattner37f077a2009-08-23 04:02:03 +0000240 MessagesStr << "compilation terminated.\n";
Nick Lewycky57174882009-03-15 06:40:32 +0000241 return true;
Chris Lattner3e1f1442002-09-19 16:12:19 +0000242 }
Chandler Carruth732f05c2012-01-10 18:08:01 +0000243 llvm_unreachable("Invalid action");
Chris Lattner3e1f1442002-09-19 16:12:19 +0000244 }
245
Chris Lattner53997412003-04-16 20:42:40 +0000246
Chris Lattnerd231fc32002-04-18 20:37:37 +0000247 // Verification methods...
Chris Lattner53997412003-04-16 20:42:40 +0000248 void visitGlobalValue(GlobalValue &GV);
Chris Lattner56998b22004-12-15 20:23:49 +0000249 void visitGlobalVariable(GlobalVariable &GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000250 void visitGlobalAlias(GlobalAlias &GA);
Duncan Sandse704d9d2010-04-29 16:10:30 +0000251 void visitNamedMDNode(NamedMDNode &NMD);
252 void visitMDNode(MDNode &MD, Function *F);
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000253 void visitModuleFlags(Module &M);
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000254 void visitModuleFlag(MDNode *Op, DenseMap<MDString*, MDNode*> &SeenIDs,
255 SmallVectorImpl<MDNode*> &Requirements);
Chris Lattner24e845f2002-06-25 15:56:27 +0000256 void visitFunction(Function &F);
257 void visitBasicBlock(BasicBlock &BB);
Chris Lattnercad208b2008-08-28 04:02:44 +0000258 using InstVisitor<Verifier>::visit;
Nick Lewycky29ef6592009-09-07 20:44:51 +0000259
Chris Lattnercad208b2008-08-28 04:02:44 +0000260 void visit(Instruction &I);
Nick Lewycky29ef6592009-09-07 20:44:51 +0000261
Reid Spencer3da59db2006-11-27 01:05:10 +0000262 void visitTruncInst(TruncInst &I);
263 void visitZExtInst(ZExtInst &I);
264 void visitSExtInst(SExtInst &I);
265 void visitFPTruncInst(FPTruncInst &I);
266 void visitFPExtInst(FPExtInst &I);
267 void visitFPToUIInst(FPToUIInst &I);
268 void visitFPToSIInst(FPToSIInst &I);
269 void visitUIToFPInst(UIToFPInst &I);
270 void visitSIToFPInst(SIToFPInst &I);
271 void visitIntToPtrInst(IntToPtrInst &I);
272 void visitPtrToIntInst(PtrToIntInst &I);
273 void visitBitCastInst(BitCastInst &I);
Chris Lattner24e845f2002-06-25 15:56:27 +0000274 void visitPHINode(PHINode &PN);
275 void visitBinaryOperator(BinaryOperator &B);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000276 void visitICmpInst(ICmpInst &IC);
277 void visitFCmpInst(FCmpInst &FC);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000278 void visitExtractElementInst(ExtractElementInst &EI);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000279 void visitInsertElementInst(InsertElementInst &EI);
Chris Lattner00f10232006-04-08 01:18:18 +0000280 void visitShuffleVectorInst(ShuffleVectorInst &EI);
Chris Lattner4d45bd02003-10-18 05:57:43 +0000281 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
Chris Lattner24e845f2002-06-25 15:56:27 +0000282 void visitCallInst(CallInst &CI);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000283 void visitInvokeInst(InvokeInst &II);
Chris Lattner24e845f2002-06-25 15:56:27 +0000284 void visitGetElementPtrInst(GetElementPtrInst &GEP);
285 void visitLoadInst(LoadInst &LI);
286 void visitStoreInst(StoreInst &SI);
Rafael Espindolac987f4c2012-02-26 02:23:37 +0000287 void verifyDominatesUse(Instruction &I, unsigned i);
Chris Lattner24e845f2002-06-25 15:56:27 +0000288 void visitInstruction(Instruction &I);
289 void visitTerminatorInst(TerminatorInst &I);
Nick Lewycky6a7cb632010-02-15 22:09:09 +0000290 void visitBranchInst(BranchInst &BI);
Chris Lattner24e845f2002-06-25 15:56:27 +0000291 void visitReturnInst(ReturnInst &RI);
Chris Lattner0f9e9d02004-05-21 16:47:21 +0000292 void visitSwitchInst(SwitchInst &SI);
Dan Gohman37680912010-08-02 23:08:33 +0000293 void visitIndirectBrInst(IndirectBrInst &BI);
Chris Lattner230c1a72004-03-12 05:54:31 +0000294 void visitSelectInst(SelectInst &SI);
Chris Lattner627079d2002-11-21 16:54:22 +0000295 void visitUserOp1(Instruction &I);
296 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
Brian Gaeked0fde302003-11-11 22:41:34 +0000297 void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI);
Eli Friedmanff030482011-07-28 21:48:00 +0000298 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI);
299 void visitAtomicRMWInst(AtomicRMWInst &RMWI);
Eli Friedman47f35132011-07-25 23:16:38 +0000300 void visitFenceInst(FenceInst &FI);
Victor Hernandez7b929da2009-10-23 21:09:37 +0000301 void visitAllocaInst(AllocaInst &AI);
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000302 void visitExtractValueInst(ExtractValueInst &EVI);
303 void visitInsertValueInst(InsertValueInst &IVI);
Bill Wendlinge6e88262011-08-12 20:24:12 +0000304 void visitLandingPadInst(LandingPadInst &LPI);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000305
Duncan Sandsd9d70392007-12-21 19:19:01 +0000306 void VerifyCallSite(CallSite CS);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000307 bool PerformTypeCheck(Intrinsic::ID ID, Function *F, Type *Ty,
Bill Wendlinga6ce05f2008-11-13 07:11:27 +0000308 int VT, unsigned ArgNo, std::string &Suffix);
Chris Lattner55dc5c72012-05-27 19:37:05 +0000309 bool VerifyIntrinsicType(Type *Ty,
310 ArrayRef<Intrinsic::IITDescriptor> &Infos,
311 SmallVectorImpl<Type*> &ArgTys);
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000312 bool VerifyAttributeCount(AttributeSet Attrs, unsigned Params);
313 void VerifyAttributeTypes(AttributeSet Attrs, unsigned Idx,
314 bool isFunction, const Value *V);
315 void VerifyParameterAttrs(AttributeSet Attrs, unsigned Idx, Type *Ty,
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000316 bool isReturnValue, const Value *V);
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000317 void VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs,
Duncan Sandscfad1b42008-01-12 16:42:01 +0000318 const Value *V);
Chris Lattner15e87522003-11-21 17:35:51 +0000319
Manman Rencd262572013-07-19 00:31:03 +0000320 void verifyDebugInfo(Module &M);
321
Chris Lattner15e87522003-11-21 17:35:51 +0000322 void WriteValue(const Value *V) {
323 if (!V) return;
Chris Lattner31f84992003-11-21 20:23:48 +0000324 if (isa<Instruction>(V)) {
Nick Lewycky2bb6f6a2009-10-17 19:43:45 +0000325 MessagesStr << *V << '\n';
Chris Lattner31f84992003-11-21 20:23:48 +0000326 } else {
Chris Lattner37f077a2009-08-23 04:02:03 +0000327 WriteAsOperand(MessagesStr, V, true, Mod);
Nick Lewycky2bb6f6a2009-10-17 19:43:45 +0000328 MessagesStr << '\n';
Chris Lattner15e87522003-11-21 17:35:51 +0000329 }
330 }
331
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000332 void WriteType(Type *T) {
Chris Lattnerc2871372009-02-28 21:05:51 +0000333 if (!T) return;
Chris Lattner1afcace2011-07-09 17:41:24 +0000334 MessagesStr << ' ' << *T;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000335 }
336
Chris Lattner15e87522003-11-21 17:35:51 +0000337
Chris Lattnerd231fc32002-04-18 20:37:37 +0000338 // CheckFailed - A check failed, so print out the condition and the message
339 // that failed. This provides a nice place to put a breakpoint if you want
340 // to see why something is not correct.
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000341 void CheckFailed(const Twine &Message,
Chris Lattner15e87522003-11-21 17:35:51 +0000342 const Value *V1 = 0, const Value *V2 = 0,
343 const Value *V3 = 0, const Value *V4 = 0) {
Chris Lattner37f077a2009-08-23 04:02:03 +0000344 MessagesStr << Message.str() << "\n";
Chris Lattner15e87522003-11-21 17:35:51 +0000345 WriteValue(V1);
346 WriteValue(V2);
347 WriteValue(V3);
348 WriteValue(V4);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000349 Broken = true;
350 }
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000351
Nick Lewycky4b6af8a2009-09-08 02:02:39 +0000352 void CheckFailed(const Twine &Message, const Value *V1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000353 Type *T2, const Value *V3 = 0) {
Chris Lattner37f077a2009-08-23 04:02:03 +0000354 MessagesStr << Message.str() << "\n";
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000355 WriteValue(V1);
356 WriteType(T2);
357 WriteValue(V3);
Reid Spencer5dff1582004-05-27 21:58:13 +0000358 Broken = true;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000359 }
Nick Lewycky49072472009-09-08 01:23:52 +0000360
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000361 void CheckFailed(const Twine &Message, Type *T1,
362 Type *T2 = 0, Type *T3 = 0) {
Nick Lewycky49072472009-09-08 01:23:52 +0000363 MessagesStr << Message.str() << "\n";
364 WriteType(T1);
365 WriteType(T2);
366 WriteType(T3);
367 Broken = true;
368 }
Chris Lattnerd231fc32002-04-18 20:37:37 +0000369 };
Chris Lattner31f84992003-11-21 20:23:48 +0000370} // End anonymous namespace
371
Dan Gohman844731a2008-05-13 00:00:25 +0000372char Verifier::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +0000373INITIALIZE_PASS_BEGIN(Verifier, "verify", "Module Verifier", false, false)
374INITIALIZE_PASS_DEPENDENCY(PreVerifier)
375INITIALIZE_PASS_DEPENDENCY(DominatorTree)
376INITIALIZE_PASS_END(Verifier, "verify", "Module Verifier", false, false)
Chris Lattner00950542001-06-06 20:29:01 +0000377
Chris Lattner44d5bd92002-02-20 17:55:43 +0000378// Assert - We know that cond should be true, if not print an error message.
379#define Assert(C, M) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000380 do { if (!(C)) { CheckFailed(M); return; } } while (0)
Chris Lattner44d5bd92002-02-20 17:55:43 +0000381#define Assert1(C, M, V1) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000382 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
Chris Lattner44d5bd92002-02-20 17:55:43 +0000383#define Assert2(C, M, V1, V2) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000384 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
Chris Lattner24e845f2002-06-25 15:56:27 +0000385#define Assert3(C, M, V1, V2, V3) \
386 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
387#define Assert4(C, M, V1, V2, V3, V4) \
388 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
Chris Lattner00950542001-06-06 20:29:01 +0000389
Chris Lattnercad208b2008-08-28 04:02:44 +0000390void Verifier::visit(Instruction &I) {
391 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
392 Assert1(I.getOperand(i) != 0, "Operand is null", &I);
393 InstVisitor<Verifier>::visit(I);
394}
395
396
Chris Lattner53997412003-04-16 20:42:40 +0000397void Verifier::visitGlobalValue(GlobalValue &GV) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000398 Assert1(!GV.isDeclaration() ||
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000399 GV.isMaterializable() ||
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000400 GV.hasExternalLinkage() ||
401 GV.hasDLLImportLinkage() ||
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000402 GV.hasExternalWeakLinkage() ||
403 (isa<GlobalAlias>(GV) &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000404 (GV.hasLocalLinkage() || GV.hasWeakLinkage())),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000405 "Global is external, but doesn't have external or dllimport or weak linkage!",
406 &GV);
407
Reid Spencer5cbf9852007-01-30 20:08:39 +0000408 Assert1(!GV.hasDLLImportLinkage() || GV.isDeclaration(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000409 "Global is marked as dllimport, but not external", &GV);
Nick Lewycky49072472009-09-08 01:23:52 +0000410
Chris Lattner53997412003-04-16 20:42:40 +0000411 Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
412 "Only global variables can have appending linkage!", &GV);
413
414 if (GV.hasAppendingLinkage()) {
Nick Lewycky49072472009-09-08 01:23:52 +0000415 GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
Duncan Sands1df98592010-02-16 11:11:14 +0000416 Assert1(GVar && GVar->getType()->getElementType()->isArrayTy(),
Nick Lewycky49072472009-09-08 01:23:52 +0000417 "Only global arrays can have appending linkage!", GVar);
Chris Lattner53997412003-04-16 20:42:40 +0000418 }
Bill Wendling55ae5152010-08-20 22:05:50 +0000419
Bill Wendling32811be2012-08-17 18:33:14 +0000420 Assert1(!GV.hasLinkOnceODRAutoHideLinkage() || GV.hasDefaultVisibility(),
421 "linkonce_odr_auto_hide can only have default visibility!",
Bill Wendling55ae5152010-08-20 22:05:50 +0000422 &GV);
Chris Lattner53997412003-04-16 20:42:40 +0000423}
424
Chris Lattner56998b22004-12-15 20:23:49 +0000425void Verifier::visitGlobalVariable(GlobalVariable &GV) {
Chris Lattner6693da02007-09-19 17:14:45 +0000426 if (GV.hasInitializer()) {
Chris Lattner56998b22004-12-15 20:23:49 +0000427 Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(),
428 "Global variable initializer type does not match global "
429 "variable type!", &GV);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000430
Chris Lattnercd81f5d2009-08-05 05:41:44 +0000431 // If the global has common linkage, it must have a zero initializer and
432 // cannot be constant.
433 if (GV.hasCommonLinkage()) {
Chris Lattner26d054d2009-08-05 05:21:07 +0000434 Assert1(GV.getInitializer()->isNullValue(),
435 "'common' global must have a zero initializer!", &GV);
Chris Lattnercd81f5d2009-08-05 05:41:44 +0000436 Assert1(!GV.isConstant(), "'common' global may not be marked constant!",
437 &GV);
438 }
Chris Lattner6693da02007-09-19 17:14:45 +0000439 } else {
440 Assert1(GV.hasExternalLinkage() || GV.hasDLLImportLinkage() ||
441 GV.hasExternalWeakLinkage(),
442 "invalid linkage type for global declaration", &GV);
443 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000444
Nick Lewycky2c44a802011-04-08 07:30:21 +0000445 if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
446 GV.getName() == "llvm.global_dtors")) {
447 Assert1(!GV.hasInitializer() || GV.hasAppendingLinkage(),
448 "invalid linkage for intrinsic global variable", &GV);
449 // Don't worry about emitting an error for it not being an array,
450 // visitGlobalValue will complain on appending non-array.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000451 if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getType())) {
452 StructType *STy = dyn_cast<StructType>(ATy->getElementType());
453 PointerType *FuncPtrTy =
Micah Villmowb8bce922012-10-24 17:25:11 +0000454 FunctionType::get(Type::getVoidTy(*Context), false)->getPointerTo();
Nick Lewycky2c44a802011-04-08 07:30:21 +0000455 Assert1(STy && STy->getNumElements() == 2 &&
456 STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
457 STy->getTypeAtIndex(1) == FuncPtrTy,
458 "wrong type for intrinsic global variable", &GV);
459 }
460 }
461
Rafael Espindola97bf57d2013-04-22 15:16:51 +0000462 if (GV.hasName() && (GV.getName() == "llvm.used" ||
Rafael Espindola70968312013-07-19 18:44:51 +0000463 GV.getName() == "llvm.compiler.used")) {
Rafael Espindolacde25b42013-04-22 14:58:02 +0000464 Assert1(!GV.hasInitializer() || GV.hasAppendingLinkage(),
465 "invalid linkage for intrinsic global variable", &GV);
466 Type *GVType = GV.getType()->getElementType();
467 if (ArrayType *ATy = dyn_cast<ArrayType>(GVType)) {
468 PointerType *PTy = dyn_cast<PointerType>(ATy->getElementType());
469 Assert1(PTy, "wrong type for intrinsic global variable", &GV);
470 if (GV.hasInitializer()) {
471 Constant *Init = GV.getInitializer();
472 ConstantArray *InitArray = dyn_cast<ConstantArray>(Init);
473 Assert1(InitArray, "wrong initalizer for intrinsic global variable",
474 Init);
475 for (unsigned i = 0, e = InitArray->getNumOperands(); i != e; ++i) {
Rafael Espindolaaf10fe62013-05-27 22:47:09 +0000476 Value *V = Init->getOperand(i)->stripPointerCastsNoFollowAliases();
477 Assert1(
478 isa<GlobalVariable>(V) || isa<Function>(V) || isa<GlobalAlias>(V),
479 "invalid llvm.used member", V);
Rafael Espindola9f8e6da2013-06-11 13:18:13 +0000480 Assert1(V->hasName(), "members of llvm.used must be named", V);
Rafael Espindolacde25b42013-04-22 14:58:02 +0000481 }
482 }
483 }
484 }
485
Chris Lattner56998b22004-12-15 20:23:49 +0000486 visitGlobalValue(GV);
487}
488
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000489void Verifier::visitGlobalAlias(GlobalAlias &GA) {
490 Assert1(!GA.getName().empty(),
491 "Alias name cannot be empty!", &GA);
Rafael Espindolabb46f522009-01-15 20:18:42 +0000492 Assert1(GA.hasExternalLinkage() || GA.hasLocalLinkage() ||
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000493 GA.hasWeakLinkage(),
494 "Alias should have external or external weak linkage!", &GA);
Anton Korobeynikov018f7712008-05-08 23:11:06 +0000495 Assert1(GA.getAliasee(),
496 "Aliasee cannot be NULL!", &GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000497 Assert1(GA.getType() == GA.getAliasee()->getType(),
498 "Alias and aliasee types should match!", &GA);
Rafael Espindolabea46262011-01-08 16:42:36 +0000499 Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA);
Anton Korobeynikov018f7712008-05-08 23:11:06 +0000500
Anton Korobeynikov0f53f7f2007-04-28 14:35:41 +0000501 if (!isa<GlobalValue>(GA.getAliasee())) {
502 const ConstantExpr *CE = dyn_cast<ConstantExpr>(GA.getAliasee());
Chris Lattnera2165ed2009-04-25 21:23:19 +0000503 Assert1(CE &&
504 (CE->getOpcode() == Instruction::BitCast ||
505 CE->getOpcode() == Instruction::GetElementPtr) &&
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000506 isa<GlobalValue>(CE->getOperand(0)),
Anton Korobeynikov0f53f7f2007-04-28 14:35:41 +0000507 "Aliasee should be either GlobalValue or bitcast of GlobalValue",
508 &GA);
509 }
Anton Korobeynikov726d45c2008-03-22 08:36:14 +0000510
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000511 const GlobalValue* Aliasee = GA.resolveAliasedGlobal(/*stopOnWeak*/ false);
Anton Korobeynikov726d45c2008-03-22 08:36:14 +0000512 Assert1(Aliasee,
Anton Korobeynikovef30c1d2008-03-22 08:37:05 +0000513 "Aliasing chain should end with function or global variable", &GA);
Anton Korobeynikov726d45c2008-03-22 08:36:14 +0000514
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000515 visitGlobalValue(GA);
516}
517
Duncan Sandse704d9d2010-04-29 16:10:30 +0000518void Verifier::visitNamedMDNode(NamedMDNode &NMD) {
519 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) {
520 MDNode *MD = NMD.getOperand(i);
521 if (!MD)
522 continue;
523
Dan Gohman17aa92c2010-07-21 23:38:33 +0000524 Assert1(!MD->isFunctionLocal(),
525 "Named metadata operand cannot be function local!", MD);
Duncan Sandse704d9d2010-04-29 16:10:30 +0000526 visitMDNode(*MD, 0);
527 }
528}
529
530void Verifier::visitMDNode(MDNode &MD, Function *F) {
531 // Only visit each node once. Metadata can be mutually recursive, so this
532 // avoids infinite recursion here, as well as being an optimization.
533 if (!MDNodes.insert(&MD))
534 return;
535
536 for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) {
537 Value *Op = MD.getOperand(i);
538 if (!Op)
539 continue;
Dan Gohman557c83c2010-07-21 20:25:43 +0000540 if (isa<Constant>(Op) || isa<MDString>(Op))
Duncan Sandse704d9d2010-04-29 16:10:30 +0000541 continue;
542 if (MDNode *N = dyn_cast<MDNode>(Op)) {
543 Assert2(MD.isFunctionLocal() || !N->isFunctionLocal(),
544 "Global metadata operand cannot be function local!", &MD, N);
545 visitMDNode(*N, F);
546 continue;
547 }
548 Assert2(MD.isFunctionLocal(), "Invalid operand for global metadata!", &MD, Op);
549
550 // If this was an instruction, bb, or argument, verify that it is in the
551 // function that we expect.
552 Function *ActualF = 0;
553 if (Instruction *I = dyn_cast<Instruction>(Op))
554 ActualF = I->getParent()->getParent();
555 else if (BasicBlock *BB = dyn_cast<BasicBlock>(Op))
556 ActualF = BB->getParent();
557 else if (Argument *A = dyn_cast<Argument>(Op))
558 ActualF = A->getParent();
559 assert(ActualF && "Unimplemented function local metadata case!");
560
561 Assert2(ActualF == F, "function-local metadata used in wrong function",
562 &MD, Op);
563 }
564}
565
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000566void Verifier::visitModuleFlags(Module &M) {
567 const NamedMDNode *Flags = M.getModuleFlagsMetadata();
568 if (!Flags) return;
569
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000570 // Scan each flag, and track the flags and requirements.
571 DenseMap<MDString*, MDNode*> SeenIDs;
572 SmallVector<MDNode*, 16> Requirements;
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000573 for (unsigned I = 0, E = Flags->getNumOperands(); I != E; ++I) {
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000574 visitModuleFlag(Flags->getOperand(I), SeenIDs, Requirements);
575 }
576
577 // Validate that the requirements in the module are valid.
578 for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
579 MDNode *Requirement = Requirements[I];
580 MDString *Flag = cast<MDString>(Requirement->getOperand(0));
581 Value *ReqValue = Requirement->getOperand(1);
582
583 MDNode *Op = SeenIDs.lookup(Flag);
584 if (!Op) {
585 CheckFailed("invalid requirement on flag, flag is not present in module",
586 Flag);
587 continue;
588 }
589
590 if (Op->getOperand(2) != ReqValue) {
591 CheckFailed(("invalid requirement on flag, "
592 "flag does not have the required value"),
593 Flag);
594 continue;
595 }
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000596 }
597}
598
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000599void Verifier::visitModuleFlag(MDNode *Op, DenseMap<MDString*, MDNode*>&SeenIDs,
600 SmallVectorImpl<MDNode*> &Requirements) {
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000601 // Each module flag should have three arguments, the merge behavior (a
602 // constant int), the flag ID (an MDString), and the value.
603 Assert1(Op->getNumOperands() == 3,
604 "incorrect number of operands in module flag", Op);
605 ConstantInt *Behavior = dyn_cast<ConstantInt>(Op->getOperand(0));
606 MDString *ID = dyn_cast<MDString>(Op->getOperand(1));
607 Assert1(Behavior,
608 "invalid behavior operand in module flag (expected constant integer)",
609 Op->getOperand(0));
610 unsigned BehaviorValue = Behavior->getZExtValue();
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000611 Assert1(ID,
612 "invalid ID operand in module flag (expected metadata string)",
613 Op->getOperand(1));
614
Daniel Dunbar5db391c2013-01-16 21:38:56 +0000615 // Sanity check the values for behaviors with additional requirements.
616 switch (BehaviorValue) {
617 default:
618 Assert1(false,
619 "invalid behavior operand in module flag (unexpected constant)",
620 Op->getOperand(0));
621 break;
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000622
Daniel Dunbar5db391c2013-01-16 21:38:56 +0000623 case Module::Error:
624 case Module::Warning:
625 case Module::Override:
626 // These behavior types accept any value.
627 break;
628
629 case Module::Require: {
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000630 // The value should itself be an MDNode with two operands, a flag ID (an
631 // MDString), and a value.
632 MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2));
633 Assert1(Value && Value->getNumOperands() == 2,
634 "invalid value for 'require' module flag (expected metadata pair)",
635 Op->getOperand(2));
636 Assert1(isa<MDString>(Value->getOperand(0)),
637 ("invalid value for 'require' module flag "
638 "(first value operand should be a string)"),
639 Value->getOperand(0));
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000640
641 // Append it to the list of requirements, to check once all module flags are
642 // scanned.
643 Requirements.push_back(Value);
Daniel Dunbar5db391c2013-01-16 21:38:56 +0000644 break;
645 }
646
647 case Module::Append:
648 case Module::AppendUnique: {
649 // These behavior types require the operand be an MDNode.
650 Assert1(isa<MDNode>(Op->getOperand(2)),
651 "invalid value for 'append'-type module flag "
652 "(expected a metadata node)", Op->getOperand(2));
653 break;
654 }
655 }
656
657 // Unless this is a "requires" flag, check the ID is unique.
658 if (BehaviorValue != Module::Require) {
659 bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
660 Assert1(Inserted,
661 "module flag identifiers must be unique (or of 'require' type)",
662 ID);
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000663 }
664}
665
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000666void Verifier::VerifyAttributeTypes(AttributeSet Attrs, unsigned Idx,
Nick Lewyckydc897372013-07-06 00:29:58 +0000667 bool isFunction, const Value *V) {
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000668 unsigned Slot = ~0U;
669 for (unsigned I = 0, E = Attrs.getNumSlots(); I != E; ++I)
670 if (Attrs.getSlotIndex(I) == Idx) {
671 Slot = I;
672 break;
673 }
674
675 assert(Slot != ~0U && "Attribute set inconsistency!");
676
677 for (AttributeSet::iterator I = Attrs.begin(Slot), E = Attrs.end(Slot);
678 I != E; ++I) {
679 if (I->isStringAttribute())
680 continue;
681
682 if (I->getKindAsEnum() == Attribute::NoReturn ||
683 I->getKindAsEnum() == Attribute::NoUnwind ||
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000684 I->getKindAsEnum() == Attribute::NoInline ||
685 I->getKindAsEnum() == Attribute::AlwaysInline ||
686 I->getKindAsEnum() == Attribute::OptimizeForSize ||
687 I->getKindAsEnum() == Attribute::StackProtect ||
688 I->getKindAsEnum() == Attribute::StackProtectReq ||
689 I->getKindAsEnum() == Attribute::StackProtectStrong ||
690 I->getKindAsEnum() == Attribute::NoRedZone ||
691 I->getKindAsEnum() == Attribute::NoImplicitFloat ||
692 I->getKindAsEnum() == Attribute::Naked ||
693 I->getKindAsEnum() == Attribute::InlineHint ||
694 I->getKindAsEnum() == Attribute::StackAlignment ||
695 I->getKindAsEnum() == Attribute::UWTable ||
696 I->getKindAsEnum() == Attribute::NonLazyBind ||
697 I->getKindAsEnum() == Attribute::ReturnsTwice ||
698 I->getKindAsEnum() == Attribute::SanitizeAddress ||
699 I->getKindAsEnum() == Attribute::SanitizeThread ||
700 I->getKindAsEnum() == Attribute::SanitizeMemory ||
701 I->getKindAsEnum() == Attribute::MinSize ||
702 I->getKindAsEnum() == Attribute::NoDuplicate ||
Michael Gottesman2253a2f2013-06-27 00:25:01 +0000703 I->getKindAsEnum() == Attribute::Builtin ||
Diego Novillo77226a02013-05-24 12:26:52 +0000704 I->getKindAsEnum() == Attribute::NoBuiltin ||
705 I->getKindAsEnum() == Attribute::Cold) {
Tobias Grosser068acc52013-07-02 03:28:10 +0000706 if (!isFunction) {
Nick Lewyckydc897372013-07-06 00:29:58 +0000707 CheckFailed("Attribute '" + I->getAsString() +
708 "' only applies to functions!", V);
709 return;
710 }
711 } else if (I->getKindAsEnum() == Attribute::ReadOnly ||
712 I->getKindAsEnum() == Attribute::ReadNone) {
713 if (Idx == 0) {
714 CheckFailed("Attribute '" + I->getAsString() +
715 "' does not apply to function returns");
716 return;
Tobias Grosser068acc52013-07-02 03:28:10 +0000717 }
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000718 } else if (isFunction) {
Nick Lewyckydc897372013-07-06 00:29:58 +0000719 CheckFailed("Attribute '" + I->getAsString() +
720 "' does not apply to functions!", V);
721 return;
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000722 }
723 }
724}
725
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000726// VerifyParameterAttrs - Check the given attributes for an argument or return
Duncan Sandscfad1b42008-01-12 16:42:01 +0000727// value of the specified type. The value V is printed in error messages.
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000728void Verifier::VerifyParameterAttrs(AttributeSet Attrs, unsigned Idx, Type *Ty,
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000729 bool isReturnValue, const Value *V) {
Bill Wendlingbed80592013-01-21 23:03:18 +0000730 if (!Attrs.hasAttributes(Idx))
Duncan Sandscfad1b42008-01-12 16:42:01 +0000731 return;
732
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000733 VerifyAttributeTypes(Attrs, Idx, false, V);
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000734
Bill Wendling943c2912012-10-09 09:51:10 +0000735 if (isReturnValue)
Bill Wendlingbed80592013-01-21 23:03:18 +0000736 Assert1(!Attrs.hasAttribute(Idx, Attribute::ByVal) &&
737 !Attrs.hasAttribute(Idx, Attribute::Nest) &&
738 !Attrs.hasAttribute(Idx, Attribute::StructRet) &&
Stephen Lin456ca042013-04-20 05:14:40 +0000739 !Attrs.hasAttribute(Idx, Attribute::NoCapture) &&
740 !Attrs.hasAttribute(Idx, Attribute::Returned),
741 "Attribute 'byval', 'nest', 'sret', 'nocapture', and 'returned' "
Bill Wendling943c2912012-10-09 09:51:10 +0000742 "do not apply to return values!", V);
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000743
Bill Wendling28d1c602012-10-09 20:11:19 +0000744 // Check for mutually incompatible attributes.
Bill Wendlingbed80592013-01-21 23:03:18 +0000745 Assert1(!((Attrs.hasAttribute(Idx, Attribute::ByVal) &&
746 Attrs.hasAttribute(Idx, Attribute::Nest)) ||
747 (Attrs.hasAttribute(Idx, Attribute::ByVal) &&
748 Attrs.hasAttribute(Idx, Attribute::StructRet)) ||
749 (Attrs.hasAttribute(Idx, Attribute::Nest) &&
750 Attrs.hasAttribute(Idx, Attribute::StructRet))), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000751 "'byval, nest, and sret' are incompatible!", V);
752
Bill Wendlingbed80592013-01-21 23:03:18 +0000753 Assert1(!((Attrs.hasAttribute(Idx, Attribute::ByVal) &&
754 Attrs.hasAttribute(Idx, Attribute::Nest)) ||
755 (Attrs.hasAttribute(Idx, Attribute::ByVal) &&
756 Attrs.hasAttribute(Idx, Attribute::InReg)) ||
757 (Attrs.hasAttribute(Idx, Attribute::Nest) &&
758 Attrs.hasAttribute(Idx, Attribute::InReg))), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000759 "'byval, nest, and inreg' are incompatible!", V);
760
Stephen Lin13aba142013-04-23 16:31:56 +0000761 Assert1(!(Attrs.hasAttribute(Idx, Attribute::StructRet) &&
762 Attrs.hasAttribute(Idx, Attribute::Returned)), "Attributes "
763 "'sret and returned' are incompatible!", V);
764
Bill Wendlingbed80592013-01-21 23:03:18 +0000765 Assert1(!(Attrs.hasAttribute(Idx, Attribute::ZExt) &&
766 Attrs.hasAttribute(Idx, Attribute::SExt)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000767 "'zeroext and signext' are incompatible!", V);
768
Bill Wendlingbed80592013-01-21 23:03:18 +0000769 Assert1(!(Attrs.hasAttribute(Idx, Attribute::ReadNone) &&
770 Attrs.hasAttribute(Idx, Attribute::ReadOnly)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000771 "'readnone and readonly' are incompatible!", V);
772
Bill Wendlingbed80592013-01-21 23:03:18 +0000773 Assert1(!(Attrs.hasAttribute(Idx, Attribute::NoInline) &&
774 Attrs.hasAttribute(Idx, Attribute::AlwaysInline)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000775 "'noinline and alwaysinline' are incompatible!", V);
Duncan Sandscfad1b42008-01-12 16:42:01 +0000776
Bill Wendlingbed80592013-01-21 23:03:18 +0000777 Assert1(!AttrBuilder(Attrs, Idx).
Bill Wendlinge7436542013-01-30 23:07:40 +0000778 hasAttributes(AttributeFuncs::typeIncompatible(Ty, Idx), Idx),
Bill Wendling1feacad2012-10-14 07:52:48 +0000779 "Wrong types for attribute: " +
Bill Wendlinge7436542013-01-30 23:07:40 +0000780 AttributeFuncs::typeIncompatible(Ty, Idx).getAsString(Idx), V);
Dan Gohman39dfc2c2008-08-27 14:48:06 +0000781
Bill Wendling943c2912012-10-09 09:51:10 +0000782 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
Bill Wendlingbed80592013-01-21 23:03:18 +0000783 Assert1(!Attrs.hasAttribute(Idx, Attribute::ByVal) ||
Bill Wendling943c2912012-10-09 09:51:10 +0000784 PTy->getElementType()->isSized(),
785 "Attribute 'byval' does not support unsized types!", V);
786 else
Bill Wendlingbed80592013-01-21 23:03:18 +0000787 Assert1(!Attrs.hasAttribute(Idx, Attribute::ByVal),
Bill Wendling943c2912012-10-09 09:51:10 +0000788 "Attribute 'byval' only applies to parameters with pointer type!",
789 V);
Duncan Sandscfad1b42008-01-12 16:42:01 +0000790}
791
792// VerifyFunctionAttrs - Check parameter attributes against a function type.
Duncan Sandsd9d70392007-12-21 19:19:01 +0000793// The value V is printed in error messages.
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000794void Verifier::VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs,
Duncan Sandscfad1b42008-01-12 16:42:01 +0000795 const Value *V) {
Chris Lattner58d74912008-03-12 17:45:29 +0000796 if (Attrs.isEmpty())
Duncan Sandsd9d70392007-12-21 19:19:01 +0000797 return;
798
Duncan Sandsd9d70392007-12-21 19:19:01 +0000799 bool SawNest = false;
Stephen Lin456ca042013-04-20 05:14:40 +0000800 bool SawReturned = false;
Duncan Sandsd9d70392007-12-21 19:19:01 +0000801
Chris Lattner58d74912008-03-12 17:45:29 +0000802 for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000803 unsigned Idx = Attrs.getSlotIndex(i);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000804
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000805 Type *Ty;
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000806 if (Idx == 0)
Chris Lattner58d74912008-03-12 17:45:29 +0000807 Ty = FT->getReturnType();
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000808 else if (Idx-1 < FT->getNumParams())
809 Ty = FT->getParamType(Idx-1);
Chris Lattner58d74912008-03-12 17:45:29 +0000810 else
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000811 break; // VarArgs attributes, verified elsewhere.
812
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000813 VerifyParameterAttrs(Attrs, Idx, Ty, Idx == 0, V);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000814
Stephen Lin456ca042013-04-20 05:14:40 +0000815 if (Idx == 0)
816 continue;
817
818 if (Attrs.hasAttribute(Idx, Attribute::Nest)) {
Duncan Sandsd9d70392007-12-21 19:19:01 +0000819 Assert1(!SawNest, "More than one parameter has attribute nest!", V);
820 SawNest = true;
821 }
822
Stephen Lin456ca042013-04-20 05:14:40 +0000823 if (Attrs.hasAttribute(Idx, Attribute::Returned)) {
824 Assert1(!SawReturned, "More than one parameter has attribute returned!",
825 V);
826 Assert1(Ty->canLosslesslyBitCastTo(FT->getReturnType()), "Incompatible "
827 "argument and return types for 'returned' attribute", V);
828 SawReturned = true;
829 }
830
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000831 if (Attrs.hasAttribute(Idx, Attribute::StructRet))
832 Assert1(Idx == 1, "Attribute sret is not on first parameter!", V);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000833 }
Devang Patel7c310852008-10-01 23:41:25 +0000834
Bill Wendling956f1342013-01-18 21:11:39 +0000835 if (!Attrs.hasAttributes(AttributeSet::FunctionIndex))
836 return;
837
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000838 VerifyAttributeTypes(Attrs, AttributeSet::FunctionIndex, true, V);
Bill Wendling28d1c602012-10-09 20:11:19 +0000839
Bill Wendling956f1342013-01-18 21:11:39 +0000840 Assert1(!(Attrs.hasAttribute(AttributeSet::FunctionIndex,
841 Attribute::ReadNone) &&
842 Attrs.hasAttribute(AttributeSet::FunctionIndex,
843 Attribute::ReadOnly)),
844 "Attributes 'readnone and readonly' are incompatible!", V);
Bill Wendling28d1c602012-10-09 20:11:19 +0000845
Bill Wendling956f1342013-01-18 21:11:39 +0000846 Assert1(!(Attrs.hasAttribute(AttributeSet::FunctionIndex,
847 Attribute::NoInline) &&
848 Attrs.hasAttribute(AttributeSet::FunctionIndex,
849 Attribute::AlwaysInline)),
850 "Attributes 'noinline and alwaysinline' are incompatible!", V);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000851}
852
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000853bool Verifier::VerifyAttributeCount(AttributeSet Attrs, unsigned Params) {
Bill Wendling254aed52013-01-30 06:54:41 +0000854 if (Attrs.getNumSlots() == 0)
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000855 return true;
Nick Lewycky29ef6592009-09-07 20:44:51 +0000856
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000857 unsigned LastSlot = Attrs.getNumSlots() - 1;
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000858 unsigned LastIndex = Attrs.getSlotIndex(LastSlot);
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000859 if (LastIndex <= Params
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000860 || (LastIndex == AttributeSet::FunctionIndex
861 && (LastSlot == 0 || Attrs.getSlotIndex(LastSlot - 1) <= Params)))
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000862 return true;
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000863
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000864 return false;
865}
Nick Lewycky29ef6592009-09-07 20:44:51 +0000866
Chris Lattnerd231fc32002-04-18 20:37:37 +0000867// visitFunction - Verify that a function is ok.
Chris Lattner44d5bd92002-02-20 17:55:43 +0000868//
Chris Lattner24e845f2002-06-25 15:56:27 +0000869void Verifier::visitFunction(Function &F) {
Chris Lattner37c121a2005-05-08 22:27:09 +0000870 // Check function arguments.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000871 FunctionType *FT = F.getFunctionType();
Chris Lattner453eed12007-08-18 06:13:19 +0000872 unsigned NumArgs = F.arg_size();
Chris Lattnerea249242002-04-13 22:48:46 +0000873
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000874 Assert1(Context == &F.getContext(),
875 "Function context does not match Module context!", &F);
876
Chris Lattner26d054d2009-08-05 05:21:07 +0000877 Assert1(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
Chris Lattner69da5cf2002-10-13 20:57:00 +0000878 Assert2(FT->getNumParams() == NumArgs,
Chris Lattnerea249242002-04-13 22:48:46 +0000879 "# formal arguments must match # of arguments for function type!",
Chris Lattner24e845f2002-06-25 15:56:27 +0000880 &F, FT);
Chris Lattnerc282f5a2003-11-21 22:32:23 +0000881 Assert1(F.getReturnType()->isFirstClassType() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000882 F.getReturnType()->isVoidTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000883 F.getReturnType()->isStructTy(),
Chris Lattnerc282f5a2003-11-21 22:32:23 +0000884 "Functions cannot return aggregate values!", &F);
Chris Lattnerea249242002-04-13 22:48:46 +0000885
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000886 Assert1(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
Devang Patel41e23972008-03-03 21:46:28 +0000887 "Invalid struct return type!", &F);
888
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000889 AttributeSet Attrs = F.getAttributes();
Duncan Sands623a3892008-01-11 22:36:48 +0000890
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000891 Assert1(VerifyAttributeCount(Attrs, FT->getNumParams()),
Bill Wendling034b94b2012-12-19 07:18:57 +0000892 "Attribute after last parameter!", &F);
Duncan Sands623a3892008-01-11 22:36:48 +0000893
Duncan Sandsd9d70392007-12-21 19:19:01 +0000894 // Check function attributes.
Duncan Sandscfad1b42008-01-12 16:42:01 +0000895 VerifyFunctionAttrs(FT, Attrs, &F);
Duncan Sandsfdef00f2007-07-27 15:09:54 +0000896
Michael Gottesman2253a2f2013-06-27 00:25:01 +0000897 // On function declarations/definitions, we do not support the builtin
898 // attribute. We do not check this in VerifyFunctionAttrs since that is
899 // checking for Attributes that can/can not ever be on functions.
900 Assert1(!Attrs.hasAttribute(AttributeSet::FunctionIndex,
901 Attribute::Builtin),
902 "Attribute 'builtin' can only be applied to a callsite.", &F);
903
Chris Lattner80105dd2006-05-19 21:25:17 +0000904 // Check that this function meets the restrictions on this calling convention.
905 switch (F.getCallingConv()) {
906 default:
907 break;
908 case CallingConv::C:
909 break;
Chris Lattner80105dd2006-05-19 21:25:17 +0000910 case CallingConv::Fast:
911 case CallingConv::Cold:
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000912 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +0000913 case CallingConv::X86_ThisCall:
Elena Demikhovsky35752222012-10-24 14:46:16 +0000914 case CallingConv::Intel_OCL_BI:
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000915 case CallingConv::PTX_Kernel:
916 case CallingConv::PTX_Device:
Chris Lattner80105dd2006-05-19 21:25:17 +0000917 Assert1(!F.isVarArg(),
918 "Varargs functions must have C calling conventions!", &F);
919 break;
920 }
Nick Lewycky29ef6592009-09-07 20:44:51 +0000921
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000922 bool isLLVMdotName = F.getName().size() >= 5 &&
923 F.getName().substr(0, 5) == "llvm.";
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000924
Chris Lattnerea249242002-04-13 22:48:46 +0000925 // Check that the argument values match the function type for this function...
Chris Lattner69da5cf2002-10-13 20:57:00 +0000926 unsigned i = 0;
Chris Lattnere3cbe032006-12-16 02:25:35 +0000927 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
928 I != E; ++I, ++i) {
Chris Lattner69da5cf2002-10-13 20:57:00 +0000929 Assert2(I->getType() == FT->getParamType(i),
930 "Argument value does not match function argument type!",
931 I, FT->getParamType(i));
Misha Brukmanfd939082005-04-21 23:48:37 +0000932 Assert1(I->getType()->isFirstClassType(),
Dan Gohman9f50eee2008-08-27 14:44:57 +0000933 "Function arguments must have first-class types!", I);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000934 if (!isLLVMdotName)
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000935 Assert2(!I->getType()->isMetadataTy(),
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000936 "Function takes metadata but isn't an intrinsic", I, &F);
Dan Gohman9f50eee2008-08-27 14:44:57 +0000937 }
Chris Lattnerea249242002-04-13 22:48:46 +0000938
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000939 if (F.isMaterializable()) {
940 // Function has a body somewhere we can't see.
941 } else if (F.isDeclaration()) {
Chris Lattner6693da02007-09-19 17:14:45 +0000942 Assert1(F.hasExternalLinkage() || F.hasDLLImportLinkage() ||
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000943 F.hasExternalWeakLinkage(),
Chris Lattner6693da02007-09-19 17:14:45 +0000944 "invalid linkage type for function declaration", &F);
945 } else {
Chris Lattner4d17caa2006-12-13 04:45:46 +0000946 // Verify that this function (which has a body) is not named "llvm.*". It
947 // is not legal to define intrinsics.
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000948 Assert1(!isLLVMdotName, "llvm intrinsics cannot be defined!", &F);
Chris Lattner4d17caa2006-12-13 04:45:46 +0000949
Chris Lattner69da5cf2002-10-13 20:57:00 +0000950 // Check the entry node
Chris Lattner02a3be02003-09-20 14:39:18 +0000951 BasicBlock *Entry = &F.getEntryBlock();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000952 Assert1(pred_begin(Entry) == pred_end(Entry),
953 "Entry block to function must not have predecessors!", Entry);
Chris Lattner660a4f32009-11-01 04:08:01 +0000954
955 // The address of the entry block cannot be taken, unless it is dead.
956 if (Entry->hasAddressTaken()) {
Chris Lattner4a7642e2009-11-01 18:11:50 +0000957 Assert1(!BlockAddress::get(Entry)->isConstantUsed(),
Chris Lattner660a4f32009-11-01 04:08:01 +0000958 "blockaddress may not be used with the entry block!", Entry);
959 }
Chris Lattner69da5cf2002-10-13 20:57:00 +0000960 }
Gabor Greifc9f75002010-03-24 13:21:49 +0000961
Chris Lattner13202de2009-09-11 17:05:29 +0000962 // If this function is actually an intrinsic, verify that it is only used in
963 // direct call/invokes, never having its "address taken".
964 if (F.getIntrinsicID()) {
Gabor Greifc9f75002010-03-24 13:21:49 +0000965 const User *U;
966 if (F.hasAddressTaken(&U))
Chris Lattner13202de2009-09-11 17:05:29 +0000967 Assert1(0, "Invalid user of intrinsic instruction!", U);
Chris Lattner13202de2009-09-11 17:05:29 +0000968 }
Chris Lattner44d5bd92002-02-20 17:55:43 +0000969}
970
Chris Lattnerd231fc32002-04-18 20:37:37 +0000971// verifyBasicBlock - Verify that a basic block is well formed...
972//
Chris Lattner24e845f2002-06-25 15:56:27 +0000973void Verifier::visitBasicBlock(BasicBlock &BB) {
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000974 InstsInThisBlock.clear();
975
Alkis Evlogimenos4f4cf992004-12-04 02:30:42 +0000976 // Ensure that basic blocks have terminators!
977 Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
978
Chris Lattnerbede31f2003-10-05 17:44:18 +0000979 // Check constraints that this basic block imposes on all of the PHI nodes in
980 // it.
981 if (isa<PHINode>(BB.front())) {
Chris Lattnerf8edb622007-02-10 08:33:11 +0000982 SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB));
983 SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
Chris Lattnerbede31f2003-10-05 17:44:18 +0000984 std::sort(Preds.begin(), Preds.end());
Misha Brukmanfd939082005-04-21 23:48:37 +0000985 PHINode *PN;
Chris Lattnerc70a5092004-06-05 17:44:48 +0000986 for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) {
Chris Lattnerbede31f2003-10-05 17:44:18 +0000987 // Ensure that PHI nodes have at least one entry!
988 Assert1(PN->getNumIncomingValues() != 0,
989 "PHI nodes must have at least one entry. If the block is dead, "
990 "the PHI should be removed!", PN);
Brian Gaeke2fea9ad2004-05-17 21:15:18 +0000991 Assert1(PN->getNumIncomingValues() == Preds.size(),
992 "PHINode should have one entry for each predecessor of its "
993 "parent basic block!", PN);
Misha Brukmanfd939082005-04-21 23:48:37 +0000994
Chris Lattnerbede31f2003-10-05 17:44:18 +0000995 // Get and sort all incoming values in the PHI node...
Chris Lattnerf8edb622007-02-10 08:33:11 +0000996 Values.clear();
Chris Lattnerbede31f2003-10-05 17:44:18 +0000997 Values.reserve(PN->getNumIncomingValues());
998 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
999 Values.push_back(std::make_pair(PN->getIncomingBlock(i),
1000 PN->getIncomingValue(i)));
1001 std::sort(Values.begin(), Values.end());
Misha Brukmanfd939082005-04-21 23:48:37 +00001002
Chris Lattnerbede31f2003-10-05 17:44:18 +00001003 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
1004 // Check to make sure that if there is more than one entry for a
1005 // particular basic block in this PHI node, that the incoming values are
1006 // all identical.
1007 //
1008 Assert4(i == 0 || Values[i].first != Values[i-1].first ||
1009 Values[i].second == Values[i-1].second,
1010 "PHI node has multiple entries for the same basic block with "
1011 "different incoming values!", PN, Values[i].first,
1012 Values[i].second, Values[i-1].second);
Misha Brukmanfd939082005-04-21 23:48:37 +00001013
Chris Lattnerbede31f2003-10-05 17:44:18 +00001014 // Check to make sure that the predecessors and PHI node entries are
1015 // matched up.
1016 Assert3(Values[i].first == Preds[i],
1017 "PHI node entries do not match predecessors!", PN,
Misha Brukmanfd939082005-04-21 23:48:37 +00001018 Values[i].first, Preds[i]);
Chris Lattnerbede31f2003-10-05 17:44:18 +00001019 }
1020 }
1021 }
Chris Lattner24e845f2002-06-25 15:56:27 +00001022}
Chris Lattneracd3cae2002-03-15 20:25:09 +00001023
Chris Lattner24e845f2002-06-25 15:56:27 +00001024void Verifier::visitTerminatorInst(TerminatorInst &I) {
1025 // Ensure that terminators only exist at the end of the basic block.
1026 Assert1(&I == I.getParent()->getTerminator(),
1027 "Terminator found in the middle of a basic block!", I.getParent());
Chris Lattner3535c9b2002-07-18 00:13:42 +00001028 visitInstruction(I);
Chris Lattner24e845f2002-06-25 15:56:27 +00001029}
1030
Nick Lewycky6a7cb632010-02-15 22:09:09 +00001031void Verifier::visitBranchInst(BranchInst &BI) {
1032 if (BI.isConditional()) {
1033 Assert2(BI.getCondition()->getType()->isIntegerTy(1),
1034 "Branch condition is not 'i1' type!", &BI, BI.getCondition());
1035 }
1036 visitTerminatorInst(BI);
1037}
1038
Chris Lattner24e845f2002-06-25 15:56:27 +00001039void Verifier::visitReturnInst(ReturnInst &RI) {
1040 Function *F = RI.getParent()->getParent();
Devang Patel57ef4f42008-02-23 00:35:18 +00001041 unsigned N = RI.getNumOperands();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001042 if (F->getReturnType()->isVoidTy())
Chris Lattner80b8f5d2008-04-23 20:33:41 +00001043 Assert2(N == 0,
Nick Lewycky70c44f02008-11-15 17:50:47 +00001044 "Found return instr that returns non-void in Function of void "
Alkis Evlogimenos8b42b432004-12-04 01:25:06 +00001045 "return type!", &RI, F->getReturnType());
Jay Foad3e2f74e2011-04-04 07:44:02 +00001046 else
1047 Assert2(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
1048 "Function return type does not match operand "
1049 "type of return inst!", &RI, F->getReturnType());
Nick Lewycky29ef6592009-09-07 20:44:51 +00001050
Misha Brukman5560c9d2003-08-18 14:43:39 +00001051 // Check to make sure that the return value has necessary properties for
Chris Lattner24e845f2002-06-25 15:56:27 +00001052 // terminators...
1053 visitTerminatorInst(RI);
Chris Lattner44d5bd92002-02-20 17:55:43 +00001054}
1055
Chris Lattner0f9e9d02004-05-21 16:47:21 +00001056void Verifier::visitSwitchInst(SwitchInst &SI) {
1057 // Check to make sure that all of the constants in the switch instruction
1058 // have the same type as the switched-on value.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001059 Type *SwitchTy = SI.getCondition()->getType();
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00001060 IntegerType *IntTy = cast<IntegerType>(SwitchTy);
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00001061 IntegersSubsetToBB Mapping;
1062 std::map<IntegersSubset::Range, unsigned> RangeSetMap;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00001063 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); i != e; ++i) {
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00001064 IntegersSubset CaseRanges = i.getCaseValueEx();
1065 for (unsigned ri = 0, rie = CaseRanges.getNumItems(); ri < rie; ++ri) {
1066 IntegersSubset::Range r = CaseRanges.getItem(ri);
Stepan Dyatkovskiy43eb31b2012-06-02 09:42:43 +00001067 Assert1(((const APInt&)r.getLow()).getBitWidth() == IntTy->getBitWidth(),
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001068 "Switch constants must all be same type as switch value!", &SI);
Stepan Dyatkovskiy43eb31b2012-06-02 09:42:43 +00001069 Assert1(((const APInt&)r.getHigh()).getBitWidth() == IntTy->getBitWidth(),
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001070 "Switch constants must all be same type as switch value!", &SI);
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00001071 Mapping.add(r);
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001072 RangeSetMap[r] = i.getCaseIndex();
1073 }
Chris Lattnerd682a602009-11-11 17:37:02 +00001074 }
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001075
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00001076 IntegersSubsetToBB::RangeIterator errItem;
1077 if (!Mapping.verify(errItem)) {
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001078 unsigned CaseIndex = RangeSetMap[errItem->first];
1079 SwitchInst::CaseIt i(&SI, CaseIndex);
1080 Assert2(false, "Duplicate integer as switch case", &SI, i.getCaseValueEx());
1081 }
1082
Chris Lattner0f9e9d02004-05-21 16:47:21 +00001083 visitTerminatorInst(SI);
1084}
1085
Dan Gohman37680912010-08-02 23:08:33 +00001086void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
1087 Assert1(BI.getAddress()->getType()->isPointerTy(),
1088 "Indirectbr operand must have pointer type!", &BI);
1089 for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
1090 Assert1(BI.getDestination(i)->getType()->isLabelTy(),
1091 "Indirectbr destinations must all have pointer type!", &BI);
1092
1093 visitTerminatorInst(BI);
1094}
1095
Chris Lattner230c1a72004-03-12 05:54:31 +00001096void Verifier::visitSelectInst(SelectInst &SI) {
Chris Lattnerb76ec322008-12-29 00:12:50 +00001097 Assert1(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
1098 SI.getOperand(2)),
1099 "Invalid operands for select instruction!", &SI);
1100
Chris Lattner230c1a72004-03-12 05:54:31 +00001101 Assert1(SI.getTrueValue()->getType() == SI.getType(),
1102 "Select values must have same type as select instruction!", &SI);
Chris Lattner0030e6c2004-09-29 21:19:28 +00001103 visitInstruction(SI);
Chris Lattner230c1a72004-03-12 05:54:31 +00001104}
1105
Misha Brukmanab5c6002004-03-02 00:22:19 +00001106/// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
1107/// a pass, if any exist, it's an error.
1108///
Chris Lattner627079d2002-11-21 16:54:22 +00001109void Verifier::visitUserOp1(Instruction &I) {
Chris Lattner536a9d52006-03-31 04:46:47 +00001110 Assert1(0, "User-defined operators should not live outside of a pass!", &I);
Chris Lattner627079d2002-11-21 16:54:22 +00001111}
Chris Lattnerd231fc32002-04-18 20:37:37 +00001112
Reid Spencer3da59db2006-11-27 01:05:10 +00001113void Verifier::visitTruncInst(TruncInst &I) {
1114 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001115 Type *SrcTy = I.getOperand(0)->getType();
1116 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001117
1118 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001119 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1120 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001121
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001122 Assert1(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
1123 Assert1(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001124 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001125 "trunc source and destination must both be a vector or neither", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001126 Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I);
1127
1128 visitInstruction(I);
1129}
1130
1131void Verifier::visitZExtInst(ZExtInst &I) {
1132 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001133 Type *SrcTy = I.getOperand(0)->getType();
1134 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001135
1136 // Get the size of the types in bits, we'll need this later
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001137 Assert1(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
1138 Assert1(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001139 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001140 "zext source and destination must both be a vector or neither", &I);
Dan Gohman6de29f82009-06-15 22:12:54 +00001141 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1142 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001143
Reid Spencer3da59db2006-11-27 01:05:10 +00001144 Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I);
1145
1146 visitInstruction(I);
1147}
1148
1149void Verifier::visitSExtInst(SExtInst &I) {
1150 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001151 Type *SrcTy = I.getOperand(0)->getType();
1152 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001153
1154 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001155 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1156 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001157
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001158 Assert1(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
1159 Assert1(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001160 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001161 "sext source and destination must both be a vector or neither", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001162 Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I);
1163
1164 visitInstruction(I);
1165}
1166
1167void Verifier::visitFPTruncInst(FPTruncInst &I) {
1168 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001169 Type *SrcTy = I.getOperand(0)->getType();
1170 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001171 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001172 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1173 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001174
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001175 Assert1(SrcTy->isFPOrFPVectorTy(),"FPTrunc only operates on FP", &I);
1176 Assert1(DestTy->isFPOrFPVectorTy(),"FPTrunc only produces an FP", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001177 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001178 "fptrunc source and destination must both be a vector or neither",&I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001179 Assert1(SrcBitSize > DestBitSize,"DestTy too big for FPTrunc", &I);
1180
1181 visitInstruction(I);
1182}
1183
1184void Verifier::visitFPExtInst(FPExtInst &I) {
1185 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001186 Type *SrcTy = I.getOperand(0)->getType();
1187 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001188
1189 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001190 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1191 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001192
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001193 Assert1(SrcTy->isFPOrFPVectorTy(),"FPExt only operates on FP", &I);
1194 Assert1(DestTy->isFPOrFPVectorTy(),"FPExt only produces an FP", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001195 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001196 "fpext source and destination must both be a vector or neither", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001197 Assert1(SrcBitSize < DestBitSize,"DestTy too small for FPExt", &I);
1198
1199 visitInstruction(I);
1200}
1201
1202void Verifier::visitUIToFPInst(UIToFPInst &I) {
1203 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001204 Type *SrcTy = I.getOperand(0)->getType();
1205 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001206
Duncan Sands1df98592010-02-16 11:11:14 +00001207 bool SrcVec = SrcTy->isVectorTy();
1208 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001209
Chris Lattner58d74912008-03-12 17:45:29 +00001210 Assert1(SrcVec == DstVec,
1211 "UIToFP source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001212 Assert1(SrcTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001213 "UIToFP source must be integer or integer vector", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001214 Assert1(DestTy->isFPOrFPVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001215 "UIToFP result must be FP or FP vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001216
1217 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001218 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1219 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001220 "UIToFP source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001221
1222 visitInstruction(I);
1223}
1224
1225void Verifier::visitSIToFPInst(SIToFPInst &I) {
1226 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001227 Type *SrcTy = I.getOperand(0)->getType();
1228 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001229
Duncan Sands1df98592010-02-16 11:11:14 +00001230 bool SrcVec = SrcTy->isVectorTy();
1231 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001232
Chris Lattner58d74912008-03-12 17:45:29 +00001233 Assert1(SrcVec == DstVec,
1234 "SIToFP source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001235 Assert1(SrcTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001236 "SIToFP source must be integer or integer vector", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001237 Assert1(DestTy->isFPOrFPVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001238 "SIToFP result must be FP or FP vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001239
1240 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001241 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1242 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001243 "SIToFP source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001244
1245 visitInstruction(I);
1246}
1247
1248void Verifier::visitFPToUIInst(FPToUIInst &I) {
1249 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001250 Type *SrcTy = I.getOperand(0)->getType();
1251 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001252
Duncan Sands1df98592010-02-16 11:11:14 +00001253 bool SrcVec = SrcTy->isVectorTy();
1254 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001255
Chris Lattner58d74912008-03-12 17:45:29 +00001256 Assert1(SrcVec == DstVec,
1257 "FPToUI source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001258 Assert1(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector",
1259 &I);
1260 Assert1(DestTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001261 "FPToUI result must be integer or integer vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001262
1263 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001264 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1265 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001266 "FPToUI source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001267
1268 visitInstruction(I);
1269}
1270
1271void Verifier::visitFPToSIInst(FPToSIInst &I) {
1272 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001273 Type *SrcTy = I.getOperand(0)->getType();
1274 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001275
Duncan Sands1df98592010-02-16 11:11:14 +00001276 bool SrcVec = SrcTy->isVectorTy();
1277 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001278
Chris Lattner58d74912008-03-12 17:45:29 +00001279 Assert1(SrcVec == DstVec,
1280 "FPToSI source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001281 Assert1(SrcTy->isFPOrFPVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001282 "FPToSI source must be FP or FP vector", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001283 Assert1(DestTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001284 "FPToSI result must be integer or integer vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001285
1286 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001287 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1288 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001289 "FPToSI source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001290
1291 visitInstruction(I);
1292}
1293
1294void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
1295 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001296 Type *SrcTy = I.getOperand(0)->getType();
1297 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001298
Nadav Rotem16087692011-12-05 06:29:09 +00001299 Assert1(SrcTy->getScalarType()->isPointerTy(),
1300 "PtrToInt source must be pointer", &I);
1301 Assert1(DestTy->getScalarType()->isIntegerTy(),
1302 "PtrToInt result must be integral", &I);
1303 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
1304 "PtrToInt type mismatch", &I);
1305
1306 if (SrcTy->isVectorTy()) {
1307 VectorType *VSrc = dyn_cast<VectorType>(SrcTy);
1308 VectorType *VDest = dyn_cast<VectorType>(DestTy);
1309 Assert1(VSrc->getNumElements() == VDest->getNumElements(),
1310 "PtrToInt Vector width mismatch", &I);
1311 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001312
1313 visitInstruction(I);
1314}
1315
1316void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
1317 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001318 Type *SrcTy = I.getOperand(0)->getType();
1319 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001320
Nadav Rotem16087692011-12-05 06:29:09 +00001321 Assert1(SrcTy->getScalarType()->isIntegerTy(),
1322 "IntToPtr source must be an integral", &I);
1323 Assert1(DestTy->getScalarType()->isPointerTy(),
1324 "IntToPtr result must be a pointer",&I);
1325 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
1326 "IntToPtr type mismatch", &I);
1327 if (SrcTy->isVectorTy()) {
1328 VectorType *VSrc = dyn_cast<VectorType>(SrcTy);
1329 VectorType *VDest = dyn_cast<VectorType>(DestTy);
1330 Assert1(VSrc->getNumElements() == VDest->getNumElements(),
1331 "IntToPtr Vector width mismatch", &I);
1332 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001333 visitInstruction(I);
1334}
1335
1336void Verifier::visitBitCastInst(BitCastInst &I) {
1337 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001338 Type *SrcTy = I.getOperand(0)->getType();
1339 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001340
1341 // Get the size of the types in bits, we'll need this later
1342 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1343 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
1344
1345 // BitCast implies a no-op cast of type only. No bits change.
1346 // However, you can't cast pointers to anything but pointers.
Nick Lewyckyc2de3dd2012-08-15 02:37:07 +00001347 Assert1(SrcTy->isPointerTy() == DestTy->isPointerTy(),
Reid Spencer3da59db2006-11-27 01:05:10 +00001348 "Bitcast requires both operands to be pointer or neither", &I);
Nate Begeman55a961f2009-07-30 02:00:06 +00001349 Assert1(SrcBitSize == DestBitSize, "Bitcast requires types of same width",&I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001350
Dan Gohman500233a2008-09-08 16:45:59 +00001351 // Disallow aggregates.
1352 Assert1(!SrcTy->isAggregateType(),
1353 "Bitcast operand must not be aggregate", &I);
1354 Assert1(!DestTy->isAggregateType(),
1355 "Bitcast type must not be aggregate", &I);
1356
Reid Spencer3da59db2006-11-27 01:05:10 +00001357 visitInstruction(I);
1358}
1359
Misha Brukmanab5c6002004-03-02 00:22:19 +00001360/// visitPHINode - Ensure that a PHI node is well formed.
1361///
Chris Lattner24e845f2002-06-25 15:56:27 +00001362void Verifier::visitPHINode(PHINode &PN) {
1363 // Ensure that the PHI nodes are all grouped together at the top of the block.
1364 // This can be tested by checking whether the instruction before this is
Misha Brukman6b634522003-10-10 17:54:14 +00001365 // either nonexistent (because this is begin()) or is a PHI node. If not,
Chris Lattner24e845f2002-06-25 15:56:27 +00001366 // then there is some other instruction before a PHI.
Chris Lattner4d8c16f2007-04-17 17:36:12 +00001367 Assert2(&PN == &PN.getParent()->front() ||
1368 isa<PHINode>(--BasicBlock::iterator(&PN)),
Chris Lattner24e845f2002-06-25 15:56:27 +00001369 "PHI nodes not grouped at top of basic block!",
1370 &PN, PN.getParent());
1371
Nick Lewycky49072472009-09-08 01:23:52 +00001372 // Check that all of the values of the PHI node have the same type as the
1373 // result, and that the incoming blocks are really basic blocks.
1374 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Chris Lattner579de712003-11-12 07:13:37 +00001375 Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
1376 "PHI node operands are not the same type as the result!", &PN);
Nick Lewycky49072472009-09-08 01:23:52 +00001377 }
Chris Lattner579de712003-11-12 07:13:37 +00001378
Chris Lattnerbede31f2003-10-05 17:44:18 +00001379 // All other PHI node constraints are checked in the visitBasicBlock method.
Chris Lattnerd231fc32002-04-18 20:37:37 +00001380
1381 visitInstruction(PN);
1382}
1383
Duncan Sandsd9d70392007-12-21 19:19:01 +00001384void Verifier::VerifyCallSite(CallSite CS) {
1385 Instruction *I = CS.getInstruction();
1386
Duncan Sands1df98592010-02-16 11:11:14 +00001387 Assert1(CS.getCalledValue()->getType()->isPointerTy(),
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001388 "Called function must be a pointer!", I);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001389 PointerType *FPTy = cast<PointerType>(CS.getCalledValue()->getType());
Chris Lattner56732fb2002-05-08 19:49:50 +00001390
Duncan Sands1df98592010-02-16 11:11:14 +00001391 Assert1(FPTy->getElementType()->isFunctionTy(),
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001392 "Called function is not pointer to function type!", I);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001393 FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
Chris Lattner56732fb2002-05-08 19:49:50 +00001394
1395 // Verify that the correct number of arguments are being passed
1396 if (FTy->isVarArg())
Duncan Sandsd9d70392007-12-21 19:19:01 +00001397 Assert1(CS.arg_size() >= FTy->getNumParams(),
1398 "Called function requires more parameters than were provided!",I);
Chris Lattner56732fb2002-05-08 19:49:50 +00001399 else
Duncan Sandsd9d70392007-12-21 19:19:01 +00001400 Assert1(CS.arg_size() == FTy->getNumParams(),
1401 "Incorrect number of arguments passed to called function!", I);
Chris Lattner56732fb2002-05-08 19:49:50 +00001402
Chris Lattner775aba22010-05-10 20:58:42 +00001403 // Verify that all arguments to the call match the function type.
Chris Lattner56732fb2002-05-08 19:49:50 +00001404 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Duncan Sandsd9d70392007-12-21 19:19:01 +00001405 Assert3(CS.getArgument(i)->getType() == FTy->getParamType(i),
Chris Lattner56732fb2002-05-08 19:49:50 +00001406 "Call parameter type does not match function signature!",
Duncan Sandsd9d70392007-12-21 19:19:01 +00001407 CS.getArgument(i), FTy->getParamType(i), I);
1408
Bill Wendlingbb1b63c2013-04-18 20:15:25 +00001409 AttributeSet Attrs = CS.getAttributes();
Duncan Sands623a3892008-01-11 22:36:48 +00001410
Devang Pateld9b4a5f2008-09-23 22:35:17 +00001411 Assert1(VerifyAttributeCount(Attrs, CS.arg_size()),
Bill Wendling034b94b2012-12-19 07:18:57 +00001412 "Attribute after last parameter!", I);
Duncan Sands623a3892008-01-11 22:36:48 +00001413
Duncan Sandsd9d70392007-12-21 19:19:01 +00001414 // Verify call attributes.
Duncan Sandscfad1b42008-01-12 16:42:01 +00001415 VerifyFunctionAttrs(FTy, Attrs, I);
Duncan Sands623a3892008-01-11 22:36:48 +00001416
Stephen Lin456ca042013-04-20 05:14:40 +00001417 if (FTy->isVarArg()) {
1418 // FIXME? is 'nest' even legal here?
1419 bool SawNest = false;
1420 bool SawReturned = false;
1421
1422 for (unsigned Idx = 1; Idx < 1 + FTy->getNumParams(); ++Idx) {
1423 if (Attrs.hasAttribute(Idx, Attribute::Nest))
1424 SawNest = true;
1425 if (Attrs.hasAttribute(Idx, Attribute::Returned))
1426 SawReturned = true;
1427 }
1428
Duncan Sands623a3892008-01-11 22:36:48 +00001429 // Check attributes on the varargs part.
1430 for (unsigned Idx = 1 + FTy->getNumParams(); Idx <= CS.arg_size(); ++Idx) {
Stephen Lin456ca042013-04-20 05:14:40 +00001431 Type *Ty = CS.getArgument(Idx-1)->getType();
1432 VerifyParameterAttrs(Attrs, Idx, Ty, false, I);
1433
1434 if (Attrs.hasAttribute(Idx, Attribute::Nest)) {
1435 Assert1(!SawNest, "More than one parameter has attribute nest!", I);
1436 SawNest = true;
1437 }
1438
1439 if (Attrs.hasAttribute(Idx, Attribute::Returned)) {
1440 Assert1(!SawReturned, "More than one parameter has attribute returned!",
1441 I);
1442 Assert1(Ty->canLosslesslyBitCastTo(FTy->getReturnType()),
1443 "Incompatible argument and return types for 'returned' "
1444 "attribute", I);
1445 SawReturned = true;
1446 }
Duncan Sandscfad1b42008-01-12 16:42:01 +00001447
Bill Wendlingbed80592013-01-21 23:03:18 +00001448 Assert1(!Attrs.hasAttribute(Idx, Attribute::StructRet),
Bill Wendling15c37892012-10-09 09:33:01 +00001449 "Attribute 'sret' cannot be used for vararg call arguments!", I);
Duncan Sands623a3892008-01-11 22:36:48 +00001450 }
Stephen Lin456ca042013-04-20 05:14:40 +00001451 }
Duncan Sandsd9d70392007-12-21 19:19:01 +00001452
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001453 // Verify that there's no metadata unless it's a direct call to an intrinsic.
Chris Lattner1afcace2011-07-09 17:41:24 +00001454 if (CS.getCalledFunction() == 0 ||
Chris Lattner775aba22010-05-10 20:58:42 +00001455 !CS.getCalledFunction()->getName().startswith("llvm.")) {
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001456 for (FunctionType::param_iterator PI = FTy->param_begin(),
1457 PE = FTy->param_end(); PI != PE; ++PI)
Chris Lattner1afcace2011-07-09 17:41:24 +00001458 Assert1(!(*PI)->isMetadataTy(),
Owen Anderson1d0be152009-08-13 21:58:54 +00001459 "Function has metadata parameter but isn't an intrinsic", I);
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001460 }
1461
Michael Gottesman2253a2f2013-06-27 00:25:01 +00001462 // If the call site has the 'builtin' attribute, verify that it's applied to a
1463 // direct call to a function with the 'nobuiltin' attribute.
1464 if (CS.hasFnAttr(Attribute::Builtin))
1465 Assert1(CS.getCalledFunction() &&
1466 CS.getCalledFunction()->hasFnAttribute(Attribute::NoBuiltin),
1467 "Attribute 'builtin' can only be used in a call to a function with "
1468 "the 'nobuiltin' attribute.", I);
1469
Duncan Sandsd9d70392007-12-21 19:19:01 +00001470 visitInstruction(*I);
1471}
1472
1473void Verifier::visitCallInst(CallInst &CI) {
1474 VerifyCallSite(&CI);
Chris Lattner3535c9b2002-07-18 00:13:42 +00001475
Dale Johannesen49de9822009-02-05 01:49:45 +00001476 if (Function *F = CI.getCalledFunction())
Brian Gaeked0fde302003-11-11 22:41:34 +00001477 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
Chris Lattnerdd035d12003-05-08 03:47:33 +00001478 visitIntrinsicFunctionCall(ID, CI);
Duncan Sandsd9d70392007-12-21 19:19:01 +00001479}
1480
1481void Verifier::visitInvokeInst(InvokeInst &II) {
1482 VerifyCallSite(&II);
Bill Wendlingcccfd192011-09-21 22:57:02 +00001483
1484 // Verify that there is a landingpad instruction as the first non-PHI
1485 // instruction of the 'unwind' destination.
1486 Assert1(II.getUnwindDest()->isLandingPad(),
1487 "The unwind destination does not have a landingpad instruction!",&II);
1488
Dan Gohmandacfc5d2010-08-02 23:09:14 +00001489 visitTerminatorInst(II);
Chris Lattnerefdd0a22002-04-18 22:11:52 +00001490}
Chris Lattnerd231fc32002-04-18 20:37:37 +00001491
Misha Brukmanab5c6002004-03-02 00:22:19 +00001492/// visitBinaryOperator - Check that both arguments to the binary operator are
1493/// of the same type!
1494///
Chris Lattner24e845f2002-06-25 15:56:27 +00001495void Verifier::visitBinaryOperator(BinaryOperator &B) {
Chris Lattner1a143ae2002-09-09 20:26:04 +00001496 Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
1497 "Both operands to a binary operator are not of the same type!", &B);
Chris Lattnerd231fc32002-04-18 20:37:37 +00001498
Reid Spencer832254e2007-02-02 02:16:23 +00001499 switch (B.getOpcode()) {
Dan Gohman11cc35d2009-06-05 16:10:00 +00001500 // Check that integer arithmetic operators are only used with
1501 // integral operands.
1502 case Instruction::Add:
1503 case Instruction::Sub:
1504 case Instruction::Mul:
1505 case Instruction::SDiv:
1506 case Instruction::UDiv:
1507 case Instruction::SRem:
1508 case Instruction::URem:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001509 Assert1(B.getType()->isIntOrIntVectorTy(),
Dan Gohman11cc35d2009-06-05 16:10:00 +00001510 "Integer arithmetic operators only work with integral types!", &B);
1511 Assert1(B.getType() == B.getOperand(0)->getType(),
1512 "Integer arithmetic operators must have same type "
1513 "for operands and result!", &B);
1514 break;
1515 // Check that floating-point arithmetic operators are only used with
1516 // floating-point operands.
1517 case Instruction::FAdd:
1518 case Instruction::FSub:
1519 case Instruction::FMul:
1520 case Instruction::FDiv:
1521 case Instruction::FRem:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001522 Assert1(B.getType()->isFPOrFPVectorTy(),
Dan Gohman11cc35d2009-06-05 16:10:00 +00001523 "Floating-point arithmetic operators only work with "
Dan Gohmanf38fd692009-06-05 18:34:16 +00001524 "floating-point types!", &B);
Dan Gohman11cc35d2009-06-05 16:10:00 +00001525 Assert1(B.getType() == B.getOperand(0)->getType(),
1526 "Floating-point arithmetic operators must have same type "
1527 "for operands and result!", &B);
1528 break;
Chris Lattner1a143ae2002-09-09 20:26:04 +00001529 // Check that logical operators are only used with integral operands.
Reid Spencer832254e2007-02-02 02:16:23 +00001530 case Instruction::And:
1531 case Instruction::Or:
1532 case Instruction::Xor:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001533 Assert1(B.getType()->isIntOrIntVectorTy(),
Chris Lattner1a143ae2002-09-09 20:26:04 +00001534 "Logical operators only work with integral types!", &B);
1535 Assert1(B.getType() == B.getOperand(0)->getType(),
1536 "Logical operators must have same type for operands and result!",
1537 &B);
Reid Spencer832254e2007-02-02 02:16:23 +00001538 break;
1539 case Instruction::Shl:
1540 case Instruction::LShr:
1541 case Instruction::AShr:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001542 Assert1(B.getType()->isIntOrIntVectorTy(),
Nate Begeman5bc1ea02008-07-29 15:49:41 +00001543 "Shifts only work with integral types!", &B);
Reid Spencer832254e2007-02-02 02:16:23 +00001544 Assert1(B.getType() == B.getOperand(0)->getType(),
1545 "Shift return type must be same as operands!", &B);
Reid Spencer832254e2007-02-02 02:16:23 +00001546 break;
Dan Gohman11cc35d2009-06-05 16:10:00 +00001547 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001548 llvm_unreachable("Unknown BinaryOperator opcode!");
Chris Lattner1a143ae2002-09-09 20:26:04 +00001549 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001550
Chris Lattnerd231fc32002-04-18 20:37:37 +00001551 visitInstruction(B);
1552}
1553
Nick Lewycky55e97d42010-08-22 23:45:14 +00001554void Verifier::visitICmpInst(ICmpInst &IC) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00001555 // Check that the operands are the same type
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001556 Type *Op0Ty = IC.getOperand(0)->getType();
1557 Type *Op1Ty = IC.getOperand(1)->getType();
Reid Spencer45fb3f32006-11-20 01:22:35 +00001558 Assert1(Op0Ty == Op1Ty,
1559 "Both operands to ICmp instruction are not of the same type!", &IC);
1560 // Check that the operands are the right type
Nadav Rotem16087692011-12-05 06:29:09 +00001561 Assert1(Op0Ty->isIntOrIntVectorTy() || Op0Ty->getScalarType()->isPointerTy(),
Reid Spencer45fb3f32006-11-20 01:22:35 +00001562 "Invalid operand types for ICmp instruction", &IC);
Nick Lewycky55e97d42010-08-22 23:45:14 +00001563 // Check that the predicate is valid.
1564 Assert1(IC.getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1565 IC.getPredicate() <= CmpInst::LAST_ICMP_PREDICATE,
1566 "Invalid predicate in ICmp instruction!", &IC);
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001567
Reid Spencer45fb3f32006-11-20 01:22:35 +00001568 visitInstruction(IC);
1569}
1570
Nick Lewycky55e97d42010-08-22 23:45:14 +00001571void Verifier::visitFCmpInst(FCmpInst &FC) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00001572 // Check that the operands are the same type
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001573 Type *Op0Ty = FC.getOperand(0)->getType();
1574 Type *Op1Ty = FC.getOperand(1)->getType();
Reid Spencer45fb3f32006-11-20 01:22:35 +00001575 Assert1(Op0Ty == Op1Ty,
1576 "Both operands to FCmp instruction are not of the same type!", &FC);
1577 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001578 Assert1(Op0Ty->isFPOrFPVectorTy(),
Reid Spencer45fb3f32006-11-20 01:22:35 +00001579 "Invalid operand types for FCmp instruction", &FC);
Nick Lewycky55e97d42010-08-22 23:45:14 +00001580 // Check that the predicate is valid.
1581 Assert1(FC.getPredicate() >= CmpInst::FIRST_FCMP_PREDICATE &&
1582 FC.getPredicate() <= CmpInst::LAST_FCMP_PREDICATE,
1583 "Invalid predicate in FCmp instruction!", &FC);
1584
Reid Spencer45fb3f32006-11-20 01:22:35 +00001585 visitInstruction(FC);
1586}
1587
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001588void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
Chris Lattner1cbe05b2006-04-08 04:07:52 +00001589 Assert1(ExtractElementInst::isValidOperands(EI.getOperand(0),
1590 EI.getOperand(1)),
1591 "Invalid extractelement operands!", &EI);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001592 visitInstruction(EI);
1593}
1594
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001595void Verifier::visitInsertElementInst(InsertElementInst &IE) {
Chris Lattner1cbe05b2006-04-08 04:07:52 +00001596 Assert1(InsertElementInst::isValidOperands(IE.getOperand(0),
1597 IE.getOperand(1),
1598 IE.getOperand(2)),
1599 "Invalid insertelement operands!", &IE);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001600 visitInstruction(IE);
1601}
1602
Chris Lattner00f10232006-04-08 01:18:18 +00001603void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
1604 Assert1(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
1605 SV.getOperand(2)),
1606 "Invalid shufflevector operands!", &SV);
Chris Lattner00f10232006-04-08 01:18:18 +00001607 visitInstruction(SV);
1608}
1609
Chris Lattner24e845f2002-06-25 15:56:27 +00001610void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Duncan Sandsb95d1ff2012-02-03 17:28:51 +00001611 Type *TargetTy = GEP.getPointerOperandType()->getScalarType();
Nadav Rotem16087692011-12-05 06:29:09 +00001612
Duncan Sandsb95d1ff2012-02-03 17:28:51 +00001613 Assert1(isa<PointerType>(TargetTy),
Bill Wendling6ebddd22012-10-17 23:56:05 +00001614 "GEP base pointer is not a vector or a vector of pointers", &GEP);
Nadav Rotem16087692011-12-05 06:29:09 +00001615 Assert1(cast<PointerType>(TargetTy)->getElementType()->isSized(),
Chris Lattner4cea5ba2011-07-29 20:32:28 +00001616 "GEP into unsized type!", &GEP);
Duncan Sands2333e292012-11-13 12:59:33 +00001617 Assert1(GEP.getPointerOperandType()->isVectorTy() ==
1618 GEP.getType()->isVectorTy(), "Vector GEP must return a vector value",
1619 &GEP);
Nadav Rotem16087692011-12-05 06:29:09 +00001620
Chris Lattner8552fae2007-02-10 08:30:29 +00001621 SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end());
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001622 Type *ElTy =
Nadav Rotem16087692011-12-05 06:29:09 +00001623 GetElementPtrInst::getIndexedType(GEP.getPointerOperandType(), Idxs);
Chris Lattner24e845f2002-06-25 15:56:27 +00001624 Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
Nadav Rotem16087692011-12-05 06:29:09 +00001625
Duncan Sands2333e292012-11-13 12:59:33 +00001626 Assert2(GEP.getType()->getScalarType()->isPointerTy() &&
1627 cast<PointerType>(GEP.getType()->getScalarType())->getElementType()
1628 == ElTy, "GEP is not of right type for indices!", &GEP, ElTy);
1629
1630 if (GEP.getPointerOperandType()->isVectorTy()) {
1631 // Additional checks for vector GEPs.
1632 unsigned GepWidth = GEP.getPointerOperandType()->getVectorNumElements();
1633 Assert1(GepWidth == GEP.getType()->getVectorNumElements(),
1634 "Vector GEP result width doesn't match operand's", &GEP);
1635 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
1636 Type *IndexTy = Idxs[i]->getType();
1637 Assert1(IndexTy->isVectorTy(),
1638 "Vector GEP must have vector indices!", &GEP);
1639 unsigned IndexWidth = IndexTy->getVectorNumElements();
1640 Assert1(IndexWidth == GepWidth, "Invalid GEP index vector width", &GEP);
1641 }
Nadav Rotem16087692011-12-05 06:29:09 +00001642 }
Chris Lattnera00409e2002-04-24 19:12:21 +00001643 visitInstruction(GEP);
1644}
1645
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001646static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
1647 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
1648}
1649
Chris Lattner24e845f2002-06-25 15:56:27 +00001650void Verifier::visitLoadInst(LoadInst &LI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001651 PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
Nick Lewycky49072472009-09-08 01:23:52 +00001652 Assert1(PTy, "Load operand must be a pointer.", &LI);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001653 Type *ElTy = PTy->getElementType();
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001654 Assert2(ElTy == LI.getType(),
1655 "Load result type does not match pointer operand type!", &LI, ElTy);
Eli Friedman21006d42011-08-09 23:02:53 +00001656 if (LI.isAtomic()) {
1657 Assert1(LI.getOrdering() != Release && LI.getOrdering() != AcquireRelease,
1658 "Load cannot have Release ordering", &LI);
1659 Assert1(LI.getAlignment() != 0,
1660 "Atomic load must specify explicit alignment", &LI);
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001661 if (!ElTy->isPointerTy()) {
1662 Assert2(ElTy->isIntegerTy(),
1663 "atomic store operand must have integer type!",
1664 &LI, ElTy);
1665 unsigned Size = ElTy->getPrimitiveSizeInBits();
1666 Assert2(Size >= 8 && !(Size & (Size - 1)),
1667 "atomic store operand must be power-of-two byte-sized integer",
1668 &LI, ElTy);
1669 }
Eli Friedman21006d42011-08-09 23:02:53 +00001670 } else {
1671 Assert1(LI.getSynchScope() == CrossThread,
1672 "Non-atomic load cannot have SynchronizationScope specified", &LI);
1673 }
Rafael Espindola39dd3282012-03-24 00:14:51 +00001674
1675 if (MDNode *Range = LI.getMetadata(LLVMContext::MD_range)) {
1676 unsigned NumOperands = Range->getNumOperands();
1677 Assert1(NumOperands % 2 == 0, "Unfinished range!", Range);
1678 unsigned NumRanges = NumOperands / 2;
1679 Assert1(NumRanges >= 1, "It should have at least one range!", Range);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001680
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001681 ConstantRange LastRange(1); // Dummy initial value
Rafael Espindola39dd3282012-03-24 00:14:51 +00001682 for (unsigned i = 0; i < NumRanges; ++i) {
1683 ConstantInt *Low = dyn_cast<ConstantInt>(Range->getOperand(2*i));
1684 Assert1(Low, "The lower limit must be an integer!", Low);
1685 ConstantInt *High = dyn_cast<ConstantInt>(Range->getOperand(2*i + 1));
1686 Assert1(High, "The upper limit must be an integer!", High);
1687 Assert1(High->getType() == Low->getType() &&
1688 High->getType() == ElTy, "Range types must match load type!",
1689 &LI);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001690
1691 APInt HighV = High->getValue();
1692 APInt LowV = Low->getValue();
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001693 ConstantRange CurRange(LowV, HighV);
1694 Assert1(!CurRange.isEmptySet() && !CurRange.isFullSet(),
1695 "Range must not be empty!", Range);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001696 if (i != 0) {
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001697 Assert1(CurRange.intersectWith(LastRange).isEmptySet(),
1698 "Intervals are overlapping", Range);
1699 Assert1(LowV.sgt(LastRange.getLower()), "Intervals are not in order",
1700 Range);
1701 Assert1(!isContiguous(CurRange, LastRange), "Intervals are contiguous",
1702 Range);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001703 }
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001704 LastRange = ConstantRange(LowV, HighV);
Rafael Espindola39dd3282012-03-24 00:14:51 +00001705 }
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001706 if (NumRanges > 2) {
1707 APInt FirstLow =
1708 dyn_cast<ConstantInt>(Range->getOperand(0))->getValue();
1709 APInt FirstHigh =
1710 dyn_cast<ConstantInt>(Range->getOperand(1))->getValue();
1711 ConstantRange FirstRange(FirstLow, FirstHigh);
1712 Assert1(FirstRange.intersectWith(LastRange).isEmptySet(),
1713 "Intervals are overlapping", Range);
1714 Assert1(!isContiguous(FirstRange, LastRange), "Intervals are contiguous",
1715 Range);
1716 }
1717
1718
Rafael Espindola39dd3282012-03-24 00:14:51 +00001719 }
1720
Chris Lattnera00409e2002-04-24 19:12:21 +00001721 visitInstruction(LI);
1722}
1723
Chris Lattner24e845f2002-06-25 15:56:27 +00001724void Verifier::visitStoreInst(StoreInst &SI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001725 PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
Chris Lattnerb4a96312010-07-11 19:42:53 +00001726 Assert1(PTy, "Store operand must be a pointer.", &SI);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001727 Type *ElTy = PTy->getElementType();
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001728 Assert2(ElTy == SI.getOperand(0)->getType(),
1729 "Stored value type does not match pointer operand type!",
1730 &SI, ElTy);
Eli Friedman21006d42011-08-09 23:02:53 +00001731 if (SI.isAtomic()) {
1732 Assert1(SI.getOrdering() != Acquire && SI.getOrdering() != AcquireRelease,
1733 "Store cannot have Acquire ordering", &SI);
1734 Assert1(SI.getAlignment() != 0,
1735 "Atomic store must specify explicit alignment", &SI);
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001736 if (!ElTy->isPointerTy()) {
1737 Assert2(ElTy->isIntegerTy(),
1738 "atomic store operand must have integer type!",
1739 &SI, ElTy);
1740 unsigned Size = ElTy->getPrimitiveSizeInBits();
1741 Assert2(Size >= 8 && !(Size & (Size - 1)),
1742 "atomic store operand must be power-of-two byte-sized integer",
1743 &SI, ElTy);
1744 }
Eli Friedman21006d42011-08-09 23:02:53 +00001745 } else {
1746 Assert1(SI.getSynchScope() == CrossThread,
1747 "Non-atomic store cannot have SynchronizationScope specified", &SI);
1748 }
Chris Lattnera00409e2002-04-24 19:12:21 +00001749 visitInstruction(SI);
1750}
1751
Victor Hernandez7b929da2009-10-23 21:09:37 +00001752void Verifier::visitAllocaInst(AllocaInst &AI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001753 PointerType *PTy = AI.getType();
Chris Lattnerab3b7782008-03-01 09:01:57 +00001754 Assert1(PTy->getAddressSpace() == 0,
1755 "Allocation instruction pointer not in the generic address space!",
1756 &AI);
1757 Assert1(PTy->getElementType()->isSized(), "Cannot allocate unsized type",
1758 &AI);
Dan Gohmanf75a7d32010-05-28 01:14:11 +00001759 Assert1(AI.getArraySize()->getType()->isIntegerTy(),
1760 "Alloca array size must have integer type", &AI);
Christopher Lamb303dae92007-12-17 01:00:21 +00001761 visitInstruction(AI);
1762}
1763
Eli Friedmanff030482011-07-28 21:48:00 +00001764void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) {
1765 Assert1(CXI.getOrdering() != NotAtomic,
1766 "cmpxchg instructions must be atomic.", &CXI);
1767 Assert1(CXI.getOrdering() != Unordered,
1768 "cmpxchg instructions cannot be unordered.", &CXI);
1769 PointerType *PTy = dyn_cast<PointerType>(CXI.getOperand(0)->getType());
1770 Assert1(PTy, "First cmpxchg operand must be a pointer.", &CXI);
1771 Type *ElTy = PTy->getElementType();
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001772 Assert2(ElTy->isIntegerTy(),
1773 "cmpxchg operand must have integer type!",
1774 &CXI, ElTy);
1775 unsigned Size = ElTy->getPrimitiveSizeInBits();
1776 Assert2(Size >= 8 && !(Size & (Size - 1)),
1777 "cmpxchg operand must be power-of-two byte-sized integer",
1778 &CXI, ElTy);
Eli Friedmanff030482011-07-28 21:48:00 +00001779 Assert2(ElTy == CXI.getOperand(1)->getType(),
1780 "Expected value type does not match pointer operand type!",
1781 &CXI, ElTy);
1782 Assert2(ElTy == CXI.getOperand(2)->getType(),
1783 "Stored value type does not match pointer operand type!",
1784 &CXI, ElTy);
1785 visitInstruction(CXI);
1786}
1787
1788void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
1789 Assert1(RMWI.getOrdering() != NotAtomic,
1790 "atomicrmw instructions must be atomic.", &RMWI);
1791 Assert1(RMWI.getOrdering() != Unordered,
1792 "atomicrmw instructions cannot be unordered.", &RMWI);
1793 PointerType *PTy = dyn_cast<PointerType>(RMWI.getOperand(0)->getType());
1794 Assert1(PTy, "First atomicrmw operand must be a pointer.", &RMWI);
1795 Type *ElTy = PTy->getElementType();
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001796 Assert2(ElTy->isIntegerTy(),
1797 "atomicrmw operand must have integer type!",
1798 &RMWI, ElTy);
1799 unsigned Size = ElTy->getPrimitiveSizeInBits();
1800 Assert2(Size >= 8 && !(Size & (Size - 1)),
1801 "atomicrmw operand must be power-of-two byte-sized integer",
1802 &RMWI, ElTy);
Eli Friedmanff030482011-07-28 21:48:00 +00001803 Assert2(ElTy == RMWI.getOperand(1)->getType(),
1804 "Argument value type does not match pointer operand type!",
1805 &RMWI, ElTy);
1806 Assert1(AtomicRMWInst::FIRST_BINOP <= RMWI.getOperation() &&
1807 RMWI.getOperation() <= AtomicRMWInst::LAST_BINOP,
1808 "Invalid binary operation!", &RMWI);
1809 visitInstruction(RMWI);
1810}
1811
Eli Friedman47f35132011-07-25 23:16:38 +00001812void Verifier::visitFenceInst(FenceInst &FI) {
1813 const AtomicOrdering Ordering = FI.getOrdering();
1814 Assert1(Ordering == Acquire || Ordering == Release ||
1815 Ordering == AcquireRelease || Ordering == SequentiallyConsistent,
1816 "fence instructions may only have "
Bill Wendling63012202011-08-08 08:02:48 +00001817 "acquire, release, acq_rel, or seq_cst ordering.", &FI);
Eli Friedman47f35132011-07-25 23:16:38 +00001818 visitInstruction(FI);
1819}
1820
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001821void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
1822 Assert1(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001823 EVI.getIndices()) ==
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001824 EVI.getType(),
1825 "Invalid ExtractValueInst operands!", &EVI);
Chris Lattner42369b72008-04-23 04:06:15 +00001826
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001827 visitInstruction(EVI);
Devang Patel40a04212008-02-19 22:15:16 +00001828}
1829
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001830void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
1831 Assert1(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001832 IVI.getIndices()) ==
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001833 IVI.getOperand(1)->getType(),
1834 "Invalid InsertValueInst operands!", &IVI);
1835
1836 visitInstruction(IVI);
1837}
Chris Lattnerd231fc32002-04-18 20:37:37 +00001838
Bill Wendlinge6e88262011-08-12 20:24:12 +00001839void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
1840 BasicBlock *BB = LPI.getParent();
1841
1842 // The landingpad instruction is ill-formed if it doesn't have any clauses and
1843 // isn't a cleanup.
1844 Assert1(LPI.getNumClauses() > 0 || LPI.isCleanup(),
1845 "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
1846
1847 // The landingpad instruction defines its parent as a landing pad block. The
1848 // landing pad block may be branched to only by the unwind edge of an invoke.
1849 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
1850 const InvokeInst *II = dyn_cast<InvokeInst>((*I)->getTerminator());
Eli Friedman6b951b22012-08-10 20:55:20 +00001851 Assert1(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
Bill Wendlinge6e88262011-08-12 20:24:12 +00001852 "Block containing LandingPadInst must be jumped to "
1853 "only by the unwind edge of an invoke.", &LPI);
1854 }
1855
1856 // The landingpad instruction must be the first non-PHI instruction in the
1857 // block.
1858 Assert1(LPI.getParent()->getLandingPadInst() == &LPI,
1859 "LandingPadInst not the first non-PHI instruction in the block.",
1860 &LPI);
1861
1862 // The personality functions for all landingpad instructions within the same
1863 // function should match.
1864 if (PersonalityFn)
1865 Assert1(LPI.getPersonalityFn() == PersonalityFn,
1866 "Personality function doesn't match others in function", &LPI);
1867 PersonalityFn = LPI.getPersonalityFn();
1868
Duncan Sands00418ea2011-09-27 16:43:19 +00001869 // All operands must be constants.
1870 Assert1(isa<Constant>(PersonalityFn), "Personality function is not constant!",
1871 &LPI);
1872 for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) {
1873 Value *Clause = LPI.getClause(i);
1874 Assert1(isa<Constant>(Clause), "Clause is not constant!", &LPI);
Duncan Sands040bff02011-09-27 19:34:22 +00001875 if (LPI.isCatch(i)) {
1876 Assert1(isa<PointerType>(Clause->getType()),
1877 "Catch operand does not have pointer type!", &LPI);
1878 } else {
1879 Assert1(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI);
Duncan Sands00418ea2011-09-27 16:43:19 +00001880 Assert1(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause),
Duncan Sands040bff02011-09-27 19:34:22 +00001881 "Filter operand is not an array of constants!", &LPI);
1882 }
Duncan Sands00418ea2011-09-27 16:43:19 +00001883 }
1884
Bill Wendlinge6e88262011-08-12 20:24:12 +00001885 visitInstruction(LPI);
1886}
1887
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001888void Verifier::verifyDominatesUse(Instruction &I, unsigned i) {
1889 Instruction *Op = cast<Instruction>(I.getOperand(i));
Rafael Espindolad5118c82012-08-17 18:21:28 +00001890 // If the we have an invalid invoke, don't try to compute the dominance.
1891 // We already reject it in the invoke specific checks and the dominance
1892 // computation doesn't handle multiple edges.
1893 if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
1894 if (II->getNormalDest() == II->getUnwindDest())
1895 return;
1896 }
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001897
Rafael Espindola20907662012-06-01 21:56:26 +00001898 const Use &U = I.getOperandUse(i);
1899 Assert2(InstsInThisBlock.count(Op) || DT->dominates(Op, U),
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001900 "Instruction does not dominate all uses!", Op, &I);
1901}
1902
Misha Brukmanab5c6002004-03-02 00:22:19 +00001903/// verifyInstruction - Verify that an instruction is well formed.
1904///
Chris Lattner24e845f2002-06-25 15:56:27 +00001905void Verifier::visitInstruction(Instruction &I) {
Misha Brukmanfd939082005-04-21 23:48:37 +00001906 BasicBlock *BB = I.getParent();
Chris Lattner1a143ae2002-09-09 20:26:04 +00001907 Assert1(BB, "Instruction not embedded in basic block!", &I);
Chris Lattnerd231fc32002-04-18 20:37:37 +00001908
Chris Lattnerbede31f2003-10-05 17:44:18 +00001909 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
1910 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
1911 UI != UE; ++UI)
Duncan Sands44008252009-05-29 19:39:36 +00001912 Assert1(*UI != (User*)&I || !DT->isReachableFromEntry(BB),
Chris Lattnerbede31f2003-10-05 17:44:18 +00001913 "Only PHI nodes may reference their own value!", &I);
1914 }
Nick Lewycky29ef6592009-09-07 20:44:51 +00001915
Chris Lattnerbede31f2003-10-05 17:44:18 +00001916 // Check that void typed values don't have names
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001917 Assert1(!I.getType()->isVoidTy() || !I.hasName(),
Chris Lattnerbede31f2003-10-05 17:44:18 +00001918 "Instruction has a name, but provides a void value!", &I);
1919
Chris Lattner944cfaf2004-03-29 00:29:36 +00001920 // Check that the return value of the instruction is either void or a legal
1921 // value type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001922 Assert1(I.getType()->isVoidTy() ||
Nick Lewyckyc261df92009-09-27 23:27:42 +00001923 I.getType()->isFirstClassType(),
Chris Lattner944cfaf2004-03-29 00:29:36 +00001924 "Instruction returns a non-scalar type!", &I);
1925
Nick Lewyckyc261df92009-09-27 23:27:42 +00001926 // Check that the instruction doesn't produce metadata. Calls are already
1927 // checked against the callee type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001928 Assert1(!I.getType()->isMetadataTy() ||
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001929 isa<CallInst>(I) || isa<InvokeInst>(I),
1930 "Invalid use of metadata!", &I);
1931
Chris Lattnerd231fc32002-04-18 20:37:37 +00001932 // Check that all uses of the instruction, if they are instructions
1933 // themselves, actually have parent basic blocks. If the use is not an
1934 // instruction, it is an error!
Chris Lattner24e845f2002-06-25 15:56:27 +00001935 for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattnerd231fc32002-04-18 20:37:37 +00001936 UI != UE; ++UI) {
Nick Lewycky49072472009-09-08 01:23:52 +00001937 if (Instruction *Used = dyn_cast<Instruction>(*UI))
1938 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
1939 " embedded in a basic block!", &I, Used);
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001940 else {
Nick Lewycky49072472009-09-08 01:23:52 +00001941 CheckFailed("Use of instruction is not an instruction!", *UI);
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001942 return;
1943 }
Chris Lattnerd231fc32002-04-18 20:37:37 +00001944 }
1945
Chris Lattnerbede31f2003-10-05 17:44:18 +00001946 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Chris Lattneraab18202005-02-24 16:58:29 +00001947 Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I);
Chris Lattnerf4ea9212006-07-11 20:29:49 +00001948
1949 // Check to make sure that only first-class-values are operands to
1950 // instructions.
Devang Patelbb4f8d42008-02-21 01:54:02 +00001951 if (!I.getOperand(i)->getType()->isFirstClassType()) {
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001952 Assert1(0, "Instruction operands must be first-class values!", &I);
Devang Patelbb4f8d42008-02-21 01:54:02 +00001953 }
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001954
Chris Lattner59c35692004-03-14 03:23:54 +00001955 if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
Chris Lattnerf4ea9212006-07-11 20:29:49 +00001956 // Check to make sure that the "address of" an intrinsic function is never
1957 // taken.
Nuno Lopes917f97c2012-06-28 22:57:00 +00001958 Assert1(!F->isIntrinsic() || i == (isa<CallInst>(I) ? e-1 : 0),
Chris Lattnerdd035d12003-05-08 03:47:33 +00001959 "Cannot take the address of an intrinsic!", &I);
Nuno Lopes917f97c2012-06-28 22:57:00 +00001960 Assert1(!F->isIntrinsic() || isa<CallInst>(I) ||
1961 F->getIntrinsicID() == Intrinsic::donothing,
1962 "Cannot invoke an intrinsinc other than donothing", &I);
Chris Lattner19b6dcd2007-04-20 21:48:08 +00001963 Assert1(F->getParent() == Mod, "Referencing function in another module!",
1964 &I);
Chris Lattner59c35692004-03-14 03:23:54 +00001965 } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
1966 Assert1(OpBB->getParent() == BB->getParent(),
1967 "Referring to a basic block in another function!", &I);
1968 } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
1969 Assert1(OpArg->getParent() == BB->getParent(),
1970 "Referring to an argument in another function!", &I);
Chris Lattner19b6dcd2007-04-20 21:48:08 +00001971 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
1972 Assert1(GV->getParent() == Mod, "Referencing global in another module!",
1973 &I);
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001974 } else if (isa<Instruction>(I.getOperand(i))) {
1975 verifyDominatesUse(I, i);
Chris Lattner3188b732006-01-26 00:08:45 +00001976 } else if (isa<InlineAsm>(I.getOperand(i))) {
Gabor Greif63d024f2010-07-13 15:31:36 +00001977 Assert1((i + 1 == e && isa<CallInst>(I)) ||
1978 (i + 3 == e && isa<InvokeInst>(I)),
Chris Lattner3188b732006-01-26 00:08:45 +00001979 "Cannot take the address of an inline asm!", &I);
Chris Lattnerbede31f2003-10-05 17:44:18 +00001980 }
1981 }
Rafael Espindola39dd3282012-03-24 00:14:51 +00001982
Duncan Sands5e5c5f82012-04-14 12:36:06 +00001983 if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) {
Duncan Sands1fd63df2012-04-10 08:22:43 +00001984 Assert1(I.getType()->isFPOrFPVectorTy(),
Duncan Sands5e5c5f82012-04-14 12:36:06 +00001985 "fpmath requires a floating point result!", &I);
1986 Assert1(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
Duncan Sands8883c432012-04-16 16:28:59 +00001987 Value *Op0 = MD->getOperand(0);
1988 if (ConstantFP *CFP0 = dyn_cast_or_null<ConstantFP>(Op0)) {
1989 APFloat Accuracy = CFP0->getValueAPF();
Michael Gottesman07969dc2013-06-19 21:23:18 +00001990 Assert1(Accuracy.isFiniteNonZero() && !Accuracy.isNegative(),
Duncan Sands8883c432012-04-16 16:28:59 +00001991 "fpmath accuracy not a positive number!", &I);
Duncan Sands8883c432012-04-16 16:28:59 +00001992 } else {
1993 Assert1(false, "invalid fpmath accuracy!", &I);
1994 }
Duncan Sands1fd63df2012-04-10 08:22:43 +00001995 }
1996
Rafael Espindola39dd3282012-03-24 00:14:51 +00001997 MDNode *MD = I.getMetadata(LLVMContext::MD_range);
1998 Assert1(!MD || isa<LoadInst>(I), "Ranges are only for loads!", &I);
1999
Chris Lattnera7b1c7e2004-09-29 20:07:45 +00002000 InstsInThisBlock.insert(&I);
Chris Lattnerdd035d12003-05-08 03:47:33 +00002001}
2002
Chris Lattner55dc5c72012-05-27 19:37:05 +00002003/// VerifyIntrinsicType - Verify that the specified type (which comes from an
2004/// intrinsic argument or return value) matches the type constraints specified
2005/// by the .td file (e.g. an "any integer" argument really is an integer).
2006///
2007/// This return true on error but does not print a message.
2008bool Verifier::VerifyIntrinsicType(Type *Ty,
2009 ArrayRef<Intrinsic::IITDescriptor> &Infos,
2010 SmallVectorImpl<Type*> &ArgTys) {
2011 using namespace Intrinsic;
2012
2013 // If we ran out of descriptors, there are too many arguments.
2014 if (Infos.empty()) return true;
2015 IITDescriptor D = Infos.front();
2016 Infos = Infos.slice(1);
2017
2018 switch (D.Kind) {
2019 case IITDescriptor::Void: return !Ty->isVoidTy();
2020 case IITDescriptor::MMX: return !Ty->isX86_MMXTy();
2021 case IITDescriptor::Metadata: return !Ty->isMetadataTy();
Michael Ilseman4d0b4a42013-01-11 01:45:05 +00002022 case IITDescriptor::Half: return !Ty->isHalfTy();
Chris Lattner55dc5c72012-05-27 19:37:05 +00002023 case IITDescriptor::Float: return !Ty->isFloatTy();
2024 case IITDescriptor::Double: return !Ty->isDoubleTy();
2025 case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
2026 case IITDescriptor::Vector: {
2027 VectorType *VT = dyn_cast<VectorType>(Ty);
2028 return VT == 0 || VT->getNumElements() != D.Vector_Width ||
2029 VerifyIntrinsicType(VT->getElementType(), Infos, ArgTys);
2030 }
2031 case IITDescriptor::Pointer: {
2032 PointerType *PT = dyn_cast<PointerType>(Ty);
2033 return PT == 0 || PT->getAddressSpace() != D.Pointer_AddressSpace ||
2034 VerifyIntrinsicType(PT->getElementType(), Infos, ArgTys);
2035 }
2036
2037 case IITDescriptor::Struct: {
2038 StructType *ST = dyn_cast<StructType>(Ty);
2039 if (ST == 0 || ST->getNumElements() != D.Struct_NumElements)
2040 return true;
2041
2042 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
2043 if (VerifyIntrinsicType(ST->getElementType(i), Infos, ArgTys))
2044 return true;
2045 return false;
2046 }
2047
2048 case IITDescriptor::Argument:
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002049 // Two cases here - If this is the second occurrence of an argument, verify
Chris Lattner55dc5c72012-05-27 19:37:05 +00002050 // that the later instance matches the previous instance.
2051 if (D.getArgumentNumber() < ArgTys.size())
2052 return Ty != ArgTys[D.getArgumentNumber()];
2053
2054 // Otherwise, if this is the first instance of an argument, record it and
2055 // verify the "Any" kind.
2056 assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error");
2057 ArgTys.push_back(Ty);
2058
2059 switch (D.getArgumentKind()) {
2060 case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
2061 case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy();
2062 case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty);
2063 case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
2064 }
2065 llvm_unreachable("all argument kinds not covered");
2066
2067 case IITDescriptor::ExtendVecArgument:
2068 // This may only be used when referring to a previous vector argument.
2069 return D.getArgumentNumber() >= ArgTys.size() ||
2070 !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
2071 VectorType::getExtendedElementVectorType(
2072 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
2073
2074 case IITDescriptor::TruncVecArgument:
2075 // This may only be used when referring to a previous vector argument.
2076 return D.getArgumentNumber() >= ArgTys.size() ||
2077 !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
2078 VectorType::getTruncatedElementVectorType(
2079 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
2080 }
2081 llvm_unreachable("unhandled");
2082}
Bob Wilsonbc039792009-01-07 00:09:01 +00002083
Chris Lattnerdd035d12003-05-08 03:47:33 +00002084/// visitIntrinsicFunction - Allow intrinsics to be verified in different ways.
Misha Brukmanab5c6002004-03-02 00:22:19 +00002085///
Brian Gaeked0fde302003-11-11 22:41:34 +00002086void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnerdd035d12003-05-08 03:47:33 +00002087 Function *IF = CI.getCalledFunction();
Chris Lattner19b6dcd2007-04-20 21:48:08 +00002088 Assert1(IF->isDeclaration(), "Intrinsic functions should never be defined!",
2089 IF);
Nick Lewycky29ef6592009-09-07 20:44:51 +00002090
Chris Lattner55dc5c72012-05-27 19:37:05 +00002091 // Verify that the intrinsic prototype lines up with what the .td files
2092 // describe.
2093 FunctionType *IFTy = IF->getFunctionType();
2094 Assert1(!IFTy->isVarArg(), "Intrinsic prototypes are not varargs", IF);
2095
2096 SmallVector<Intrinsic::IITDescriptor, 8> Table;
2097 getIntrinsicInfoTableEntries(ID, Table);
2098 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
Nick Lewycky29ef6592009-09-07 20:44:51 +00002099
Chris Lattner55dc5c72012-05-27 19:37:05 +00002100 SmallVector<Type *, 4> ArgTys;
2101 Assert1(!VerifyIntrinsicType(IFTy->getReturnType(), TableRef, ArgTys),
2102 "Intrinsic has incorrect return type!", IF);
2103 for (unsigned i = 0, e = IFTy->getNumParams(); i != e; ++i)
2104 Assert1(!VerifyIntrinsicType(IFTy->getParamType(i), TableRef, ArgTys),
2105 "Intrinsic has incorrect argument type!", IF);
2106 Assert1(TableRef.empty(), "Intrinsic has too few arguments!", IF);
2107
2108 // Now that we have the intrinsic ID and the actual argument types (and we
2109 // know they are legal for the intrinsic!) get the intrinsic name through the
2110 // usual means. This allows us to verify the mangling of argument types into
2111 // the name.
2112 Assert1(Intrinsic::getName(ID, ArgTys) == IF->getName(),
2113 "Intrinsic name not mangled correctly for type arguments!", IF);
2114
Chris Lattnerb241b372009-12-28 09:07:21 +00002115 // If the intrinsic takes MDNode arguments, verify that they are either global
2116 // or are local to *this* function.
Bill Wendlingd942df22010-06-07 19:18:58 +00002117 for (unsigned i = 0, e = CI.getNumArgOperands(); i != e; ++i)
2118 if (MDNode *MD = dyn_cast<MDNode>(CI.getArgOperand(i)))
Duncan Sandse704d9d2010-04-29 16:10:30 +00002119 visitMDNode(*MD, CI.getParent()->getParent());
Victor Hernandez5d301622009-12-18 20:09:14 +00002120
Gordon Henriksen8c33da52007-09-17 20:30:04 +00002121 switch (ID) {
2122 default:
2123 break;
Chandler Carruthc4eab902011-12-12 04:36:02 +00002124 case Intrinsic::ctlz: // llvm.ctlz
2125 case Intrinsic::cttz: // llvm.cttz
2126 Assert1(isa<ConstantInt>(CI.getArgOperand(1)),
2127 "is_zero_undef argument of bit counting intrinsics must be a "
2128 "constant int", &CI);
2129 break;
Victor Hernandez02d574d2010-01-22 19:06:12 +00002130 case Intrinsic::dbg_declare: { // llvm.dbg.declare
Gabor Greifb37a64a2010-06-23 13:56:57 +00002131 Assert1(CI.getArgOperand(0) && isa<MDNode>(CI.getArgOperand(0)),
Victor Hernandez02d574d2010-01-22 19:06:12 +00002132 "invalid llvm.dbg.declare intrinsic call 1", &CI);
Gabor Greifb37a64a2010-06-23 13:56:57 +00002133 MDNode *MD = cast<MDNode>(CI.getArgOperand(0));
Victor Hernandez02d574d2010-01-22 19:06:12 +00002134 Assert1(MD->getNumOperands() == 1,
2135 "invalid llvm.dbg.declare intrinsic call 2", &CI);
Victor Hernandez02d574d2010-01-22 19:06:12 +00002136 } break;
Chris Lattner824b9582008-11-21 16:42:48 +00002137 case Intrinsic::memcpy:
2138 case Intrinsic::memmove:
2139 case Intrinsic::memset:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002140 Assert1(isa<ConstantInt>(CI.getArgOperand(3)),
Chris Lattner259f88e2008-08-23 05:31:10 +00002141 "alignment argument of memory intrinsics must be a constant int",
2142 &CI);
Eli Friedmane65e9ee2011-05-31 20:12:07 +00002143 Assert1(isa<ConstantInt>(CI.getArgOperand(4)),
2144 "isvolatile argument of memory intrinsics must be a constant int",
2145 &CI);
Chris Lattner259f88e2008-08-23 05:31:10 +00002146 break;
Bill Wendling955fdeb2008-08-23 09:46:46 +00002147 case Intrinsic::gcroot:
2148 case Intrinsic::gcwrite:
Chris Lattner415b4142008-08-24 20:46:13 +00002149 case Intrinsic::gcread:
2150 if (ID == Intrinsic::gcroot) {
Gordon Henriksena2cbe6c2008-10-25 16:28:35 +00002151 AllocaInst *AI =
Gabor Greifb37a64a2010-06-23 13:56:57 +00002152 dyn_cast<AllocaInst>(CI.getArgOperand(0)->stripPointerCasts());
Talinc87cfb62010-09-30 20:23:47 +00002153 Assert1(AI, "llvm.gcroot parameter #1 must be an alloca.", &CI);
Gabor Greifb37a64a2010-06-23 13:56:57 +00002154 Assert1(isa<Constant>(CI.getArgOperand(1)),
Chris Lattner415b4142008-08-24 20:46:13 +00002155 "llvm.gcroot parameter #2 must be a constant.", &CI);
Talinc87cfb62010-09-30 20:23:47 +00002156 if (!AI->getType()->getElementType()->isPointerTy()) {
2157 Assert1(!isa<ConstantPointerNull>(CI.getArgOperand(1)),
2158 "llvm.gcroot parameter #1 must either be a pointer alloca, "
2159 "or argument #2 must be a non-null constant.", &CI);
2160 }
Chris Lattner415b4142008-08-24 20:46:13 +00002161 }
Nick Lewycky29ef6592009-09-07 20:44:51 +00002162
Chris Lattner415b4142008-08-24 20:46:13 +00002163 Assert1(CI.getParent()->getParent()->hasGC(),
2164 "Enclosing function does not use GC.", &CI);
2165 break;
Duncan Sandsf51edad2007-09-29 16:25:54 +00002166 case Intrinsic::init_trampoline:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002167 Assert1(isa<Function>(CI.getArgOperand(1)->stripPointerCasts()),
Duncan Sandsf51edad2007-09-29 16:25:54 +00002168 "llvm.init_trampoline parameter #2 must resolve to a function.",
2169 &CI);
Gordon Henriksen27acd3a2007-12-25 02:02:10 +00002170 break;
Chris Lattnerd3745472008-10-16 06:00:36 +00002171 case Intrinsic::prefetch:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002172 Assert1(isa<ConstantInt>(CI.getArgOperand(1)) &&
2173 isa<ConstantInt>(CI.getArgOperand(2)) &&
2174 cast<ConstantInt>(CI.getArgOperand(1))->getZExtValue() < 2 &&
2175 cast<ConstantInt>(CI.getArgOperand(2))->getZExtValue() < 4,
Chris Lattnerd3745472008-10-16 06:00:36 +00002176 "invalid arguments to llvm.prefetch",
2177 &CI);
2178 break;
Bill Wendlingc5b795e2008-11-18 23:09:31 +00002179 case Intrinsic::stackprotector:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002180 Assert1(isa<AllocaInst>(CI.getArgOperand(1)->stripPointerCasts()),
Bill Wendlingc5b795e2008-11-18 23:09:31 +00002181 "llvm.stackprotector parameter #2 must resolve to an alloca.",
2182 &CI);
2183 break;
Nick Lewycky321333e2009-10-13 07:57:33 +00002184 case Intrinsic::lifetime_start:
2185 case Intrinsic::lifetime_end:
2186 case Intrinsic::invariant_start:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002187 Assert1(isa<ConstantInt>(CI.getArgOperand(0)),
Nick Lewycky321333e2009-10-13 07:57:33 +00002188 "size argument of memory use markers must be a constant integer",
2189 &CI);
2190 break;
2191 case Intrinsic::invariant_end:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002192 Assert1(isa<ConstantInt>(CI.getArgOperand(1)),
Nick Lewycky321333e2009-10-13 07:57:33 +00002193 "llvm.invariant.end parameter #2 must be a constant integer", &CI);
2194 break;
Gordon Henriksen8c33da52007-09-17 20:30:04 +00002195 }
Chris Lattnerd231fc32002-04-18 20:37:37 +00002196}
2197
Manman Rencd262572013-07-19 00:31:03 +00002198void Verifier::verifyDebugInfo(Module &M) {
2199 // Verify Debug Info.
2200 if (!DisableDebugInfoVerifier) {
2201 DebugInfoFinder Finder;
2202 Finder.processModule(M);
2203
2204 for (DebugInfoFinder::iterator I = Finder.compile_unit_begin(),
2205 E = Finder.compile_unit_end(); I != E; ++I)
2206 Assert1(DICompileUnit(*I).Verify(), "DICompileUnit does not Verify!", *I);
2207 for (DebugInfoFinder::iterator I = Finder.subprogram_begin(),
2208 E = Finder.subprogram_end(); I != E; ++I)
2209 Assert1(DISubprogram(*I).Verify(), "DISubprogram does not Verify!", *I);
2210 for (DebugInfoFinder::iterator I = Finder.global_variable_begin(),
2211 E = Finder.global_variable_end(); I != E; ++I)
2212 Assert1(DIGlobalVariable(*I).Verify(),
2213 "DIGlobalVariable does not Verify!", *I);
2214 for (DebugInfoFinder::iterator I = Finder.type_begin(),
2215 E = Finder.type_end(); I != E; ++I)
2216 Assert1(DIType(*I).Verify(), "DIType does not Verify!", *I);
2217 }
2218}
2219
Chris Lattnerd231fc32002-04-18 20:37:37 +00002220//===----------------------------------------------------------------------===//
2221// Implement the public interfaces to this file...
2222//===----------------------------------------------------------------------===//
2223
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002224FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) {
2225 return new Verifier(action);
Chris Lattnerd231fc32002-04-18 20:37:37 +00002226}
2227
Chris Lattner9ce231f2002-08-02 17:37:08 +00002228
Dan Gohmanbcf9f002010-04-08 15:57:10 +00002229/// verifyFunction - Check a function for errors, printing messages on stderr.
2230/// Return true if the function is corrupt.
2231///
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002232bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) {
Chris Lattner2eff8592004-03-14 03:16:15 +00002233 Function &F = const_cast<Function&>(f);
Reid Spencer5cbf9852007-01-30 20:08:39 +00002234 assert(!F.isDeclaration() && "Cannot verify external functions");
Misha Brukmanfd939082005-04-21 23:48:37 +00002235
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002236 FunctionPassManager FPM(F.getParent());
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002237 Verifier *V = new Verifier(action);
Chris Lattner2eff8592004-03-14 03:16:15 +00002238 FPM.add(V);
2239 FPM.run(F);
2240 return V->Broken;
Chris Lattner44d5bd92002-02-20 17:55:43 +00002241}
2242
Misha Brukmanab5c6002004-03-02 00:22:19 +00002243/// verifyModule - Check a module for errors, printing messages on stderr.
2244/// Return true if the module is corrupt.
2245///
Chris Lattner05ac92c2006-07-06 18:02:27 +00002246bool llvm::verifyModule(const Module &M, VerifierFailureAction action,
2247 std::string *ErrorInfo) {
Chris Lattner9ce231f2002-08-02 17:37:08 +00002248 PassManager PM;
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002249 Verifier *V = new Verifier(action);
Chris Lattner9ce231f2002-08-02 17:37:08 +00002250 PM.add(V);
Dan Gohman78f39da2008-06-24 17:47:37 +00002251 PM.run(const_cast<Module&>(M));
Nick Lewycky29ef6592009-09-07 20:44:51 +00002252
Chris Lattner05ac92c2006-07-06 18:02:27 +00002253 if (ErrorInfo && V->Broken)
Chris Lattner37f077a2009-08-23 04:02:03 +00002254 *ErrorInfo = V->MessagesStr.str();
Chris Lattner9ce231f2002-08-02 17:37:08 +00002255 return V->Broken;
Chris Lattner00950542001-06-06 20:29:01 +00002256}