LLVM  14.0.0git
GlobalStatus.h
Go to the documentation of this file.
1 //===- GlobalStatus.h - Compute status info for globals ---------*- 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 #ifndef LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
10 #define LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
11 
12 #include "llvm/IR/Instructions.h"
14 
15 namespace llvm {
16 
17 class Constant;
18 class Function;
19 class Value;
20 
21 /// It is safe to destroy a constant iff it is only used by constants itself.
22 /// Note that constants cannot be cyclic, so this test is pretty easy to
23 /// implement recursively.
24 ///
26 
27 /// As we analyze each global, keep track of some information about it. If we
28 /// find out that the address of the global is taken, none of this info will be
29 /// accurate.
30 struct GlobalStatus {
31  /// True if the global's address is used in a comparison.
32  bool IsCompared = false;
33 
34  /// True if the global is ever loaded. If the global isn't ever loaded it
35  /// can be deleted.
36  bool IsLoaded = false;
37 
38  /// Keep track of what stores to the global look like.
39  enum StoredType {
40  /// There is no store to this global. It can thus be marked constant.
42 
43  /// This global is stored to, but the only thing stored is the constant it
44  /// was initialized with. This is only tracked for scalar globals.
46 
47  /// This global is stored to, but only its initializer and one other value
48  /// is ever stored to it. If this global isStoredOnce, we track the value
49  /// stored to it via StoredOnceStore below. This is only tracked for scalar
50  /// globals.
52 
53  /// This global is stored to by multiple values or something else that we
54  /// cannot track.
57 
58  /// If only one value (besides the initializer constant) is ever stored to
59  /// this global, keep track of what value it is via the store instruction.
60  const StoreInst *StoredOnceStore = nullptr;
61 
62  /// If only one value (besides the initializer constant) is ever stored to
63  /// this global return the stored value.
67  : nullptr;
68  }
69 
70  /// These start out null/false. When the first accessing function is noticed,
71  /// it is recorded. When a second different accessing function is noticed,
72  /// HasMultipleAccessingFunctions is set to true.
73  const Function *AccessingFunction = nullptr;
75 
76  /// Set to true if this global has a user that is not an instruction (e.g. a
77  /// constant expr or GV initializer).
78  bool HasNonInstructionUser = false;
79 
80  /// Set to the strongest atomic ordering requirement.
82 
83  GlobalStatus();
84 
85  /// Look at all uses of the global and fill in the GlobalStatus structure. If
86  /// the global has its address taken, return true to indicate we can't do
87  /// anything with it.
88  static bool analyzeGlobal(const Value *V, GlobalStatus &GS);
89 };
90 
91 } // end namespace llvm
92 
93 #endif // LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
AtomicOrdering.h
llvm::Function
Definition: Function.h:61
llvm::GlobalStatus::analyzeGlobal
static bool analyzeGlobal(const Value *V, GlobalStatus &GS)
Look at all uses of the global and fill in the GlobalStatus structure.
Definition: GlobalStatus.cpp:193
llvm::GlobalStatus::StoredOnceStore
const StoreInst * StoredOnceStore
If only one value (besides the initializer constant) is ever stored to this global,...
Definition: GlobalStatus.h:60
llvm::GlobalStatus::getStoredOnceValue
Value * getStoredOnceValue() const
If only one value (besides the initializer constant) is ever stored to this global return the stored ...
Definition: GlobalStatus.h:64
llvm::GlobalStatus
As we analyze each global, keep track of some information about it.
Definition: GlobalStatus.h:30
llvm::GlobalStatus::Ordering
AtomicOrdering Ordering
Set to the strongest atomic ordering requirement.
Definition: GlobalStatus.h:81
llvm::ISD::Constant
@ Constant
Definition: ISDOpcodes.h:76
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
llvm::GlobalStatus::IsLoaded
bool IsLoaded
True if the global is ever loaded.
Definition: GlobalStatus.h:36
llvm::isSafeToDestroyConstant
bool isSafeToDestroyConstant(const Constant *C)
It is safe to destroy a constant iff it is only used by constants itself.
Definition: GlobalStatus.cpp:44
llvm::GlobalStatus::IsCompared
bool IsCompared
True if the global's address is used in a comparison.
Definition: GlobalStatus.h:32
llvm::AtomicOrdering
AtomicOrdering
Atomic ordering for LLVM's memory model.
Definition: AtomicOrdering.h:56
llvm::StoreInst
An instruction for storing to memory.
Definition: Instructions.h:304
llvm::X86AS::GS
@ GS
Definition: X86.h:187
llvm::GlobalStatus::Stored
@ Stored
This global is stored to by multiple values or something else that we cannot track.
Definition: GlobalStatus.h:55
llvm::GlobalStatus::HasMultipleAccessingFunctions
bool HasMultipleAccessingFunctions
Definition: GlobalStatus.h:74
llvm::GlobalStatus::HasNonInstructionUser
bool HasNonInstructionUser
Set to true if this global has a user that is not an instruction (e.g.
Definition: GlobalStatus.h:78
llvm::GlobalStatus::InitializerStored
@ InitializerStored
This global is stored to, but the only thing stored is the constant it was initialized with.
Definition: GlobalStatus.h:45
llvm::GlobalStatus::AccessingFunction
const Function * AccessingFunction
These start out null/false.
Definition: GlobalStatus.h:73
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::GlobalStatus::GlobalStatus
GlobalStatus()
llvm::GlobalStatus::StoredType
StoredType
Keep track of what stores to the global look like.
Definition: GlobalStatus.h:39
llvm::GlobalStatus::StoredOnce
@ StoredOnce
This global is stored to, but only its initializer and one other value is ever stored to it.
Definition: GlobalStatus.h:51
Instructions.h
llvm::User::getOperand
Value * getOperand(unsigned i) const
Definition: User.h:169
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::AtomicOrdering::NotAtomic
@ NotAtomic
llvm::codeview::PublicSymFlags::Function
@ Function
llvm::GlobalStatus::NotStored
@ NotStored
There is no store to this global. It can thus be marked constant.
Definition: GlobalStatus.h:41