Skip to content

Commit b0bd023

Browse files
momchil-velikovantoniofrighetto
authored andcommitted
[GVN] MemorySSA for GVN: eliminate redundant loads via MemorySSA
1 parent 18e4f77 commit b0bd023

File tree

2 files changed

+571
-48
lines changed
  • llvm
    • include/llvm/Transforms/Scalar
    • lib/Transforms/Scalar

2 files changed

+571
-48
lines changed

llvm/include/llvm/Transforms/Scalar/GVN.h

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class AAResults;
3636
class AssumeInst;
3737
class AssumptionCache;
3838
class BasicBlock;
39+
class BatchAAResults;
3940
class BranchInst;
4041
class CallInst;
4142
class ExtractValueInst;
@@ -253,6 +254,7 @@ class GVNPass : public PassInfoMixin<GVNPass> {
253254
OptimizationRemarkEmitter *ORE = nullptr;
254255
ImplicitControlFlowTracking *ICF = nullptr;
255256
LoopInfo *LI = nullptr;
257+
AAResults *AA = nullptr;
256258
MemorySSAUpdater *MSSAU = nullptr;
257259

258260
ValueTable VN;
@@ -347,20 +349,59 @@ class GVNPass : public PassInfoMixin<GVNPass> {
347349
// List of critical edges to be split between iterations.
348350
SmallVector<std::pair<Instruction *, unsigned>, 4> ToSplit;
349351

352+
enum class DepKind {
353+
Other = 0, // Unknown value.
354+
Def, // Exactly overlapping locations.
355+
Clobber, // Reaching value superset of needed bits.
356+
};
357+
358+
struct ReachingMemVal {
359+
DepKind Kind;
360+
BasicBlock *Block;
361+
const Value *Addr;
362+
Instruction *Inst;
363+
int32_t Offset;
364+
365+
static ReachingMemVal getUnknown(BasicBlock *BB, const Value *Addr,
366+
Instruction *Inst = nullptr) {
367+
return {DepKind::Other, BB, Addr, Inst, -1};
368+
}
369+
370+
static ReachingMemVal getDef(const Value *Addr, Instruction *Inst) {
371+
return {DepKind::Def, Inst->getParent(), Addr, Inst, -1};
372+
}
373+
374+
static ReachingMemVal getClobber(const Value *Addr, Instruction *Inst,
375+
int32_t Offset = -1) {
376+
return {DepKind::Clobber, Inst->getParent(), Addr, Inst, Offset};
377+
}
378+
};
379+
380+
std::optional<ReachingMemVal> findReachingValueForLoadInBlock(
381+
const MemoryLocation &Loc, bool IsInvariantload, BasicBlock *BB,
382+
Instruction *DomLower, Instruction *DomUpper, MemoryAccess *ClobberMA,
383+
MemorySSA &MSSA, BatchAAResults &AA);
384+
385+
bool findReachingValuesForLoad(LoadInst *Inst, MemorySSA &MSSA, AAResults &AA,
386+
SmallVectorImpl<ReachingMemVal> &Values);
387+
350388
// Helper functions of redundant load elimination.
351389
bool processLoad(LoadInst *L);
352390
bool processNonLocalLoad(LoadInst *L);
391+
bool processNonLocalLoad(LoadInst *L, SmallVectorImpl<ReachingMemVal> &Deps);
353392
bool processAssumeIntrinsic(AssumeInst *II);
354393

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

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

0 commit comments

Comments
 (0)