[llvm] [indvars] Missing variables at Og (PR #88270)

Carlos Alberto Enciso via llvm-commits llvm-commits at lists.llvm.org
Tue May 21 06:07:18 PDT 2024


================
@@ -1611,11 +1657,30 @@ int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI,
     // Replace PN with ExitVal if that is legal and does not break LCSSA.
     if (PN->getNumIncomingValues() == 1 &&
         LI->replacementPreservesLCSSAForm(PN, ExitVal)) {
+      addDebugValuesToLoopVariable(PN->getParent(), ExitVal, PN);
       PN->replaceAllUsesWith(ExitVal);
       PN->eraseFromParent();
     }
   }
 
+  // If the loop can be deleted and there are no PHIs to be rewritten (there
+  // are no loop live-out values), record debug variables corresponding to the
+  // induction variable with their constant exit-values. Those values will be
+  // inserted by the 'deletion loop' logic.
+  if (LoopCanBeDel && RewritePhiSet.empty()) {
+    if (auto *IndVar = L->getInductionVariable(*SE)) {
+      const SCEV *PNSCEV = SE->getSCEVAtScope(IndVar, L->getParentLoop());
+      if (auto *Const = dyn_cast<SCEVConstant>(PNSCEV)) {
+        Value *FinalIVValue = Const->getValue();
+        if (L->getUniqueExitBlock()) {
+          SmallVector<DbgVariableIntrinsic *> DbgUsers;
+          findDbgUsers(DbgUsers, IndVar);
+          L->preserveDebugInductionVariableInfo(FinalIVValue, DbgUsers);
----------------
CarlosAlbertoEnciso wrote:

@OCHyams Sorry for my delay in answering your question.

```
  // If the loop can be deleted and there are no PHIs to be rewritten (there
  // are no loop live-out values), record debug variables corresponding to the
  // induction variable with their constant exit-values. Those values will be
  // inserted by the 'deletion loop' logic.
  if (LoopCanBeDel && RewritePhiSet.empty()) {
    ...
        if (L->getUniqueExitBlock()) {
          ...
        }
  }
```
The `RewritePhiSet.empty()` indicates that the `indvars` pass has not being triggered (No PHIs to be rewritten); therefore no changes should be made. That was raised on early feedback by @nikic. And as we know that the loop can be deleted, we just collect the information and pass it to the `loop-deletion` pass.


https://github.com/llvm/llvm-project/pull/88270


More information about the llvm-commits mailing list