Skip to content

Commit

Permalink
[BBUtils][NFC] Delete SplitBlockAndInsertIfThen with DT.
Browse files Browse the repository at this point in the history
The method is marked for deprecation. Delete the method and move all of
its consumers to use the DomTreeUpdater version.

Reviewed By: foad

Differential Revision: https://reviews.llvm.org/D149428
  • Loading branch information
caojoshua committed May 24, 2023
1 parent 597dd1f commit 0c316f0
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 89 deletions.
31 changes: 1 addition & 30 deletions llvm/include/llvm/Transforms/Utils/BasicBlockUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,36 +416,7 @@ ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
/// UnreachableInst, otherwise it branches to Tail.
/// Returns the NewBasicBlock's terminator.
///
/// Updates DT and LI if given.
///
/// FIXME: deprecated, switch to the DomTreeUpdater-based one.
Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
bool Unreachable, MDNode *BranchWeights,
DominatorTree *DT,
LoopInfo *LI = nullptr,
BasicBlock *ThenBlock = nullptr);

/// Split the containing block at the specified instruction - everything before
/// SplitBefore stays in the old basic block, and the rest of the instructions
/// in the BB are moved to a new block. The two blocks are connected by a
/// conditional branch (with value of Cmp being the condition).
/// Before:
/// Head
/// SplitBefore
/// Tail
/// After:
/// Head
/// if (Cond)
/// ThenBlock
/// SplitBefore
/// Tail
///
/// If \p ThenBlock is not specified, a new block will be created for it.
/// If \p Unreachable is true, the newly created block will end with
/// UnreachableInst, otherwise it branches to Tail.
/// Returns the NewBasicBlock's terminator.
///
/// Updates DT and LI if given.
/// Updates DTU and LI if given.
Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
bool Unreachable,
MDNode *BranchWeights = nullptr,
Expand Down
21 changes: 12 additions & 9 deletions llvm/lib/Target/AMDGPU/AMDGPUAtomicOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "AMDGPU.h"
#include "GCNSubtarget.h"
#include "llvm/Analysis/DomTreeUpdater.h"
#include "llvm/Analysis/UniformityAnalysis.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/IR/IRBuilder.h"
Expand Down Expand Up @@ -59,7 +60,7 @@ class AMDGPUAtomicOptimizerImpl
SmallVector<ReplacementInfo, 8> ToReplace;
const UniformityInfo *UA;
const DataLayout *DL;
DominatorTree *DT;
DomTreeUpdater &DTU;
const GCNSubtarget *ST;
bool IsPixelShader;

Expand All @@ -76,9 +77,9 @@ class AMDGPUAtomicOptimizerImpl
AMDGPUAtomicOptimizerImpl() = delete;

AMDGPUAtomicOptimizerImpl(const UniformityInfo *UA, const DataLayout *DL,
DominatorTree *DT, const GCNSubtarget *ST,
DomTreeUpdater &DTU, const GCNSubtarget *ST,
bool IsPixelShader)
: UA(UA), DL(DL), DT(DT), ST(ST), IsPixelShader(IsPixelShader) {}
: UA(UA), DL(DL), DTU(DTU), ST(ST), IsPixelShader(IsPixelShader) {}

bool run(Function &F);

Expand All @@ -103,15 +104,16 @@ bool AMDGPUAtomicOptimizer::runOnFunction(Function &F) {

DominatorTreeWrapperPass *const DTW =
getAnalysisIfAvailable<DominatorTreeWrapperPass>();
DominatorTree *DT = DTW ? &DTW->getDomTree() : nullptr;
DomTreeUpdater DTU(DTW ? &DTW->getDomTree() : nullptr,
DomTreeUpdater::UpdateStrategy::Lazy);

const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
const TargetMachine &TM = TPC.getTM<TargetMachine>();
const GCNSubtarget *ST = &TM.getSubtarget<GCNSubtarget>(F);

bool IsPixelShader = F.getCallingConv() == CallingConv::AMDGPU_PS;

return AMDGPUAtomicOptimizerImpl(UA, DL, DT, ST, IsPixelShader).run(F);
return AMDGPUAtomicOptimizerImpl(UA, DL, DTU, ST, IsPixelShader).run(F);
}

