LLVM  3.7.0
Lint.cpp
Go to the documentation of this file.
1 //===-- 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. This is the main reason why most
23 // of these checks live here instead of in the Verifier pass.
24 //
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.
29 //
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.
34 //
35 //===----------------------------------------------------------------------===//
36 
37 #include "llvm/Analysis/Lint.h"
38 #include "llvm/ADT/STLExtras.h"
39 #include "llvm/ADT/SmallSet.h"
44 #include "llvm/Analysis/Loads.h"
45 #include "llvm/Analysis/Passes.h"
48 #include "llvm/IR/CallSite.h"
49 #include "llvm/IR/DataLayout.h"
50 #include "llvm/IR/Dominators.h"
51 #include "llvm/IR/Function.h"
52 #include "llvm/IR/InstVisitor.h"
53 #include "llvm/IR/IntrinsicInst.h"
55 #include "llvm/Pass.h"
56 #include "llvm/Support/Debug.h"
58 using namespace llvm;
59 
60 namespace {
61  namespace MemRef {
62  static const unsigned Read = 1;
63  static const unsigned Write = 2;
64  static const unsigned Callee = 4;
65  static const unsigned Branchee = 8;
66  }
67 
68  class Lint : public FunctionPass, public InstVisitor<Lint> {
69  friend class InstVisitor<Lint>;
70 
71  void visitFunction(Function &F);
72 
73  void visitCallSite(CallSite CS);
74  void visitMemoryReference(Instruction &I, Value *Ptr,
75  uint64_t Size, unsigned Align,
76  Type *Ty, unsigned Flags);
77  void visitEHBeginCatch(IntrinsicInst *II);
78  void visitEHEndCatch(IntrinsicInst *II);
79 
80  void visitCallInst(CallInst &I);
81  void visitInvokeInst(InvokeInst &I);
82  void visitReturnInst(ReturnInst &I);
83  void visitLoadInst(LoadInst &I);
84  void visitStoreInst(StoreInst &I);
85  void visitXor(BinaryOperator &I);
86  void visitSub(BinaryOperator &I);
87  void visitLShr(BinaryOperator &I);
88  void visitAShr(BinaryOperator &I);
89  void visitShl(BinaryOperator &I);
90  void visitSDiv(BinaryOperator &I);
91  void visitUDiv(BinaryOperator &I);
92  void visitSRem(BinaryOperator &I);
93  void visitURem(BinaryOperator &I);
94  void visitAllocaInst(AllocaInst &I);
95  void visitVAArgInst(VAArgInst &I);
96  void visitIndirectBrInst(IndirectBrInst &I);
97  void visitExtractElementInst(ExtractElementInst &I);
98  void visitInsertElementInst(InsertElementInst &I);
99  void visitUnreachableInst(UnreachableInst &I);
100 
101  Value *findValue(Value *V, const DataLayout &DL, bool OffsetOk) const;
102  Value *findValueImpl(Value *V, const DataLayout &DL, bool OffsetOk,
103  SmallPtrSetImpl<Value *> &Visited) const;
104 
105  public:
106  Module *Mod;
107  AliasAnalysis *AA;
108  AssumptionCache *AC;
109  DominatorTree *DT;
110  TargetLibraryInfo *TLI;
111 
112  std::string Messages;
113  raw_string_ostream MessagesStr;
114 
115  static char ID; // Pass identification, replacement for typeid
116  Lint() : FunctionPass(ID), MessagesStr(Messages) {
118  }
119 
120  bool runOnFunction(Function &F) override;
121 
122  void getAnalysisUsage(AnalysisUsage &AU) const override {
123  AU.setPreservesAll();
128  }
129  void print(raw_ostream &O, const Module *M) const override {}
130 
131  void WriteValues(ArrayRef<const Value *> Vs) {
132  for (const Value *V : Vs) {
133  if (!V)
134  continue;
135  if (isa<Instruction>(V)) {
136  MessagesStr << *V << '\n';
137  } else {
138  V->printAsOperand(MessagesStr, true, Mod);
139  MessagesStr << '\n';
140  }
141  }
142  }
143 
144  /// \brief A check failed, so printout out the condition and the message.
145  ///
146  /// This provides a nice place to put a breakpoint if you want to see why
147  /// something is not correct.
148  void CheckFailed(const Twine &Message) { MessagesStr << Message << '\n'; }
149 
150  /// \brief A check failed (with values to print).
151  ///
152  /// This calls the Message-only version so that the above is easier to set
153  /// a breakpoint on.
154  template <typename T1, typename... Ts>
155  void CheckFailed(const Twine &Message, const T1 &V1, const Ts &...Vs) {
156  CheckFailed(Message);
157  WriteValues({V1, Vs...});
158  }
159  };
160 }
161 
162 char Lint::ID = 0;
163 INITIALIZE_PASS_BEGIN(Lint, "lint", "Statically lint-checks LLVM IR",
164  false, true)
169 INITIALIZE_PASS_END(Lint, "lint", "Statically lint-checks LLVM IR",
170  false, true)
171 
172 // Assert - We know that cond should be true, if not print an error message.
173 #define Assert(C, ...) \
174  do { if (!(C)) { CheckFailed(__VA_ARGS__); return; } } while (0)
175 
176 // Lint::run - This is the main Analysis entry point for a
177 // function.
178 //
179 bool Lint::runOnFunction(Function &F) {
180  Mod = F.getParent();
181  AA = &getAnalysis<AliasAnalysis>();
182  AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
183  DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
184  TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
185  visit(F);
186  dbgs() << MessagesStr.str();
187  Messages.clear();
188  return false;
189 }
190 
191 void Lint::visitFunction(Function &F) {
192  // This isn't undefined behavior, it's just a little unusual, and it's a
193  // fairly common mistake to neglect to name a function.
194  Assert(F.hasName() || F.hasLocalLinkage(),
195  "Unusual: Unnamed function with non-local linkage", &F);
196 
197  // TODO: Check for irreducible control flow.
198 }
199 
200 void Lint::visitCallSite(CallSite CS) {
201  Instruction &I = *CS.getInstruction();
202  Value *Callee = CS.getCalledValue();
203  const DataLayout &DL = CS->getModule()->getDataLayout();
204 
205  visitMemoryReference(I, Callee, MemoryLocation::UnknownSize, 0, nullptr,
206  MemRef::Callee);
207 
208  if (Function *F = dyn_cast<Function>(findValue(Callee, DL,
209  /*OffsetOk=*/false))) {
210  Assert(CS.getCallingConv() == F->getCallingConv(),
211  "Undefined behavior: Caller and callee calling convention differ",
212  &I);
213 
214  FunctionType *FT = F->getFunctionType();
215  unsigned NumActualArgs = CS.arg_size();
216 
217  Assert(FT->isVarArg() ? FT->getNumParams() <= NumActualArgs
218  : FT->getNumParams() == NumActualArgs,
219  "Undefined behavior: Call argument count mismatches callee "
220  "argument count",
221  &I);
222 
223  Assert(FT->getReturnType() == I.getType(),
224  "Undefined behavior: Call return type mismatches "
225  "callee return type",
226  &I);
227 
228  // Check argument types (in case the callee was casted) and attributes.
229  // TODO: Verify that caller and callee attributes are compatible.
230  Function::arg_iterator PI = F->arg_begin(), PE = F->arg_end();
231  CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
232  for (; AI != AE; ++AI) {
233  Value *Actual = *AI;
234  if (PI != PE) {
235  Argument *Formal = PI++;
236  Assert(Formal->getType() == Actual->getType(),
237  "Undefined behavior: Call argument type mismatches "
238  "callee parameter type",
239  &I);
240 
241  // Check that noalias arguments don't alias other arguments. This is
242  // not fully precise because we don't know the sizes of the dereferenced
243  // memory regions.
244  if (Formal->hasNoAliasAttr() && Actual->getType()->isPointerTy())
245  for (CallSite::arg_iterator BI = CS.arg_begin(); BI != AE; ++BI)
246  if (AI != BI && (*BI)->getType()->isPointerTy()) {
247  AliasResult Result = AA->alias(*AI, *BI);
248  Assert(Result != MustAlias && Result != PartialAlias,
249  "Unusual: noalias argument aliases another argument", &I);
250  }
251 
252  // Check that an sret argument points to valid memory.
253  if (Formal->hasStructRetAttr() && Actual->getType()->isPointerTy()) {
254  Type *Ty =
255  cast<PointerType>(Formal->getType())->getElementType();
256  visitMemoryReference(I, Actual, AA->getTypeStoreSize(Ty),
257  DL.getABITypeAlignment(Ty), Ty,
258  MemRef::Read | MemRef::Write);
259  }
260  }
261  }
262  }
263 
264  if (CS.isCall() && cast<CallInst>(CS.getInstruction())->isTailCall())
265  for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end();
266  AI != AE; ++AI) {
267  Value *Obj = findValue(*AI, DL, /*OffsetOk=*/true);
268  Assert(!isa<AllocaInst>(Obj),
269  "Undefined behavior: Call with \"tail\" keyword references "
270  "alloca",
271  &I);
272  }
273 
274 
275  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I))
276  switch (II->getIntrinsicID()) {
277  default: break;
278 
279  // TODO: Check more intrinsics
280 
281  case Intrinsic::memcpy: {
282  MemCpyInst *MCI = cast<MemCpyInst>(&I);
283  // TODO: If the size is known, use it.
284  visitMemoryReference(I, MCI->getDest(), MemoryLocation::UnknownSize,
285  MCI->getAlignment(), nullptr, MemRef::Write);
286  visitMemoryReference(I, MCI->getSource(), MemoryLocation::UnknownSize,
287  MCI->getAlignment(), nullptr, MemRef::Read);
288 
289  // Check that the memcpy arguments don't overlap. The AliasAnalysis API
290  // isn't expressive enough for what we really want to do. Known partial
291  // overlap is not distinguished from the case where nothing is known.
292  uint64_t Size = 0;
293  if (const ConstantInt *Len =
294  dyn_cast<ConstantInt>(findValue(MCI->getLength(), DL,
295  /*OffsetOk=*/false)))
296  if (Len->getValue().isIntN(32))
297  Size = Len->getValue().getZExtValue();
298  Assert(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) !=
299  MustAlias,
300  "Undefined behavior: memcpy source and destination overlap", &I);
301  break;
302  }
303  case Intrinsic::memmove: {
304  MemMoveInst *MMI = cast<MemMoveInst>(&I);
305  // TODO: If the size is known, use it.
306  visitMemoryReference(I, MMI->getDest(), MemoryLocation::UnknownSize,
307  MMI->getAlignment(), nullptr, MemRef::Write);
308  visitMemoryReference(I, MMI->getSource(), MemoryLocation::UnknownSize,
309  MMI->getAlignment(), nullptr, MemRef::Read);
310  break;
311  }
312  case Intrinsic::memset: {
313  MemSetInst *MSI = cast<MemSetInst>(&I);
314  // TODO: If the size is known, use it.
315  visitMemoryReference(I, MSI->getDest(), MemoryLocation::UnknownSize,
316  MSI->getAlignment(), nullptr, MemRef::Write);
317  break;
318  }
319 
320  case Intrinsic::vastart:
322  "Undefined behavior: va_start called in a non-varargs function",
323  &I);
324 
325  visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0,
326  nullptr, MemRef::Read | MemRef::Write);
327  break;
328  case Intrinsic::vacopy:
329  visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0,
330  nullptr, MemRef::Write);
331  visitMemoryReference(I, CS.getArgument(1), MemoryLocation::UnknownSize, 0,
332  nullptr, MemRef::Read);
333  break;
334  case Intrinsic::vaend:
335  visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0,
336  nullptr, MemRef::Read | MemRef::Write);
337  break;
338 
339  case Intrinsic::stackrestore:
340  // Stackrestore doesn't read or write memory, but it sets the
341  // stack pointer, which the compiler may read from or write to
342  // at any time, so check it for both readability and writeability.
343  visitMemoryReference(I, CS.getArgument(0), MemoryLocation::UnknownSize, 0,
344  nullptr, MemRef::Read | MemRef::Write);
345  break;
346 
347  case Intrinsic::eh_begincatch:
348  visitEHBeginCatch(II);
349  break;
350  case Intrinsic::eh_endcatch:
351  visitEHEndCatch(II);
352  break;
353  }
354 }
355 
356 void Lint::visitCallInst(CallInst &I) {
357  return visitCallSite(&I);
358 }
359 
360 void Lint::visitInvokeInst(InvokeInst &I) {
361  return visitCallSite(&I);
362 }
363 
364 void Lint::visitReturnInst(ReturnInst &I) {
365  Function *F = I.getParent()->getParent();
366  Assert(!F->doesNotReturn(),
367  "Unusual: Return statement in function with noreturn attribute", &I);
368 
369  if (Value *V = I.getReturnValue()) {
370  Value *Obj =
371  findValue(V, F->getParent()->getDataLayout(), /*OffsetOk=*/true);
372  Assert(!isa<AllocaInst>(Obj), "Unusual: Returning alloca value", &I);
373  }
374 }
375 
376 // TODO: Check that the reference is in bounds.
377 // TODO: Check readnone/readonly function attributes.
378 void Lint::visitMemoryReference(Instruction &I,
379  Value *Ptr, uint64_t Size, unsigned Align,
380  Type *Ty, unsigned Flags) {
381  // If no memory is being referenced, it doesn't matter if the pointer
382  // is valid.
383  if (Size == 0)
384  return;
385 
386  Value *UnderlyingObject =
387  findValue(Ptr, I.getModule()->getDataLayout(), /*OffsetOk=*/true);
388  Assert(!isa<ConstantPointerNull>(UnderlyingObject),
389  "Undefined behavior: Null pointer dereference", &I);
390  Assert(!isa<UndefValue>(UnderlyingObject),
391  "Undefined behavior: Undef pointer dereference", &I);
392  Assert(!isa<ConstantInt>(UnderlyingObject) ||
393  !cast<ConstantInt>(UnderlyingObject)->isAllOnesValue(),
394  "Unusual: All-ones pointer dereference", &I);
395  Assert(!isa<ConstantInt>(UnderlyingObject) ||
396  !cast<ConstantInt>(UnderlyingObject)->isOne(),
397  "Unusual: Address one pointer dereference", &I);
398 
399  if (Flags & MemRef::Write) {
400  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject))
401  Assert(!GV->isConstant(), "Undefined behavior: Write to read-only memory",
402  &I);
403  Assert(!isa<Function>(UnderlyingObject) &&
404  !isa<BlockAddress>(UnderlyingObject),
405  "Undefined behavior: Write to text section", &I);
406  }
407  if (Flags & MemRef::Read) {
408  Assert(!isa<Function>(UnderlyingObject), "Unusual: Load from function body",
409  &I);
410  Assert(!isa<BlockAddress>(UnderlyingObject),
411  "Undefined behavior: Load from block address", &I);
412  }
413  if (Flags & MemRef::Callee) {
414  Assert(!isa<BlockAddress>(UnderlyingObject),
415  "Undefined behavior: Call to block address", &I);
416  }
417  if (Flags & MemRef::Branchee) {
418  Assert(!isa<Constant>(UnderlyingObject) ||
419  isa<BlockAddress>(UnderlyingObject),
420  "Undefined behavior: Branch to non-blockaddress", &I);
421  }
422 
423  // Check for buffer overflows and misalignment.
424  // Only handles memory references that read/write something simple like an
425  // alloca instruction or a global variable.
426  auto &DL = I.getModule()->getDataLayout();
427  int64_t Offset = 0;
428  if (Value *Base = GetPointerBaseWithConstantOffset(Ptr, Offset, DL)) {
429  // OK, so the access is to a constant offset from Ptr. Check that Ptr is
430  // something we can handle and if so extract the size of this base object
431  // along with its alignment.
432  uint64_t BaseSize = MemoryLocation::UnknownSize;
433  unsigned BaseAlign = 0;
434 
435  if (AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
436  Type *ATy = AI->getAllocatedType();
437  if (!AI->isArrayAllocation() && ATy->isSized())
438  BaseSize = DL.getTypeAllocSize(ATy);
439  BaseAlign = AI->getAlignment();
440  if (BaseAlign == 0 && ATy->isSized())
441  BaseAlign = DL.getABITypeAlignment(ATy);
442  } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
443  // If the global may be defined differently in another compilation unit
444  // then don't warn about funky memory accesses.
445  if (GV->hasDefinitiveInitializer()) {
446  Type *GTy = GV->getType()->getElementType();
447  if (GTy->isSized())
448  BaseSize = DL.getTypeAllocSize(GTy);
449  BaseAlign = GV->getAlignment();
450  if (BaseAlign == 0 && GTy->isSized())
451  BaseAlign = DL.getABITypeAlignment(GTy);
452  }
453  }
454 
455  // Accesses from before the start or after the end of the object are not
456  // defined.
458  BaseSize == MemoryLocation::UnknownSize ||
459  (Offset >= 0 && Offset + Size <= BaseSize),
460  "Undefined behavior: Buffer overflow", &I);
461 
462  // Accesses that say that the memory is more aligned than it is are not
463  // defined.
464  if (Align == 0 && Ty && Ty->isSized())
465  Align = DL.getABITypeAlignment(Ty);
466  Assert(!BaseAlign || Align <= MinAlign(BaseAlign, Offset),
467  "Undefined behavior: Memory reference address is misaligned", &I);
468  }
469 }
470 
471 void Lint::visitLoadInst(LoadInst &I) {
472  visitMemoryReference(I, I.getPointerOperand(),
473  AA->getTypeStoreSize(I.getType()), I.getAlignment(),
474  I.getType(), MemRef::Read);
475 }
476 
477 void Lint::visitStoreInst(StoreInst &I) {
478  visitMemoryReference(I, I.getPointerOperand(),
479  AA->getTypeStoreSize(I.getOperand(0)->getType()),
480  I.getAlignment(),
481  I.getOperand(0)->getType(), MemRef::Write);
482 }
483 
484 void Lint::visitXor(BinaryOperator &I) {
485  Assert(!isa<UndefValue>(I.getOperand(0)) || !isa<UndefValue>(I.getOperand(1)),
486  "Undefined result: xor(undef, undef)", &I);
487 }
488 
489 void Lint::visitSub(BinaryOperator &I) {
490  Assert(!isa<UndefValue>(I.getOperand(0)) || !isa<UndefValue>(I.getOperand(1)),
491  "Undefined result: sub(undef, undef)", &I);
492 }
493 
494 void Lint::visitLShr(BinaryOperator &I) {
495  if (ConstantInt *CI = dyn_cast<ConstantInt>(
496  findValue(I.getOperand(1), I.getModule()->getDataLayout(),
497  /*OffsetOk=*/false)))
498  Assert(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
499  "Undefined result: Shift count out of range", &I);
500 }
501 
502 void Lint::visitAShr(BinaryOperator &I) {
503  if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(
504  I.getOperand(1), I.getModule()->getDataLayout(), /*OffsetOk=*/false)))
505  Assert(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
506  "Undefined result: Shift count out of range", &I);
507 }
508 
509 void Lint::visitShl(BinaryOperator &I) {
510  if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(
511  I.getOperand(1), I.getModule()->getDataLayout(), /*OffsetOk=*/false)))
512  Assert(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
513  "Undefined result: Shift count out of range", &I);
514 }
515 
516 static bool
518  SmallSet<BasicBlock *, 4> &VisitedBlocks) {
519  VisitedBlocks.insert(BB);
520  if (BB->isLandingPad())
521  return true;
522  // If we find a block with no predecessors, the search failed.
523  if (pred_empty(BB))
524  return false;
525  for (BasicBlock *Pred : predecessors(BB)) {
526  if (VisitedBlocks.count(Pred))
527  continue;
528  if (!allPredsCameFromLandingPad(Pred, VisitedBlocks))
529  return false;
530  }
531  return true;
532 }
533 
534 static bool
536  IntrinsicInst **SecondBeginCatch,
537  SmallSet<BasicBlock *, 4> &VisitedBlocks) {
538  VisitedBlocks.insert(BB);
539  for (BasicBlock::iterator I = InstBegin, E = BB->end(); I != E; ++I) {
541  if (IC && IC->getIntrinsicID() == Intrinsic::eh_endcatch)
542  return true;
543  // If we find another begincatch while looking for an endcatch,
544  // that's also an error.
545  if (IC && IC->getIntrinsicID() == Intrinsic::eh_begincatch) {
546  *SecondBeginCatch = IC;
547  return false;
548  }
549  }
550 
551  // If we reach a block with no successors while searching, the
552  // search has failed.
553  if (succ_empty(BB))
554  return false;
555  // Otherwise, search all of the successors.
556  for (BasicBlock *Succ : successors(BB)) {
557  if (VisitedBlocks.count(Succ))
558  continue;
559  if (!allSuccessorsReachEndCatch(Succ, Succ->begin(), SecondBeginCatch,
560  VisitedBlocks))
561  return false;
562  }
563  return true;
564 }
565 
566 void Lint::visitEHBeginCatch(IntrinsicInst *II) {
567  // The checks in this function make a potentially dubious assumption about
568  // the CFG, namely that any block involved in a catch is only used for the
569  // catch. This will very likely be true of IR generated by a front end,
570  // but it may cease to be true, for example, if the IR is run through a
571  // pass which combines similar blocks.
572  //
573  // In general, if we encounter a block the isn't dominated by the catch
574  // block while we are searching the catch block's successors for a call
575  // to end catch intrinsic, then it is possible that it will be legal for
576  // a path through this block to never reach a call to llvm.eh.endcatch.
577  // An analogous statement could be made about our search for a landing
578  // pad among the catch block's predecessors.
579  //
580  // What is actually required is that no path is possible at runtime that
581  // reaches a call to llvm.eh.begincatch without having previously visited
582  // a landingpad instruction and that no path is possible at runtime that
583  // calls llvm.eh.begincatch and does not subsequently call llvm.eh.endcatch
584  // (mentally adjusting for the fact that in reality these calls will be
585  // removed before code generation).
586  //
587  // Because this is a lint check, we take a pessimistic approach and warn if
588  // the control flow is potentially incorrect.
589 
590  SmallSet<BasicBlock *, 4> VisitedBlocks;
591  BasicBlock *CatchBB = II->getParent();
592 
593  // The begin catch must occur in a landing pad block or all paths
594  // to it must have come from a landing pad.
595  Assert(allPredsCameFromLandingPad(CatchBB, VisitedBlocks),
596  "llvm.eh.begincatch may be reachable without passing a landingpad",
597  II);
598 
599  // Reset the visited block list.
600  VisitedBlocks.clear();
601 
602  IntrinsicInst *SecondBeginCatch = nullptr;
603 
604  // This has to be called before it is asserted. Otherwise, the first assert
605  // below can never be hit.
606  bool EndCatchFound = allSuccessorsReachEndCatch(
607  CatchBB, std::next(static_cast<BasicBlock::iterator>(II)),
608  &SecondBeginCatch, VisitedBlocks);
609  Assert(
610  SecondBeginCatch == nullptr,
611  "llvm.eh.begincatch may be called a second time before llvm.eh.endcatch",
612  II, SecondBeginCatch);
613  Assert(EndCatchFound,
614  "Some paths from llvm.eh.begincatch may not reach llvm.eh.endcatch",
615  II);
616 }
617 
620  IntrinsicInst **SecondEndCatch, SmallSet<BasicBlock *, 4> &VisitedBlocks) {
621  VisitedBlocks.insert(BB);
622  // Look for a begincatch in this block.
623  for (BasicBlock::reverse_iterator RI = InstRbegin, RE = BB->rend(); RI != RE;
624  ++RI) {
625  IntrinsicInst *IC = dyn_cast<IntrinsicInst>(&*RI);
626  if (IC && IC->getIntrinsicID() == Intrinsic::eh_begincatch)
627  return true;
628  // If we find another end catch before we find a begin catch, that's
629  // an error.
630  if (IC && IC->getIntrinsicID() == Intrinsic::eh_endcatch) {
631  *SecondEndCatch = IC;
632  return false;
633  }
634  // If we encounter a landingpad instruction, the search failed.
635  if (isa<LandingPadInst>(*RI))
636  return false;
637  }
638  // If while searching we find a block with no predeccesors,
639  // the search failed.
640  if (pred_empty(BB))
641  return false;
642  // Search any predecessors we haven't seen before.
643  for (BasicBlock *Pred : predecessors(BB)) {
644  if (VisitedBlocks.count(Pred))
645  continue;
646  if (!allPredCameFromBeginCatch(Pred, Pred->rbegin(), SecondEndCatch,
647  VisitedBlocks))
648  return false;
649  }
650  return true;
651 }
652 
653 void Lint::visitEHEndCatch(IntrinsicInst *II) {
654  // The check in this function makes a potentially dubious assumption about
655  // the CFG, namely that any block involved in a catch is only used for the
656  // catch. This will very likely be true of IR generated by a front end,
657  // but it may cease to be true, for example, if the IR is run through a
658  // pass which combines similar blocks.
659  //
660  // In general, if we encounter a block the isn't post-dominated by the
661  // end catch block while we are searching the end catch block's predecessors
662  // for a call to the begin catch intrinsic, then it is possible that it will
663  // be legal for a path to reach the end catch block without ever having
664  // called llvm.eh.begincatch.
665  //
666  // What is actually required is that no path is possible at runtime that
667  // reaches a call to llvm.eh.endcatch without having previously visited
668  // a call to llvm.eh.begincatch (mentally adjusting for the fact that in
669  // reality these calls will be removed before code generation).
670  //
671  // Because this is a lint check, we take a pessimistic approach and warn if
672  // the control flow is potentially incorrect.
673 
674  BasicBlock *EndCatchBB = II->getParent();
675 
676  // Alls paths to the end catch call must pass through a begin catch call.
677 
678  // If llvm.eh.begincatch wasn't called in the current block, we'll use this
679  // lambda to recursively look for it in predecessors.
680  SmallSet<BasicBlock *, 4> VisitedBlocks;
681  IntrinsicInst *SecondEndCatch = nullptr;
682 
683  // This has to be called before it is asserted. Otherwise, the first assert
684  // below can never be hit.
685  bool BeginCatchFound =
687  &SecondEndCatch, VisitedBlocks);
688  Assert(
689  SecondEndCatch == nullptr,
690  "llvm.eh.endcatch may be called a second time after llvm.eh.begincatch",
691  II, SecondEndCatch);
692  Assert(BeginCatchFound,
693  "llvm.eh.endcatch may be reachable without passing llvm.eh.begincatch",
694  II);
695 }
696 
697 static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT,
698  AssumptionCache *AC) {
699  // Assume undef could be zero.
700  if (isa<UndefValue>(V))
701  return true;
702 
703  VectorType *VecTy = dyn_cast<VectorType>(V->getType());
704  if (!VecTy) {
705  unsigned BitWidth = V->getType()->getIntegerBitWidth();
706  APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
707  computeKnownBits(V, KnownZero, KnownOne, DL, 0, AC,
708  dyn_cast<Instruction>(V), DT);
709  return KnownZero.isAllOnesValue();
710  }
711 
712  // Per-component check doesn't work with zeroinitializer
713  Constant *C = dyn_cast<Constant>(V);
714  if (!C)
715  return false;
716 
717  if (C->isZeroValue())
718  return true;
719 
720  // For a vector, KnownZero will only be true if all values are zero, so check
721  // this per component
722  unsigned BitWidth = VecTy->getElementType()->getIntegerBitWidth();
723  for (unsigned I = 0, N = VecTy->getNumElements(); I != N; ++I) {
724  Constant *Elem = C->getAggregateElement(I);
725  if (isa<UndefValue>(Elem))
726  return true;
727 
728  APInt KnownZero(BitWidth, 0), KnownOne(BitWidth, 0);
729  computeKnownBits(Elem, KnownZero, KnownOne, DL);
730  if (KnownZero.isAllOnesValue())
731  return true;
732  }
733 
734  return false;
735 }
736 
737 void Lint::visitSDiv(BinaryOperator &I) {
738  Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
739  "Undefined behavior: Division by zero", &I);
740 }
741 
742 void Lint::visitUDiv(BinaryOperator &I) {
743  Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
744  "Undefined behavior: Division by zero", &I);
745 }
746 
747 void Lint::visitSRem(BinaryOperator &I) {
748  Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
749  "Undefined behavior: Division by zero", &I);
750 }
751 
752 void Lint::visitURem(BinaryOperator &I) {
753  Assert(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
754  "Undefined behavior: Division by zero", &I);
755 }
756 
757 void Lint::visitAllocaInst(AllocaInst &I) {
758  if (isa<ConstantInt>(I.getArraySize()))
759  // This isn't undefined behavior, it's just an obvious pessimization.
761  "Pessimization: Static alloca outside of entry block", &I);
762 
763  // TODO: Check for an unusual size (MSB set?)
764 }
765 
766 void Lint::visitVAArgInst(VAArgInst &I) {
767  visitMemoryReference(I, I.getOperand(0), MemoryLocation::UnknownSize, 0,
768  nullptr, MemRef::Read | MemRef::Write);
769 }
770 
771 void Lint::visitIndirectBrInst(IndirectBrInst &I) {
772  visitMemoryReference(I, I.getAddress(), MemoryLocation::UnknownSize, 0,
773  nullptr, MemRef::Branchee);
774 
775  Assert(I.getNumDestinations() != 0,
776  "Undefined behavior: indirectbr with no destinations", &I);
777 }
778 
779 void Lint::visitExtractElementInst(ExtractElementInst &I) {
780  if (ConstantInt *CI = dyn_cast<ConstantInt>(
781  findValue(I.getIndexOperand(), I.getModule()->getDataLayout(),
782  /*OffsetOk=*/false)))
783  Assert(CI->getValue().ult(I.getVectorOperandType()->getNumElements()),
784  "Undefined result: extractelement index out of range", &I);
785 }
786 
787 void Lint::visitInsertElementInst(InsertElementInst &I) {
788  if (ConstantInt *CI = dyn_cast<ConstantInt>(
789  findValue(I.getOperand(2), I.getModule()->getDataLayout(),
790  /*OffsetOk=*/false)))
791  Assert(CI->getValue().ult(I.getType()->getNumElements()),
792  "Undefined result: insertelement index out of range", &I);
793 }
794 
795 void Lint::visitUnreachableInst(UnreachableInst &I) {
796  // This isn't undefined behavior, it's merely suspicious.
797  Assert(&I == I.getParent()->begin() ||
798  std::prev(BasicBlock::iterator(&I))->mayHaveSideEffects(),
799  "Unusual: unreachable immediately preceded by instruction without "
800  "side effects",
801  &I);
802 }
803 
804 /// findValue - Look through bitcasts and simple memory reference patterns
805 /// to identify an equivalent, but more informative, value. If OffsetOk
806 /// is true, look through getelementptrs with non-zero offsets too.
807 ///
808 /// Most analysis passes don't require this logic, because instcombine
809 /// will simplify most of these kinds of things away. But it's a goal of
810 /// this Lint pass to be useful even on non-optimized IR.
811 Value *Lint::findValue(Value *V, const DataLayout &DL, bool OffsetOk) const {
812  SmallPtrSet<Value *, 4> Visited;
813  return findValueImpl(V, DL, OffsetOk, Visited);
814 }
815 
816 /// findValueImpl - Implementation helper for findValue.
817 Value *Lint::findValueImpl(Value *V, const DataLayout &DL, bool OffsetOk,
818  SmallPtrSetImpl<Value *> &Visited) const {
819  // Detect self-referential values.
820  if (!Visited.insert(V).second)
821  return UndefValue::get(V->getType());
822 
823  // TODO: Look through sext or zext cast, when the result is known to
824  // be interpreted as signed or unsigned, respectively.
825  // TODO: Look through eliminable cast pairs.
826  // TODO: Look through calls with unique return values.
827  // TODO: Look through vector insert/extract/shuffle.
828  V = OffsetOk ? GetUnderlyingObject(V, DL) : V->stripPointerCasts();
829  if (LoadInst *L = dyn_cast<LoadInst>(V)) {
830  BasicBlock::iterator BBI = L;
831  BasicBlock *BB = L->getParent();
832  SmallPtrSet<BasicBlock *, 4> VisitedBlocks;
833  for (;;) {
834  if (!VisitedBlocks.insert(BB).second)
835  break;
836  if (Value *U = FindAvailableLoadedValue(L->getPointerOperand(),
837  BB, BBI, 6, AA))
838  return findValueImpl(U, DL, OffsetOk, Visited);
839  if (BBI != BB->begin()) break;
840  BB = BB->getUniquePredecessor();
841  if (!BB) break;
842  BBI = BB->end();
843  }
844  } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
845  if (Value *W = PN->hasConstantValue())
846  if (W != V)
847  return findValueImpl(W, DL, OffsetOk, Visited);
848  } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
849  if (CI->isNoopCast(DL))
850  return findValueImpl(CI->getOperand(0), DL, OffsetOk, Visited);
851  } else if (ExtractValueInst *Ex = dyn_cast<ExtractValueInst>(V)) {
852  if (Value *W = FindInsertedValue(Ex->getAggregateOperand(),
853  Ex->getIndices()))
854  if (W != V)
855  return findValueImpl(W, DL, OffsetOk, Visited);
856  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
857  // Same as above, but for ConstantExpr instead of Instruction.
858  if (Instruction::isCast(CE->getOpcode())) {
859  if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()),
860  CE->getOperand(0)->getType(), CE->getType(),
861  DL.getIntPtrType(V->getType())))
862  return findValueImpl(CE->getOperand(0), DL, OffsetOk, Visited);
863  } else if (CE->getOpcode() == Instruction::ExtractValue) {
864  ArrayRef<unsigned> Indices = CE->getIndices();
865  if (Value *W = FindInsertedValue(CE->getOperand(0), Indices))
866  if (W != V)
867  return findValueImpl(W, DL, OffsetOk, Visited);
868  }
869  }
870 
871  // As a last resort, try SimplifyInstruction or constant folding.
872  if (Instruction *Inst = dyn_cast<Instruction>(V)) {
873  if (Value *W = SimplifyInstruction(Inst, DL, TLI, DT, AC))
874  return findValueImpl(W, DL, OffsetOk, Visited);
875  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
876  if (Value *W = ConstantFoldConstantExpression(CE, DL, TLI))
877  if (W != V)
878  return findValueImpl(W, DL, OffsetOk, Visited);
879  }
880 
881  return V;
882 }
883 
884 //===----------------------------------------------------------------------===//
885 // Implement the public interfaces to this file...
886 //===----------------------------------------------------------------------===//
887 
889  return new Lint();
890 }
891 
892 /// lintFunction - Check a function for errors, printing messages on stderr.
893 ///
894 void llvm::lintFunction(const Function &f) {
895  Function &F = const_cast<Function&>(f);
896  assert(!F.isDeclaration() && "Cannot lint external functions");
897 
899  Lint *V = new Lint();
900  FPM.add(V);
901  FPM.run(F);
902 }
903 
904 /// lintModule - Check a module for errors, printing messages on stderr.
905 ///
906 void llvm::lintModule(const Module &M) {
908  Lint *V = new Lint();
909  PM.add(V);
910  PM.run(const_cast<Module&>(M));
911 }
unsigned getAlignment() const
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type (if unknown returns 0).
bool hasNoAliasAttr() const
Return true if this argument has the noalias attribute on it in its containing function.
Definition: Function.cpp:139
The two locations precisely alias each other.
Definition: AliasAnalysis.h:84
ReturnInst - Return a value (possibly void), from a function.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:104
void lintModule(const Module &M)
Check a module.
Definition: Lint.cpp:906
BasicBlock * getUniquePredecessor()
Return the predecessor of this block if it has a unique predecessor block.
Definition: BasicBlock.cpp:224
ExtractValueInst - This instruction extracts a struct member or array element value from an aggregate...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVM Argument representation.
Definition: Argument.h:35
Base class for instruction visitors.
Definition: InstVisitor.h:81
bool hasName() const
Definition: Value.h:228
ValTy * getArgument(unsigned ArgNo) const
Definition: CallSite.h:119
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
InstrTy * getInstruction() const
Definition: CallSite.h:82
Intrinsic::ID getIntrinsicID() const
getIntrinsicID - Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:44
The two locations alias, but only due to a partial overlap.
Definition: AliasAnalysis.h:82
CallInst - This class represents a function call, abstracting a target machine's calling convention...
void lintFunction(const Function &F)
lintFunction - Check a function for errors, printing messages on stderr.
Definition: Lint.cpp:894
Statically lint checks LLVM false
Definition: Lint.cpp:169
An immutable pass that tracks lazily created AssumptionCache objects.
void computeKnownBits(Value *V, APInt &KnownZero, APInt &KnownOne, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
A cache of .assume calls within a function.
MemSetInst - This class wraps the llvm.memset intrinsic.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:111
arg_iterator arg_end()
Definition: Function.h:480
F(f)
reverse_iterator rend()
Definition: BasicBlock.h:238
LoadInst - an instruction for reading from memory.
Definition: Instructions.h:177
INITIALIZE_PASS_BEGIN(Lint,"lint","Statically lint-checks LLVM IR", false, true) INITIALIZE_PASS_END(Lint
User::op_iterator arg_iterator
arg_iterator - The type of iterator to use when looping over actual arguments at this call site...
Definition: CallSite.h:147
#define Assert(C,...)
Definition: Lint.cpp:173
Constant * ConstantFoldConstantExpression(const ConstantExpr *CE, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstantExpression - Attempt to fold the constant expression using the specified DataLayo...
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:172
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:242
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:231
bool isCast() const
Definition: Instruction.h:118
IterTy arg_end() const
Definition: CallSite.h:157
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:70
void add(Pass *P) override
Add a pass to the queue of passes to run.
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:389
MemMoveInst - This class wraps the llvm.memmove intrinsic.
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:75
VectorType * getVectorOperandType() const
static bool isNoopCast(Instruction::CastOps Opcode, Type *SrcTy, Type *DstTy, Type *IntPtrTy)
A no-op cast is one that can be effected without changing any bits.
bool isSized(SmallPtrSetImpl< const Type * > *Visited=nullptr) const
isSized - Return true if it makes sense to take the size of this type.
Definition: Type.h:268
Windows NT (Windows on ARM)
static bool allSuccessorsReachEndCatch(BasicBlock *BB, BasicBlock::iterator InstBegin, IntrinsicInst **SecondBeginCatch, SmallSet< BasicBlock *, 4 > &VisitedBlocks)
Definition: Lint.cpp:535
ConstantExpr - a constant value that is initialized with an expression using other constant values...
Definition: Constants.h:852
FunctionType - Class to represent function types.
Definition: DerivedTypes.h:96
VectorType * getType() const
getType - Overload to return most specific vector type.
void clear()
Definition: SmallSet.h:107
ValTy * getCalledValue() const
getCalledValue - Return the pointer to function that is being called.
Definition: CallSite.h:91
bool hasStructRetAttr() const
Return true if this argument has the sret attribute on it in its containing function.
Definition: Function.cpp:155
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL)
GetPointerBaseWithConstantOffset - Analyze the specified pointer to see if it can be expressed as a b...
lint
Definition: Lint.cpp:169
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
StoreInst - an instruction for storing to memory.
Definition: Instructions.h:316
unsigned getNumElements() const
Return the number of elements in the Vector type.
Definition: DerivedTypes.h:432
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:67
Type * getElementType() const
Definition: DerivedTypes.h:323
PassManager manages ModulePassManagers.
InstListType::reverse_iterator reverse_iterator
Definition: BasicBlock.h:95
#define true
Definition: ConvertUTF.c:66
InsertElementInst - This instruction inserts a single (scalar) element into a VectorType value...
unsigned getAlignment() const
getAlignment - Return the alignment of the access that is being performed
Definition: Instructions.h:365
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
UnreachableInst - This function has undefined behavior.
This is an important base class in LLVM.
Definition: Constant.h:41
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:32
IndirectBrInst - Indirect Branch Instruction.
bool succ_empty(const BasicBlock *BB)
Definition: IR/CFG.h:268
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:264
static bool allPredCameFromBeginCatch(BasicBlock *BB, BasicBlock::reverse_iterator InstRbegin, IntrinsicInst **SecondEndCatch, SmallSet< BasicBlock *, 4 > &VisitedBlocks)
Definition: Lint.cpp:618
bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition: Constants.cpp:66
bool doesNotReturn() const
Determine if the function cannot return.
Definition: Function.h:307
AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:72
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
Value * getOperand(unsigned i) const
Definition: User.h:118
Value * getPointerOperand()
Definition: Instructions.h:284
arg_iterator arg_begin()
Definition: Function.h:472
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:69
bool pred_empty(const BasicBlock *BB)
Definition: IR/CFG.h:99
Constant * getAggregateElement(unsigned Elt) const
getAggregateElement - For aggregates (struct/array/vector) return the constant that corresponds to th...
Definition: Constants.cpp:250
#define INITIALIZE_AG_DEPENDENCY(depName)
Definition: PassSupport.h:72
FunctionPassManager manages FunctionPasses and BasicBlockPassManagers.
bool isPointerTy() const
isPointerTy - True if this is an instance of PointerType.
Definition: Type.h:217
static UndefValue * get(Type *T)
get() - Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1473
VAArgInst - This class represents the va_arg llvm instruction, which returns an argument of the speci...
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
GetUnderlyingObject - This method strips off any GEP address adjustments and pointer casts from the s...
Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, Instruction *InsertBefore=nullptr)
FindInsertedValue - Given an aggregrate and an sequence of indices, see if the scalar value indexed i...
FunctionPass * createLintPass()
Create a lint pass.
Definition: Lint.cpp:888
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:53
static bool allPredsCameFromLandingPad(BasicBlock *BB, SmallSet< BasicBlock *, 4 > &VisitedBlocks)
Definition: Lint.cpp:517
IntegerType * getIntPtrType(LLVMContext &C, unsigned AddressSpace=0) const
Returns an integer type with size at least as big as that of a pointer in the given address space...
Definition: DataLayout.cpp:694
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Definition: DataLayout.cpp:674
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:299
unsigned getIntegerBitWidth() const
Definition: Type.cpp:176
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:388
Value * getDest() const
getDest - This is just like getRawDest, but it strips off any cast instructions that feed it...
iterator end()
Definition: BasicBlock.h:233
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:57
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
Provides information about what library functions are available for the current target.
Value * getLength() const
unsigned arg_size() const
Definition: CallSite.h:162
void initializeLintPass(PassRegistry &)
MemCpyInst - This class wraps the llvm.memcpy intrinsic.
pred_range predecessors(BasicBlock *BB)
Definition: IR/CFG.h:102
const BasicBlock & getEntryBlock() const
Definition: Function.h:442
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(NoStrictAlign), cl::values(clEnumValN(StrictAlign,"aarch64-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"aarch64-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
VectorType - Class to represent vector types.
Definition: DerivedTypes.h:362
Class for arbitrary precision integers.
Definition: APInt.h:73
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
Value * getSource() const
getSource - This is just like getRawSource, but it strips off any cast instructions that feed it...
uint64_t MinAlign(uint64_t A, uint64_t B)
MinAlign - A and B are either alignments or offsets.
Definition: MathExtras.h:552
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:697
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:372
implicit null checks
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:128
unsigned getAlignment() const
getAlignment - Return the alignment of the access that is being performed
Definition: Instructions.h:243
CallingConv::ID getCallingConv() const
getCallingConv/setCallingConv - get or set the calling convention of the call.
Definition: CallSite.h:212
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
bool isLandingPad() const
Return true if this basic block is a landing pad.
Definition: BasicBlock.cpp:413
FunctionType * getFunctionType() const
Definition: Function.cpp:227
ExtractElementInst - This instruction extracts a single (scalar) element from a VectorType value...
bool hasLocalLinkage() const
Definition: GlobalValue.h:280
Value * FindAvailableLoadedValue(Value *Ptr, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=6, AliasAnalysis *AA=nullptr, AAMDNodes *AATags=nullptr)
FindAvailableLoadedValue - Scan the ScanBB block backwards (starting at the instruction before ScanFr...
Definition: Loads.cpp:183
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:465
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
LLVM Value Representation.
Definition: Value.h:69
succ_range successors(BasicBlock *BB)
Definition: IR/CFG.h:271
const Value * getArraySize() const
getArraySize - Get the number of elements allocated.
Definition: Instructions.h:110
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:38
InvokeInst - Invoke instruction.
IterTy arg_begin() const
arg_begin/arg_end - Return iterators corresponding to the actual argument list for a call site...
Definition: CallSite.h:151
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Value * SimplifyInstruction(Instruction *I, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
SimplifyInstruction - See if we can compute a simplified version of this instruction.
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:203
bool isCall() const
isCall - true if a CallInst is enclosed.
Definition: CallSite.h:76
bool isVarArg() const
isVarArg - Return true if this function takes a variable number of arguments.
Definition: Function.cpp:229
Value * getPointerOperand()
Definition: Instructions.h:409
unsigned getNumDestinations() const
getNumDestinations - return the number of possible destinations in this indirectbr instruction...
Statically lint checks LLVM IR
Definition: Lint.cpp:169
const BasicBlock * getParent() const
Definition: Instruction.h:72
#define T1
IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:37
AllocaInst - an instruction to allocate memory on the stack.
Definition: Instructions.h:76