GlobalStatus.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/IR/Instructions.h"
  16. #include "llvm/Support/AtomicOrdering.h"
  17. namespace llvm {
  18. class Constant;
  19. class Function;
  20. class Value;
  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. ///
  25. bool isSafeToDestroyConstant(const Constant *C);
  26. /// As we analyze each global, keep track of some information about it. If we
  27. /// find out that the address of the global is taken, none of this info will be
  28. /// accurate.
  29. struct GlobalStatus {
  30. /// True if the global's address is used in a comparison.
  31. bool IsCompared = false;
  32. /// True if the global is ever loaded. If the global isn't ever loaded it
  33. /// can be deleted.
  34. bool IsLoaded = false;
  35. /// Number of stores to the global.
  36. unsigned NumStores = 0;
  37. /// Keep track of what stores to the global look like.
  38. enum StoredType {
  39. /// There is no store to this global. It can thus be marked constant.
  40. NotStored,
  41. /// This global is stored to, but the only thing stored is the constant it
  42. /// was initialized with. This is only tracked for scalar globals.
  43. InitializerStored,
  44. /// This global is stored to, but only its initializer and one other value
  45. /// is ever stored to it. If this global isStoredOnce, we track the value
  46. /// stored to it via StoredOnceStore below. This is only tracked for scalar
  47. /// globals.
  48. StoredOnce,
  49. /// This global is stored to by multiple values or something else that we
  50. /// cannot track.
  51. Stored
  52. } StoredType = NotStored;
  53. /// If only one value (besides the initializer constant) is ever stored to
  54. /// this global, keep track of what value it is via the store instruction.
  55. const StoreInst *StoredOnceStore = nullptr;
  56. /// If only one value (besides the initializer constant) is ever stored to
  57. /// this global return the stored value.
  58. Value *getStoredOnceValue() const {
  59. return (StoredType == StoredOnce && StoredOnceStore)
  60. ? StoredOnceStore->getOperand(0)
  61. : nullptr;
  62. }
  63. /// These start out null/false. When the first accessing function is noticed,
  64. /// it is recorded. When a second different accessing function is noticed,
  65. /// HasMultipleAccessingFunctions is set to true.
  66. const Function *AccessingFunction = nullptr;
  67. bool HasMultipleAccessingFunctions = false;
  68. /// Set to the strongest atomic ordering requirement.
  69. AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
  70. GlobalStatus();
  71. /// Look at all uses of the global and fill in the GlobalStatus structure. If
  72. /// the global has its address taken, return true to indicate we can't do
  73. /// anything with it.
  74. static bool analyzeGlobal(const Value *V, GlobalStatus &GS);
  75. };
  76. } // end namespace llvm
  77. #endif // LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
  78. #ifdef __GNUC__
  79. #pragma GCC diagnostic pop
  80. #endif