[llvm] [InstCombine] Fix shift calculation in InstCombineCasts (PR #84027)
Quentin Dian via llvm-commits
llvm-commits at lists.llvm.org
Tue Mar 5 07:10:26 PST 2024
https://github.com/DianQK created https://github.com/llvm/llvm-project/pull/84027
Fixes #84025.
>From f7ebd19341a4f779f83714c30f1aeecd293161f1 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Tue, 5 Mar 2024 22:39:55 +0800
Subject: [PATCH 1/2] Pre-commit test cases
---
llvm/test/Transforms/InstCombine/bitcast.ll | 15 +++++++++++++++
1 file changed, 15 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/bitcast.ll b/llvm/test/Transforms/InstCombine/bitcast.ll
index 176b432ea0b5c9..57e885911334e1 100644
--- a/llvm/test/Transforms/InstCombine/bitcast.ll
+++ b/llvm/test/Transforms/InstCombine/bitcast.ll
@@ -686,6 +686,21 @@ define ptr @bitcast_from_single_element_pointer_vector_to_pointer(<1 x ptr> %ptr
ret ptr %ptr
}
+; Sure that we calculate the correct shift.
+define <4 x i32> @bitcast_shl(i32 %arg) {
+; CHECK-LABEL: @bitcast_shl(
+; CHECK-NEXT: [[I5:%.*]] = insertelement <4 x i32> <i32 0, i32 0, i32 0, i32 poison>, i32 [[ARG:%.*]], i64 3
+; CHECK-NEXT: ret <4 x i32> [[I5]]
+;
+ %i = zext i32 %arg to i64
+ %i1 = shl i64 %i, 32
+ %i2 = or i64 %i1, 65
+ %i3 = zext i64 %i2 to i128
+ %i4 = shl i128 %i3, 64
+ %i5 = bitcast i128 %i4 to <4 x i32>
+ ret <4 x i32> %i5
+}
+
declare void @f1()
declare void @f2()
define ptr @select_bitcast_unsized_pointer(i1 %c) {
>From f029500dab8cba8e243d8a0f45083b1d333fcb58 Mon Sep 17 00:00:00 2001
From: DianQK <dianqk at dianqk.net>
Date: Tue, 5 Mar 2024 22:56:49 +0800
Subject: [PATCH 2/2] [InstCombine] Fix shift calculation in InstCombineCasts
---
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp | 4 ++--
llvm/test/Transforms/InstCombine/bitcast.ll | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 33ed1d5575375a..45afa6363ae01f 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -2167,14 +2167,14 @@ static bool collectInsertionElements(Value *V, unsigned Shift,
Type *ElementIntTy = IntegerType::get(C->getContext(), ElementSize);
for (unsigned i = 0; i != NumElts; ++i) {
- unsigned ShiftI = Shift + i * ElementSize;
+ unsigned ShiftI = i * ElementSize;
Constant *Piece = ConstantFoldBinaryInstruction(
Instruction::LShr, C, ConstantInt::get(C->getType(), ShiftI));
if (!Piece)
return false;
Piece = ConstantExpr::getTrunc(Piece, ElementIntTy);
- if (!collectInsertionElements(Piece, ShiftI, Elements, VecEltTy,
+ if (!collectInsertionElements(Piece, ShiftI + Shift, Elements, VecEltTy,
isBigEndian))
return false;
}
diff --git a/llvm/test/Transforms/InstCombine/bitcast.ll b/llvm/test/Transforms/InstCombine/bitcast.ll
index 57e885911334e1..5599604b666fb7 100644
--- a/llvm/test/Transforms/InstCombine/bitcast.ll
+++ b/llvm/test/Transforms/InstCombine/bitcast.ll
@@ -689,7 +689,7 @@ define ptr @bitcast_from_single_element_pointer_vector_to_pointer(<1 x ptr> %ptr
; Sure that we calculate the correct shift.
define <4 x i32> @bitcast_shl(i32 %arg) {
; CHECK-LABEL: @bitcast_shl(
-; CHECK-NEXT: [[I5:%.*]] = insertelement <4 x i32> <i32 0, i32 0, i32 0, i32 poison>, i32 [[ARG:%.*]], i64 3
+; CHECK-NEXT: [[I5:%.*]] = insertelement <4 x i32> <i32 0, i32 0, i32 65, i32 poison>, i32 [[ARG:%.*]], i64 3
; CHECK-NEXT: ret <4 x i32> [[I5]]
;
%i = zext i32 %arg to i64
More information about the llvm-commits
mailing list