From 1bf0915d7a50a12c46b4e42a6e5336fa1f3e5f7f Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Tue, 29 Aug 2017 14:07:48 +0000 Subject: [PATCH] [Instruction] add moveAfter() convenience function; NFCI As suggested in D37121, here's a wrapper for removeFromParent() + insertAfter(), but implemented using moveBefore() for symmetry/efficiency. Differential Revision: https://reviews.llvm.org/D37239 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@312001 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/Instruction.h | 4 ++++ lib/CodeGen/AtomicExpandPass.cpp | 10 ++-------- lib/CodeGen/CodeGenPrepare.cpp | 9 +++------ lib/IR/Instruction.cpp | 4 ++++ lib/Transforms/Vectorize/SLPVectorizer.cpp | 3 +-- 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/include/llvm/IR/Instruction.h b/include/llvm/IR/Instruction.h index 8dc02111b86..0cf8003423f 100644 --- a/include/llvm/IR/Instruction.h +++ b/include/llvm/IR/Instruction.h @@ -113,6 +113,10 @@ public: /// \pre I is a valid iterator into BB. void moveBefore(BasicBlock &BB, SymbolTableList::iterator I); + /// Unlink this instruction from its current basic block and insert it into + /// the basic block that MovePos lives in, right after MovePos. + void moveAfter(Instruction *MovePos); + //===--------------------------------------------------------------------===// // Subclass classification. //===--------------------------------------------------------------------===// diff --git a/lib/CodeGen/AtomicExpandPass.cpp b/lib/CodeGen/AtomicExpandPass.cpp index aa9c8e94d08..9d1f94a8a43 100644 --- a/lib/CodeGen/AtomicExpandPass.cpp +++ b/lib/CodeGen/AtomicExpandPass.cpp @@ -320,16 +320,10 @@ bool AtomicExpand::bracketInstWithFences(Instruction *I, AtomicOrdering Order) { auto LeadingFence = TLI->emitLeadingFence(Builder, I, Order); auto TrailingFence = TLI->emitTrailingFence(Builder, I, Order); - // The trailing fence is emitted before the instruction instead of after - // because there is no easy way of setting Builder insertion point after - // an instruction. So we must erase it from the BB, and insert it back - // in the right place. // We have a guard here because not every atomic operation generates a // trailing fence. - if (TrailingFence) { - TrailingFence->removeFromParent(); - TrailingFence->insertAfter(I); - } + if (TrailingFence) + TrailingFence->moveAfter(I); return (LeadingFence || TrailingFence); } diff --git a/lib/CodeGen/CodeGenPrepare.cpp b/lib/CodeGen/CodeGenPrepare.cpp index 03ef8985210..1250d025fbc 100644 --- a/lib/CodeGen/CodeGenPrepare.cpp +++ b/lib/CodeGen/CodeGenPrepare.cpp @@ -3586,9 +3586,8 @@ Value *TypePromotionHelper::promoteOperandForOther( // Create the truncate now. Value *Trunc = TPT.createTrunc(Ext, ExtOpnd->getType()); if (Instruction *ITrunc = dyn_cast(Trunc)) { - ITrunc->removeFromParent(); // Insert it just after the definition. - ITrunc->insertAfter(ExtOpnd); + ITrunc->moveAfter(ExtOpnd); if (Truncs) Truncs->push_back(ITrunc); } @@ -4943,8 +4942,7 @@ bool CodeGenPrepare::optimizeExt(Instruction *&Inst) { assert(LI && ExtFedByLoad && "Expect a valid load and extension"); TPT.commit(); // Move the extend into the same block as the load - ExtFedByLoad->removeFromParent(); - ExtFedByLoad->insertAfter(LI); + ExtFedByLoad->moveAfter(LI); // CGP does not check if the zext would be speculatively executed when moved // to the same basic block as the load. Preserving its original location // would pessimize the debugging experience, as well as negatively impact @@ -5936,8 +5934,7 @@ void VectorPromoteHelper::promoteImpl(Instruction *ToBePromoted) { "this?"); ToBePromoted->setOperand(U.getOperandNo(), NewVal); } - Transition->removeFromParent(); - Transition->insertAfter(ToBePromoted); + Transition->moveAfter(ToBePromoted); Transition->setOperand(getTransitionOriginalValueIdx(), ToBePromoted); } diff --git a/lib/IR/Instruction.cpp b/lib/IR/Instruction.cpp index 365cb019aec..ceb521c4c48 100644 --- a/lib/IR/Instruction.cpp +++ b/lib/IR/Instruction.cpp @@ -89,6 +89,10 @@ void Instruction::moveBefore(Instruction *MovePos) { moveBefore(*MovePos->getParent(), MovePos->getIterator()); } +void Instruction::moveAfter(Instruction *MovePos) { + moveBefore(*MovePos->getParent(), ++MovePos->getIterator()); +} + void Instruction::moveBefore(BasicBlock &BB, SymbolTableList::iterator I) { assert(I == BB.end() || I->getParent() == &BB); diff --git a/lib/Transforms/Vectorize/SLPVectorizer.cpp b/lib/Transforms/Vectorize/SLPVectorizer.cpp index d8da03ae2a9..1f530cbb5d2 100644 --- a/lib/Transforms/Vectorize/SLPVectorizer.cpp +++ b/lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -4467,8 +4467,7 @@ bool SLPVectorizerPass::tryToVectorizeList(ArrayRef VL, BoUpSLP &R, cast(Builder.CreateExtractElement( VectorizedRoot, Builder.getInt32(VecIdx++))); I->setOperand(1, Extract); - I->removeFromParent(); - I->insertAfter(Extract); + I->moveAfter(Extract); InsertAfter = I; } } -- 2.50.1