[IR] Add BasicBlock::isEntryBlock() (NFC)

This is a recurring and somewhat awkward pattern. Add a helper
method for it.
This commit is contained in:
Nikita Popov 2021-05-15 12:38:27 +02:00
parent eae445f65d
commit fb9ed1979a
15 changed files with 27 additions and 23 deletions

View File

@ -480,6 +480,10 @@ public:
/// Return true if it is legal to hoist instructions into this block. /// Return true if it is legal to hoist instructions into this block.
bool isLegalToHoistInto() const; bool isLegalToHoistInto() const;
/// Return true if this is the entry block of the containing function.
/// This method can only be used on blocks that have a parent function.
bool isEntryBlock() const;
Optional<uint64_t> getIrrLoopHeaderWeight() const; Optional<uint64_t> getIrrLoopHeaderWeight() const;
/// Returns true if the Order field of child Instructions is valid. /// Returns true if the Order field of child Instructions is valid.

View File

@ -248,7 +248,7 @@ bool llvm::isPotentiallyReachable(
// Can't be in a loop if it's the entry block -- the entry block may not // Can't be in a loop if it's the entry block -- the entry block may not
// have predecessors. // have predecessors.
if (BB == &BB->getParent()->getEntryBlock()) if (BB->isEntryBlock())
return false; return false;
// Otherwise, continue doing the normal per-BB CFG walk. // Otherwise, continue doing the normal per-BB CFG walk.
@ -267,10 +267,10 @@ bool llvm::isPotentiallyReachable(
!DT->isReachableFromEntry(B->getParent())) !DT->isReachableFromEntry(B->getParent()))
return false; return false;
if (!ExclusionSet || ExclusionSet->empty()) { if (!ExclusionSet || ExclusionSet->empty()) {
if (A->getParent() == &A->getParent()->getParent()->getEntryBlock() && if (A->getParent()->isEntryBlock() &&
DT->isReachableFromEntry(B->getParent())) DT->isReachableFromEntry(B->getParent()))
return true; return true;
if (B->getParent() == &A->getParent()->getParent()->getEntryBlock() && if (B->getParent()->isEntryBlock() &&
DT->isReachableFromEntry(A->getParent())) DT->isReachableFromEntry(A->getParent()))
return false; return false;
} }

View File

