1//===- GlobalStatus.h - Compute status info for globals ---------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
11#define LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
12
13#include "llvm/Support/AtomicOrdering.h"
14
15namespace llvm {
16
17class Constant;
18class Function;
19class 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///
25bool isSafeToDestroyConstant(const Constant *C);
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.
30struct 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.
41    NotStored,
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.
45    InitializerStored,
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 in StoredOnceValue below.  This is only tracked for scalar
50    /// globals.
51    StoredOnce,
52
53    /// This global is stored to by multiple values or something else that we
54    /// cannot track.
55    Stored
56  } StoredType = NotStored;
57
58  /// If only one value (besides the initializer constant) is ever stored to
59  /// this global, keep track of what value it is.
60  Value *StoredOnceValue = nullptr;
61
62  /// These start out null/false.  When the first accessing function is noticed,
63  /// it is recorded. When a second different accessing function is noticed,
64  /// HasMultipleAccessingFunctions is set to true.
65  const Function *AccessingFunction = nullptr;
66  bool HasMultipleAccessingFunctions = false;
67
68  /// Set to true if this global has a user that is not an instruction (e.g. a
69  /// constant expr or GV initializer).
70  bool HasNonInstructionUser = false;
71
72  /// Set to the strongest atomic ordering requirement.
73  AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
74
75  GlobalStatus();
76
77  /// Look at all uses of the global and fill in the GlobalStatus structure.  If
78  /// the global has its address taken, return true to indicate we can't do
79  /// anything with it.
80  static bool analyzeGlobal(const Value *V, GlobalStatus &GS);
81};
82
83} // end namespace llvm
84
85#endif // LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
86