LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

GlobalOpt.cpp File Reference

#include "llvm/Transforms/IPO.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Instructions.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/Debug.h"
#include "llvm/Target/TargetData.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringExtras.h"
#include <set>
#include <algorithm>

Include dependency graph for GlobalOpt.cpp:

Go to the source code of this file.

Classes

struct  GlobalStatus

Defines

#define DEBUG_TYPE   "globalopt"

Functions

ModulePassllvm::createGlobalOptimizerPass ()
static bool ConstantIsDead (Constant *C)
static bool AnalyzeGlobal (Value *V, GlobalStatus &GS, std::set< PHINode * > &PHIUsers)
static ConstantgetAggregateConstantElement (Constant *Agg, Constant *Idx)
static ConstantTraverseGEPInitializer (User *GEP, Constant *Init)
static bool CleanupConstantGlobalUsers (Value *V, Constant *Init)
static GlobalVariableSRAGlobal (GlobalVariable *GV)
static bool AllUsesOfValueWillTrapIfNull (Value *V)
static bool AllUsesOfLoadedValueWillTrapIfNull (GlobalVariable *GV)
static bool OptimizeAwayTrappingUsesOfValue (Value *V, Constant *NewV)
static bool OptimizeAwayTrappingUsesOfLoads (GlobalVariable *GV, Constant *LV)
static void ConstantPropUsersOf (Value *V)
static GlobalVariableOptimizeGlobalAddressOfMalloc (GlobalVariable *GV, MallocInst *MI)
static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal (Instruction *V, GlobalVariable *GV)
static bool OptimizeOnceStoredGlobal (GlobalVariable *GV, Value *StoredOnceVal, Module::giterator &GVI, TargetData &TD)


Define Documentation

#define DEBUG_TYPE   "globalopt"
 

Definition at line 16 of file GlobalOpt.cpp.


Function Documentation

static bool AllUsesOfLoadedValueWillTrapIfNull GlobalVariable GV  )  [static]
 

AllUsesOfLoadedValueWillTrapIfNull - Return true if all uses of any loads from GV will trap if the loaded value is null. Note that this also permits comparisons of the loaded value against null, as a special case.

Definition at line 480 of file GlobalOpt.cpp.

References AllUsesOfValueWillTrapIfNull(), E, llvm::Value::use_begin(), and llvm::Value::use_end().

Referenced by OptimizeOnceStoredGlobal().

static bool AllUsesOfValueWillTrapIfNull Value V  )  [static]
 

AllUsesOfValueWillTrapIfNull - Return true if all users of the specified value will trap if the value is dynamically null.

Definition at line 444 of file GlobalOpt.cpp.

References E, llvm::Value::use_begin(), and llvm::Value::use_end().

Referenced by AllUsesOfLoadedValueWillTrapIfNull().

static bool AnalyzeGlobal Value V,
GlobalStatus GS,
std::set< PHINode * > &  PHIUsers
[static]
 

AnalyzeGlobal - Look at all uses of the global and fill in the GlobalStatus structure. If the global has its address taken, return true to indicate we can't do anything with it.

Definition at line 124 of file GlobalOpt.cpp.

References C, ConstantIsDead(), E, I, GlobalStatus::isInitializerStored, GlobalStatus::isLoaded, GlobalStatus::isNotSuitableForSRA, GlobalStatus::isStored, GlobalStatus::isStoredOnce, llvm::Select, GlobalStatus::StoredOnceValue, GlobalStatus::StoredType, llvm::Value::use_begin(), and llvm::Value::use_end().

static bool CleanupConstantGlobalUsers Value V,
Constant Init
[static]
 

CleanupConstantGlobalUsers - We just marked GV constant. Loop over all users of the global, cleaning up the obvious ones. This is largely just a quick scan over the use list to clean up the easy and obvious cruft. This returns true if it made a change.

Definition at line 273 of file GlobalOpt.cpp.

References C, ConstantIsDead(), E, TraverseGEPInitializer(), U, llvm::Value::use_begin(), and llvm::Value::use_end().

Referenced by OptimizeAwayTrappingUsesOfLoads().

static bool ConstantIsDead Constant C  )  [static]
 

ConstantIsDead - Return true if the specified constant is (transitively) dead. The constant may be used by other constants (e.g. constant arrays and constant exprs) as long as they are dead, but it cannot be used by anything else.

Definition at line 108 of file GlobalOpt.cpp.

References E.

Referenced by AnalyzeGlobal(), and CleanupConstantGlobalUsers().

static void ConstantPropUsersOf Value V  )  [static]
 

ConstantPropUsersOf - Walk the use list of V, constant folding all of the instructions that are foldable.

Definition at line 608 of file GlobalOpt.cpp.

References llvm::ConstantFoldInstruction(), E, I, llvm::Value::use_begin(), and llvm::Value::use_end().

Referenced by OptimizeGlobalAddressOfMalloc().

static Constant* getAggregateConstantElement Constant Agg,
Constant Idx
[static]
 

Definition at line 223 of file GlobalOpt.cpp.

References llvm::ConstantIntegral::getRawValue().

Referenced by SRAGlobal(), and TraverseGEPInitializer().

static bool OptimizeAwayTrappingUsesOfLoads GlobalVariable GV,
Constant LV
[static]
 

OptimizeAwayTrappingUsesOfLoads - The specified global has only one non-null value stored into it. If there are uses of the loaded value that would trap if the loaded value is dynamically null, then we know that they cannot be reachable with a null optimize away the load.

Definition at line 560 of file GlobalOpt.cpp.

