[clang] [analyzer] Fix zext assertion failure in loop unrolling (PR #121203)

via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 27 06:09:27 PST 2024


https://github.com/shenjunjiekoda updated https://github.com/llvm/llvm-project/pull/121203

>From 2cee5fc2abd0cf2979c9ba975906e659adcd4463 Mon Sep 17 00:00:00 2001
From: shenjunjie <shenjunjiekoda at foxmail.com>
Date: Fri, 27 Dec 2024 14:08:55 +0000
Subject: [PATCH] [analyzer] Fix zext assertion failure in loop unrolling

---
 clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
index 96f5d7c44baf89..9227a7876c0b2f 100644
--- a/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
+++ b/clang/lib/StaticAnalyzer/Core/LoopUnrolling.cpp
@@ -283,10 +283,12 @@ static bool shouldCompletelyUnroll(const Stmt *LoopStmt, ASTContext &ASTCtx,
   llvm::APInt InitNum =
       Matches[0].getNodeAs<IntegerLiteral>("initNum")->getValue();
   auto CondOp = Matches[0].getNodeAs<BinaryOperator>("conditionOperator");
-  if (InitNum.getBitWidth() != BoundNum.getBitWidth()) {
-    InitNum = InitNum.zext(BoundNum.getBitWidth());
-    BoundNum = BoundNum.zext(InitNum.getBitWidth());
-  }
+  unsigned MaxWidth = std::max(InitNum.getBitWidth(), BoundNum.getBitWidth());
+
+  if (InitNum.getBitWidth() != MaxWidth)
+    InitNum = InitNum.zext(MaxWidth);
+  if (BoundNum.getBitWidth() != MaxWidth)
+    BoundNum = BoundNum.zext(MaxWidth);
 
   if (CondOp->getOpcode() == BO_GE || CondOp->getOpcode() == BO_LE)
     maxStep = (BoundNum - InitNum + 1).abs().getZExtValue();



More information about the cfe-commits mailing list