[llvm] [IndVarSimplify] Add safety check for getTruncateExpr in genLoopLimit (PR #181296)

via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 12 19:19:52 PST 2026


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff origin/main HEAD --extensions cpp -- llvm/lib/Transforms/Scalar/IndVarSimplify.cpp --diff_from_common_commit
``````````

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
index c444fda8c..507da46f4 100644
--- a/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ b/llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -643,8 +643,7 @@ static void visitIVCast(CastInst *Cast, WideIVInfo &WI,
   // because at least an ADD is required to increment the induction variable. We
   // could compute more comprehensively the cost of all instructions on the
   // induction variable when necessary.
-  if (TTI &&
-      TTI->getArithmeticInstrCost(Instruction::Add, Ty) >
+  if (TTI && TTI->getArithmeticInstrCost(Instruction::Add, Ty) >
                  TTI->getArithmeticInstrCost(Instruction::Add,
                                              Cast->getOperand(0)->getType())) {
     return;
@@ -1057,13 +1056,13 @@ static Value *genLoopLimit(PHINode *IndVar, BasicBlock *ExitingBB,
 /// able to rewrite the exit tests of any loop where the SCEV analysis can
 /// determine a loop-invariant trip count of the loop, which is actually a much
 /// broader range than just linear tests.
-bool IndVarSimplify::
-linearFunctionTestReplace(Loop *L, BasicBlock *ExitingBB,
+bool IndVarSimplify::linearFunctionTestReplace(Loop *L, BasicBlock *ExitingBB,
                                                const SCEV *ExitCount,
-                          PHINode *IndVar, SCEVExpander &Rewriter) {
+                                               PHINode *IndVar,
+                                               SCEVExpander &Rewriter) {
   assert(L->getLoopLatch() && "Loop no longer in simplified form?");
   assert(isLoopCounter(IndVar, L, SE));
-  Instruction * const IncVar =
+  Instruction *const IncVar =
       cast<Instruction>(IndVar->getIncomingValueForBlock(L->getLoopLatch()));
 
   // Initialize CmpIndVar to the preincremented IV.
@@ -1705,12 +1704,12 @@ bool IndVarSimplify::optimizeLoopExits(Loop *L, SCEVExpander &Rewriter) {
   llvm::sort(ExitingBlocks, [&](BasicBlock *A, BasicBlock *B) {
     // std::sort sorts in ascending order, so we want the inverse of
     // the normal dominance relation.
-               if (A == B) return false;
+    if (A == B)
+      return false;
     if (DT->properlyDominates(A, B))
       return true;
     else {
-                 assert(DT->properlyDominates(B, A) &&
-                        "expected total dominance order!");
+      assert(DT->properlyDominates(B, A) && "expected total dominance order!");
       return false;
     }
   });

``````````

</details>


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


More information about the llvm-commits mailing list