blob: f8774bc3d9d8a96b759f6049a8f7cfb2b820633e [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"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000056#include "llvm/IR/CallingConv.h"
57#include "llvm/IR/Constants.h"
58#include "llvm/IR/DerivedTypes.h"
59#include "llvm/IR/InlineAsm.h"
60#include "llvm/IR/IntrinsicInst.h"
61#include "llvm/IR/LLVMContext.h"
62#include "llvm/IR/Metadata.h"
63#include "llvm/IR/Module.h"
Chandler Carruth84bcf932012-11-30 03:08:41 +000064#include "llvm/InstVisitor.h"
Chris Lattner58d74912008-03-12 17:45:29 +000065#include "llvm/Pass.h"
Chris Lattnercf899082004-02-14 02:47:17 +000066#include "llvm/PassManager.h"
Chris Lattner44d5bd92002-02-20 17:55:43 +000067#include "llvm/Support/CFG.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000068#include "llvm/Support/CallSite.h"
Rafael Espindolaa1b95f52012-05-31 16:04:26 +000069#include "llvm/Support/ConstantRange.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000070#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000071#include "llvm/Support/ErrorHandling.h"
Chris Lattnerc2871372009-02-28 21:05:51 +000072#include "llvm/Support/raw_ostream.h"
Chris Lattner44d5bd92002-02-20 17:55:43 +000073#include <algorithm>
Jeff Cohen4c5701d2006-03-31 07:22:05 +000074#include <cstdarg>
Chris Lattner31f84992003-11-21 20:23:48 +000075using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000076
Chris Lattnerd231fc32002-04-18 20:37:37 +000077namespace { // Anonymous namespace for class
Nick Lewycky6726b6d2009-10-25 06:33:48 +000078 struct PreVerifier : public FunctionPass {
Owen Anderson765d6452007-11-01 03:54:23 +000079 static char ID; // Pass ID, replacement for typeid
Duncan Sandsd0561902007-11-01 10:50:26 +000080
Owen Anderson081c34b2010-10-19 17:21:58 +000081 PreVerifier() : FunctionPass(ID) {
82 initializePreVerifierPass(*PassRegistry::getPassRegistry());
83 }
Duncan Sandsd0561902007-11-01 10:50:26 +000084
Chris Lattner11240d02008-12-01 03:58:38 +000085 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
86 AU.setPreservesAll();
87 }
88
Duncan Sandsd0561902007-11-01 10:50:26 +000089 // Check that the prerequisites for successful DominatorTree construction
90 // are satisfied.
Owen Anderson765d6452007-11-01 03:54:23 +000091 bool runOnFunction(Function &F) {
Duncan Sandsd0561902007-11-01 10:50:26 +000092 bool Broken = false;
93
94 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
95 if (I->empty() || !I->back().isTerminator()) {
Chris Lattner2a9a8ae2010-06-12 15:50:24 +000096 dbgs() << "Basic Block in function '" << F.getName()
97 << "' does not have terminator!\n";
David Greene4b121682010-01-05 01:29:14 +000098 WriteAsOperand(dbgs(), I, true);
99 dbgs() << "\n";
Duncan Sandsd0561902007-11-01 10:50:26 +0000100 Broken = true;
101 }
102 }
103
104 if (Broken)
Chris Lattner75361b62010-04-07 22:58:41 +0000105 report_fatal_error("Broken module, no Basic Block terminator!");
Duncan Sandsd0561902007-11-01 10:50:26 +0000106
107 return false;
Owen Anderson765d6452007-11-01 03:54:23 +0000108 }
Owen Andersonc570e332007-10-31 21:04:18 +0000109 };
Dan Gohman844731a2008-05-13 00:00:25 +0000110}
Duncan Sandsd0561902007-11-01 10:50:26 +0000111
Dan Gohman844731a2008-05-13 00:00:25 +0000112char PreVerifier::ID = 0;
Owen Anderson02dd53e2010-08-23 17:52:01 +0000113INITIALIZE_PASS(PreVerifier, "preverify", "Preliminary module verification",
Owen Andersonce665bd2010-10-07 22:25:06 +0000114 false, false)
Benjamin Kramera3ac4272010-10-22 17:35:07 +0000115static char &PreVerifyID = PreVerifier::ID;
Duncan Sandsd0561902007-11-01 10:50:26 +0000116
Dan Gohman844731a2008-05-13 00:00:25 +0000117namespace {
Nick Lewyckybc6836b2009-09-13 23:45:39 +0000118 struct Verifier : public FunctionPass, public InstVisitor<Verifier> {
Devang Patel19974732007-05-03 01:11:54 +0000119 static char ID; // Pass ID, replacement for typeid
Chris Lattner9ce231f2002-08-02 17:37:08 +0000120 bool Broken; // Is this module found to be broken?
Chris Lattnerfdc38c42004-04-02 15:45:08 +0000121 VerifierFailureAction action;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000122 // What to do if verification fails.
Misha Brukmanab5c6002004-03-02 00:22:19 +0000123 Module *Mod; // Module we are verifying right now
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000124 LLVMContext *Context; // Context within which we are verifying
125 DominatorTree *DT; // Dominator Tree, caution can be null!
Nick Lewycky29ef6592009-09-07 20:44:51 +0000126
Chris Lattner37f077a2009-08-23 04:02:03 +0000127 std::string Messages;
128 raw_string_ostream MessagesStr;
Chris Lattner00950542001-06-06 20:29:01 +0000129
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000130 /// InstInThisBlock - when verifying a basic block, keep track of all of the
131 /// instructions we have seen so far. This allows us to do efficient
132 /// dominance checks for the case when an instruction has an operand that is
133 /// an instruction in the same block.
Chris Lattner78287b42007-02-10 08:19:44 +0000134 SmallPtrSet<Instruction*, 16> InstsInThisBlock;
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000135
Duncan Sandse704d9d2010-04-29 16:10:30 +0000136 /// MDNodes - keep track of the metadata nodes that have been checked
137 /// already.
138 SmallPtrSet<MDNode *, 32> MDNodes;
139
Bill Wendlinge6e88262011-08-12 20:24:12 +0000140 /// PersonalityFn - The personality function referenced by the
141 /// LandingPadInsts. All LandingPadInsts within the same function must use
142 /// the same personality function.
143 const Value *PersonalityFn;
144
Misha Brukmanfd939082005-04-21 23:48:37 +0000145 Verifier()
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000146 : FunctionPass(ID), Broken(false),
Bill Wendlinge6e88262011-08-12 20:24:12 +0000147 action(AbortProcessAction), Mod(0), Context(0), DT(0),
148 MessagesStr(Messages), PersonalityFn(0) {
149 initializeVerifierPass(*PassRegistry::getPassRegistry());
150 }
Dan Gohman950a4c42008-03-25 22:06:05 +0000151 explicit Verifier(VerifierFailureAction ctn)
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000152 : FunctionPass(ID), Broken(false), action(ctn), Mod(0),
Bill Wendlinge6e88262011-08-12 20:24:12 +0000153 Context(0), DT(0), MessagesStr(Messages), PersonalityFn(0) {
154 initializeVerifierPass(*PassRegistry::getPassRegistry());
155 }
Chris Lattner00950542001-06-06 20:29:01 +0000156
Chris Lattner24e845f2002-06-25 15:56:27 +0000157 bool doInitialization(Module &M) {
Brian Gaeke9cebe2d2003-11-16 23:07:42 +0000158 Mod = &M;
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000159 Context = &M.getContext();
Chris Lattner3e1f1442002-09-19 16:12:19 +0000160
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000161 // We must abort before returning back to the pass manager, or else the
162 // pass manager may try to run other passes on the broken module.
163 return abortIfBroken();
Chris Lattnerd231fc32002-04-18 20:37:37 +0000164 }
165
Chris Lattner24e845f2002-06-25 15:56:27 +0000166 bool runOnFunction(Function &F) {
Chris Lattner9ce231f2002-08-02 17:37:08 +0000167 // Get dominator information if we are being run by PassManager
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000168 DT = &getAnalysis<DominatorTree>();
Chris Lattnercd070752007-04-20 23:59:29 +0000169
170 Mod = F.getParent();
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000171 if (!Context) Context = &F.getContext();
Chris Lattnercd070752007-04-20 23:59:29 +0000172
Chris Lattnerd231fc32002-04-18 20:37:37 +0000173 visit(F);
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000174 InstsInThisBlock.clear();
Bill Wendlinge6e88262011-08-12 20:24:12 +0000175 PersonalityFn = 0;
Chris Lattner3e1f1442002-09-19 16:12:19 +0000176
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000177 // We must abort before returning back to the pass manager, or else the
178 // pass manager may try to run other passes on the broken module.
179 return abortIfBroken();
Chris Lattnerd231fc32002-04-18 20:37:37 +0000180 }
181
Chris Lattner24e845f2002-06-25 15:56:27 +0000182 bool doFinalization(Module &M) {
Chris Lattner794caa12002-04-28 16:04:26 +0000183 // Scan through, checking all of the external function's linkage now...
Chris Lattner7c277b32004-06-03 06:38:43 +0000184 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattner53997412003-04-16 20:42:40 +0000185 visitGlobalValue(*I);
Chris Lattner794caa12002-04-28 16:04:26 +0000186
Chris Lattner7c277b32004-06-03 06:38:43 +0000187 // Check to make sure function prototypes are okay.
Reid Spencer5cbf9852007-01-30 20:08:39 +0000188 if (I->isDeclaration()) visitFunction(*I);
Chris Lattner7c277b32004-06-03 06:38:43 +0000189 }
190
Reid Spencer0b118202006-01-16 21:12:35 +0000191 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
192 I != E; ++I)
Chris Lattner56998b22004-12-15 20:23:49 +0000193 visitGlobalVariable(*I);
Chris Lattner61b91bc2002-10-06 22:47:32 +0000194
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000195 for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end();
196 I != E; ++I)
197 visitGlobalAlias(*I);
198
Duncan Sandse704d9d2010-04-29 16:10:30 +0000199 for (Module::named_metadata_iterator I = M.named_metadata_begin(),
200 E = M.named_metadata_end(); I != E; ++I)
201 visitNamedMDNode(*I);
202
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000203 visitModuleFlags(M);
204
Chris Lattner3e1f1442002-09-19 16:12:19 +0000205 // If the module is broken, abort at this time.
Reid Spencer7107c3b2006-07-26 16:18:00 +0000206 return abortIfBroken();
Chris Lattnera00409e2002-04-24 19:12:21 +0000207 }
208
Chris Lattner97e52e42002-04-28 21:27:06 +0000209 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
210 AU.setPreservesAll();
Owen Andersonc570e332007-10-31 21:04:18 +0000211 AU.addRequiredID(PreVerifyID);
Rafael Espindolaafe629d2012-03-24 20:02:25 +0000212 AU.addRequired<DominatorTree>();
Chris Lattner97e52e42002-04-28 21:27:06 +0000213 }
214
Misha Brukmanab5c6002004-03-02 00:22:19 +0000215 /// abortIfBroken - If the module is broken and we are supposed to abort on
216 /// this condition, do so.
217 ///
Reid Spencer7107c3b2006-07-26 16:18:00 +0000218 bool abortIfBroken() {
Chris Lattner505569d2008-08-27 17:36:58 +0000219 if (!Broken) return false;
Chris Lattner37f077a2009-08-23 04:02:03 +0000220 MessagesStr << "Broken module found, ";
Chris Lattner505569d2008-08-27 17:36:58 +0000221 switch (action) {
Chris Lattner505569d2008-08-27 17:36:58 +0000222 case AbortProcessAction:
Chris Lattner37f077a2009-08-23 04:02:03 +0000223 MessagesStr << "compilation aborted!\n";
David Greene4b121682010-01-05 01:29:14 +0000224 dbgs() << MessagesStr.str();
Torok Edwindac237e2009-07-08 20:53:28 +0000225 // Client should choose different reaction if abort is not desired
226 abort();
Chris Lattner505569d2008-08-27 17:36:58 +0000227 case PrintMessageAction:
Chris Lattner37f077a2009-08-23 04:02:03 +0000228 MessagesStr << "verification continues.\n";
David Greene4b121682010-01-05 01:29:14 +0000229 dbgs() << MessagesStr.str();
Chris Lattner505569d2008-08-27 17:36:58 +0000230 return false;
231 case ReturnStatusAction:
Chris Lattner37f077a2009-08-23 04:02:03 +0000232 MessagesStr << "compilation terminated.\n";
Nick Lewycky57174882009-03-15 06:40:32 +0000233 return true;
Chris Lattner3e1f1442002-09-19 16:12:19 +0000234 }
Chandler Carruth732f05c2012-01-10 18:08:01 +0000235 llvm_unreachable("Invalid action");
Chris Lattner3e1f1442002-09-19 16:12:19 +0000236 }
237
Chris Lattner53997412003-04-16 20:42:40 +0000238
Chris Lattnerd231fc32002-04-18 20:37:37 +0000239 // Verification methods...
Chris Lattner53997412003-04-16 20:42:40 +0000240 void visitGlobalValue(GlobalValue &GV);
Chris Lattner56998b22004-12-15 20:23:49 +0000241 void visitGlobalVariable(GlobalVariable &GV);
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000242 void visitGlobalAlias(GlobalAlias &GA);
Duncan Sandse704d9d2010-04-29 16:10:30 +0000243 void visitNamedMDNode(NamedMDNode &NMD);
244 void visitMDNode(MDNode &MD, Function *F);
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000245 void visitModuleFlags(Module &M);
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000246 void visitModuleFlag(MDNode *Op, DenseMap<MDString*, MDNode*> &SeenIDs,
247 SmallVectorImpl<MDNode*> &Requirements);
Chris Lattner24e845f2002-06-25 15:56:27 +0000248 void visitFunction(Function &F);
249 void visitBasicBlock(BasicBlock &BB);
Chris Lattnercad208b2008-08-28 04:02:44 +0000250 using InstVisitor<Verifier>::visit;
Nick Lewycky29ef6592009-09-07 20:44:51 +0000251
Chris Lattnercad208b2008-08-28 04:02:44 +0000252 void visit(Instruction &I);
Nick Lewycky29ef6592009-09-07 20:44:51 +0000253
Reid Spencer3da59db2006-11-27 01:05:10 +0000254 void visitTruncInst(TruncInst &I);
255 void visitZExtInst(ZExtInst &I);
256 void visitSExtInst(SExtInst &I);
257 void visitFPTruncInst(FPTruncInst &I);
258 void visitFPExtInst(FPExtInst &I);
259 void visitFPToUIInst(FPToUIInst &I);
260 void visitFPToSIInst(FPToSIInst &I);
261 void visitUIToFPInst(UIToFPInst &I);
262 void visitSIToFPInst(SIToFPInst &I);
263 void visitIntToPtrInst(IntToPtrInst &I);
264 void visitPtrToIntInst(PtrToIntInst &I);
265 void visitBitCastInst(BitCastInst &I);
Chris Lattner24e845f2002-06-25 15:56:27 +0000266 void visitPHINode(PHINode &PN);
267 void visitBinaryOperator(BinaryOperator &B);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000268 void visitICmpInst(ICmpInst &IC);
269 void visitFCmpInst(FCmpInst &FC);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000270 void visitExtractElementInst(ExtractElementInst &EI);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000271 void visitInsertElementInst(InsertElementInst &EI);
Chris Lattner00f10232006-04-08 01:18:18 +0000272 void visitShuffleVectorInst(ShuffleVectorInst &EI);
Chris Lattner4d45bd02003-10-18 05:57:43 +0000273 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
Chris Lattner24e845f2002-06-25 15:56:27 +0000274 void visitCallInst(CallInst &CI);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000275 void visitInvokeInst(InvokeInst &II);
Chris Lattner24e845f2002-06-25 15:56:27 +0000276 void visitGetElementPtrInst(GetElementPtrInst &GEP);
277 void visitLoadInst(LoadInst &LI);
278 void visitStoreInst(StoreInst &SI);
Rafael Espindolac987f4c2012-02-26 02:23:37 +0000279 void verifyDominatesUse(Instruction &I, unsigned i);
Chris Lattner24e845f2002-06-25 15:56:27 +0000280 void visitInstruction(Instruction &I);
281 void visitTerminatorInst(TerminatorInst &I);
Nick Lewycky6a7cb632010-02-15 22:09:09 +0000282 void visitBranchInst(BranchInst &BI);
Chris Lattner24e845f2002-06-25 15:56:27 +0000283 void visitReturnInst(ReturnInst &RI);
Chris Lattner0f9e9d02004-05-21 16:47:21 +0000284 void visitSwitchInst(SwitchInst &SI);
Dan Gohman37680912010-08-02 23:08:33 +0000285 void visitIndirectBrInst(IndirectBrInst &BI);
Chris Lattner230c1a72004-03-12 05:54:31 +0000286 void visitSelectInst(SelectInst &SI);
Chris Lattner627079d2002-11-21 16:54:22 +0000287 void visitUserOp1(Instruction &I);
288 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
Brian Gaeked0fde302003-11-11 22:41:34 +0000289 void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI);
Eli Friedmanff030482011-07-28 21:48:00 +0000290 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI);
291 void visitAtomicRMWInst(AtomicRMWInst &RMWI);
Eli Friedman47f35132011-07-25 23:16:38 +0000292 void visitFenceInst(FenceInst &FI);
Victor Hernandez7b929da2009-10-23 21:09:37 +0000293 void visitAllocaInst(AllocaInst &AI);
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000294 void visitExtractValueInst(ExtractValueInst &EVI);
295 void visitInsertValueInst(InsertValueInst &IVI);
Bill Wendlinge6e88262011-08-12 20:24:12 +0000296 void visitLandingPadInst(LandingPadInst &LPI);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000297
Duncan Sandsd9d70392007-12-21 19:19:01 +0000298 void VerifyCallSite(CallSite CS);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000299 bool PerformTypeCheck(Intrinsic::ID ID, Function *F, Type *Ty,
Bill Wendlinga6ce05f2008-11-13 07:11:27 +0000300 int VT, unsigned ArgNo, std::string &Suffix);
Chris Lattner55dc5c72012-05-27 19:37:05 +0000301 bool VerifyIntrinsicType(Type *Ty,
302 ArrayRef<Intrinsic::IITDescriptor> &Infos,
303 SmallVectorImpl<Type*> &ArgTys);
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000304 bool VerifyAttributeCount(AttributeSet Attrs, unsigned Params);
305 void VerifyAttributeTypes(AttributeSet Attrs, unsigned Idx,
306 bool isFunction, const Value *V);
307 void VerifyParameterAttrs(AttributeSet Attrs, unsigned Idx, Type *Ty,
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000308 bool isReturnValue, const Value *V);
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000309 void VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs,
Duncan Sandscfad1b42008-01-12 16:42:01 +0000310 const Value *V);
Chris Lattner15e87522003-11-21 17:35:51 +0000311
312 void WriteValue(const Value *V) {
313 if (!V) return;
Chris Lattner31f84992003-11-21 20:23:48 +0000314 if (isa<Instruction>(V)) {
Nick Lewycky2bb6f6a2009-10-17 19:43:45 +0000315 MessagesStr << *V << '\n';
Chris Lattner31f84992003-11-21 20:23:48 +0000316 } else {
Chris Lattner37f077a2009-08-23 04:02:03 +0000317 WriteAsOperand(MessagesStr, V, true, Mod);
Nick Lewycky2bb6f6a2009-10-17 19:43:45 +0000318 MessagesStr << '\n';
Chris Lattner15e87522003-11-21 17:35:51 +0000319 }
320 }
321
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000322 void WriteType(Type *T) {
Chris Lattnerc2871372009-02-28 21:05:51 +0000323 if (!T) return;
Chris Lattner1afcace2011-07-09 17:41:24 +0000324 MessagesStr << ' ' << *T;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000325 }
326
Chris Lattner15e87522003-11-21 17:35:51 +0000327
Chris Lattnerd231fc32002-04-18 20:37:37 +0000328 // CheckFailed - A check failed, so print out the condition and the message
329 // that failed. This provides a nice place to put a breakpoint if you want
330 // to see why something is not correct.
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000331 void CheckFailed(const Twine &Message,
Chris Lattner15e87522003-11-21 17:35:51 +0000332 const Value *V1 = 0, const Value *V2 = 0,
333 const Value *V3 = 0, const Value *V4 = 0) {
Chris Lattner37f077a2009-08-23 04:02:03 +0000334 MessagesStr << Message.str() << "\n";
Chris Lattner15e87522003-11-21 17:35:51 +0000335 WriteValue(V1);
336 WriteValue(V2);
337 WriteValue(V3);
338 WriteValue(V4);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000339 Broken = true;
340 }
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000341
Nick Lewycky4b6af8a2009-09-08 02:02:39 +0000342 void CheckFailed(const Twine &Message, const Value *V1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000343 Type *T2, const Value *V3 = 0) {
Chris Lattner37f077a2009-08-23 04:02:03 +0000344 MessagesStr << Message.str() << "\n";
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000345 WriteValue(V1);
346 WriteType(T2);
347 WriteValue(V3);
Reid Spencer5dff1582004-05-27 21:58:13 +0000348 Broken = true;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000349 }
Nick Lewycky49072472009-09-08 01:23:52 +0000350
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000351 void CheckFailed(const Twine &Message, Type *T1,
352 Type *T2 = 0, Type *T3 = 0) {
Nick Lewycky49072472009-09-08 01:23:52 +0000353 MessagesStr << Message.str() << "\n";
354 WriteType(T1);
355 WriteType(T2);
356 WriteType(T3);
357 Broken = true;
358 }
Chris Lattnerd231fc32002-04-18 20:37:37 +0000359 };
Chris Lattner31f84992003-11-21 20:23:48 +0000360} // End anonymous namespace
361
Dan Gohman844731a2008-05-13 00:00:25 +0000362char Verifier::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +0000363INITIALIZE_PASS_BEGIN(Verifier, "verify", "Module Verifier", false, false)
364INITIALIZE_PASS_DEPENDENCY(PreVerifier)
365INITIALIZE_PASS_DEPENDENCY(DominatorTree)
366INITIALIZE_PASS_END(Verifier, "verify", "Module Verifier", false, false)
Chris Lattner00950542001-06-06 20:29:01 +0000367
Chris Lattner44d5bd92002-02-20 17:55:43 +0000368// Assert - We know that cond should be true, if not print an error message.
369#define Assert(C, M) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000370 do { if (!(C)) { CheckFailed(M); return; } } while (0)
Chris Lattner44d5bd92002-02-20 17:55:43 +0000371#define Assert1(C, M, V1) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000372 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
Chris Lattner44d5bd92002-02-20 17:55:43 +0000373#define Assert2(C, M, V1, V2) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000374 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
Chris Lattner24e845f2002-06-25 15:56:27 +0000375#define Assert3(C, M, V1, V2, V3) \
376 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
377#define Assert4(C, M, V1, V2, V3, V4) \
378 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
Chris Lattner00950542001-06-06 20:29:01 +0000379
Chris Lattnercad208b2008-08-28 04:02:44 +0000380void Verifier::visit(Instruction &I) {
381 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
382 Assert1(I.getOperand(i) != 0, "Operand is null", &I);
383 InstVisitor<Verifier>::visit(I);
384}
385
386
Chris Lattner53997412003-04-16 20:42:40 +0000387void Verifier::visitGlobalValue(GlobalValue &GV) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000388 Assert1(!GV.isDeclaration() ||
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000389 GV.isMaterializable() ||
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000390 GV.hasExternalLinkage() ||
391 GV.hasDLLImportLinkage() ||
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000392 GV.hasExternalWeakLinkage() ||
393 (isa<GlobalAlias>(GV) &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000394 (GV.hasLocalLinkage() || GV.hasWeakLinkage())),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000395 "Global is external, but doesn't have external or dllimport or weak linkage!",
396 &GV);
397
Reid Spencer5cbf9852007-01-30 20:08:39 +0000398 Assert1(!GV.hasDLLImportLinkage() || GV.isDeclaration(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000399 "Global is marked as dllimport, but not external", &GV);
Nick Lewycky49072472009-09-08 01:23:52 +0000400
Chris Lattner53997412003-04-16 20:42:40 +0000401 Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
402 "Only global variables can have appending linkage!", &GV);
403
404 if (GV.hasAppendingLinkage()) {
Nick Lewycky49072472009-09-08 01:23:52 +0000405 GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
Duncan Sands1df98592010-02-16 11:11:14 +0000406 Assert1(GVar && GVar->getType()->getElementType()->isArrayTy(),
Nick Lewycky49072472009-09-08 01:23:52 +0000407 "Only global arrays can have appending linkage!", GVar);
Chris Lattner53997412003-04-16 20:42:40 +0000408 }
Bill Wendling55ae5152010-08-20 22:05:50 +0000409
Bill Wendling32811be2012-08-17 18:33:14 +0000410 Assert1(!GV.hasLinkOnceODRAutoHideLinkage() || GV.hasDefaultVisibility(),
411 "linkonce_odr_auto_hide can only have default visibility!",
Bill Wendling55ae5152010-08-20 22:05:50 +0000412 &GV);
Chris Lattner53997412003-04-16 20:42:40 +0000413}
414
Chris Lattner56998b22004-12-15 20:23:49 +0000415void Verifier::visitGlobalVariable(GlobalVariable &GV) {
Chris Lattner6693da02007-09-19 17:14:45 +0000416 if (GV.hasInitializer()) {
Chris Lattner56998b22004-12-15 20:23:49 +0000417 Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(),
418 "Global variable initializer type does not match global "
419 "variable type!", &GV);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000420
Chris Lattnercd81f5d2009-08-05 05:41:44 +0000421 // If the global has common linkage, it must have a zero initializer and
422 // cannot be constant.
423 if (GV.hasCommonLinkage()) {
Chris Lattner26d054d2009-08-05 05:21:07 +0000424 Assert1(GV.getInitializer()->isNullValue(),
425 "'common' global must have a zero initializer!", &GV);
Chris Lattnercd81f5d2009-08-05 05:41:44 +0000426 Assert1(!GV.isConstant(), "'common' global may not be marked constant!",
427 &GV);
428 }
Chris Lattner6693da02007-09-19 17:14:45 +0000429 } else {
430 Assert1(GV.hasExternalLinkage() || GV.hasDLLImportLinkage() ||
431 GV.hasExternalWeakLinkage(),
432 "invalid linkage type for global declaration", &GV);
433 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000434
Nick Lewycky2c44a802011-04-08 07:30:21 +0000435 if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
436 GV.getName() == "llvm.global_dtors")) {
437 Assert1(!GV.hasInitializer() || GV.hasAppendingLinkage(),
438 "invalid linkage for intrinsic global variable", &GV);
439 // Don't worry about emitting an error for it not being an array,
440 // visitGlobalValue will complain on appending non-array.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000441 if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getType())) {
442 StructType *STy = dyn_cast<StructType>(ATy->getElementType());
443 PointerType *FuncPtrTy =
Micah Villmowb8bce922012-10-24 17:25:11 +0000444 FunctionType::get(Type::getVoidTy(*Context), false)->getPointerTo();
Nick Lewycky2c44a802011-04-08 07:30:21 +0000445 Assert1(STy && STy->getNumElements() == 2 &&
446 STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
447 STy->getTypeAtIndex(1) == FuncPtrTy,
448 "wrong type for intrinsic global variable", &GV);
449 }
450 }
451
Rafael Espindola97bf57d2013-04-22 15:16:51 +0000452 if (GV.hasName() && (GV.getName() == "llvm.used" ||
453 GV.getName() == "llvm.compiler_used")) {
Rafael Espindolacde25b42013-04-22 14:58:02 +0000454 Assert1(!GV.hasInitializer() || GV.hasAppendingLinkage(),
455 "invalid linkage for intrinsic global variable", &GV);
456 Type *GVType = GV.getType()->getElementType();
457 if (ArrayType *ATy = dyn_cast<ArrayType>(GVType)) {
458 PointerType *PTy = dyn_cast<PointerType>(ATy->getElementType());
459 Assert1(PTy, "wrong type for intrinsic global variable", &GV);
460 if (GV.hasInitializer()) {
461 Constant *Init = GV.getInitializer();
462 ConstantArray *InitArray = dyn_cast<ConstantArray>(Init);
463 Assert1(InitArray, "wrong initalizer for intrinsic global variable",
464 Init);
465 for (unsigned i = 0, e = InitArray->getNumOperands(); i != e; ++i) {
Rafael Espindolaaf10fe62013-05-27 22:47:09 +0000466 Value *V = Init->getOperand(i)->stripPointerCastsNoFollowAliases();
467 Assert1(
468 isa<GlobalVariable>(V) || isa<Function>(V) || isa<GlobalAlias>(V),
469 "invalid llvm.used member", V);
Rafael Espindola9f8e6da2013-06-11 13:18:13 +0000470 Assert1(V->hasName(), "members of llvm.used must be named", V);
Rafael Espindolacde25b42013-04-22 14:58:02 +0000471 }
472 }
473 }
474 }
475
Chris Lattner56998b22004-12-15 20:23:49 +0000476 visitGlobalValue(GV);
477}
478
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000479void Verifier::visitGlobalAlias(GlobalAlias &GA) {
480 Assert1(!GA.getName().empty(),
481 "Alias name cannot be empty!", &GA);
Rafael Espindolabb46f522009-01-15 20:18:42 +0000482 Assert1(GA.hasExternalLinkage() || GA.hasLocalLinkage() ||
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000483 GA.hasWeakLinkage(),
484 "Alias should have external or external weak linkage!", &GA);
Anton Korobeynikov018f7712008-05-08 23:11:06 +0000485 Assert1(GA.getAliasee(),
486 "Aliasee cannot be NULL!", &GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000487 Assert1(GA.getType() == GA.getAliasee()->getType(),
488 "Alias and aliasee types should match!", &GA);
Rafael Espindolabea46262011-01-08 16:42:36 +0000489 Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA);
Anton Korobeynikov018f7712008-05-08 23:11:06 +0000490
Anton Korobeynikov0f53f7f2007-04-28 14:35:41 +0000491 if (!isa<GlobalValue>(GA.getAliasee())) {
492 const ConstantExpr *CE = dyn_cast<ConstantExpr>(GA.getAliasee());
Chris Lattnera2165ed2009-04-25 21:23:19 +0000493 Assert1(CE &&
494 (CE->getOpcode() == Instruction::BitCast ||
495 CE->getOpcode() == Instruction::GetElementPtr) &&
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000496 isa<GlobalValue>(CE->getOperand(0)),
Anton Korobeynikov0f53f7f2007-04-28 14:35:41 +0000497 "Aliasee should be either GlobalValue or bitcast of GlobalValue",
498 &GA);
499 }
Anton Korobeynikov726d45c2008-03-22 08:36:14 +0000500
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000501 const GlobalValue* Aliasee = GA.resolveAliasedGlobal(/*stopOnWeak*/ false);
Anton Korobeynikov726d45c2008-03-22 08:36:14 +0000502 Assert1(Aliasee,
Anton Korobeynikovef30c1d2008-03-22 08:37:05 +0000503 "Aliasing chain should end with function or global variable", &GA);
Anton Korobeynikov726d45c2008-03-22 08:36:14 +0000504
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000505 visitGlobalValue(GA);
506}
507
Duncan Sandse704d9d2010-04-29 16:10:30 +0000508void Verifier::visitNamedMDNode(NamedMDNode &NMD) {
509 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) {
510 MDNode *MD = NMD.getOperand(i);
511 if (!MD)
512 continue;
513
Dan Gohman17aa92c2010-07-21 23:38:33 +0000514 Assert1(!MD->isFunctionLocal(),
515 "Named metadata operand cannot be function local!", MD);
Duncan Sandse704d9d2010-04-29 16:10:30 +0000516 visitMDNode(*MD, 0);
517 }
518}
519
520void Verifier::visitMDNode(MDNode &MD, Function *F) {
521 // Only visit each node once. Metadata can be mutually recursive, so this
522 // avoids infinite recursion here, as well as being an optimization.
523 if (!MDNodes.insert(&MD))
524 return;
525
526 for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) {
527 Value *Op = MD.getOperand(i);
528 if (!Op)
529 continue;
Dan Gohman557c83c2010-07-21 20:25:43 +0000530 if (isa<Constant>(Op) || isa<MDString>(Op))
Duncan Sandse704d9d2010-04-29 16:10:30 +0000531 continue;
532 if (MDNode *N = dyn_cast<MDNode>(Op)) {
533 Assert2(MD.isFunctionLocal() || !N->isFunctionLocal(),
534 "Global metadata operand cannot be function local!", &MD, N);
535 visitMDNode(*N, F);
536 continue;
537 }
538 Assert2(MD.isFunctionLocal(), "Invalid operand for global metadata!", &MD, Op);
539
540 // If this was an instruction, bb, or argument, verify that it is in the
541 // function that we expect.
542 Function *ActualF = 0;
543 if (Instruction *I = dyn_cast<Instruction>(Op))
544 ActualF = I->getParent()->getParent();
545 else if (BasicBlock *BB = dyn_cast<BasicBlock>(Op))
546 ActualF = BB->getParent();
547 else if (Argument *A = dyn_cast<Argument>(Op))
548 ActualF = A->getParent();
549 assert(ActualF && "Unimplemented function local metadata case!");
550
551 Assert2(ActualF == F, "function-local metadata used in wrong function",
552 &MD, Op);
553 }
554}
555
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000556void Verifier::visitModuleFlags(Module &M) {
557 const NamedMDNode *Flags = M.getModuleFlagsMetadata();
558 if (!Flags) return;
559
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000560 // Scan each flag, and track the flags and requirements.
561 DenseMap<MDString*, MDNode*> SeenIDs;
562 SmallVector<MDNode*, 16> Requirements;
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000563 for (unsigned I = 0, E = Flags->getNumOperands(); I != E; ++I) {
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000564 visitModuleFlag(Flags->getOperand(I), SeenIDs, Requirements);
565 }
566
567 // Validate that the requirements in the module are valid.
568 for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
569 MDNode *Requirement = Requirements[I];
570 MDString *Flag = cast<MDString>(Requirement->getOperand(0));
571 Value *ReqValue = Requirement->getOperand(1);
572
573 MDNode *Op = SeenIDs.lookup(Flag);
574 if (!Op) {
575 CheckFailed("invalid requirement on flag, flag is not present in module",
576 Flag);
577 continue;
578 }
579
580 if (Op->getOperand(2) != ReqValue) {
581 CheckFailed(("invalid requirement on flag, "
582 "flag does not have the required value"),
583 Flag);
584 continue;
585 }
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000586 }
587}
588
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000589void Verifier::visitModuleFlag(MDNode *Op, DenseMap<MDString*, MDNode*>&SeenIDs,
590 SmallVectorImpl<MDNode*> &Requirements) {
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000591 // Each module flag should have three arguments, the merge behavior (a
592 // constant int), the flag ID (an MDString), and the value.
593 Assert1(Op->getNumOperands() == 3,
594 "incorrect number of operands in module flag", Op);
595 ConstantInt *Behavior = dyn_cast<ConstantInt>(Op->getOperand(0));
596 MDString *ID = dyn_cast<MDString>(Op->getOperand(1));
597 Assert1(Behavior,
598 "invalid behavior operand in module flag (expected constant integer)",
599 Op->getOperand(0));
600 unsigned BehaviorValue = Behavior->getZExtValue();
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000601 Assert1(ID,
602 "invalid ID operand in module flag (expected metadata string)",
603 Op->getOperand(1));
604
Daniel Dunbar5db391c2013-01-16 21:38:56 +0000605 // Sanity check the values for behaviors with additional requirements.
606 switch (BehaviorValue) {
607 default:
608 Assert1(false,
609 "invalid behavior operand in module flag (unexpected constant)",
610 Op->getOperand(0));
611 break;
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000612
Daniel Dunbar5db391c2013-01-16 21:38:56 +0000613 case Module::Error:
614 case Module::Warning:
615 case Module::Override:
616 // These behavior types accept any value.
617 break;
618
619 case Module::Require: {
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000620 // The value should itself be an MDNode with two operands, a flag ID (an
621 // MDString), and a value.
622 MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2));
623 Assert1(Value && Value->getNumOperands() == 2,
624 "invalid value for 'require' module flag (expected metadata pair)",
625 Op->getOperand(2));
626 Assert1(isa<MDString>(Value->getOperand(0)),
627 ("invalid value for 'require' module flag "
628 "(first value operand should be a string)"),
629 Value->getOperand(0));
Daniel Dunbar12bfff42013-01-15 20:52:06 +0000630
631 // Append it to the list of requirements, to check once all module flags are
632 // scanned.
633 Requirements.push_back(Value);
Daniel Dunbar5db391c2013-01-16 21:38:56 +0000634 break;
635 }
636
637 case Module::Append:
638 case Module::AppendUnique: {
639 // These behavior types require the operand be an MDNode.
640 Assert1(isa<MDNode>(Op->getOperand(2)),
641 "invalid value for 'append'-type module flag "
642 "(expected a metadata node)", Op->getOperand(2));
643 break;
644 }
645 }
646
647 // Unless this is a "requires" flag, check the ID is unique.
648 if (BehaviorValue != Module::Require) {
649 bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
650 Assert1(Inserted,
651 "module flag identifiers must be unique (or of 'require' type)",
652 ID);
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000653 }
654}
655
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000656void Verifier::VerifyAttributeTypes(AttributeSet Attrs, unsigned Idx,
657 bool isFunction, const Value* V) {
658 unsigned Slot = ~0U;
659 for (unsigned I = 0, E = Attrs.getNumSlots(); I != E; ++I)
660 if (Attrs.getSlotIndex(I) == Idx) {
661 Slot = I;
662 break;
663 }
664
665 assert(Slot != ~0U && "Attribute set inconsistency!");
666
667 for (AttributeSet::iterator I = Attrs.begin(Slot), E = Attrs.end(Slot);
668 I != E; ++I) {
669 if (I->isStringAttribute())
670 continue;
671
672 if (I->getKindAsEnum() == Attribute::NoReturn ||
673 I->getKindAsEnum() == Attribute::NoUnwind ||
674 I->getKindAsEnum() == Attribute::ReadNone ||
675 I->getKindAsEnum() == Attribute::ReadOnly ||
676 I->getKindAsEnum() == Attribute::NoInline ||
677 I->getKindAsEnum() == Attribute::AlwaysInline ||
678 I->getKindAsEnum() == Attribute::OptimizeForSize ||
679 I->getKindAsEnum() == Attribute::StackProtect ||
680 I->getKindAsEnum() == Attribute::StackProtectReq ||
681 I->getKindAsEnum() == Attribute::StackProtectStrong ||
682 I->getKindAsEnum() == Attribute::NoRedZone ||
683 I->getKindAsEnum() == Attribute::NoImplicitFloat ||
684 I->getKindAsEnum() == Attribute::Naked ||
685 I->getKindAsEnum() == Attribute::InlineHint ||
686 I->getKindAsEnum() == Attribute::StackAlignment ||
687 I->getKindAsEnum() == Attribute::UWTable ||
688 I->getKindAsEnum() == Attribute::NonLazyBind ||
689 I->getKindAsEnum() == Attribute::ReturnsTwice ||
690 I->getKindAsEnum() == Attribute::SanitizeAddress ||
691 I->getKindAsEnum() == Attribute::SanitizeThread ||
692 I->getKindAsEnum() == Attribute::SanitizeMemory ||
693 I->getKindAsEnum() == Attribute::MinSize ||
694 I->getKindAsEnum() == Attribute::NoDuplicate ||
Diego Novillo77226a02013-05-24 12:26:52 +0000695 I->getKindAsEnum() == Attribute::NoBuiltin ||
696 I->getKindAsEnum() == Attribute::Cold) {
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000697 if (!isFunction)
698 CheckFailed("Attribute '" + I->getKindAsString() +
699 "' only applies to functions!", V);
700 return;
701 } else if (isFunction) {
702 CheckFailed("Attribute '" + I->getKindAsString() +
703 "' does not apply to functions!", V);
704 return;
705 }
706 }
707}
708
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000709// VerifyParameterAttrs - Check the given attributes for an argument or return
Duncan Sandscfad1b42008-01-12 16:42:01 +0000710// value of the specified type. The value V is printed in error messages.
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000711void Verifier::VerifyParameterAttrs(AttributeSet Attrs, unsigned Idx, Type *Ty,
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000712 bool isReturnValue, const Value *V) {
Bill Wendlingbed80592013-01-21 23:03:18 +0000713 if (!Attrs.hasAttributes(Idx))
Duncan Sandscfad1b42008-01-12 16:42:01 +0000714 return;
715
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000716 VerifyAttributeTypes(Attrs, Idx, false, V);
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000717
Bill Wendling943c2912012-10-09 09:51:10 +0000718 if (isReturnValue)
Bill Wendlingbed80592013-01-21 23:03:18 +0000719 Assert1(!Attrs.hasAttribute(Idx, Attribute::ByVal) &&
720 !Attrs.hasAttribute(Idx, Attribute::Nest) &&
721 !Attrs.hasAttribute(Idx, Attribute::StructRet) &&
Stephen Lin456ca042013-04-20 05:14:40 +0000722 !Attrs.hasAttribute(Idx, Attribute::NoCapture) &&
723 !Attrs.hasAttribute(Idx, Attribute::Returned),
724 "Attribute 'byval', 'nest', 'sret', 'nocapture', and 'returned' "
Bill Wendling943c2912012-10-09 09:51:10 +0000725 "do not apply to return values!", V);
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000726
Bill Wendling28d1c602012-10-09 20:11:19 +0000727 // Check for mutually incompatible attributes.
Bill Wendlingbed80592013-01-21 23:03:18 +0000728 Assert1(!((Attrs.hasAttribute(Idx, Attribute::ByVal) &&
729 Attrs.hasAttribute(Idx, Attribute::Nest)) ||
730 (Attrs.hasAttribute(Idx, Attribute::ByVal) &&
731 Attrs.hasAttribute(Idx, Attribute::StructRet)) ||
732 (Attrs.hasAttribute(Idx, Attribute::Nest) &&
733 Attrs.hasAttribute(Idx, Attribute::StructRet))), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000734 "'byval, nest, and sret' are incompatible!", V);
735
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::ByVal) &&
739 Attrs.hasAttribute(Idx, Attribute::InReg)) ||
740 (Attrs.hasAttribute(Idx, Attribute::Nest) &&
741 Attrs.hasAttribute(Idx, Attribute::InReg))), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000742 "'byval, nest, and inreg' are incompatible!", V);
743
Stephen Lin13aba142013-04-23 16:31:56 +0000744 Assert1(!(Attrs.hasAttribute(Idx, Attribute::StructRet) &&
745 Attrs.hasAttribute(Idx, Attribute::Returned)), "Attributes "
746 "'sret and returned' are incompatible!", V);
747
Bill Wendlingbed80592013-01-21 23:03:18 +0000748 Assert1(!(Attrs.hasAttribute(Idx, Attribute::ZExt) &&
749 Attrs.hasAttribute(Idx, Attribute::SExt)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000750 "'zeroext and signext' are incompatible!", V);
751
Bill Wendlingbed80592013-01-21 23:03:18 +0000752 Assert1(!(Attrs.hasAttribute(Idx, Attribute::ReadNone) &&
753 Attrs.hasAttribute(Idx, Attribute::ReadOnly)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000754 "'readnone and readonly' are incompatible!", V);
755
Bill Wendlingbed80592013-01-21 23:03:18 +0000756 Assert1(!(Attrs.hasAttribute(Idx, Attribute::NoInline) &&
757 Attrs.hasAttribute(Idx, Attribute::AlwaysInline)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000758 "'noinline and alwaysinline' are incompatible!", V);
Duncan Sandscfad1b42008-01-12 16:42:01 +0000759
Bill Wendlingbed80592013-01-21 23:03:18 +0000760 Assert1(!AttrBuilder(Attrs, Idx).
Bill Wendlinge7436542013-01-30 23:07:40 +0000761 hasAttributes(AttributeFuncs::typeIncompatible(Ty, Idx), Idx),
Bill Wendling1feacad2012-10-14 07:52:48 +0000762 "Wrong types for attribute: " +
Bill Wendlinge7436542013-01-30 23:07:40 +0000763 AttributeFuncs::typeIncompatible(Ty, Idx).getAsString(Idx), V);
Dan Gohman39dfc2c2008-08-27 14:48:06 +0000764
Bill Wendling943c2912012-10-09 09:51:10 +0000765 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
Bill Wendlingbed80592013-01-21 23:03:18 +0000766 Assert1(!Attrs.hasAttribute(Idx, Attribute::ByVal) ||
Bill Wendling943c2912012-10-09 09:51:10 +0000767 PTy->getElementType()->isSized(),
768 "Attribute 'byval' does not support unsized types!", V);
769 else
Bill Wendlingbed80592013-01-21 23:03:18 +0000770 Assert1(!Attrs.hasAttribute(Idx, Attribute::ByVal),
Bill Wendling943c2912012-10-09 09:51:10 +0000771 "Attribute 'byval' only applies to parameters with pointer type!",
772 V);
Duncan Sandscfad1b42008-01-12 16:42:01 +0000773}
774
775// VerifyFunctionAttrs - Check parameter attributes against a function type.
Duncan Sandsd9d70392007-12-21 19:19:01 +0000776// The value V is printed in error messages.
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000777void Verifier::VerifyFunctionAttrs(FunctionType *FT, AttributeSet Attrs,
Duncan Sandscfad1b42008-01-12 16:42:01 +0000778 const Value *V) {
Chris Lattner58d74912008-03-12 17:45:29 +0000779 if (Attrs.isEmpty())
Duncan Sandsd9d70392007-12-21 19:19:01 +0000780 return;
781
Duncan Sandsd9d70392007-12-21 19:19:01 +0000782 bool SawNest = false;
Stephen Lin456ca042013-04-20 05:14:40 +0000783 bool SawReturned = false;
Duncan Sandsd9d70392007-12-21 19:19:01 +0000784
Chris Lattner58d74912008-03-12 17:45:29 +0000785 for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000786 unsigned Idx = Attrs.getSlotIndex(i);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000787
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000788 Type *Ty;
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000789 if (Idx == 0)
Chris Lattner58d74912008-03-12 17:45:29 +0000790 Ty = FT->getReturnType();
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000791 else if (Idx-1 < FT->getNumParams())
792 Ty = FT->getParamType(Idx-1);
Chris Lattner58d74912008-03-12 17:45:29 +0000793 else
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000794 break; // VarArgs attributes, verified elsewhere.
795
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000796 VerifyParameterAttrs(Attrs, Idx, Ty, Idx == 0, V);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000797
Stephen Lin456ca042013-04-20 05:14:40 +0000798 if (Idx == 0)
799 continue;
800
801 if (Attrs.hasAttribute(Idx, Attribute::Nest)) {
Duncan Sandsd9d70392007-12-21 19:19:01 +0000802 Assert1(!SawNest, "More than one parameter has attribute nest!", V);
803 SawNest = true;
804 }
805
Stephen Lin456ca042013-04-20 05:14:40 +0000806 if (Attrs.hasAttribute(Idx, Attribute::Returned)) {
807 Assert1(!SawReturned, "More than one parameter has attribute returned!",
808 V);
809 Assert1(Ty->canLosslesslyBitCastTo(FT->getReturnType()), "Incompatible "
810 "argument and return types for 'returned' attribute", V);
811 SawReturned = true;
812 }
813
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000814 if (Attrs.hasAttribute(Idx, Attribute::StructRet))
815 Assert1(Idx == 1, "Attribute sret is not on first parameter!", V);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000816 }
Devang Patel7c310852008-10-01 23:41:25 +0000817
Bill Wendling956f1342013-01-18 21:11:39 +0000818 if (!Attrs.hasAttributes(AttributeSet::FunctionIndex))
819 return;
820
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000821 VerifyAttributeTypes(Attrs, AttributeSet::FunctionIndex, true, V);
Bill Wendling28d1c602012-10-09 20:11:19 +0000822
Bill Wendling956f1342013-01-18 21:11:39 +0000823 Assert1(!(Attrs.hasAttribute(AttributeSet::FunctionIndex,
824 Attribute::ReadNone) &&
825 Attrs.hasAttribute(AttributeSet::FunctionIndex,
826 Attribute::ReadOnly)),
827 "Attributes 'readnone and readonly' are incompatible!", V);
Bill Wendling28d1c602012-10-09 20:11:19 +0000828
Bill Wendling956f1342013-01-18 21:11:39 +0000829 Assert1(!(Attrs.hasAttribute(AttributeSet::FunctionIndex,
830 Attribute::NoInline) &&
831 Attrs.hasAttribute(AttributeSet::FunctionIndex,
832 Attribute::AlwaysInline)),
833 "Attributes 'noinline and alwaysinline' are incompatible!", V);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000834}
835
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000836bool Verifier::VerifyAttributeCount(AttributeSet Attrs, unsigned Params) {
Bill Wendling254aed52013-01-30 06:54:41 +0000837 if (Attrs.getNumSlots() == 0)
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000838 return true;
Nick Lewycky29ef6592009-09-07 20:44:51 +0000839
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000840 unsigned LastSlot = Attrs.getNumSlots() - 1;
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000841 unsigned LastIndex = Attrs.getSlotIndex(LastSlot);
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000842 if (LastIndex <= Params
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000843 || (LastIndex == AttributeSet::FunctionIndex
844 && (LastSlot == 0 || Attrs.getSlotIndex(LastSlot - 1) <= Params)))
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000845 return true;
Bill Wendlinge1f95db2013-01-25 21:30:53 +0000846
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000847 return false;
848}
Nick Lewycky29ef6592009-09-07 20:44:51 +0000849
Chris Lattnerd231fc32002-04-18 20:37:37 +0000850// visitFunction - Verify that a function is ok.
Chris Lattner44d5bd92002-02-20 17:55:43 +0000851//
Chris Lattner24e845f2002-06-25 15:56:27 +0000852void Verifier::visitFunction(Function &F) {
Chris Lattner37c121a2005-05-08 22:27:09 +0000853 // Check function arguments.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000854 FunctionType *FT = F.getFunctionType();
Chris Lattner453eed12007-08-18 06:13:19 +0000855 unsigned NumArgs = F.arg_size();
Chris Lattnerea249242002-04-13 22:48:46 +0000856
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000857 Assert1(Context == &F.getContext(),
858 "Function context does not match Module context!", &F);
859
Chris Lattner26d054d2009-08-05 05:21:07 +0000860 Assert1(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
Chris Lattner69da5cf2002-10-13 20:57:00 +0000861 Assert2(FT->getNumParams() == NumArgs,
Chris Lattnerea249242002-04-13 22:48:46 +0000862 "# formal arguments must match # of arguments for function type!",
Chris Lattner24e845f2002-06-25 15:56:27 +0000863 &F, FT);
Chris Lattnerc282f5a2003-11-21 22:32:23 +0000864 Assert1(F.getReturnType()->isFirstClassType() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000865 F.getReturnType()->isVoidTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000866 F.getReturnType()->isStructTy(),
Chris Lattnerc282f5a2003-11-21 22:32:23 +0000867 "Functions cannot return aggregate values!", &F);
Chris Lattnerea249242002-04-13 22:48:46 +0000868
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000869 Assert1(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
Devang Patel41e23972008-03-03 21:46:28 +0000870 "Invalid struct return type!", &F);
871
Bill Wendlingbb1b63c2013-04-18 20:15:25 +0000872 AttributeSet Attrs = F.getAttributes();
Duncan Sands623a3892008-01-11 22:36:48 +0000873
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000874 Assert1(VerifyAttributeCount(Attrs, FT->getNumParams()),
Bill Wendling034b94b2012-12-19 07:18:57 +0000875 "Attribute after last parameter!", &F);
Duncan Sands623a3892008-01-11 22:36:48 +0000876
Duncan Sandsd9d70392007-12-21 19:19:01 +0000877 // Check function attributes.
Duncan Sandscfad1b42008-01-12 16:42:01 +0000878 VerifyFunctionAttrs(FT, Attrs, &F);
Duncan Sandsfdef00f2007-07-27 15:09:54 +0000879
Chris Lattner80105dd2006-05-19 21:25:17 +0000880 // Check that this function meets the restrictions on this calling convention.
881 switch (F.getCallingConv()) {
882 default:
883 break;
884 case CallingConv::C:
885 break;
Chris Lattner80105dd2006-05-19 21:25:17 +0000886 case CallingConv::Fast:
887 case CallingConv::Cold:
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000888 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +0000889 case CallingConv::X86_ThisCall:
Elena Demikhovsky35752222012-10-24 14:46:16 +0000890 case CallingConv::Intel_OCL_BI:
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000891 case CallingConv::PTX_Kernel:
892 case CallingConv::PTX_Device:
Chris Lattner80105dd2006-05-19 21:25:17 +0000893 Assert1(!F.isVarArg(),
894 "Varargs functions must have C calling conventions!", &F);
895 break;
896 }
Nick Lewycky29ef6592009-09-07 20:44:51 +0000897
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000898 bool isLLVMdotName = F.getName().size() >= 5 &&
899 F.getName().substr(0, 5) == "llvm.";
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000900
Chris Lattnerea249242002-04-13 22:48:46 +0000901 // Check that the argument values match the function type for this function...
Chris Lattner69da5cf2002-10-13 20:57:00 +0000902 unsigned i = 0;
Chris Lattnere3cbe032006-12-16 02:25:35 +0000903 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
904 I != E; ++I, ++i) {
Chris Lattner69da5cf2002-10-13 20:57:00 +0000905 Assert2(I->getType() == FT->getParamType(i),
906 "Argument value does not match function argument type!",
907 I, FT->getParamType(i));
Misha Brukmanfd939082005-04-21 23:48:37 +0000908 Assert1(I->getType()->isFirstClassType(),
Dan Gohman9f50eee2008-08-27 14:44:57 +0000909 "Function arguments must have first-class types!", I);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000910 if (!isLLVMdotName)
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000911 Assert2(!I->getType()->isMetadataTy(),
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000912 "Function takes metadata but isn't an intrinsic", I, &F);
Dan Gohman9f50eee2008-08-27 14:44:57 +0000913 }
Chris Lattnerea249242002-04-13 22:48:46 +0000914
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000915 if (F.isMaterializable()) {
916 // Function has a body somewhere we can't see.
917 } else if (F.isDeclaration()) {
Chris Lattner6693da02007-09-19 17:14:45 +0000918 Assert1(F.hasExternalLinkage() || F.hasDLLImportLinkage() ||
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000919 F.hasExternalWeakLinkage(),
Chris Lattner6693da02007-09-19 17:14:45 +0000920 "invalid linkage type for function declaration", &F);
921 } else {
Chris Lattner4d17caa2006-12-13 04:45:46 +0000922 // Verify that this function (which has a body) is not named "llvm.*". It
923 // is not legal to define intrinsics.
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000924 Assert1(!isLLVMdotName, "llvm intrinsics cannot be defined!", &F);
Chris Lattner4d17caa2006-12-13 04:45:46 +0000925
Chris Lattner69da5cf2002-10-13 20:57:00 +0000926 // Check the entry node
Chris Lattner02a3be02003-09-20 14:39:18 +0000927 BasicBlock *Entry = &F.getEntryBlock();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000928 Assert1(pred_begin(Entry) == pred_end(Entry),
929 "Entry block to function must not have predecessors!", Entry);
Chris Lattner660a4f32009-11-01 04:08:01 +0000930
931 // The address of the entry block cannot be taken, unless it is dead.
932 if (Entry->hasAddressTaken()) {
Chris Lattner4a7642e2009-11-01 18:11:50 +0000933 Assert1(!BlockAddress::get(Entry)->isConstantUsed(),
Chris Lattner660a4f32009-11-01 04:08:01 +0000934 "blockaddress may not be used with the entry block!", Entry);
935 }
Chris Lattner69da5cf2002-10-13 20:57:00 +0000936 }
Gabor Greifc9f75002010-03-24 13:21:49 +0000937
Chris Lattner13202de2009-09-11 17:05:29 +0000938 // If this function is actually an intrinsic, verify that it is only used in
939 // direct call/invokes, never having its "address taken".
940 if (F.getIntrinsicID()) {
Gabor Greifc9f75002010-03-24 13:21:49 +0000941 const User *U;
942 if (F.hasAddressTaken(&U))
Chris Lattner13202de2009-09-11 17:05:29 +0000943 Assert1(0, "Invalid user of intrinsic instruction!", U);
Chris Lattner13202de2009-09-11 17:05:29 +0000944 }
Chris Lattner44d5bd92002-02-20 17:55:43 +0000945}
946
Chris Lattnerd231fc32002-04-18 20:37:37 +0000947// verifyBasicBlock - Verify that a basic block is well formed...
948//
Chris Lattner24e845f2002-06-25 15:56:27 +0000949void Verifier::visitBasicBlock(BasicBlock &BB) {
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000950 InstsInThisBlock.clear();
951
Alkis Evlogimenos4f4cf992004-12-04 02:30:42 +0000952 // Ensure that basic blocks have terminators!
953 Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
954
Chris Lattnerbede31f2003-10-05 17:44:18 +0000955 // Check constraints that this basic block imposes on all of the PHI nodes in
956 // it.
957 if (isa<PHINode>(BB.front())) {
Chris Lattnerf8edb622007-02-10 08:33:11 +0000958 SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB));
959 SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
Chris Lattnerbede31f2003-10-05 17:44:18 +0000960 std::sort(Preds.begin(), Preds.end());
Misha Brukmanfd939082005-04-21 23:48:37 +0000961 PHINode *PN;
Chris Lattnerc70a5092004-06-05 17:44:48 +0000962 for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) {
Chris Lattnerbede31f2003-10-05 17:44:18 +0000963 // Ensure that PHI nodes have at least one entry!
964 Assert1(PN->getNumIncomingValues() != 0,
965 "PHI nodes must have at least one entry. If the block is dead, "
966 "the PHI should be removed!", PN);
Brian Gaeke2fea9ad2004-05-17 21:15:18 +0000967 Assert1(PN->getNumIncomingValues() == Preds.size(),
968 "PHINode should have one entry for each predecessor of its "
969 "parent basic block!", PN);
Misha Brukmanfd939082005-04-21 23:48:37 +0000970
Chris Lattnerbede31f2003-10-05 17:44:18 +0000971 // Get and sort all incoming values in the PHI node...
Chris Lattnerf8edb622007-02-10 08:33:11 +0000972 Values.clear();
Chris Lattnerbede31f2003-10-05 17:44:18 +0000973 Values.reserve(PN->getNumIncomingValues());
974 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
975 Values.push_back(std::make_pair(PN->getIncomingBlock(i),
976 PN->getIncomingValue(i)));
977 std::sort(Values.begin(), Values.end());
Misha Brukmanfd939082005-04-21 23:48:37 +0000978
Chris Lattnerbede31f2003-10-05 17:44:18 +0000979 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
980 // Check to make sure that if there is more than one entry for a
981 // particular basic block in this PHI node, that the incoming values are
982 // all identical.
983 //
984 Assert4(i == 0 || Values[i].first != Values[i-1].first ||
985 Values[i].second == Values[i-1].second,
986 "PHI node has multiple entries for the same basic block with "
987 "different incoming values!", PN, Values[i].first,
988 Values[i].second, Values[i-1].second);
Misha Brukmanfd939082005-04-21 23:48:37 +0000989
Chris Lattnerbede31f2003-10-05 17:44:18 +0000990 // Check to make sure that the predecessors and PHI node entries are
991 // matched up.
992 Assert3(Values[i].first == Preds[i],
993 "PHI node entries do not match predecessors!", PN,
Misha Brukmanfd939082005-04-21 23:48:37 +0000994 Values[i].first, Preds[i]);
Chris Lattnerbede31f2003-10-05 17:44:18 +0000995 }
996 }
997 }
Chris Lattner24e845f2002-06-25 15:56:27 +0000998}
Chris Lattneracd3cae2002-03-15 20:25:09 +0000999
Chris Lattner24e845f2002-06-25 15:56:27 +00001000void Verifier::visitTerminatorInst(TerminatorInst &I) {
1001 // Ensure that terminators only exist at the end of the basic block.
1002 Assert1(&I == I.getParent()->getTerminator(),
1003 "Terminator found in the middle of a basic block!", I.getParent());
Chris Lattner3535c9b2002-07-18 00:13:42 +00001004 visitInstruction(I);
Chris Lattner24e845f2002-06-25 15:56:27 +00001005}
1006
Nick Lewycky6a7cb632010-02-15 22:09:09 +00001007void Verifier::visitBranchInst(BranchInst &BI) {
1008 if (BI.isConditional()) {
1009 Assert2(BI.getCondition()->getType()->isIntegerTy(1),
1010 "Branch condition is not 'i1' type!", &BI, BI.getCondition());
1011 }
1012 visitTerminatorInst(BI);
1013}
1014
Chris Lattner24e845f2002-06-25 15:56:27 +00001015void Verifier::visitReturnInst(ReturnInst &RI) {
1016 Function *F = RI.getParent()->getParent();
Devang Patel57ef4f42008-02-23 00:35:18 +00001017 unsigned N = RI.getNumOperands();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001018 if (F->getReturnType()->isVoidTy())
Chris Lattner80b8f5d2008-04-23 20:33:41 +00001019 Assert2(N == 0,
Nick Lewycky70c44f02008-11-15 17:50:47 +00001020 "Found return instr that returns non-void in Function of void "
Alkis Evlogimenos8b42b432004-12-04 01:25:06 +00001021 "return type!", &RI, F->getReturnType());
Jay Foad3e2f74e2011-04-04 07:44:02 +00001022 else
1023 Assert2(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
1024 "Function return type does not match operand "
1025 "type of return inst!", &RI, F->getReturnType());
Nick Lewycky29ef6592009-09-07 20:44:51 +00001026
Misha Brukman5560c9d2003-08-18 14:43:39 +00001027 // Check to make sure that the return value has necessary properties for
Chris Lattner24e845f2002-06-25 15:56:27 +00001028 // terminators...
1029 visitTerminatorInst(RI);
Chris Lattner44d5bd92002-02-20 17:55:43 +00001030}
1031
Chris Lattner0f9e9d02004-05-21 16:47:21 +00001032void Verifier::visitSwitchInst(SwitchInst &SI) {
1033 // Check to make sure that all of the constants in the switch instruction
1034 // have the same type as the switched-on value.
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001035 Type *SwitchTy = SI.getCondition()->getType();
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +00001036 IntegerType *IntTy = cast<IntegerType>(SwitchTy);
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00001037 IntegersSubsetToBB Mapping;
1038 std::map<IntegersSubset::Range, unsigned> RangeSetMap;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +00001039 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); i != e; ++i) {
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00001040 IntegersSubset CaseRanges = i.getCaseValueEx();
1041 for (unsigned ri = 0, rie = CaseRanges.getNumItems(); ri < rie; ++ri) {
1042 IntegersSubset::Range r = CaseRanges.getItem(ri);
Stepan Dyatkovskiy43eb31b2012-06-02 09:42:43 +00001043 Assert1(((const APInt&)r.getLow()).getBitWidth() == IntTy->getBitWidth(),
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001044 "Switch constants must all be same type as switch value!", &SI);
Stepan Dyatkovskiy43eb31b2012-06-02 09:42:43 +00001045 Assert1(((const APInt&)r.getHigh()).getBitWidth() == IntTy->getBitWidth(),
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001046 "Switch constants must all be same type as switch value!", &SI);
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00001047 Mapping.add(r);
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001048 RangeSetMap[r] = i.getCaseIndex();
1049 }
Chris Lattnerd682a602009-11-11 17:37:02 +00001050 }
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001051
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +00001052 IntegersSubsetToBB::RangeIterator errItem;
1053 if (!Mapping.verify(errItem)) {
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +00001054 unsigned CaseIndex = RangeSetMap[errItem->first];
1055 SwitchInst::CaseIt i(&SI, CaseIndex);
1056 Assert2(false, "Duplicate integer as switch case", &SI, i.getCaseValueEx());
1057 }
1058
Chris Lattner0f9e9d02004-05-21 16:47:21 +00001059 visitTerminatorInst(SI);
1060}
1061
Dan Gohman37680912010-08-02 23:08:33 +00001062void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
1063 Assert1(BI.getAddress()->getType()->isPointerTy(),
1064 "Indirectbr operand must have pointer type!", &BI);
1065 for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
1066 Assert1(BI.getDestination(i)->getType()->isLabelTy(),
1067 "Indirectbr destinations must all have pointer type!", &BI);
1068
1069 visitTerminatorInst(BI);
1070}
1071
Chris Lattner230c1a72004-03-12 05:54:31 +00001072void Verifier::visitSelectInst(SelectInst &SI) {
Chris Lattnerb76ec322008-12-29 00:12:50 +00001073 Assert1(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
1074 SI.getOperand(2)),
1075 "Invalid operands for select instruction!", &SI);
1076
Chris Lattner230c1a72004-03-12 05:54:31 +00001077 Assert1(SI.getTrueValue()->getType() == SI.getType(),
1078 "Select values must have same type as select instruction!", &SI);
Chris Lattner0030e6c2004-09-29 21:19:28 +00001079 visitInstruction(SI);
Chris Lattner230c1a72004-03-12 05:54:31 +00001080}
1081
Misha Brukmanab5c6002004-03-02 00:22:19 +00001082/// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
1083/// a pass, if any exist, it's an error.
1084///
Chris Lattner627079d2002-11-21 16:54:22 +00001085void Verifier::visitUserOp1(Instruction &I) {
Chris Lattner536a9d52006-03-31 04:46:47 +00001086 Assert1(0, "User-defined operators should not live outside of a pass!", &I);
Chris Lattner627079d2002-11-21 16:54:22 +00001087}
Chris Lattnerd231fc32002-04-18 20:37:37 +00001088
Reid Spencer3da59db2006-11-27 01:05:10 +00001089void Verifier::visitTruncInst(TruncInst &I) {
1090 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001091 Type *SrcTy = I.getOperand(0)->getType();
1092 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001093
1094 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001095 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1096 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001097
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001098 Assert1(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
1099 Assert1(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001100 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001101 "trunc source and destination must both be a vector or neither", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001102 Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I);
1103
1104 visitInstruction(I);
1105}
1106
1107void Verifier::visitZExtInst(ZExtInst &I) {
1108 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001109 Type *SrcTy = I.getOperand(0)->getType();
1110 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001111
1112 // Get the size of the types in bits, we'll need this later
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001113 Assert1(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
1114 Assert1(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001115 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001116 "zext source and destination must both be a vector or neither", &I);
Dan Gohman6de29f82009-06-15 22:12:54 +00001117 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1118 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001119
Reid Spencer3da59db2006-11-27 01:05:10 +00001120 Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I);
1121
1122 visitInstruction(I);
1123}
1124
1125void Verifier::visitSExtInst(SExtInst &I) {
1126 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001127 Type *SrcTy = I.getOperand(0)->getType();
1128 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001129
1130 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001131 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1132 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001133
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001134 Assert1(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
1135 Assert1(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001136 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001137 "sext source and destination must both be a vector or neither", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001138 Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I);
1139
1140 visitInstruction(I);
1141}
1142
1143void Verifier::visitFPTruncInst(FPTruncInst &I) {
1144 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001145 Type *SrcTy = I.getOperand(0)->getType();
1146 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001147 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001148 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1149 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001150
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001151 Assert1(SrcTy->isFPOrFPVectorTy(),"FPTrunc only operates on FP", &I);
1152 Assert1(DestTy->isFPOrFPVectorTy(),"FPTrunc only produces an FP", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001153 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001154 "fptrunc source and destination must both be a vector or neither",&I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001155 Assert1(SrcBitSize > DestBitSize,"DestTy too big for FPTrunc", &I);
1156
1157 visitInstruction(I);
1158}
1159
1160void Verifier::visitFPExtInst(FPExtInst &I) {
1161 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001162 Type *SrcTy = I.getOperand(0)->getType();
1163 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001164
1165 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001166 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1167 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001168
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001169 Assert1(SrcTy->isFPOrFPVectorTy(),"FPExt only operates on FP", &I);
1170 Assert1(DestTy->isFPOrFPVectorTy(),"FPExt only produces an FP", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001171 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001172 "fpext source and destination must both be a vector or neither", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001173 Assert1(SrcBitSize < DestBitSize,"DestTy too small for FPExt", &I);
1174
1175 visitInstruction(I);
1176}
1177
1178void Verifier::visitUIToFPInst(UIToFPInst &I) {
1179 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001180 Type *SrcTy = I.getOperand(0)->getType();
1181 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001182
Duncan Sands1df98592010-02-16 11:11:14 +00001183 bool SrcVec = SrcTy->isVectorTy();
1184 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001185
Chris Lattner58d74912008-03-12 17:45:29 +00001186 Assert1(SrcVec == DstVec,
1187 "UIToFP source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001188 Assert1(SrcTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001189 "UIToFP source must be integer or integer vector", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001190 Assert1(DestTy->isFPOrFPVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001191 "UIToFP result must be FP or FP vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001192
1193 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001194 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1195 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001196 "UIToFP source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001197
1198 visitInstruction(I);
1199}
1200
1201void Verifier::visitSIToFPInst(SIToFPInst &I) {
1202 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001203 Type *SrcTy = I.getOperand(0)->getType();
1204 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001205
Duncan Sands1df98592010-02-16 11:11:14 +00001206 bool SrcVec = SrcTy->isVectorTy();
1207 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001208
Chris Lattner58d74912008-03-12 17:45:29 +00001209 Assert1(SrcVec == DstVec,
1210 "SIToFP source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001211 Assert1(SrcTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001212 "SIToFP source must be integer or integer vector", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001213 Assert1(DestTy->isFPOrFPVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001214 "SIToFP result must be FP or FP vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001215
1216 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001217 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1218 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001219 "SIToFP source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001220
1221 visitInstruction(I);
1222}
1223
1224void Verifier::visitFPToUIInst(FPToUIInst &I) {
1225 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001226 Type *SrcTy = I.getOperand(0)->getType();
1227 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001228
Duncan Sands1df98592010-02-16 11:11:14 +00001229 bool SrcVec = SrcTy->isVectorTy();
1230 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001231
Chris Lattner58d74912008-03-12 17:45:29 +00001232 Assert1(SrcVec == DstVec,
1233 "FPToUI source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001234 Assert1(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector",
1235 &I);
1236 Assert1(DestTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001237 "FPToUI result must be integer or integer vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001238
1239 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001240 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1241 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001242 "FPToUI source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001243
1244 visitInstruction(I);
1245}
1246
1247void Verifier::visitFPToSIInst(FPToSIInst &I) {
1248 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001249 Type *SrcTy = I.getOperand(0)->getType();
1250 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001251
Duncan Sands1df98592010-02-16 11:11:14 +00001252 bool SrcVec = SrcTy->isVectorTy();
1253 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001254
Chris Lattner58d74912008-03-12 17:45:29 +00001255 Assert1(SrcVec == DstVec,
1256 "FPToSI source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001257 Assert1(SrcTy->isFPOrFPVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001258 "FPToSI source must be FP or FP vector", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001259 Assert1(DestTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001260 "FPToSI result must be integer or integer vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001261
1262 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001263 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1264 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001265 "FPToSI source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001266
1267 visitInstruction(I);
1268}
1269
1270void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
1271 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001272 Type *SrcTy = I.getOperand(0)->getType();
1273 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001274
Nadav Rotem16087692011-12-05 06:29:09 +00001275 Assert1(SrcTy->getScalarType()->isPointerTy(),
1276 "PtrToInt source must be pointer", &I);
1277 Assert1(DestTy->getScalarType()->isIntegerTy(),
1278 "PtrToInt result must be integral", &I);
1279 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
1280 "PtrToInt type mismatch", &I);
1281
1282 if (SrcTy->isVectorTy()) {
1283 VectorType *VSrc = dyn_cast<VectorType>(SrcTy);
1284 VectorType *VDest = dyn_cast<VectorType>(DestTy);
1285 Assert1(VSrc->getNumElements() == VDest->getNumElements(),
1286 "PtrToInt Vector width mismatch", &I);
1287 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001288
1289 visitInstruction(I);
1290}
1291
1292void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
1293 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001294 Type *SrcTy = I.getOperand(0)->getType();
1295 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001296
Nadav Rotem16087692011-12-05 06:29:09 +00001297 Assert1(SrcTy->getScalarType()->isIntegerTy(),
1298 "IntToPtr source must be an integral", &I);
1299 Assert1(DestTy->getScalarType()->isPointerTy(),
1300 "IntToPtr result must be a pointer",&I);
1301 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
1302 "IntToPtr type mismatch", &I);
1303 if (SrcTy->isVectorTy()) {
1304 VectorType *VSrc = dyn_cast<VectorType>(SrcTy);
1305 VectorType *VDest = dyn_cast<VectorType>(DestTy);
1306 Assert1(VSrc->getNumElements() == VDest->getNumElements(),
1307 "IntToPtr Vector width mismatch", &I);
1308 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001309 visitInstruction(I);
1310}
1311
1312void Verifier::visitBitCastInst(BitCastInst &I) {
1313 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001314 Type *SrcTy = I.getOperand(0)->getType();
1315 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001316
1317 // Get the size of the types in bits, we'll need this later
1318 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1319 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
1320
1321 // BitCast implies a no-op cast of type only. No bits change.
1322 // However, you can't cast pointers to anything but pointers.
Nick Lewyckyc2de3dd2012-08-15 02:37:07 +00001323 Assert1(SrcTy->isPointerTy() == DestTy->isPointerTy(),
Reid Spencer3da59db2006-11-27 01:05:10 +00001324 "Bitcast requires both operands to be pointer or neither", &I);
Nate Begeman55a961f2009-07-30 02:00:06 +00001325 Assert1(SrcBitSize == DestBitSize, "Bitcast requires types of same width",&I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001326
Dan Gohman500233a2008-09-08 16:45:59 +00001327 // Disallow aggregates.
1328 Assert1(!SrcTy->isAggregateType(),
1329 "Bitcast operand must not be aggregate", &I);
1330 Assert1(!DestTy->isAggregateType(),
1331 "Bitcast type must not be aggregate", &I);
1332
Reid Spencer3da59db2006-11-27 01:05:10 +00001333 visitInstruction(I);
1334}
1335
Misha Brukmanab5c6002004-03-02 00:22:19 +00001336/// visitPHINode - Ensure that a PHI node is well formed.
1337///
Chris Lattner24e845f2002-06-25 15:56:27 +00001338void Verifier::visitPHINode(PHINode &PN) {
1339 // Ensure that the PHI nodes are all grouped together at the top of the block.
1340 // This can be tested by checking whether the instruction before this is
Misha Brukman6b634522003-10-10 17:54:14 +00001341 // either nonexistent (because this is begin()) or is a PHI node. If not,
Chris Lattner24e845f2002-06-25 15:56:27 +00001342 // then there is some other instruction before a PHI.
Chris Lattner4d8c16f2007-04-17 17:36:12 +00001343 Assert2(&PN == &PN.getParent()->front() ||
1344 isa<PHINode>(--BasicBlock::iterator(&PN)),
Chris Lattner24e845f2002-06-25 15:56:27 +00001345 "PHI nodes not grouped at top of basic block!",
1346 &PN, PN.getParent());
1347
Nick Lewycky49072472009-09-08 01:23:52 +00001348 // Check that all of the values of the PHI node have the same type as the
1349 // result, and that the incoming blocks are really basic blocks.
1350 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Chris Lattner579de712003-11-12 07:13:37 +00001351 Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
1352 "PHI node operands are not the same type as the result!", &PN);
Nick Lewycky49072472009-09-08 01:23:52 +00001353 }
Chris Lattner579de712003-11-12 07:13:37 +00001354
Chris Lattnerbede31f2003-10-05 17:44:18 +00001355 // All other PHI node constraints are checked in the visitBasicBlock method.
Chris Lattnerd231fc32002-04-18 20:37:37 +00001356
1357 visitInstruction(PN);
1358}
1359
Duncan Sandsd9d70392007-12-21 19:19:01 +00001360void Verifier::VerifyCallSite(CallSite CS) {
1361 Instruction *I = CS.getInstruction();
1362
Duncan Sands1df98592010-02-16 11:11:14 +00001363 Assert1(CS.getCalledValue()->getType()->isPointerTy(),
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001364 "Called function must be a pointer!", I);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001365 PointerType *FPTy = cast<PointerType>(CS.getCalledValue()->getType());
Chris Lattner56732fb2002-05-08 19:49:50 +00001366
Duncan Sands1df98592010-02-16 11:11:14 +00001367 Assert1(FPTy->getElementType()->isFunctionTy(),
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001368 "Called function is not pointer to function type!", I);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001369 FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
Chris Lattner56732fb2002-05-08 19:49:50 +00001370
1371 // Verify that the correct number of arguments are being passed
1372 if (FTy->isVarArg())
Duncan Sandsd9d70392007-12-21 19:19:01 +00001373 Assert1(CS.arg_size() >= FTy->getNumParams(),
1374 "Called function requires more parameters than were provided!",I);
Chris Lattner56732fb2002-05-08 19:49:50 +00001375 else
Duncan Sandsd9d70392007-12-21 19:19:01 +00001376 Assert1(CS.arg_size() == FTy->getNumParams(),
1377 "Incorrect number of arguments passed to called function!", I);
Chris Lattner56732fb2002-05-08 19:49:50 +00001378
Chris Lattner775aba22010-05-10 20:58:42 +00001379 // Verify that all arguments to the call match the function type.
Chris Lattner56732fb2002-05-08 19:49:50 +00001380 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Duncan Sandsd9d70392007-12-21 19:19:01 +00001381 Assert3(CS.getArgument(i)->getType() == FTy->getParamType(i),
Chris Lattner56732fb2002-05-08 19:49:50 +00001382 "Call parameter type does not match function signature!",
Duncan Sandsd9d70392007-12-21 19:19:01 +00001383 CS.getArgument(i), FTy->getParamType(i), I);
1384
Bill Wendlingbb1b63c2013-04-18 20:15:25 +00001385 AttributeSet Attrs = CS.getAttributes();
Duncan Sands623a3892008-01-11 22:36:48 +00001386
Devang Pateld9b4a5f2008-09-23 22:35:17 +00001387 Assert1(VerifyAttributeCount(Attrs, CS.arg_size()),
Bill Wendling034b94b2012-12-19 07:18:57 +00001388 "Attribute after last parameter!", I);
Duncan Sands623a3892008-01-11 22:36:48 +00001389
Duncan Sandsd9d70392007-12-21 19:19:01 +00001390 // Verify call attributes.
Duncan Sandscfad1b42008-01-12 16:42:01 +00001391 VerifyFunctionAttrs(FTy, Attrs, I);
Duncan Sands623a3892008-01-11 22:36:48 +00001392
Stephen Lin456ca042013-04-20 05:14:40 +00001393 if (FTy->isVarArg()) {
1394 // FIXME? is 'nest' even legal here?
1395 bool SawNest = false;
1396 bool SawReturned = false;
1397
1398 for (unsigned Idx = 1; Idx < 1 + FTy->getNumParams(); ++Idx) {
1399 if (Attrs.hasAttribute(Idx, Attribute::Nest))
1400 SawNest = true;
1401 if (Attrs.hasAttribute(Idx, Attribute::Returned))
1402 SawReturned = true;
1403 }
1404
Duncan Sands623a3892008-01-11 22:36:48 +00001405 // Check attributes on the varargs part.
1406 for (unsigned Idx = 1 + FTy->getNumParams(); Idx <= CS.arg_size(); ++Idx) {
Stephen Lin456ca042013-04-20 05:14:40 +00001407 Type *Ty = CS.getArgument(Idx-1)->getType();
1408 VerifyParameterAttrs(Attrs, Idx, Ty, false, I);
1409
1410 if (Attrs.hasAttribute(Idx, Attribute::Nest)) {
1411 Assert1(!SawNest, "More than one parameter has attribute nest!", I);
1412 SawNest = true;
1413 }
1414
1415 if (Attrs.hasAttribute(Idx, Attribute::Returned)) {
1416 Assert1(!SawReturned, "More than one parameter has attribute returned!",
1417 I);
1418 Assert1(Ty->canLosslesslyBitCastTo(FTy->getReturnType()),
1419 "Incompatible argument and return types for 'returned' "
1420 "attribute", I);
1421 SawReturned = true;
1422 }
Duncan Sandscfad1b42008-01-12 16:42:01 +00001423
Bill Wendlingbed80592013-01-21 23:03:18 +00001424 Assert1(!Attrs.hasAttribute(Idx, Attribute::StructRet),
Bill Wendling15c37892012-10-09 09:33:01 +00001425 "Attribute 'sret' cannot be used for vararg call arguments!", I);
Duncan Sands623a3892008-01-11 22:36:48 +00001426 }
Stephen Lin456ca042013-04-20 05:14:40 +00001427 }
Duncan Sandsd9d70392007-12-21 19:19:01 +00001428
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001429 // Verify that there's no metadata unless it's a direct call to an intrinsic.
Chris Lattner1afcace2011-07-09 17:41:24 +00001430 if (CS.getCalledFunction() == 0 ||
Chris Lattner775aba22010-05-10 20:58:42 +00001431 !CS.getCalledFunction()->getName().startswith("llvm.")) {
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001432 for (FunctionType::param_iterator PI = FTy->param_begin(),
1433 PE = FTy->param_end(); PI != PE; ++PI)
Chris Lattner1afcace2011-07-09 17:41:24 +00001434 Assert1(!(*PI)->isMetadataTy(),
Owen Anderson1d0be152009-08-13 21:58:54 +00001435 "Function has metadata parameter but isn't an intrinsic", I);
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001436 }
1437
Duncan Sandsd9d70392007-12-21 19:19:01 +00001438 visitInstruction(*I);
1439}
1440
1441void Verifier::visitCallInst(CallInst &CI) {
1442 VerifyCallSite(&CI);
Chris Lattner3535c9b2002-07-18 00:13:42 +00001443
Dale Johannesen49de9822009-02-05 01:49:45 +00001444 if (Function *F = CI.getCalledFunction())
Brian Gaeked0fde302003-11-11 22:41:34 +00001445 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
Chris Lattnerdd035d12003-05-08 03:47:33 +00001446 visitIntrinsicFunctionCall(ID, CI);
Duncan Sandsd9d70392007-12-21 19:19:01 +00001447}
1448
1449void Verifier::visitInvokeInst(InvokeInst &II) {
1450 VerifyCallSite(&II);
Bill Wendlingcccfd192011-09-21 22:57:02 +00001451
1452 // Verify that there is a landingpad instruction as the first non-PHI
1453 // instruction of the 'unwind' destination.
1454 Assert1(II.getUnwindDest()->isLandingPad(),
1455 "The unwind destination does not have a landingpad instruction!",&II);
1456
Dan Gohmandacfc5d2010-08-02 23:09:14 +00001457 visitTerminatorInst(II);
Chris Lattnerefdd0a22002-04-18 22:11:52 +00001458}
Chris Lattnerd231fc32002-04-18 20:37:37 +00001459
Misha Brukmanab5c6002004-03-02 00:22:19 +00001460/// visitBinaryOperator - Check that both arguments to the binary operator are
1461/// of the same type!
1462///
Chris Lattner24e845f2002-06-25 15:56:27 +00001463void Verifier::visitBinaryOperator(BinaryOperator &B) {
Chris Lattner1a143ae2002-09-09 20:26:04 +00001464 Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
1465 "Both operands to a binary operator are not of the same type!", &B);
Chris Lattnerd231fc32002-04-18 20:37:37 +00001466
Reid Spencer832254e2007-02-02 02:16:23 +00001467 switch (B.getOpcode()) {
Dan Gohman11cc35d2009-06-05 16:10:00 +00001468 // Check that integer arithmetic operators are only used with
1469 // integral operands.
1470 case Instruction::Add:
1471 case Instruction::Sub:
1472 case Instruction::Mul:
1473 case Instruction::SDiv:
1474 case Instruction::UDiv:
1475 case Instruction::SRem:
1476 case Instruction::URem:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001477 Assert1(B.getType()->isIntOrIntVectorTy(),
Dan Gohman11cc35d2009-06-05 16:10:00 +00001478 "Integer arithmetic operators only work with integral types!", &B);
1479 Assert1(B.getType() == B.getOperand(0)->getType(),
1480 "Integer arithmetic operators must have same type "
1481 "for operands and result!", &B);
1482 break;
1483 // Check that floating-point arithmetic operators are only used with
1484 // floating-point operands.
1485 case Instruction::FAdd:
1486 case Instruction::FSub:
1487 case Instruction::FMul:
1488 case Instruction::FDiv:
1489 case Instruction::FRem:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001490 Assert1(B.getType()->isFPOrFPVectorTy(),
Dan Gohman11cc35d2009-06-05 16:10:00 +00001491 "Floating-point arithmetic operators only work with "
Dan Gohmanf38fd692009-06-05 18:34:16 +00001492 "floating-point types!", &B);
Dan Gohman11cc35d2009-06-05 16:10:00 +00001493 Assert1(B.getType() == B.getOperand(0)->getType(),
1494 "Floating-point arithmetic operators must have same type "
1495 "for operands and result!", &B);
1496 break;
Chris Lattner1a143ae2002-09-09 20:26:04 +00001497 // Check that logical operators are only used with integral operands.
Reid Spencer832254e2007-02-02 02:16:23 +00001498 case Instruction::And:
1499 case Instruction::Or:
1500 case Instruction::Xor:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001501 Assert1(B.getType()->isIntOrIntVectorTy(),
Chris Lattner1a143ae2002-09-09 20:26:04 +00001502 "Logical operators only work with integral types!", &B);
1503 Assert1(B.getType() == B.getOperand(0)->getType(),
1504 "Logical operators must have same type for operands and result!",
1505 &B);
Reid Spencer832254e2007-02-02 02:16:23 +00001506 break;
1507 case Instruction::Shl:
1508 case Instruction::LShr:
1509 case Instruction::AShr:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001510 Assert1(B.getType()->isIntOrIntVectorTy(),
Nate Begeman5bc1ea02008-07-29 15:49:41 +00001511 "Shifts only work with integral types!", &B);
Reid Spencer832254e2007-02-02 02:16:23 +00001512 Assert1(B.getType() == B.getOperand(0)->getType(),
1513 "Shift return type must be same as operands!", &B);
Reid Spencer832254e2007-02-02 02:16:23 +00001514 break;
Dan Gohman11cc35d2009-06-05 16:10:00 +00001515 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001516 llvm_unreachable("Unknown BinaryOperator opcode!");
Chris Lattner1a143ae2002-09-09 20:26:04 +00001517 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001518
Chris Lattnerd231fc32002-04-18 20:37:37 +00001519 visitInstruction(B);
1520}
1521
Nick Lewycky55e97d42010-08-22 23:45:14 +00001522void Verifier::visitICmpInst(ICmpInst &IC) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00001523 // Check that the operands are the same type
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001524 Type *Op0Ty = IC.getOperand(0)->getType();
1525 Type *Op1Ty = IC.getOperand(1)->getType();
Reid Spencer45fb3f32006-11-20 01:22:35 +00001526 Assert1(Op0Ty == Op1Ty,
1527 "Both operands to ICmp instruction are not of the same type!", &IC);
1528 // Check that the operands are the right type
Nadav Rotem16087692011-12-05 06:29:09 +00001529 Assert1(Op0Ty->isIntOrIntVectorTy() || Op0Ty->getScalarType()->isPointerTy(),
Reid Spencer45fb3f32006-11-20 01:22:35 +00001530 "Invalid operand types for ICmp instruction", &IC);
Nick Lewycky55e97d42010-08-22 23:45:14 +00001531 // Check that the predicate is valid.
1532 Assert1(IC.getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1533 IC.getPredicate() <= CmpInst::LAST_ICMP_PREDICATE,
1534 "Invalid predicate in ICmp instruction!", &IC);
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001535
Reid Spencer45fb3f32006-11-20 01:22:35 +00001536 visitInstruction(IC);
1537}
1538
Nick Lewycky55e97d42010-08-22 23:45:14 +00001539void Verifier::visitFCmpInst(FCmpInst &FC) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00001540 // Check that the operands are the same type
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001541 Type *Op0Ty = FC.getOperand(0)->getType();
1542 Type *Op1Ty = FC.getOperand(1)->getType();
Reid Spencer45fb3f32006-11-20 01:22:35 +00001543 Assert1(Op0Ty == Op1Ty,
1544 "Both operands to FCmp instruction are not of the same type!", &FC);
1545 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001546 Assert1(Op0Ty->isFPOrFPVectorTy(),
Reid Spencer45fb3f32006-11-20 01:22:35 +00001547 "Invalid operand types for FCmp instruction", &FC);
Nick Lewycky55e97d42010-08-22 23:45:14 +00001548 // Check that the predicate is valid.
1549 Assert1(FC.getPredicate() >= CmpInst::FIRST_FCMP_PREDICATE &&
1550 FC.getPredicate() <= CmpInst::LAST_FCMP_PREDICATE,
1551 "Invalid predicate in FCmp instruction!", &FC);
1552
Reid Spencer45fb3f32006-11-20 01:22:35 +00001553 visitInstruction(FC);
1554}
1555
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001556void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
Chris Lattner1cbe05b2006-04-08 04:07:52 +00001557 Assert1(ExtractElementInst::isValidOperands(EI.getOperand(0),
1558 EI.getOperand(1)),
1559 "Invalid extractelement operands!", &EI);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001560 visitInstruction(EI);
1561}
1562
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001563void Verifier::visitInsertElementInst(InsertElementInst &IE) {
Chris Lattner1cbe05b2006-04-08 04:07:52 +00001564 Assert1(InsertElementInst::isValidOperands(IE.getOperand(0),
1565 IE.getOperand(1),
1566 IE.getOperand(2)),
1567 "Invalid insertelement operands!", &IE);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001568 visitInstruction(IE);
1569}
1570
Chris Lattner00f10232006-04-08 01:18:18 +00001571void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
1572 Assert1(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
1573 SV.getOperand(2)),
1574 "Invalid shufflevector operands!", &SV);
Chris Lattner00f10232006-04-08 01:18:18 +00001575 visitInstruction(SV);
1576}
1577
Chris Lattner24e845f2002-06-25 15:56:27 +00001578void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Duncan Sandsb95d1ff2012-02-03 17:28:51 +00001579 Type *TargetTy = GEP.getPointerOperandType()->getScalarType();
Nadav Rotem16087692011-12-05 06:29:09 +00001580
Duncan Sandsb95d1ff2012-02-03 17:28:51 +00001581 Assert1(isa<PointerType>(TargetTy),
Bill Wendling6ebddd22012-10-17 23:56:05 +00001582 "GEP base pointer is not a vector or a vector of pointers", &GEP);
Nadav Rotem16087692011-12-05 06:29:09 +00001583 Assert1(cast<PointerType>(TargetTy)->getElementType()->isSized(),
Chris Lattner4cea5ba2011-07-29 20:32:28 +00001584 "GEP into unsized type!", &GEP);
Duncan Sands2333e292012-11-13 12:59:33 +00001585 Assert1(GEP.getPointerOperandType()->isVectorTy() ==
1586 GEP.getType()->isVectorTy(), "Vector GEP must return a vector value",
1587 &GEP);
Nadav Rotem16087692011-12-05 06:29:09 +00001588
Chris Lattner8552fae2007-02-10 08:30:29 +00001589 SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end());
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001590 Type *ElTy =
Nadav Rotem16087692011-12-05 06:29:09 +00001591 GetElementPtrInst::getIndexedType(GEP.getPointerOperandType(), Idxs);
Chris Lattner24e845f2002-06-25 15:56:27 +00001592 Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
Nadav Rotem16087692011-12-05 06:29:09 +00001593
Duncan Sands2333e292012-11-13 12:59:33 +00001594 Assert2(GEP.getType()->getScalarType()->isPointerTy() &&
1595 cast<PointerType>(GEP.getType()->getScalarType())->getElementType()
1596 == ElTy, "GEP is not of right type for indices!", &GEP, ElTy);
1597
1598 if (GEP.getPointerOperandType()->isVectorTy()) {
1599 // Additional checks for vector GEPs.
1600 unsigned GepWidth = GEP.getPointerOperandType()->getVectorNumElements();
1601 Assert1(GepWidth == GEP.getType()->getVectorNumElements(),
1602 "Vector GEP result width doesn't match operand's", &GEP);
1603 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
1604 Type *IndexTy = Idxs[i]->getType();
1605 Assert1(IndexTy->isVectorTy(),
1606 "Vector GEP must have vector indices!", &GEP);
1607 unsigned IndexWidth = IndexTy->getVectorNumElements();
1608 Assert1(IndexWidth == GepWidth, "Invalid GEP index vector width", &GEP);
1609 }
Nadav Rotem16087692011-12-05 06:29:09 +00001610 }
Chris Lattnera00409e2002-04-24 19:12:21 +00001611 visitInstruction(GEP);
1612}
1613
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001614static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
1615 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
1616}
1617
Chris Lattner24e845f2002-06-25 15:56:27 +00001618void Verifier::visitLoadInst(LoadInst &LI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001619 PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
Nick Lewycky49072472009-09-08 01:23:52 +00001620 Assert1(PTy, "Load operand must be a pointer.", &LI);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001621 Type *ElTy = PTy->getElementType();
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001622 Assert2(ElTy == LI.getType(),
1623 "Load result type does not match pointer operand type!", &LI, ElTy);
Eli Friedman21006d42011-08-09 23:02:53 +00001624 if (LI.isAtomic()) {
1625 Assert1(LI.getOrdering() != Release && LI.getOrdering() != AcquireRelease,
1626 "Load cannot have Release ordering", &LI);
1627 Assert1(LI.getAlignment() != 0,
1628 "Atomic load must specify explicit alignment", &LI);
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001629 if (!ElTy->isPointerTy()) {
1630 Assert2(ElTy->isIntegerTy(),
1631 "atomic store operand must have integer type!",
1632 &LI, ElTy);
1633 unsigned Size = ElTy->getPrimitiveSizeInBits();
1634 Assert2(Size >= 8 && !(Size & (Size - 1)),
1635 "atomic store operand must be power-of-two byte-sized integer",
1636 &LI, ElTy);
1637 }
Eli Friedman21006d42011-08-09 23:02:53 +00001638 } else {
1639 Assert1(LI.getSynchScope() == CrossThread,
1640 "Non-atomic load cannot have SynchronizationScope specified", &LI);
1641 }
Rafael Espindola39dd3282012-03-24 00:14:51 +00001642
1643 if (MDNode *Range = LI.getMetadata(LLVMContext::MD_range)) {
1644 unsigned NumOperands = Range->getNumOperands();
1645 Assert1(NumOperands % 2 == 0, "Unfinished range!", Range);
1646 unsigned NumRanges = NumOperands / 2;
1647 Assert1(NumRanges >= 1, "It should have at least one range!", Range);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001648
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001649 ConstantRange LastRange(1); // Dummy initial value
Rafael Espindola39dd3282012-03-24 00:14:51 +00001650 for (unsigned i = 0; i < NumRanges; ++i) {
1651 ConstantInt *Low = dyn_cast<ConstantInt>(Range->getOperand(2*i));
1652 Assert1(Low, "The lower limit must be an integer!", Low);
1653 ConstantInt *High = dyn_cast<ConstantInt>(Range->getOperand(2*i + 1));
1654 Assert1(High, "The upper limit must be an integer!", High);
1655 Assert1(High->getType() == Low->getType() &&
1656 High->getType() == ElTy, "Range types must match load type!",
1657 &LI);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001658
1659 APInt HighV = High->getValue();
1660 APInt LowV = Low->getValue();
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001661 ConstantRange CurRange(LowV, HighV);
1662 Assert1(!CurRange.isEmptySet() && !CurRange.isFullSet(),
1663 "Range must not be empty!", Range);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001664 if (i != 0) {
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001665 Assert1(CurRange.intersectWith(LastRange).isEmptySet(),
1666 "Intervals are overlapping", Range);
1667 Assert1(LowV.sgt(LastRange.getLower()), "Intervals are not in order",
1668 Range);
1669 Assert1(!isContiguous(CurRange, LastRange), "Intervals are contiguous",
1670 Range);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001671 }
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001672 LastRange = ConstantRange(LowV, HighV);
Rafael Espindola39dd3282012-03-24 00:14:51 +00001673 }
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001674 if (NumRanges > 2) {
1675 APInt FirstLow =
1676 dyn_cast<ConstantInt>(Range->getOperand(0))->getValue();
1677 APInt FirstHigh =
1678 dyn_cast<ConstantInt>(Range->getOperand(1))->getValue();
1679 ConstantRange FirstRange(FirstLow, FirstHigh);
1680 Assert1(FirstRange.intersectWith(LastRange).isEmptySet(),
1681 "Intervals are overlapping", Range);
1682 Assert1(!isContiguous(FirstRange, LastRange), "Intervals are contiguous",
1683 Range);
1684 }
1685
1686
Rafael Espindola39dd3282012-03-24 00:14:51 +00001687 }
1688
Chris Lattnera00409e2002-04-24 19:12:21 +00001689 visitInstruction(LI);
1690}
1691
Chris Lattner24e845f2002-06-25 15:56:27 +00001692void Verifier::visitStoreInst(StoreInst &SI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001693 PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
Chris Lattnerb4a96312010-07-11 19:42:53 +00001694 Assert1(PTy, "Store operand must be a pointer.", &SI);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001695 Type *ElTy = PTy->getElementType();
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001696 Assert2(ElTy == SI.getOperand(0)->getType(),
1697 "Stored value type does not match pointer operand type!",
1698 &SI, ElTy);
Eli Friedman21006d42011-08-09 23:02:53 +00001699 if (SI.isAtomic()) {
1700 Assert1(SI.getOrdering() != Acquire && SI.getOrdering() != AcquireRelease,
1701 "Store cannot have Acquire ordering", &SI);
1702 Assert1(SI.getAlignment() != 0,
1703 "Atomic store must specify explicit alignment", &SI);
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001704 if (!ElTy->isPointerTy()) {
1705 Assert2(ElTy->isIntegerTy(),
1706 "atomic store operand must have integer type!",
1707 &SI, ElTy);
1708 unsigned Size = ElTy->getPrimitiveSizeInBits();
1709 Assert2(Size >= 8 && !(Size & (Size - 1)),
1710 "atomic store operand must be power-of-two byte-sized integer",
1711 &SI, ElTy);
1712 }
Eli Friedman21006d42011-08-09 23:02:53 +00001713 } else {
1714 Assert1(SI.getSynchScope() == CrossThread,
1715 "Non-atomic store cannot have SynchronizationScope specified", &SI);
1716 }
Chris Lattnera00409e2002-04-24 19:12:21 +00001717 visitInstruction(SI);
1718}
1719
Victor Hernandez7b929da2009-10-23 21:09:37 +00001720void Verifier::visitAllocaInst(AllocaInst &AI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001721 PointerType *PTy = AI.getType();
Chris Lattnerab3b7782008-03-01 09:01:57 +00001722 Assert1(PTy->getAddressSpace() == 0,
1723 "Allocation instruction pointer not in the generic address space!",
1724 &AI);
1725 Assert1(PTy->getElementType()->isSized(), "Cannot allocate unsized type",
1726 &AI);
Dan Gohmanf75a7d32010-05-28 01:14:11 +00001727 Assert1(AI.getArraySize()->getType()->isIntegerTy(),
1728 "Alloca array size must have integer type", &AI);
Christopher Lamb303dae92007-12-17 01:00:21 +00001729 visitInstruction(AI);
1730}
1731
Eli Friedmanff030482011-07-28 21:48:00 +00001732void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) {
1733 Assert1(CXI.getOrdering() != NotAtomic,
1734 "cmpxchg instructions must be atomic.", &CXI);
1735 Assert1(CXI.getOrdering() != Unordered,
1736 "cmpxchg instructions cannot be unordered.", &CXI);
1737 PointerType *PTy = dyn_cast<PointerType>(CXI.getOperand(0)->getType());
1738 Assert1(PTy, "First cmpxchg operand must be a pointer.", &CXI);
1739 Type *ElTy = PTy->getElementType();
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001740 Assert2(ElTy->isIntegerTy(),
1741 "cmpxchg operand must have integer type!",
1742 &CXI, ElTy);
1743 unsigned Size = ElTy->getPrimitiveSizeInBits();
1744 Assert2(Size >= 8 && !(Size & (Size - 1)),
1745 "cmpxchg operand must be power-of-two byte-sized integer",
1746 &CXI, ElTy);
Eli Friedmanff030482011-07-28 21:48:00 +00001747 Assert2(ElTy == CXI.getOperand(1)->getType(),
1748 "Expected value type does not match pointer operand type!",
1749 &CXI, ElTy);
1750 Assert2(ElTy == CXI.getOperand(2)->getType(),
1751 "Stored value type does not match pointer operand type!",
1752 &CXI, ElTy);
1753 visitInstruction(CXI);
1754}
1755
1756void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
1757 Assert1(RMWI.getOrdering() != NotAtomic,
1758 "atomicrmw instructions must be atomic.", &RMWI);
1759 Assert1(RMWI.getOrdering() != Unordered,
1760 "atomicrmw instructions cannot be unordered.", &RMWI);
1761 PointerType *PTy = dyn_cast<PointerType>(RMWI.getOperand(0)->getType());
1762 Assert1(PTy, "First atomicrmw operand must be a pointer.", &RMWI);
1763 Type *ElTy = PTy->getElementType();
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001764 Assert2(ElTy->isIntegerTy(),
1765 "atomicrmw operand must have integer type!",
1766 &RMWI, ElTy);
1767 unsigned Size = ElTy->getPrimitiveSizeInBits();
1768 Assert2(Size >= 8 && !(Size & (Size - 1)),
1769 "atomicrmw operand must be power-of-two byte-sized integer",
1770 &RMWI, ElTy);
Eli Friedmanff030482011-07-28 21:48:00 +00001771 Assert2(ElTy == RMWI.getOperand(1)->getType(),
1772 "Argument value type does not match pointer operand type!",
1773 &RMWI, ElTy);
1774 Assert1(AtomicRMWInst::FIRST_BINOP <= RMWI.getOperation() &&
1775 RMWI.getOperation() <= AtomicRMWInst::LAST_BINOP,
1776 "Invalid binary operation!", &RMWI);
1777 visitInstruction(RMWI);
1778}
1779
Eli Friedman47f35132011-07-25 23:16:38 +00001780void Verifier::visitFenceInst(FenceInst &FI) {
1781 const AtomicOrdering Ordering = FI.getOrdering();
1782 Assert1(Ordering == Acquire || Ordering == Release ||
1783 Ordering == AcquireRelease || Ordering == SequentiallyConsistent,
1784 "fence instructions may only have "
Bill Wendling63012202011-08-08 08:02:48 +00001785 "acquire, release, acq_rel, or seq_cst ordering.", &FI);
Eli Friedman47f35132011-07-25 23:16:38 +00001786 visitInstruction(FI);
1787}
1788
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001789void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
1790 Assert1(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001791 EVI.getIndices()) ==
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001792 EVI.getType(),
1793 "Invalid ExtractValueInst operands!", &EVI);
Chris Lattner42369b72008-04-23 04:06:15 +00001794
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001795 visitInstruction(EVI);
Devang Patel40a04212008-02-19 22:15:16 +00001796}
1797
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001798void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
1799 Assert1(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001800 IVI.getIndices()) ==
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001801 IVI.getOperand(1)->getType(),
1802 "Invalid InsertValueInst operands!", &IVI);
1803
1804 visitInstruction(IVI);
1805}
Chris Lattnerd231fc32002-04-18 20:37:37 +00001806
Bill Wendlinge6e88262011-08-12 20:24:12 +00001807void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
1808 BasicBlock *BB = LPI.getParent();
1809
1810 // The landingpad instruction is ill-formed if it doesn't have any clauses and
1811 // isn't a cleanup.
1812 Assert1(LPI.getNumClauses() > 0 || LPI.isCleanup(),
1813 "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
1814
1815 // The landingpad instruction defines its parent as a landing pad block. The
1816 // landing pad block may be branched to only by the unwind edge of an invoke.
1817 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
1818 const InvokeInst *II = dyn_cast<InvokeInst>((*I)->getTerminator());
Eli Friedman6b951b22012-08-10 20:55:20 +00001819 Assert1(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
Bill Wendlinge6e88262011-08-12 20:24:12 +00001820 "Block containing LandingPadInst must be jumped to "
1821 "only by the unwind edge of an invoke.", &LPI);
1822 }
1823
1824 // The landingpad instruction must be the first non-PHI instruction in the
1825 // block.
1826 Assert1(LPI.getParent()->getLandingPadInst() == &LPI,
1827 "LandingPadInst not the first non-PHI instruction in the block.",
1828 &LPI);
1829
1830 // The personality functions for all landingpad instructions within the same
1831 // function should match.
1832 if (PersonalityFn)
1833 Assert1(LPI.getPersonalityFn() == PersonalityFn,
1834 "Personality function doesn't match others in function", &LPI);
1835 PersonalityFn = LPI.getPersonalityFn();
1836
Duncan Sands00418ea2011-09-27 16:43:19 +00001837 // All operands must be constants.
1838 Assert1(isa<Constant>(PersonalityFn), "Personality function is not constant!",
1839 &LPI);
1840 for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) {
1841 Value *Clause = LPI.getClause(i);
1842 Assert1(isa<Constant>(Clause), "Clause is not constant!", &LPI);
Duncan Sands040bff02011-09-27 19:34:22 +00001843 if (LPI.isCatch(i)) {
1844 Assert1(isa<PointerType>(Clause->getType()),
1845 "Catch operand does not have pointer type!", &LPI);
1846 } else {
1847 Assert1(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI);
Duncan Sands00418ea2011-09-27 16:43:19 +00001848 Assert1(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause),
Duncan Sands040bff02011-09-27 19:34:22 +00001849 "Filter operand is not an array of constants!", &LPI);
1850 }
Duncan Sands00418ea2011-09-27 16:43:19 +00001851 }
1852
Bill Wendlinge6e88262011-08-12 20:24:12 +00001853 visitInstruction(LPI);
1854}
1855
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001856void Verifier::verifyDominatesUse(Instruction &I, unsigned i) {
1857 Instruction *Op = cast<Instruction>(I.getOperand(i));
Rafael Espindolad5118c82012-08-17 18:21:28 +00001858 // If the we have an invalid invoke, don't try to compute the dominance.
1859 // We already reject it in the invoke specific checks and the dominance
1860 // computation doesn't handle multiple edges.
1861 if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
1862 if (II->getNormalDest() == II->getUnwindDest())
1863 return;
1864 }
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001865
Rafael Espindola20907662012-06-01 21:56:26 +00001866 const Use &U = I.getOperandUse(i);
1867 Assert2(InstsInThisBlock.count(Op) || DT->dominates(Op, U),
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001868 "Instruction does not dominate all uses!", Op, &I);
1869}
1870
Misha Brukmanab5c6002004-03-02 00:22:19 +00001871/// verifyInstruction - Verify that an instruction is well formed.
1872///
Chris Lattner24e845f2002-06-25 15:56:27 +00001873void Verifier::visitInstruction(Instruction &I) {
Misha Brukmanfd939082005-04-21 23:48:37 +00001874 BasicBlock *BB = I.getParent();
Chris Lattner1a143ae2002-09-09 20:26:04 +00001875 Assert1(BB, "Instruction not embedded in basic block!", &I);
Chris Lattnerd231fc32002-04-18 20:37:37 +00001876
Chris Lattnerbede31f2003-10-05 17:44:18 +00001877 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
1878 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
1879 UI != UE; ++UI)
Duncan Sands44008252009-05-29 19:39:36 +00001880 Assert1(*UI != (User*)&I || !DT->isReachableFromEntry(BB),
Chris Lattnerbede31f2003-10-05 17:44:18 +00001881 "Only PHI nodes may reference their own value!", &I);
1882 }
Nick Lewycky29ef6592009-09-07 20:44:51 +00001883
Chris Lattnerbede31f2003-10-05 17:44:18 +00001884 // Check that void typed values don't have names
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001885 Assert1(!I.getType()->isVoidTy() || !I.hasName(),
Chris Lattnerbede31f2003-10-05 17:44:18 +00001886 "Instruction has a name, but provides a void value!", &I);
1887
Chris Lattner944cfaf2004-03-29 00:29:36 +00001888 // Check that the return value of the instruction is either void or a legal
1889 // value type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001890 Assert1(I.getType()->isVoidTy() ||
Nick Lewyckyc261df92009-09-27 23:27:42 +00001891 I.getType()->isFirstClassType(),
Chris Lattner944cfaf2004-03-29 00:29:36 +00001892 "Instruction returns a non-scalar type!", &I);
1893
Nick Lewyckyc261df92009-09-27 23:27:42 +00001894 // Check that the instruction doesn't produce metadata. Calls are already
1895 // checked against the callee type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001896 Assert1(!I.getType()->isMetadataTy() ||
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001897 isa<CallInst>(I) || isa<InvokeInst>(I),
1898 "Invalid use of metadata!", &I);
1899
Chris Lattnerd231fc32002-04-18 20:37:37 +00001900 // Check that all uses of the instruction, if they are instructions
1901 // themselves, actually have parent basic blocks. If the use is not an
1902 // instruction, it is an error!
Chris Lattner24e845f2002-06-25 15:56:27 +00001903 for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattnerd231fc32002-04-18 20:37:37 +00001904 UI != UE; ++UI) {
Nick Lewycky49072472009-09-08 01:23:52 +00001905 if (Instruction *Used = dyn_cast<Instruction>(*UI))
1906 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
1907 " embedded in a basic block!", &I, Used);
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001908 else {
Nick Lewycky49072472009-09-08 01:23:52 +00001909 CheckFailed("Use of instruction is not an instruction!", *UI);
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001910 return;
1911 }
Chris Lattnerd231fc32002-04-18 20:37:37 +00001912 }
1913
Chris Lattnerbede31f2003-10-05 17:44:18 +00001914 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Chris Lattneraab18202005-02-24 16:58:29 +00001915 Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I);
Chris Lattnerf4ea9212006-07-11 20:29:49 +00001916
1917 // Check to make sure that only first-class-values are operands to
1918 // instructions.
Devang Patelbb4f8d42008-02-21 01:54:02 +00001919 if (!I.getOperand(i)->getType()->isFirstClassType()) {
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001920 Assert1(0, "Instruction operands must be first-class values!", &I);
Devang Patelbb4f8d42008-02-21 01:54:02 +00001921 }
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001922
Chris Lattner59c35692004-03-14 03:23:54 +00001923 if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
Chris Lattnerf4ea9212006-07-11 20:29:49 +00001924 // Check to make sure that the "address of" an intrinsic function is never
1925 // taken.
Nuno Lopes917f97c2012-06-28 22:57:00 +00001926 Assert1(!F->isIntrinsic() || i == (isa<CallInst>(I) ? e-1 : 0),
Chris Lattnerdd035d12003-05-08 03:47:33 +00001927 "Cannot take the address of an intrinsic!", &I);
Nuno Lopes917f97c2012-06-28 22:57:00 +00001928 Assert1(!F->isIntrinsic() || isa<CallInst>(I) ||
1929 F->getIntrinsicID() == Intrinsic::donothing,
1930 "Cannot invoke an intrinsinc other than donothing", &I);
Chris Lattner19b6dcd2007-04-20 21:48:08 +00001931 Assert1(F->getParent() == Mod, "Referencing function in another module!",
1932 &I);
Chris Lattner59c35692004-03-14 03:23:54 +00001933 } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
1934 Assert1(OpBB->getParent() == BB->getParent(),
1935 "Referring to a basic block in another function!", &I);
1936 } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
1937 Assert1(OpArg->getParent() == BB->getParent(),
1938 "Referring to an argument in another function!", &I);
Chris Lattner19b6dcd2007-04-20 21:48:08 +00001939 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
1940 Assert1(GV->getParent() == Mod, "Referencing global in another module!",
1941 &I);
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001942 } else if (isa<Instruction>(I.getOperand(i))) {
1943 verifyDominatesUse(I, i);
Chris Lattner3188b732006-01-26 00:08:45 +00001944 } else if (isa<InlineAsm>(I.getOperand(i))) {
Gabor Greif63d024f2010-07-13 15:31:36 +00001945 Assert1((i + 1 == e && isa<CallInst>(I)) ||
1946 (i + 3 == e && isa<InvokeInst>(I)),
Chris Lattner3188b732006-01-26 00:08:45 +00001947 "Cannot take the address of an inline asm!", &I);
Chris Lattnerbede31f2003-10-05 17:44:18 +00001948 }
1949 }
Rafael Espindola39dd3282012-03-24 00:14:51 +00001950
Duncan Sands5e5c5f82012-04-14 12:36:06 +00001951 if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) {
Duncan Sands1fd63df2012-04-10 08:22:43 +00001952 Assert1(I.getType()->isFPOrFPVectorTy(),
Duncan Sands5e5c5f82012-04-14 12:36:06 +00001953 "fpmath requires a floating point result!", &I);
1954 Assert1(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
Duncan Sands8883c432012-04-16 16:28:59 +00001955 Value *Op0 = MD->getOperand(0);
1956 if (ConstantFP *CFP0 = dyn_cast_or_null<ConstantFP>(Op0)) {
1957 APFloat Accuracy = CFP0->getValueAPF();
1958 Assert1(Accuracy.isNormal() && !Accuracy.isNegative(),
1959 "fpmath accuracy not a positive number!", &I);
Duncan Sands8883c432012-04-16 16:28:59 +00001960 } else {
1961 Assert1(false, "invalid fpmath accuracy!", &I);
1962 }
Duncan Sands1fd63df2012-04-10 08:22:43 +00001963 }
1964
Rafael Espindola39dd3282012-03-24 00:14:51 +00001965 MDNode *MD = I.getMetadata(LLVMContext::MD_range);
1966 Assert1(!MD || isa<LoadInst>(I), "Ranges are only for loads!", &I);
1967
Chris Lattnera7b1c7e2004-09-29 20:07:45 +00001968 InstsInThisBlock.insert(&I);
Chris Lattnerdd035d12003-05-08 03:47:33 +00001969}
1970
Chris Lattner55dc5c72012-05-27 19:37:05 +00001971/// VerifyIntrinsicType - Verify that the specified type (which comes from an
1972/// intrinsic argument or return value) matches the type constraints specified
1973/// by the .td file (e.g. an "any integer" argument really is an integer).
1974///
1975/// This return true on error but does not print a message.
1976bool Verifier::VerifyIntrinsicType(Type *Ty,
1977 ArrayRef<Intrinsic::IITDescriptor> &Infos,
1978 SmallVectorImpl<Type*> &ArgTys) {
1979 using namespace Intrinsic;
1980
1981 // If we ran out of descriptors, there are too many arguments.
1982 if (Infos.empty()) return true;
1983 IITDescriptor D = Infos.front();
1984 Infos = Infos.slice(1);
1985
1986 switch (D.Kind) {
1987 case IITDescriptor::Void: return !Ty->isVoidTy();
1988 case IITDescriptor::MMX: return !Ty->isX86_MMXTy();
1989 case IITDescriptor::Metadata: return !Ty->isMetadataTy();
Michael Ilseman4d0b4a42013-01-11 01:45:05 +00001990 case IITDescriptor::Half: return !Ty->isHalfTy();
Chris Lattner55dc5c72012-05-27 19:37:05 +00001991 case IITDescriptor::Float: return !Ty->isFloatTy();
1992 case IITDescriptor::Double: return !Ty->isDoubleTy();
1993 case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
1994 case IITDescriptor::Vector: {
1995 VectorType *VT = dyn_cast<VectorType>(Ty);
1996 return VT == 0 || VT->getNumElements() != D.Vector_Width ||
1997 VerifyIntrinsicType(VT->getElementType(), Infos, ArgTys);
1998 }
1999 case IITDescriptor::Pointer: {
2000 PointerType *PT = dyn_cast<PointerType>(Ty);
2001 return PT == 0 || PT->getAddressSpace() != D.Pointer_AddressSpace ||
2002 VerifyIntrinsicType(PT->getElementType(), Infos, ArgTys);
2003 }
2004
2005 case IITDescriptor::Struct: {
2006 StructType *ST = dyn_cast<StructType>(Ty);
2007 if (ST == 0 || ST->getNumElements() != D.Struct_NumElements)
2008 return true;
2009
2010 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
2011 if (VerifyIntrinsicType(ST->getElementType(i), Infos, ArgTys))
2012 return true;
2013 return false;
2014 }
2015
2016 case IITDescriptor::Argument:
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00002017 // Two cases here - If this is the second occurrence of an argument, verify
Chris Lattner55dc5c72012-05-27 19:37:05 +00002018 // that the later instance matches the previous instance.
2019 if (D.getArgumentNumber() < ArgTys.size())
2020 return Ty != ArgTys[D.getArgumentNumber()];
2021
2022 // Otherwise, if this is the first instance of an argument, record it and
2023 // verify the "Any" kind.
2024 assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error");
2025 ArgTys.push_back(Ty);
2026
2027 switch (D.getArgumentKind()) {
2028 case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
2029 case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy();
2030 case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty);
2031 case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
2032 }
2033 llvm_unreachable("all argument kinds not covered");
2034
2035 case IITDescriptor::ExtendVecArgument:
2036 // This may only be used when referring to a previous vector argument.
2037 return D.getArgumentNumber() >= ArgTys.size() ||
2038 !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
2039 VectorType::getExtendedElementVectorType(
2040 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
2041
2042 case IITDescriptor::TruncVecArgument:
2043 // This may only be used when referring to a previous vector argument.
2044 return D.getArgumentNumber() >= ArgTys.size() ||
2045 !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
2046 VectorType::getTruncatedElementVectorType(
2047 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
2048 }
2049 llvm_unreachable("unhandled");
2050}
Bob Wilsonbc039792009-01-07 00:09:01 +00002051
Chris Lattnerdd035d12003-05-08 03:47:33 +00002052/// visitIntrinsicFunction - Allow intrinsics to be verified in different ways.
Misha Brukmanab5c6002004-03-02 00:22:19 +00002053///
Brian Gaeked0fde302003-11-11 22:41:34 +00002054void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnerdd035d12003-05-08 03:47:33 +00002055 Function *IF = CI.getCalledFunction();
Chris Lattner19b6dcd2007-04-20 21:48:08 +00002056 Assert1(IF->isDeclaration(), "Intrinsic functions should never be defined!",
2057 IF);
Nick Lewycky29ef6592009-09-07 20:44:51 +00002058
Chris Lattner55dc5c72012-05-27 19:37:05 +00002059 // Verify that the intrinsic prototype lines up with what the .td files
2060 // describe.
2061 FunctionType *IFTy = IF->getFunctionType();
2062 Assert1(!IFTy->isVarArg(), "Intrinsic prototypes are not varargs", IF);
2063
2064 SmallVector<Intrinsic::IITDescriptor, 8> Table;
2065 getIntrinsicInfoTableEntries(ID, Table);
2066 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
Nick Lewycky29ef6592009-09-07 20:44:51 +00002067
Chris Lattner55dc5c72012-05-27 19:37:05 +00002068 SmallVector<Type *, 4> ArgTys;
2069 Assert1(!VerifyIntrinsicType(IFTy->getReturnType(), TableRef, ArgTys),
2070 "Intrinsic has incorrect return type!", IF);
2071 for (unsigned i = 0, e = IFTy->getNumParams(); i != e; ++i)
2072 Assert1(!VerifyIntrinsicType(IFTy->getParamType(i), TableRef, ArgTys),
2073 "Intrinsic has incorrect argument type!", IF);
2074 Assert1(TableRef.empty(), "Intrinsic has too few arguments!", IF);
2075
2076 // Now that we have the intrinsic ID and the actual argument types (and we
2077 // know they are legal for the intrinsic!) get the intrinsic name through the
2078 // usual means. This allows us to verify the mangling of argument types into
2079 // the name.
2080 Assert1(Intrinsic::getName(ID, ArgTys) == IF->getName(),
2081 "Intrinsic name not mangled correctly for type arguments!", IF);
2082
Chris Lattnerb241b372009-12-28 09:07:21 +00002083 // If the intrinsic takes MDNode arguments, verify that they are either global
2084 // or are local to *this* function.
Bill Wendlingd942df22010-06-07 19:18:58 +00002085 for (unsigned i = 0, e = CI.getNumArgOperands(); i != e; ++i)
2086 if (MDNode *MD = dyn_cast<MDNode>(CI.getArgOperand(i)))
Duncan Sandse704d9d2010-04-29 16:10:30 +00002087 visitMDNode(*MD, CI.getParent()->getParent());
Victor Hernandez5d301622009-12-18 20:09:14 +00002088
Gordon Henriksen8c33da52007-09-17 20:30:04 +00002089 switch (ID) {
2090 default:
2091 break;
Chandler Carruthc4eab902011-12-12 04:36:02 +00002092 case Intrinsic::ctlz: // llvm.ctlz
2093 case Intrinsic::cttz: // llvm.cttz
2094 Assert1(isa<ConstantInt>(CI.getArgOperand(1)),
2095 "is_zero_undef argument of bit counting intrinsics must be a "
2096 "constant int", &CI);
2097 break;
Victor Hernandez02d574d2010-01-22 19:06:12 +00002098 case Intrinsic::dbg_declare: { // llvm.dbg.declare
Gabor Greifb37a64a2010-06-23 13:56:57 +00002099 Assert1(CI.getArgOperand(0) && isa<MDNode>(CI.getArgOperand(0)),
Victor Hernandez02d574d2010-01-22 19:06:12 +00002100 "invalid llvm.dbg.declare intrinsic call 1", &CI);
Gabor Greifb37a64a2010-06-23 13:56:57 +00002101 MDNode *MD = cast<MDNode>(CI.getArgOperand(0));
Victor Hernandez02d574d2010-01-22 19:06:12 +00002102 Assert1(MD->getNumOperands() == 1,
2103 "invalid llvm.dbg.declare intrinsic call 2", &CI);
Victor Hernandez02d574d2010-01-22 19:06:12 +00002104 } break;
Chris Lattner824b9582008-11-21 16:42:48 +00002105 case Intrinsic::memcpy:
2106 case Intrinsic::memmove:
2107 case Intrinsic::memset:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002108 Assert1(isa<ConstantInt>(CI.getArgOperand(3)),
Chris Lattner259f88e2008-08-23 05:31:10 +00002109 "alignment argument of memory intrinsics must be a constant int",
2110 &CI);
Eli Friedmane65e9ee2011-05-31 20:12:07 +00002111 Assert1(isa<ConstantInt>(CI.getArgOperand(4)),
2112 "isvolatile argument of memory intrinsics must be a constant int",
2113 &CI);
Chris Lattner259f88e2008-08-23 05:31:10 +00002114 break;
Bill Wendling955fdeb2008-08-23 09:46:46 +00002115 case Intrinsic::gcroot:
2116 case Intrinsic::gcwrite:
Chris Lattner415b4142008-08-24 20:46:13 +00002117 case Intrinsic::gcread:
2118 if (ID == Intrinsic::gcroot) {
Gordon Henriksena2cbe6c2008-10-25 16:28:35 +00002119 AllocaInst *AI =
Gabor Greifb37a64a2010-06-23 13:56:57 +00002120 dyn_cast<AllocaInst>(CI.getArgOperand(0)->stripPointerCasts());
Talinc87cfb62010-09-30 20:23:47 +00002121 Assert1(AI, "llvm.gcroot parameter #1 must be an alloca.", &CI);
Gabor Greifb37a64a2010-06-23 13:56:57 +00002122 Assert1(isa<Constant>(CI.getArgOperand(1)),
Chris Lattner415b4142008-08-24 20:46:13 +00002123 "llvm.gcroot parameter #2 must be a constant.", &CI);
Talinc87cfb62010-09-30 20:23:47 +00002124 if (!AI->getType()->getElementType()->isPointerTy()) {
2125 Assert1(!isa<ConstantPointerNull>(CI.getArgOperand(1)),
2126 "llvm.gcroot parameter #1 must either be a pointer alloca, "
2127 "or argument #2 must be a non-null constant.", &CI);
2128 }
Chris Lattner415b4142008-08-24 20:46:13 +00002129 }
Nick Lewycky29ef6592009-09-07 20:44:51 +00002130
Chris Lattner415b4142008-08-24 20:46:13 +00002131 Assert1(CI.getParent()->getParent()->hasGC(),
2132 "Enclosing function does not use GC.", &CI);
2133 break;
Duncan Sandsf51edad2007-09-29 16:25:54 +00002134 case Intrinsic::init_trampoline:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002135 Assert1(isa<Function>(CI.getArgOperand(1)->stripPointerCasts()),
Duncan Sandsf51edad2007-09-29 16:25:54 +00002136 "llvm.init_trampoline parameter #2 must resolve to a function.",
2137 &CI);
Gordon Henriksen27acd3a2007-12-25 02:02:10 +00002138 break;
Chris Lattnerd3745472008-10-16 06:00:36 +00002139 case Intrinsic::prefetch:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002140 Assert1(isa<ConstantInt>(CI.getArgOperand(1)) &&
2141 isa<ConstantInt>(CI.getArgOperand(2)) &&
2142 cast<ConstantInt>(CI.getArgOperand(1))->getZExtValue() < 2 &&
2143 cast<ConstantInt>(CI.getArgOperand(2))->getZExtValue() < 4,
Chris Lattnerd3745472008-10-16 06:00:36 +00002144 "invalid arguments to llvm.prefetch",
2145 &CI);
2146 break;
Bill Wendlingc5b795e2008-11-18 23:09:31 +00002147 case Intrinsic::stackprotector:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002148 Assert1(isa<AllocaInst>(CI.getArgOperand(1)->stripPointerCasts()),
Bill Wendlingc5b795e2008-11-18 23:09:31 +00002149 "llvm.stackprotector parameter #2 must resolve to an alloca.",
2150 &CI);
2151 break;
Nick Lewycky321333e2009-10-13 07:57:33 +00002152 case Intrinsic::lifetime_start:
2153 case Intrinsic::lifetime_end:
2154 case Intrinsic::invariant_start:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002155 Assert1(isa<ConstantInt>(CI.getArgOperand(0)),
Nick Lewycky321333e2009-10-13 07:57:33 +00002156 "size argument of memory use markers must be a constant integer",
2157 &CI);
2158 break;
2159 case Intrinsic::invariant_end:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002160 Assert1(isa<ConstantInt>(CI.getArgOperand(1)),
Nick Lewycky321333e2009-10-13 07:57:33 +00002161 "llvm.invariant.end parameter #2 must be a constant integer", &CI);
2162 break;
Gordon Henriksen8c33da52007-09-17 20:30:04 +00002163 }
Chris Lattnerd231fc32002-04-18 20:37:37 +00002164}
2165
Chris Lattnerd231fc32002-04-18 20:37:37 +00002166//===----------------------------------------------------------------------===//
2167// Implement the public interfaces to this file...
2168//===----------------------------------------------------------------------===//
2169
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002170FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) {
2171 return new Verifier(action);
Chris Lattnerd231fc32002-04-18 20:37:37 +00002172}
2173
Chris Lattner9ce231f2002-08-02 17:37:08 +00002174
Dan Gohmanbcf9f002010-04-08 15:57:10 +00002175/// verifyFunction - Check a function for errors, printing messages on stderr.
2176/// Return true if the function is corrupt.
2177///
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002178bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) {
Chris Lattner2eff8592004-03-14 03:16:15 +00002179 Function &F = const_cast<Function&>(f);
Reid Spencer5cbf9852007-01-30 20:08:39 +00002180 assert(!F.isDeclaration() && "Cannot verify external functions");
Misha Brukmanfd939082005-04-21 23:48:37 +00002181
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002182 FunctionPassManager FPM(F.getParent());
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002183 Verifier *V = new Verifier(action);
Chris Lattner2eff8592004-03-14 03:16:15 +00002184 FPM.add(V);
2185 FPM.run(F);
2186 return V->Broken;
Chris Lattner44d5bd92002-02-20 17:55:43 +00002187}
2188
Misha Brukmanab5c6002004-03-02 00:22:19 +00002189/// verifyModule - Check a module for errors, printing messages on stderr.
2190/// Return true if the module is corrupt.
2191///
Chris Lattner05ac92c2006-07-06 18:02:27 +00002192bool llvm::verifyModule(const Module &M, VerifierFailureAction action,
2193 std::string *ErrorInfo) {
Chris Lattner9ce231f2002-08-02 17:37:08 +00002194 PassManager PM;
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002195 Verifier *V = new Verifier(action);
Chris Lattner9ce231f2002-08-02 17:37:08 +00002196 PM.add(V);
Dan Gohman78f39da2008-06-24 17:47:37 +00002197 PM.run(const_cast<Module&>(M));
Nick Lewycky29ef6592009-09-07 20:44:51 +00002198
Chris Lattner05ac92c2006-07-06 18:02:27 +00002199 if (ErrorInfo && V->Broken)
Chris Lattner37f077a2009-08-23 04:02:03 +00002200 *ErrorInfo = V->MessagesStr.str();
Chris Lattner9ce231f2002-08-02 17:37:08 +00002201 return V->Broken;
Chris Lattner00950542001-06-06 20:29:01 +00002202}