Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:43

0001 //===- GlobalStatus.h - Compute status info for globals ---------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 
0009 #ifndef LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
0010 #define LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H
0011 
0012 #include "llvm/IR/Instructions.h"
0013 #include "llvm/Support/AtomicOrdering.h"
0014 
0015 namespace llvm {
0016 
0017 class Constant;
0018 class Function;
0019 class Value;
0020 
0021 /// It is safe to destroy a constant iff it is only used by constants itself.
0022 /// Note that constants cannot be cyclic, so this test is pretty easy to
0023 /// implement recursively.
0024 ///
0025 bool isSafeToDestroyConstant(const Constant *C);
0026 
0027 /// As we analyze each global or thread-local variable, keep track of some
0028 /// information about it.  If we find out that the address of the global is
0029 /// taken, none of this info will be accurate.
0030 struct GlobalStatus {
0031   /// True if the global's address is used in a comparison.
0032   bool IsCompared = false;
0033 
0034   /// True if the global is ever loaded.  If the global isn't ever loaded it
0035   /// can be deleted.
0036   bool IsLoaded = false;
0037 
0038   /// Number of stores to the global.
0039   unsigned NumStores = 0;
0040 
0041   /// Keep track of what stores to the global look like.
0042   enum StoredType {
0043     /// There is no store to this global.  It can thus be marked constant.
0044     NotStored,
0045 
0046     /// This global is stored to, but the only thing stored is the constant it
0047     /// was initialized with. This is only tracked for scalar globals.
0048     InitializerStored,
0049 
0050     /// This global is stored to, but only its initializer and one other value
0051     /// is ever stored to it.  If this global isStoredOnce, we track the value
0052     /// stored to it via StoredOnceStore below.  This is only tracked for scalar
0053     /// globals.
0054     StoredOnce,
0055 
0056     /// This global is stored to by multiple values or something else that we
0057     /// cannot track.
0058     Stored
0059   } StoredType = NotStored;
0060 
0061   /// If only one value (besides the initializer constant) is ever stored to
0062   /// this global, keep track of what value it is via the store instruction.
0063   const StoreInst *StoredOnceStore = nullptr;
0064 
0065   /// If only one value (besides the initializer constant) is ever stored to
0066   /// this global return the stored value.
0067   Value *getStoredOnceValue() const {
0068     return (StoredType == StoredOnce && StoredOnceStore)
0069                ? StoredOnceStore->getOperand(0)
0070                : nullptr;
0071   }
0072 
0073   /// These start out null/false.  When the first accessing function is noticed,
0074   /// it is recorded. When a second different accessing function is noticed,
0075   /// HasMultipleAccessingFunctions is set to true.
0076   const Function *AccessingFunction = nullptr;
0077   bool HasMultipleAccessingFunctions = false;
0078 
0079   /// Set to the strongest atomic ordering requirement.
0080   AtomicOrdering Ordering = AtomicOrdering::NotAtomic;
0081 
0082   GlobalStatus();
0083 
0084   /// Look at all uses of the global and fill in the GlobalStatus structure.  If
0085   /// the global has its address taken, return true to indicate we can't do
0086   /// anything with it.
0087   static bool analyzeGlobal(const Value *V, GlobalStatus &GS);
0088 };
0089 
0090 } // end namespace llvm
0091 
0092 #endif // LLVM_TRANSFORMS_UTILS_GLOBALSTATUS_H