LLVM  15.0.0git
Lint.cpp
Go to the documentation of this file.
1 //===-- Lint.cpp - Check for common errors in LLVM IR ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass statically checks for common and easily-identified constructs
10 // which produce undefined or likely unintended behavior in LLVM IR.
11 //
12 // It is not a guarantee of correctness, in two ways. First, it isn't
13 // comprehensive. There are checks which could be done statically which are
14 // not yet implemented. Some of these are indicated by TODO comments, but
15 // those aren't comprehensive either. Second, many conditions cannot be
16 // checked statically. This pass does no dynamic instrumentation, so it
17 // can't check for all possible problems.
18 //
19 // Another limitation is that it assumes all code will be executed. A store
20 // through a null pointer in a basic block which is never reached is harmless,
21 // but this pass will warn about it anyway. This is the main reason why most
22 // of these checks live here instead of in the Verifier pass.
23 //
24 // 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/Lint.h"
37 #include "llvm/ADT/APInt.h"
38 #include "llvm/ADT/ArrayRef.h"
39 #include "llvm/ADT/SmallPtrSet.h"
40 #include "llvm/ADT/Twine.h"
45 #include "llvm/Analysis/Loads.h"
49 #include "llvm/IR/Argument.h"
50 #include "llvm/IR/BasicBlock.h"
51 #include "llvm/IR/Constant.h"
52 #include "llvm/IR/Constants.h"
53 #include "llvm/IR/DataLayout.h"
54 #include "llvm/IR/DerivedTypes.h"
55 #include "llvm/IR/Dominators.h"
56 #include "llvm/IR/Function.h"
57 #include "llvm/IR/GlobalVariable.h"
58 #include "llvm/IR/InstVisitor.h"
59 #include "llvm/IR/InstrTypes.h"
60 #include "llvm/IR/Instruction.h"
61 #include "llvm/IR/Instructions.h"
62 #include "llvm/IR/IntrinsicInst.h"
64 #include "llvm/IR/Module.h"
65 #include "llvm/IR/PassManager.h"
66 #include "llvm/IR/Type.h"
67 #include "llvm/IR/Value.h"
68 #include "llvm/InitializePasses.h"
69 #include "llvm/Pass.h"
70 #include "llvm/Support/Casting.h"
71 #include "llvm/Support/KnownBits.h"
73 #include <cassert>
74 #include <cstdint>
75 #include <iterator>
76 #include <string>
77 
78 using namespace llvm;
79 
80 namespace {
81 namespace MemRef {
82 static const unsigned Read = 1;
83 static const unsigned Write = 2;
84 static const unsigned Callee = 4;
85 static const unsigned Branchee = 8;
86 } // end namespace MemRef
87 
88 class Lint : public InstVisitor<Lint> {
89  friend class InstVisitor<Lint>;
90 
91  void visitFunction(Function &F);
92 
93  void visitCallBase(CallBase &CB);
94  void visitMemoryReference(Instruction &I, const MemoryLocation &Loc,
95  MaybeAlign Alignment, Type *Ty, unsigned Flags);
96  void visitEHBeginCatch(IntrinsicInst *II);
97  void visitEHEndCatch(IntrinsicInst *II);
98 
99  void visitReturnInst(ReturnInst &I);
100  void visitLoadInst(LoadInst &I);
101  void visitStoreInst(StoreInst &I);
102  void visitXor(BinaryOperator &I);
103  void visitSub(BinaryOperator &I);
104  void visitLShr(BinaryOperator &I);
105  void visitAShr(BinaryOperator &I);
106  void visitShl(BinaryOperator &I);
107  void visitSDiv(BinaryOperator &I);
108  void visitUDiv(BinaryOperator &I);
109  void visitSRem(BinaryOperator &I);
110  void visitURem(BinaryOperator &I);
111  void visitAllocaInst(AllocaInst &I);
112  void visitVAArgInst(VAArgInst &I);
113  void visitIndirectBrInst(IndirectBrInst &I);
114  void visitExtractElementInst(ExtractElementInst &I);
115  void visitInsertElementInst(InsertElementInst &I);
116  void visitUnreachableInst(UnreachableInst &I);
117 
118  Value *findValue(Value *V, bool OffsetOk) const;
119  Value *findValueImpl(Value *V, bool OffsetOk,
120  SmallPtrSetImpl<Value *> &Visited) const;
121 
122 public:
123  Module *Mod;
124  const DataLayout *DL;
125  AliasAnalysis *AA;
126  AssumptionCache *AC;
127  DominatorTree *DT;
128  TargetLibraryInfo *TLI;
129 
130  std::string Messages;
131  raw_string_ostream MessagesStr;
132 
133  Lint(Module *Mod, const DataLayout *DL, AliasAnalysis *AA,
135  : Mod(Mod), DL(DL), AA(AA), AC(AC), DT(DT), TLI(TLI),
136  MessagesStr(Messages) {}
137 
138  void WriteValues(ArrayRef<const Value *> Vs) {
139  for (const Value *V : Vs) {
140  if (!V)
141  continue;
142  if (isa<Instruction>(V)) {
143  MessagesStr << *V << '\n';
144  } else {
145  V->printAsOperand(MessagesStr, true, Mod);
146  MessagesStr << '\n';
147  }
148  }
149  }
150 
151  /// A check failed, so printout out the condition and the message.
152  ///
153  /// This provides a nice place to put a breakpoint if you want to see why
154  /// something is not correct.
155  void CheckFailed(const Twine &Message) { MessagesStr << Message << '\n'; }
156 
157  /// A check failed (with values to print).
158  ///
159  /// This calls the Message-only version so that the above is easier to set
160  /// a breakpoint on.
161  template <typename T1, typename... Ts>
162  void CheckFailed(const Twine &Message, const T1 &V1, const Ts &... Vs) {
163  CheckFailed(Message);
164  WriteValues({V1, Vs...});
165  }
166 };
167 } // end anonymous namespace
168 
169 // Check - We know that cond should be true, if not print an error message.
170 #define Check(C, ...) \
171  do { \
172  if (!(C)) { \
173  CheckFailed(__VA_ARGS__); \
174  return; \
175  } \
176  } while (false)
177 
178 void Lint::visitFunction(Function &F) {
179  // This isn't undefined behavior, it's just a little unusual, and it's a
180  // fairly common mistake to neglect to name a function.
181  Check(F.hasName() || F.hasLocalLinkage(),
182  "Unusual: Unnamed function with non-local linkage", &F);
183 
184  // TODO: Check for irreducible control flow.
185 }
186 
187 void Lint::visitCallBase(CallBase &I) {
188  Value *Callee = I.getCalledOperand();
189 
190  visitMemoryReference(I, MemoryLocation::getAfter(Callee), None, nullptr,
191  MemRef::Callee);
192 
193  if (Function *F = dyn_cast<Function>(findValue(Callee,
194  /*OffsetOk=*/false))) {
195  Check(I.getCallingConv() == F->getCallingConv(),
196  "Undefined behavior: Caller and callee calling convention differ",
197  &I);
198 
199  FunctionType *FT = F->getFunctionType();
200  unsigned NumActualArgs = I.arg_size();
201 
202  Check(FT->isVarArg() ? FT->getNumParams() <= NumActualArgs
203  : FT->getNumParams() == NumActualArgs,
204  "Undefined behavior: Call argument count mismatches callee "
205  "argument count",
206  &I);
207 
208  Check(FT->getReturnType() == I.getType(),
209  "Undefined behavior: Call return type mismatches "
210  "callee return type",
211  &I);
212 
213  // Check argument types (in case the callee was casted) and attributes.
214  // TODO: Verify that caller and callee attributes are compatible.
215  Function::arg_iterator PI = F->arg_begin(), PE = F->arg_end();
216  auto AI = I.arg_begin(), AE = I.arg_end();
217  for (; AI != AE; ++AI) {
218  Value *Actual = *AI;
219  if (PI != PE) {
220  Argument *Formal = &*PI++;
221  Check(Formal->getType() == Actual->getType(),
222  "Undefined behavior: Call argument type mismatches "
223  "callee parameter type",
224  &I);
225 
226  // Check that noalias arguments don't alias other arguments. This is
227  // not fully precise because we don't know the sizes of the dereferenced
228  // memory regions.
229  if (Formal->hasNoAliasAttr() && Actual->getType()->isPointerTy()) {
230  AttributeList PAL = I.getAttributes();
231  unsigned ArgNo = 0;
232  for (auto BI = I.arg_begin(); BI != AE; ++BI, ++ArgNo) {
233  // Skip ByVal arguments since they will be memcpy'd to the callee's
234  // stack so we're not really passing the pointer anyway.
235  if (PAL.hasParamAttr(ArgNo, Attribute::ByVal))
236  continue;
237  // If both arguments are readonly, they have no dependence.
238  if (Formal->onlyReadsMemory() && I.onlyReadsMemory(ArgNo))
239  continue;
240  if (AI != BI && (*BI)->getType()->isPointerTy()) {
241  AliasResult Result = AA->alias(*AI, *BI);
242  Check(Result != AliasResult::MustAlias &&
243  Result != AliasResult::PartialAlias,
244  "Unusual: noalias argument aliases another argument", &I);
245  }
246  }
247  }
248 
249  // Check that an sret argument points to valid memory.
250  if (Formal->hasStructRetAttr() && Actual->getType()->isPointerTy()) {
251  Type *Ty = Formal->getParamStructRetType();
252  MemoryLocation Loc(
253  Actual, LocationSize::precise(DL->getTypeStoreSize(Ty)));
254  visitMemoryReference(I, Loc, DL->getABITypeAlign(Ty), Ty,
255  MemRef::Read | MemRef::Write);
256  }
257  }
258  }
259  }
260 
261  if (const auto *CI = dyn_cast<CallInst>(&I)) {
262  if (CI->isTailCall()) {
263  const AttributeList &PAL = CI->getAttributes();
264  unsigned ArgNo = 0;
265  for (Value *Arg : I.args()) {
266  // Skip ByVal arguments since they will be memcpy'd to the callee's
267  // stack anyway.
268  if (PAL.hasParamAttr(ArgNo++, Attribute::ByVal))
269  continue;
270  Value *Obj = findValue(Arg, /*OffsetOk=*/true);
271  Check(!isa<AllocaInst>(Obj),
272  "Undefined behavior: Call with \"tail\" keyword references "
273  "alloca",
274  &I);
275  }
276  }
277  }
278 
279  if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(&I))
280  switch (II->getIntrinsicID()) {
281  default:
282  break;
283 
284  // TODO: Check more intrinsics
285 
286  case Intrinsic::memcpy: {
287  MemCpyInst *MCI = cast<MemCpyInst>(&I);
288  visitMemoryReference(I, MemoryLocation::getForDest(MCI),
289  MCI->getDestAlign(), nullptr, MemRef::Write);
290  visitMemoryReference(I, MemoryLocation::getForSource(MCI),
291  MCI->getSourceAlign(), nullptr, MemRef::Read);
292 
293  // Check that the memcpy arguments don't overlap. The AliasAnalysis API
294  // isn't expressive enough for what we really want to do. Known partial
295  // overlap is not distinguished from the case where nothing is known.
297  if (const ConstantInt *Len =
298  dyn_cast<ConstantInt>(findValue(MCI->getLength(),
299  /*OffsetOk=*/false)))
300  if (Len->getValue().isIntN(32))
301  Size = LocationSize::precise(Len->getValue().getZExtValue());
302  Check(AA->alias(MCI->getSource(), Size, MCI->getDest(), Size) !=
304  "Undefined behavior: memcpy source and destination overlap", &I);
305  break;
306  }
307  case Intrinsic::memcpy_inline: {
308  MemCpyInlineInst *MCII = cast<MemCpyInlineInst>(&I);
309  const uint64_t Size = MCII->getLength()->getValue().getLimitedValue();
310  visitMemoryReference(I, MemoryLocation::getForDest(MCII),
311  MCII->getDestAlign(), nullptr, MemRef::Write);
312  visitMemoryReference(I, MemoryLocation::getForSource(MCII),
313  MCII->getSourceAlign(), nullptr, MemRef::Read);
314 
315  // Check that the memcpy arguments don't overlap. The AliasAnalysis API
316  // isn't expressive enough for what we really want to do. Known partial
317  // overlap is not distinguished from the case where nothing is known.
319  Check(AA->alias(MCII->getSource(), LS, MCII->getDest(), LS) !=
321  "Undefined behavior: memcpy source and destination overlap", &I);
322  break;
323  }
324  case Intrinsic::memmove: {
325  MemMoveInst *MMI = cast<MemMoveInst>(&I);
326  visitMemoryReference(I, MemoryLocation::getForDest(MMI),
327  MMI->getDestAlign(), nullptr, MemRef::Write);
328  visitMemoryReference(I, MemoryLocation::getForSource(MMI),
329  MMI->getSourceAlign(), nullptr, MemRef::Read);
330  break;
331  }
332  case Intrinsic::memset: {
333  MemSetInst *MSI = cast<MemSetInst>(&I);
334  visitMemoryReference(I, MemoryLocation::getForDest(MSI),
335  MSI->getDestAlign(), nullptr, MemRef::Write);
336  break;
337  }
338  case Intrinsic::memset_inline: {
339  MemSetInlineInst *MSII = cast<MemSetInlineInst>(&I);
340  visitMemoryReference(I, MemoryLocation::getForDest(MSII),
341  MSII->getDestAlign(), nullptr, MemRef::Write);
342  break;
343  }
344 
345  case Intrinsic::vastart:
346  Check(I.getParent()->getParent()->isVarArg(),
347  "Undefined behavior: va_start called in a non-varargs function",
348  &I);
349 
350  visitMemoryReference(I, MemoryLocation::getForArgument(&I, 0, TLI), None,
351  nullptr, MemRef::Read | MemRef::Write);
352  break;
353  case Intrinsic::vacopy:
354  visitMemoryReference(I, MemoryLocation::getForArgument(&I, 0, TLI), None,
355  nullptr, MemRef::Write);
356  visitMemoryReference(I, MemoryLocation::getForArgument(&I, 1, TLI), None,
357  nullptr, MemRef::Read);
358  break;
359  case Intrinsic::vaend:
360  visitMemoryReference(I, MemoryLocation::getForArgument(&I, 0, TLI), None,
361  nullptr, MemRef::Read | MemRef::Write);
362  break;
363 
364  case Intrinsic::stackrestore:
365  // Stackrestore doesn't read or write memory, but it sets the
366  // stack pointer, which the compiler may read from or write to
367  // at any time, so check it for both readability and writeability.
368  visitMemoryReference(I, MemoryLocation::getForArgument(&I, 0, TLI), None,
369  nullptr, MemRef::Read | MemRef::Write);
370  break;
371  case Intrinsic::get_active_lane_mask:
372  if (auto *TripCount = dyn_cast<ConstantInt>(I.getArgOperand(1)))
373  Check(!TripCount->isZero(),
374  "get_active_lane_mask: operand #2 "
375  "must be greater than 0",
376  &I);
377  break;
378  }
379 }
380 
381 void Lint::visitReturnInst(ReturnInst &I) {
382  Function *F = I.getParent()->getParent();
383  Check(!F->doesNotReturn(),
384  "Unusual: Return statement in function with noreturn attribute", &I);
385 
386  if (Value *V = I.getReturnValue()) {
387  Value *Obj = findValue(V, /*OffsetOk=*/true);
388  Check(!isa<AllocaInst>(Obj), "Unusual: Returning alloca value", &I);
389  }
390 }
391 
392 // TODO: Check that the reference is in bounds.
393 // TODO: Check readnone/readonly function attributes.
394 void Lint::visitMemoryReference(Instruction &I, const MemoryLocation &Loc,
395  MaybeAlign Align, Type *Ty, unsigned Flags) {
396  // If no memory is being referenced, it doesn't matter if the pointer
397  // is valid.
398  if (Loc.Size.isZero())
399  return;
400 
401  Value *Ptr = const_cast<Value *>(Loc.Ptr);
402  Value *UnderlyingObject = findValue(Ptr, /*OffsetOk=*/true);
403  Check(!isa<ConstantPointerNull>(UnderlyingObject),
404  "Undefined behavior: Null pointer dereference", &I);
405  Check(!isa<UndefValue>(UnderlyingObject),
406  "Undefined behavior: Undef pointer dereference", &I);
407  Check(!isa<ConstantInt>(UnderlyingObject) ||
408  !cast<ConstantInt>(UnderlyingObject)->isMinusOne(),
409  "Unusual: All-ones pointer dereference", &I);
410  Check(!isa<ConstantInt>(UnderlyingObject) ||
411  !cast<ConstantInt>(UnderlyingObject)->isOne(),
412  "Unusual: Address one pointer dereference", &I);
413 
414  if (Flags & MemRef::Write) {
415  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(UnderlyingObject))
416  Check(!GV->isConstant(), "Undefined behavior: Write to read-only memory",
417  &I);
418  Check(!isa<Function>(UnderlyingObject) &&
419  !isa<BlockAddress>(UnderlyingObject),
420  "Undefined behavior: Write to text section", &I);
421  }
422  if (Flags & MemRef::Read) {
423  Check(!isa<Function>(UnderlyingObject), "Unusual: Load from function body",
424  &I);
425  Check(!isa<BlockAddress>(UnderlyingObject),
426  "Undefined behavior: Load from block address", &I);
427  }
428  if (Flags & MemRef::Callee) {
429  Check(!isa<BlockAddress>(UnderlyingObject),
430  "Undefined behavior: Call to block address", &I);
431  }
432  if (Flags & MemRef::Branchee) {
433  Check(!isa<Constant>(UnderlyingObject) ||
434  isa<BlockAddress>(UnderlyingObject),
435  "Undefined behavior: Branch to non-blockaddress", &I);
436  }
437 
438  // Check for buffer overflows and misalignment.
439  // Only handles memory references that read/write something simple like an
440  // alloca instruction or a global variable.
441  int64_t Offset = 0;
443  // OK, so the access is to a constant offset from Ptr. Check that Ptr is
444  // something we can handle and if so extract the size of this base object
445  // along with its alignment.
447  MaybeAlign BaseAlign;
448 
449  if (AllocaInst *AI = dyn_cast<AllocaInst>(Base)) {
450  Type *ATy = AI->getAllocatedType();
451  if (!AI->isArrayAllocation() && ATy->isSized())
452  BaseSize = DL->getTypeAllocSize(ATy);
453  BaseAlign = AI->getAlign();
454  } else if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Base)) {
455  // If the global may be defined differently in another compilation unit
456  // then don't warn about funky memory accesses.
457  if (GV->hasDefinitiveInitializer()) {
458  Type *GTy = GV->getValueType();
459  if (GTy->isSized())
460  BaseSize = DL->getTypeAllocSize(GTy);
461  BaseAlign = GV->getAlign();
462  if (!BaseAlign && GTy->isSized())
463  BaseAlign = DL->getABITypeAlign(GTy);
464  }
465  }
466 
467  // Accesses from before the start or after the end of the object are not
468  // defined.
469  Check(!Loc.Size.hasValue() || BaseSize == MemoryLocation::UnknownSize ||
470  (Offset >= 0 && Offset + Loc.Size.getValue() <= BaseSize),
471  "Undefined behavior: Buffer overflow", &I);
472 
473  // Accesses that say that the memory is more aligned than it is are not
474  // defined.
475  if (!Align && Ty && Ty->isSized())
476  Align = DL->getABITypeAlign(Ty);
477  if (BaseAlign && Align)
478  Check(*Align <= commonAlignment(*BaseAlign, Offset),
479  "Undefined behavior: Memory reference address is misaligned", &I);
480  }
481 }
482 
483 void Lint::visitLoadInst(LoadInst &I) {
484  visitMemoryReference(I, MemoryLocation::get(&I), I.getAlign(), I.getType(),
485  MemRef::Read);
486 }
487 
488 void Lint::visitStoreInst(StoreInst &I) {
489  visitMemoryReference(I, MemoryLocation::get(&I), I.getAlign(),
490  I.getOperand(0)->getType(), MemRef::Write);
491 }
492 
493 void Lint::visitXor(BinaryOperator &I) {
494  Check(!isa<UndefValue>(I.getOperand(0)) || !isa<UndefValue>(I.getOperand(1)),
495  "Undefined result: xor(undef, undef)", &I);
496 }
497 
498 void Lint::visitSub(BinaryOperator &I) {
499  Check(!isa<UndefValue>(I.getOperand(0)) || !isa<UndefValue>(I.getOperand(1)),
500  "Undefined result: sub(undef, undef)", &I);
501 }
502 
503 void Lint::visitLShr(BinaryOperator &I) {
504  if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getOperand(1),
505  /*OffsetOk=*/false)))
506  Check(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
507  "Undefined result: Shift count out of range", &I);
508 }
509 
510 void Lint::visitAShr(BinaryOperator &I) {
511  if (ConstantInt *CI =
512  dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false)))
513  Check(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
514  "Undefined result: Shift count out of range", &I);
515 }
516 
517 void Lint::visitShl(BinaryOperator &I) {
518  if (ConstantInt *CI =
519  dyn_cast<ConstantInt>(findValue(I.getOperand(1), /*OffsetOk=*/false)))
520  Check(CI->getValue().ult(cast<IntegerType>(I.getType())->getBitWidth()),
521  "Undefined result: Shift count out of range", &I);
522 }
523 
524 static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT,
525  AssumptionCache *AC) {
526  // Assume undef could be zero.
527  if (isa<UndefValue>(V))
528  return true;
529 
530  VectorType *VecTy = dyn_cast<VectorType>(V->getType());
531  if (!VecTy) {
532  KnownBits Known =
533  computeKnownBits(V, DL, 0, AC, dyn_cast<Instruction>(V), DT);
534  return Known.isZero();
535  }
536 
537  // Per-component check doesn't work with zeroinitializer
538  Constant *C = dyn_cast<Constant>(V);
539  if (!C)
540  return false;
541 
542  if (C->isZeroValue())
543  return true;
544 
545  // For a vector, KnownZero will only be true if all values are zero, so check
546  // this per component
547  for (unsigned I = 0, N = cast<FixedVectorType>(VecTy)->getNumElements();
548  I != N; ++I) {
549  Constant *Elem = C->getAggregateElement(I);
550  if (isa<UndefValue>(Elem))
551  return true;
552 
553  KnownBits Known = computeKnownBits(Elem, DL);
554  if (Known.isZero())
555  return true;
556  }
557 
558  return false;
559 }
560 
561 void Lint::visitSDiv(BinaryOperator &I) {
562  Check(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
563  "Undefined behavior: Division by zero", &I);
564 }
565 
566 void Lint::visitUDiv(BinaryOperator &I) {
567  Check(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
568  "Undefined behavior: Division by zero", &I);
569 }
570 
571 void Lint::visitSRem(BinaryOperator &I) {
572  Check(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
573  "Undefined behavior: Division by zero", &I);
574 }
575 
576 void Lint::visitURem(BinaryOperator &I) {
577  Check(!isZero(I.getOperand(1), I.getModule()->getDataLayout(), DT, AC),
578  "Undefined behavior: Division by zero", &I);
579 }
580 
581 void Lint::visitAllocaInst(AllocaInst &I) {
582  if (isa<ConstantInt>(I.getArraySize()))
583  // This isn't undefined behavior, it's just an obvious pessimization.
584  Check(&I.getParent()->getParent()->getEntryBlock() == I.getParent(),
585  "Pessimization: Static alloca outside of entry block", &I);
586 
587  // TODO: Check for an unusual size (MSB set?)
588 }
589 
590 void Lint::visitVAArgInst(VAArgInst &I) {
591  visitMemoryReference(I, MemoryLocation::get(&I), None, nullptr,
592  MemRef::Read | MemRef::Write);
593 }
594 
595 void Lint::visitIndirectBrInst(IndirectBrInst &I) {
596  visitMemoryReference(I, MemoryLocation::getAfter(I.getAddress()), None,
597  nullptr, MemRef::Branchee);
598 
599  Check(I.getNumDestinations() != 0,
600  "Undefined behavior: indirectbr with no destinations", &I);
601 }
602 
603 void Lint::visitExtractElementInst(ExtractElementInst &I) {
604  if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getIndexOperand(),
605  /*OffsetOk=*/false)))
606  Check(
607  CI->getValue().ult(
608  cast<FixedVectorType>(I.getVectorOperandType())->getNumElements()),
609  "Undefined result: extractelement index out of range", &I);
610 }
611 
612 void Lint::visitInsertElementInst(InsertElementInst &I) {
613  if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getOperand(2),
614  /*OffsetOk=*/false)))
615  Check(CI->getValue().ult(
616  cast<FixedVectorType>(I.getType())->getNumElements()),
617  "Undefined result: insertelement index out of range", &I);
618 }
619 
620 void Lint::visitUnreachableInst(UnreachableInst &I) {
621  // This isn't undefined behavior, it's merely suspicious.
622  Check(&I == &I.getParent()->front() ||
623  std::prev(I.getIterator())->mayHaveSideEffects(),
624  "Unusual: unreachable immediately preceded by instruction without "
625  "side effects",
626  &I);
627 }
628 
629 /// findValue - Look through bitcasts and simple memory reference patterns
630 /// to identify an equivalent, but more informative, value. If OffsetOk
631 /// is true, look through getelementptrs with non-zero offsets too.
632 ///
633 /// Most analysis passes don't require this logic, because instcombine
634 /// will simplify most of these kinds of things away. But it's a goal of
635 /// this Lint pass to be useful even on non-optimized IR.
636 Value *Lint::findValue(Value *V, bool OffsetOk) const {
637  SmallPtrSet<Value *, 4> Visited;
638  return findValueImpl(V, OffsetOk, Visited);
639 }
640 
641 /// findValueImpl - Implementation helper for findValue.
642 Value *Lint::findValueImpl(Value *V, bool OffsetOk,
643  SmallPtrSetImpl<Value *> &Visited) const {
644  // Detect self-referential values.
645  if (!Visited.insert(V).second)
646  return UndefValue::get(V->getType());
647 
648  // TODO: Look through sext or zext cast, when the result is known to
649  // be interpreted as signed or unsigned, respectively.
650  // TODO: Look through eliminable cast pairs.
651  // TODO: Look through calls with unique return values.
652  // TODO: Look through vector insert/extract/shuffle.
653  V = OffsetOk ? getUnderlyingObject(V) : V->stripPointerCasts();
654  if (LoadInst *L = dyn_cast<LoadInst>(V)) {
655  BasicBlock::iterator BBI = L->getIterator();
656  BasicBlock *BB = L->getParent();
657  SmallPtrSet<BasicBlock *, 4> VisitedBlocks;
658  for (;;) {
659  if (!VisitedBlocks.insert(BB).second)
660  break;
661  if (Value *U =
663  return findValueImpl(U, OffsetOk, Visited);
664  if (BBI != BB->begin())
665  break;
666  BB = BB->getUniquePredecessor();
667  if (!BB)
668  break;
669  BBI = BB->end();
670  }
671  } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
672  if (Value *W = PN->hasConstantValue())
673  return findValueImpl(W, OffsetOk, Visited);
674  } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
675  if (CI->isNoopCast(*DL))
676  return findValueImpl(CI->getOperand(0), OffsetOk, Visited);
677  } else if (ExtractValueInst *Ex = dyn_cast<ExtractValueInst>(V)) {
678  if (Value *W =
679  FindInsertedValue(Ex->getAggregateOperand(), Ex->getIndices()))
680  if (W != V)
681  return findValueImpl(W, OffsetOk, Visited);
682  } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
683  // Same as above, but for ConstantExpr instead of Instruction.
684  if (Instruction::isCast(CE->getOpcode())) {
685  if (CastInst::isNoopCast(Instruction::CastOps(CE->getOpcode()),
686  CE->getOperand(0)->getType(), CE->getType(),
687  *DL))
688  return findValueImpl(CE->getOperand(0), OffsetOk, Visited);
689  } else if (CE->getOpcode() == Instruction::ExtractValue) {
690  ArrayRef<unsigned> Indices = CE->getIndices();
691  if (Value *W = FindInsertedValue(CE->getOperand(0), Indices))
692  if (W != V)
693  return findValueImpl(W, OffsetOk, Visited);
694  }
695  }
696 
697  // As a last resort, try SimplifyInstruction or constant folding.
698  if (Instruction *Inst = dyn_cast<Instruction>(V)) {
699  if (Value *W = simplifyInstruction(Inst, {*DL, TLI, DT, AC}))
700  return findValueImpl(W, OffsetOk, Visited);
701  } else if (auto *C = dyn_cast<Constant>(V)) {
702  Value *W = ConstantFoldConstant(C, *DL, TLI);
703  if (W != V)
704  return findValueImpl(W, OffsetOk, Visited);
705  }
706 
707  return V;
708 }
709 
711  auto *Mod = F.getParent();
712  auto *DL = &F.getParent()->getDataLayout();
713  auto *AA = &AM.getResult<AAManager>(F);
714  auto *AC = &AM.getResult<AssumptionAnalysis>(F);
715  auto *DT = &AM.getResult<DominatorTreeAnalysis>(F);
716  auto *TLI = &AM.getResult<TargetLibraryAnalysis>(F);
717  Lint L(Mod, DL, AA, AC, DT, TLI);
718  L.visit(F);
719  dbgs() << L.MessagesStr.str();
720  return PreservedAnalyses::all();
721 }
722 
723 namespace {
724 class LintLegacyPass : public FunctionPass {
725 public:
726  static char ID; // Pass identification, replacement for typeid
727  LintLegacyPass() : FunctionPass(ID) {
729  }
730 
731  bool runOnFunction(Function &F) override;
732 
733  void getAnalysisUsage(AnalysisUsage &AU) const override {
734  AU.setPreservesAll();
739  }
740  void print(raw_ostream &O, const Module *M) const override {}
741 };
742 } // namespace
743 
744 char LintLegacyPass::ID = 0;
745 INITIALIZE_PASS_BEGIN(LintLegacyPass, "lint", "Statically lint-checks LLVM IR",
746  false, true)
751 INITIALIZE_PASS_END(LintLegacyPass, "lint", "Statically lint-checks LLVM IR",
753 
754 bool LintLegacyPass::runOnFunction(Function &F) {
755  auto *Mod = F.getParent();
756  auto *DL = &F.getParent()->getDataLayout();
757  auto *AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
758  auto *AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
759  auto *DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
760  auto *TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI(F);
761  Lint L(Mod, DL, AA, AC, DT, TLI);
762  L.visit(F);
763  dbgs() << L.MessagesStr.str();
764  return false;
765 }
766 
767 //===----------------------------------------------------------------------===//
768 // Implement the public interfaces to this file...
769 //===----------------------------------------------------------------------===//
770 
771 FunctionPass *llvm::createLintLegacyPassPass() { return new LintLegacyPass(); }
772 
773 /// lintFunction - Check a function for errors, printing messages on stderr.
774 ///
776  Function &F = const_cast<Function &>(f);
777  assert(!F.isDeclaration() && "Cannot lint external functions");
778 
779  legacy::FunctionPassManager FPM(F.getParent());
780  auto *V = new LintLegacyPass();
781  FPM.add(V);
782  FPM.run(F);
783 }
784 
785 /// lintModule - Check a module for errors, printing messages on stderr.
786 ///
787 void llvm::lintModule(const Module &M) {
789  auto *V = new LintLegacyPass();
790  PM.add(V);
791  PM.run(const_cast<Module &>(M));
792 }
llvm::Check::Size
@ Size
Definition: FileCheck.h:77
llvm::PreservedAnalyses
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition: Argument.h:28
AssumptionCache.h
llvm::AAManager
A manager for alias analyses.
Definition: AliasAnalysis.h:1303
llvm::Argument::hasStructRetAttr
bool hasStructRetAttr() const
Return true if this argument has the sret attribute.
Definition: Function.cpp:256
llvm::Type::isSized
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:264
Lint.h
llvm::MemCpyInlineInst::getLength
ConstantInt * getLength() const
Definition: IntrinsicInst.h:1068
llvm::AliasResult::PartialAlias
@ PartialAlias
The two locations alias, but only due to a partial overlap.
Definition: AliasAnalysis.h:104
llvm::MemoryLocation::get
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
Definition: MemoryLocation.cpp:35
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::ReturnInst
Return a value (possibly void), from a function.
Definition: Instructions.h:3004
llvm::VAArgInst
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
Definition: Instructions.h:1819
llvm::CastInst::isNoopCast
static bool isNoopCast(Instruction::CastOps Opcode, Type *SrcTy, Type *DstTy, const DataLayout &DL)
A no-op cast is one that can be effected without changing any bits.
Definition: Instructions.cpp:2915
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:113
llvm::LocationSize::afterPointer
constexpr static LocationSize afterPointer()
Any location after the base pointer (but still within the underlying object).
Definition: MemoryLocation.h:124
print
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
Definition: ArchiveWriter.cpp:189
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:87
IntrinsicInst.h
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:218
llvm::MemIntrinsicBase::getDestAlign
MaybeAlign getDestAlign() const
Definition: IntrinsicInst.h:753
llvm::AnalysisManager::getResult
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:780
llvm::MemoryLocation::Ptr
const Value * Ptr
The address of the start of the location.
Definition: MemoryLocation.h:218
Loads.h
llvm::ExtractElementInst
This instruction extracts a single (scalar) element from a VectorType value.
Definition: Instructions.h:1859
llvm::Function
Definition: Function.h:60
llvm::Instruction::isCast
bool isCast() const
Definition: Instruction.h:165
Pass.h
llvm::UnderlyingObject
Definition: ScheduleDAGInstrs.h:108
llvm::MemMoveInst
This class wraps the llvm.memmove intrinsic.
Definition: IntrinsicInst.h:1054
llvm::raw_string_ostream
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:632
llvm::ConstantInt::getValue
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:133
llvm::RISCVFenceField::W
@ W
Definition: RISCVBaseInfo.h:241
llvm::GlobalVariable
Definition: GlobalVariable.h:39
ValueTracking.h
llvm::createLintLegacyPassPass
FunctionPass * createLintLegacyPassPass()
Definition: Lint.cpp:771
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:166
APInt.h
true
basic Basic Alias true
Definition: BasicAliasAnalysis.cpp:1886
llvm::lintModule
void lintModule(const Module &M)
Lint a module.
Definition: Lint.cpp:787
llvm::MemoryLocation::Size
LocationSize Size
The maximum size of the location, in address-units, or UnknownSize if the size is not known.
Definition: MemoryLocation.h:227
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
llvm::AttributeList
Definition: Attributes.h:425
T1
#define T1
Definition: Mips16ISelLowering.cpp:340
llvm::KnownBits::isZero
bool isZero() const
Returns true if value is all zero.
Definition: KnownBits.h:72
llvm::AliasResult
The possible results of an alias query.
Definition: AliasAnalysis.h:81
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:79
llvm::SmallPtrSet< Value *, 4 >
llvm::FunctionType::getNumParams
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
Definition: DerivedTypes.h:139
llvm::initializeLintLegacyPassPass
void initializeLintLegacyPassPass(PassRegistry &)
LegacyPassManager.h
ConstantFolding.h
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::FunctionType::isVarArg
bool isVarArg() const
Definition: DerivedTypes.h:123
KnownBits.h
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
AliasAnalysis.h
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
checks
Implicit null checks
Definition: ImplicitNullChecks.cpp:822
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:186
Instruction.h
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
Constants.h
isZero
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:524
llvm::AAResults
Definition: AliasAnalysis.h:511
f
Itanium Name Demangler i e convert the string _Z1fv into f()". You can also use the CRTP base ManglingParser to perform some simple analysis on the mangled name
llvm::LocationSize
Definition: MemoryLocation.h:66
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
Twine.h
InstrTypes.h
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition: PassAnalysisSupport.h:47
llvm::MemoryLocation::getForSource
static MemoryLocation getForSource(const MemTransferInst *MTI)
Return a location representing the source of a memory transfer.
Definition: MemoryLocation.cpp:93
llvm::LocationSize::getValue
uint64_t getValue() const
Definition: MemoryLocation.h:159
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
llvm::MemCpyInlineInst
This class wraps the llvm.memcpy.inline intrinsic.
Definition: IntrinsicInst.h:1066
Check
#define Check(C,...)
Definition: Lint.cpp:170
llvm::APInt::getLimitedValue
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
Definition: APInt.h:456
TargetLibraryInfo.h
llvm::InsertElementInst
This instruction inserts a single (scalar) element into a VectorType value.
Definition: Instructions.h:1923
false
Definition: StackSlotColoring.cpp:141
llvm::MaybeAlign
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.
Definition: Alignment.h:117
llvm::Instruction::CastOps
CastOps
Definition: Instruction.h:800
llvm::Instruction
Definition: Instruction.h:42
llvm::DominatorTreeWrapperPass
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:302
llvm::AttributeList::getAttributes
AttributeSet getAttributes(unsigned Index) const
The attributes for the specified index are returned.
Definition: Attributes.cpp:1501
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1777
llvm::MemSetInlineInst
This class wraps the llvm.memset.inline intrinsic.
Definition: IntrinsicInst.h:1007
llvm::LocationSize::precise
static LocationSize precise(uint64_t Value)
Definition: MemoryLocation.h:101
llvm::getUnderlyingObject
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value,...
Definition: ValueTracking.cpp:4455
IR
Statically lint checks LLVM IR
Definition: Lint.cpp:751
SmallPtrSet.h
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
llvm::MemTransferBase::getSourceAlign
MaybeAlign getSourceAlign() const
Definition: IntrinsicInst.h:814
llvm::None
const NoneType None
Definition: None.h:24
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Type.h
llvm::MemSetInst
This class wraps the llvm.memset and llvm.memset.inline intrinsics.
Definition: IntrinsicInst.h:989
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:58
llvm::Argument::getParamStructRetType
Type * getParamStructRetType() const
If this is an sret argument, return its type.
Definition: Function.cpp:209
llvm::VectorType
Base class of all SIMD vector types.
Definition: DerivedTypes.h:389
BasicBlock.h
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:239
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:297
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::TargetLibraryInfoWrapperPass
Definition: TargetLibraryInfo.h:468
uint64_t
llvm::LocationSize::hasValue
bool hasValue() const
Definition: MemoryLocation.h:156
llvm::LocationSize::isZero
bool isZero() const
Definition: MemoryLocation.h:171
llvm::AssumptionAnalysis
A function analysis which provides an AssumptionCache.
Definition: AssumptionCache.h:173
INITIALIZE_PASS_DEPENDENCY
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
MemoryLocation.h
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::simplifyInstruction
Value * simplifyInstruction(Instruction *I, const SimplifyQuery &Q, OptimizationRemarkEmitter *ORE=nullptr)
See if we can compute a simplified version of this instruction.
Definition: InstructionSimplify.cpp:6486
ArrayRef.h
llvm::computeKnownBits
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, OptimizationRemarkEmitter *ORE=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
Definition: ValueTracking.cpp:222
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::MemoryLocation::getForArgument
static MemoryLocation getForArgument(const CallBase *Call, unsigned ArgIdx, const TargetLibraryInfo *TLI)
Return a location representing a particular argument of a call.
Definition: MemoryLocation.cpp:158
memcpy
<%struct.s * > cast struct s *S to sbyte *< sbyte * > sbyte uint cast struct s *agg result to sbyte *< sbyte * > sbyte uint cast struct s *memtmp to sbyte *< sbyte * > sbyte uint ret void llc ends up issuing two memcpy or custom lower memcpy(of small size) to be ldmia/stmia. I think option 2 is better but the current register allocator cannot allocate a chunk of registers at a time. A feasible temporary solution is to use specific physical registers at the lowering time for small(<
llvm::WinEH::EncodingType::CE
@ CE
Windows NT (Windows on ARM)
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::legacy::FunctionPassManager::add
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition: LegacyPassManager.cpp:1335
llvm::MemIntrinsicBase::getDest
Value * getDest() const
This is just like getRawDest, but it strips off any cast instructions (including addrspacecast) that ...
Definition: IntrinsicInst.h:740
llvm::AssumptionCacheTracker
An immutable pass that tracks lazily created AssumptionCache objects.
Definition: AssumptionCache.h:202
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::BinaryOperator
Definition: InstrTypes.h:188
DataLayout.h
InstVisitor.h
llvm::AssumptionCache
A cache of @llvm.assume calls within a function.
Definition: AssumptionCache.h:42
llvm::Constant::getAggregateElement
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:410
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::AliasResult::MustAlias
@ MustAlias
The two locations precisely alias each other.
Definition: AliasAnalysis.h:106
llvm::InstVisitor
Base class for instruction visitors.
Definition: InstVisitor.h:78
llvm::CastInst
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:429
llvm::lintFunction
void lintFunction(const Function &F)
lintFunction - Check a function for errors, printing messages on stderr.
Definition: Lint.cpp:775
llvm::LoadInst
An instruction for reading from memory.
Definition: Instructions.h:173
Argument.h
Callee
amdgpu Simplify well known AMD library false FunctionCallee Callee
Definition: AMDGPULibCalls.cpp:186
runOnFunction
static bool runOnFunction(Function &F, bool PostInlining)
Definition: EntryExitInstrumenter.cpp:69
Constant.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::FindInsertedValue
Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, Instruction *InsertBefore=nullptr)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
Definition: ValueTracking.cpp:4076
llvm::commonAlignment
Align commonAlignment(Align A, Align B)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:213
llvm::ConstantExpr
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:972
llvm::KnownBits
Definition: KnownBits.h:23
llvm::legacy::FunctionPassManager::run
bool run(Function &F)
run - Execute all of the passes scheduled for execution.
Definition: LegacyPassManager.cpp:1343
llvm::AnalysisUsage::setPreservesAll
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: PassAnalysisSupport.h:130
llvm::MemIntrinsicBase::getLength
Value * getLength() const
Definition: IntrinsicInst.h:731
llvm::PreservedAnalyses::all
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
llvm::legacy::PassManager
PassManager manages ModulePassManagers.
Definition: LegacyPassManager.h:52
GlobalVariable.h
llvm::ExtractValueInst
This instruction extracts a struct member or array element value from an aggregate value.
Definition: Instructions.h:2398
llvm::AArch64CC::LS
@ LS
Definition: AArch64BaseInfo.h:264
Casting.h
Function.h
llvm::FindAvailableLoadedValue
Value * FindAvailableLoadedValue(LoadInst *Load, BasicBlock *ScanBB, BasicBlock::iterator &ScanFrom, unsigned MaxInstsToScan=DefMaxInstsToScan, AAResults *AA=nullptr, bool *IsLoadCSE=nullptr, unsigned *NumScanedInst=nullptr)
Scan backwards to see if we have the value of the given load available locally within a small number ...
Definition: Loads.cpp:424
PassManager.h
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition: TargetLibraryInfo.h:222
llvm::GetPointerBaseWithConstantOffset
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
Definition: ValueTracking.h:278
llvm::legacy::PassManager::add
void add(Pass *P) override
Add a pass to the queue of passes to run.
Definition: LegacyPassManager.cpp:1665
MemRef
Definition: Lint.cpp:81
llvm::IndirectBrInst
Indirect Branch Instruction.
Definition: Instructions.h:3628
llvm::IntrinsicInst
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:46
AA
llvm::DominatorTreeAnalysis
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:267
Instructions.h
llvm::MemCpyInst
This class wraps the llvm.memcpy intrinsic.
Definition: IntrinsicInst.h:1041
llvm::MemoryLocation::UnknownSize
@ UnknownSize
Definition: MemoryLocation.h:215
Dominators.h
llvm::MemoryLocation::getAfter
static MemoryLocation getAfter(const Value *Ptr, const AAMDNodes &AATags=AAMDNodes())
Return a location that may access any location after Ptr, while remaining within the underlying objec...
Definition: MemoryLocation.h:270
N
#define N
llvm::AAResultsWrapperPass
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object.
Definition: AliasAnalysis.h:1351
InstructionSimplify.h
llvm::legacy::FunctionPassManager
FunctionPassManager manages FunctionPasses.
Definition: LegacyPassManager.h:71
llvm::Argument::hasNoAliasAttr
bool hasNoAliasAttr() const
Return true if this argument has the noalias attribute.
Definition: Function.cpp:241
llvm::PHINode
Definition: Instructions.h:2651
llvm::ConstantFoldConstant
Constant * ConstantFoldConstant(const Constant *C, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr)
ConstantFoldConstant - Fold the constant using the specified DataLayout.
Definition: ConstantFolding.cpp:1206
llvm::legacy::PassManager::run
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
Definition: LegacyPassManager.cpp:1671
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1174
DerivedTypes.h
llvm::SmallPtrSetImpl< Value * >
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:42
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
lint
lint
Definition: Lint.cpp:751
llvm::UnreachableInst
This function has undefined behavior.
Definition: Instructions.h:4727
llvm::MemoryLocation::getForDest
static MemoryLocation getForDest(const MemIntrinsic *MI)
Return a location representing the destination of a memory set or transfer.
Definition: MemoryLocation.cpp:106
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition: PassAnalysisSupport.h:75
llvm::AllocaInst
an instruction to allocate memory on the stack
Definition: Instructions.h:58
Mod
Module * Mod
Definition: PassBuilderBindings.cpp:54
raw_ostream.h
llvm::AttributeList::hasParamAttr
bool hasParamAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Return true if the attribute exists for the given argument.
Definition: Attributes.h:751
llvm::Argument::onlyReadsMemory
bool onlyReadsMemory() const
Return true if this argument has the readonly or readnone attribute.
Definition: Function.cpp:277
Value.h
INITIALIZE_PASS_BEGIN
INITIALIZE_PASS_BEGIN(LintLegacyPass, "lint", "Statically lint-checks LLVM IR", false, true) INITIALIZE_PASS_END(LintLegacyPass
InitializePasses.h
llvm::FunctionType::getReturnType
Type * getReturnType() const
Definition: DerivedTypes.h:124
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::TargetLibraryAnalysis
Analysis pass providing the TargetLibraryInfo.
Definition: TargetLibraryInfo.h:443
llvm::MemoryLocation
Representation for a specific memory location.
Definition: MemoryLocation.h:210
llvm::sampleprof::Base
@ Base
Definition: Discriminator.h:58
llvm::DefMaxInstsToScan
cl::opt< unsigned > DefMaxInstsToScan
The default number of maximum instructions to scan in the block, used by FindAvailableLoadedValue().
llvm::FunctionType
Class to represent function types.
Definition: DerivedTypes.h:103
llvm::MemTransferBase::getSource
Value * getSource() const
This is just like getRawSource, but it strips off any cast instructions that feed it,...
Definition: IntrinsicInst.h:800
llvm::SmallPtrSetImpl::insert
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:365
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38
llvm::LintPass::run
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
Definition: Lint.cpp:710