Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 76 additions & 2 deletions llvm/include/llvm/Transforms/Scalar/GVN.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Analysis/PHITransAddr.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/PassManager.h"
Expand All @@ -37,8 +38,10 @@ class AAResults;
class AssumeInst;
class AssumptionCache;
class BasicBlock;
class BatchAAResults;
class BranchInst;
class CallInst;
class EarliestEscapeAnalysis;
class ExtractValueInst;
class Function;
class FunctionPass;
Expand Down Expand Up @@ -255,6 +258,7 @@ class GVNPass : public PassInfoMixin<GVNPass> {
OptimizationRemarkEmitter *ORE = nullptr;
ImplicitControlFlowTracking *ICF = nullptr;
LoopInfo *LI = nullptr;
AAResults *AA = nullptr;
MemorySSAUpdater *MSSAU = nullptr;

ValueTable VN;
Expand Down Expand Up @@ -344,21 +348,91 @@ class GVNPass : public PassInfoMixin<GVNPass> {
// List of critical edges to be split between iterations.
SmallVector<std::pair<Instruction *, unsigned>, 4> ToSplit;

enum class DepKind {
Other = 0, // Unknown value.
Def, // Exactly overlapping locations.
Clobber, // Reaching value superset of needed bits.
};

// Describe a memory location value, such that there exists a path to a point
// in the program, along which that memory location is not modified.
struct ReachingMemVal {
DepKind Kind;
BasicBlock *Block;
const Value *Addr;
Instruction *Inst;
int32_t Offset;

static ReachingMemVal getUnknown(BasicBlock *BB, const Value *Addr,
Instruction *Inst = nullptr) {
return {DepKind::Other, BB, Addr, Inst, -1};
}

static ReachingMemVal getDef(const Value *Addr, Instruction *Inst) {
return {DepKind::Def, Inst->getParent(), Addr, Inst, -1};
}

static ReachingMemVal getClobber(const Value *Addr, Instruction *Inst,
int32_t Offset = -1) {
return {DepKind::Clobber, Inst->getParent(), Addr, Inst, Offset};
}
};

struct DependencyBlockInfo {
DependencyBlockInfo() = delete;
DependencyBlockInfo(const PHITransAddr &Addr, MemoryAccess *ClobberMA)
: Addr(Addr), InitialClobberMA(ClobberMA), ClobberMA(ClobberMA),
ForceUnknown(false), Visited(false) {}
PHITransAddr Addr;
MemoryAccess *InitialClobberMA;
MemoryAccess *ClobberMA;
std::optional<ReachingMemVal> MemVal;
bool ForceUnknown : 1;
bool Visited : 1;
};

using DependencyBlockSet = DenseMap<BasicBlock *, DependencyBlockInfo>;

std::optional<GVNPass::ReachingMemVal> scanMemoryAccessesUsers(
const MemoryLocation &Loc, bool IsInvariantLoad, BasicBlock *BB,
const SmallVectorImpl<MemoryAccess *> &ClobbersList, MemorySSA &MSSA,
BatchAAResults &AA, LoadInst *L = nullptr);

std::optional<GVNPass::ReachingMemVal>
accessMayModifyLocation(MemoryAccess *ClobberMA, const MemoryLocation &Loc,
bool IsInvariantLoad, BasicBlock *BB, MemorySSA &MSSA,
BatchAAResults &AA);

bool collectPredecessors(BasicBlock *BB, const PHITransAddr &Addr,
MemoryAccess *ClobberMA, DependencyBlockSet &Blocks,
SmallVectorImpl<BasicBlock *> &Worklist);

void collectClobberList(SmallVectorImpl<MemoryAccess *> &Clobbers,
BasicBlock *BB, const DependencyBlockInfo &StartInfo,
const DependencyBlockSet &Blocks, MemorySSA &MSSA);

bool findReachingValuesForLoad(LoadInst *Inst,
SmallVectorImpl<ReachingMemVal> &Values,
MemorySSA &MSSA, AAResults &AA);

// Helper functions of redundant load elimination.
bool processLoad(LoadInst *L);
bool processMaskedLoad(IntrinsicInst *I);
bool processNonLocalLoad(LoadInst *L);
bool processNonLocalLoad(LoadInst *L, SmallVectorImpl<ReachingMemVal> &Deps);
bool processAssumeIntrinsic(AssumeInst *II);

/// Given a local dependency (Def or Clobber) determine if a value is
/// available for the load.
std::optional<gvn::AvailableValue>
AnalyzeLoadAvailability(LoadInst *Load, MemDepResult DepInfo, Value *Address);
AnalyzeLoadAvailability(LoadInst *Load, const ReachingMemVal &Dep,
Value *Address);

/// Given a list of non-local dependencies, determine if a value is
/// available for the load in each specified block. If it is, add it to
/// ValuesPerBlock. If not, add it to UnavailableBlocks.
void AnalyzeLoadAvailability(LoadInst *Load, LoadDepVect &Deps,
void AnalyzeLoadAvailability(LoadInst *Load,
SmallVectorImpl<ReachingMemVal> &Deps,
AvailValInBlkVect &ValuesPerBlock,
UnavailBlkVect &UnavailableBlocks);

Expand Down
Loading
Loading