Skip to content

Commit 747242a

Browse files
committed
[InstCombine] allow more narrowing of casted select
D47163 created a rule that we should not change the casted type of a select when we have matching types in its compare condition. That was intended to help vector codegen, but it also could create situations where we miss subsequent folds as shown in PR44545: https://bugs.llvm.org/show_bug.cgi?id=44545 By using shouldChangeType(), we can continue to get the vector folds (because we always return false for vector types). But we also solve the motivating bug because it's ok to narrow the scalar select in that example. Our canonicalization rules around select are a mess, but AFAICT, this will not induce any infinite looping from the reverse transform (but we'll need to watch for that possibility if committed). Side note: there's a similar use of shouldChangeType() for phi ops just below this diff, and the source and destination types appear to be reversed. Differential Revision: https://reviews.llvm.org/D72733
1 parent 74df89f commit 747242a

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,16 +276,20 @@ Instruction *InstCombiner::commonCastTransforms(CastInst &CI) {
276276
}
277277

278278
if (auto *Sel = dyn_cast<SelectInst>(Src)) {
279-
// We are casting a select. Try to fold the cast into the select, but only
280-
// if the select does not have a compare instruction with matching operand
281-
// types. Creating a select with operands that are different sizes than its
279+
// We are casting a select. Try to fold the cast into the select if the
280+
// select does not have a compare instruction with matching operand types
281+
// or the select is likely better done in a narrow type.
282+
// Creating a select with operands that are different sizes than its
282283
// condition may inhibit other folds and lead to worse codegen.
283284
auto *Cmp = dyn_cast<CmpInst>(Sel->getCondition());
284-
if (!Cmp || Cmp->getOperand(0)->getType() != Sel->getType())
285+
if (!Cmp || Cmp->getOperand(0)->getType() != Sel->getType() ||
286+
(CI.getOpcode() == Instruction::Trunc &&
287+
shouldChangeType(CI.getSrcTy(), CI.getType()))) {
285288
if (Instruction *NV = FoldOpIntoSelect(CI, Sel)) {
286289
replaceAllDbgUsesWith(*Sel, *NV, CI, DT);
287290
return NV;
288291
}
292+
}
289293
}
290294

291295
// If we are casting a PHI, then fold the cast into the PHI.

llvm/test/Transforms/InstCombine/cast-select.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ define <2 x i32> @sext_vec(<2 x i8> %x, <2 x i8> %y, <2 x i8> %z) {
5656
define i16 @trunc(i32 %x, i32 %y, i32 %z) {
5757
; CHECK-LABEL: @trunc(
5858
; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], [[Y:%.*]]
59-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[CMP]], i32 42, i32 [[Z:%.*]]
60-
; CHECK-NEXT: [[R:%.*]] = trunc i32 [[SEL]] to i16
59+
; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[Z:%.*]] to i16
60+
; CHECK-NEXT: [[R:%.*]] = select i1 [[CMP]], i16 42, i16 [[TMP1]]
6161
; CHECK-NEXT: ret i16 [[R]]
6262
;
6363
%cmp = icmp ult i32 %x, %y

llvm/test/Transforms/InstCombine/select-imm-canon.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ define i8 @thisdoesnotloop(i32 %A, i32 %B) {
3939
; CHECK-LABEL: @thisdoesnotloop(
4040
; CHECK-NEXT: entry:
4141
; CHECK-NEXT: [[L1:%.*]] = icmp slt i32 [[A:%.*]], -128
42-
; CHECK-NEXT: [[L2:%.*]] = select i1 [[L1]], i32 -128, i32 [[B:%.*]]
43-
; CHECK-NEXT: [[CONV7:%.*]] = trunc i32 [[L2]] to i8
42+
; CHECK-NEXT: [[TMP0:%.*]] = trunc i32 [[B:%.*]] to i8
43+
; CHECK-NEXT: [[CONV7:%.*]] = select i1 [[L1]], i8 -128, i8 [[TMP0]]
4444
; CHECK-NEXT: ret i8 [[CONV7]]
4545
;
4646
entry:

llvm/test/Transforms/InstCombine/trunc.ll

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,15 +626,13 @@ define <2 x i8> @narrow_sub_vec_constant(<2 x i32> %x) {
626626
ret <2 x i8> %tr
627627
}
628628

629-
; FIXME: If the select is narrowed based on the target's datalayout, we allow more optimizations.
629+
; If the select is narrowed based on the target's datalayout, we allow more optimizations.
630630

631631
define i16 @PR44545(i32 %t0, i32 %data) {
632632
; CHECK-LABEL: @PR44545(
633-
; CHECK-NEXT: [[T1:%.*]] = add nuw nsw i32 [[T0:%.*]], 1
634633
; CHECK-NEXT: [[ISZERO:%.*]] = icmp eq i32 [[DATA:%.*]], 0
635-
; CHECK-NEXT: [[FFS:%.*]] = select i1 [[ISZERO]], i32 0, i32 [[T1]]
636-
; CHECK-NEXT: [[CAST:%.*]] = trunc i32 [[FFS]] to i16
637-
; CHECK-NEXT: [[SUB:%.*]] = add nsw i16 [[CAST]], -1
634+
; CHECK-NEXT: [[TMP1:%.*]] = trunc i32 [[T0:%.*]] to i16
635+
; CHECK-NEXT: [[SUB:%.*]] = select i1 [[ISZERO]], i16 -1, i16 [[TMP1]]
638636
; CHECK-NEXT: ret i16 [[SUB]]
639637
;
640638
%t1 = add nuw nsw i32 %t0, 1

0 commit comments

Comments
 (0)