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