diff --git a/llvm/lib/Transforms/Scalar/Sink.cpp b/llvm/lib/Transforms/Scalar/Sink.cpp index 1a48a59c4189e..4b63698e5e132 100644 --- a/llvm/lib/Transforms/Scalar/Sink.cpp +++ b/llvm/lib/Transforms/Scalar/Sink.cpp @@ -82,11 +82,6 @@ static bool IsAcceptableTarget(Instruction *Inst, BasicBlock *SuccToSinkTo, !Inst->hasMetadata(LLVMContext::MD_invariant_load)) return false; - // We don't want to sink across a critical edge if we don't dominate the - // successor. We could be introducing calculations to new code paths. - if (!DT.dominates(Inst->getParent(), SuccToSinkTo)) - return false; - // Don't sink instructions into a loop. Loop *succ = LI.getLoopFor(SuccToSinkTo); Loop *cur = LI.getLoopFor(Inst->getParent()); @@ -144,9 +139,6 @@ static bool SinkInstruction(Instruction *Inst, SuccToSinkTo = DT.findNearestCommonDominator(SuccToSinkTo, UseBlock); else SuccToSinkTo = UseBlock; - // The current basic block needs to dominate the candidate. - if (!DT.dominates(BB, SuccToSinkTo)) - return false; } if (SuccToSinkTo) { @@ -167,6 +159,12 @@ static bool SinkInstruction(Instruction *Inst, Inst->getParent()->printAsOperand(dbgs(), false); dbgs() << " -> "; SuccToSinkTo->printAsOperand(dbgs(), false); dbgs() << ")\n"); + // The current location of Inst dominates all uses, thus it must dominate + // SuccToSinkTo, which is on the IDom chain between the nearest common + // dominator to all uses and the current location. + assert(DT.dominates(BB, SuccToSinkTo) && + "SuccToSinkTo must be dominated by current Inst location!"); + // Move the instruction. Inst->moveBefore(SuccToSinkTo->getFirstInsertionPt()); return true;