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

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 27 08:18:23 PST 2024


================
@@ -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());
----------------
steakhal wrote:

Have you checked if there is a utility achieving this for us? Like the `APSIntType` type? (IDK, I rarely ever use it)
I also wonder if we actually need to operate at an APSInt level, maybe we could just convert both of these into int64 and do the math on those.

Is zero extension is actually correct in semantics? What if the `InitNum` was negative, léike `-1`, then shouldn't we use sign extension?

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


More information about the cfe-commits mailing list