[clang] [CGExprScalar] Fix inc/dec of vector larger than 64-bit (PR #172301)

Nikita Popov via cfe-commits cfe-commits at lists.llvm.org
Mon Dec 15 06:01:39 PST 2025


https://github.com/nikic created https://github.com/llvm/llvm-project/pull/172301

Use getSigned() to create the 1 or -1 constant, so it gets properly sign extended.

This miscompile was found while working on https://github.com/llvm/llvm-project/pull/171456.

>From f9c0de4fa0959f432ae90d44a07c806eba028b45 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Mon, 15 Dec 2025 14:38:52 +0100
Subject: [PATCH] [CGExprScalar] Fix inc/dec of vector larger than 64-bit

Use getSigned() to create the 1 or -1 constant, so it gets properly
sign extended.
---
 clang/lib/CodeGen/CGExprScalar.cpp   | 2 +-
 clang/test/CodeGen/SystemZ/zvector.c | 8 ++++----
 2 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 769bc37b0e131..9b6497fca829a 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -3335,7 +3335,7 @@ ScalarExprEmitter::EmitScalarPrePostIncDec(const UnaryOperator *E, LValue LV,
   // Vector increment/decrement.
   } else if (type->isVectorType()) {
     if (type->hasIntegerRepresentation()) {
-      llvm::Value *amt = llvm::ConstantInt::get(value->getType(), amount);
+      llvm::Value *amt = llvm::ConstantInt::getSigned(value->getType(), amount);
 
       value = Builder.CreateAdd(value, amt, isInc ? "inc" : "dec");
     } else {
diff --git a/clang/test/CodeGen/SystemZ/zvector.c b/clang/test/CodeGen/SystemZ/zvector.c
index a0b654d9acc9a..35bee8d41df61 100644
--- a/clang/test/CodeGen/SystemZ/zvector.c
+++ b/clang/test/CodeGen/SystemZ/zvector.c
@@ -298,10 +298,10 @@ void test_postinc(void) {
 // CHECK-NEXT:    [[DEC7:%.*]] = add <2 x i64> [[TMP7]], splat (i64 -1)
 // CHECK-NEXT:    store volatile <2 x i64> [[DEC7]], ptr @ul2, align 8
 // CHECK-NEXT:    [[TMP8:%.*]] = load volatile <1 x i128>, ptr @slll2, align 8
-// CHECK-NEXT:    [[DEC8:%.*]] = add <1 x i128> [[TMP8]], splat (i128 18446744073709551615)
+// CHECK-NEXT:    [[DEC8:%.*]] = add <1 x i128> [[TMP8]], splat (i128 -1)
 // CHECK-NEXT:    store volatile <1 x i128> [[DEC8]], ptr @slll2, align 8
 // CHECK-NEXT:    [[TMP9:%.*]] = load volatile <1 x i128>, ptr @ulll2, align 8
-// CHECK-NEXT:    [[DEC9:%.*]] = add <1 x i128> [[TMP9]], splat (i128 18446744073709551615)
+// CHECK-NEXT:    [[DEC9:%.*]] = add <1 x i128> [[TMP9]], splat (i128 -1)
 // CHECK-NEXT:    store volatile <1 x i128> [[DEC9]], ptr @ulll2, align 8
 // CHECK-NEXT:    [[TMP10:%.*]] = load volatile <2 x double>, ptr @fd2, align 8
 // CHECK-NEXT:    [[DEC10:%.*]] = fadd <2 x double> [[TMP10]], splat (double -1.000000e+00)
@@ -356,10 +356,10 @@ void test_predec(void) {
 // CHECK-NEXT:    [[DEC7:%.*]] = add <2 x i64> [[TMP7]], splat (i64 -1)
 // CHECK-NEXT:    store volatile <2 x i64> [[DEC7]], ptr @ul2, align 8
 // CHECK-NEXT:    [[TMP8:%.*]] = load volatile <1 x i128>, ptr @slll2, align 8
-// CHECK-NEXT:    [[DEC8:%.*]] = add <1 x i128> [[TMP8]], splat (i128 18446744073709551615)
+// CHECK-NEXT:    [[DEC8:%.*]] = add <1 x i128> [[TMP8]], splat (i128 -1)
 // CHECK-NEXT:    store volatile <1 x i128> [[DEC8]], ptr @slll2, align 8
 // CHECK-NEXT:    [[TMP9:%.*]] = load volatile <1 x i128>, ptr @ulll2, align 8
-// CHECK-NEXT:    [[DEC9:%.*]] = add <1 x i128> [[TMP9]], splat (i128 18446744073709551615)
+// CHECK-NEXT:    [[DEC9:%.*]] = add <1 x i128> [[TMP9]], splat (i128 -1)
 // CHECK-NEXT:    store volatile <1 x i128> [[DEC9]], ptr @ulll2, align 8
 // CHECK-NEXT:    [[TMP10:%.*]] = load volatile <2 x double>, ptr @fd2, align 8
 // CHECK-NEXT:    [[DEC10:%.*]] = fadd <2 x double> [[TMP10]], splat (double -1.000000e+00)



More information about the cfe-commits mailing list