[polly] [Polly] Use separate DT/LI/SE for outlined subfn. NFC. (PR #102460)

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 9 02:07:14 PDT 2024


================
@@ -319,101 +359,117 @@ struct ScopExpander final : SCEVVisitor<ScopExpander, const SCEV *> {
                   Inst->getOpcode() != Instruction::SDiv))
       return visitGenericInst(E, Inst, IP);
 
-    const SCEV *LHSScev = SE.getSCEV(Inst->getOperand(0));
-    const SCEV *RHSScev = SE.getSCEV(Inst->getOperand(1));
+    const SCEV *LHSScev = GenSE.getSCEV(Inst->getOperand(0));
+    const SCEV *RHSScev = GenSE.getSCEV(Inst->getOperand(1));
 
-    if (!SE.isKnownNonZero(RHSScev))
-      RHSScev = SE.getUMaxExpr(RHSScev, SE.getConstant(E->getType(), 1));
+    if (!GenSE.isKnownNonZero(RHSScev))
+      RHSScev = GenSE.getUMaxExpr(RHSScev, GenSE.getConstant(E->getType(), 1));
 
     Value *LHS = expandCodeFor(LHSScev, E->getType(), IP);
     Value *RHS = expandCodeFor(RHSScev, E->getType(), IP);
 
     Inst =
         BinaryOperator::Create((Instruction::BinaryOps)Inst->getOpcode(), LHS,
                                RHS, Inst->getName() + Name, IP->getIterator());
-    return SE.getSCEV(Inst);
+    return GenSE.getSCEV(Inst);
   }
 
