blob: 1fae14f801191027a93deb68268573892e57e93b [file] [log] [blame]
Dan Gohman113902e2010-04-08 18:47:09 +00001//===-- Lint.cpp - Check for common errors in LLVM IR ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass statically checks for common and easily-identified constructs
11// which produce undefined or likely unintended behavior in LLVM IR.
12//
13// It is not a guarantee of correctness, in two ways. First, it isn't
14// comprehensive. There are checks which could be done statically which are
15// not yet implemented. Some of these are indicated by TODO comments, but
16// those aren't comprehensive either. Second, many conditions cannot be
17// checked statically. This pass does no dynamic instrumentation, so it
18// can't check for all possible problems.
19//
20// Another limitation is that it assumes all code will be executed. A store
21// through a null pointer in a basic block which is never reached is harmless,
22// but this pass will warn about it anyway.
Dan Gohman08833552010-04-22 01:30:05 +000023//
Dan Gohman113902e2010-04-08 18:47:09 +000024// Optimization passes may make conditions that this pass checks for more or
25// less obvious. If an optimization pass appears to be introducing a warning,
26// it may be that the optimization pass is merely exposing an existing
27// condition in the code.
28//
29// This code may be run before instcombine. In many cases, instcombine checks
30// for the same kinds of things and turns instructions with undefined behavior
31// into unreachable (or equivalent). Because of this, this pass makes some
32// effort to look through bitcasts and so on.
33//
34//===----------------------------------------------------------------------===//
35
36#include "llvm/Analysis/Passes.h"
37#include "llvm/Analysis/AliasAnalysis.h"
38#include "llvm/Analysis/Lint.h"
39#include "llvm/Analysis/ValueTracking.h"
40#include "llvm/Assembly/Writer.h"
41#include "llvm/Target/TargetData.h"
42#include "llvm/Pass.h"
43#include "llvm/PassManager.h"
44#include "llvm/IntrinsicInst.h"
45#include "llvm/Function.h"
46#include "llvm/Support/CallSite.h"
47#include "llvm/Support/Debug.h"
48#include "llvm/Support/InstVisitor.h"
49#include "llvm/Support/raw_ostream.h"
Dan Gohmanbe02b202010-04-09 01:39:53 +000050#include "llvm/ADT/STLExtras.h"
Dan Gohman113902e2010-04-08 18:47:09 +000051using namespace llvm;
52
53namespace {
Dan Gohman5b61b382010-04-30 19:05:00 +000054 namespace MemRef {
55 static unsigned Read = 1;
56 static unsigned Write = 2;
57 static unsigned Callee = 4;
58 static unsigned Branchee = 8;
59 }
60
Dan Gohman113902e2010-04-08 18:47:09 +000061 class Lint : public FunctionPass, public InstVisitor<Lint> {
62 friend class InstVisitor<Lint>;
63
Dan Gohmanbe02b202010-04-09 01:39:53 +000064 void visitFunction(Function &F);
65
Dan Gohman113902e2010-04-08 18:47:09 +000066 void visitCallSite(CallSite CS);
67 void visitMemoryReference(Instruction &I, Value *Ptr, unsigned Align,
Dan Gohman5b61b382010-04-30 19:05:00 +000068 const Type *Ty, unsigned Flags);
Dan Gohman113902e2010-04-08 18:47:09 +000069
Dan Gohman113902e2010-04-08 18:47:09 +000070 void visitCallInst(CallInst &I);
71 void visitInvokeInst(InvokeInst &I);
72 void visitReturnInst(ReturnInst &I);
73 void visitLoadInst(LoadInst &I);
74 void visitStoreInst(StoreInst &I);
Dan Gohmanbe02b202010-04-09 01:39:53 +000075 void visitXor(BinaryOperator &I);
76 void visitSub(BinaryOperator &I);
Dan Gohmandd98c4d2010-04-08 23:05:57 +000077 void visitLShr(BinaryOperator &I);
78 void visitAShr(BinaryOperator &I);
79 void visitShl(BinaryOperator &I);
Dan Gohman113902e2010-04-08 18:47:09 +000080 void visitSDiv(BinaryOperator &I);
81 void visitUDiv(BinaryOperator &I);
82 void visitSRem(BinaryOperator &I);
83 void visitURem(BinaryOperator &I);
84 void visitAllocaInst(AllocaInst &I);
85 void visitVAArgInst(VAArgInst &I);
86 void visitIndirectBrInst(IndirectBrInst &I);
Dan Gohmandd98c4d2010-04-08 23:05:57 +000087 void visitExtractElementInst(ExtractElementInst &I);
88 void visitInsertElementInst(InsertElementInst &I);
Dan Gohmanbe02b202010-04-09 01:39:53 +000089 void visitUnreachableInst(UnreachableInst &I);
Dan Gohman113902e2010-04-08 18:47:09 +000090
91 public:
92 Module *Mod;
93 AliasAnalysis *AA;
94 TargetData *TD;
95
96 std::string Messages;
97 raw_string_ostream MessagesStr;
98
99 static char ID; // Pass identification, replacement for typeid
100 Lint() : FunctionPass(&ID), MessagesStr(Messages) {}
101
102 virtual bool runOnFunction(Function &F);
103
104 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
105 AU.setPreservesAll();
106 AU.addRequired<AliasAnalysis>();
107 }
108 virtual void print(raw_ostream &O, const Module *M) const {}
109
110 void WriteValue(const Value *V) {
111 if (!V) return;
112 if (isa<Instruction>(V)) {
113 MessagesStr << *V << '\n';
114 } else {
115 WriteAsOperand(MessagesStr, V, true, Mod);
116 MessagesStr << '\n';
117 }
118 }
119
120 void WriteType(const Type *T) {
121 if (!T) return;
122 MessagesStr << ' ';
123 WriteTypeSymbolic(MessagesStr, T, Mod);
124 }
125
126 // CheckFailed - A check failed, so print out the condition and the message
127 // that failed. This provides a nice place to put a breakpoint if you want
128 // to see why something is not correct.
129 void CheckFailed(const Twine &Message,
130 const Value *V1 = 0, const Value *V2 = 0,
131 const Value *V3 = 0, const Value *V4 = 0) {
132 MessagesStr << Message.str() << "\n";
133 WriteValue(V1);
134 WriteValue(V2);
135 WriteValue(V3);
136 WriteValue(V4);
137 }
138
139 void CheckFailed(const Twine &Message, const Value *V1,
140 const Type *T2, const Value *V3 = 0) {
141 MessagesStr << Message.str() << "\n";
142 WriteValue(V1);
143 WriteType(T2);
144 WriteValue(V3);
145 }
146
147 void CheckFailed(const Twine &Message, const Type *T1,
148 const Type *T2 = 0, const Type *T3 = 0) {
149 MessagesStr << Message.str() << "\n";
150 WriteType(T1);
151 WriteType(T2);
152 WriteType(T3);
153 }
154 };
155}
156
157char Lint::ID = 0;
158static RegisterPass<Lint>
159X("lint", "Statically lint-checks LLVM IR", false, true);
160
161// Assert - We know that cond should be true, if not print an error message.
162#define Assert(C, M) \
163 do { if (!(C)) { CheckFailed(M); return; } } while (0)
164#define Assert1(C, M, V1) \
165 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
166#define Assert2(C, M, V1, V2) \
167 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
168#define Assert3(C, M, V1, V2, V3) \
169 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
170#define Assert4(C, M, V1, V2, V3, V4) \
171 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
172
173// Lint::run - This is the main Analysis entry point for a
174// function.
175//
176bool Lint::runOnFunction(Function &F) {
177 Mod = F.getParent();
178 AA = &getAnalysis<AliasAnalysis>();
179 TD = getAnalysisIfAvailable<TargetData>();
180 visit(F);
181 dbgs() << MessagesStr.str();
182 return false;
183}
184
Dan Gohmanbe02b202010-04-09 01:39:53 +0000185void Lint::visitFunction(Function &F) {
186 // This isn't undefined behavior, it's just a little unusual, and it's a
187 // fairly common mistake to neglect to name a function.
188 Assert1(F.hasName() || F.hasLocalLinkage(),
189 "Unusual: Unnamed function with non-local linkage", &F);
Dan Gohman113902e2010-04-08 18:47:09 +0000190}
191
192void Lint::visitCallSite(CallSite CS) {
193 Instruction &I = *CS.getInstruction();
194 Value *Callee = CS.getCalledValue();
195
Dan Gohman5b61b382010-04-30 19:05:00 +0000196 visitMemoryReference(I, Callee, 0, 0, MemRef::Callee);
Dan Gohman113902e2010-04-08 18:47:09 +0000197
198 if (Function *F = dyn_cast<Function>(Callee->stripPointerCasts())) {
199 Assert1(CS.getCallingConv() == F->getCallingConv(),
Dan Gohmanbe02b202010-04-09 01:39:53 +0000200 "Undefined behavior: Caller and callee calling convention differ",
201 &I);
Dan Gohman113902e2010-04-08 18:47:09 +0000202
203 const FunctionType *FT = F->getFunctionType();
204 unsigned NumActualArgs = unsigned(CS.arg_end()-CS.arg_begin());
205
206 Assert1(FT->isVarArg() ?
207 FT->getNumParams() <= NumActualArgs :
208 FT->getNumParams() == NumActualArgs,
Dan Gohmanbe02b202010-04-09 01:39:53 +0000209 "Undefined behavior: Call argument count mismatches callee "
210 "argument count", &I);
Dan Gohman113902e2010-04-08 18:47:09 +0000211
212 // TODO: Check argument types (in case the callee was casted)
213
214 // TODO: Check ABI-significant attributes.
215
216 // TODO: Check noalias attribute.
217
218 // TODO: Check sret attribute.
219 }
220
Dan Gohman113b3e22010-05-26 21:46:36 +0000221 if (CS.isCall() && cast<CallInst>(CS.getInstruction())->isTailCall())
222 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
223 AI != AE; ++AI) {
224 Value *Obj = (*AI)->getUnderlyingObject();
225 Assert1(!isa<AllocaInst>(Obj) && !isa<VAArgInst>(Obj),
226 "Undefined behavior: Call with \"tail\" keyword references "
227 "alloca or va_arg", &I);
228 }
229
Dan Gohman113902e2010-04-08 18:47:09 +0000230
231 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I))
232 switch (II->getIntrinsicID()) {
233 default: break;
234
235 // TODO: Check more intrinsics
236
237 case Intrinsic::memcpy: {
238 MemCpyInst *MCI = cast<MemCpyInst>(&I);
Dan Gohman5b61b382010-04-30 19:05:00 +0000239 visitMemoryReference(I, MCI->getSource(), MCI->getAlignment(), 0,
240 MemRef::Write);
241 visitMemoryReference(I, MCI->getDest(), MCI->getAlignment(), 0,
242 MemRef::Read);
Dan Gohman113902e2010-04-08 18:47:09 +0000243
Dan Gohmanbe02b202010-04-09 01:39:53 +0000244 // Check that the memcpy arguments don't overlap. The AliasAnalysis API
245 // isn't expressive enough for what we really want to do. Known partial
246 // overlap is not distinguished from the case where nothing is known.
Dan Gohman113902e2010-04-08 18:47:09 +0000247 unsigned Size = 0;
248 if (const ConstantInt *Len =
249 dyn_cast<ConstantInt>(MCI->getLength()->stripPointerCasts()))
250 if (Len->getValue().isIntN(32))
251 Size = Len->getValue().getZExtValue();
252 Assert1(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) !=
253 AliasAnalysis::MustAlias,
Dan Gohmanbe02b202010-04-09 01:39:53 +0000254 "Undefined behavior: memcpy source and destination overlap", &I);
Dan Gohman113902e2010-04-08 18:47:09 +0000255 break;
256 }
257 case Intrinsic::memmove: {
258 MemMoveInst *MMI = cast<MemMoveInst>(&I);
Dan Gohman5b61b382010-04-30 19:05:00 +0000259 visitMemoryReference(I, MMI->getSource(), MMI->getAlignment(), 0,
260 MemRef::Write);
261 visitMemoryReference(I, MMI->getDest(), MMI->getAlignment(), 0,
262 MemRef::Read);
Dan Gohman113902e2010-04-08 18:47:09 +0000263 break;
264 }
265 case Intrinsic::memset: {
266 MemSetInst *MSI = cast<MemSetInst>(&I);
Dan Gohman5b61b382010-04-30 19:05:00 +0000267 visitMemoryReference(I, MSI->getDest(), MSI->getAlignment(), 0,
268 MemRef::Write);
Dan Gohman113902e2010-04-08 18:47:09 +0000269 break;
270 }
271
272 case Intrinsic::vastart:
Dan Gohmanbe02b202010-04-09 01:39:53 +0000273 Assert1(I.getParent()->getParent()->isVarArg(),
274 "Undefined behavior: va_start called in a non-varargs function",
275 &I);
276
Dan Gohman5b61b382010-04-30 19:05:00 +0000277 visitMemoryReference(I, CS.getArgument(0), 0, 0,
278 MemRef::Read | MemRef::Write);
Dan Gohman113902e2010-04-08 18:47:09 +0000279 break;
280 case Intrinsic::vacopy:
Dan Gohman5b61b382010-04-30 19:05:00 +0000281 visitMemoryReference(I, CS.getArgument(0), 0, 0, MemRef::Write);
282 visitMemoryReference(I, CS.getArgument(1), 0, 0, MemRef::Read);
Dan Gohman113902e2010-04-08 18:47:09 +0000283 break;
284 case Intrinsic::vaend:
Dan Gohman5b61b382010-04-30 19:05:00 +0000285 visitMemoryReference(I, CS.getArgument(0), 0, 0,
286 MemRef::Read | MemRef::Write);
Dan Gohman113902e2010-04-08 18:47:09 +0000287 break;
Dan Gohman882ddb42010-05-26 22:21:25 +0000288
289 case Intrinsic::stackrestore:
290 // Stackrestore doesn't read or write memory, but it sets the
291 // stack pointer, which the compiler may read from or write to
292 // at any time, so check it for both readability and writeability.
293 visitMemoryReference(I, CS.getArgument(0), 0, 0,
294 MemRef::Read | MemRef::Write);
295 break;
Dan Gohman113902e2010-04-08 18:47:09 +0000296 }
297}
298
299void Lint::visitCallInst(CallInst &I) {
300 return visitCallSite(&I);
301}
302
303void Lint::visitInvokeInst(InvokeInst &I) {
304 return visitCallSite(&I);
305}
306
307void Lint::visitReturnInst(ReturnInst &I) {
308 Function *F = I.getParent()->getParent();
309 Assert1(!F->doesNotReturn(),
Dan Gohmanbe02b202010-04-09 01:39:53 +0000310 "Unusual: Return statement in function with noreturn attribute",
311 &I);
Dan Gohman113902e2010-04-08 18:47:09 +0000312}
313
314// TODO: Add a length argument and check that the reference is in bounds
Dan Gohman113902e2010-04-08 18:47:09 +0000315void Lint::visitMemoryReference(Instruction &I,
Dan Gohman5b61b382010-04-30 19:05:00 +0000316 Value *Ptr, unsigned Align, const Type *Ty,
317 unsigned Flags) {
Dan Gohmanbe02b202010-04-09 01:39:53 +0000318 Value *UnderlyingObject = Ptr->getUnderlyingObject();
319 Assert1(!isa<ConstantPointerNull>(UnderlyingObject),
320 "Undefined behavior: Null pointer dereference", &I);
321 Assert1(!isa<UndefValue>(UnderlyingObject),
322 "Undefined behavior: Undef pointer dereference", &I);
Dan Gohman113902e2010-04-08 18:47:09 +0000323
Dan Gohman5b61b382010-04-30 19:05:00 +0000324 if (Flags & MemRef::Write) {
325 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject))
326 Assert1(!GV->isConstant(),
327 "Undefined behavior: Write to read-only memory", &I);
328 Assert1(!isa<Function>(UnderlyingObject) &&
329 !isa<BlockAddress>(UnderlyingObject),
330 "Undefined behavior: Write to text section", &I);
331 }
332 if (Flags & MemRef::Read) {
333 Assert1(!isa<Function>(UnderlyingObject),
334 "Unusual: Load from function body", &I);
335 Assert1(!isa<BlockAddress>(UnderlyingObject),
336 "Undefined behavior: Load from block address", &I);
337 }
338 if (Flags & MemRef::Callee) {
339 Assert1(!isa<BlockAddress>(UnderlyingObject),
340 "Undefined behavior: Call to block address", &I);
341 }
342 if (Flags & MemRef::Branchee) {
343 Assert1(!isa<Constant>(UnderlyingObject) ||
344 isa<BlockAddress>(UnderlyingObject),
345 "Undefined behavior: Branch to non-blockaddress", &I);
346 }
347
Dan Gohman113902e2010-04-08 18:47:09 +0000348 if (TD) {
349 if (Align == 0 && Ty) Align = TD->getABITypeAlignment(Ty);
350
351 if (Align != 0) {
352 unsigned BitWidth = TD->getTypeSizeInBits(Ptr->getType());
353 APInt Mask = APInt::getAllOnesValue(BitWidth),
354 KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
355 ComputeMaskedBits(Ptr, Mask, KnownZero, KnownOne, TD);
356 Assert1(!(KnownOne & APInt::getLowBitsSet(BitWidth, Log2_32(Align))),
Dan Gohmanbe02b202010-04-09 01:39:53 +0000357 "Undefined behavior: Memory reference address is misaligned", &I);
Dan Gohman113902e2010-04-08 18:47:09 +0000358 }
359 }
360}
361
362void Lint::visitLoadInst(LoadInst &I) {
Dan Gohman5b61b382010-04-30 19:05:00 +0000363 visitMemoryReference(I, I.getPointerOperand(), I.getAlignment(), I.getType(),
364 MemRef::Read);
Dan Gohman113902e2010-04-08 18:47:09 +0000365}
366
367void Lint::visitStoreInst(StoreInst &I) {
368 visitMemoryReference(I, I.getPointerOperand(), I.getAlignment(),
Dan Gohman5b61b382010-04-30 19:05:00 +0000369 I.getOperand(0)->getType(), MemRef::Write);
Dan Gohman113902e2010-04-08 18:47:09 +0000370}
371
Dan Gohmanbe02b202010-04-09 01:39:53 +0000372void Lint::visitXor(BinaryOperator &I) {
373 Assert1(!isa<UndefValue>(I.getOperand(0)) ||
374 !isa<UndefValue>(I.getOperand(1)),
375 "Undefined result: xor(undef, undef)", &I);
376}
377
378void Lint::visitSub(BinaryOperator &I) {
379 Assert1(!isa<UndefValue>(I.getOperand(0)) ||
380 !isa<UndefValue>(I.getOperand(1)),
381 "Undefined result: sub(undef, undef)", &I);
382}
383
Dan Gohmandd98c4d2010-04-08 23:05:57 +0000384void Lint::visitLShr(BinaryOperator &I) {
385 if (ConstantInt *CI =
386 dyn_cast<ConstantInt>(I.getOperand(1)->stripPointerCasts()))
387 Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
Dan Gohmanbe02b202010-04-09 01:39:53 +0000388 "Undefined result: Shift count out of range", &I);
Dan Gohmandd98c4d2010-04-08 23:05:57 +0000389}
390
391void Lint::visitAShr(BinaryOperator &I) {
392 if (ConstantInt *CI =
393 dyn_cast<ConstantInt>(I.getOperand(1)->stripPointerCasts()))
394 Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
Dan Gohmanbe02b202010-04-09 01:39:53 +0000395 "Undefined result: Shift count out of range", &I);
Dan Gohmandd98c4d2010-04-08 23:05:57 +0000396}
397
398void Lint::visitShl(BinaryOperator &I) {
399 if (ConstantInt *CI =
400 dyn_cast<ConstantInt>(I.getOperand(1)->stripPointerCasts()))
401 Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
Dan Gohmanbe02b202010-04-09 01:39:53 +0000402 "Undefined result: Shift count out of range", &I);
Dan Gohmandd98c4d2010-04-08 23:05:57 +0000403}
404
Dan Gohman113902e2010-04-08 18:47:09 +0000405static bool isZero(Value *V, TargetData *TD) {
Dan Gohmanbe02b202010-04-09 01:39:53 +0000406 // Assume undef could be zero.
407 if (isa<UndefValue>(V)) return true;
408
Dan Gohman113902e2010-04-08 18:47:09 +0000409 unsigned BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
410 APInt Mask = APInt::getAllOnesValue(BitWidth),
411 KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
412 ComputeMaskedBits(V, Mask, KnownZero, KnownOne, TD);
413 return KnownZero.isAllOnesValue();
414}
415
416void Lint::visitSDiv(BinaryOperator &I) {
Dan Gohmanbe02b202010-04-09 01:39:53 +0000417 Assert1(!isZero(I.getOperand(1), TD),
418 "Undefined behavior: Division by zero", &I);
Dan Gohman113902e2010-04-08 18:47:09 +0000419}
420
421void Lint::visitUDiv(BinaryOperator &I) {
Dan Gohmanbe02b202010-04-09 01:39:53 +0000422 Assert1(!isZero(I.getOperand(1), TD),
423 "Undefined behavior: Division by zero", &I);
Dan Gohman113902e2010-04-08 18:47:09 +0000424}
425
426void Lint::visitSRem(BinaryOperator &I) {
Dan Gohmanbe02b202010-04-09 01:39:53 +0000427 Assert1(!isZero(I.getOperand(1), TD),
428 "Undefined behavior: Division by zero", &I);
Dan Gohman113902e2010-04-08 18:47:09 +0000429}
430
431void Lint::visitURem(BinaryOperator &I) {
Dan Gohmanbe02b202010-04-09 01:39:53 +0000432 Assert1(!isZero(I.getOperand(1), TD),
433 "Undefined behavior: Division by zero", &I);
Dan Gohman113902e2010-04-08 18:47:09 +0000434}
435
436void Lint::visitAllocaInst(AllocaInst &I) {
437 if (isa<ConstantInt>(I.getArraySize()))
438 // This isn't undefined behavior, it's just an obvious pessimization.
439 Assert1(&I.getParent()->getParent()->getEntryBlock() == I.getParent(),
Dan Gohmanbe02b202010-04-09 01:39:53 +0000440 "Pessimization: Static alloca outside of entry block", &I);
Dan Gohman113902e2010-04-08 18:47:09 +0000441}
442
443void Lint::visitVAArgInst(VAArgInst &I) {
Dan Gohman5b61b382010-04-30 19:05:00 +0000444 visitMemoryReference(I, I.getOperand(0), 0, 0,
445 MemRef::Read | MemRef::Write);
Dan Gohman113902e2010-04-08 18:47:09 +0000446}
447
448void Lint::visitIndirectBrInst(IndirectBrInst &I) {
Dan Gohman5b61b382010-04-30 19:05:00 +0000449 visitMemoryReference(I, I.getAddress(), 0, 0, MemRef::Branchee);
Dan Gohman113902e2010-04-08 18:47:09 +0000450}
451
Dan Gohmandd98c4d2010-04-08 23:05:57 +0000452void Lint::visitExtractElementInst(ExtractElementInst &I) {
453 if (ConstantInt *CI =
454 dyn_cast<ConstantInt>(I.getIndexOperand()->stripPointerCasts()))
455 Assert1(CI->getValue().ult(I.getVectorOperandType()->getNumElements()),
Dan Gohmanbe02b202010-04-09 01:39:53 +0000456 "Undefined result: extractelement index out of range", &I);
Dan Gohmandd98c4d2010-04-08 23:05:57 +0000457}
458
459void Lint::visitInsertElementInst(InsertElementInst &I) {
460 if (ConstantInt *CI =
461 dyn_cast<ConstantInt>(I.getOperand(2)->stripPointerCasts()))
462 Assert1(CI->getValue().ult(I.getType()->getNumElements()),
Dan Gohmanbe02b202010-04-09 01:39:53 +0000463 "Undefined result: insertelement index out of range", &I);
464}
465
466void Lint::visitUnreachableInst(UnreachableInst &I) {
467 // This isn't undefined behavior, it's merely suspicious.
468 Assert1(&I == I.getParent()->begin() ||
469 prior(BasicBlock::iterator(&I))->mayHaveSideEffects(),
470 "Unusual: unreachable immediately preceded by instruction without "
471 "side effects", &I);
Dan Gohmandd98c4d2010-04-08 23:05:57 +0000472}
473
Dan Gohman113902e2010-04-08 18:47:09 +0000474//===----------------------------------------------------------------------===//
475// Implement the public interfaces to this file...
476//===----------------------------------------------------------------------===//
477
478FunctionPass *llvm::createLintPass() {
479 return new Lint();
480}
481
482/// lintFunction - Check a function for errors, printing messages on stderr.
483///
484void llvm::lintFunction(const Function &f) {
485 Function &F = const_cast<Function&>(f);
486 assert(!F.isDeclaration() && "Cannot lint external functions");
487
488 FunctionPassManager FPM(F.getParent());
489 Lint *V = new Lint();
490 FPM.add(V);
491 FPM.run(F);
492}
493
494/// lintModule - Check a module for errors, printing messages on stderr.
495/// Return true if the module is corrupt.
496///
497void llvm::lintModule(const Module &M, std::string *ErrorInfo) {
498 PassManager PM;
499 Lint *V = new Lint();
500 PM.add(V);
501 PM.run(const_cast<Module&>(M));
502
503 if (ErrorInfo)
504 *ErrorInfo = V->MessagesStr.str();
505}