ValueLatticeUtils.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //===-- ValueLatticeUtils.cpp - Utils for solving lattices ------*- C++ -*-===//
  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 file implements common functions useful for performing data-flow
  10. // analyses that propagate values across function boundaries.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Analysis/ValueLatticeUtils.h"
  14. #include "llvm/IR/GlobalVariable.h"
  15. #include "llvm/IR/Instructions.h"
  16. using namespace llvm;
  17. bool llvm::canTrackArgumentsInterprocedurally(Function *F) {
  18. return F->hasLocalLinkage() && !F->hasAddressTaken();
  19. }
  20. bool llvm::canTrackReturnsInterprocedurally(Function *F) {
  21. return F->hasExactDefinition() && !F->hasFnAttribute(Attribute::Naked);
  22. }
  23. bool llvm::canTrackGlobalVariableInterprocedurally(GlobalVariable *GV) {
  24. if (GV->isConstant() || !GV->hasLocalLinkage() ||
  25. !GV->hasDefinitiveInitializer())
  26. return false;
  27. return all_of(GV->users(), [&](User *U) {
  28. // Currently all users of a global variable have to be none-volatile loads
  29. // or stores and the global cannot be stored itself.
  30. if (auto *Store = dyn_cast<StoreInst>(U))
  31. return Store->getValueOperand() != GV && !Store->isVolatile();
  32. if (auto *Load = dyn_cast<LoadInst>(U))
  33. return !Load->isVolatile();
  34. return false;
  35. });
  36. }