Skip to content

Commit 4c36ecb

Browse files
dianqktstellar
authored andcommitted
[InstCombine] Fix shift calculation in InstCombineCasts (#84027)
Fixes #84025. (cherry picked from commit e96c0c1)
1 parent e90bfdb commit 4c36ecb

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -2156,14 +2156,14 @@ static bool collectInsertionElements(Value *V, unsigned Shift,
21562156
Type *ElementIntTy = IntegerType::get(C->getContext(), ElementSize);
21572157

21582158
for (unsigned i = 0; i != NumElts; ++i) {
2159-
unsigned ShiftI = Shift + i * ElementSize;
2159+
unsigned ShiftI = i * ElementSize;
21602160
Constant *Piece = ConstantFoldBinaryInstruction(
21612161
Instruction::LShr, C, ConstantInt::get(C->getType(), ShiftI));
21622162
if (!Piece)
21632163
return false;
21642164

21652165
Piece = ConstantExpr::getTrunc(Piece, ElementIntTy);
2166-
if (!collectInsertionElements(Piece, ShiftI, Elements, VecEltTy,
2166+
if (!collectInsertionElements(Piece, ShiftI + Shift, Elements, VecEltTy,
21672167
isBigEndian))
21682168
return false;
21692169
}

llvm/test/Transforms/InstCombine/bitcast.ll

+15
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,21 @@ define ptr @bitcast_from_single_element_pointer_vector_to_pointer(<1 x ptr> %ptr
686686
ret ptr %ptr
687687
}
688688

689+
; Sure that we calculate the correct shift.
690+
define <4 x i32> @bitcast_shl(i32 %arg) {
691+
; CHECK-LABEL: @bitcast_shl(
692+
; CHECK-NEXT: [[I5:%.*]] = insertelement <4 x i32> <i32 0, i32 0, i32 65, i32 poison>, i32 [[ARG:%.*]], i64 3
693+
; CHECK-NEXT: ret <4 x i32> [[I5]]
694+
;
695+
%i = zext i32 %arg to i64
696+
%i1 = shl i64 %i, 32
697+
%i2 = or i64 %i1, 65
698+
%i3 = zext i64 %i2 to i128
699+
%i4 = shl i128 %i3, 64
700+
%i5 = bitcast i128 %i4 to <4 x i32>
701+
ret <4 x i32> %i5
702+
}
703+
689704
declare void @f1()
690705
declare void @f2()
691706
define ptr @select_bitcast_unsized_pointer(i1 %c) {

0 commit comments

Comments
 (0)