Index: llvm/include/llvm/IR/Instruction.h =================================================================== --- llvm/include/llvm/IR/Instruction.h +++ llvm/include/llvm/IR/Instruction.h @@ -137,15 +137,34 @@ /// the basic block that MovePos lives in, right before MovePos. void moveBefore(Instruction *MovePos); + /// Perform a \ref moveBefore operation, while signalling that the caller + /// intends to preserve the original ordering of instructions. This implicitly + /// means that any adjacent debug-info should move with this instruction. + /// This method is currently a no-op placeholder, but it will become meaningful + /// when the "RemoveDIs" project is enabled. + void moveBeforePreserving(Instruction *MovePos) { + moveBefore(MovePos); + } + /// Unlink this instruction and insert into BB before I. /// /// \pre I is a valid iterator into BB. void moveBefore(BasicBlock &BB, SymbolTableList::iterator I); + /// (See other overload for moveBeforePreserving). + void moveBeforePreserving(BasicBlock &BB, SymbolTableList::iterator I) { + moveBefore(BB, 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); + /// See \ref moveBeforePreserving . + void moveAfterPreserving(Instruction *MovePos) { + moveAfter(MovePos); + } + /// Given an instruction Other in the same basic block as this instruction, /// return true if this instruction comes before Other. In this worst case, /// this takes linear time in the number of instructions in the block. The Index: llvm/lib/CodeGen/SelectOptimize.cpp =================================================================== --- llvm/lib/CodeGen/SelectOptimize.cpp +++ llvm/lib/CodeGen/SelectOptimize.cpp @@ -438,7 +438,7 @@ DIt++; } for (auto *DI : DebugPseudoINS) { - DI->moveBefore(&*EndBlock->getFirstInsertionPt()); + DI->moveBeforePreserving(&*EndBlock->getFirstInsertionPt()); } // These are the new basic blocks for the conditional branch. Index: llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp =================================================================== --- llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp +++ llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp @@ -518,7 +518,7 @@ if (I.isTerminator()) continue; - I.moveBefore(*OI.EntryBB, OI.EntryBB->getFirstInsertionPt()); + I.moveBeforePreserving(*OI.EntryBB, OI.EntryBB->getFirstInsertionPt()); } OI.EntryBB->moveBefore(&ArtificialEntry); Index: llvm/lib/Transforms/IPO/IROutliner.cpp =================================================================== --- llvm/lib/Transforms/IPO/IROutliner.cpp +++ llvm/lib/Transforms/IPO/IROutliner.cpp @@ -155,7 +155,7 @@ /// \param TargetBB - the BasicBlock to put Instruction into. static void moveBBContents(BasicBlock &SourceBB, BasicBlock &TargetBB) { for (Instruction &I : llvm::make_early_inc_range(SourceBB)) - I.moveBefore(TargetBB, TargetBB.end()); + I.moveBeforePreserving(TargetBB, TargetBB.end()); } /// A function to sort the keys of \p Map, which must be a mapping of constant Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp =================================================================== --- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp +++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -2642,7 +2642,7 @@ for (Instruction &Instr : llvm::make_early_inc_range(*FreeInstrBB)) { if (&Instr == FreeInstrBBTerminator) break; - Instr.moveBefore(TI); + Instr.moveBeforePreserving(TI); } assert(FreeInstrBB->size() == 1 && "Only the branch instruction should remain"); Index: llvm/lib/Transforms/Scalar/LoopInterchange.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LoopInterchange.cpp +++ llvm/lib/Transforms/Scalar/LoopInterchange.cpp @@ -1375,7 +1375,7 @@ for (Instruction &I : make_early_inc_range(make_range(InnerLoopPreHeader->begin(), std::prev(InnerLoopPreHeader->end())))) - I.moveBefore(OuterLoopHeader->getTerminator()); + I.moveBeforePreserving(OuterLoopHeader->getTerminator()); } Transformed |= adjustLoopLinks(); Index: llvm/lib/Transforms/Scalar/MergeICmps.cpp =================================================================== --- llvm/lib/Transforms/Scalar/MergeICmps.cpp +++ llvm/lib/Transforms/Scalar/MergeICmps.cpp @@ -274,7 +274,7 @@ // Do the actual spliting. for (Instruction *Inst : reverse(OtherInsts)) - Inst->moveBefore(*NewParent, NewParent->begin()); + Inst->moveBeforePreserving(*NewParent, NewParent->begin()); } bool BCECmpBlock::canSplit(AliasAnalysis &AA) const { Index: llvm/lib/Transforms/Scalar/SpeculativeExecution.cpp =================================================================== --- llvm/lib/Transforms/Scalar/SpeculativeExecution.cpp +++ llvm/lib/Transforms/Scalar/SpeculativeExecution.cpp @@ -316,7 +316,7 @@ auto Current = I; ++I; if (!NotHoisted.count(&*Current)) { - Current->moveBefore(ToBlock.getTerminator()); + Current->moveBeforePreserving(ToBlock.getTerminator()); } } return true; Index: llvm/lib/Transforms/Utils/BasicBlockUtils.cpp =================================================================== --- llvm/lib/Transforms/Utils/BasicBlockUtils.cpp +++ llvm/lib/Transforms/Utils/BasicBlockUtils.cpp @@ -300,7 +300,7 @@ PredBB->back().eraseFromParent(); // Move terminator instruction. - PredBB->splice(PredBB->end(), BB); + BB->back().moveBeforePreserving(*PredBB, PredBB->end()); // Terminator may be a memory accessing instruction too. if (MSSAU) Index: llvm/lib/Transforms/Utils/CodeMoverUtils.cpp =================================================================== --- llvm/lib/Transforms/Utils/CodeMoverUtils.cpp +++ llvm/lib/Transforms/Utils/CodeMoverUtils.cpp @@ -417,7 +417,7 @@ Instruction *MovePos = ToBB.getFirstNonPHIOrDbg(); if (isSafeToMoveBefore(I, *MovePos, DT, &PDT, &DI)) - I.moveBefore(MovePos); + I.moveBeforePreserving(MovePos); } } @@ -429,7 +429,7 @@ while (FromBB.size() > 1) { Instruction &I = FromBB.front(); if (isSafeToMoveBefore(I, *MovePos, DT, &PDT, &DI)) - I.moveBefore(MovePos); + I.moveBeforePreserving(MovePos); } } Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- llvm/lib/Transforms/Utils/SimplifyCFG.cpp +++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp @@ -1605,13 +1605,14 @@ // The debug location is an integral part of a debug info intrinsic // and can't be separated from it or replaced. Instead of attempting // to merge locations, simply hoist both copies of the intrinsic. - BIParent->splice(BI->getIterator(), BB1, I1->getIterator()); - BIParent->splice(BI->getIterator(), BB2, I2->getIterator()); + I1->moveBeforePreserving(BI); + I2->moveBeforePreserving(BI); + Changed = true; } else { // For a normal instruction, we just move one to right before the // branch, then replace all uses of the other with the first. Finally, // we remove the now redundant second instruction. - BIParent->splice(BI->getIterator(), BB1, I1->getIterator()); + I1->moveBeforePreserving(BI); if (!I2->use_empty()) I2->replaceAllUsesWith(I1); I1->andIRFlags(I2);