[llvm] [InstCombine] Fold binop into multi-use select when one select arm and the other operand are constant (PR #196583)
Iris Shi via llvm-commits
llvm-commits at lists.llvm.org
Fri May 8 10:05:37 PDT 2026
https://github.com/el-ev created https://github.com/llvm/llvm-project/pull/196583
Downstream issue: https://github.com/rust-lang/rust/issues/155985
Fold `binop(select(c, C, x), C2) => select(c, C3, binop(x, C2))` when one arm and the binop operand are both constants, even if the select has multiple uses.
It is a common pattern in Rust for tagged unions like `Result<bool, X>`, where `size_of::<X>() == 4`, to be packed into one `i64`, with the tags stored in the lower half and the value in the higher.
https://godbolt.org/z/aG9G6o7Ee shows a regression where LLVM failed to recognize the pattern of accessing the value of a known discriminant (`Err(x)` in this case), resulting in huge constant loads like `0x00000001_00000001`. This PR adds support for that.
Alive2: https://alive2.llvm.org/ce/z/Sg-5Lj
>From c733dedc05dfcd089aea65b9338095dd935920d7 Mon Sep 17 00:00:00 2001
From: Iris Shi <0.0 at owo.li>
Date: Sat, 9 May 2026 00:15:21 +0800
Subject: [PATCH 1/2] pre-commit tests
---
.../fold-multi-use-select-packed-constants.ll | 182 ++++++++++++++++++
1 file changed, 182 insertions(+)
create mode 100644 llvm/test/Transforms/InstCombine/fold-multi-use-select-packed-constants.ll
diff --git a/llvm/test/Transforms/InstCombine/fold-multi-use-select-packed-constants.ll b/llvm/test/Transforms/InstCombine/fold-multi-use-select-packed-constants.ll
new file mode 100644
index 0000000000000..32999f3f3e278
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fold-multi-use-select-packed-constants.ll
@@ -0,0 +1,182 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define noundef i32 @rust_155985(i32 noundef %n, i1 noundef zeroext %b) {
+; CHECK-LABEL: @rust_155985(
+; CHECK-NEXT: start:
+; CHECK-NEXT: [[EQ:%.*]] = icmp eq i32 [[N:%.*]], 0
+; CHECK-NEXT: [[INNER:%.*]] = select i1 [[EQ]], i64 4294967297, i64 8589934593
+; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[B:%.*]], i64 0, i64 [[INNER]]
+; CHECK-NEXT: [[TAG:%.*]] = trunc i64 [[OUTER]] to i1
+; CHECK-NEXT: [[SHIFT:%.*]] = lshr i64 [[OUTER]], 32
+; CHECK-NEXT: [[PAYLOAD:%.*]] = trunc nuw nsw i64 [[SHIFT]] to i32
+; CHECK-NEXT: [[FINAL:%.*]] = select i1 [[TAG]], i32 [[PAYLOAD]], i32 0
+; CHECK-NEXT: ret i32 [[FINAL]]
+;
+start:
+ %eq = icmp eq i32 %n, 0
+ ; Err(X::_1) -> 0x00000001_00000001, Err(X::_2) -> 0x00000002_00000001
+ %inner = select i1 %eq, i64 4294967297, i64 8589934593
+ ; Ok(()) -> 0
+ %outer = select i1 %b, i64 0, i64 %inner
+ %tag = trunc i64 %outer to i1
+ %shift = lshr i64 %outer, 32
+ %payload = trunc i64 %shift to i32
+ %final = select i1 %tag, i32 %payload, i32 0
+ ret i32 %final
+}
+
+define i32 @lshr_through_multi_use_nested_select(i1 %c1, i1 %c2, ptr %sink) {
+; CHECK-LABEL: @lshr_through_multi_use_nested_select(
+; CHECK-NEXT: [[INNER:%.*]] = select i1 [[C1:%.*]], i64 4294967297, i64 8589934593
+; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C2:%.*]], i64 0, i64 [[INNER]]
+; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
+; CHECK-NEXT: [[S:%.*]] = lshr i64 [[OUTER]], 32
+; CHECK-NEXT: [[T:%.*]] = trunc nuw nsw i64 [[S]] to i32
+; CHECK-NEXT: ret i32 [[T]]
+;
+ %inner = select i1 %c1, i64 4294967297, i64 8589934593
+ %outer = select i1 %c2, i64 0, i64 %inner
+ store i64 %outer, ptr %sink
+ %s = lshr i64 %outer, 32
+ %t = trunc i64 %s to i32
+ ret i32 %t
+}
+
+define i64 @and_through_multi_use_nested_select(i1 %c1, i1 %c2, ptr %sink) {
+; CHECK-LABEL: @and_through_multi_use_nested_select(
+; CHECK-NEXT: [[INNER:%.*]] = select i1 [[C1:%.*]], i64 4294967297, i64 8589934595
+; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C2:%.*]], i64 0, i64 [[INNER]]
+; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
+; CHECK-NEXT: [[M:%.*]] = and i64 [[OUTER]], 3
+; CHECK-NEXT: ret i64 [[M]]
+;
+ %inner = select i1 %c1, i64 4294967297, i64 8589934595
+ %outer = select i1 %c2, i64 0, i64 %inner
+ store i64 %outer, ptr %sink
+ %m = and i64 %outer, 4294967295
+ ret i64 %m
+}
+
+define i64 @or_through_multi_use_nested_select(i1 %c1, i1 %c2, ptr %sink) {
+; CHECK-LABEL: @or_through_multi_use_nested_select(
+; CHECK-NEXT: [[INNER:%.*]] = select i1 [[C1:%.*]], i64 10, i64 20
+; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C2:%.*]], i64 0, i64 [[INNER]]
+; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
+; CHECK-NEXT: [[M:%.*]] = or i64 [[OUTER]], 3
+; CHECK-NEXT: ret i64 [[M]]
+;
+ %inner = select i1 %c1, i64 10, i64 20
+ %outer = select i1 %c2, i64 0, i64 %inner
+ store i64 %outer, ptr %sink
+ %m = or i64 %outer, 3
+ ret i64 %m
+}
+
+define i64 @add_through_multi_use_nested_select(i1 %c1, i1 %c2, ptr %sink) {
+; CHECK-LABEL: @add_through_multi_use_nested_select(
+; CHECK-NEXT: [[INNER:%.*]] = select i1 [[C1:%.*]], i64 100, i64 200
+; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C2:%.*]], i64 0, i64 [[INNER]]
+; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
+; CHECK-NEXT: [[M:%.*]] = add nuw nsw i64 [[OUTER]], 42
+; CHECK-NEXT: ret i64 [[M]]
+;
+ %inner = select i1 %c1, i64 100, i64 200
+ %outer = select i1 %c2, i64 0, i64 %inner
+ store i64 %outer, ptr %sink
+ %m = add i64 %outer, 42
+ ret i64 %m
+}
+
+define i32 @lshr_const_on_true_arm(i1 %c1, i1 %c2, ptr %sink) {
+; CHECK-LABEL: @lshr_const_on_true_arm(
+; CHECK-NEXT: [[INNER:%.*]] = select i1 [[C1:%.*]], i64 4294967297, i64 8589934593
+; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C2:%.*]], i64 [[INNER]], i64 0
+; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
+; CHECK-NEXT: [[S:%.*]] = lshr i64 [[OUTER]], 32
+; CHECK-NEXT: [[T:%.*]] = trunc nuw nsw i64 [[S]] to i32
+; CHECK-NEXT: ret i32 [[T]]
+;
+ %inner = select i1 %c1, i64 4294967297, i64 8589934593
+ %outer = select i1 %c2, i64 %inner, i64 0
+ store i64 %outer, ptr %sink
+ %s = lshr i64 %outer, 32
+ %t = trunc i64 %s to i32
+ ret i32 %t
+}
+
+define i64 @binop_both_arms_constant_multi_use(i1 %c, ptr %sink) {
+; CHECK-LABEL: @binop_both_arms_constant_multi_use(
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C:%.*]], i64 100, i64 200
+; CHECK-NEXT: store i64 [[SEL]], ptr [[SINK:%.*]], align 8
+; CHECK-NEXT: [[M:%.*]] = add nuw nsw i64 [[SEL]], 5
+; CHECK-NEXT: ret i64 [[M]]
+;
+ %sel = select i1 %c, i64 100, i64 200
+ store i64 %sel, ptr %sink
+ %m = add i64 %sel, 5
+ ret i64 %m
+}
+
+define i64 @nested3_select(i1 %c1, i1 %c2, i1 %c3, ptr %sink) {
+; CHECK-LABEL: @nested3_select(
+; CHECK-NEXT: [[S1:%.*]] = select i1 [[C1:%.*]], i64 10, i64 20
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C3:%.*]], i1 true, i1 [[C2:%.*]]
+; CHECK-NEXT: [[S3:%.*]] = select i1 [[TMP1]], i64 0, i64 [[S1]]
+; CHECK-NEXT: store i64 [[S3]], ptr [[SINK:%.*]], align 8
+; CHECK-NEXT: ret i64 [[S3]]
+;
+ %s1 = select i1 %c1, i64 10, i64 20
+ %s2 = select i1 %c2, i64 0, i64 %s1
+ %s3 = select i1 %c3, i64 0, i64 %s2
+ store i64 %s3, ptr %sink
+ %m = and i64 %s3, 255
+ ret i64 %m
+}
+
+; Negative: binop other operand is not a constant
+define i64 @neg_binop_other_operand_not_const(i1 %c1, i1 %c2, i64 %x, ptr %sink) {
+; CHECK-LABEL: @neg_binop_other_operand_not_const(
+; CHECK-NEXT: [[INNER:%.*]] = select i1 [[C1:%.*]], i64 100, i64 200
+; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C2:%.*]], i64 0, i64 [[INNER]]
+; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
+; CHECK-NEXT: [[M:%.*]] = add i64 [[OUTER]], [[X:%.*]]
+; CHECK-NEXT: ret i64 [[M]]
+;
+ %inner = select i1 %c1, i64 100, i64 200
+ %outer = select i1 %c2, i64 0, i64 %inner
+ store i64 %outer, ptr %sink
+ %m = add i64 %outer, %x
+ ret i64 %m
+}
+
+; Negative: neither arm of the multi-use select is a constant
+define i64 @neg_no_const_arm(i1 %c, i64 %x, i64 %y, ptr %sink) {
+; CHECK-LABEL: @neg_no_const_arm(
+; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C:%.*]], i64 [[X:%.*]], i64 [[Y:%.*]]
+; CHECK-NEXT: store i64 [[SEL]], ptr [[SINK:%.*]], align 8
+; CHECK-NEXT: [[M:%.*]] = add i64 [[SEL]], 42
+; CHECK-NEXT: ret i64 [[M]]
+;
+ %sel = select i1 %c, i64 %x, i64 %y
+ store i64 %sel, ptr %sink
+ %m = add i64 %sel, 42
+ ret i64 %m
+}
+
+; Negative: variable arm of outer select is not a const-select
+define i64 @neg_variable_arm_not_select(i1 %c, i64 %x, ptr %sink) {
+; CHECK-LABEL: @neg_variable_arm_not_select(
+; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C:%.*]], i64 0, i64 [[X:%.*]]
+; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
+; CHECK-NEXT: [[M:%.*]] = add i64 [[OUTER]], 42
+; CHECK-NEXT: ret i64 [[M]]
+;
+ %outer = select i1 %c, i64 0, i64 %x
+ store i64 %outer, ptr %sink
+ %m = add i64 %outer, 42
+ ret i64 %m
+}
>From ebf4b1469291a59642f96c1da9cfff25cbc0c817 Mon Sep 17 00:00:00 2001
From: Iris Shi <0.0 at owo.li>
Date: Sat, 9 May 2026 00:42:27 +0800
Subject: [PATCH 2/2] [InstCombine] Fold binop into multi-use select when one
select arm and the other operand are constant
---
.../InstCombine/InstructionCombining.cpp | 12 ++++++--
.../Transforms/InstCombine/binop-select.ll | 2 +-
.../Transforms/InstCombine/extractelement.ll | 5 +---
.../fold-multi-use-select-packed-constants.ll | 30 +++++++++----------
llvm/test/Transforms/InstCombine/pr63791.ll | 2 +-
llvm/test/Transforms/InstCombine/pr72433.ll | 9 ++----
llvm/test/Transforms/InstCombine/pr80597.ll | 9 ++----
llvm/test/Transforms/InstCombine/shift.ll | 6 ++--
8 files changed, 35 insertions(+), 40 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index a09f881e06595..27ff708521385 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2309,8 +2309,16 @@ Instruction *InstCombinerImpl::foldBinopWithPhiOperands(BinaryOperator &BO) {
Instruction *InstCombinerImpl::foldBinOpIntoSelectOrPhi(BinaryOperator &I) {
auto TryFoldOperand = [&](unsigned OpIdx,
bool IsOtherParamConst) -> Instruction * {
- if (auto *Sel = dyn_cast<SelectInst>(I.getOperand(OpIdx)))
- return FoldOpIntoSelect(I, Sel, false, !IsOtherParamConst);
+ if (auto *Sel = dyn_cast<SelectInst>(I.getOperand(OpIdx))) {
+ // Fold binop(select(c, C, x), C2) => select(c, C3, binop(x, C2))
+ // when the select has multiple uses, as long as at least one
+ // arm and the binop operand are both constants.
+ bool MultiUse = IsOtherParamConst && !Sel->hasOneUser() &&
+ (isa<Constant>(Sel->getTrueValue()) ||
+ isa<Constant>(Sel->getFalseValue()));
+ return FoldOpIntoSelect(I, Sel, /*FoldWithMultiUse=*/MultiUse,
+ !IsOtherParamConst);
+ }
if (auto *PN = dyn_cast<PHINode>(I.getOperand(OpIdx)))
return foldOpIntoPhi(I, PN);
return nullptr;
diff --git a/llvm/test/Transforms/InstCombine/binop-select.ll b/llvm/test/Transforms/InstCombine/binop-select.ll
index 3265ea54a831e..3efeba83d12ef 100644
--- a/llvm/test/Transforms/InstCombine/binop-select.ll
+++ b/llvm/test/Transforms/InstCombine/binop-select.ll
@@ -274,7 +274,7 @@ define i32 @and_sel_op0_use(i1 %b) {
; CHECK-LABEL: @and_sel_op0_use(
; CHECK-NEXT: [[S:%.*]] = select i1 [[B:%.*]], i32 25, i32 0
; CHECK-NEXT: call void @use(i32 [[S]])
-; CHECK-NEXT: [[R:%.*]] = and i32 [[S]], 1
+; CHECK-NEXT: [[R:%.*]] = zext i1 [[B]] to i32
; CHECK-NEXT: ret i32 [[R]]
;
%s = select i1 %b, i32 25, i32 0
diff --git a/llvm/test/Transforms/InstCombine/extractelement.ll b/llvm/test/Transforms/InstCombine/extractelement.ll
index 04a35e19fb0bb..6a5a1ab7e98a2 100644
--- a/llvm/test/Transforms/InstCombine/extractelement.ll
+++ b/llvm/test/Transforms/InstCombine/extractelement.ll
@@ -799,10 +799,7 @@ define i32 @extelt_vecselect_const_operand_vector(<3 x i1> %c) {
define i32 @extelt_select_const_operand_extractelt_use(i1 %c) {
; ANY-LABEL: @extelt_select_const_operand_extractelt_use(
-; ANY-NEXT: [[E:%.*]] = select i1 [[C:%.*]], i32 4, i32 7
-; ANY-NEXT: [[M:%.*]] = shl nuw nsw i32 [[E]], 1
-; ANY-NEXT: [[M_2:%.*]] = shl nuw nsw i32 [[E]], 2
-; ANY-NEXT: [[R:%.*]] = mul nuw nsw i32 [[M]], [[M_2]]
+; ANY-NEXT: [[R:%.*]] = select i1 [[C:%.*]], i32 128, i32 392
; ANY-NEXT: ret i32 [[R]]
;
%s = select i1 %c, <3 x i32> <i32 2, i32 3, i32 4>, <3 x i32> <i32 5, i32 6, i32 7>
diff --git a/llvm/test/Transforms/InstCombine/fold-multi-use-select-packed-constants.ll b/llvm/test/Transforms/InstCombine/fold-multi-use-select-packed-constants.ll
index 32999f3f3e278..3256ad67b0768 100644
--- a/llvm/test/Transforms/InstCombine/fold-multi-use-select-packed-constants.ll
+++ b/llvm/test/Transforms/InstCombine/fold-multi-use-select-packed-constants.ll
@@ -8,12 +8,8 @@ define noundef i32 @rust_155985(i32 noundef %n, i1 noundef zeroext %b) {
; CHECK-LABEL: @rust_155985(
; CHECK-NEXT: start:
; CHECK-NEXT: [[EQ:%.*]] = icmp eq i32 [[N:%.*]], 0
-; CHECK-NEXT: [[INNER:%.*]] = select i1 [[EQ]], i64 4294967297, i64 8589934593
-; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[B:%.*]], i64 0, i64 [[INNER]]
-; CHECK-NEXT: [[TAG:%.*]] = trunc i64 [[OUTER]] to i1
-; CHECK-NEXT: [[SHIFT:%.*]] = lshr i64 [[OUTER]], 32
-; CHECK-NEXT: [[PAYLOAD:%.*]] = trunc nuw nsw i64 [[SHIFT]] to i32
-; CHECK-NEXT: [[FINAL:%.*]] = select i1 [[TAG]], i32 [[PAYLOAD]], i32 0
+; CHECK-NEXT: [[TMP0:%.*]] = select i1 [[EQ]], i32 1, i32 2
+; CHECK-NEXT: [[FINAL:%.*]] = select i1 [[B:%.*]], i32 0, i32 [[TMP0]]
; CHECK-NEXT: ret i32 [[FINAL]]
;
start:
@@ -34,8 +30,8 @@ define i32 @lshr_through_multi_use_nested_select(i1 %c1, i1 %c2, ptr %sink) {
; CHECK-NEXT: [[INNER:%.*]] = select i1 [[C1:%.*]], i64 4294967297, i64 8589934593
; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C2:%.*]], i64 0, i64 [[INNER]]
; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
-; CHECK-NEXT: [[S:%.*]] = lshr i64 [[OUTER]], 32
-; CHECK-NEXT: [[T:%.*]] = trunc nuw nsw i64 [[S]] to i32
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C1]], i32 1, i32 2
+; CHECK-NEXT: [[T:%.*]] = select i1 [[C2]], i32 0, i32 [[TMP1]]
; CHECK-NEXT: ret i32 [[T]]
;
%inner = select i1 %c1, i64 4294967297, i64 8589934593
@@ -51,7 +47,8 @@ define i64 @and_through_multi_use_nested_select(i1 %c1, i1 %c2, ptr %sink) {
; CHECK-NEXT: [[INNER:%.*]] = select i1 [[C1:%.*]], i64 4294967297, i64 8589934595
; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C2:%.*]], i64 0, i64 [[INNER]]
; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
-; CHECK-NEXT: [[M:%.*]] = and i64 [[OUTER]], 3
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C1]], i64 1, i64 3
+; CHECK-NEXT: [[M:%.*]] = select i1 [[C2]], i64 0, i64 [[TMP1]]
; CHECK-NEXT: ret i64 [[M]]
;
%inner = select i1 %c1, i64 4294967297, i64 8589934595
@@ -66,7 +63,8 @@ define i64 @or_through_multi_use_nested_select(i1 %c1, i1 %c2, ptr %sink) {
; CHECK-NEXT: [[INNER:%.*]] = select i1 [[C1:%.*]], i64 10, i64 20
; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C2:%.*]], i64 0, i64 [[INNER]]
; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
-; CHECK-NEXT: [[M:%.*]] = or i64 [[OUTER]], 3
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C1]], i64 11, i64 23
+; CHECK-NEXT: [[M:%.*]] = select i1 [[C2]], i64 3, i64 [[TMP1]]
; CHECK-NEXT: ret i64 [[M]]
;
%inner = select i1 %c1, i64 10, i64 20
@@ -81,7 +79,8 @@ define i64 @add_through_multi_use_nested_select(i1 %c1, i1 %c2, ptr %sink) {
; CHECK-NEXT: [[INNER:%.*]] = select i1 [[C1:%.*]], i64 100, i64 200
; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C2:%.*]], i64 0, i64 [[INNER]]
; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
-; CHECK-NEXT: [[M:%.*]] = add nuw nsw i64 [[OUTER]], 42
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C1]], i64 142, i64 242
+; CHECK-NEXT: [[M:%.*]] = select i1 [[C2]], i64 42, i64 [[TMP1]]
; CHECK-NEXT: ret i64 [[M]]
;
%inner = select i1 %c1, i64 100, i64 200
@@ -96,8 +95,8 @@ define i32 @lshr_const_on_true_arm(i1 %c1, i1 %c2, ptr %sink) {
; CHECK-NEXT: [[INNER:%.*]] = select i1 [[C1:%.*]], i64 4294967297, i64 8589934593
; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C2:%.*]], i64 [[INNER]], i64 0
; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
-; CHECK-NEXT: [[S:%.*]] = lshr i64 [[OUTER]], 32
-; CHECK-NEXT: [[T:%.*]] = trunc nuw nsw i64 [[S]] to i32
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[C1]], i32 1, i32 2
+; CHECK-NEXT: [[T:%.*]] = select i1 [[C2]], i32 [[TMP1]], i32 0
; CHECK-NEXT: ret i32 [[T]]
;
%inner = select i1 %c1, i64 4294967297, i64 8589934593
@@ -112,7 +111,7 @@ define i64 @binop_both_arms_constant_multi_use(i1 %c, ptr %sink) {
; CHECK-LABEL: @binop_both_arms_constant_multi_use(
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C:%.*]], i64 100, i64 200
; CHECK-NEXT: store i64 [[SEL]], ptr [[SINK:%.*]], align 8
-; CHECK-NEXT: [[M:%.*]] = add nuw nsw i64 [[SEL]], 5
+; CHECK-NEXT: [[M:%.*]] = select i1 [[C]], i64 105, i64 205
; CHECK-NEXT: ret i64 [[M]]
;
%sel = select i1 %c, i64 100, i64 200
@@ -172,7 +171,8 @@ define i64 @neg_variable_arm_not_select(i1 %c, i64 %x, ptr %sink) {
; CHECK-LABEL: @neg_variable_arm_not_select(
; CHECK-NEXT: [[OUTER:%.*]] = select i1 [[C:%.*]], i64 0, i64 [[X:%.*]]
; CHECK-NEXT: store i64 [[OUTER]], ptr [[SINK:%.*]], align 8
-; CHECK-NEXT: [[M:%.*]] = add i64 [[OUTER]], 42
+; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[X]], 42
+; CHECK-NEXT: [[M:%.*]] = select i1 [[C]], i64 42, i64 [[TMP1]]
; CHECK-NEXT: ret i64 [[M]]
;
%outer = select i1 %c, i64 0, i64 %x
diff --git a/llvm/test/Transforms/InstCombine/pr63791.ll b/llvm/test/Transforms/InstCombine/pr63791.ll
index 73a559f989261..6f1fb02fb12cb 100644
--- a/llvm/test/Transforms/InstCombine/pr63791.ll
+++ b/llvm/test/Transforms/InstCombine/pr63791.ll
@@ -17,7 +17,7 @@ define void @y() {
; CHECK: for.cond5.preheader.i:
; CHECK-NEXT: br i1 true, label [[FOR_COND1_LOOPEXIT_I:%.*]], label [[FOR_INC19_I:%.*]]
; CHECK: for.inc19.i:
-; CHECK-NEXT: br i1 poison, label [[FOR_INC19_I]], label [[FOR_COND1_LOOPEXIT_I]]
+; CHECK-NEXT: br i1 false, label [[FOR_INC19_I]], label [[FOR_COND1_LOOPEXIT_I]]
;
entry:
br label %for.cond.i
diff --git a/llvm/test/Transforms/InstCombine/pr72433.ll b/llvm/test/Transforms/InstCombine/pr72433.ll
index c6e74582a13d3..b5eafb7fed86f 100644
--- a/llvm/test/Transforms/InstCombine/pr72433.ll
+++ b/llvm/test/Transforms/InstCombine/pr72433.ll
@@ -5,13 +5,8 @@ define i32 @widget(i32 %arg, i32 %arg1) {
; CHECK-LABEL: define i32 @widget(
; CHECK-SAME: i32 [[ARG:%.*]], i32 [[ARG1:%.*]]) {
; CHECK-NEXT: bb:
-; CHECK-NEXT: [[ICMP:%.*]] = icmp ne i32 [[ARG]], 0
-; CHECK-NEXT: [[TMP0:%.*]] = zext i1 [[ICMP]] to i32
-; CHECK-NEXT: [[MUL:%.*]] = shl nuw nsw i32 20, [[TMP0]]
-; CHECK-NEXT: [[XOR:%.*]] = zext i1 [[ICMP]] to i32
-; CHECK-NEXT: [[ADD9:%.*]] = or disjoint i32 [[MUL]], [[XOR]]
-; CHECK-NEXT: [[TMP1:%.*]] = zext i1 [[ICMP]] to i32
-; CHECK-NEXT: [[MUL2:%.*]] = shl nuw nsw i32 [[ADD9]], [[TMP1]]
+; CHECK-NEXT: [[ICMP_NOT:%.*]] = icmp eq i32 [[ARG]], 0
+; CHECK-NEXT: [[MUL2:%.*]] = select i1 [[ICMP_NOT]], i32 20, i32 82
; CHECK-NEXT: ret i32 [[MUL2]]
;
bb:
diff --git a/llvm/test/Transforms/InstCombine/pr80597.ll b/llvm/test/Transforms/InstCombine/pr80597.ll
index 148da056486f9..bf536b9ecd133 100644
--- a/llvm/test/Transforms/InstCombine/pr80597.ll
+++ b/llvm/test/Transforms/InstCombine/pr80597.ll
@@ -5,14 +5,9 @@ define i64 @pr80597(i1 %cond) {
; CHECK-LABEL: define i64 @pr80597(
; CHECK-SAME: i1 [[COND:%.*]]) {
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[ADD:%.*]] = select i1 [[COND]], i64 0, i64 -12884901888
-; CHECK-NEXT: [[SEXT1:%.*]] = add nsw i64 [[ADD]], 8836839514384105472
-; CHECK-NEXT: [[CMP:%.*]] = icmp ult i64 [[SEXT1]], -34359738368
-; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
+; CHECK-NEXT: br i1 true, label [[IF_THEN:%.*]], label [[IF_ELSE:%.*]]
; CHECK: if.else:
-; CHECK-NEXT: [[SEXT2:%.*]] = ashr exact i64 [[ADD]], 1
-; CHECK-NEXT: [[ASHR:%.*]] = or disjoint i64 [[SEXT2]], 4418419761487020032
-; CHECK-NEXT: ret i64 [[ASHR]]
+; CHECK-NEXT: ret i64 poison
; CHECK: if.then:
; CHECK-NEXT: ret i64 0
;
diff --git a/llvm/test/Transforms/InstCombine/shift.ll b/llvm/test/Transforms/InstCombine/shift.ll
index 3a9e756bafc36..f9be88a5b2f93 100644
--- a/llvm/test/Transforms/InstCombine/shift.ll
+++ b/llvm/test/Transforms/InstCombine/shift.ll
@@ -1779,11 +1779,11 @@ define void @ashr_out_of_range_1(ptr %A) {
; CHECK-NEXT: [[L_FROZEN:%.*]] = freeze i177 [[L]]
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i177 [[L_FROZEN]], -1
; CHECK-NEXT: [[TMP2:%.*]] = trunc i177 [[L_FROZEN]] to i64
-; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP1]], i64 0, i64 [[TMP2]]
+; CHECK-NEXT: [[TMP7:%.*]] = add i64 [[TMP2]], -1
+; CHECK-NEXT: [[TMP3:%.*]] = select i1 [[TMP1]], i64 -1, i64 [[TMP7]]
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr [24 x i8], ptr [[A]], i64 [[TMP3]]
-; CHECK-NEXT: [[G11:%.*]] = getelementptr i8, ptr [[TMP4]], i64 -24
; CHECK-NEXT: [[TMP5:%.*]] = sext i1 [[TMP1]] to i64
-; CHECK-NEXT: [[G62:%.*]] = getelementptr [24 x i8], ptr [[G11]], i64 [[TMP5]]
+; CHECK-NEXT: [[G62:%.*]] = getelementptr [24 x i8], ptr [[TMP4]], i64 [[TMP5]]
; CHECK-NEXT: [[TMP6:%.*]] = icmp eq i177 [[L_FROZEN]], -1
; CHECK-NEXT: [[B28:%.*]] = select i1 [[TMP6]], i177 0, i177 [[L_FROZEN]]
; CHECK-NEXT: store i177 [[B28]], ptr [[G62]], align 4
More information about the llvm-commits
mailing list