[llvm] [test][AggressiveInstCombine] Test type legality handling in `TruncInstCombine` (NFC) (PR #222930)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 11 04:37:58 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Ömer Sinan Ağacan (osa1)
<details>
<summary>Changes</summary>
I noticed that this block of code that checks type legality in `TruncInstCombine::getMinBitWidth` is not tested:
```
else { // MinBitWidth == TruncBitWidth
// In this case the expression can be evaluated with the trunc instruction
// destination type, and trunc instruction can be omitted. However, we
// should not perform the evaluation if the original type is a legal scalar
// type and the target type is illegal.
bool FromLegal = MinBitWidth == 1 || DL.isLegalInteger(OrigBitWidth);
bool ToLegal = MinBitWidth == 1 || DL.isLegalInteger(MinBitWidth);
if (!DstTy->isVectorTy() && FromLegal && !ToLegal)
return OrigBitWidth;
}
```
This commit adds tests.
---
Full diff: https://github.com/llvm/llvm-project/pull/222930.diff
1 Files Affected:
- (added) llvm/test/Transforms/AggressiveInstCombine/trunc_illegal_type.ll (+61)
``````````diff
diff --git a/llvm/test/Transforms/AggressiveInstCombine/trunc_illegal_type.ll b/llvm/test/Transforms/AggressiveInstCombine/trunc_illegal_type.ll
new file mode 100644
index 0000000000000..c643006d2a161
--- /dev/null
+++ b/llvm/test/Transforms/AggressiveInstCombine/trunc_illegal_type.ll
@@ -0,0 +1,61 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=aggressive-instcombine -S | FileCheck %s
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+
+; Check type legality handling in `TruncInstCombine::getMinBitWidth`.
+
+; Illegal truncate target, don't reduce.
+define i24 @illegal_dst_legal_src(i24 %a, i24 %b) {
+; CHECK-LABEL: @illegal_dst_legal_src(
+; CHECK-NEXT: [[ZA:%.*]] = zext i24 [[A:%.*]] to i32
+; CHECK-NEXT: [[ZB:%.*]] = zext i24 [[B:%.*]] to i32
+; CHECK-NEXT: [[ADD:%.*]] = add i32 [[ZA]], [[ZB]]
+; CHECK-NEXT: [[T:%.*]] = trunc i32 [[ADD]] to i24
+; CHECK-NEXT: ret i24 [[T]]
+;
+ %za = zext i24 %a to i32
+ %zb = zext i24 %b to i32
+ %add = add i32 %za, %zb
+ %t = trunc i32 %add to i24
+ ret i24 %t
+}
+
+; Source and target types are both legal, reduce.
+define i16 @legal_dst_legal_src(i16 %a, i16 %b) {
+; CHECK-LABEL: @legal_dst_legal_src(
+; CHECK-NEXT: [[ADD:%.*]] = add i16 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: ret i16 [[ADD]]
+;
+ %za = zext i16 %a to i32
+ %zb = zext i16 %b to i32
+ %add = add i32 %za, %zb
+ %t = trunc i32 %add to i16
+ ret i16 %t
+}
+
+; Both source and target are illegal, reduce.
+define i24 @illegal_dst_illegal_src(i24 %a, i24 %b) {
+; CHECK-LABEL: @illegal_dst_illegal_src(
+; CHECK-NEXT: [[ADD:%.*]] = add i24 [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: ret i24 [[ADD]]
+;
+ %za = zext i24 %a to i48
+ %zb = zext i24 %b to i48
+ %add = add i48 %za, %zb
+ %t = trunc i48 %add to i24
+ ret i24 %t
+}
+
+; The legality check only applies to scalars, vectors are still reduced.
+define <4 x i24> @illegal_dst_legal_src_vec(<4 x i24> %a, <4 x i24> %b) {
+; CHECK-LABEL: @illegal_dst_legal_src_vec(
+; CHECK-NEXT: [[ADD:%.*]] = add <4 x i24> [[A:%.*]], [[B:%.*]]
+; CHECK-NEXT: ret <4 x i24> [[ADD]]
+;
+ %za = zext <4 x i24> %a to <4 x i32>
+ %zb = zext <4 x i24> %b to <4 x i32>
+ %add = add <4 x i32> %za, %zb
+ %t = trunc <4 x i32> %add to <4 x i24>
+ ret <4 x i24> %t
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/222930
More information about the llvm-commits
mailing list