ValueLatticeUtils.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 non-volatile loads
  29. // or stores of the global type, and the global cannot be stored itself.
  30. if (auto *Store = dyn_cast<StoreInst>(U))
  31. return Store->getValueOperand() != GV && !Store->isVolatile() &&
  32. Store->getValueOperand()->getType() == GV->getValueType();
  33. if (auto *Load = dyn_cast<LoadInst>(U))
  34. return !Load->isVolatile() && Load->getType() == GV->getValueType();
  35. return false;
  36. });
  37. }