Add support for select instructions in the sequences that may be optimized in the AggressiveInstCombine pass.
Details
Diff Detail
Event Timeline
Verify that the translation is correct, but I don't think this patch is safe:
define i16 @cmp_select_sext_const(i8 %a) { %conv = sext i8 %a to i32 %cmp = icmp slt i32 %conv, 32768 %cond = select i1 %cmp, i32 32768, i32 %conv %conv4 = trunc i32 %cond to i16 ret i16 %conv4 } $ opt -aggressive-instcombine agg.ll -S define i16 @cmp_select_sext_const(i8 %a) { %conv = sext i8 %a to i16 %cmp = icmp slt i16 %conv, -32768 %cond = select i1 %cmp, i16 -32768, i16 %conv ret i16 %cond }
Fix @spatel's test case. Thanks for the review.
Involve constants in cmp operations in the MinBitWidth calculations.
Is it possible to split this into a patch that adds just the select handling, and another that extends the icmp support? It seems like the select part should be testable independently, as long as you don't derive the select condition from the select operands.
Following @nikic's comment:
I agree, the patch is missing some select exclusive tests.
Adding such test cases.
IMHO, there is no added value to splitting to 2 patches, the select changed are too obvious, and the compare is strongly related to the select changes.
That's kind of the point: The select change is obvious and can land right away, the icmp part is trickier.
I'm not really familiar with this code, but coupling the icmp and the select handling seems like a bit of a hack: The transform is also possible if the select condition is unrelated to the trunc chain, and the transform is also possible if there is an icmp dependent on the trunc chain but not used in a select condition. I think the more principled approach would be to extend the code in https://github.com/llvm/llvm-project/blob/0e0c65264aeb6f66b6f711884c55cdbf66d975f6/llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp#L241-L256 to collect the extra use icmp.
+1. Minimal patches are easier to review/debug, and we've already shown that this patch has tricky edge cases (and I don't know this code very well either). Also, please push the test file with baseline CHECKs as a preliminary commit.
llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp | ||
---|---|---|
225–243 | Why is this being changed? |
llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp | ||
---|---|---|
225–243 | The max operation is not needed here. |
llvm/lib/Transforms/AggressiveInstCombine/TruncInstCombine.cpp | ||
---|---|---|
225–243 | Can you submit that as a separate change then please? |
Style nit: Omit {} if not necessary.