LLVM 20.0.0git
Macros | Functions
PromoteMemoryToRegister.cpp File Reference
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitVector.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Analysis/AssumptionCache.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/IteratedDominanceFrontier.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DebugProgramInstruction.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/User.h"
#include "llvm/Support/Casting.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/PromoteMemToReg.h"
#include <algorithm>
#include <cassert>
#include <iterator>
#include <utility>
#include <vector>

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "mem2reg"
 

Functions

 STATISTIC (NumLocalPromoted, "Number of alloca's promoted within one block")
 
 STATISTIC (NumSingleStore, "Number of alloca's promoted with a single store")
 
 STATISTIC (NumDeadAlloca, "Number of dead alloca's removed")
 
 STATISTIC (NumPHIInsert, "Number of PHI nodes inserted")
 
static void addAssumeNonNull (AssumptionCache *AC, LoadInst *LI)
 Given a LoadInst LI this adds assume(LI != null) after it.
 
static void convertMetadataToAssumes (LoadInst *LI, Value *Val, const DataLayout &DL, AssumptionCache *AC, const DominatorTree *DT)
 
static void removeIntrinsicUsers (AllocaInst *AI)
 
static bool rewriteSingleStoreAlloca (AllocaInst *AI, AllocaInfo &Info, LargeBlockInfo &LBI, const DataLayout &DL, DominatorTree &DT, AssumptionCache *AC, SmallSet< DbgAssignIntrinsic *, 8 > *DbgAssignsToDelete, SmallSet< DbgVariableRecord *, 8 > *DVRAssignsToDelete)
 Rewrite as many loads as possible given a single store.
 
static bool promoteSingleBlockAlloca (AllocaInst *AI, const AllocaInfo &Info, LargeBlockInfo &LBI, const DataLayout &DL, DominatorTree &DT, AssumptionCache *AC, SmallSet< DbgAssignIntrinsic *, 8 > *DbgAssignsToDelete, SmallSet< DbgVariableRecord *, 8 > *DVRAssignsToDelete)
 Many allocas are only used within a single basic block.
 
static void updateForIncomingValueLocation (PHINode *PN, DebugLoc DL, bool ApplyMergedLoc)
 Update the debug location of a phi.
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "mem2reg"

Definition at line 59 of file PromoteMemoryToRegister.cpp.

Function Documentation

◆ addAssumeNonNull()

static void addAssumeNonNull ( AssumptionCache AC,
LoadInst LI 
)
static

◆ convertMetadataToAssumes()

static void convertMetadataToAssumes ( LoadInst LI,
Value Val,
const DataLayout DL,
AssumptionCache AC,
const DominatorTree DT 
)
static

◆ promoteSingleBlockAlloca()

static bool promoteSingleBlockAlloca ( AllocaInst AI,
const AllocaInfo &  Info,
LargeBlockInfo &  LBI,
const DataLayout DL,
DominatorTree DT,
AssumptionCache AC,
SmallSet< DbgAssignIntrinsic *, 8 > *  DbgAssignsToDelete,
SmallSet< DbgVariableRecord *, 8 > *  DVRAssignsToDelete 
)
static

Many allocas are only used within a single basic block.

If this is the case, avoid traversing the CFG and inserting a lot of potentially useless PHI nodes by just performing a single linear pass over the basic block using the Alloca.

If we cannot promote this alloca (because it is read before it is written), return false. This is necessary in cases where, due to control flow, the alloca is undefined only on some control flow paths. e.g. code like this is correct in LLVM IR: // A is an alloca with no stores so far for (...) { int t = *A; if (!first_iteration) use(t); *A = 42; }

Definition at line 635 of file PromoteMemoryToRegister.cpp.

References llvm::ConvertDebugDeclareToDebugValue(), convertMetadataToAssumes(), llvm::at::deleteAssignmentMarkers(), DL, llvm::Instruction::eraseFromParent(), llvm::UndefValue::get(), llvm::PoisonValue::get(), llvm::Instruction::getModule(), llvm::Value::getType(), I, Info, llvm::lower_bound(), llvm::make_early_inc_range(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), llvm::Value::replaceAllUsesWith(), llvm::sort(), llvm::Value::use_empty(), llvm::Instruction::user_back(), and llvm::Value::users().

◆ removeIntrinsicUsers()

static void removeIntrinsicUsers ( AllocaInst AI)
static

◆ rewriteSingleStoreAlloca()

static bool rewriteSingleStoreAlloca ( AllocaInst AI,
AllocaInfo &  Info,
LargeBlockInfo &  LBI,
const DataLayout DL,
DominatorTree DT,
AssumptionCache AC,
SmallSet< DbgAssignIntrinsic *, 8 > *  DbgAssignsToDelete,
SmallSet< DbgVariableRecord *, 8 > *  DVRAssignsToDelete 
)
static

Rewrite as many loads as possible given a single store.

When there is only a single store, we can use the domtree to trivially replace all of the dominated loads with the stored value. Do so, and return true if this has successfully promoted the alloca entirely. If this returns false there were some loads which were not dominated by the single store and thus must be phi-ed with undef. We fall back to the standard alloca promotion algorithm in that case.

Definition at line 520 of file PromoteMemoryToRegister.cpp.

References llvm::ConvertDebugDeclareToDebugValue(), convertMetadataToAssumes(), llvm::at::deleteAssignmentMarkers(), DL, llvm::DominatorTree::dominates(), llvm::Instruction::eraseFromParent(), llvm::PoisonValue::get(), llvm::Instruction::getModule(), llvm::User::getOperand(), llvm::ilist_detail::node_parent_access< NodeTy, ParentTy >::getParent(), llvm::Value::getType(), Info, llvm::isGuaranteedNotToBePoison(), llvm::make_early_inc_range(), llvm::Value::replaceAllUsesWith(), and llvm::Value::users().

◆ STATISTIC() [1/4]

STATISTIC ( NumDeadAlloca  ,
"Number of dead alloca's removed"   
)

◆ STATISTIC() [2/4]

STATISTIC ( NumLocalPromoted  ,
"Number of alloca's promoted within one block"   
)

◆ STATISTIC() [3/4]

STATISTIC ( NumPHIInsert  ,
"Number of PHI nodes inserted"   
)

◆ STATISTIC() [4/4]

STATISTIC ( NumSingleStore  ,
"Number of alloca's promoted with a single store"   
)

◆ updateForIncomingValueLocation()

static void updateForIncomingValueLocation ( PHINode PN,
DebugLoc  DL,
bool  ApplyMergedLoc 
)
static

Update the debug location of a phi.

ApplyMergedLoc indicates whether to create a merged location incorporating DL, or to set DL directly.

Definition at line 1082 of file PromoteMemoryToRegister.cpp.

References llvm::Instruction::applyMergedLocation(), DL, llvm::Instruction::getDebugLoc(), and llvm::Instruction::setDebugLoc().