Skip to content

Commit d46a69c

Browse files
authored
[PredicateInfo] Use BumpPtrAllocator for predicates (NFC) (#145692)
Currently predicates are allocated on the heap and tracked with an intrusive list. Use a bump pointer allocator instead, which is more efficient. The list is no longer needed, as we don't have to track predicates for freeing. The bump pointer allocator is provided as a parameter for PredicateInfo to allow reusing the same allocator for all functions during IPSCCP.
1 parent 5fb0ae1 commit d46a69c

File tree

4 files changed

+27
-22
lines changed

4 files changed

+27
-22
lines changed

llvm/include/llvm/Transforms/Utils/PredicateInfo.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@
5252

5353
#include "llvm/ADT/DenseMap.h"
5454
#include "llvm/ADT/SmallSet.h"
55-
#include "llvm/ADT/ilist.h"
56-
#include "llvm/ADT/ilist_node.h"
5755
#include "llvm/IR/Instructions.h"
5856
#include "llvm/IR/PassManager.h"
5957
#include "llvm/IR/ValueHandle.h"
58+
#include "llvm/Support/Allocator.h"
6059
#include "llvm/Support/Compiler.h"
6160

6261
namespace llvm {
@@ -79,7 +78,7 @@ struct PredicateConstraint {
7978

8079
// Base class for all predicate information we provide.
8180
// All of our predicate information has at least a comparison.
82-
class PredicateBase : public ilist_node<PredicateBase> {
81+
class PredicateBase {
8382
public:
8483
PredicateType Type;
8584
// The original operand before we renamed it.
@@ -96,7 +95,6 @@ class PredicateBase : public ilist_node<PredicateBase> {
9695
PredicateBase(const PredicateBase &) = delete;
9796
PredicateBase &operator=(const PredicateBase &) = delete;
9897
PredicateBase() = delete;
99-
virtual ~PredicateBase() = default;
10098
static bool classof(const PredicateBase *PB) {
10199
return PB->Type == PT_Assume || PB->Type == PT_Branch ||
102100
PB->Type == PT_Switch;
@@ -177,7 +175,8 @@ class PredicateSwitch : public PredicateWithEdge {
177175
/// accesses.
178176
class PredicateInfo {
179177
public:
180-
LLVM_ABI PredicateInfo(Function &, DominatorTree &, AssumptionCache &);
178+
LLVM_ABI PredicateInfo(Function &, DominatorTree &, AssumptionCache &,
179+
BumpPtrAllocator &);
181180
LLVM_ABI ~PredicateInfo();
182181

183182
LLVM_ABI void verifyPredicateInfo() const;
@@ -197,9 +196,6 @@ class PredicateInfo {
197196
private:
198197
Function &F;
199198

200-
// This owns the all the predicate infos in the function, placed or not.
201-
iplist<PredicateBase> AllInfos;
202-
203199
// This maps from copy operands to Predicate Info. Note that it does not own
204200
// the Predicate Info, they belong to the ValueInfo structs in the ValueInfos
205201
// vector.

llvm/lib/Transforms/Scalar/NewGVN.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,14 @@ class NewGVN {
505505
MemorySSAWalker *MSSAWalker = nullptr;
506506
AssumptionCache *AC = nullptr;
507507
const DataLayout &DL;
508-
std::unique_ptr<PredicateInfo> PredInfo;
509508

510509
// These are the only two things the create* functions should have
511510
// side-effects on due to allocating memory.
512511
mutable BumpPtrAllocator ExpressionAllocator;
513512
mutable ArrayRecycler<Value *> ArgRecycler;
514513
mutable TarjanSCC SCCFinder;
514+
515+
std::unique_ptr<PredicateInfo> PredInfo;
515516
const SimplifyQuery SQ;
516517

517518
// Number of function arguments, used by ranking
@@ -673,7 +674,9 @@ class NewGVN {
673674
TargetLibraryInfo *TLI, AliasAnalysis *AA, MemorySSA *MSSA,
674675
const DataLayout &DL)
675676
: F(F), DT(DT), TLI(TLI), AA(AA), MSSA(MSSA), AC(AC), DL(DL),
676-
PredInfo(std::make_unique<PredicateInfo>(F, *DT, *AC)),
677+
// Reuse ExpressionAllocator for PredicateInfo as well.
678+
PredInfo(
679+
std::make_unique<PredicateInfo>(F, *DT, *AC, ExpressionAllocator)),
677680
SQ(DL, TLI, DT, AC, /*CtxI=*/nullptr, /*UseInstrInfo=*/false,
678681
/*CanUseUndef=*/false) {}
679682

llvm/lib/Transforms/Utils/PredicateInfo.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ class PredicateInfoBuilder {
214214
// whether it returned a valid result.
215215
DenseMap<Value *, unsigned int> ValueInfoNums;
216216

217+
BumpPtrAllocator &Allocator;
218+
217219
ValueInfo &getOrCreateValueInfo(Value *);
218220
const ValueInfo &getValueInfo(Value *) const;
219221

@@ -242,8 +244,8 @@ class PredicateInfoBuilder {
242244

243245
public:
244246
PredicateInfoBuilder(PredicateInfo &PI, Function &F, DominatorTree &DT,
245-
AssumptionCache &AC)
246-
: PI(PI), F(F), DT(DT), AC(AC) {
247+
AssumptionCache &AC, BumpPtrAllocator &Allocator)
248+
: PI(PI), F(F), DT(DT), AC(AC), Allocator(Allocator) {
247249
// Push an empty operand info so that we can detect 0 as not finding one
248250
ValueInfos.resize(1);
249251
}
@@ -341,7 +343,6 @@ void PredicateInfoBuilder::addInfoFor(SmallVectorImpl<Value *> &OpsToRename,
341343
auto &OperandInfo = getOrCreateValueInfo(Op);
342344
if (OperandInfo.Infos.empty())
343345
OpsToRename.push_back(Op);
344-
PI.AllInfos.push_back(PB);
345346
OperandInfo.Infos.push_back(PB);
346347
}
347348

@@ -373,7 +374,7 @@ void PredicateInfoBuilder::processAssume(
373374

374375
for (Value *V : Values) {
375376
if (shouldRename(V)) {
376-
auto *PA = new PredicateAssume(V, II, Cond);
377+
auto *PA = new (Allocator) PredicateAssume(V, II, Cond);
377378
addInfoFor(OpsToRename, V, PA);
378379
}
379380
}
@@ -419,8 +420,8 @@ void PredicateInfoBuilder::processBranch(
419420

420421
for (Value *V : Values) {
421422
if (shouldRename(V)) {
422-
PredicateBase *PB =
423-
new PredicateBranch(V, BranchBB, Succ, Cond, TakenEdge);
423+
PredicateBase *PB = new (Allocator)
424+
PredicateBranch(V, BranchBB, Succ, Cond, TakenEdge);
424425
addInfoFor(OpsToRename, V, PB);
425426
}
426427
}
@@ -445,7 +446,7 @@ void PredicateInfoBuilder::processSwitch(
445446
for (auto C : SI->cases()) {
446447
BasicBlock *TargetBlock = C.getCaseSuccessor();
447448
if (SwitchEdges.lookup(TargetBlock) == 1) {
448-
PredicateSwitch *PS = new PredicateSwitch(
449+
PredicateSwitch *PS = new (Allocator) PredicateSwitch(
449450
Op, SI->getParent(), TargetBlock, C.getCaseValue(), SI);
450451
addInfoFor(OpsToRename, Op, PS);
451452
}
@@ -704,9 +705,9 @@ PredicateInfoBuilder::getValueInfo(Value *Operand) const {
704705
}
705706

706707
PredicateInfo::PredicateInfo(Function &F, DominatorTree &DT,
707-
AssumptionCache &AC)
708+
AssumptionCache &AC, BumpPtrAllocator &Allocator)
708709
: F(F) {
709-
PredicateInfoBuilder Builder(*this, F, DT, AC);
710+
PredicateInfoBuilder Builder(*this, F, DT, AC, Allocator);
710711
Builder.buildPredicateInfo();
711712
}
712713

@@ -797,7 +798,8 @@ PreservedAnalyses PredicateInfoPrinterPass::run(Function &F,
797798
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
798799
auto &AC = AM.getResult<AssumptionAnalysis>(F);
799800
OS << "PredicateInfo for function: " << F.getName() << "\n";
800-
auto PredInfo = std::make_unique<PredicateInfo>(F, DT, AC);
801+
BumpPtrAllocator Allocator;
802+
auto PredInfo = std::make_unique<PredicateInfo>(F, DT, AC, Allocator);
801803
PredInfo->print(OS);
802804

803805
replaceCreatedSSACopys(*PredInfo, F);
@@ -859,7 +861,8 @@ PreservedAnalyses PredicateInfoVerifierPass::run(Function &F,
859861
FunctionAnalysisManager &AM) {
860862
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
861863
auto &AC = AM.getResult<AssumptionAnalysis>(F);
862-
std::make_unique<PredicateInfo>(F, DT, AC)->verifyPredicateInfo();
864+
BumpPtrAllocator Allocator;
865+
std::make_unique<PredicateInfo>(F, DT, AC, Allocator)->verifyPredicateInfo();
863866

864867
return PreservedAnalyses::all();
865868
}

llvm/lib/Transforms/Utils/SCCPSolver.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,8 @@ class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
490490

491491
LLVMContext &Ctx;
492492

493+
BumpPtrAllocator PredicateInfoAllocator;
494+
493495
private:
494496
ConstantInt *getConstantInt(const ValueLatticeElement &IV, Type *Ty) const {
495497
return dyn_cast_or_null<ConstantInt>(getConstant(IV, Ty));
@@ -761,7 +763,8 @@ class SCCPInstVisitor : public InstVisitor<SCCPInstVisitor> {
761763

762764
public:
763765
void addPredicateInfo(Function &F, DominatorTree &DT, AssumptionCache &AC) {
764-
FnPredicateInfo.insert({&F, std::make_unique<PredicateInfo>(F, DT, AC)});
766+
FnPredicateInfo.insert({&F, std::make_unique<PredicateInfo>(
767+
F, DT, AC, PredicateInfoAllocator)});
765768
}
766769

767770
void removeSSACopies(Function &F) {

0 commit comments

Comments
 (0)