PreservedAnalyses AMDGPUAtomicOptimizerPass::run(Function &F,
Expand All @@ -120,12 +122,13 @@ PreservedAnalyses AMDGPUAtomicOptimizerPass::run(Function &F,
const auto *UA = &AM.getResult<UniformityInfoAnalysis>(F);
const DataLayout *DL = &F.getParent()->getDataLayout();

DominatorTree *DT = &AM.getResult<DominatorTreeAnalysis>(F);
DomTreeUpdater DTU(&AM.getResult<DominatorTreeAnalysis>(F),
DomTreeUpdater::UpdateStrategy::Lazy);
const GCNSubtarget *ST = &TM.getSubtarget<GCNSubtarget>(F);

bool IsPixelShader = F.getCallingConv() == CallingConv::AMDGPU_PS;

return AMDGPUAtomicOptimizerImpl(UA, DL, DT, ST, IsPixelShader).run(F)
return AMDGPUAtomicOptimizerImpl(UA, DL, DTU, ST, IsPixelShader).run(F)
? PreservedAnalyses::none()
: PreservedAnalyses::all();
}
Expand Down Expand Up @@ -519,7 +522,7 @@ void AMDGPUAtomicOptimizerImpl::optimizeAtomic(Instruction &I,

Value *const Cond = B.CreateIntrinsic(Intrinsic::amdgcn_ps_live, {}, {});
Instruction *const NonHelperTerminator =
SplitBlockAndInsertIfThen(Cond, &I, false, nullptr, DT, nullptr);
SplitBlockAndInsertIfThen(Cond, &I, false, nullptr, &DTU, nullptr);

// Record I's new position as the exit block.
PixelExitBB = I.getParent();
Expand Down Expand Up @@ -648,7 +651,7 @@ void AMDGPUAtomicOptimizerImpl::optimizeAtomic(Instruction &I,
// entry --> single_lane -\
// \------------------> exit
Instruction *const SingleLaneTerminator =
SplitBlockAndInsertIfThen(Cond, &I, false, nullptr, DT, nullptr);
SplitBlockAndInsertIfThen(Cond, &I, false, nullptr, &DTU, nullptr);

// Move the IR builder into single_lane next.
B.SetInsertPoint(SingleLaneTerminator);
Expand Down
4 changes: 3 additions & 1 deletion llvm/lib/Transforms/Instrumentation/DataFlowSanitizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/iterator.h"
#include "llvm/Analysis/DomTreeUpdater.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/ValueTracking.h"
Expand Down Expand Up @@ -2524,8 +2525,9 @@ void DFSanFunction::storeOrigin(Instruction *Pos, Value *Addr, uint64_t Size,
ConstantInt::get(DFS.IntptrTy, Size), Origin});
} else {
Value *Cmp = convertToBool(CollapsedShadow, IRB, "_dfscmp");
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
Instruction *CheckTerm = SplitBlockAndInsertIfThen(
Cmp, &*IRB.GetInsertPoint(), false, DFS.OriginStoreWeights, &DT);
Cmp, &*IRB.GetInsertPoint(), false, DFS.OriginStoreWeights, &DTU);
IRBuilder<> IRBNew(CheckTerm);
paintOrigin(IRBNew, updateOrigin(Origin, IRBNew), StoreOriginAddr, Size,
OriginAlignment);
Expand Down
47 changes: 6 additions & 41 deletions llvm/lib/Transforms/Utils/BasicBlockUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1471,11 +1471,12 @@ ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
return cast<ReturnInst>(NewRet);
}

