GlobalStatus.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. #ifndef LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
  10. #define LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
  11. #include "llvm/IR/Instructions.h"
  12. namespace llvm {
  13. class Value;
  14. class Function;
  15. /// It is safe to destroy a constant iff it is only used by constants itself.
  16. /// Note that constants cannot be cyclic, so this test is pretty easy to
  17. /// implement recursively.
  18. ///
  19. bool isSafeToDestroyConstant(const Constant *C);
  20. /// As we analyze each global, keep track of some information about it. If we
  21. /// find out that the address of the global is taken, none of this info will be
  22. /// accurate.
  23. struct GlobalStatus {
  24. /// True if the global's address is used in a comparison.
  25. bool IsCompared;
  26. /// True if the global is ever loaded. If the global isn't ever loaded it
  27. /// can be deleted.
  28. bool IsLoaded;
  29. /// Keep track of what stores to the global look like.
  30. enum StoredType {
  31. /// There is no store to this global. It can thus be marked constant.
  32. NotStored,
  33. /// This global is stored to, but the only thing stored is the constant it
  34. /// was initialized with. This is only tracked for scalar globals.
  35. InitializerStored,
  36. /// This global is stored to, but only its initializer and one other value
  37. /// is ever stored to it. If this global isStoredOnce, we track the value
  38. /// stored to it in StoredOnceValue below. This is only tracked for scalar
  39. /// globals.
  40. StoredOnce,
  41. /// This global is stored to by multiple values or something else that we
  42. /// cannot track.
  43. Stored
  44. } StoredType;
  45. /// If only one value (besides the initializer constant) is ever stored to
  46. /// this global, keep track of what value it is.
  47. Value *StoredOnceValue;
  48. /// These start out null/false. When the first accessing function is noticed,
  49. /// it is recorded. When a second different accessing function is noticed,
  50. /// HasMultipleAccessingFunctions is set to true.
  51. const Function *AccessingFunction;
  52. bool HasMultipleAccessingFunctions;
  53. /// Set to true if this global has a user that is not an instruction (e.g. a
  54. /// constant expr or GV initializer).
  55. bool HasNonInstructionUser;
  56. /// Set to the strongest atomic ordering requirement.
  57. AtomicOrdering Ordering;
  58. /// Look at all uses of the global and fill in the GlobalStatus structure. If
  59. /// the global has its address taken, return true to indicate we can't do
  60. /// anything with it.
  61. static bool analyzeGlobal(const Value *V, GlobalStatus &GS);
  62. GlobalStatus();
  63. };
  64. }
  65. #endif