[llvm] r298827 - [IR] Share implementation of pairs of const and non-const methods in BasicBlock using the const version instead of the non-const version
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 26 19:38:17 PDT 2017
Author: ctopper
Date: Sun Mar 26 21:38:17 2017
New Revision: 298827
URL: http://llvm.org/viewvc/llvm-project?rev=298827&view=rev
Log:
[IR] Share implementation of pairs of const and non-const methods in BasicBlock using the const version instead of the non-const version
Summary:
During post-commit review of a previous change I made it was pointed out that const casting 'this' is technically a bad practice. This patch re-implements all of the methods in BasicBlock that do this to use the const BasicBlock version and const_cast the return value instead.
I think there are still many other classes that do similar things. I may look at more in the future.
Reviewers: dblaikie
Reviewed By: dblaikie
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D31377
Modified:
llvm/trunk/include/llvm/IR/BasicBlock.h
llvm/trunk/lib/IR/BasicBlock.cpp
Modified: llvm/trunk/include/llvm/IR/BasicBlock.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/BasicBlock.h?rev=298827&r1=298826&r2=298827&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/BasicBlock.h (original)
+++ llvm/trunk/include/llvm/IR/BasicBlock.h Sun Mar 26 21:38:17 2017
@@ -104,32 +104,36 @@ public:
/// or nullptr it the function does not have a module.
///
/// Note: this is undefined behavior if the block does not have a parent.
- Module *getModule();
- const Module *getModule() const {
- return const_cast<BasicBlock *>(this)->getModule();
+ const Module *getModule() const;
+ Module *getModule() {
+ return const_cast<Module *>(
+ static_cast<const BasicBlock *>(this)->getModule());
}
/// \brief Returns the terminator instruction if the block is well formed or
/// null if the block is not well formed.
- TerminatorInst *getTerminator() LLVM_READONLY;
- const TerminatorInst *getTerminator() const {
- return const_cast<BasicBlock *>(this)->getTerminator();
+ const TerminatorInst *getTerminator() const LLVM_READONLY;
+ TerminatorInst *getTerminator() {
+ return const_cast<TerminatorInst *>(
+ static_cast<const BasicBlock *>(this)->getTerminator());
}
/// \brief Returns the call instruction calling @llvm.experimental.deoptimize
/// prior to the terminating return instruction of this basic block, if such a
/// call is present. Otherwise, returns null.
- CallInst *getTerminatingDeoptimizeCall();
- const CallInst *getTerminatingDeoptimizeCall() const {
- return const_cast<BasicBlock *>(this)->getTerminatingDeoptimizeCall();
+ const CallInst *getTerminatingDeoptimizeCall() const;
+ CallInst *getTerminatingDeoptimizeCall() {
+ return const_cast<CallInst *>(
+ static_cast<const BasicBlock *>(this)->getTerminatingDeoptimizeCall());
}
/// \brief Returns the call instruction marked 'musttail' prior to the
/// terminating return instruction of this basic block, if such a call is
/// present. Otherwise, returns null.
- CallInst *getTerminatingMustTailCall();
- const CallInst *getTerminatingMustTailCall() const {
- return const_cast<BasicBlock *>(this)->getTerminatingMustTailCall();
+ const CallInst *getTerminatingMustTailCall() const;
+ CallInst *getTerminatingMustTailCall() {
+ return const_cast<CallInst *>(
+ static_cast<const BasicBlock *>(this)->getTerminatingMustTailCall());
}
/// \brief Returns a pointer to the first instruction in this block that is
@@ -138,32 +142,36 @@ public:
/// When adding instructions to the beginning of the basic block, they should
/// be added before the returned value, not before the first instruction,
/// which might be PHI. Returns 0 is there's no non-PHI instruction.
- Instruction* getFirstNonPHI();
- const Instruction* getFirstNonPHI() const {
- return const_cast<BasicBlock*>(this)->getFirstNonPHI();
+ const Instruction* getFirstNonPHI() const;
+ Instruction* getFirstNonPHI() {
+ return const_cast<Instruction *>(
+ static_cast<const BasicBlock *>(this)->getFirstNonPHI());
}
/// \brief Returns a pointer to the first instruction in this block that is not
/// a PHINode or a debug intrinsic.
- Instruction* getFirstNonPHIOrDbg();
- const Instruction* getFirstNonPHIOrDbg() const {
- return const_cast<BasicBlock*>(this)->getFirstNonPHIOrDbg();
+ const Instruction* getFirstNonPHIOrDbg() const;
+ Instruction* getFirstNonPHIOrDbg() {
+ return const_cast<Instruction *>(
+ static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbg());
}
/// \brief Returns a pointer to the first instruction in this block that is not
/// a PHINode, a debug intrinsic, or a lifetime intrinsic.
- Instruction* getFirstNonPHIOrDbgOrLifetime();
- const Instruction* getFirstNonPHIOrDbgOrLifetime() const {
- return const_cast<BasicBlock*>(this)->getFirstNonPHIOrDbgOrLifetime();
+ const Instruction* getFirstNonPHIOrDbgOrLifetime() const;
+ Instruction* getFirstNonPHIOrDbgOrLifetime() {
+ return const_cast<Instruction *>(
+ static_cast<const BasicBlock *>(this)->getFirstNonPHIOrDbgOrLifetime());
}
/// \brief Returns an iterator to the first instruction in this block that is
/// suitable for inserting a non-PHI instruction.
///
/// In particular, it skips all PHIs and LandingPad instructions.
- iterator getFirstInsertionPt();
- const_iterator getFirstInsertionPt() const {
- return const_cast<BasicBlock*>(this)->getFirstInsertionPt();
+ const_iterator getFirstInsertionPt() const;
+ iterator getFirstInsertionPt() {
+ return static_cast<const BasicBlock *>(this)
+ ->getFirstInsertionPt().getNonConst();
}
/// \brief Unlink 'this' from the containing function, but do not delete it.
@@ -192,9 +200,10 @@ public:
/// \brief Return the predecessor of this block if it has a single predecessor
/// block. Otherwise return a null pointer.
- BasicBlock *getSinglePredecessor();
- const BasicBlock *getSinglePredecessor() const {
- return const_cast<BasicBlock*>(this)->getSinglePredecessor();
+ const BasicBlock *getSinglePredecessor() const;
+ BasicBlock *getSinglePredecessor() {
+ return const_cast<BasicBlock *>(
+ static_cast<const BasicBlock *>(this)->getSinglePredecessor());
}
/// \brief Return the predecessor of this block if it has a unique predecessor
@@ -203,27 +212,30 @@ public:
/// Note that unique predecessor doesn't mean single edge, there can be
/// multiple edges from the unique predecessor to this block (for example a
/// switch statement with multiple cases having the same destination).
- BasicBlock *getUniquePredecessor();
- const BasicBlock *getUniquePredecessor() const {
- return const_cast<BasicBlock*>(this)->getUniquePredecessor();
+ const BasicBlock *getUniquePredecessor() const;
+ BasicBlock *getUniquePredecessor() {
+ return const_cast<BasicBlock *>(
+ static_cast<const BasicBlock *>(this)->getUniquePredecessor());
}
/// \brief Return the successor of this block if it has a single successor.
/// Otherwise return a null pointer.
///
/// This method is analogous to getSinglePredecessor above.
- BasicBlock *getSingleSuccessor();
- const BasicBlock *getSingleSuccessor() const {
- return const_cast<BasicBlock*>(this)->getSingleSuccessor();
+ const BasicBlock *getSingleSuccessor() const;
+ BasicBlock *getSingleSuccessor() {
+ return const_cast<BasicBlock *>(
+ static_cast<const BasicBlock *>(this)->getSingleSuccessor());
}
/// \brief Return the successor of this block if it has a unique successor.
/// Otherwise return a null pointer.
///
/// This method is analogous to getUniquePredecessor above.
- BasicBlock *getUniqueSuccessor();
- const BasicBlock *getUniqueSuccessor() const {
- return const_cast<BasicBlock*>(this)->getUniqueSuccessor();
+ const BasicBlock *getUniqueSuccessor() const;
+ BasicBlock *getUniqueSuccessor() {
+ return const_cast<BasicBlock *>(
+ static_cast<const BasicBlock *>(this)->getUniqueSuccessor());
}
//===--------------------------------------------------------------------===//
Modified: llvm/trunk/lib/IR/BasicBlock.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/BasicBlock.cpp?rev=298827&r1=298826&r2=298827&view=diff
==============================================================================
--- llvm/trunk/lib/IR/BasicBlock.cpp (original)
+++ llvm/trunk/lib/IR/BasicBlock.cpp Sun Mar 26 21:38:17 2017
@@ -113,23 +113,23 @@ void BasicBlock::moveAfter(BasicBlock *M
getIterator());
}
-Module *BasicBlock::getModule() {
+const Module *BasicBlock::getModule() const {
return getParent()->getParent();
}
-TerminatorInst *BasicBlock::getTerminator() {
+const TerminatorInst *BasicBlock::getTerminator() const {
if (InstList.empty()) return nullptr;
return dyn_cast<TerminatorInst>(&InstList.back());
}
-CallInst *BasicBlock::getTerminatingMustTailCall() {
+const CallInst *BasicBlock::getTerminatingMustTailCall() const {
if (InstList.empty())
return nullptr;
- ReturnInst *RI = dyn_cast<ReturnInst>(&InstList.back());
+ const ReturnInst *RI = dyn_cast<ReturnInst>(&InstList.back());
if (!RI || RI == &InstList.front())
return nullptr;
- Instruction *Prev = RI->getPrevNode();
+ const Instruction *Prev = RI->getPrevNode();
if (!Prev)
return nullptr;
@@ -153,7 +153,7 @@ CallInst *BasicBlock::getTerminatingMust
return nullptr;
}
-CallInst *BasicBlock::getTerminatingDeoptimizeCall() {
+const CallInst *BasicBlock::getTerminatingDeoptimizeCall() const {
if (InstList.empty())
return nullptr;
auto *RI = dyn_cast<ReturnInst>(&InstList.back());
@@ -168,22 +168,22 @@ CallInst *BasicBlock::getTerminatingDeop
return nullptr;
}
-Instruction* BasicBlock::getFirstNonPHI() {
- for (Instruction &I : *this)
+const Instruction* BasicBlock::getFirstNonPHI() const {
+ for (const Instruction &I : *this)
if (!isa<PHINode>(I))
return &I;
return nullptr;
}
-Instruction* BasicBlock::getFirstNonPHIOrDbg() {
- for (Instruction &I : *this)
+const Instruction* BasicBlock::getFirstNonPHIOrDbg() const {
+ for (const Instruction &I : *this)
if (!isa<PHINode>(I) && !isa<DbgInfoIntrinsic>(I))
return &I;
return nullptr;
}
-Instruction* BasicBlock::getFirstNonPHIOrDbgOrLifetime() {
- for (Instruction &I : *this) {
+const Instruction* BasicBlock::getFirstNonPHIOrDbgOrLifetime() const {
+ for (const Instruction &I : *this) {
if (isa<PHINode>(I) || isa<DbgInfoIntrinsic>(I))
continue;
@@ -197,12 +197,12 @@ Instruction* BasicBlock::getFirstNonPHIO
return nullptr;
}
-BasicBlock::iterator BasicBlock::getFirstInsertionPt() {
- Instruction *FirstNonPHI = getFirstNonPHI();
+BasicBlock::const_iterator BasicBlock::getFirstInsertionPt() const {
+ const Instruction *FirstNonPHI = getFirstNonPHI();
if (!FirstNonPHI)
return end();
- iterator InsertPt = FirstNonPHI->getIterator();
+ const_iterator InsertPt = FirstNonPHI->getIterator();
if (InsertPt->isEHPad()) ++InsertPt;
return InsertPt;
}
@@ -214,10 +214,10 @@ void BasicBlock::dropAllReferences() {
/// If this basic block has a single predecessor block,
/// return the block, otherwise return a null pointer.
-BasicBlock *BasicBlock::getSinglePredecessor() {
- pred_iterator PI = pred_begin(this), E = pred_end(this);
+const BasicBlock *BasicBlock::getSinglePredecessor() const {
+ const_pred_iterator PI = pred_begin(this), E = pred_end(this);
if (PI == E) return nullptr; // No preds.
- BasicBlock *ThePred = *PI;
+ const BasicBlock *ThePred = *PI;
++PI;
return (PI == E) ? ThePred : nullptr /*multiple preds*/;
}
@@ -227,10 +227,10 @@ BasicBlock *BasicBlock::getSinglePredece
/// Note that unique predecessor doesn't mean single edge, there can be
/// multiple edges from the unique predecessor to this block (for example
/// a switch statement with multiple cases having the same destination).
-BasicBlock *BasicBlock::getUniquePredecessor() {
- pred_iterator PI = pred_begin(this), E = pred_end(this);
+const BasicBlock *BasicBlock::getUniquePredecessor() const {
+ const_pred_iterator PI = pred_begin(this), E = pred_end(this);
if (PI == E) return nullptr; // No preds.
- BasicBlock *PredBB = *PI;
+ const BasicBlock *PredBB = *PI;
++PI;
for (;PI != E; ++PI) {
if (*PI != PredBB)
@@ -241,18 +241,18 @@ BasicBlock *BasicBlock::getUniquePredece
return PredBB;
}
-BasicBlock *BasicBlock::getSingleSuccessor() {
- succ_iterator SI = succ_begin(this), E = succ_end(this);
+const BasicBlock *BasicBlock::getSingleSuccessor() const {
+ succ_const_iterator SI = succ_begin(this), E = succ_end(this);
if (SI == E) return nullptr; // no successors
- BasicBlock *TheSucc = *SI;
+ const BasicBlock *TheSucc = *SI;
++SI;
return (SI == E) ? TheSucc : nullptr /* multiple successors */;
}
-BasicBlock *BasicBlock::getUniqueSuccessor() {
- succ_iterator SI = succ_begin(this), E = succ_end(this);
+const BasicBlock *BasicBlock::getUniqueSuccessor() const {
+ succ_const_iterator SI = succ_begin(this), E = succ_end(this);
if (SI == E) return nullptr; // No successors
- BasicBlock *SuccBB = *SI;
+ const BasicBlock *SuccBB = *SI;
++SI;
for (;SI != E; ++SI) {
if (*SI != SuccBB)
More information about the llvm-commits
mailing list