static Instruction *
SplitBlockAndInsertIfThenImpl(Value *Cond, Instruction *SplitBefore,
bool Unreachable, MDNode *BranchWeights,
DomTreeUpdater *DTU, DominatorTree *DT,
LoopInfo *LI, BasicBlock *ThenBlock) {
Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,
Instruction *SplitBefore,
bool Unreachable,
MDNode *BranchWeights,
DomTreeUpdater *DTU, LoopInfo *LI,
BasicBlock *ThenBlock) {
SmallVector<DominatorTree::UpdateType, 8> Updates;
BasicBlock *Head = SplitBefore->getParent();
BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
Expand Down Expand Up @@ -1514,21 +1515,6 @@ SplitBlockAndInsertIfThenImpl(Value *Cond, Instruction *SplitBefore,

if (DTU)
DTU->applyUpdates(Updates);
else if (DT) {
if (DomTreeNode *OldNode = DT->getNode(Head)) {
std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());

DomTreeNode *NewNode = DT->addNewBlock(Tail, Head);
for (DomTreeNode *Child : Children)
DT->changeImmediateDominator(Child, NewNode);

// Head dominates ThenBlock.
if (CreateThenBlock)
DT->addNewBlock(ThenBlock, Head);
else
DT->changeImmediateDominator(ThenBlock, Head);
}
}

if (LI) {
if (Loop *L = LI->getLoopFor(Head)) {
Expand All @@ -1540,27 +1526,6 @@ SplitBlockAndInsertIfThenImpl(Value *Cond, Instruction *SplitBefore,
return CheckTerm;
}

Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,
Instruction *SplitBefore,
bool Unreachable,
MDNode *BranchWeights,
DominatorTree *DT, LoopInfo *LI,
BasicBlock *ThenBlock) {
return SplitBlockAndInsertIfThenImpl(Cond, SplitBefore, Unreachable,
BranchWeights,
/*DTU=*/nullptr, DT, LI, ThenBlock);
}
Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,
Instruction *SplitBefore,
bool Unreachable,
MDNode *BranchWeights,
DomTreeUpdater *DTU, LoopInfo *LI,
BasicBlock *ThenBlock) {
return SplitBlockAndInsertIfThenImpl(Cond, SplitBefore, Unreachable,
BranchWeights, DTU, /*DT=*/nullptr, LI,
ThenBlock);
}

void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
Instruction **ThenTerm,
Instruction **ElseTerm,
Expand Down
17 changes: 10 additions & 7 deletions llvm/lib/Transforms/Utils/LibCallsShrinkWrap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/DomTreeUpdater.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/IR/Constants.h"
Expand All @@ -51,8 +52,8 @@ STATISTIC(NumWrappedTwoCond, "Number of Two-Condition Wrappers Inserted");
namespace {
class LibCallsShrinkWrap : public InstVisitor<LibCallsShrinkWrap> {
public:
LibCallsShrinkWrap(const TargetLibraryInfo &TLI, DominatorTree *DT)
: TLI(TLI), DT(DT){};
LibCallsShrinkWrap(const TargetLibraryInfo &TLI, DomTreeUpdater &DTU)
: TLI(TLI), DTU(DTU){};
void visitCallInst(CallInst &CI) { checkCandidate(CI); }
bool perform() {
bool Changed = false;
Expand Down Expand Up @@ -105,7 +106,7 @@ class LibCallsShrinkWrap : public InstVisitor<LibCallsShrinkWrap> {
}

const TargetLibraryInfo &TLI;
DominatorTree *DT;
DomTreeUpdater &DTU;
SmallVector<CallInst *, 16> WorkList;
};
} // end anonymous namespace
Expand Down Expand Up @@ -466,7 +467,7 @@ void LibCallsShrinkWrap::shrinkWrapCI(CallInst *CI, Value *Cond) {
MDBuilder(CI->getContext()).createBranchWeights(1, 2000);

Instruction *NewInst =
SplitBlockAndInsertIfThen(Cond, CI, false, BranchWeights, DT);
SplitBlockAndInsertIfThen(Cond, CI, false, BranchWeights, &DTU);
BasicBlock *CallBB = NewInst->getParent();
CallBB->setName("cdce.call");
BasicBlock *SuccBB = CallBB->getSingleSuccessor();
Expand Down Expand Up @@ -496,12 +497,14 @@ static bool runImpl(Function &F, const TargetLibraryInfo &TLI,
DominatorTree *DT) {
if (F.hasFnAttribute(Attribute::OptimizeForSize))
return false;
LibCallsShrinkWrap CCDCE(TLI, DT);
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
LibCallsShrinkWrap CCDCE(TLI, DTU);
CCDCE.visit(F);
bool Changed = CCDCE.perform();

// Verify the dominator after we've updated it locally.
assert(!DT || DT->verify(DominatorTree::VerificationLevel::Fast));
// Verify the dominator after we've updated it locally.
assert(!DT ||
DTU.getDomTree().verify(DominatorTree::VerificationLevel::Fast));
return Changed;
}

Expand Down
4 changes: 3 additions & 1 deletion polly/lib/CodeGen/BlockGenerators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "polly/Support/ISLTools.h"
#include "polly/Support/ScopHelper.h"
#include "polly/Support/VirtualInstruction.h"
#include "llvm/Analysis/DomTreeUpdater.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/RegionInfo.h"
#include "llvm/Analysis/ScalarEvolution.h"
Expand Down Expand Up @@ -626,8 +627,9 @@ void BlockGenerator::generateConditionalExecution(
StringRef BlockName = HeadBlock->getName();

// Generate the conditional block.
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
SplitBlockAndInsertIfThen(Cond, &*Builder.GetInsertPoint(), false, nullptr,
&DT, &LI);
&DTU, &LI);
BranchInst *Branch = cast<BranchInst>(HeadBlock->getTerminator());
BasicBlock *ThenBlock = Branch->getSuccessor(0);
BasicBlock *TailBlock = Branch->getSuccessor(1);
Expand Down

0 comments on commit 0c316f0

Please sign in to comment.