1 //===-- Lint.cpp - Check for common errors in LLVM IR ---------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This pass statically checks for common and easily-identified constructs
11 // which produce undefined or likely unintended behavior in LLVM IR.
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.
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. This is the main reason why most
23 // of these checks live here instead of in the Verifier pass.
25 // Optimization passes may make conditions that this pass checks for more or
26 // less obvious. If an optimization pass appears to be introducing a warning,
27 // it may be that the optimization pass is merely exposing an existing
28 // condition in the code.
30 // This code may be run before instcombine. In many cases, instcombine checks
31 // for the same kinds of things and turns instructions with undefined behavior
32 // into unreachable (or equivalent). Because of this, this pass makes some
33 // effort to look through bitcasts and so on.
35 //===----------------------------------------------------------------------===//
37 #include "llvm/Analysis/Lint.h"
38 #include "llvm/ADT/STLExtras.h"
39 #include "llvm/Analysis/AliasAnalysis.h"
40 #include "llvm/Analysis/ConstantFolding.h"
41 #include "llvm/Analysis/InstructionSimplify.h"
42 #include "llvm/Analysis/Loads.h"
43 #include "llvm/Analysis/Passes.h"
44 #include "llvm/Analysis/ValueTracking.h"
45 #include "llvm/IR/DataLayout.h"
46 #include "llvm/IR/Dominators.h"
47 #include "llvm/IR/Function.h"
48 #include "llvm/IR/IntrinsicInst.h"
49 #include "llvm/InstVisitor.h"
50 #include "llvm/Pass.h"
51 #include "llvm/PassManager.h"
52 #include "llvm/Support/CallSite.h"
53 #include "llvm/Support/Debug.h"
54 #include "llvm/Support/raw_ostream.h"
55 #include "llvm/Target/TargetLibraryInfo.h"
60 static unsigned Read = 1;
61 static unsigned Write = 2;
62 static unsigned Callee = 4;
63 static unsigned Branchee = 8;
66 class Lint : public FunctionPass, public InstVisitor<Lint> {
67 friend class InstVisitor<Lint>;
69 void visitFunction(Function &F);
71 void visitCallSite(CallSite CS);
72 void visitMemoryReference(Instruction &I, Value *Ptr,
73 uint64_t Size, unsigned Align,
74 Type *Ty, unsigned Flags);
76 void visitCallInst(CallInst &I);
77 void visitInvokeInst(InvokeInst &I);
78 void visitReturnInst(ReturnInst &I);
79 void visitLoadInst(LoadInst &I);
80 void visitStoreInst(StoreInst &I);
81 void visitXor(BinaryOperator &I);
82 void visitSub(BinaryOperator &I);
83 void visitLShr(BinaryOperator &I);
84 void visitAShr(BinaryOperator &I);
85 void visitShl(BinaryOperator &I);
86 void visitSDiv(BinaryOperator &I);
87 void visitUDiv(BinaryOperator &I);
88 void visitSRem(BinaryOperator &I);
89 void visitURem(BinaryOperator &I);
90 void visitAllocaInst(AllocaInst &I);
91 void visitVAArgInst(VAArgInst &I);
92 void visitIndirectBrInst(IndirectBrInst &I);
93 void visitExtractElementInst(ExtractElementInst &I);
94 void visitInsertElementInst(InsertElementInst &I);
95 void visitUnreachableInst(UnreachableInst &I);
97 Value *findValue(Value *V, bool OffsetOk) const;
98 Value *findValueImpl(Value *V, bool OffsetOk,
99 SmallPtrSet<Value *, 4> &Visited) const;
106 TargetLibraryInfo *TLI;
108 std::string Messages;
109 raw_string_ostream MessagesStr;
111 static char ID; // Pass identification, replacement for typeid
112 Lint() : FunctionPass(ID), MessagesStr(Messages) {
113 initializeLintPass(*PassRegistry::getPassRegistry());
116 virtual bool runOnFunction(Function &F);
118 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
119 AU.setPreservesAll();
120 AU.addRequired<AliasAnalysis>();
121 AU.addRequired<TargetLibraryInfo>();
122 AU.addRequired<DominatorTree>();
124 virtual void print(raw_ostream &O, const Module *M) const {}
126 void WriteValue(const Value *V) {
128 if (isa<Instruction>(V)) {
129 MessagesStr << *V << '\n';
131 V->printAsOperand(MessagesStr, true, Mod);
136 // CheckFailed - A check failed, so print out the condition and the message
137 // that failed. This provides a nice place to put a breakpoint if you want
138 // to see why something is not correct.
139 void CheckFailed(const Twine &Message,
140 const Value *V1 = 0, const Value *V2 = 0,
141 const Value *V3 = 0, const Value *V4 = 0) {
142 MessagesStr << Message.str() << "\n";
152 INITIALIZE_PASS_BEGIN(Lint, "lint", "Statically lint-checks LLVM IR",
154 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
155 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
156 INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
157 INITIALIZE_PASS_END(Lint, "lint", "Statically lint-checks LLVM IR",
160 // Assert - We know that cond should be true, if not print an error message.
161 #define Assert(C, M) \
162 do { if (!(C)) { CheckFailed(M); return; } } while (0)
163 #define Assert1(C, M, V1) \
164 do { if (!(C)) { CheckFailed(M, V1); return; } } while (0)
165 #define Assert2(C, M, V1, V2) \
166 do { if (!(C)) { CheckFailed(M, V1, V2); return; } } while (0)
167 #define Assert3(C, M, V1, V2, V3) \
168 do { if (!(C)) { CheckFailed(M, V1, V2, V3); return; } } while (0)
169 #define Assert4(C, M, V1, V2, V3, V4) \
170 do { if (!(C)) { CheckFailed(M, V1, V2, V3, V4); return; } } while (0)
172 // Lint::run - This is the main Analysis entry point for a
175 bool Lint::runOnFunction(Function &F) {
177 AA = &getAnalysis<AliasAnalysis>();
178 DT = &getAnalysis<DominatorTree>();
179 TD = getAnalysisIfAvailable<DataLayout>();
180 TLI = &getAnalysis<TargetLibraryInfo>();
182 dbgs() << MessagesStr.str();
187 void Lint::visitFunction(Function &F) {
188 // This isn't undefined behavior, it's just a little unusual, and it's a
189 // fairly common mistake to neglect to name a function.
190 Assert1(F.hasName() || F.hasLocalLinkage(),
191 "Unusual: Unnamed function with non-local linkage", &F);
193 // TODO: Check for irreducible control flow.
196 void Lint::visitCallSite(CallSite CS) {
197 Instruction &I = *CS.getInstruction();
198 Value *Callee = CS.getCalledValue();
200 visitMemoryReference(I, Callee, AliasAnalysis::UnknownSize,
201 0, 0, MemRef::Callee);
203 if (Function *F = dyn_cast<Function>(findValue(Callee, /*OffsetOk=*/false))) {
204 Assert1(CS.getCallingConv() == F->getCallingConv(),
205 "Undefined behavior: Caller and callee calling convention differ",
208 FunctionType *FT = F->getFunctionType();
209 unsigned NumActualArgs = CS.arg_size();
211 Assert1(FT->isVarArg() ?
212 FT->getNumParams() <= NumActualArgs :
213 FT->getNumParams() == NumActualArgs,
214 "Undefined behavior: Call argument count mismatches callee "
215 "argument count", &I);
217 Assert1(FT->getReturnType() == I.getType(),
218 "Undefined behavior: Call return type mismatches "
219 "callee return type", &I);
221 // Check argument types (in case the callee was casted) and attributes.
222 // TODO: Verify that caller and callee attributes are compatible.
223 Function::arg_iterator PI = F->arg_begin(), PE = F->arg_end();
224 CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
225 for (; AI != AE; ++AI) {
228 Argument *Formal = PI++;
229 Assert1(Formal->getType() == Actual->getType(),
230 "Undefined behavior: Call argument type mismatches "
231 "callee parameter type", &I);
233 // Check that noalias arguments don't alias other arguments. This is
234 // not fully precise because we don't know the sizes of the dereferenced
236 if (Formal->hasNoAliasAttr() && Actual->getType()->isPointerTy())
237 for (CallSite::arg_iterator BI = CS.arg_begin(); BI != AE; ++BI)
238 if (AI != BI && (*BI)->getType()->isPointerTy()) {
239 AliasAnalysis::AliasResult Result = AA->alias(*AI, *BI);
240 Assert1(Result != AliasAnalysis::MustAlias &&
241 Result != AliasAnalysis::PartialAlias,
242 "Unusual: noalias argument aliases another argument", &I);
245 // Check that an sret argument points to valid memory.
246 if (Formal->hasStructRetAttr() && Actual->getType()->isPointerTy()) {
248 cast<PointerType>(Formal->getType())->getElementType();
249 visitMemoryReference(I, Actual, AA->getTypeStoreSize(Ty),
250 TD ? TD->getABITypeAlignment(Ty) : 0,
251 Ty, MemRef::Read | MemRef::Write);
257 if (CS.isCall() && cast<CallInst>(CS.getInstruction())->isTailCall())
258 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
260 Value *Obj = findValue(*AI, /*OffsetOk=*/true);
261 Assert1(!isa<AllocaInst>(Obj),
262 "Undefined behavior: Call with \"tail\" keyword references "
267 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I))
268 switch (II->getIntrinsicID()) {
271 // TODO: Check more intrinsics
273 case Intrinsic::memcpy: {
274 MemCpyInst *MCI = cast<MemCpyInst>(&I);
275 // TODO: If the size is known, use it.
276 visitMemoryReference(I, MCI->getDest(), AliasAnalysis::UnknownSize,
277 MCI->getAlignment(), 0,
279 visitMemoryReference(I, MCI->getSource(), AliasAnalysis::UnknownSize,
280 MCI->getAlignment(), 0,
283 // Check that the memcpy arguments don't overlap. The AliasAnalysis API
284 // isn't expressive enough for what we really want to do. Known partial
285 // overlap is not distinguished from the case where nothing is known.
287 if (const ConstantInt *Len =
288 dyn_cast<ConstantInt>(findValue(MCI->getLength(),
289 /*OffsetOk=*/false)))
290 if (Len->getValue().isIntN(32))
291 Size = Len->getValue().getZExtValue();
292 Assert1(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) !=
293 AliasAnalysis::MustAlias,
294 "Undefined behavior: memcpy source and destination overlap", &I);
297 case Intrinsic::memmove: {
298 MemMoveInst *MMI = cast<MemMoveInst>(&I);
299 // TODO: If the size is known, use it.
300 visitMemoryReference(I, MMI->getDest(), AliasAnalysis::UnknownSize,
301 MMI->getAlignment(), 0,
303 visitMemoryReference(I, MMI->getSource(), AliasAnalysis::UnknownSize,
304 MMI->getAlignment(), 0,
308 case Intrinsic::memset: {
309 MemSetInst *MSI = cast<MemSetInst>(&I);
310 // TODO: If the size is known, use it.
311 visitMemoryReference(I, MSI->getDest(), AliasAnalysis::UnknownSize,
312 MSI->getAlignment(), 0,
317 case Intrinsic::vastart:
318 Assert1(I.getParent()->getParent()->isVarArg(),
319 "Undefined behavior: va_start called in a non-varargs function",
322 visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize,
323 0, 0, MemRef::Read | MemRef::Write);
325 case Intrinsic::vacopy:
326 visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize,
327 0, 0, MemRef::Write);
328 visitMemoryReference(I, CS.getArgument(1), AliasAnalysis::UnknownSize,
331 case Intrinsic::vaend:
332 visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize,
333 0, 0, MemRef::Read | MemRef::Write);
336 case Intrinsic::stackrestore:
337 // Stackrestore doesn't read or write memory, but it sets the
338 // stack pointer, which the compiler may read from or write to
339 // at any time, so check it for both readability and writeability.
340 visitMemoryReference(I, CS.getArgument(0), AliasAnalysis::UnknownSize,
341 0, 0, MemRef::Read | MemRef::Write);
346 void Lint::visitCallInst(CallInst &I) {
347 return visitCallSite(&I);
350 void Lint::visitInvokeInst(InvokeInst &I) {
351 return visitCallSite(&I);
354 void Lint::visitReturnInst(ReturnInst &I) {
355 Function *F = I.getParent()->getParent();
356 Assert1(!F->doesNotReturn(),
357 "Unusual: Return statement in function with noreturn attribute",
360 if (Value *V = I.getReturnValue()) {
361 Value *Obj = findValue(V, /*OffsetOk=*/true);
362 Assert1(!isa<AllocaInst>(Obj),
363 "Unusual: Returning alloca value", &I);
367 // TODO: Check that the reference is in bounds.
368 // TODO: Check readnone/readonly function attributes.
369 void Lint::visitMemoryReference(Instruction &I,
370 Value *Ptr, uint64_t Size, unsigned Align,
371 Type *Ty, unsigned Flags) {
372 // If no memory is being referenced, it doesn't matter if the pointer
377 Value *UnderlyingObject = findValue(Ptr, /*OffsetOk=*/true);
378 Assert1(!isa<ConstantPointerNull>(UnderlyingObject),
379 "Undefined behavior: Null pointer dereference", &I);
380 Assert1(!isa<UndefValue>(UnderlyingObject),
381 "Undefined behavior: Undef pointer dereference", &I);
382 Assert1(!isa<ConstantInt>(UnderlyingObject) ||
383 !cast<ConstantInt>(UnderlyingObject)->isAllOnesValue(),
384 "Unusual: All-ones pointer dereference", &I);
385 Assert1(!isa<ConstantInt>(UnderlyingObject) ||
386 !cast<ConstantInt>(UnderlyingObject)->isOne(),
387 "Unusual: Address one pointer dereference", &I);
389 if (Flags & MemRef::Write) {
390 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject))
391 Assert1(!GV->isConstant(),
392 "Undefined behavior: Write to read-only memory", &I);
393 Assert1(!isa<Function>(UnderlyingObject) &&
394 !isa<BlockAddress>(UnderlyingObject),
395 "Undefined behavior: Write to text section", &I);
397 if (Flags & MemRef::Read) {
398 Assert1(!isa<Function>(UnderlyingObject),
399 "Unusual: Load from function body", &I);
400 Assert1(!isa<BlockAddress>(UnderlyingObject),
401 "Undefined behavior: Load from block address", &I);
403 if (Flags & MemRef::Callee) {
404 Assert1(!isa<BlockAddress>(UnderlyingObject),
405 "Undefined behavior: Call to block address", &I);
407 if (Flags & MemRef::Branchee) {
408 Assert1(!isa<Constant>(UnderlyingObject) ||
409 isa<BlockAddress>(UnderlyingObject),
410 "Undefined behavior: Branch to non-blockaddress", &I);
413 // Check for buffer overflows and misalignment.
414 // Only handles memory references that read/write something simple like an
415 // alloca instruction or a global variable.
417 if (Value *Base = GetPointerBaseWithConstantOffset(Ptr, Offset, TD)) {
418 // OK, so the access is to a constant offset from Ptr. Check that Ptr is
419 // something we can handle and if so extract the size of this base object
420 // along with its alignment.
421 uint64_t BaseSize = AliasAnalysis::UnknownSize;
422 unsigned BaseAlign = 0;
424 if (AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
425 Type *ATy = AI->getAllocatedType();
426 if (TD && !AI->isArrayAllocation() && ATy->isSized())
427 BaseSize = TD->getTypeAllocSize(ATy);
428 BaseAlign = AI->getAlignment();
429 if (TD && BaseAlign == 0 && ATy->isSized())
430 BaseAlign = TD->getABITypeAlignment(ATy);
431 } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
432 // If the global may be defined differently in another compilation unit
433 // then don't warn about funky memory accesses.
434 if (GV->hasDefinitiveInitializer()) {
435 Type *GTy = GV->getType()->getElementType();
436 if (TD && GTy->isSized())
437 BaseSize = TD->getTypeAllocSize(GTy);
438 BaseAlign = GV->getAlignment();
439 if (TD && BaseAlign == 0 && GTy->isSized())
440 BaseAlign = TD->getABITypeAlignment(GTy);
444 // Accesses from before the start or after the end of the object are not
446 Assert1(Size == AliasAnalysis::UnknownSize ||
447 BaseSize == AliasAnalysis::UnknownSize ||
448 (Offset >= 0 && Offset + Size <= BaseSize),
449 "Undefined behavior: Buffer overflow", &I);
451 // Accesses that say that the memory is more aligned than it is are not
453 if (TD && Align == 0 && Ty && Ty->isSized())
454 Align = TD->getABITypeAlignment(Ty);
455 Assert1(!BaseAlign || Align <= MinAlign(BaseAlign, Offset),
456 "Undefined behavior: Memory reference address is misaligned", &I);
460 void Lint::visitLoadInst(LoadInst &I) {
461 visitMemoryReference(I, I.getPointerOperand(),
462 AA->getTypeStoreSize(I.getType()), I.getAlignment(),
463 I.getType(), MemRef::Read);
466 void Lint::visitStoreInst(StoreInst &I) {
467 visitMemoryReference(I, I.getPointerOperand(),
468 AA->getTypeStoreSize(I.getOperand(0)->getType()),
470 I.getOperand(0)->getType(), MemRef::Write);
473 void Lint::visitXor(BinaryOperator &I) {
474 Assert1(!isa<UndefValue>(I.getOperand(0)) ||
475 !isa<UndefValue>(I.getOperand(1)),
476 "Undefined result: xor(undef, undef)", &I);
479 void Lint::visitSub(BinaryOperator &I) {
480 Assert1(!isa<UndefValue>(I.getOperand(0)) ||
481 !isa<UndefValue>(I.getOperand(1)),
482 "Undefined result: sub(undef, undef)", &I);
485 void Lint::visitLShr(BinaryOperator &I) {
486 if (ConstantInt *CI =
487 dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false)))
488 Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
489 "Undefined result: Shift count out of range", &I);
492 void Lint::visitAShr(BinaryOperator &I) {
493 if (ConstantInt *CI =
494 dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false)))
495 Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
496 "Undefined result: Shift count out of range", &I);
499 void Lint::visitShl(BinaryOperator &I) {
500 if (ConstantInt *CI =
501 dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false)))
502 Assert1(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
503 "Undefined result: Shift count out of range", &I);
506 static bool isZero(Value *V, DataLayout *DL) {
507 // Assume undef could be zero.
508 if (isa<UndefValue>(V))
511 VectorType *VecTy = dyn_cast<VectorType>(V->getType());
513 unsigned BitWidth = V->getType()->getIntegerBitWidth();
514 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
515 ComputeMaskedBits(V, KnownZero, KnownOne, DL);
516 return KnownZero.isAllOnesValue();
519 // Per-component check doesn't work with zeroinitializer
520 Constant *C = dyn_cast<Constant>(V);
524 if (C->isZeroValue())
527 // For a vector, KnownZero will only be true if all values are zero, so check
528 // this per component
529 unsigned BitWidth = VecTy->getElementType()->getIntegerBitWidth();
530 for (unsigned I = 0, N = VecTy->getNumElements(); I != N; ++I) {
531 Constant *Elem = C->getAggregateElement(I);
532 if (isa<UndefValue>(Elem))
535 APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
536 ComputeMaskedBits(Elem, KnownZero, KnownOne, DL);
537 if (KnownZero.isAllOnesValue())
544 void Lint::visitSDiv(BinaryOperator &I) {
545 Assert1(!isZero(I.getOperand(1), TD),
546 "Undefined behavior: Division by zero", &I);
549 void Lint::visitUDiv(BinaryOperator &I) {
550 Assert1(!isZero(I.getOperand(1), TD),
551 "Undefined behavior: Division by zero", &I);
554 void Lint::visitSRem(BinaryOperator &I) {
555 Assert1(!isZero(I.getOperand(1), TD),
556 "Undefined behavior: Division by zero", &I);
559 void Lint::visitURem(BinaryOperator &I) {
560 Assert1(!isZero(I.getOperand(1), TD),
561 "Undefined behavior: Division by zero", &I);
564 void Lint::visitAllocaInst(AllocaInst &I) {
565 if (isa<ConstantInt>(I.getArraySize()))
566 // This isn't undefined behavior, it's just an obvious pessimization.
567 Assert1(&I.getParent()->getParent()->getEntryBlock() == I.getParent(),
568 "Pessimization: Static alloca outside of entry block", &I);
570 // TODO: Check for an unusual size (MSB set?)
573 void Lint::visitVAArgInst(VAArgInst &I) {
574 visitMemoryReference(I, I.getOperand(0), AliasAnalysis::UnknownSize, 0, 0,
575 MemRef::Read | MemRef::Write);
578 void Lint::visitIndirectBrInst(IndirectBrInst &I) {
579 visitMemoryReference(I, I.getAddress(), AliasAnalysis::UnknownSize, 0, 0,
582 Assert1(I.getNumDestinations() != 0,
583 "Undefined behavior: indirectbr with no destinations", &I);
586 void Lint::visitExtractElementInst(ExtractElementInst &I) {
587 if (ConstantInt *CI =
588 dyn_cast<ConstantInt>(findValue(I.getIndexOperand(),
589 /*OffsetOk=*/false)))
590 Assert1(CI->getValue().ult(I.getVectorOperandType()->getNumElements()),
591 "Undefined result: extractelement index out of range", &I);
594 void Lint::visitInsertElementInst(InsertElementInst &I) {
595 if (ConstantInt *CI =
596 dyn_cast<ConstantInt>(findValue(I.getOperand(2),
597 /*OffsetOk=*/false)))
598 Assert1(CI->getValue().ult(I.getType()->getNumElements()),
599 "Undefined result: insertelement index out of range", &I);
602 void Lint::visitUnreachableInst(UnreachableInst &I) {
603 // This isn't undefined behavior, it's merely suspicious.
604 Assert1(&I == I.getParent()->begin() ||
605 prior(BasicBlock::iterator(&I))->mayHaveSideEffects(),
606 "Unusual: unreachable immediately preceded by instruction without "
610 /// findValue - Look through bitcasts and simple memory reference patterns
611 /// to identify an equivalent, but more informative, value. If OffsetOk
612 /// is true, look through getelementptrs with non-zero offsets too.
614 /// Most analysis passes don't require this logic, because instcombine
615 /// will simplify most of these kinds of things away. But it's a goal of
616 /// this Lint pass to be useful even on non-optimized IR.
617 Value *Lint::findValue(Value *V, bool OffsetOk) const {
618 SmallPtrSet<Value *, 4> Visited;
619 return findValueImpl(V, OffsetOk, Visited);
622 /// findValueImpl - Implementation helper for findValue.
623 Value *Lint::findValueImpl(Value *V, bool OffsetOk,
624 SmallPtrSet<Value *, 4> &Visited) const {
625 // Detect self-referential values.
626 if (!Visited.insert(V))
627 return UndefValue::get(V->getType());
629 // TODO: Look through sext or zext cast, when the result is known to
630 // be interpreted as signed or unsigned, respectively.
631 // TODO: Look through eliminable cast pairs.
632 // TODO: Look through calls with unique return values.
633 // TODO: Look through vector insert/extract/shuffle.
634 V = OffsetOk ? GetUnderlyingObject(V, TD) : V->stripPointerCasts();
635 if (LoadInst *L = dyn_cast<LoadInst>(V)) {
636 BasicBlock::iterator BBI = L;
637 BasicBlock *BB = L->getParent();
638 SmallPtrSet<BasicBlock *, 4> VisitedBlocks;
640 if (!VisitedBlocks.insert(BB)) break;
641 if (Value *U = FindAvailableLoadedValue(L->getPointerOperand(),
643 return findValueImpl(U, OffsetOk, Visited);
644 if (BBI != BB->begin()) break;
645 BB = BB->getUniquePredecessor();
649 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
650 if (Value *W = PN->hasConstantValue())
652 return findValueImpl(W, OffsetOk, Visited);
653 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
654 if (CI->isNoopCast(TD ? TD->getIntPtrType(V->getContext()) :
655 Type::getInt64Ty(V->getContext())))
656 return findValueImpl(CI->getOperand(0), OffsetOk, Visited);
657 } else if (ExtractValueInst *Ex = dyn_cast<ExtractValueInst>(V)) {
658 if (Value *W = FindInsertedValue(Ex->getAggregateOperand(),
661 return findValueImpl(W, OffsetOk, Visited);
662 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
663 // Same as above, but for ConstantExpr instead of Instruction.
664 if (Instruction::isCast(CE->getOpcode())) {
665 if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()),
666 CE->getOperand(0)->getType(),
668 TD ? TD->getIntPtrType(V->getContext()) :
669 Type::getInt64Ty(V->getContext())))
670 return findValueImpl(CE->getOperand(0), OffsetOk, Visited);
671 } else if (CE->getOpcode() == Instruction::ExtractValue) {
672 ArrayRef<unsigned> Indices = CE->getIndices();
673 if (Value *W = FindInsertedValue(CE->getOperand(0), Indices))
675 return findValueImpl(W, OffsetOk, Visited);
679 // As a last resort, try SimplifyInstruction or constant folding.
680 if (Instruction *Inst = dyn_cast<Instruction>(V)) {
681 if (Value *W = SimplifyInstruction(Inst, TD, TLI, DT))
682 return findValueImpl(W, OffsetOk, Visited);
683 } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
684 if (Value *W = ConstantFoldConstantExpression(CE, TD, TLI))
686 return findValueImpl(W, OffsetOk, Visited);
692 //===----------------------------------------------------------------------===//
693 // Implement the public interfaces to this file...
694 //===----------------------------------------------------------------------===//
696 FunctionPass *llvm::createLintPass() {
700 /// lintFunction - Check a function for errors, printing messages on stderr.
702 void llvm::lintFunction(const Function &f) {
703 Function &F = const_cast<Function&>(f);
704 assert(!F.isDeclaration() && "Cannot lint external functions");
706 FunctionPassManager FPM(F.getParent());
707 Lint *V = new Lint();
712 /// lintModule - Check a module for errors, printing messages on stderr.
714 void llvm::lintModule(const Module &M) {
716 Lint *V = new Lint();
718 PM.run(const_cast<Module&>(M));