GlobalStatus.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. /// Keep track of what stores to the global look like.
  36. enum StoredType {
  37. /// There is no store to this global. It can thus be marked constant.
  38. NotStored,
  39. /// This global is stored to, but the only thing stored is the constant it
  40. /// was initialized with. This is only tracked for scalar globals.
  41. InitializerStored,
  42. /// This global is stored to, but only its initializer and one other value
  43. /// is ever stored to it. If this global isStoredOnce, we track the value
  44. /// stored to it via StoredOnceStore below. This is only tracked for scalar
  45. /// globals.
  46. StoredOnce,
  47. /// This global is stored to by multiple values or something else that we
  48. /// cannot track.
  49. Stored
  50. } StoredType = NotStored;
  51. /// If only one value (besides the initializer constant) is ever stored to
  52. /// this global, keep track of what value it is via the store instruction.
  53. const StoreInst *StoredOnceStore = nullptr;
  54. /// If only one value (besides the initializer constant) is ever stored to
  55. /// this global return the stored value.
  56. Value *getStoredOnceValue() const {
  57. return (StoredType == StoredOnce && StoredOnceStore)
  58. ? StoredOnceStore->getOperand(0)
  59. : nullptr;
  60. }
  61. /// These start out null/false. When the first accessing function is noticed,
  62. /// it is recorded. When a second different accessing function is noticed,
  63. /// HasMultipleAccessingFunctions is set to true.
  64. const Function *AccessingFunction = nullptr;
  65. bool HasMultipleAccessingFunctions = false;
  66. /// Set to the strongest atomic ordering requirement.
  67. AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
  68. GlobalStatus();
  69. /// Look at all uses of the global and fill in the GlobalStatus structure. If
  70. /// the global has its address taken, return true to indicate we can't do
  71. /// anything with it.
  72. static bool analyzeGlobal(const Value *V, GlobalStatus &GS);
  73. };
  74. } // end namespace llvm
  75. #endif // LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
  76. #ifdef __GNUC__
  77. #pragma GCC diagnostic pop
  78. #endif