Skip to content

Commit 27eaa8a

Browse files
authored
[InstCombine] Prevent infinite loop with two shifts (#118806)
The following pattern: `(C2 << X) << C1` will usually be transformed into `(C2 << C1) << X`, essentially swapping `X` and `C1`. However, this should only be done when `C1` is an immediate constant, otherwise thiscan lead to both constants being swapped forever. This fixes #118798.
1 parent 8a90b5b commit 27eaa8a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ Instruction *InstCombinerImpl::commonShiftTransforms(BinaryOperator &I) {
427427
if (Instruction *R = FoldOpIntoSelect(I, SI))
428428
return R;
429429

430-
if (Constant *CUI = dyn_cast<Constant>(Op1))
430+
Constant *CUI;
431+
if (match(Op1, m_ImmConstant(CUI)))
431432
if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I))
432433
return Res;
433434

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
4+
@c = external constant i8
5+
@c2 = external constant i8
6+
7+
define i64 @testfunc() {
8+
; CHECK-LABEL: @testfunc(
9+
; CHECK-NEXT: [[SHL1:%.*]] = shl nuw i64 1, ptrtoint (ptr @c2 to i64)
10+
; CHECK-NEXT: [[SHL2:%.*]] = shl i64 [[SHL1]], ptrtoint (ptr @c to i64)
11+
; CHECK-NEXT: ret i64 [[SHL2]]
12+
;
13+
%shl1 = shl i64 1, ptrtoint (ptr @c2 to i64)
14+
%shl2 = shl i64 %shl1, ptrtoint (ptr @c to i64)
15+
ret i64 %shl2
16+
}

0 commit comments

Comments
 (0)