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
14#include "llvm/Analysis/ValueLatticeUtils.h"
15#include "llvm/IR/GlobalVariable.h"
16#include "llvm/IR/Instructions.h"
17using namespace llvm;
18
19bool llvm::canTrackArgumentsInterprocedurally(Function *F) {
20 return F->hasExactDefinition() && F->hasLocalLinkage() &&
21 !F->hasAddressTaken();
22}
23
24bool llvm::canTrackReturnsInterprocedurally(Function *F) {
25 return F->hasExactDefinition() && !F->hasFnAttribute(Kind: Attribute::Naked);
26}
27
28bool llvm::canTrackGlobalVariableInterprocedurally(GlobalVariable *GV) {
29 if (GV->isConstant() || !GV->hasLocalLinkage() ||
30 !GV->hasDefinitiveInitializer())
31 return false;
32 return all_of(Range: GV->users(), P: [&](User *U) {
33 // Currently all users of a global variable have to be non-volatile loads
34 // or stores of the global type, and the global cannot be stored itself.
35 if (auto *Store = dyn_cast<StoreInst>(Val: U))
36 return Store->getValueOperand() != GV && !Store->isVolatile() &&
37 Store->getValueOperand()->getType() == GV->getValueType();
38 if (auto *Load = dyn_cast<LoadInst>(Val: U))
39 return !Load->isVolatile() && Load->getType() == GV->getValueType();
40
41 return false;
42 });
43}
44