References CleanupConstantGlobalUsers(), DEBUG, E, llvm::GlobalVariable::eraseFromParent(), llvm::Instruction::eraseFromParent(), NumDeleted, NumGlobUses, OptimizeAwayTrappingUsesOfValue(), llvm::Value::use_begin(), llvm::Value::use_empty(), and llvm::Value::use_end().

Referenced by OptimizeOnceStoredGlobal().

static bool OptimizeAwayTrappingUsesOfValue Value V,
Constant NewV
[static]
 

Definition at line 496 of file GlobalOpt.cpp.

References C, E, llvm::Value::getType(), I, llvm::Value::use_begin(), and llvm::Value::use_end().

Referenced by OptimizeAwayTrappingUsesOfLoads().

static GlobalVariable* OptimizeGlobalAddressOfMalloc GlobalVariable GV,
MallocInst MI
[static]
 

OptimizeGlobalAddressOfMalloc - This function takes the specified global variable, and transforms the program as if it always contained the result of the specified malloc. Because it is always the result of the specified malloc, there is no reason to actually DO the malloc. Instead, turn the malloc into a global, and any laods of GV as uses of the new global.

Definition at line 633 of file GlobalOpt.cpp.

References ConstantPropUsersOf(), DEBUG, llvm::GlobalVariable::eraseFromParent(), llvm::Instruction::eraseFromParent(), False, llvm::AllocationInst::getAllocatedType(), llvm::AllocationInst::getArraySize(), llvm::SequentialType::getElementType(), llvm::Module::getGlobalList(), llvm::Value::getName(), llvm::BinaryOperator::getOpcode(), llvm::GlobalValue::getParent(), llvm::ConstantIntegral::getRawValue(), llvm::GlobalValue::getType(), llvm::Use::getUser(), llvm::Value::replaceAllUsesWith(), llvm::ISD::SetEQ, llvm::ISD::SetGE, llvm::ISD::SetGT, llvm::ISD::SetLE, llvm::ISD::SetLT, llvm::ISD::SetNE, True, llvm::Value::use_back(), llvm::Value::use_begin(), and llvm::Value::use_empty().

Referenced by OptimizeOnceStoredGlobal().

static bool OptimizeOnceStoredGlobal GlobalVariable GV,
Value StoredOnceVal,
Module::giterator &  GVI,
TargetData TD
[static]
 

Definition at line 766 of file GlobalOpt.cpp.

References AllUsesOfLoadedValueWillTrapIfNull(), llvm::GlobalVariable::getInitializer(), llvm::Value::getType(), llvm::TargetData::getTypeSize(), llvm::Constant::isNullValue(), OptimizeAwayTrappingUsesOfLoads(), OptimizeGlobalAddressOfMalloc(), and ValueIsOnlyUsedLocallyOrStoredToOneGlobal().

static GlobalVariable* SRAGlobal GlobalVariable GV  )  [static]
 

SRAGlobal - Perform scalar replacement of aggregates on the specified global variable. This opens the door for other optimizations by exposing the behavior of the program in a more fine-grained way. We have determined that this transformation is safe already. We return the first global variable we insert so that the caller can reprocess it.

Definition at line 333 of file GlobalOpt.cpp.

References DEBUG, getAggregateConstantElement(), llvm::Module::getGlobalList(), llvm::GlobalVariable::getInitializer(), llvm::Value::getName(), llvm::User::getNumOperands(), llvm::User::getOperand(), llvm::GlobalValue::getParent(), llvm::Value::getType(), llvm::GlobalValue::hasInternalLinkage(), llvm::GlobalVariable::isConstant(), NumSRA, llvm::Value::replaceAllUsesWith(), llvm::Value::use_back(), llvm::Value::use_empty(), llvm::Value::use_size(), and llvm::utostr().

static Constant* TraverseGEPInitializer User GEP,
Constant Init
[static]
 

Definition at line 254 of file GlobalOpt.cpp.

References getAggregateConstantElement(), llvm::User::getNumOperands(), and llvm::User::getOperand().

Referenced by CleanupConstantGlobalUsers().

static bool ValueIsOnlyUsedLocallyOrStoredToOneGlobal Instruction V,
GlobalVariable GV
[static]
 

ValueIsOnlyUsedLocallyOrStoredToOneGlobal - Scan the use-list of V checking to make sure that there are no complex uses of V. We permit simple things like dereferencing the pointer, but not storing through the address, unless it is to the specified global.

Definition at line 745 of file GlobalOpt.cpp.

References E, llvm::User::getOperand(), llvm::Value::use_begin(), and llvm::Value::use_end().

Referenced by OptimizeOnceStoredGlobal().


Variable Documentation

Statistic NumDeleted("globalopt","Number of globals deleted") [static]
 

Referenced by llvm::Inliner::doFinalization(), InlineCallIfPossible(), OptimizeAwayTrappingUsesOfLoads(), and removeIdenticalCalls().

Statistic NumFnDeleted("globalopt","Number of functions deleted") [static]
 

Statistic NumGlobUses("globalopt","Number of global uses devirtualized") [static]
 

Referenced by OptimizeAwayTrappingUsesOfLoads().

Statistic NumMarked("globalopt","Number of globals marked constant") [static]
 

Statistic NumSRA("globalopt","Number of aggregate globals broken ""into scalars") [static]
 

Referenced by SRAGlobal().

Statistic NumSubstitute("globalopt","Number of globals with initializers stored into them") [static]
 

RegisterOpt<GlobalOpt> X("globalopt","Global Variable Optimizer") [static]