[llvm] [InstCombine] fold `select (trunc nuw X to i1), X, Y` to `select (trunc nuw X to i1), 1, Y` (PR #105914)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 23 18:52:15 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: None (c8ef)
<details>
<summary>Changes</summary>
This patch adds a fold from `select (trunc nuw X to i1), X, Y` to `select (trunc nuw X to i1), 1, Y`. Considering the semantics of the nuw flag, if there is no poison value in this expression, this code assumes that X can only be 0 or 1.
Partially resolved: #<!-- -->96765
---
Full diff: https://github.com/llvm/llvm-project/pull/105914.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp (+10)
- (added) llvm/test/Transforms/InstCombine/fold-select-trunc-if-nuw.ll (+35)
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 18ffc209f259e0..49582004d9a4f6 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -4201,5 +4201,15 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
}
}
+ // select (trunc nuw X to i1), X, Y --> select (trunc nuw X to i1), 1, Y
+ if (auto *TI = dyn_cast<TruncInst>(CondVal)) {
+ Value *Trunc;
+ if (TI->hasNoUnsignedWrap() && match(CondVal, m_Trunc(m_Value(Trunc))) &&
+ match(TrueVal, m_Specific(Trunc))) {
+ return SelectInst::Create(
+ CondVal, ConstantInt::get(TrueVal->getType(), 1), FalseVal);
+ }
+ }
+
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/fold-select-trunc-if-nuw.ll b/llvm/test/Transforms/InstCombine/fold-select-trunc-if-nuw.ll
new file mode 100644
index 00000000000000..09fb2d77f9ef56
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fold-select-trunc-if-nuw.ll
@@ -0,0 +1,35 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=instcombine -S | FileCheck %s
+
+define i8 @fold_select_trunc_if_nuw(i8 %x, i8 %y) {
+; CHECK-LABEL: @fold_select_trunc_if_nuw(
+; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw i8 [[X:%.*]] to i1
+; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i8 1, i8 [[Y:%.*]]
+; CHECK-NEXT: ret i8 [[RET]]
+;
+ %trunc = trunc nuw i8 %x to i1
+ %ret = select i1 %trunc, i8 %x, i8 %y
+ ret i8 %ret
+}
+
+define i8 @fold_select_trunc_if_nuw_negative(i8 %x, i8 %y) {
+; CHECK-LABEL: @fold_select_trunc_if_nuw_negative(
+; CHECK-NEXT: [[TRUNC:%.*]] = trunc nsw i8 [[X:%.*]] to i1
+; CHECK-NEXT: [[RET:%.*]] = select i1 [[TRUNC]], i8 [[X]], i8 [[Y:%.*]]
+; CHECK-NEXT: ret i8 [[RET]]
+;
+ %trunc = trunc nsw i8 %x to i1
+ %ret = select i1 %trunc, i8 %x, i8 %y
+ ret i8 %ret
+}
+
+define <2 x i8> @fold_select_trunc_if_nuw_vector(<2 x i8> %x, <2 x i8> %y) {
+; CHECK-LABEL: @fold_select_trunc_if_nuw_vector(
+; CHECK-NEXT: [[TRUNC:%.*]] = trunc nuw <2 x i8> [[X:%.*]] to <2 x i1>
+; CHECK-NEXT: [[RET:%.*]] = select <2 x i1> [[TRUNC]], <2 x i8> <i8 1, i8 1>, <2 x i8> [[Y:%.*]]
+; CHECK-NEXT: ret <2 x i8> [[RET]]
+;
+ %trunc = trunc nuw <2 x i8> %x to <2 x i1>
+ %ret = select <2 x i1> %trunc, <2 x i8> %x, <2 x i8> %y
+ ret <2 x i8> %ret
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/105914
More information about the llvm-commits
mailing list