-  /// The following functions will just traverse the SCEV and rebuild it with
-  /// the new operands returned by the traversal.
+  /// The following functions will just traverse the SCEV and rebuild it using
+  /// GenSE and the new operands returned by the traversal.
   ///
   ///{
   const SCEV *visitConstant(const SCEVConstant *E) { return E; }
   const SCEV *visitVScale(const SCEVVScale *E) { return E; }
   const SCEV *visitPtrToIntExpr(const SCEVPtrToIntExpr *E) {
-    return SE.getPtrToIntExpr(visit(E->getOperand()), E->getType());
+    return GenSE.getPtrToIntExpr(visit(E->getOperand()), E->getType());
   }
   const SCEV *visitTruncateExpr(const SCEVTruncateExpr *E) {
-    return SE.getTruncateExpr(visit(E->getOperand()), E->getType());
+    return GenSE.getTruncateExpr(visit(E->getOperand()), E->getType());
   }
   const SCEV *visitZeroExtendExpr(const SCEVZeroExtendExpr *E) {
-    return SE.getZeroExtendExpr(visit(E->getOperand()), E->getType());
+    return GenSE.getZeroExtendExpr(visit(E->getOperand()), E->getType());
   }
   const SCEV *visitSignExtendExpr(const SCEVSignExtendExpr *E) {
-    return SE.getSignExtendExpr(visit(E->getOperand()), E->getType());
+    return GenSE.getSignExtendExpr(visit(E->getOperand()), E->getType());
   }
   const SCEV *visitUDivExpr(const SCEVUDivExpr *E) {
     auto *RHSScev = visit(E->getRHS());
-    if (!SE.isKnownNonZero(RHSScev))
-      RHSScev = SE.getUMaxExpr(RHSScev, SE.getConstant(E->getType(), 1));
-    return SE.getUDivExpr(visit(E->getLHS()), RHSScev);
+    if (!GenSE.isKnownNonZero(RHSScev))
+      RHSScev = GenSE.getUMaxExpr(RHSScev, GenSE.getConstant(E->getType(), 1));
+    return GenSE.getUDivExpr(visit(E->getLHS()), RHSScev);
   }
   const SCEV *visitAddExpr(const SCEVAddExpr *E) {
     SmallVector<const SCEV *, 4> NewOps;
     for (const SCEV *Op : E->operands())
       NewOps.push_back(visit(Op));
-    return SE.getAddExpr(NewOps);
+    return GenSE.getAddExpr(NewOps);
   }
   const SCEV *visitMulExpr(const SCEVMulExpr *E) {
     SmallVector<const SCEV *, 4> NewOps;
     for (const SCEV *Op : E->operands())
       NewOps.push_back(visit(Op));
-    return SE.getMulExpr(NewOps);
+    return GenSE.getMulExpr(NewOps);
   }
   const SCEV *visitUMaxExpr(const SCEVUMaxExpr *E) {
     SmallVector<const SCEV *, 4> NewOps;
     for (const SCEV *Op : E->operands())
       NewOps.push_back(visit(Op));
-    return SE.getUMaxExpr(NewOps);
+    return GenSE.getUMaxExpr(NewOps);
   }
   const SCEV *visitSMaxExpr(const SCEVSMaxExpr *E) {
     SmallVector<const SCEV *, 4> NewOps;
     for (const SCEV *Op : E->operands())
       NewOps.push_back(visit(Op));
-    return SE.getSMaxExpr(NewOps);
+    return GenSE.getSMaxExpr(NewOps);
   }
   const SCEV *visitUMinExpr(const SCEVUMinExpr *E) {
     SmallVector<const SCEV *, 4> NewOps;
     for (const SCEV *Op : E->operands())
       NewOps.push_back(visit(Op));
-    return SE.getUMinExpr(NewOps);
+    return GenSE.getUMinExpr(NewOps);
   }
   const SCEV *visitSMinExpr(const SCEVSMinExpr *E) {
     SmallVector<const SCEV *, 4> NewOps;
     for (const SCEV *Op : E->operands())
       NewOps.push_back(visit(Op));
-    return SE.getSMinExpr(NewOps);
+    return GenSE.getSMinExpr(NewOps);
   }
   const SCEV *visitSequentialUMinExpr(const SCEVSequentialUMinExpr *E) {
     SmallVector<const SCEV *, 4> NewOps;
     for (const SCEV *Op : E->operands())
       NewOps.push_back(visit(Op));
-    return SE.getUMinExpr(NewOps, /*Sequential=*/true);
+    return GenSE.getUMinExpr(NewOps, /*Sequential=*/true);
   }
   const SCEV *visitAddRecExpr(const SCEVAddRecExpr *E) {
     SmallVector<const SCEV *, 4> NewOps;
     for (const SCEV *Op : E->operands())
       NewOps.push_back(visit(Op));
-    return SE.getAddRecExpr(NewOps, E->getLoop(), E->getNoWrapFlags());
+
+    const Loop *L = E->getLoop();
+    const SCEV *GenLRepl = LoopMap ? LoopMap->lookup(L) : nullptr;
+    if (!GenLRepl)
+      return GenSE.getAddRecExpr(NewOps, L, E->getNoWrapFlags());
+
+    // evaluateAtIteration replaces the SCEVAddrExpr with a direct calculation.
+    const SCEV *Evaluated =
+        SCEVAddRecExpr::evaluateAtIteration(NewOps, GenLRepl, GenSE);
+
+    // FIXME: This emits a SCEV for GenSE (since GenLRepl will refer to the
+    // induction variable of a generated loop), so we should not use SCEVVisitor
+    // with it. Howver, it still contains references to the SCoP region.
----------------
Meinersbur wrote:

`GenLRepl` contains them. The `LoopMap`/`LTS` is constructed somewhere without considering uses in a subfunction. 

It does not make a difference in practice since SCEVs don't remember which SE they belong to. `visitUnknown` has another FIXME of this kind that I am even less sure on how to fix. I don't want to spend too much time hunting behind all the FIXMEs since I think @aengelke wants to land https://github.com/llvm/llvm-project/pull/101198.

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


More information about the llvm-commits mailing list