Skip to content

Commit 8936a8e

Browse files
committed
[InstCombine] Canonicalize extractvalue + select
1 parent fe4a864 commit 8936a8e

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3834,6 +3834,12 @@ Instruction *InstCombinerImpl::visitExtractValueInst(ExtractValueInst &EV) {
38343834
if (Instruction *Res = foldOpIntoPhi(EV, PN))
38353835
return Res;
38363836

3837+
// Canonicalize extract (select Cond, TV, FV)
3838+
// -> select cond, (extract TV), (extract FV)
3839+
if (auto *SI = dyn_cast<SelectInst>(Agg))
3840+
if (Instruction *R = FoldOpIntoSelect(EV, SI, /*FoldWithMultiUse=*/true))
3841+
return R;
3842+
38373843
// We could simplify extracts from other values. Note that nested extracts may
38383844
// already be simplified implicitly by the above: extract (extract (insert) )
38393845
// will be translated into extract ( insert ( extract ) ) first and then just

llvm/test/Transforms/InstCombine/extract-select-agg.ll

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ define i64 @test_select_agg_constant_agg(i64 %val, i1 %cond) {
55
; CHECK-LABEL: define i64 @test_select_agg_constant_agg(
66
; CHECK-SAME: i64 [[VAL:%.*]], i1 [[COND:%.*]]) {
77
; CHECK-NEXT: entry:
8-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], { i64, i64 } { i64 1, i64 2 }, { i64, i64 } { i64 0, i64 3 }
9-
; CHECK-NEXT: [[RET:%.*]] = extractvalue { i64, i64 } [[SEL]], 0
8+
; CHECK-NEXT: [[RET:%.*]] = zext i1 [[COND]] to i64
109
; CHECK-NEXT: ret i64 [[RET]]
1110
;
1211
entry:
@@ -19,10 +18,9 @@ define void @test_select_agg_constant_agg_multiuse(i64 %val, i1 %cond) {
1918
; CHECK-LABEL: define void @test_select_agg_constant_agg_multiuse(
2019
; CHECK-SAME: i64 [[VAL:%.*]], i1 [[COND:%.*]]) {
2120
; CHECK-NEXT: entry:
22-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], { i64, i64 } { i64 1, i64 2 }, { i64, i64 } { i64 0, i64 3 }
23-
; CHECK-NEXT: [[RET:%.*]] = extractvalue { i64, i64 } [[SEL]], 0
21+
; CHECK-NEXT: [[RET:%.*]] = zext i1 [[COND]] to i64
2422
; CHECK-NEXT: call void @use(i64 [[RET]])
25-
; CHECK-NEXT: [[V1:%.*]] = extractvalue { i64, i64 } [[SEL]], 1
23+
; CHECK-NEXT: [[V1:%.*]] = select i1 [[COND]], i64 2, i64 3
2624
; CHECK-NEXT: call void @use(i64 [[V1]])
2725
; CHECK-NEXT: ret void
2826
;

0 commit comments

Comments
 (0)