GlobalStatus.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- GlobalStatus.h - Compute status info for globals ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
  14. #define LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
  15. #include "llvm/Support/AtomicOrdering.h"
  16. namespace llvm {
  17. class Constant;
  18. class Function;
  19. class Value;
  20. /// It is safe to destroy a constant iff it is only used by constants itself.
  21. /// Note that constants cannot be cyclic, so this test is pretty easy to
  22. /// implement recursively.
  23. ///
  24. bool isSafeToDestroyConstant(const Constant *C);
  25. /// As we analyze each global, keep track of some information about it. If we
  26. /// find out that the address of the global is taken, none of this info will be
  27. /// accurate.
  28. struct GlobalStatus {
  29. /// True if the global's address is used in a comparison.
  30. bool IsCompared = false;
  31. /// True if the global is ever loaded. If the global isn't ever loaded it
  32. /// can be deleted.
  33. bool IsLoaded = false;
  34. /// Keep track of what stores to the global look like.
  35. enum StoredType {
  36. /// There is no store to this global. It can thus be marked constant.
  37. NotStored,
  38. /// This global is stored to, but the only thing stored is the constant it
  39. /// was initialized with. This is only tracked for scalar globals.
  40. InitializerStored,
  41. /// This global is stored to, but only its initializer and one other value
  42. /// is ever stored to it. If this global isStoredOnce, we track the value
  43. /// stored to it in StoredOnceValue below. This is only tracked for scalar
  44. /// globals.
  45. StoredOnce,
  46. /// This global is stored to by multiple values or something else that we
  47. /// cannot track.
  48. Stored
  49. } StoredType = NotStored;
  50. /// If only one value (besides the initializer constant) is ever stored to
  51. /// this global, keep track of what value it is.
  52. Value *StoredOnceValue = nullptr;
  53. /// These start out null/false. When the first accessing function is noticed,
  54. /// it is recorded. When a second different accessing function is noticed,
  55. /// HasMultipleAccessingFunctions is set to true.
  56. const Function *AccessingFunction = nullptr;
  57. bool HasMultipleAccessingFunctions = false;
  58. /// Set to true if this global has a user that is not an instruction (e.g. a
  59. /// constant expr or GV initializer).
  60. bool HasNonInstructionUser = false;
  61. /// Set to the strongest atomic ordering requirement.
  62. AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
  63. GlobalStatus();
  64. /// Look at all uses of the global and fill in the GlobalStatus structure. If
  65. /// the global has its address taken, return true to indicate we can't do
  66. /// anything with it.
  67. static bool analyzeGlobal(const Value *V, GlobalStatus &GS);
  68. };
  69. } // end namespace llvm
  70. #endif // LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
  71. #ifdef __GNUC__
  72. #pragma GCC diagnostic pop
  73. #endif