[llvm] r292753 - [IR] Use const_cast to reuse the const version of two BasicBlock methods that are duplicated for both const and non-const. NFC
David Blaikie via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 23 10:31:58 PST 2017
Because the code is in a non-const context & can depend on the invariants
of the class that 'getModule() const' is only adding const on to a
non-const entity in the first place. (ie: it knows that 'Parent' is a
pointer to non-const Function - and, perhaps a bit of a stretch, but knows
that getParent on a non-const Function and on a const Function return the
same pointer, etc)
But it can't know that the BasicBlock itself is not const - that's up to
the user.
On Mon, Jan 23, 2017 at 10:27 AM Craig Topper <craig.topper at gmail.com>
wrote:
> Why would const_casting the Module * result in your formulation not also
> be a problem?
>
> ~Craig
>
> On Mon, Jan 23, 2017 at 9:36 AM, David Blaikie <dblaikie at gmail.com> wrote:
>
>
>
> On Sat, Jan 21, 2017 at 11:04 PM Craig Topper via llvm-commits <
> llvm-commits at lists.llvm.org> wrote:
>
> Author: ctopper
> Date: Sun Jan 22 00:53:04 2017
> New Revision: 292753
>
> URL: http://llvm.org/viewvc/llvm-project?rev=292753&view=rev
> Log:
> [IR] Use const_cast to reuse the const version of two BasicBlock methods
> that are duplicated for both const and non-const. NFC
>
> Similar is already done for other methods in BasicBlock.
>
> 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=292753&r1=292752&r2=292753&view=diff
>
> ==============================================================================
> --- llvm/trunk/include/llvm/IR/BasicBlock.h (original)
> +++ llvm/trunk/include/llvm/IR/BasicBlock.h Sun Jan 22 00:53:04 2017
> @@ -104,13 +104,17 @@ public:
> /// or nullptr it the function does not have a module.
> ///
> /// Note: this is undefined behavior if the block does not have a
> parent.
> - const Module *getModule() const;
> Module *getModule();
> + const Module *getModule() const {
> + return const_cast<BasicBlock *>(this)->getModule();
>
>
> I think this is technically UB if the BasicBlock were actually const
> (though I suppose we don't have any of those)
>
> But the opposite formulation - implementing the non-const version in terms
> of the const one, doesn't have that problem:
>
> Module *getModule() {
> return const_cast<Module*>(implicit_cast<const
> BasicBlock*>(this)->getModule());
> }
>
> (I don't think we have an implicit_cast template - we could add one, but
> don't necessarily have to for this example/case)
>
>
> + }
>
> /// \brief Returns the terminator instruction if the block is well
> formed or
> /// null if the block is not well formed.
> TerminatorInst *getTerminator();
> - const TerminatorInst *getTerminator() const;
> + const TerminatorInst *getTerminator() const {
> + return const_cast<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
>
> Modified: llvm/trunk/lib/IR/BasicBlock.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/BasicBlock.cpp?rev=292753&r1=292752&r2=292753&view=diff
>
> ==============================================================================
> --- llvm/trunk/lib/IR/BasicBlock.cpp (original)
> +++ llvm/trunk/lib/IR/BasicBlock.cpp Sun Jan 22 00:53:04 2017
> @@ -113,10 +113,6 @@ void BasicBlock::moveAfter(BasicBlock *M
> getIterator());
> }
>
> -const Module *BasicBlock::getModule() const {
> - return getParent()->getParent();
> -}
> -
> Module *BasicBlock::getModule() {
> return getParent()->getParent();
> }
> @@ -125,11 +121,6 @@ TerminatorInst *BasicBlock::getTerminato
> if (InstList.empty()) return nullptr;
> return dyn_cast<TerminatorInst>(&InstList.back());
> }
> -
> -const TerminatorInst *BasicBlock::getTerminator() const {
> - if (InstList.empty()) return nullptr;
> - return dyn_cast<TerminatorInst>(&InstList.back());
> -}
>
> CallInst *BasicBlock::getTerminatingMustTailCall() {
> if (InstList.empty())
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170123/10690840/attachment.html>
More information about the llvm-commits
mailing list