From e5be660e25774e4a769be52a9b7817a1917b7ff7 Mon Sep 17 00:00:00 2001 From: Roman Lebedev Date: Sun, 5 May 2019 18:59:12 +0000 Subject: [PATCH] [NFC][Utils] deleteDeadLoop(): add an assert that exit block has some non-PHI instruction Summary: If `deleteDeadLoop()` is called on such a loop, that has "bad" exit block, one that e.g. has no terminator instruction, the `DIBuilder::insertDbgValueIntrinsic()` will be told to insert the Dbg Value Intrinsic after `nullptr` (since there is no first non-PHI instruction), which will cause it to not insert those instructions into any basic block. The instructions will be parent-less, and IR verifier will complain. It is rather obvious to track down the root cause when that happens, so let's just assert it never happens. Reviewers: sanjoy, davide, vsk Reviewed By: vsk Subscribers: llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D61008 llvm-svn: 359993 --- llvm/lib/Transforms/Utils/LoopUtils.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/llvm/lib/Transforms/Utils/LoopUtils.cpp b/llvm/lib/Transforms/Utils/LoopUtils.cpp index 045cb62..16971b2 100644 --- a/llvm/lib/Transforms/Utils/LoopUtils.cpp +++ b/llvm/lib/Transforms/Utils/LoopUtils.cpp @@ -584,10 +584,14 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT = nullptr, // dbg.value truncates the range of any dbg.value before the loop where the // loop used to be. This is particularly important for constant values. DIBuilder DIB(*ExitBlock->getModule()); + Instruction *InsertDbgValueBefore = ExitBlock->getFirstNonPHI(); + assert(InsertDbgValueBefore && + "There should be a non-PHI instruction in exit block, else these " + "instructions will have no parent."); for (auto *DVI : DeadDebugInst) - DIB.insertDbgValueIntrinsic( - UndefValue::get(Builder.getInt32Ty()), DVI->getVariable(), - DVI->getExpression(), DVI->getDebugLoc(), ExitBlock->getFirstNonPHI()); + DIB.insertDbgValueIntrinsic(UndefValue::get(Builder.getInt32Ty()), + DVI->getVariable(), DVI->getExpression(), + DVI->getDebugLoc(), InsertDbgValueBefore); // Remove the block from the reference counting scheme, so that we can // delete it freely later. -- 2.7.4