@ -134,8 +134,7 @@ namespace {
// //
// (1) BB is an entry block or have no successors. // (1) BB is an entry block or have no successors.
// (2) There's no path coming back through BB successors. // (2) There's no path coming back through BB successors.
if (BB == &BB->getParent()->getEntryBlock() || if (BB->isEntryBlock() || !BB->getTerminator()->getNumSuccessors())
!BB->getTerminator()->getNumSuccessors())
return true; return true;
SmallVector<BasicBlock*, 32> Worklist; SmallVector<BasicBlock*, 32> Worklist;

View File

@ -224,8 +224,8 @@ static bool valueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
// Otherwise, if the instruction is in the entry block and is not an invoke, // Otherwise, if the instruction is in the entry block and is not an invoke,
// then it obviously dominates all phi nodes. // then it obviously dominates all phi nodes.
if (I->getParent() == &I->getFunction()->getEntryBlock() && if (I->getParent()->isEntryBlock() && !isa<InvokeInst>(I) &&
!isa<InvokeInst>(I) && !isa<CallBrInst>(I)) !isa<CallBrInst>(I))
return true; return true;
return false; return false;

View File

@ -673,7 +673,7 @@ Optional<ValueLatticeElement> LazyValueInfoImpl::solveBlockValueNonLocal(
// If this is the entry block, we must be asking about an argument. The // If this is the entry block, we must be asking about an argument. The
// value is overdefined. // value is overdefined.
if (BB == &BB->getParent()->getEntryBlock()) { if (BB->isEntryBlock()) {
assert(isa<Argument>(Val) && "Unknown live-in to the entry block"); assert(isa<Argument>(Val) && "Unknown live-in to the entry block");
return ValueLatticeElement::getOverdefined(); return ValueLatticeElement::getOverdefined();
} }

View File

@ -2553,9 +2553,8 @@ bool upward_defs_iterator::IsGuaranteedLoopInvariant(Value *Ptr) const {
Ptr = Ptr->stripPointerCasts(); Ptr = Ptr->stripPointerCasts();
if (auto *I = dyn_cast<Instruction>(Ptr)) { if (auto *I = dyn_cast<Instruction>(Ptr)) {
if (I->getParent() == &I->getFunction()->getEntryBlock()) { if (I->getParent()->isEntryBlock())
return true; return true;
}
} }
if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) { if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
return IsGuaranteedLoopInvariantBase(GEP->getPointerOperand()) && return IsGuaranteedLoopInvariantBase(GEP->getPointerOperand()) &&

View File

@ -2072,7 +2072,7 @@ bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
// If this is an argument, we can export it if the BB is the entry block or // If this is an argument, we can export it if the BB is the entry block or
// if it is already exported. // if it is already exported.
if (isa<Argument>(V)) { if (isa<Argument>(V)) {
if (FromBB == &FromBB->getParent()->getEntryBlock()) if (FromBB->isEntryBlock())
return true; return true;
// Otherwise, can only export this if it is already exported. // Otherwise, can only export this if it is already exported.

View File

@ -3858,7 +3858,7 @@ void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
/// printBasicBlock - This member is called for each basic block in a method. /// printBasicBlock - This member is called for each basic block in a method.
void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
assert(BB && BB->getParent() && "block without parent!"); assert(BB && BB->getParent() && "block without parent!");
bool IsEntryBlock = BB == &BB->getParent()->getEntryBlock(); bool IsEntryBlock = BB->isEntryBlock();
if (BB->hasName()) { // Print out the label if it exists... if (BB->hasName()) { // Print out the label if it exists...
Out << "\n"; Out << "\n";
PrintLLVMName(Out, BB->getName(), LabelPrefix); PrintLLVMName(Out, BB->getName(), LabelPrefix);

View File

@ -372,6 +372,12 @@ bool BasicBlock::isLegalToHoistInto() const {
return !Term->isExceptionalTerminator(); return !Term->isExceptionalTerminator();
} }
bool BasicBlock::isEntryBlock() const {
const Function *F = getParent();
assert(F && "Block must have a parent function to use this API");
return this == &F->getEntryBlock();
}
BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName, BasicBlock *BasicBlock::splitBasicBlock(iterator I, const Twine &BBName,
bool Before) { bool Before) {
if (Before) if (Before)

View File

@ -970,7 +970,7 @@ static void printBBName(raw_ostream &out, const BasicBlock *BB) {
return; return;
} }
if (BB == &BB->getParent()->getEntryBlock()) { if (BB->isEntryBlock()) {
out << "entry" out << "entry"
<< "<" << BB << ">"; << "<" << BB << ">";
return; return;

View File

@ -1270,9 +1270,8 @@ struct DSEState {
Ptr = Ptr->stripPointerCasts(); Ptr = Ptr->stripPointerCasts();
if (auto *I = dyn_cast<Instruction>(Ptr)) { if (auto *I = dyn_cast<Instruction>(Ptr)) {
if (I->getParent() == &I->getFunction()->getEntryBlock()) { if (I->getParent()->isEntryBlock())
return true; return true;
}
} }
if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) { if (auto *GEP = dyn_cast<GEPOperator>(Ptr)) {
return IsGuaranteedLoopInvariantBase(GEP->getPointerOperand()) && return IsGuaranteedLoopInvariantBase(GEP->getPointerOperand()) &&

View File

@ -114,7 +114,7 @@ static bool isLoopNeverExecuted(Loop *L) {
// predecessor. // predecessor.
assert(Preheader && "Needs preheader!"); assert(Preheader && "Needs preheader!");
if (Preheader == &Preheader->getParent()->getEntryBlock()) if (Preheader->isEntryBlock())
return false; return false;
// All predecessors of the preheader should have a constant conditional // All predecessors of the preheader should have a constant conditional
// branch, with the loop's preheader as not-taken. // branch, with the loop's preheader as not-taken.

View File

@ -732,8 +732,7 @@ bool BCECmpChain::simplify(const TargetLibraryInfo &TLI, AliasAnalysis &AA,
// If the old cmp chain was the function entry, we need to update the function // If the old cmp chain was the function entry, we need to update the function
// entry. // entry.
const bool ChainEntryIsFnEntry = const bool ChainEntryIsFnEntry = EntryBlock_->isEntryBlock();
(EntryBlock_ == &EntryBlock_->getParent()->getEntryBlock());
if (ChainEntryIsFnEntry && DTU.hasDomTree()) { if (ChainEntryIsFnEntry && DTU.hasDomTree()) {
LLVM_DEBUG(dbgs() << "Changing function entry from " LLVM_DEBUG(dbgs() << "Changing function entry from "
<< EntryBlock_->getName() << " to " << EntryBlock_->getName() << " to "

View File

@ -887,7 +887,7 @@ static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,
if (DTU) { if (DTU) {
// Recalculation of DomTree is needed when updating a forward DomTree and // Recalculation of DomTree is needed when updating a forward DomTree and
// the Entry BB is replaced. // the Entry BB is replaced.
if (NewBB == &NewBB->getParent()->getEntryBlock() && DTU->hasDomTree()) { if (NewBB->isEntryBlock() && DTU->hasDomTree()) {
// The entry block was removed and there is no external interface for // The entry block was removed and there is no external interface for
// the dominator tree to be notified of this change. In this corner-case // the dominator tree to be notified of this change. In this corner-case
// we recalculate the entire tree. // we recalculate the entire tree.
@ -906,7 +906,7 @@ static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,
} }
} else if (DT) { } else if (DT) {
if (OldBB == DT->getRootNode()->getBlock()) { if (OldBB == DT->getRootNode()->getBlock()) {
assert(NewBB == &NewBB->getParent()->getEntryBlock()); assert(NewBB->isEntryBlock());
DT->setNewRoot(NewBB); DT->setNewRoot(NewBB);
} else { } else {
// Split block expects NewBB to have a non-empty set of predecessors. // Split block expects NewBB to have a non-empty set of predecessors.

View File

@ -738,9 +738,7 @@ void llvm::MergeBasicBlockIntoOnlyPred(BasicBlock *DestBB,
BasicBlock *PredBB = DestBB->getSinglePredecessor(); BasicBlock *PredBB = DestBB->getSinglePredecessor();
assert(PredBB && "Block doesn't have a single predecessor!"); assert(PredBB && "Block doesn't have a single predecessor!");
bool ReplaceEntryBB = false; bool ReplaceEntryBB = PredBB->isEntryBlock();
if (PredBB == &DestBB->getParent()->getEntryBlock())
ReplaceEntryBB = true;
// DTU updates: Collect all the edges that enter // DTU updates: Collect all the edges that enter
// PredBB. These dominator edges will be redirected to DestBB. // PredBB. These dominator edges will be redirected to DestBB.