blob: 5a4a5a790ed586c41be68f72276770694cdac56a [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);
246 void visitModuleFlag(MDNode *Op, SmallSetVector<MDString*, 16> &SeenIDs);
Chris Lattner24e845f2002-06-25 15:56:27 +0000247 void visitFunction(Function &F);
248 void visitBasicBlock(BasicBlock &BB);
Chris Lattnercad208b2008-08-28 04:02:44 +0000249 using InstVisitor<Verifier>::visit;
Nick Lewycky29ef6592009-09-07 20:44:51 +0000250
Chris Lattnercad208b2008-08-28 04:02:44 +0000251 void visit(Instruction &I);
Nick Lewycky29ef6592009-09-07 20:44:51 +0000252
Reid Spencer3da59db2006-11-27 01:05:10 +0000253 void visitTruncInst(TruncInst &I);
254 void visitZExtInst(ZExtInst &I);
255 void visitSExtInst(SExtInst &I);
256 void visitFPTruncInst(FPTruncInst &I);
257 void visitFPExtInst(FPExtInst &I);
258 void visitFPToUIInst(FPToUIInst &I);
259 void visitFPToSIInst(FPToSIInst &I);
260 void visitUIToFPInst(UIToFPInst &I);
261 void visitSIToFPInst(SIToFPInst &I);
262 void visitIntToPtrInst(IntToPtrInst &I);
263 void visitPtrToIntInst(PtrToIntInst &I);
264 void visitBitCastInst(BitCastInst &I);
Chris Lattner24e845f2002-06-25 15:56:27 +0000265 void visitPHINode(PHINode &PN);
266 void visitBinaryOperator(BinaryOperator &B);
Reid Spencer45fb3f32006-11-20 01:22:35 +0000267 void visitICmpInst(ICmpInst &IC);
268 void visitFCmpInst(FCmpInst &FC);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +0000269 void visitExtractElementInst(ExtractElementInst &EI);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +0000270 void visitInsertElementInst(InsertElementInst &EI);
Chris Lattner00f10232006-04-08 01:18:18 +0000271 void visitShuffleVectorInst(ShuffleVectorInst &EI);
Chris Lattner4d45bd02003-10-18 05:57:43 +0000272 void visitVAArgInst(VAArgInst &VAA) { visitInstruction(VAA); }
Chris Lattner24e845f2002-06-25 15:56:27 +0000273 void visitCallInst(CallInst &CI);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000274 void visitInvokeInst(InvokeInst &II);
Chris Lattner24e845f2002-06-25 15:56:27 +0000275 void visitGetElementPtrInst(GetElementPtrInst &GEP);
276 void visitLoadInst(LoadInst &LI);
277 void visitStoreInst(StoreInst &SI);
Rafael Espindolac987f4c2012-02-26 02:23:37 +0000278 void verifyDominatesUse(Instruction &I, unsigned i);
Chris Lattner24e845f2002-06-25 15:56:27 +0000279 void visitInstruction(Instruction &I);
280 void visitTerminatorInst(TerminatorInst &I);
Nick Lewycky6a7cb632010-02-15 22:09:09 +0000281 void visitBranchInst(BranchInst &BI);
Chris Lattner24e845f2002-06-25 15:56:27 +0000282 void visitReturnInst(ReturnInst &RI);
Chris Lattner0f9e9d02004-05-21 16:47:21 +0000283 void visitSwitchInst(SwitchInst &SI);
Dan Gohman37680912010-08-02 23:08:33 +0000284 void visitIndirectBrInst(IndirectBrInst &BI);
Chris Lattner230c1a72004-03-12 05:54:31 +0000285 void visitSelectInst(SelectInst &SI);
Chris Lattner627079d2002-11-21 16:54:22 +0000286 void visitUserOp1(Instruction &I);
287 void visitUserOp2(Instruction &I) { visitUserOp1(I); }
Brian Gaeked0fde302003-11-11 22:41:34 +0000288 void visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI);
Eli Friedmanff030482011-07-28 21:48:00 +0000289 void visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI);
290 void visitAtomicRMWInst(AtomicRMWInst &RMWI);
Eli Friedman47f35132011-07-25 23:16:38 +0000291 void visitFenceInst(FenceInst &FI);
Victor Hernandez7b929da2009-10-23 21:09:37 +0000292 void visitAllocaInst(AllocaInst &AI);
Dan Gohmanfc74abf2008-07-23 00:34:11 +0000293 void visitExtractValueInst(ExtractValueInst &EVI);
294 void visitInsertValueInst(InsertValueInst &IVI);
Bill Wendlinge6e88262011-08-12 20:24:12 +0000295 void visitLandingPadInst(LandingPadInst &LPI);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000296
Duncan Sandsd9d70392007-12-21 19:19:01 +0000297 void VerifyCallSite(CallSite CS);
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000298 bool PerformTypeCheck(Intrinsic::ID ID, Function *F, Type *Ty,
Bill Wendlinga6ce05f2008-11-13 07:11:27 +0000299 int VT, unsigned ArgNo, std::string &Suffix);
Chris Lattner55dc5c72012-05-27 19:37:05 +0000300 bool VerifyIntrinsicType(Type *Ty,
301 ArrayRef<Intrinsic::IITDescriptor> &Infos,
302 SmallVectorImpl<Type*> &ArgTys);
Bill Wendling034b94b2012-12-19 07:18:57 +0000303 void VerifyParameterAttrs(Attribute Attrs, Type *Ty,
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000304 bool isReturnValue, const Value *V);
Bill Wendling99faa3b2012-12-07 23:16:57 +0000305 void VerifyFunctionAttrs(FunctionType *FT, const AttributeSet &Attrs,
Duncan Sandscfad1b42008-01-12 16:42:01 +0000306 const Value *V);
Chris Lattner15e87522003-11-21 17:35:51 +0000307
308 void WriteValue(const Value *V) {
309 if (!V) return;
Chris Lattner31f84992003-11-21 20:23:48 +0000310 if (isa<Instruction>(V)) {
Nick Lewycky2bb6f6a2009-10-17 19:43:45 +0000311 MessagesStr << *V << '\n';
Chris Lattner31f84992003-11-21 20:23:48 +0000312 } else {
Chris Lattner37f077a2009-08-23 04:02:03 +0000313 WriteAsOperand(MessagesStr, V, true, Mod);
Nick Lewycky2bb6f6a2009-10-17 19:43:45 +0000314 MessagesStr << '\n';
Chris Lattner15e87522003-11-21 17:35:51 +0000315 }
316 }
317
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000318 void WriteType(Type *T) {
Chris Lattnerc2871372009-02-28 21:05:51 +0000319 if (!T) return;
Chris Lattner1afcace2011-07-09 17:41:24 +0000320 MessagesStr << ' ' << *T;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000321 }
322
Chris Lattner15e87522003-11-21 17:35:51 +0000323
Chris Lattnerd231fc32002-04-18 20:37:37 +0000324 // CheckFailed - A check failed, so print out the condition and the message
325 // that failed. This provides a nice place to put a breakpoint if you want
326 // to see why something is not correct.
Daniel Dunbar6e0d1cb2009-07-25 04:41:11 +0000327 void CheckFailed(const Twine &Message,
Chris Lattner15e87522003-11-21 17:35:51 +0000328 const Value *V1 = 0, const Value *V2 = 0,
329 const Value *V3 = 0, const Value *V4 = 0) {
Chris Lattner37f077a2009-08-23 04:02:03 +0000330 MessagesStr << Message.str() << "\n";
Chris Lattner15e87522003-11-21 17:35:51 +0000331 WriteValue(V1);
332 WriteValue(V2);
333 WriteValue(V3);
334 WriteValue(V4);
Chris Lattnerd231fc32002-04-18 20:37:37 +0000335 Broken = true;
336 }
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000337
Nick Lewycky4b6af8a2009-09-08 02:02:39 +0000338 void CheckFailed(const Twine &Message, const Value *V1,
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000339 Type *T2, const Value *V3 = 0) {
Chris Lattner37f077a2009-08-23 04:02:03 +0000340 MessagesStr << Message.str() << "\n";
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000341 WriteValue(V1);
342 WriteType(T2);
343 WriteValue(V3);
Reid Spencer5dff1582004-05-27 21:58:13 +0000344 Broken = true;
Reid Spenceraf90b0d2004-05-25 08:53:29 +0000345 }
Nick Lewycky49072472009-09-08 01:23:52 +0000346
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000347 void CheckFailed(const Twine &Message, Type *T1,
348 Type *T2 = 0, Type *T3 = 0) {
Nick Lewycky49072472009-09-08 01:23:52 +0000349 MessagesStr << Message.str() << "\n";
350 WriteType(T1);
351 WriteType(T2);
352 WriteType(T3);
353 Broken = true;
354 }
Chris Lattnerd231fc32002-04-18 20:37:37 +0000355 };
Chris Lattner31f84992003-11-21 20:23:48 +0000356} // End anonymous namespace
357
Dan Gohman844731a2008-05-13 00:00:25 +0000358char Verifier::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +0000359INITIALIZE_PASS_BEGIN(Verifier, "verify", "Module Verifier", false, false)
360INITIALIZE_PASS_DEPENDENCY(PreVerifier)
361INITIALIZE_PASS_DEPENDENCY(DominatorTree)
362INITIALIZE_PASS_END(Verifier, "verify", "Module Verifier", false, false)
Chris Lattner00950542001-06-06 20:29:01 +0000363
Chris Lattner44d5bd92002-02-20 17:55:43 +0000364// Assert - We know that cond should be true, if not print an error message.
365#define Assert(C, M) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000366 do { if (!(C)) { CheckFailed(M); return; } } while (0)
Chris Lattner44d5bd92002-02-20 17:55:43 +0000367#define Assert1(C, M, V1) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000368 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
Chris Lattner44d5bd92002-02-20 17:55:43 +0000369#define Assert2(C, M, V1, V2) \
Chris Lattner39dd0242002-04-28 16:06:24 +0000370 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
Chris Lattner24e845f2002-06-25 15:56:27 +0000371#define Assert3(C, M, V1, V2, V3) \
372 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
373#define Assert4(C, M, V1, V2, V3, V4) \
374 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
Chris Lattner00950542001-06-06 20:29:01 +0000375
Chris Lattnercad208b2008-08-28 04:02:44 +0000376void Verifier::visit(Instruction &I) {
377 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
378 Assert1(I.getOperand(i) != 0, "Operand is null", &I);
379 InstVisitor<Verifier>::visit(I);
380}
381
382
Chris Lattner53997412003-04-16 20:42:40 +0000383void Verifier::visitGlobalValue(GlobalValue &GV) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000384 Assert1(!GV.isDeclaration() ||
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000385 GV.isMaterializable() ||
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000386 GV.hasExternalLinkage() ||
387 GV.hasDLLImportLinkage() ||
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000388 GV.hasExternalWeakLinkage() ||
389 (isa<GlobalAlias>(GV) &&
Rafael Espindolabb46f522009-01-15 20:18:42 +0000390 (GV.hasLocalLinkage() || GV.hasWeakLinkage())),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000391 "Global is external, but doesn't have external or dllimport or weak linkage!",
392 &GV);
393
Reid Spencer5cbf9852007-01-30 20:08:39 +0000394 Assert1(!GV.hasDLLImportLinkage() || GV.isDeclaration(),
Anton Korobeynikovb74ed072006-09-14 18:23:27 +0000395 "Global is marked as dllimport, but not external", &GV);
Nick Lewycky49072472009-09-08 01:23:52 +0000396
Chris Lattner53997412003-04-16 20:42:40 +0000397 Assert1(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),
398 "Only global variables can have appending linkage!", &GV);
399
400 if (GV.hasAppendingLinkage()) {
Nick Lewycky49072472009-09-08 01:23:52 +0000401 GlobalVariable *GVar = dyn_cast<GlobalVariable>(&GV);
Duncan Sands1df98592010-02-16 11:11:14 +0000402 Assert1(GVar && GVar->getType()->getElementType()->isArrayTy(),
Nick Lewycky49072472009-09-08 01:23:52 +0000403 "Only global arrays can have appending linkage!", GVar);
Chris Lattner53997412003-04-16 20:42:40 +0000404 }
Bill Wendling55ae5152010-08-20 22:05:50 +0000405
Bill Wendling32811be2012-08-17 18:33:14 +0000406 Assert1(!GV.hasLinkOnceODRAutoHideLinkage() || GV.hasDefaultVisibility(),
407 "linkonce_odr_auto_hide can only have default visibility!",
Bill Wendling55ae5152010-08-20 22:05:50 +0000408 &GV);
Chris Lattner53997412003-04-16 20:42:40 +0000409}
410
Chris Lattner56998b22004-12-15 20:23:49 +0000411void Verifier::visitGlobalVariable(GlobalVariable &GV) {
Chris Lattner6693da02007-09-19 17:14:45 +0000412 if (GV.hasInitializer()) {
Chris Lattner56998b22004-12-15 20:23:49 +0000413 Assert1(GV.getInitializer()->getType() == GV.getType()->getElementType(),
414 "Global variable initializer type does not match global "
415 "variable type!", &GV);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000416
Chris Lattnercd81f5d2009-08-05 05:41:44 +0000417 // If the global has common linkage, it must have a zero initializer and
418 // cannot be constant.
419 if (GV.hasCommonLinkage()) {
Chris Lattner26d054d2009-08-05 05:21:07 +0000420 Assert1(GV.getInitializer()->isNullValue(),
421 "'common' global must have a zero initializer!", &GV);
Chris Lattnercd81f5d2009-08-05 05:41:44 +0000422 Assert1(!GV.isConstant(), "'common' global may not be marked constant!",
423 &GV);
424 }
Chris Lattner6693da02007-09-19 17:14:45 +0000425 } else {
426 Assert1(GV.hasExternalLinkage() || GV.hasDLLImportLinkage() ||
427 GV.hasExternalWeakLinkage(),
428 "invalid linkage type for global declaration", &GV);
429 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000430
Nick Lewycky2c44a802011-04-08 07:30:21 +0000431 if (GV.hasName() && (GV.getName() == "llvm.global_ctors" ||
432 GV.getName() == "llvm.global_dtors")) {
433 Assert1(!GV.hasInitializer() || GV.hasAppendingLinkage(),
434 "invalid linkage for intrinsic global variable", &GV);
435 // Don't worry about emitting an error for it not being an array,
436 // visitGlobalValue will complain on appending non-array.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000437 if (ArrayType *ATy = dyn_cast<ArrayType>(GV.getType())) {
438 StructType *STy = dyn_cast<StructType>(ATy->getElementType());
439 PointerType *FuncPtrTy =
Micah Villmowb8bce922012-10-24 17:25:11 +0000440 FunctionType::get(Type::getVoidTy(*Context), false)->getPointerTo();
Nick Lewycky2c44a802011-04-08 07:30:21 +0000441 Assert1(STy && STy->getNumElements() == 2 &&
442 STy->getTypeAtIndex(0u)->isIntegerTy(32) &&
443 STy->getTypeAtIndex(1) == FuncPtrTy,
444 "wrong type for intrinsic global variable", &GV);
445 }
446 }
447
Chris Lattner56998b22004-12-15 20:23:49 +0000448 visitGlobalValue(GV);
449}
450
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000451void Verifier::visitGlobalAlias(GlobalAlias &GA) {
452 Assert1(!GA.getName().empty(),
453 "Alias name cannot be empty!", &GA);
Rafael Espindolabb46f522009-01-15 20:18:42 +0000454 Assert1(GA.hasExternalLinkage() || GA.hasLocalLinkage() ||
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000455 GA.hasWeakLinkage(),
456 "Alias should have external or external weak linkage!", &GA);
Anton Korobeynikov018f7712008-05-08 23:11:06 +0000457 Assert1(GA.getAliasee(),
458 "Aliasee cannot be NULL!", &GA);
Anton Korobeynikova80e1182007-04-28 13:45:00 +0000459 Assert1(GA.getType() == GA.getAliasee()->getType(),
460 "Alias and aliasee types should match!", &GA);
Rafael Espindolabea46262011-01-08 16:42:36 +0000461 Assert1(!GA.hasUnnamedAddr(), "Alias cannot have unnamed_addr!", &GA);
Anton Korobeynikov018f7712008-05-08 23:11:06 +0000462
Anton Korobeynikov0f53f7f2007-04-28 14:35:41 +0000463 if (!isa<GlobalValue>(GA.getAliasee())) {
464 const ConstantExpr *CE = dyn_cast<ConstantExpr>(GA.getAliasee());
Chris Lattnera2165ed2009-04-25 21:23:19 +0000465 Assert1(CE &&
466 (CE->getOpcode() == Instruction::BitCast ||
467 CE->getOpcode() == Instruction::GetElementPtr) &&
Anton Korobeynikovc6c98af2007-04-29 18:02:48 +0000468 isa<GlobalValue>(CE->getOperand(0)),
Anton Korobeynikov0f53f7f2007-04-28 14:35:41 +0000469 "Aliasee should be either GlobalValue or bitcast of GlobalValue",
470 &GA);
471 }
Anton Korobeynikov726d45c2008-03-22 08:36:14 +0000472
Anton Korobeynikov19e861a2008-09-09 20:05:04 +0000473 const GlobalValue* Aliasee = GA.resolveAliasedGlobal(/*stopOnWeak*/ false);
Anton Korobeynikov726d45c2008-03-22 08:36:14 +0000474 Assert1(Aliasee,
Anton Korobeynikovef30c1d2008-03-22 08:37:05 +0000475 "Aliasing chain should end with function or global variable", &GA);
Anton Korobeynikov726d45c2008-03-22 08:36:14 +0000476
Anton Korobeynikov8b0a8c82007-04-25 14:27:10 +0000477 visitGlobalValue(GA);
478}
479
Duncan Sandse704d9d2010-04-29 16:10:30 +0000480void Verifier::visitNamedMDNode(NamedMDNode &NMD) {
481 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) {
482 MDNode *MD = NMD.getOperand(i);
483 if (!MD)
484 continue;
485
Dan Gohman17aa92c2010-07-21 23:38:33 +0000486 Assert1(!MD->isFunctionLocal(),
487 "Named metadata operand cannot be function local!", MD);
Duncan Sandse704d9d2010-04-29 16:10:30 +0000488 visitMDNode(*MD, 0);
489 }
490}
491
492void Verifier::visitMDNode(MDNode &MD, Function *F) {
493 // Only visit each node once. Metadata can be mutually recursive, so this
494 // avoids infinite recursion here, as well as being an optimization.
495 if (!MDNodes.insert(&MD))
496 return;
497
498 for (unsigned i = 0, e = MD.getNumOperands(); i != e; ++i) {
499 Value *Op = MD.getOperand(i);
500 if (!Op)
501 continue;
Dan Gohman557c83c2010-07-21 20:25:43 +0000502 if (isa<Constant>(Op) || isa<MDString>(Op))
Duncan Sandse704d9d2010-04-29 16:10:30 +0000503 continue;
504 if (MDNode *N = dyn_cast<MDNode>(Op)) {
505 Assert2(MD.isFunctionLocal() || !N->isFunctionLocal(),
506 "Global metadata operand cannot be function local!", &MD, N);
507 visitMDNode(*N, F);
508 continue;
509 }
510 Assert2(MD.isFunctionLocal(), "Invalid operand for global metadata!", &MD, Op);
511
512 // If this was an instruction, bb, or argument, verify that it is in the
513 // function that we expect.
514 Function *ActualF = 0;
515 if (Instruction *I = dyn_cast<Instruction>(Op))
516 ActualF = I->getParent()->getParent();
517 else if (BasicBlock *BB = dyn_cast<BasicBlock>(Op))
518 ActualF = BB->getParent();
519 else if (Argument *A = dyn_cast<Argument>(Op))
520 ActualF = A->getParent();
521 assert(ActualF && "Unimplemented function local metadata case!");
522
523 Assert2(ActualF == F, "function-local metadata used in wrong function",
524 &MD, Op);
525 }
526}
527
Daniel Dunbar8dd938e2013-01-15 01:22:53 +0000528void Verifier::visitModuleFlags(Module &M) {
529 const NamedMDNode *Flags = M.getModuleFlagsMetadata();
530 if (!Flags) return;
531
532 // Scan each flag.
533 SmallSetVector<MDString*, 16> SeenIDs;
534 for (unsigned I = 0, E = Flags->getNumOperands(); I != E; ++I) {
535 visitModuleFlag(Flags->getOperand(I), SeenIDs);
536 }
537}
538
539void Verifier::visitModuleFlag(MDNode *Op,
540 SmallSetVector<MDString*, 16> &SeenIDs) {
541 // Each module flag should have three arguments, the merge behavior (a
542 // constant int), the flag ID (an MDString), and the value.
543 Assert1(Op->getNumOperands() == 3,
544 "incorrect number of operands in module flag", Op);
545 ConstantInt *Behavior = dyn_cast<ConstantInt>(Op->getOperand(0));
546 MDString *ID = dyn_cast<MDString>(Op->getOperand(1));
547 Assert1(Behavior,
548 "invalid behavior operand in module flag (expected constant integer)",
549 Op->getOperand(0));
550 unsigned BehaviorValue = Behavior->getZExtValue();
551 Assert1((Module::Error <= BehaviorValue &&
552 BehaviorValue <= Module::Override),
553 "invalid behavior operand in module flag (unexpected constant)",
554 Op->getOperand(0));
555 Assert1(ID,
556 "invalid ID operand in module flag (expected metadata string)",
557 Op->getOperand(1));
558
559 // Unless this is a "requires" flag, check the ID is unique.
560 if (BehaviorValue != Module::Require) {
561 Assert1(SeenIDs.insert(ID),
562 "module flag identifiers must be unique (or of 'require' type)",
563 ID);
564 }
565
566 // If this is a "requires" flag, sanity check the value.
567 if (BehaviorValue == Module::Require) {
568 // The value should itself be an MDNode with two operands, a flag ID (an
569 // MDString), and a value.
570 MDNode *Value = dyn_cast<MDNode>(Op->getOperand(2));
571 Assert1(Value && Value->getNumOperands() == 2,
572 "invalid value for 'require' module flag (expected metadata pair)",
573 Op->getOperand(2));
574 Assert1(isa<MDString>(Value->getOperand(0)),
575 ("invalid value for 'require' module flag "
576 "(first value operand should be a string)"),
577 Value->getOperand(0));
578 }
579}
580
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000581// VerifyParameterAttrs - Check the given attributes for an argument or return
Duncan Sandscfad1b42008-01-12 16:42:01 +0000582// value of the specified type. The value V is printed in error messages.
Bill Wendling034b94b2012-12-19 07:18:57 +0000583void Verifier::VerifyParameterAttrs(Attribute Attrs, Type *Ty,
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000584 bool isReturnValue, const Value *V) {
Bill Wendling1fddc9d2012-10-05 06:18:50 +0000585 if (!Attrs.hasAttributes())
Duncan Sandscfad1b42008-01-12 16:42:01 +0000586 return;
587
Bill Wendling6424a782012-12-19 09:15:11 +0000588 Assert1(!Attrs.hasAttribute(Attribute::NoReturn) &&
589 !Attrs.hasAttribute(Attribute::NoUnwind) &&
590 !Attrs.hasAttribute(Attribute::ReadNone) &&
591 !Attrs.hasAttribute(Attribute::ReadOnly) &&
592 !Attrs.hasAttribute(Attribute::NoInline) &&
593 !Attrs.hasAttribute(Attribute::AlwaysInline) &&
594 !Attrs.hasAttribute(Attribute::OptimizeForSize) &&
595 !Attrs.hasAttribute(Attribute::StackProtect) &&
596 !Attrs.hasAttribute(Attribute::StackProtectReq) &&
597 !Attrs.hasAttribute(Attribute::NoRedZone) &&
598 !Attrs.hasAttribute(Attribute::NoImplicitFloat) &&
599 !Attrs.hasAttribute(Attribute::Naked) &&
600 !Attrs.hasAttribute(Attribute::InlineHint) &&
601 !Attrs.hasAttribute(Attribute::StackAlignment) &&
602 !Attrs.hasAttribute(Attribute::UWTable) &&
603 !Attrs.hasAttribute(Attribute::NonLazyBind) &&
604 !Attrs.hasAttribute(Attribute::ReturnsTwice) &&
605 !Attrs.hasAttribute(Attribute::AddressSafety) &&
606 !Attrs.hasAttribute(Attribute::MinSize),
Bill Wendling3a106e62012-10-09 19:01:18 +0000607 "Some attributes in '" + Attrs.getAsString() +
608 "' only apply to functions!", V);
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000609
Bill Wendling943c2912012-10-09 09:51:10 +0000610 if (isReturnValue)
Bill Wendling5d122b62012-12-19 09:04:58 +0000611 Assert1(!Attrs.hasAttribute(Attribute::ByVal) &&
612 !Attrs.hasAttribute(Attribute::Nest) &&
613 !Attrs.hasAttribute(Attribute::StructRet) &&
614 !Attrs.hasAttribute(Attribute::NoCapture),
Bill Wendling034b94b2012-12-19 07:18:57 +0000615 "Attribute 'byval', 'nest', 'sret', and 'nocapture' "
Bill Wendling943c2912012-10-09 09:51:10 +0000616 "do not apply to return values!", V);
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000617
Bill Wendling28d1c602012-10-09 20:11:19 +0000618 // Check for mutually incompatible attributes.
Bill Wendling034b94b2012-12-19 07:18:57 +0000619 Assert1(!((Attrs.hasAttribute(Attribute::ByVal) &&
620 Attrs.hasAttribute(Attribute::Nest)) ||
621 (Attrs.hasAttribute(Attribute::ByVal) &&
622 Attrs.hasAttribute(Attribute::StructRet)) ||
623 (Attrs.hasAttribute(Attribute::Nest) &&
624 Attrs.hasAttribute(Attribute::StructRet))), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000625 "'byval, nest, and sret' are incompatible!", V);
626
Bill Wendling034b94b2012-12-19 07:18:57 +0000627 Assert1(!((Attrs.hasAttribute(Attribute::ByVal) &&
628 Attrs.hasAttribute(Attribute::Nest)) ||
629 (Attrs.hasAttribute(Attribute::ByVal) &&
630 Attrs.hasAttribute(Attribute::InReg)) ||
631 (Attrs.hasAttribute(Attribute::Nest) &&
632 Attrs.hasAttribute(Attribute::InReg))), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000633 "'byval, nest, and inreg' are incompatible!", V);
634
Bill Wendling034b94b2012-12-19 07:18:57 +0000635 Assert1(!(Attrs.hasAttribute(Attribute::ZExt) &&
636 Attrs.hasAttribute(Attribute::SExt)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000637 "'zeroext and signext' are incompatible!", V);
638
Bill Wendling034b94b2012-12-19 07:18:57 +0000639 Assert1(!(Attrs.hasAttribute(Attribute::ReadNone) &&
640 Attrs.hasAttribute(Attribute::ReadOnly)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000641 "'readnone and readonly' are incompatible!", V);
642
Bill Wendling034b94b2012-12-19 07:18:57 +0000643 Assert1(!(Attrs.hasAttribute(Attribute::NoInline) &&
644 Attrs.hasAttribute(Attribute::AlwaysInline)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000645 "'noinline and alwaysinline' are incompatible!", V);
Duncan Sandscfad1b42008-01-12 16:42:01 +0000646
Bill Wendling702cc912012-10-15 20:35:56 +0000647 Assert1(!AttrBuilder(Attrs).
Bill Wendling034b94b2012-12-19 07:18:57 +0000648 hasAttributes(Attribute::typeIncompatible(Ty)),
Bill Wendling1feacad2012-10-14 07:52:48 +0000649 "Wrong types for attribute: " +
Bill Wendling034b94b2012-12-19 07:18:57 +0000650 Attribute::typeIncompatible(Ty).getAsString(), V);
Dan Gohman39dfc2c2008-08-27 14:48:06 +0000651
Bill Wendling943c2912012-10-09 09:51:10 +0000652 if (PointerType *PTy = dyn_cast<PointerType>(Ty))
Bill Wendling034b94b2012-12-19 07:18:57 +0000653 Assert1(!Attrs.hasAttribute(Attribute::ByVal) ||
Bill Wendling943c2912012-10-09 09:51:10 +0000654 PTy->getElementType()->isSized(),
655 "Attribute 'byval' does not support unsized types!", V);
656 else
Bill Wendling034b94b2012-12-19 07:18:57 +0000657 Assert1(!Attrs.hasAttribute(Attribute::ByVal),
Bill Wendling943c2912012-10-09 09:51:10 +0000658 "Attribute 'byval' only applies to parameters with pointer type!",
659 V);
Duncan Sandscfad1b42008-01-12 16:42:01 +0000660}
661
662// VerifyFunctionAttrs - Check parameter attributes against a function type.
Duncan Sandsd9d70392007-12-21 19:19:01 +0000663// The value V is printed in error messages.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000664void Verifier::VerifyFunctionAttrs(FunctionType *FT,
Bill Wendling99faa3b2012-12-07 23:16:57 +0000665 const AttributeSet &Attrs,
Duncan Sandscfad1b42008-01-12 16:42:01 +0000666 const Value *V) {
Chris Lattner58d74912008-03-12 17:45:29 +0000667 if (Attrs.isEmpty())
Duncan Sandsd9d70392007-12-21 19:19:01 +0000668 return;
669
Duncan Sandsd9d70392007-12-21 19:19:01 +0000670 bool SawNest = false;
671
Chris Lattner58d74912008-03-12 17:45:29 +0000672 for (unsigned i = 0, e = Attrs.getNumSlots(); i != e; ++i) {
Devang Patel05988662008-09-25 21:00:45 +0000673 const AttributeWithIndex &Attr = Attrs.getSlot(i);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000674
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000675 Type *Ty;
Chris Lattner58d74912008-03-12 17:45:29 +0000676 if (Attr.Index == 0)
677 Ty = FT->getReturnType();
678 else if (Attr.Index-1 < FT->getNumParams())
679 Ty = FT->getParamType(Attr.Index-1);
680 else
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000681 break; // VarArgs attributes, verified elsewhere.
682
683 VerifyParameterAttrs(Attr.Attrs, Ty, Attr.Index == 0, V);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000684
Bill Wendling034b94b2012-12-19 07:18:57 +0000685 if (Attr.Attrs.hasAttribute(Attribute::Nest)) {
Duncan Sandsd9d70392007-12-21 19:19:01 +0000686 Assert1(!SawNest, "More than one parameter has attribute nest!", V);
687 SawNest = true;
688 }
689
Bill Wendling034b94b2012-12-19 07:18:57 +0000690 if (Attr.Attrs.hasAttribute(Attribute::StructRet))
691 Assert1(Attr.Index == 1, "Attribute sret is not on first parameter!", V);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000692 }
Devang Patel7c310852008-10-01 23:41:25 +0000693
Bill Wendling034b94b2012-12-19 07:18:57 +0000694 Attribute FAttrs = Attrs.getFnAttributes();
Bill Wendling702cc912012-10-15 20:35:56 +0000695 AttrBuilder NotFn(FAttrs);
Bill Wendling3a106e62012-10-09 19:01:18 +0000696 NotFn.removeFunctionOnlyAttrs();
Bill Wendling034b94b2012-12-19 07:18:57 +0000697 Assert1(!NotFn.hasAttributes(), "Attribute '" +
698 Attribute::get(V->getContext(), NotFn).getAsString() +
Bill Wendling3a106e62012-10-09 19:01:18 +0000699 "' do not apply to the function!", V);
Duncan Sandsd6de30c2009-06-11 08:11:03 +0000700
Bill Wendling28d1c602012-10-09 20:11:19 +0000701 // Check for mutually incompatible attributes.
Bill Wendling034b94b2012-12-19 07:18:57 +0000702 Assert1(!((FAttrs.hasAttribute(Attribute::ByVal) &&
703 FAttrs.hasAttribute(Attribute::Nest)) ||
704 (FAttrs.hasAttribute(Attribute::ByVal) &&
705 FAttrs.hasAttribute(Attribute::StructRet)) ||
706 (FAttrs.hasAttribute(Attribute::Nest) &&
707 FAttrs.hasAttribute(Attribute::StructRet))), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000708 "'byval, nest, and sret' are incompatible!", V);
709
Bill Wendling034b94b2012-12-19 07:18:57 +0000710 Assert1(!((FAttrs.hasAttribute(Attribute::ByVal) &&
711 FAttrs.hasAttribute(Attribute::Nest)) ||
712 (FAttrs.hasAttribute(Attribute::ByVal) &&
713 FAttrs.hasAttribute(Attribute::InReg)) ||
714 (FAttrs.hasAttribute(Attribute::Nest) &&
715 FAttrs.hasAttribute(Attribute::InReg))), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000716 "'byval, nest, and inreg' are incompatible!", V);
717
Bill Wendling034b94b2012-12-19 07:18:57 +0000718 Assert1(!(FAttrs.hasAttribute(Attribute::ZExt) &&
719 FAttrs.hasAttribute(Attribute::SExt)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000720 "'zeroext and signext' are incompatible!", V);
721
Bill Wendling034b94b2012-12-19 07:18:57 +0000722 Assert1(!(FAttrs.hasAttribute(Attribute::ReadNone) &&
723 FAttrs.hasAttribute(Attribute::ReadOnly)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000724 "'readnone and readonly' are incompatible!", V);
725
Bill Wendling034b94b2012-12-19 07:18:57 +0000726 Assert1(!(FAttrs.hasAttribute(Attribute::NoInline) &&
727 FAttrs.hasAttribute(Attribute::AlwaysInline)), "Attributes "
Bill Wendling28d1c602012-10-09 20:11:19 +0000728 "'noinline and alwaysinline' are incompatible!", V);
Duncan Sandsd9d70392007-12-21 19:19:01 +0000729}
730
Bill Wendling99faa3b2012-12-07 23:16:57 +0000731static bool VerifyAttributeCount(const AttributeSet &Attrs, unsigned Params) {
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000732 if (Attrs.isEmpty())
733 return true;
Nick Lewycky29ef6592009-09-07 20:44:51 +0000734
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000735 unsigned LastSlot = Attrs.getNumSlots() - 1;
736 unsigned LastIndex = Attrs.getSlot(LastSlot).Index;
737 if (LastIndex <= Params
738 || (LastIndex == (unsigned)~0
739 && (LastSlot == 0 || Attrs.getSlot(LastSlot - 1).Index <= Params)))
740 return true;
Nick Lewycky29ef6592009-09-07 20:44:51 +0000741
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000742 return false;
743}
Nick Lewycky29ef6592009-09-07 20:44:51 +0000744
Chris Lattnerd231fc32002-04-18 20:37:37 +0000745// visitFunction - Verify that a function is ok.
Chris Lattner44d5bd92002-02-20 17:55:43 +0000746//
Chris Lattner24e845f2002-06-25 15:56:27 +0000747void Verifier::visitFunction(Function &F) {
Chris Lattner37c121a2005-05-08 22:27:09 +0000748 // Check function arguments.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000749 FunctionType *FT = F.getFunctionType();
Chris Lattner453eed12007-08-18 06:13:19 +0000750 unsigned NumArgs = F.arg_size();
Chris Lattnerea249242002-04-13 22:48:46 +0000751
Nick Lewycky6e5a2bd2010-02-15 21:52:04 +0000752 Assert1(Context == &F.getContext(),
753 "Function context does not match Module context!", &F);
754
Chris Lattner26d054d2009-08-05 05:21:07 +0000755 Assert1(!F.hasCommonLinkage(), "Functions may not have common linkage", &F);
Chris Lattner69da5cf2002-10-13 20:57:00 +0000756 Assert2(FT->getNumParams() == NumArgs,
Chris Lattnerea249242002-04-13 22:48:46 +0000757 "# formal arguments must match # of arguments for function type!",
Chris Lattner24e845f2002-06-25 15:56:27 +0000758 &F, FT);
Chris Lattnerc282f5a2003-11-21 22:32:23 +0000759 Assert1(F.getReturnType()->isFirstClassType() ||
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000760 F.getReturnType()->isVoidTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +0000761 F.getReturnType()->isStructTy(),
Chris Lattnerc282f5a2003-11-21 22:32:23 +0000762 "Functions cannot return aggregate values!", &F);
Chris Lattnerea249242002-04-13 22:48:46 +0000763
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000764 Assert1(!F.hasStructRetAttr() || F.getReturnType()->isVoidTy(),
Devang Patel41e23972008-03-03 21:46:28 +0000765 "Invalid struct return type!", &F);
766
Bill Wendling99faa3b2012-12-07 23:16:57 +0000767 const AttributeSet &Attrs = F.getAttributes();
Duncan Sands623a3892008-01-11 22:36:48 +0000768
Devang Pateld9b4a5f2008-09-23 22:35:17 +0000769 Assert1(VerifyAttributeCount(Attrs, FT->getNumParams()),
Bill Wendling034b94b2012-12-19 07:18:57 +0000770 "Attribute after last parameter!", &F);
Duncan Sands623a3892008-01-11 22:36:48 +0000771
Duncan Sandsd9d70392007-12-21 19:19:01 +0000772 // Check function attributes.
Duncan Sandscfad1b42008-01-12 16:42:01 +0000773 VerifyFunctionAttrs(FT, Attrs, &F);
Duncan Sandsfdef00f2007-07-27 15:09:54 +0000774
Chris Lattner80105dd2006-05-19 21:25:17 +0000775 // Check that this function meets the restrictions on this calling convention.
776 switch (F.getCallingConv()) {
777 default:
778 break;
779 case CallingConv::C:
780 break;
Chris Lattner80105dd2006-05-19 21:25:17 +0000781 case CallingConv::Fast:
782 case CallingConv::Cold:
Anton Korobeynikovbcb97702006-09-17 20:25:45 +0000783 case CallingConv::X86_FastCall:
Anton Korobeynikovded05e32010-05-16 09:08:45 +0000784 case CallingConv::X86_ThisCall:
Elena Demikhovsky35752222012-10-24 14:46:16 +0000785 case CallingConv::Intel_OCL_BI:
Che-Liang Chiouf9930da2010-09-25 07:46:17 +0000786 case CallingConv::PTX_Kernel:
787 case CallingConv::PTX_Device:
Chris Lattner80105dd2006-05-19 21:25:17 +0000788 Assert1(!F.isVarArg(),
789 "Varargs functions must have C calling conventions!", &F);
790 break;
791 }
Nick Lewycky29ef6592009-09-07 20:44:51 +0000792
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000793 bool isLLVMdotName = F.getName().size() >= 5 &&
794 F.getName().substr(0, 5) == "llvm.";
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000795
Chris Lattnerea249242002-04-13 22:48:46 +0000796 // Check that the argument values match the function type for this function...
Chris Lattner69da5cf2002-10-13 20:57:00 +0000797 unsigned i = 0;
Chris Lattnere3cbe032006-12-16 02:25:35 +0000798 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
799 I != E; ++I, ++i) {
Chris Lattner69da5cf2002-10-13 20:57:00 +0000800 Assert2(I->getType() == FT->getParamType(i),
801 "Argument value does not match function argument type!",
802 I, FT->getParamType(i));
Misha Brukmanfd939082005-04-21 23:48:37 +0000803 Assert1(I->getType()->isFirstClassType(),
Dan Gohman9f50eee2008-08-27 14:44:57 +0000804 "Function arguments must have first-class types!", I);
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000805 if (!isLLVMdotName)
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000806 Assert2(!I->getType()->isMetadataTy(),
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000807 "Function takes metadata but isn't an intrinsic", I, &F);
Dan Gohman9f50eee2008-08-27 14:44:57 +0000808 }
Chris Lattnerea249242002-04-13 22:48:46 +0000809
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000810 if (F.isMaterializable()) {
811 // Function has a body somewhere we can't see.
812 } else if (F.isDeclaration()) {
Chris Lattner6693da02007-09-19 17:14:45 +0000813 Assert1(F.hasExternalLinkage() || F.hasDLLImportLinkage() ||
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +0000814 F.hasExternalWeakLinkage(),
Chris Lattner6693da02007-09-19 17:14:45 +0000815 "invalid linkage type for function declaration", &F);
816 } else {
Chris Lattner4d17caa2006-12-13 04:45:46 +0000817 // Verify that this function (which has a body) is not named "llvm.*". It
818 // is not legal to define intrinsics.
Nick Lewycky7a0370f2009-05-30 05:06:04 +0000819 Assert1(!isLLVMdotName, "llvm intrinsics cannot be defined!", &F);
Chris Lattner4d17caa2006-12-13 04:45:46 +0000820
Chris Lattner69da5cf2002-10-13 20:57:00 +0000821 // Check the entry node
Chris Lattner02a3be02003-09-20 14:39:18 +0000822 BasicBlock *Entry = &F.getEntryBlock();
Chris Lattner69da5cf2002-10-13 20:57:00 +0000823 Assert1(pred_begin(Entry) == pred_end(Entry),
824 "Entry block to function must not have predecessors!", Entry);
Chris Lattner660a4f32009-11-01 04:08:01 +0000825
826 // The address of the entry block cannot be taken, unless it is dead.
827 if (Entry->hasAddressTaken()) {
Chris Lattner4a7642e2009-11-01 18:11:50 +0000828 Assert1(!BlockAddress::get(Entry)->isConstantUsed(),
Chris Lattner660a4f32009-11-01 04:08:01 +0000829 "blockaddress may not be used with the entry block!", Entry);
830 }
Chris Lattner69da5cf2002-10-13 20:57:00 +0000831 }
Gabor Greifc9f75002010-03-24 13:21:49 +0000832
Chris Lattner13202de2009-09-11 17:05:29 +0000833 // If this function is actually an intrinsic, verify that it is only used in
834 // direct call/invokes, never having its "address taken".
835 if (F.getIntrinsicID()) {
Gabor Greifc9f75002010-03-24 13:21:49 +0000836 const User *U;
837 if (F.hasAddressTaken(&U))
Chris Lattner13202de2009-09-11 17:05:29 +0000838 Assert1(0, "Invalid user of intrinsic instruction!", U);
Chris Lattner13202de2009-09-11 17:05:29 +0000839 }
Chris Lattner44d5bd92002-02-20 17:55:43 +0000840}
841
Chris Lattnerd231fc32002-04-18 20:37:37 +0000842// verifyBasicBlock - Verify that a basic block is well formed...
843//
Chris Lattner24e845f2002-06-25 15:56:27 +0000844void Verifier::visitBasicBlock(BasicBlock &BB) {
Chris Lattnera7b1c7e2004-09-29 20:07:45 +0000845 InstsInThisBlock.clear();
846
Alkis Evlogimenos4f4cf992004-12-04 02:30:42 +0000847 // Ensure that basic blocks have terminators!
848 Assert1(BB.getTerminator(), "Basic Block does not have terminator!", &BB);
849
Chris Lattnerbede31f2003-10-05 17:44:18 +0000850 // Check constraints that this basic block imposes on all of the PHI nodes in
851 // it.
852 if (isa<PHINode>(BB.front())) {
Chris Lattnerf8edb622007-02-10 08:33:11 +0000853 SmallVector<BasicBlock*, 8> Preds(pred_begin(&BB), pred_end(&BB));
854 SmallVector<std::pair<BasicBlock*, Value*>, 8> Values;
Chris Lattnerbede31f2003-10-05 17:44:18 +0000855 std::sort(Preds.begin(), Preds.end());
Misha Brukmanfd939082005-04-21 23:48:37 +0000856 PHINode *PN;
Chris Lattnerc70a5092004-06-05 17:44:48 +0000857 for (BasicBlock::iterator I = BB.begin(); (PN = dyn_cast<PHINode>(I));++I) {
Chris Lattnerbede31f2003-10-05 17:44:18 +0000858 // Ensure that PHI nodes have at least one entry!
859 Assert1(PN->getNumIncomingValues() != 0,
860 "PHI nodes must have at least one entry. If the block is dead, "
861 "the PHI should be removed!", PN);
Brian Gaeke2fea9ad2004-05-17 21:15:18 +0000862 Assert1(PN->getNumIncomingValues() == Preds.size(),
863 "PHINode should have one entry for each predecessor of its "
864 "parent basic block!", PN);
Misha Brukmanfd939082005-04-21 23:48:37 +0000865
Chris Lattnerbede31f2003-10-05 17:44:18 +0000866 // Get and sort all incoming values in the PHI node...
Chris Lattnerf8edb622007-02-10 08:33:11 +0000867 Values.clear();
Chris Lattnerbede31f2003-10-05 17:44:18 +0000868 Values.reserve(PN->getNumIncomingValues());
869 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
870 Values.push_back(std::make_pair(PN->getIncomingBlock(i),
871 PN->getIncomingValue(i)));
872 std::sort(Values.begin(), Values.end());
Misha Brukmanfd939082005-04-21 23:48:37 +0000873
Chris Lattnerbede31f2003-10-05 17:44:18 +0000874 for (unsigned i = 0, e = Values.size(); i != e; ++i) {
875 // Check to make sure that if there is more than one entry for a
876 // particular basic block in this PHI node, that the incoming values are
877 // all identical.
878 //
879 Assert4(i == 0 || Values[i].first != Values[i-1].first ||
880 Values[i].second == Values[i-1].second,
881 "PHI node has multiple entries for the same basic block with "
882 "different incoming values!", PN, Values[i].first,
883 Values[i].second, Values[i-1].second);
Misha Brukmanfd939082005-04-21 23:48:37 +0000884
Chris Lattnerbede31f2003-10-05 17:44:18 +0000885 // Check to make sure that the predecessors and PHI node entries are
886 // matched up.
887 Assert3(Values[i].first == Preds[i],
888 "PHI node entries do not match predecessors!", PN,
Misha Brukmanfd939082005-04-21 23:48:37 +0000889 Values[i].first, Preds[i]);
Chris Lattnerbede31f2003-10-05 17:44:18 +0000890 }
891 }
892 }
Chris Lattner24e845f2002-06-25 15:56:27 +0000893}
Chris Lattneracd3cae2002-03-15 20:25:09 +0000894
Chris Lattner24e845f2002-06-25 15:56:27 +0000895void Verifier::visitTerminatorInst(TerminatorInst &I) {
896 // Ensure that terminators only exist at the end of the basic block.
897 Assert1(&I == I.getParent()->getTerminator(),
898 "Terminator found in the middle of a basic block!", I.getParent());
Chris Lattner3535c9b2002-07-18 00:13:42 +0000899 visitInstruction(I);
Chris Lattner24e845f2002-06-25 15:56:27 +0000900}
901
Nick Lewycky6a7cb632010-02-15 22:09:09 +0000902void Verifier::visitBranchInst(BranchInst &BI) {
903 if (BI.isConditional()) {
904 Assert2(BI.getCondition()->getType()->isIntegerTy(1),
905 "Branch condition is not 'i1' type!", &BI, BI.getCondition());
906 }
907 visitTerminatorInst(BI);
908}
909
Chris Lattner24e845f2002-06-25 15:56:27 +0000910void Verifier::visitReturnInst(ReturnInst &RI) {
911 Function *F = RI.getParent()->getParent();
Devang Patel57ef4f42008-02-23 00:35:18 +0000912 unsigned N = RI.getNumOperands();
Chris Lattnercf0fe8d2009-10-05 05:54:46 +0000913 if (F->getReturnType()->isVoidTy())
Chris Lattner80b8f5d2008-04-23 20:33:41 +0000914 Assert2(N == 0,
Nick Lewycky70c44f02008-11-15 17:50:47 +0000915 "Found return instr that returns non-void in Function of void "
Alkis Evlogimenos8b42b432004-12-04 01:25:06 +0000916 "return type!", &RI, F->getReturnType());
Jay Foad3e2f74e2011-04-04 07:44:02 +0000917 else
918 Assert2(N == 1 && F->getReturnType() == RI.getOperand(0)->getType(),
919 "Function return type does not match operand "
920 "type of return inst!", &RI, F->getReturnType());
Nick Lewycky29ef6592009-09-07 20:44:51 +0000921
Misha Brukman5560c9d2003-08-18 14:43:39 +0000922 // Check to make sure that the return value has necessary properties for
Chris Lattner24e845f2002-06-25 15:56:27 +0000923 // terminators...
924 visitTerminatorInst(RI);
Chris Lattner44d5bd92002-02-20 17:55:43 +0000925}
926
Chris Lattner0f9e9d02004-05-21 16:47:21 +0000927void Verifier::visitSwitchInst(SwitchInst &SI) {
928 // Check to make sure that all of the constants in the switch instruction
929 // have the same type as the switched-on value.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000930 Type *SwitchTy = SI.getCondition()->getType();
Stepan Dyatkovskiy484fc932012-05-28 12:39:09 +0000931 IntegerType *IntTy = cast<IntegerType>(SwitchTy);
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +0000932 IntegersSubsetToBB Mapping;
933 std::map<IntegersSubset::Range, unsigned> RangeSetMap;
Stepan Dyatkovskiy3d3abe02012-03-11 06:09:17 +0000934 for (SwitchInst::CaseIt i = SI.case_begin(), e = SI.case_end(); i != e; ++i) {
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +0000935 IntegersSubset CaseRanges = i.getCaseValueEx();
936 for (unsigned ri = 0, rie = CaseRanges.getNumItems(); ri < rie; ++ri) {
937 IntegersSubset::Range r = CaseRanges.getItem(ri);
Stepan Dyatkovskiy43eb31b2012-06-02 09:42:43 +0000938 Assert1(((const APInt&)r.getLow()).getBitWidth() == IntTy->getBitWidth(),
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +0000939 "Switch constants must all be same type as switch value!", &SI);
Stepan Dyatkovskiy43eb31b2012-06-02 09:42:43 +0000940 Assert1(((const APInt&)r.getHigh()).getBitWidth() == IntTy->getBitWidth(),
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +0000941 "Switch constants must all be same type as switch value!", &SI);
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +0000942 Mapping.add(r);
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +0000943 RangeSetMap[r] = i.getCaseIndex();
944 }
Chris Lattnerd682a602009-11-11 17:37:02 +0000945 }
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +0000946
Stepan Dyatkovskiy0aa32d52012-05-29 12:26:47 +0000947 IntegersSubsetToBB::RangeIterator errItem;
948 if (!Mapping.verify(errItem)) {
Stepan Dyatkovskiy1c8f4b82012-05-21 10:44:40 +0000949 unsigned CaseIndex = RangeSetMap[errItem->first];
950 SwitchInst::CaseIt i(&SI, CaseIndex);
951 Assert2(false, "Duplicate integer as switch case", &SI, i.getCaseValueEx());
952 }
953
Chris Lattner0f9e9d02004-05-21 16:47:21 +0000954 visitTerminatorInst(SI);
955}
956
Dan Gohman37680912010-08-02 23:08:33 +0000957void Verifier::visitIndirectBrInst(IndirectBrInst &BI) {
958 Assert1(BI.getAddress()->getType()->isPointerTy(),
959 "Indirectbr operand must have pointer type!", &BI);
960 for (unsigned i = 0, e = BI.getNumDestinations(); i != e; ++i)
961 Assert1(BI.getDestination(i)->getType()->isLabelTy(),
962 "Indirectbr destinations must all have pointer type!", &BI);
963
964 visitTerminatorInst(BI);
965}
966
Chris Lattner230c1a72004-03-12 05:54:31 +0000967void Verifier::visitSelectInst(SelectInst &SI) {
Chris Lattnerb76ec322008-12-29 00:12:50 +0000968 Assert1(!SelectInst::areInvalidOperands(SI.getOperand(0), SI.getOperand(1),
969 SI.getOperand(2)),
970 "Invalid operands for select instruction!", &SI);
971
Chris Lattner230c1a72004-03-12 05:54:31 +0000972 Assert1(SI.getTrueValue()->getType() == SI.getType(),
973 "Select values must have same type as select instruction!", &SI);
Chris Lattner0030e6c2004-09-29 21:19:28 +0000974 visitInstruction(SI);
Chris Lattner230c1a72004-03-12 05:54:31 +0000975}
976
Misha Brukmanab5c6002004-03-02 00:22:19 +0000977/// visitUserOp1 - User defined operators shouldn't live beyond the lifetime of
978/// a pass, if any exist, it's an error.
979///
Chris Lattner627079d2002-11-21 16:54:22 +0000980void Verifier::visitUserOp1(Instruction &I) {
Chris Lattner536a9d52006-03-31 04:46:47 +0000981 Assert1(0, "User-defined operators should not live outside of a pass!", &I);
Chris Lattner627079d2002-11-21 16:54:22 +0000982}
Chris Lattnerd231fc32002-04-18 20:37:37 +0000983
Reid Spencer3da59db2006-11-27 01:05:10 +0000984void Verifier::visitTruncInst(TruncInst &I) {
985 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000986 Type *SrcTy = I.getOperand(0)->getType();
987 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +0000988
989 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +0000990 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
991 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +0000992
Duncan Sandsb0bc6c32010-02-15 16:12:20 +0000993 Assert1(SrcTy->isIntOrIntVectorTy(), "Trunc only operates on integer", &I);
994 Assert1(DestTy->isIntOrIntVectorTy(), "Trunc only produces integer", &I);
Duncan Sands1df98592010-02-16 11:11:14 +0000995 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +0000996 "trunc source and destination must both be a vector or neither", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +0000997 Assert1(SrcBitSize > DestBitSize,"DestTy too big for Trunc", &I);
998
999 visitInstruction(I);
1000}
1001
1002void Verifier::visitZExtInst(ZExtInst &I) {
1003 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001004 Type *SrcTy = I.getOperand(0)->getType();
1005 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001006
1007 // Get the size of the types in bits, we'll need this later
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001008 Assert1(SrcTy->isIntOrIntVectorTy(), "ZExt only operates on integer", &I);
1009 Assert1(DestTy->isIntOrIntVectorTy(), "ZExt only produces an integer", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001010 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001011 "zext source and destination must both be a vector or neither", &I);
Dan Gohman6de29f82009-06-15 22:12:54 +00001012 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1013 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001014
Reid Spencer3da59db2006-11-27 01:05:10 +00001015 Assert1(SrcBitSize < DestBitSize,"Type too small for ZExt", &I);
1016
1017 visitInstruction(I);
1018}
1019
1020void Verifier::visitSExtInst(SExtInst &I) {
1021 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001022 Type *SrcTy = I.getOperand(0)->getType();
1023 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001024
1025 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001026 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1027 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001028
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001029 Assert1(SrcTy->isIntOrIntVectorTy(), "SExt only operates on integer", &I);
1030 Assert1(DestTy->isIntOrIntVectorTy(), "SExt only produces an integer", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001031 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001032 "sext source and destination must both be a vector or neither", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001033 Assert1(SrcBitSize < DestBitSize,"Type too small for SExt", &I);
1034
1035 visitInstruction(I);
1036}
1037
1038void Verifier::visitFPTruncInst(FPTruncInst &I) {
1039 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001040 Type *SrcTy = I.getOperand(0)->getType();
1041 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001042 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001043 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1044 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001045
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001046 Assert1(SrcTy->isFPOrFPVectorTy(),"FPTrunc only operates on FP", &I);
1047 Assert1(DestTy->isFPOrFPVectorTy(),"FPTrunc only produces an FP", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001048 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001049 "fptrunc source and destination must both be a vector or neither",&I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001050 Assert1(SrcBitSize > DestBitSize,"DestTy too big for FPTrunc", &I);
1051
1052 visitInstruction(I);
1053}
1054
1055void Verifier::visitFPExtInst(FPExtInst &I) {
1056 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001057 Type *SrcTy = I.getOperand(0)->getType();
1058 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001059
1060 // Get the size of the types in bits, we'll need this later
Dan Gohman6de29f82009-06-15 22:12:54 +00001061 unsigned SrcBitSize = SrcTy->getScalarSizeInBits();
1062 unsigned DestBitSize = DestTy->getScalarSizeInBits();
Reid Spencer3da59db2006-11-27 01:05:10 +00001063
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001064 Assert1(SrcTy->isFPOrFPVectorTy(),"FPExt only operates on FP", &I);
1065 Assert1(DestTy->isFPOrFPVectorTy(),"FPExt only produces an FP", &I);
Duncan Sands1df98592010-02-16 11:11:14 +00001066 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
Chris Lattner585c51e2009-02-02 07:40:17 +00001067 "fpext source and destination must both be a vector or neither", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001068 Assert1(SrcBitSize < DestBitSize,"DestTy too small for FPExt", &I);
1069
1070 visitInstruction(I);
1071}
1072
1073void Verifier::visitUIToFPInst(UIToFPInst &I) {
1074 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001075 Type *SrcTy = I.getOperand(0)->getType();
1076 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001077
Duncan Sands1df98592010-02-16 11:11:14 +00001078 bool SrcVec = SrcTy->isVectorTy();
1079 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001080
Chris Lattner58d74912008-03-12 17:45:29 +00001081 Assert1(SrcVec == DstVec,
1082 "UIToFP source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001083 Assert1(SrcTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001084 "UIToFP source must be integer or integer vector", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001085 Assert1(DestTy->isFPOrFPVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001086 "UIToFP result must be FP or FP vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001087
1088 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001089 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1090 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001091 "UIToFP source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001092
1093 visitInstruction(I);
1094}
1095
1096void Verifier::visitSIToFPInst(SIToFPInst &I) {
1097 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001098 Type *SrcTy = I.getOperand(0)->getType();
1099 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001100
Duncan Sands1df98592010-02-16 11:11:14 +00001101 bool SrcVec = SrcTy->isVectorTy();
1102 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001103
Chris Lattner58d74912008-03-12 17:45:29 +00001104 Assert1(SrcVec == DstVec,
1105 "SIToFP source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001106 Assert1(SrcTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001107 "SIToFP source must be integer or integer vector", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001108 Assert1(DestTy->isFPOrFPVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001109 "SIToFP result must be FP or FP vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001110
1111 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001112 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1113 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001114 "SIToFP source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001115
1116 visitInstruction(I);
1117}
1118
1119void Verifier::visitFPToUIInst(FPToUIInst &I) {
1120 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001121 Type *SrcTy = I.getOperand(0)->getType();
1122 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001123
Duncan Sands1df98592010-02-16 11:11:14 +00001124 bool SrcVec = SrcTy->isVectorTy();
1125 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001126
Chris Lattner58d74912008-03-12 17:45:29 +00001127 Assert1(SrcVec == DstVec,
1128 "FPToUI source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001129 Assert1(SrcTy->isFPOrFPVectorTy(), "FPToUI source must be FP or FP vector",
1130 &I);
1131 Assert1(DestTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001132 "FPToUI result must be integer or integer vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001133
1134 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001135 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1136 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001137 "FPToUI source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001138
1139 visitInstruction(I);
1140}
1141
1142void Verifier::visitFPToSIInst(FPToSIInst &I) {
1143 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001144 Type *SrcTy = I.getOperand(0)->getType();
1145 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001146
Duncan Sands1df98592010-02-16 11:11:14 +00001147 bool SrcVec = SrcTy->isVectorTy();
1148 bool DstVec = DestTy->isVectorTy();
Nate Begemanb348d182007-11-17 03:58:34 +00001149
Chris Lattner58d74912008-03-12 17:45:29 +00001150 Assert1(SrcVec == DstVec,
1151 "FPToSI source and dest must both be vector or scalar", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001152 Assert1(SrcTy->isFPOrFPVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001153 "FPToSI source must be FP or FP vector", &I);
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001154 Assert1(DestTy->isIntOrIntVectorTy(),
Chris Lattner58d74912008-03-12 17:45:29 +00001155 "FPToSI result must be integer or integer vector", &I);
Nate Begemanb348d182007-11-17 03:58:34 +00001156
1157 if (SrcVec && DstVec)
Chris Lattner58d74912008-03-12 17:45:29 +00001158 Assert1(cast<VectorType>(SrcTy)->getNumElements() ==
1159 cast<VectorType>(DestTy)->getNumElements(),
Nate Begemanb348d182007-11-17 03:58:34 +00001160 "FPToSI source and dest vector length mismatch", &I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001161
1162 visitInstruction(I);
1163}
1164
1165void Verifier::visitPtrToIntInst(PtrToIntInst &I) {
1166 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001167 Type *SrcTy = I.getOperand(0)->getType();
1168 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001169
Nadav Rotem16087692011-12-05 06:29:09 +00001170 Assert1(SrcTy->getScalarType()->isPointerTy(),
1171 "PtrToInt source must be pointer", &I);
1172 Assert1(DestTy->getScalarType()->isIntegerTy(),
1173 "PtrToInt result must be integral", &I);
1174 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
1175 "PtrToInt type mismatch", &I);
1176
1177 if (SrcTy->isVectorTy()) {
1178 VectorType *VSrc = dyn_cast<VectorType>(SrcTy);
1179 VectorType *VDest = dyn_cast<VectorType>(DestTy);
1180 Assert1(VSrc->getNumElements() == VDest->getNumElements(),
1181 "PtrToInt Vector width mismatch", &I);
1182 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001183
1184 visitInstruction(I);
1185}
1186
1187void Verifier::visitIntToPtrInst(IntToPtrInst &I) {
1188 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001189 Type *SrcTy = I.getOperand(0)->getType();
1190 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001191
Nadav Rotem16087692011-12-05 06:29:09 +00001192 Assert1(SrcTy->getScalarType()->isIntegerTy(),
1193 "IntToPtr source must be an integral", &I);
1194 Assert1(DestTy->getScalarType()->isPointerTy(),
1195 "IntToPtr result must be a pointer",&I);
1196 Assert1(SrcTy->isVectorTy() == DestTy->isVectorTy(),
1197 "IntToPtr type mismatch", &I);
1198 if (SrcTy->isVectorTy()) {
1199 VectorType *VSrc = dyn_cast<VectorType>(SrcTy);
1200 VectorType *VDest = dyn_cast<VectorType>(DestTy);
1201 Assert1(VSrc->getNumElements() == VDest->getNumElements(),
1202 "IntToPtr Vector width mismatch", &I);
1203 }
Reid Spencer3da59db2006-11-27 01:05:10 +00001204 visitInstruction(I);
1205}
1206
1207void Verifier::visitBitCastInst(BitCastInst &I) {
1208 // Get the source and destination types
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001209 Type *SrcTy = I.getOperand(0)->getType();
1210 Type *DestTy = I.getType();
Reid Spencer3da59db2006-11-27 01:05:10 +00001211
1212 // Get the size of the types in bits, we'll need this later
1213 unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits();
1214 unsigned DestBitSize = DestTy->getPrimitiveSizeInBits();
1215
1216 // BitCast implies a no-op cast of type only. No bits change.
1217 // However, you can't cast pointers to anything but pointers.
Nick Lewyckyc2de3dd2012-08-15 02:37:07 +00001218 Assert1(SrcTy->isPointerTy() == DestTy->isPointerTy(),
Reid Spencer3da59db2006-11-27 01:05:10 +00001219 "Bitcast requires both operands to be pointer or neither", &I);
Nate Begeman55a961f2009-07-30 02:00:06 +00001220 Assert1(SrcBitSize == DestBitSize, "Bitcast requires types of same width",&I);
Reid Spencer3da59db2006-11-27 01:05:10 +00001221
Dan Gohman500233a2008-09-08 16:45:59 +00001222 // Disallow aggregates.
1223 Assert1(!SrcTy->isAggregateType(),
1224 "Bitcast operand must not be aggregate", &I);
1225 Assert1(!DestTy->isAggregateType(),
1226 "Bitcast type must not be aggregate", &I);
1227
Reid Spencer3da59db2006-11-27 01:05:10 +00001228 visitInstruction(I);
1229}
1230
Misha Brukmanab5c6002004-03-02 00:22:19 +00001231/// visitPHINode - Ensure that a PHI node is well formed.
1232///
Chris Lattner24e845f2002-06-25 15:56:27 +00001233void Verifier::visitPHINode(PHINode &PN) {
1234 // Ensure that the PHI nodes are all grouped together at the top of the block.
1235 // This can be tested by checking whether the instruction before this is
Misha Brukman6b634522003-10-10 17:54:14 +00001236 // either nonexistent (because this is begin()) or is a PHI node. If not,
Chris Lattner24e845f2002-06-25 15:56:27 +00001237 // then there is some other instruction before a PHI.
Chris Lattner4d8c16f2007-04-17 17:36:12 +00001238 Assert2(&PN == &PN.getParent()->front() ||
1239 isa<PHINode>(--BasicBlock::iterator(&PN)),
Chris Lattner24e845f2002-06-25 15:56:27 +00001240 "PHI nodes not grouped at top of basic block!",
1241 &PN, PN.getParent());
1242
Nick Lewycky49072472009-09-08 01:23:52 +00001243 // Check that all of the values of the PHI node have the same type as the
1244 // result, and that the incoming blocks are really basic blocks.
1245 for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
Chris Lattner579de712003-11-12 07:13:37 +00001246 Assert1(PN.getType() == PN.getIncomingValue(i)->getType(),
1247 "PHI node operands are not the same type as the result!", &PN);
Nick Lewycky49072472009-09-08 01:23:52 +00001248 }
Chris Lattner579de712003-11-12 07:13:37 +00001249
Chris Lattnerbede31f2003-10-05 17:44:18 +00001250 // All other PHI node constraints are checked in the visitBasicBlock method.
Chris Lattnerd231fc32002-04-18 20:37:37 +00001251
1252 visitInstruction(PN);
1253}
1254
Duncan Sandsd9d70392007-12-21 19:19:01 +00001255void Verifier::VerifyCallSite(CallSite CS) {
1256 Instruction *I = CS.getInstruction();
1257
Duncan Sands1df98592010-02-16 11:11:14 +00001258 Assert1(CS.getCalledValue()->getType()->isPointerTy(),
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001259 "Called function must be a pointer!", I);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001260 PointerType *FPTy = cast<PointerType>(CS.getCalledValue()->getType());
Chris Lattner56732fb2002-05-08 19:49:50 +00001261
Duncan Sands1df98592010-02-16 11:11:14 +00001262 Assert1(FPTy->getElementType()->isFunctionTy(),
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001263 "Called function is not pointer to function type!", I);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001264 FunctionType *FTy = cast<FunctionType>(FPTy->getElementType());
Chris Lattner56732fb2002-05-08 19:49:50 +00001265
1266 // Verify that the correct number of arguments are being passed
1267 if (FTy->isVarArg())
Duncan Sandsd9d70392007-12-21 19:19:01 +00001268 Assert1(CS.arg_size() >= FTy->getNumParams(),
1269 "Called function requires more parameters than were provided!",I);
Chris Lattner56732fb2002-05-08 19:49:50 +00001270 else
Duncan Sandsd9d70392007-12-21 19:19:01 +00001271 Assert1(CS.arg_size() == FTy->getNumParams(),
1272 "Incorrect number of arguments passed to called function!", I);
Chris Lattner56732fb2002-05-08 19:49:50 +00001273
Chris Lattner775aba22010-05-10 20:58:42 +00001274 // Verify that all arguments to the call match the function type.
Chris Lattner56732fb2002-05-08 19:49:50 +00001275 for (unsigned i = 0, e = FTy->getNumParams(); i != e; ++i)
Duncan Sandsd9d70392007-12-21 19:19:01 +00001276 Assert3(CS.getArgument(i)->getType() == FTy->getParamType(i),
Chris Lattner56732fb2002-05-08 19:49:50 +00001277 "Call parameter type does not match function signature!",
Duncan Sandsd9d70392007-12-21 19:19:01 +00001278 CS.getArgument(i), FTy->getParamType(i), I);
1279
Bill Wendling99faa3b2012-12-07 23:16:57 +00001280 const AttributeSet &Attrs = CS.getAttributes();
Duncan Sands623a3892008-01-11 22:36:48 +00001281
Devang Pateld9b4a5f2008-09-23 22:35:17 +00001282 Assert1(VerifyAttributeCount(Attrs, CS.arg_size()),
Bill Wendling034b94b2012-12-19 07:18:57 +00001283 "Attribute after last parameter!", I);
Duncan Sands623a3892008-01-11 22:36:48 +00001284
Duncan Sandsd9d70392007-12-21 19:19:01 +00001285 // Verify call attributes.
Duncan Sandscfad1b42008-01-12 16:42:01 +00001286 VerifyFunctionAttrs(FTy, Attrs, I);
Duncan Sands623a3892008-01-11 22:36:48 +00001287
Chris Lattner58d74912008-03-12 17:45:29 +00001288 if (FTy->isVarArg())
Duncan Sands623a3892008-01-11 22:36:48 +00001289 // Check attributes on the varargs part.
1290 for (unsigned Idx = 1 + FTy->getNumParams(); Idx <= CS.arg_size(); ++Idx) {
Bill Wendling034b94b2012-12-19 07:18:57 +00001291 Attribute Attr = Attrs.getParamAttributes(Idx);
Duncan Sandscfad1b42008-01-12 16:42:01 +00001292
Duncan Sandsd6de30c2009-06-11 08:11:03 +00001293 VerifyParameterAttrs(Attr, CS.getArgument(Idx-1)->getType(), false, I);
Duncan Sandscfad1b42008-01-12 16:42:01 +00001294
Bill Wendling1d3dcfe2012-12-19 08:57:40 +00001295 Assert1(!Attr.hasAttribute(Attribute::StructRet),
Bill Wendling15c37892012-10-09 09:33:01 +00001296 "Attribute 'sret' cannot be used for vararg call arguments!", I);
Duncan Sands623a3892008-01-11 22:36:48 +00001297 }
Duncan Sandsd9d70392007-12-21 19:19:01 +00001298
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001299 // Verify that there's no metadata unless it's a direct call to an intrinsic.
Chris Lattner1afcace2011-07-09 17:41:24 +00001300 if (CS.getCalledFunction() == 0 ||
Chris Lattner775aba22010-05-10 20:58:42 +00001301 !CS.getCalledFunction()->getName().startswith("llvm.")) {
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001302 for (FunctionType::param_iterator PI = FTy->param_begin(),
1303 PE = FTy->param_end(); PI != PE; ++PI)
Chris Lattner1afcace2011-07-09 17:41:24 +00001304 Assert1(!(*PI)->isMetadataTy(),
Owen Anderson1d0be152009-08-13 21:58:54 +00001305 "Function has metadata parameter but isn't an intrinsic", I);
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001306 }
1307
Duncan Sandsd9d70392007-12-21 19:19:01 +00001308 visitInstruction(*I);
1309}
1310
1311void Verifier::visitCallInst(CallInst &CI) {
1312 VerifyCallSite(&CI);
Chris Lattner3535c9b2002-07-18 00:13:42 +00001313
Dale Johannesen49de9822009-02-05 01:49:45 +00001314 if (Function *F = CI.getCalledFunction())
Brian Gaeked0fde302003-11-11 22:41:34 +00001315 if (Intrinsic::ID ID = (Intrinsic::ID)F->getIntrinsicID())
Chris Lattnerdd035d12003-05-08 03:47:33 +00001316 visitIntrinsicFunctionCall(ID, CI);
Duncan Sandsd9d70392007-12-21 19:19:01 +00001317}
1318
1319void Verifier::visitInvokeInst(InvokeInst &II) {
1320 VerifyCallSite(&II);
Bill Wendlingcccfd192011-09-21 22:57:02 +00001321
1322 // Verify that there is a landingpad instruction as the first non-PHI
1323 // instruction of the 'unwind' destination.
1324 Assert1(II.getUnwindDest()->isLandingPad(),
1325 "The unwind destination does not have a landingpad instruction!",&II);
1326
Dan Gohmandacfc5d2010-08-02 23:09:14 +00001327 visitTerminatorInst(II);
Chris Lattnerefdd0a22002-04-18 22:11:52 +00001328}
Chris Lattnerd231fc32002-04-18 20:37:37 +00001329
Misha Brukmanab5c6002004-03-02 00:22:19 +00001330/// visitBinaryOperator - Check that both arguments to the binary operator are
1331/// of the same type!
1332///
Chris Lattner24e845f2002-06-25 15:56:27 +00001333void Verifier::visitBinaryOperator(BinaryOperator &B) {
Chris Lattner1a143ae2002-09-09 20:26:04 +00001334 Assert1(B.getOperand(0)->getType() == B.getOperand(1)->getType(),
1335 "Both operands to a binary operator are not of the same type!", &B);
Chris Lattnerd231fc32002-04-18 20:37:37 +00001336
Reid Spencer832254e2007-02-02 02:16:23 +00001337 switch (B.getOpcode()) {
Dan Gohman11cc35d2009-06-05 16:10:00 +00001338 // Check that integer arithmetic operators are only used with
1339 // integral operands.
1340 case Instruction::Add:
1341 case Instruction::Sub:
1342 case Instruction::Mul:
1343 case Instruction::SDiv:
1344 case Instruction::UDiv:
1345 case Instruction::SRem:
1346 case Instruction::URem:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001347 Assert1(B.getType()->isIntOrIntVectorTy(),
Dan Gohman11cc35d2009-06-05 16:10:00 +00001348 "Integer arithmetic operators only work with integral types!", &B);
1349 Assert1(B.getType() == B.getOperand(0)->getType(),
1350 "Integer arithmetic operators must have same type "
1351 "for operands and result!", &B);
1352 break;
1353 // Check that floating-point arithmetic operators are only used with
1354 // floating-point operands.
1355 case Instruction::FAdd:
1356 case Instruction::FSub:
1357 case Instruction::FMul:
1358 case Instruction::FDiv:
1359 case Instruction::FRem:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001360 Assert1(B.getType()->isFPOrFPVectorTy(),
Dan Gohman11cc35d2009-06-05 16:10:00 +00001361 "Floating-point arithmetic operators only work with "
Dan Gohmanf38fd692009-06-05 18:34:16 +00001362 "floating-point types!", &B);
Dan Gohman11cc35d2009-06-05 16:10:00 +00001363 Assert1(B.getType() == B.getOperand(0)->getType(),
1364 "Floating-point arithmetic operators must have same type "
1365 "for operands and result!", &B);
1366 break;
Chris Lattner1a143ae2002-09-09 20:26:04 +00001367 // Check that logical operators are only used with integral operands.
Reid Spencer832254e2007-02-02 02:16:23 +00001368 case Instruction::And:
1369 case Instruction::Or:
1370 case Instruction::Xor:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001371 Assert1(B.getType()->isIntOrIntVectorTy(),
Chris Lattner1a143ae2002-09-09 20:26:04 +00001372 "Logical operators only work with integral types!", &B);
1373 Assert1(B.getType() == B.getOperand(0)->getType(),
1374 "Logical operators must have same type for operands and result!",
1375 &B);
Reid Spencer832254e2007-02-02 02:16:23 +00001376 break;
1377 case Instruction::Shl:
1378 case Instruction::LShr:
1379 case Instruction::AShr:
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001380 Assert1(B.getType()->isIntOrIntVectorTy(),
Nate Begeman5bc1ea02008-07-29 15:49:41 +00001381 "Shifts only work with integral types!", &B);
Reid Spencer832254e2007-02-02 02:16:23 +00001382 Assert1(B.getType() == B.getOperand(0)->getType(),
1383 "Shift return type must be same as operands!", &B);
Reid Spencer832254e2007-02-02 02:16:23 +00001384 break;
Dan Gohman11cc35d2009-06-05 16:10:00 +00001385 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001386 llvm_unreachable("Unknown BinaryOperator opcode!");
Chris Lattner1a143ae2002-09-09 20:26:04 +00001387 }
Misha Brukmanfd939082005-04-21 23:48:37 +00001388
Chris Lattnerd231fc32002-04-18 20:37:37 +00001389 visitInstruction(B);
1390}
1391
Nick Lewycky55e97d42010-08-22 23:45:14 +00001392void Verifier::visitICmpInst(ICmpInst &IC) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00001393 // Check that the operands are the same type
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001394 Type *Op0Ty = IC.getOperand(0)->getType();
1395 Type *Op1Ty = IC.getOperand(1)->getType();
Reid Spencer45fb3f32006-11-20 01:22:35 +00001396 Assert1(Op0Ty == Op1Ty,
1397 "Both operands to ICmp instruction are not of the same type!", &IC);
1398 // Check that the operands are the right type
Nadav Rotem16087692011-12-05 06:29:09 +00001399 Assert1(Op0Ty->isIntOrIntVectorTy() || Op0Ty->getScalarType()->isPointerTy(),
Reid Spencer45fb3f32006-11-20 01:22:35 +00001400 "Invalid operand types for ICmp instruction", &IC);
Nick Lewycky55e97d42010-08-22 23:45:14 +00001401 // Check that the predicate is valid.
1402 Assert1(IC.getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1403 IC.getPredicate() <= CmpInst::LAST_ICMP_PREDICATE,
1404 "Invalid predicate in ICmp instruction!", &IC);
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001405
Reid Spencer45fb3f32006-11-20 01:22:35 +00001406 visitInstruction(IC);
1407}
1408
Nick Lewycky55e97d42010-08-22 23:45:14 +00001409void Verifier::visitFCmpInst(FCmpInst &FC) {
Reid Spencer45fb3f32006-11-20 01:22:35 +00001410 // Check that the operands are the same type
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001411 Type *Op0Ty = FC.getOperand(0)->getType();
1412 Type *Op1Ty = FC.getOperand(1)->getType();
Reid Spencer45fb3f32006-11-20 01:22:35 +00001413 Assert1(Op0Ty == Op1Ty,
1414 "Both operands to FCmp instruction are not of the same type!", &FC);
1415 // Check that the operands are the right type
Duncan Sandsb0bc6c32010-02-15 16:12:20 +00001416 Assert1(Op0Ty->isFPOrFPVectorTy(),
Reid Spencer45fb3f32006-11-20 01:22:35 +00001417 "Invalid operand types for FCmp instruction", &FC);
Nick Lewycky55e97d42010-08-22 23:45:14 +00001418 // Check that the predicate is valid.
1419 Assert1(FC.getPredicate() >= CmpInst::FIRST_FCMP_PREDICATE &&
1420 FC.getPredicate() <= CmpInst::LAST_FCMP_PREDICATE,
1421 "Invalid predicate in FCmp instruction!", &FC);
1422
Reid Spencer45fb3f32006-11-20 01:22:35 +00001423 visitInstruction(FC);
1424}
1425
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001426void Verifier::visitExtractElementInst(ExtractElementInst &EI) {
Chris Lattner1cbe05b2006-04-08 04:07:52 +00001427 Assert1(ExtractElementInst::isValidOperands(EI.getOperand(0),
1428 EI.getOperand(1)),
1429 "Invalid extractelement operands!", &EI);
Robert Bocchinob52ee7f2006-01-10 19:05:34 +00001430 visitInstruction(EI);
1431}
1432
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001433void Verifier::visitInsertElementInst(InsertElementInst &IE) {
Chris Lattner1cbe05b2006-04-08 04:07:52 +00001434 Assert1(InsertElementInst::isValidOperands(IE.getOperand(0),
1435 IE.getOperand(1),
1436 IE.getOperand(2)),
1437 "Invalid insertelement operands!", &IE);
Robert Bocchinoc152f9c2006-01-17 20:07:22 +00001438 visitInstruction(IE);
1439}
1440
Chris Lattner00f10232006-04-08 01:18:18 +00001441void Verifier::visitShuffleVectorInst(ShuffleVectorInst &SV) {
1442 Assert1(ShuffleVectorInst::isValidOperands(SV.getOperand(0), SV.getOperand(1),
1443 SV.getOperand(2)),
1444 "Invalid shufflevector operands!", &SV);
Chris Lattner00f10232006-04-08 01:18:18 +00001445 visitInstruction(SV);
1446}
1447
Chris Lattner24e845f2002-06-25 15:56:27 +00001448void Verifier::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Duncan Sandsb95d1ff2012-02-03 17:28:51 +00001449 Type *TargetTy = GEP.getPointerOperandType()->getScalarType();
Nadav Rotem16087692011-12-05 06:29:09 +00001450
Duncan Sandsb95d1ff2012-02-03 17:28:51 +00001451 Assert1(isa<PointerType>(TargetTy),
Bill Wendling6ebddd22012-10-17 23:56:05 +00001452 "GEP base pointer is not a vector or a vector of pointers", &GEP);
Nadav Rotem16087692011-12-05 06:29:09 +00001453 Assert1(cast<PointerType>(TargetTy)->getElementType()->isSized(),
Chris Lattner4cea5ba2011-07-29 20:32:28 +00001454 "GEP into unsized type!", &GEP);
Duncan Sands2333e292012-11-13 12:59:33 +00001455 Assert1(GEP.getPointerOperandType()->isVectorTy() ==
1456 GEP.getType()->isVectorTy(), "Vector GEP must return a vector value",
1457 &GEP);
Nadav Rotem16087692011-12-05 06:29:09 +00001458
Chris Lattner8552fae2007-02-10 08:30:29 +00001459 SmallVector<Value*, 16> Idxs(GEP.idx_begin(), GEP.idx_end());
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001460 Type *ElTy =
Nadav Rotem16087692011-12-05 06:29:09 +00001461 GetElementPtrInst::getIndexedType(GEP.getPointerOperandType(), Idxs);
Chris Lattner24e845f2002-06-25 15:56:27 +00001462 Assert1(ElTy, "Invalid indices for GEP pointer type!", &GEP);
Nadav Rotem16087692011-12-05 06:29:09 +00001463
Duncan Sands2333e292012-11-13 12:59:33 +00001464 Assert2(GEP.getType()->getScalarType()->isPointerTy() &&
1465 cast<PointerType>(GEP.getType()->getScalarType())->getElementType()
1466 == ElTy, "GEP is not of right type for indices!", &GEP, ElTy);
1467
1468 if (GEP.getPointerOperandType()->isVectorTy()) {
1469 // Additional checks for vector GEPs.
1470 unsigned GepWidth = GEP.getPointerOperandType()->getVectorNumElements();
1471 Assert1(GepWidth == GEP.getType()->getVectorNumElements(),
1472 "Vector GEP result width doesn't match operand's", &GEP);
1473 for (unsigned i = 0, e = Idxs.size(); i != e; ++i) {
1474 Type *IndexTy = Idxs[i]->getType();
1475 Assert1(IndexTy->isVectorTy(),
1476 "Vector GEP must have vector indices!", &GEP);
1477 unsigned IndexWidth = IndexTy->getVectorNumElements();
1478 Assert1(IndexWidth == GepWidth, "Invalid GEP index vector width", &GEP);
1479 }
Nadav Rotem16087692011-12-05 06:29:09 +00001480 }
Chris Lattnera00409e2002-04-24 19:12:21 +00001481 visitInstruction(GEP);
1482}
1483
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001484static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
1485 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
1486}
1487
Chris Lattner24e845f2002-06-25 15:56:27 +00001488void Verifier::visitLoadInst(LoadInst &LI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001489 PointerType *PTy = dyn_cast<PointerType>(LI.getOperand(0)->getType());
Nick Lewycky49072472009-09-08 01:23:52 +00001490 Assert1(PTy, "Load operand must be a pointer.", &LI);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001491 Type *ElTy = PTy->getElementType();
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001492 Assert2(ElTy == LI.getType(),
1493 "Load result type does not match pointer operand type!", &LI, ElTy);
Eli Friedman21006d42011-08-09 23:02:53 +00001494 if (LI.isAtomic()) {
1495 Assert1(LI.getOrdering() != Release && LI.getOrdering() != AcquireRelease,
1496 "Load cannot have Release ordering", &LI);
1497 Assert1(LI.getAlignment() != 0,
1498 "Atomic load must specify explicit alignment", &LI);
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001499 if (!ElTy->isPointerTy()) {
1500 Assert2(ElTy->isIntegerTy(),
1501 "atomic store operand must have integer type!",
1502 &LI, ElTy);
1503 unsigned Size = ElTy->getPrimitiveSizeInBits();
1504 Assert2(Size >= 8 && !(Size & (Size - 1)),
1505 "atomic store operand must be power-of-two byte-sized integer",
1506 &LI, ElTy);
1507 }
Eli Friedman21006d42011-08-09 23:02:53 +00001508 } else {
1509 Assert1(LI.getSynchScope() == CrossThread,
1510 "Non-atomic load cannot have SynchronizationScope specified", &LI);
1511 }
Rafael Espindola39dd3282012-03-24 00:14:51 +00001512
1513 if (MDNode *Range = LI.getMetadata(LLVMContext::MD_range)) {
1514 unsigned NumOperands = Range->getNumOperands();
1515 Assert1(NumOperands % 2 == 0, "Unfinished range!", Range);
1516 unsigned NumRanges = NumOperands / 2;
1517 Assert1(NumRanges >= 1, "It should have at least one range!", Range);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001518
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001519 ConstantRange LastRange(1); // Dummy initial value
Rafael Espindola39dd3282012-03-24 00:14:51 +00001520 for (unsigned i = 0; i < NumRanges; ++i) {
1521 ConstantInt *Low = dyn_cast<ConstantInt>(Range->getOperand(2*i));
1522 Assert1(Low, "The lower limit must be an integer!", Low);
1523 ConstantInt *High = dyn_cast<ConstantInt>(Range->getOperand(2*i + 1));
1524 Assert1(High, "The upper limit must be an integer!", High);
1525 Assert1(High->getType() == Low->getType() &&
1526 High->getType() == ElTy, "Range types must match load type!",
1527 &LI);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001528
1529 APInt HighV = High->getValue();
1530 APInt LowV = Low->getValue();
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001531 ConstantRange CurRange(LowV, HighV);
1532 Assert1(!CurRange.isEmptySet() && !CurRange.isFullSet(),
1533 "Range must not be empty!", Range);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001534 if (i != 0) {
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001535 Assert1(CurRange.intersectWith(LastRange).isEmptySet(),
1536 "Intervals are overlapping", Range);
1537 Assert1(LowV.sgt(LastRange.getLower()), "Intervals are not in order",
1538 Range);
1539 Assert1(!isContiguous(CurRange, LastRange), "Intervals are contiguous",
1540 Range);
Rafael Espindolac49b29e2012-05-31 13:45:46 +00001541 }
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001542 LastRange = ConstantRange(LowV, HighV);
Rafael Espindola39dd3282012-03-24 00:14:51 +00001543 }
Rafael Espindolaa1b95f52012-05-31 16:04:26 +00001544 if (NumRanges > 2) {
1545 APInt FirstLow =
1546 dyn_cast<ConstantInt>(Range->getOperand(0))->getValue();
1547 APInt FirstHigh =
1548 dyn_cast<ConstantInt>(Range->getOperand(1))->getValue();
1549 ConstantRange FirstRange(FirstLow, FirstHigh);
1550 Assert1(FirstRange.intersectWith(LastRange).isEmptySet(),
1551 "Intervals are overlapping", Range);
1552 Assert1(!isContiguous(FirstRange, LastRange), "Intervals are contiguous",
1553 Range);
1554 }
1555
1556
Rafael Espindola39dd3282012-03-24 00:14:51 +00001557 }
1558
Chris Lattnera00409e2002-04-24 19:12:21 +00001559 visitInstruction(LI);
1560}
1561
Chris Lattner24e845f2002-06-25 15:56:27 +00001562void Verifier::visitStoreInst(StoreInst &SI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001563 PointerType *PTy = dyn_cast<PointerType>(SI.getOperand(1)->getType());
Chris Lattnerb4a96312010-07-11 19:42:53 +00001564 Assert1(PTy, "Store operand must be a pointer.", &SI);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001565 Type *ElTy = PTy->getElementType();
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001566 Assert2(ElTy == SI.getOperand(0)->getType(),
1567 "Stored value type does not match pointer operand type!",
1568 &SI, ElTy);
Eli Friedman21006d42011-08-09 23:02:53 +00001569 if (SI.isAtomic()) {
1570 Assert1(SI.getOrdering() != Acquire && SI.getOrdering() != AcquireRelease,
1571 "Store cannot have Acquire ordering", &SI);
1572 Assert1(SI.getAlignment() != 0,
1573 "Atomic store must specify explicit alignment", &SI);
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001574 if (!ElTy->isPointerTy()) {
1575 Assert2(ElTy->isIntegerTy(),
1576 "atomic store operand must have integer type!",
1577 &SI, ElTy);
1578 unsigned Size = ElTy->getPrimitiveSizeInBits();
1579 Assert2(Size >= 8 && !(Size & (Size - 1)),
1580 "atomic store operand must be power-of-two byte-sized integer",
1581 &SI, ElTy);
1582 }
Eli Friedman21006d42011-08-09 23:02:53 +00001583 } else {
1584 Assert1(SI.getSynchScope() == CrossThread,
1585 "Non-atomic store cannot have SynchronizationScope specified", &SI);
1586 }
Chris Lattnera00409e2002-04-24 19:12:21 +00001587 visitInstruction(SI);
1588}
1589
Victor Hernandez7b929da2009-10-23 21:09:37 +00001590void Verifier::visitAllocaInst(AllocaInst &AI) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001591 PointerType *PTy = AI.getType();
Chris Lattnerab3b7782008-03-01 09:01:57 +00001592 Assert1(PTy->getAddressSpace() == 0,
1593 "Allocation instruction pointer not in the generic address space!",
1594 &AI);
1595 Assert1(PTy->getElementType()->isSized(), "Cannot allocate unsized type",
1596 &AI);
Dan Gohmanf75a7d32010-05-28 01:14:11 +00001597 Assert1(AI.getArraySize()->getType()->isIntegerTy(),
1598 "Alloca array size must have integer type", &AI);
Christopher Lamb303dae92007-12-17 01:00:21 +00001599 visitInstruction(AI);
1600}
1601
Eli Friedmanff030482011-07-28 21:48:00 +00001602void Verifier::visitAtomicCmpXchgInst(AtomicCmpXchgInst &CXI) {
1603 Assert1(CXI.getOrdering() != NotAtomic,
1604 "cmpxchg instructions must be atomic.", &CXI);
1605 Assert1(CXI.getOrdering() != Unordered,
1606 "cmpxchg instructions cannot be unordered.", &CXI);
1607 PointerType *PTy = dyn_cast<PointerType>(CXI.getOperand(0)->getType());
1608 Assert1(PTy, "First cmpxchg operand must be a pointer.", &CXI);
1609 Type *ElTy = PTy->getElementType();
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001610 Assert2(ElTy->isIntegerTy(),
1611 "cmpxchg operand must have integer type!",
1612 &CXI, ElTy);
1613 unsigned Size = ElTy->getPrimitiveSizeInBits();
1614 Assert2(Size >= 8 && !(Size & (Size - 1)),
1615 "cmpxchg operand must be power-of-two byte-sized integer",
1616 &CXI, ElTy);
Eli Friedmanff030482011-07-28 21:48:00 +00001617 Assert2(ElTy == CXI.getOperand(1)->getType(),
1618 "Expected value type does not match pointer operand type!",
1619 &CXI, ElTy);
1620 Assert2(ElTy == CXI.getOperand(2)->getType(),
1621 "Stored value type does not match pointer operand type!",
1622 &CXI, ElTy);
1623 visitInstruction(CXI);
1624}
1625
1626void Verifier::visitAtomicRMWInst(AtomicRMWInst &RMWI) {
1627 Assert1(RMWI.getOrdering() != NotAtomic,
1628 "atomicrmw instructions must be atomic.", &RMWI);
1629 Assert1(RMWI.getOrdering() != Unordered,
1630 "atomicrmw instructions cannot be unordered.", &RMWI);
1631 PointerType *PTy = dyn_cast<PointerType>(RMWI.getOperand(0)->getType());
1632 Assert1(PTy, "First atomicrmw operand must be a pointer.", &RMWI);
1633 Type *ElTy = PTy->getElementType();
Eli Friedmanfd45fa12012-08-17 23:24:29 +00001634 Assert2(ElTy->isIntegerTy(),
1635 "atomicrmw operand must have integer type!",
1636 &RMWI, ElTy);
1637 unsigned Size = ElTy->getPrimitiveSizeInBits();
1638 Assert2(Size >= 8 && !(Size & (Size - 1)),
1639 "atomicrmw operand must be power-of-two byte-sized integer",
1640 &RMWI, ElTy);
Eli Friedmanff030482011-07-28 21:48:00 +00001641 Assert2(ElTy == RMWI.getOperand(1)->getType(),
1642 "Argument value type does not match pointer operand type!",
1643 &RMWI, ElTy);
1644 Assert1(AtomicRMWInst::FIRST_BINOP <= RMWI.getOperation() &&
1645 RMWI.getOperation() <= AtomicRMWInst::LAST_BINOP,
1646 "Invalid binary operation!", &RMWI);
1647 visitInstruction(RMWI);
1648}
1649
Eli Friedman47f35132011-07-25 23:16:38 +00001650void Verifier::visitFenceInst(FenceInst &FI) {
1651 const AtomicOrdering Ordering = FI.getOrdering();
1652 Assert1(Ordering == Acquire || Ordering == Release ||
1653 Ordering == AcquireRelease || Ordering == SequentiallyConsistent,
1654 "fence instructions may only have "
Bill Wendling63012202011-08-08 08:02:48 +00001655 "acquire, release, acq_rel, or seq_cst ordering.", &FI);
Eli Friedman47f35132011-07-25 23:16:38 +00001656 visitInstruction(FI);
1657}
1658
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001659void Verifier::visitExtractValueInst(ExtractValueInst &EVI) {
1660 Assert1(ExtractValueInst::getIndexedType(EVI.getAggregateOperand()->getType(),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001661 EVI.getIndices()) ==
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001662 EVI.getType(),
1663 "Invalid ExtractValueInst operands!", &EVI);
Chris Lattner42369b72008-04-23 04:06:15 +00001664
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001665 visitInstruction(EVI);
Devang Patel40a04212008-02-19 22:15:16 +00001666}
1667
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001668void Verifier::visitInsertValueInst(InsertValueInst &IVI) {
1669 Assert1(ExtractValueInst::getIndexedType(IVI.getAggregateOperand()->getType(),
Jay Foadfc6d3a42011-07-13 10:26:04 +00001670 IVI.getIndices()) ==
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001671 IVI.getOperand(1)->getType(),
1672 "Invalid InsertValueInst operands!", &IVI);
1673
1674 visitInstruction(IVI);
1675}
Chris Lattnerd231fc32002-04-18 20:37:37 +00001676
Bill Wendlinge6e88262011-08-12 20:24:12 +00001677void Verifier::visitLandingPadInst(LandingPadInst &LPI) {
1678 BasicBlock *BB = LPI.getParent();
1679
1680 // The landingpad instruction is ill-formed if it doesn't have any clauses and
1681 // isn't a cleanup.
1682 Assert1(LPI.getNumClauses() > 0 || LPI.isCleanup(),
1683 "LandingPadInst needs at least one clause or to be a cleanup.", &LPI);
1684
1685 // The landingpad instruction defines its parent as a landing pad block. The
1686 // landing pad block may be branched to only by the unwind edge of an invoke.
1687 for (pred_iterator I = pred_begin(BB), E = pred_end(BB); I != E; ++I) {
1688 const InvokeInst *II = dyn_cast<InvokeInst>((*I)->getTerminator());
Eli Friedman6b951b22012-08-10 20:55:20 +00001689 Assert1(II && II->getUnwindDest() == BB && II->getNormalDest() != BB,
Bill Wendlinge6e88262011-08-12 20:24:12 +00001690 "Block containing LandingPadInst must be jumped to "
1691 "only by the unwind edge of an invoke.", &LPI);
1692 }
1693
1694 // The landingpad instruction must be the first non-PHI instruction in the
1695 // block.
1696 Assert1(LPI.getParent()->getLandingPadInst() == &LPI,
1697 "LandingPadInst not the first non-PHI instruction in the block.",
1698 &LPI);
1699
1700 // The personality functions for all landingpad instructions within the same
1701 // function should match.
1702 if (PersonalityFn)
1703 Assert1(LPI.getPersonalityFn() == PersonalityFn,
1704 "Personality function doesn't match others in function", &LPI);
1705 PersonalityFn = LPI.getPersonalityFn();
1706
Duncan Sands00418ea2011-09-27 16:43:19 +00001707 // All operands must be constants.
1708 Assert1(isa<Constant>(PersonalityFn), "Personality function is not constant!",
1709 &LPI);
1710 for (unsigned i = 0, e = LPI.getNumClauses(); i < e; ++i) {
1711 Value *Clause = LPI.getClause(i);
1712 Assert1(isa<Constant>(Clause), "Clause is not constant!", &LPI);
Duncan Sands040bff02011-09-27 19:34:22 +00001713 if (LPI.isCatch(i)) {
1714 Assert1(isa<PointerType>(Clause->getType()),
1715 "Catch operand does not have pointer type!", &LPI);
1716 } else {
1717 Assert1(LPI.isFilter(i), "Clause is neither catch nor filter!", &LPI);
Duncan Sands00418ea2011-09-27 16:43:19 +00001718 Assert1(isa<ConstantArray>(Clause) || isa<ConstantAggregateZero>(Clause),
Duncan Sands040bff02011-09-27 19:34:22 +00001719 "Filter operand is not an array of constants!", &LPI);
1720 }
Duncan Sands00418ea2011-09-27 16:43:19 +00001721 }
1722
Bill Wendlinge6e88262011-08-12 20:24:12 +00001723 visitInstruction(LPI);
1724}
1725
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001726void Verifier::verifyDominatesUse(Instruction &I, unsigned i) {
1727 Instruction *Op = cast<Instruction>(I.getOperand(i));
Rafael Espindolad5118c82012-08-17 18:21:28 +00001728 // If the we have an invalid invoke, don't try to compute the dominance.
1729 // We already reject it in the invoke specific checks and the dominance
1730 // computation doesn't handle multiple edges.
1731 if (InvokeInst *II = dyn_cast<InvokeInst>(Op)) {
1732 if (II->getNormalDest() == II->getUnwindDest())
1733 return;
1734 }
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001735
Rafael Espindola20907662012-06-01 21:56:26 +00001736 const Use &U = I.getOperandUse(i);
1737 Assert2(InstsInThisBlock.count(Op) || DT->dominates(Op, U),
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001738 "Instruction does not dominate all uses!", Op, &I);
1739}
1740
Misha Brukmanab5c6002004-03-02 00:22:19 +00001741/// verifyInstruction - Verify that an instruction is well formed.
1742///
Chris Lattner24e845f2002-06-25 15:56:27 +00001743void Verifier::visitInstruction(Instruction &I) {
Misha Brukmanfd939082005-04-21 23:48:37 +00001744 BasicBlock *BB = I.getParent();
Chris Lattner1a143ae2002-09-09 20:26:04 +00001745 Assert1(BB, "Instruction not embedded in basic block!", &I);
Chris Lattnerd231fc32002-04-18 20:37:37 +00001746
Chris Lattnerbede31f2003-10-05 17:44:18 +00001747 if (!isa<PHINode>(I)) { // Check that non-phi nodes are not self referential
1748 for (Value::use_iterator UI = I.use_begin(), UE = I.use_end();
1749 UI != UE; ++UI)
Duncan Sands44008252009-05-29 19:39:36 +00001750 Assert1(*UI != (User*)&I || !DT->isReachableFromEntry(BB),
Chris Lattnerbede31f2003-10-05 17:44:18 +00001751 "Only PHI nodes may reference their own value!", &I);
1752 }
Nick Lewycky29ef6592009-09-07 20:44:51 +00001753
Chris Lattnerbede31f2003-10-05 17:44:18 +00001754 // Check that void typed values don't have names
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001755 Assert1(!I.getType()->isVoidTy() || !I.hasName(),
Chris Lattnerbede31f2003-10-05 17:44:18 +00001756 "Instruction has a name, but provides a void value!", &I);
1757
Chris Lattner944cfaf2004-03-29 00:29:36 +00001758 // Check that the return value of the instruction is either void or a legal
1759 // value type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001760 Assert1(I.getType()->isVoidTy() ||
Nick Lewyckyc261df92009-09-27 23:27:42 +00001761 I.getType()->isFirstClassType(),
Chris Lattner944cfaf2004-03-29 00:29:36 +00001762 "Instruction returns a non-scalar type!", &I);
1763
Nick Lewyckyc261df92009-09-27 23:27:42 +00001764 // Check that the instruction doesn't produce metadata. Calls are already
1765 // checked against the callee type.
Chris Lattnercf0fe8d2009-10-05 05:54:46 +00001766 Assert1(!I.getType()->isMetadataTy() ||
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001767 isa<CallInst>(I) || isa<InvokeInst>(I),
1768 "Invalid use of metadata!", &I);
1769
Chris Lattnerd231fc32002-04-18 20:37:37 +00001770 // Check that all uses of the instruction, if they are instructions
1771 // themselves, actually have parent basic blocks. If the use is not an
1772 // instruction, it is an error!
Chris Lattner24e845f2002-06-25 15:56:27 +00001773 for (User::use_iterator UI = I.use_begin(), UE = I.use_end();
Chris Lattnerd231fc32002-04-18 20:37:37 +00001774 UI != UE; ++UI) {
Nick Lewycky49072472009-09-08 01:23:52 +00001775 if (Instruction *Used = dyn_cast<Instruction>(*UI))
1776 Assert2(Used->getParent() != 0, "Instruction referencing instruction not"
1777 " embedded in a basic block!", &I, Used);
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001778 else {
Nick Lewycky49072472009-09-08 01:23:52 +00001779 CheckFailed("Use of instruction is not an instruction!", *UI);
Nick Lewycky4b6af8a2009-09-08 02:02:39 +00001780 return;
1781 }
Chris Lattnerd231fc32002-04-18 20:37:37 +00001782 }
1783
Chris Lattnerbede31f2003-10-05 17:44:18 +00001784 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Chris Lattneraab18202005-02-24 16:58:29 +00001785 Assert1(I.getOperand(i) != 0, "Instruction has null operand!", &I);
Chris Lattnerf4ea9212006-07-11 20:29:49 +00001786
1787 // Check to make sure that only first-class-values are operands to
1788 // instructions.
Devang Patelbb4f8d42008-02-21 01:54:02 +00001789 if (!I.getOperand(i)->getType()->isFirstClassType()) {
Dan Gohmanfc74abf2008-07-23 00:34:11 +00001790 Assert1(0, "Instruction operands must be first-class values!", &I);
Devang Patelbb4f8d42008-02-21 01:54:02 +00001791 }
Nick Lewycky7a0370f2009-05-30 05:06:04 +00001792
Chris Lattner59c35692004-03-14 03:23:54 +00001793 if (Function *F = dyn_cast<Function>(I.getOperand(i))) {
Chris Lattnerf4ea9212006-07-11 20:29:49 +00001794 // Check to make sure that the "address of" an intrinsic function is never
1795 // taken.
Nuno Lopes917f97c2012-06-28 22:57:00 +00001796 Assert1(!F->isIntrinsic() || i == (isa<CallInst>(I) ? e-1 : 0),
Chris Lattnerdd035d12003-05-08 03:47:33 +00001797 "Cannot take the address of an intrinsic!", &I);
Nuno Lopes917f97c2012-06-28 22:57:00 +00001798 Assert1(!F->isIntrinsic() || isa<CallInst>(I) ||
1799 F->getIntrinsicID() == Intrinsic::donothing,
1800 "Cannot invoke an intrinsinc other than donothing", &I);
Chris Lattner19b6dcd2007-04-20 21:48:08 +00001801 Assert1(F->getParent() == Mod, "Referencing function in another module!",
1802 &I);
Chris Lattner59c35692004-03-14 03:23:54 +00001803 } else if (BasicBlock *OpBB = dyn_cast<BasicBlock>(I.getOperand(i))) {
1804 Assert1(OpBB->getParent() == BB->getParent(),
1805 "Referring to a basic block in another function!", &I);
1806 } else if (Argument *OpArg = dyn_cast<Argument>(I.getOperand(i))) {
1807 Assert1(OpArg->getParent() == BB->getParent(),
1808 "Referring to an argument in another function!", &I);
Chris Lattner19b6dcd2007-04-20 21:48:08 +00001809 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(I.getOperand(i))) {
1810 Assert1(GV->getParent() == Mod, "Referencing global in another module!",
1811 &I);
Rafael Espindolac987f4c2012-02-26 02:23:37 +00001812 } else if (isa<Instruction>(I.getOperand(i))) {
1813 verifyDominatesUse(I, i);
Chris Lattner3188b732006-01-26 00:08:45 +00001814 } else if (isa<InlineAsm>(I.getOperand(i))) {
Gabor Greif63d024f2010-07-13 15:31:36 +00001815 Assert1((i + 1 == e && isa<CallInst>(I)) ||
1816 (i + 3 == e && isa<InvokeInst>(I)),
Chris Lattner3188b732006-01-26 00:08:45 +00001817 "Cannot take the address of an inline asm!", &I);
Chris Lattnerbede31f2003-10-05 17:44:18 +00001818 }
1819 }
Rafael Espindola39dd3282012-03-24 00:14:51 +00001820
Duncan Sands5e5c5f82012-04-14 12:36:06 +00001821 if (MDNode *MD = I.getMetadata(LLVMContext::MD_fpmath)) {
Duncan Sands1fd63df2012-04-10 08:22:43 +00001822 Assert1(I.getType()->isFPOrFPVectorTy(),
Duncan Sands5e5c5f82012-04-14 12:36:06 +00001823 "fpmath requires a floating point result!", &I);
1824 Assert1(MD->getNumOperands() == 1, "fpmath takes one operand!", &I);
Duncan Sands8883c432012-04-16 16:28:59 +00001825 Value *Op0 = MD->getOperand(0);
1826 if (ConstantFP *CFP0 = dyn_cast_or_null<ConstantFP>(Op0)) {
1827 APFloat Accuracy = CFP0->getValueAPF();
1828 Assert1(Accuracy.isNormal() && !Accuracy.isNegative(),
1829 "fpmath accuracy not a positive number!", &I);
Duncan Sands8883c432012-04-16 16:28:59 +00001830 } else {
1831 Assert1(false, "invalid fpmath accuracy!", &I);
1832 }
Duncan Sands1fd63df2012-04-10 08:22:43 +00001833 }
1834
Rafael Espindola39dd3282012-03-24 00:14:51 +00001835 MDNode *MD = I.getMetadata(LLVMContext::MD_range);
1836 Assert1(!MD || isa<LoadInst>(I), "Ranges are only for loads!", &I);
1837
Chris Lattnera7b1c7e2004-09-29 20:07:45 +00001838 InstsInThisBlock.insert(&I);
Chris Lattnerdd035d12003-05-08 03:47:33 +00001839}
1840
Chris Lattner55dc5c72012-05-27 19:37:05 +00001841/// VerifyIntrinsicType - Verify that the specified type (which comes from an
1842/// intrinsic argument or return value) matches the type constraints specified
1843/// by the .td file (e.g. an "any integer" argument really is an integer).
1844///
1845/// This return true on error but does not print a message.
1846bool Verifier::VerifyIntrinsicType(Type *Ty,
1847 ArrayRef<Intrinsic::IITDescriptor> &Infos,
1848 SmallVectorImpl<Type*> &ArgTys) {
1849 using namespace Intrinsic;
1850
1851 // If we ran out of descriptors, there are too many arguments.
1852 if (Infos.empty()) return true;
1853 IITDescriptor D = Infos.front();
1854 Infos = Infos.slice(1);
1855
1856 switch (D.Kind) {
1857 case IITDescriptor::Void: return !Ty->isVoidTy();
1858 case IITDescriptor::MMX: return !Ty->isX86_MMXTy();
1859 case IITDescriptor::Metadata: return !Ty->isMetadataTy();
Michael Ilseman4d0b4a42013-01-11 01:45:05 +00001860 case IITDescriptor::Half: return !Ty->isHalfTy();
Chris Lattner55dc5c72012-05-27 19:37:05 +00001861 case IITDescriptor::Float: return !Ty->isFloatTy();
1862 case IITDescriptor::Double: return !Ty->isDoubleTy();
1863 case IITDescriptor::Integer: return !Ty->isIntegerTy(D.Integer_Width);
1864 case IITDescriptor::Vector: {
1865 VectorType *VT = dyn_cast<VectorType>(Ty);
1866 return VT == 0 || VT->getNumElements() != D.Vector_Width ||
1867 VerifyIntrinsicType(VT->getElementType(), Infos, ArgTys);
1868 }
1869 case IITDescriptor::Pointer: {
1870 PointerType *PT = dyn_cast<PointerType>(Ty);
1871 return PT == 0 || PT->getAddressSpace() != D.Pointer_AddressSpace ||
1872 VerifyIntrinsicType(PT->getElementType(), Infos, ArgTys);
1873 }
1874
1875 case IITDescriptor::Struct: {
1876 StructType *ST = dyn_cast<StructType>(Ty);
1877 if (ST == 0 || ST->getNumElements() != D.Struct_NumElements)
1878 return true;
1879
1880 for (unsigned i = 0, e = D.Struct_NumElements; i != e; ++i)
1881 if (VerifyIntrinsicType(ST->getElementType(i), Infos, ArgTys))
1882 return true;
1883 return false;
1884 }
1885
1886 case IITDescriptor::Argument:
Benjamin Kramerd9b0b022012-06-02 10:20:22 +00001887 // Two cases here - If this is the second occurrence of an argument, verify
Chris Lattner55dc5c72012-05-27 19:37:05 +00001888 // that the later instance matches the previous instance.
1889 if (D.getArgumentNumber() < ArgTys.size())
1890 return Ty != ArgTys[D.getArgumentNumber()];
1891
1892 // Otherwise, if this is the first instance of an argument, record it and
1893 // verify the "Any" kind.
1894 assert(D.getArgumentNumber() == ArgTys.size() && "Table consistency error");
1895 ArgTys.push_back(Ty);
1896
1897 switch (D.getArgumentKind()) {
1898 case IITDescriptor::AK_AnyInteger: return !Ty->isIntOrIntVectorTy();
1899 case IITDescriptor::AK_AnyFloat: return !Ty->isFPOrFPVectorTy();
1900 case IITDescriptor::AK_AnyVector: return !isa<VectorType>(Ty);
1901 case IITDescriptor::AK_AnyPointer: return !isa<PointerType>(Ty);
1902 }
1903 llvm_unreachable("all argument kinds not covered");
1904
1905 case IITDescriptor::ExtendVecArgument:
1906 // This may only be used when referring to a previous vector argument.
1907 return D.getArgumentNumber() >= ArgTys.size() ||
1908 !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
1909 VectorType::getExtendedElementVectorType(
1910 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
1911
1912 case IITDescriptor::TruncVecArgument:
1913 // This may only be used when referring to a previous vector argument.
1914 return D.getArgumentNumber() >= ArgTys.size() ||
1915 !isa<VectorType>(ArgTys[D.getArgumentNumber()]) ||
1916 VectorType::getTruncatedElementVectorType(
1917 cast<VectorType>(ArgTys[D.getArgumentNumber()])) != Ty;
1918 }
1919 llvm_unreachable("unhandled");
1920}
Bob Wilsonbc039792009-01-07 00:09:01 +00001921
Chris Lattnerdd035d12003-05-08 03:47:33 +00001922/// visitIntrinsicFunction - Allow intrinsics to be verified in different ways.
Misha Brukmanab5c6002004-03-02 00:22:19 +00001923///
Brian Gaeked0fde302003-11-11 22:41:34 +00001924void Verifier::visitIntrinsicFunctionCall(Intrinsic::ID ID, CallInst &CI) {
Chris Lattnerdd035d12003-05-08 03:47:33 +00001925 Function *IF = CI.getCalledFunction();
Chris Lattner19b6dcd2007-04-20 21:48:08 +00001926 Assert1(IF->isDeclaration(), "Intrinsic functions should never be defined!",
1927 IF);
Nick Lewycky29ef6592009-09-07 20:44:51 +00001928
Chris Lattner55dc5c72012-05-27 19:37:05 +00001929 // Verify that the intrinsic prototype lines up with what the .td files
1930 // describe.
1931 FunctionType *IFTy = IF->getFunctionType();
1932 Assert1(!IFTy->isVarArg(), "Intrinsic prototypes are not varargs", IF);
1933
1934 SmallVector<Intrinsic::IITDescriptor, 8> Table;
1935 getIntrinsicInfoTableEntries(ID, Table);
1936 ArrayRef<Intrinsic::IITDescriptor> TableRef = Table;
Nick Lewycky29ef6592009-09-07 20:44:51 +00001937
Chris Lattner55dc5c72012-05-27 19:37:05 +00001938 SmallVector<Type *, 4> ArgTys;
1939 Assert1(!VerifyIntrinsicType(IFTy->getReturnType(), TableRef, ArgTys),
1940 "Intrinsic has incorrect return type!", IF);
1941 for (unsigned i = 0, e = IFTy->getNumParams(); i != e; ++i)
1942 Assert1(!VerifyIntrinsicType(IFTy->getParamType(i), TableRef, ArgTys),
1943 "Intrinsic has incorrect argument type!", IF);
1944 Assert1(TableRef.empty(), "Intrinsic has too few arguments!", IF);
1945
1946 // Now that we have the intrinsic ID and the actual argument types (and we
1947 // know they are legal for the intrinsic!) get the intrinsic name through the
1948 // usual means. This allows us to verify the mangling of argument types into
1949 // the name.
1950 Assert1(Intrinsic::getName(ID, ArgTys) == IF->getName(),
1951 "Intrinsic name not mangled correctly for type arguments!", IF);
1952
Chris Lattnerb241b372009-12-28 09:07:21 +00001953 // If the intrinsic takes MDNode arguments, verify that they are either global
1954 // or are local to *this* function.
Bill Wendlingd942df22010-06-07 19:18:58 +00001955 for (unsigned i = 0, e = CI.getNumArgOperands(); i != e; ++i)
1956 if (MDNode *MD = dyn_cast<MDNode>(CI.getArgOperand(i)))
Duncan Sandse704d9d2010-04-29 16:10:30 +00001957 visitMDNode(*MD, CI.getParent()->getParent());
Victor Hernandez5d301622009-12-18 20:09:14 +00001958
Gordon Henriksen8c33da52007-09-17 20:30:04 +00001959 switch (ID) {
1960 default:
1961 break;
Chandler Carruthc4eab902011-12-12 04:36:02 +00001962 case Intrinsic::ctlz: // llvm.ctlz
1963 case Intrinsic::cttz: // llvm.cttz
1964 Assert1(isa<ConstantInt>(CI.getArgOperand(1)),
1965 "is_zero_undef argument of bit counting intrinsics must be a "
1966 "constant int", &CI);
1967 break;
Victor Hernandez02d574d2010-01-22 19:06:12 +00001968 case Intrinsic::dbg_declare: { // llvm.dbg.declare
Gabor Greifb37a64a2010-06-23 13:56:57 +00001969 Assert1(CI.getArgOperand(0) && isa<MDNode>(CI.getArgOperand(0)),
Victor Hernandez02d574d2010-01-22 19:06:12 +00001970 "invalid llvm.dbg.declare intrinsic call 1", &CI);
Gabor Greifb37a64a2010-06-23 13:56:57 +00001971 MDNode *MD = cast<MDNode>(CI.getArgOperand(0));
Victor Hernandez02d574d2010-01-22 19:06:12 +00001972 Assert1(MD->getNumOperands() == 1,
1973 "invalid llvm.dbg.declare intrinsic call 2", &CI);
Victor Hernandez02d574d2010-01-22 19:06:12 +00001974 } break;
Chris Lattner824b9582008-11-21 16:42:48 +00001975 case Intrinsic::memcpy:
1976 case Intrinsic::memmove:
1977 case Intrinsic::memset:
Gabor Greifb37a64a2010-06-23 13:56:57 +00001978 Assert1(isa<ConstantInt>(CI.getArgOperand(3)),
Chris Lattner259f88e2008-08-23 05:31:10 +00001979 "alignment argument of memory intrinsics must be a constant int",
1980 &CI);
Eli Friedmane65e9ee2011-05-31 20:12:07 +00001981 Assert1(isa<ConstantInt>(CI.getArgOperand(4)),
1982 "isvolatile argument of memory intrinsics must be a constant int",
1983 &CI);
Chris Lattner259f88e2008-08-23 05:31:10 +00001984 break;
Bill Wendling955fdeb2008-08-23 09:46:46 +00001985 case Intrinsic::gcroot:
1986 case Intrinsic::gcwrite:
Chris Lattner415b4142008-08-24 20:46:13 +00001987 case Intrinsic::gcread:
1988 if (ID == Intrinsic::gcroot) {
Gordon Henriksena2cbe6c2008-10-25 16:28:35 +00001989 AllocaInst *AI =
Gabor Greifb37a64a2010-06-23 13:56:57 +00001990 dyn_cast<AllocaInst>(CI.getArgOperand(0)->stripPointerCasts());
Talinc87cfb62010-09-30 20:23:47 +00001991 Assert1(AI, "llvm.gcroot parameter #1 must be an alloca.", &CI);
Gabor Greifb37a64a2010-06-23 13:56:57 +00001992 Assert1(isa<Constant>(CI.getArgOperand(1)),
Chris Lattner415b4142008-08-24 20:46:13 +00001993 "llvm.gcroot parameter #2 must be a constant.", &CI);
Talinc87cfb62010-09-30 20:23:47 +00001994 if (!AI->getType()->getElementType()->isPointerTy()) {
1995 Assert1(!isa<ConstantPointerNull>(CI.getArgOperand(1)),
1996 "llvm.gcroot parameter #1 must either be a pointer alloca, "
1997 "or argument #2 must be a non-null constant.", &CI);
1998 }
Chris Lattner415b4142008-08-24 20:46:13 +00001999 }
Nick Lewycky29ef6592009-09-07 20:44:51 +00002000
Chris Lattner415b4142008-08-24 20:46:13 +00002001 Assert1(CI.getParent()->getParent()->hasGC(),
2002 "Enclosing function does not use GC.", &CI);
2003 break;
Duncan Sandsf51edad2007-09-29 16:25:54 +00002004 case Intrinsic::init_trampoline:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002005 Assert1(isa<Function>(CI.getArgOperand(1)->stripPointerCasts()),
Duncan Sandsf51edad2007-09-29 16:25:54 +00002006 "llvm.init_trampoline parameter #2 must resolve to a function.",
2007 &CI);
Gordon Henriksen27acd3a2007-12-25 02:02:10 +00002008 break;
Chris Lattnerd3745472008-10-16 06:00:36 +00002009 case Intrinsic::prefetch:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002010 Assert1(isa<ConstantInt>(CI.getArgOperand(1)) &&
2011 isa<ConstantInt>(CI.getArgOperand(2)) &&
2012 cast<ConstantInt>(CI.getArgOperand(1))->getZExtValue() < 2 &&
2013 cast<ConstantInt>(CI.getArgOperand(2))->getZExtValue() < 4,
Chris Lattnerd3745472008-10-16 06:00:36 +00002014 "invalid arguments to llvm.prefetch",
2015 &CI);
2016 break;
Bill Wendlingc5b795e2008-11-18 23:09:31 +00002017 case Intrinsic::stackprotector:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002018 Assert1(isa<AllocaInst>(CI.getArgOperand(1)->stripPointerCasts()),
Bill Wendlingc5b795e2008-11-18 23:09:31 +00002019 "llvm.stackprotector parameter #2 must resolve to an alloca.",
2020 &CI);
2021 break;
Nick Lewycky321333e2009-10-13 07:57:33 +00002022 case Intrinsic::lifetime_start:
2023 case Intrinsic::lifetime_end:
2024 case Intrinsic::invariant_start:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002025 Assert1(isa<ConstantInt>(CI.getArgOperand(0)),
Nick Lewycky321333e2009-10-13 07:57:33 +00002026 "size argument of memory use markers must be a constant integer",
2027 &CI);
2028 break;
2029 case Intrinsic::invariant_end:
Gabor Greifb37a64a2010-06-23 13:56:57 +00002030 Assert1(isa<ConstantInt>(CI.getArgOperand(1)),
Nick Lewycky321333e2009-10-13 07:57:33 +00002031 "llvm.invariant.end parameter #2 must be a constant integer", &CI);
2032 break;
Gordon Henriksen8c33da52007-09-17 20:30:04 +00002033 }
Chris Lattnerd231fc32002-04-18 20:37:37 +00002034}
2035
Chris Lattnerd231fc32002-04-18 20:37:37 +00002036//===----------------------------------------------------------------------===//
2037// Implement the public interfaces to this file...
2038//===----------------------------------------------------------------------===//
2039
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002040FunctionPass *llvm::createVerifierPass(VerifierFailureAction action) {
2041 return new Verifier(action);
Chris Lattnerd231fc32002-04-18 20:37:37 +00002042}
2043
Chris Lattner9ce231f2002-08-02 17:37:08 +00002044
Dan Gohmanbcf9f002010-04-08 15:57:10 +00002045/// verifyFunction - Check a function for errors, printing messages on stderr.
2046/// Return true if the function is corrupt.
2047///
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002048bool llvm::verifyFunction(const Function &f, VerifierFailureAction action) {
Chris Lattner2eff8592004-03-14 03:16:15 +00002049 Function &F = const_cast<Function&>(f);
Reid Spencer5cbf9852007-01-30 20:08:39 +00002050 assert(!F.isDeclaration() && "Cannot verify external functions");
Misha Brukmanfd939082005-04-21 23:48:37 +00002051
Jeffrey Yasskinf0356fe2010-01-27 20:34:15 +00002052 FunctionPassManager FPM(F.getParent());
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002053 Verifier *V = new Verifier(action);
Chris Lattner2eff8592004-03-14 03:16:15 +00002054 FPM.add(V);
2055 FPM.run(F);
2056 return V->Broken;
Chris Lattner44d5bd92002-02-20 17:55:43 +00002057}
2058
Misha Brukmanab5c6002004-03-02 00:22:19 +00002059/// verifyModule - Check a module for errors, printing messages on stderr.
2060/// Return true if the module is corrupt.
2061///
Chris Lattner05ac92c2006-07-06 18:02:27 +00002062bool llvm::verifyModule(const Module &M, VerifierFailureAction action,
2063 std::string *ErrorInfo) {
Chris Lattner9ce231f2002-08-02 17:37:08 +00002064 PassManager PM;
Chris Lattnerfdc38c42004-04-02 15:45:08 +00002065 Verifier *V = new Verifier(action);
Chris Lattner9ce231f2002-08-02 17:37:08 +00002066 PM.add(V);
Dan Gohman78f39da2008-06-24 17:47:37 +00002067 PM.run(const_cast<Module&>(M));
Nick Lewycky29ef6592009-09-07 20:44:51 +00002068
Chris Lattner05ac92c2006-07-06 18:02:27 +00002069 if (ErrorInfo && V->Broken)
Chris Lattner37f077a2009-08-23 04:02:03 +00002070 *ErrorInfo = V->MessagesStr.str();
Chris Lattner9ce231f2002-08-02 17:37:08 +00002071 return V->Broken;
Chris Lattner00950542001-06-06 20:29:01 +00002072}