xref: /llvm-project/llvm/lib/Analysis/ValueLatticeUtils.cpp (revision 5d639034652dda9659e45ffd863e5052ab8ffc4a)
12284937bSMatthew Simpson //===-- ValueLatticeUtils.cpp - Utils for solving lattices ------*- C++ -*-===//
22284937bSMatthew Simpson //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62284937bSMatthew Simpson //
72284937bSMatthew Simpson //===----------------------------------------------------------------------===//
82284937bSMatthew Simpson //
92284937bSMatthew Simpson // This file implements common functions useful for performing data-flow
102284937bSMatthew Simpson // analyses that propagate values across function boundaries.
112284937bSMatthew Simpson //
122284937bSMatthew Simpson //===----------------------------------------------------------------------===//
132284937bSMatthew Simpson 
142284937bSMatthew Simpson #include "llvm/Analysis/ValueLatticeUtils.h"
152284937bSMatthew Simpson #include "llvm/IR/GlobalVariable.h"
162284937bSMatthew Simpson #include "llvm/IR/Instructions.h"
172284937bSMatthew Simpson using namespace llvm;
182284937bSMatthew Simpson 
canTrackArgumentsInterprocedurally(Function * F)192284937bSMatthew Simpson bool llvm::canTrackArgumentsInterprocedurally(Function *F) {
202284937bSMatthew Simpson   return F->hasLocalLinkage() && !F->hasAddressTaken();
212284937bSMatthew Simpson }
222284937bSMatthew Simpson 
canTrackReturnsInterprocedurally(Function * F)232284937bSMatthew Simpson bool llvm::canTrackReturnsInterprocedurally(Function *F) {
242284937bSMatthew Simpson   return F->hasExactDefinition() && !F->hasFnAttribute(Attribute::Naked);
252284937bSMatthew Simpson }
262284937bSMatthew Simpson 
canTrackGlobalVariableInterprocedurally(GlobalVariable * GV)272284937bSMatthew Simpson bool llvm::canTrackGlobalVariableInterprocedurally(GlobalVariable *GV) {
282284937bSMatthew Simpson   if (GV->isConstant() || !GV->hasLocalLinkage() ||
292284937bSMatthew Simpson       !GV->hasDefinitiveInitializer())
302284937bSMatthew Simpson     return false;
310b72b9d0SFlorian Hahn   return all_of(GV->users(), [&](User *U) {
32*5d639034SNikita Popov     // Currently all users of a global variable have to be non-volatile loads
33*5d639034SNikita Popov     // or stores of the global type, and the global cannot be stored itself.
340b72b9d0SFlorian Hahn     if (auto *Store = dyn_cast<StoreInst>(U))
35*5d639034SNikita Popov       return Store->getValueOperand() != GV && !Store->isVolatile() &&
36*5d639034SNikita Popov              Store->getValueOperand()->getType() == GV->getValueType();
370b72b9d0SFlorian Hahn     if (auto *Load = dyn_cast<LoadInst>(U))
38*5d639034SNikita Popov       return !Load->isVolatile() && Load->getType() == GV->getValueType();
390b72b9d0SFlorian Hahn 
402284937bSMatthew Simpson     return false;
412284937bSMatthew Simpson   });
422284937bSMatthew Simpson }
43