Skip to content

Commit 66eedd1

Browse files
authored
[InstCombine] Fix worklist management in select fold (#77738)
`InstCombine` uses `Worklist` to manage change history. `setOperand`, which was previously used to change the `Select` Instruction, does not, so it is `run` twice, which causes an `LLVM ERROR`. This problem is resolved by changing `setOperand` to `replaceOperand` as the change history will be registered in the Worklist. Fixes #77553.
1 parent c9c8f0c commit 66eedd1

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,11 +1704,11 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
17041704
if (CmpRHS != CmpLHS && isa<Constant>(CmpRHS) && !isa<Constant>(CmpLHS)) {
17051705
if (CmpLHS == TrueVal && Pred == ICmpInst::ICMP_EQ) {
17061706
// Transform (X == C) ? X : Y -> (X == C) ? C : Y
1707-
SI.setOperand(1, CmpRHS);
1707+
replaceOperand(SI, 1, CmpRHS);
17081708
Changed = true;
17091709
} else if (CmpLHS == FalseVal && Pred == ICmpInst::ICMP_NE) {
17101710
// Transform (X != C) ? Y : X -> (X != C) ? Y : C
1711-
SI.setOperand(2, CmpRHS);
1711+
replaceOperand(SI, 2, CmpRHS);
17121712
Changed = true;
17131713
}
17141714
}

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3658,3 +3658,17 @@ loop:
36583658
exit:
36593659
ret i32 %rem
36603660
}
3661+
3662+
; (X == C) ? X : Y -> (X == C) ? C : Y
3663+
; Fixed #77553
3664+
define i32 @src_select_xxory_eq0_xorxy_y(i32 %x, i32 %y) {
3665+
; CHECK-LABEL: @src_select_xxory_eq0_xorxy_y(
3666+
; CHECK-NEXT: [[XOR0:%.*]] = icmp eq i32 [[X:%.*]], [[Y:%.*]]
3667+
; CHECK-NEXT: [[COND:%.*]] = select i1 [[XOR0]], i32 0, i32 [[Y]]
3668+
; CHECK-NEXT: ret i32 [[COND]]
3669+
;
3670+
%xor = xor i32 %x, %y
3671+
%xor0 = icmp eq i32 %xor, 0
3672+
%cond = select i1 %xor0, i32 %xor, i32 %y
3673+
ret i32 %cond
3674+
}

0 commit comments

Comments
 (0)