This patch transforms
p = phi [x, y] s = select cond, z, p
with
s = phi[x, z]
if we can prove that the Phi node takes values basing on select's condition.
Differential D82072
[InstCombine] Combine select & Phi by same condition Authored by mkazantsev on Jun 18 2020, 12:45 AM.
Details This patch transforms p = phi [x, y] s = select cond, z, p with s = phi[x, z] if we can prove that the Phi node takes values basing on select's condition.
Diff Detail Event Timeline
Comment Actions I'm probably missing something here, but wouldn't it be possible to phi-translate IfTrue and IfFalse (Inputs[Pred] = IfTrue->DoPHITranslation(BB, Pred) etc) and the existing code would already work? Why perform the conversion to a different select first, rather than going directly to the phi? (That should also be more powerful, because it removes the limitation that you need a single value.)
Comment Actions Thanks for pointing that out, I wasn't aware we already have this API. I'll rework the patch using it. Comment Actions This patch looks like a straightforward extension of the existing code, but I don't have a good sense for control-flow corner cases, so please wait for another LGTM. I think we should have at least one other test that has a switch...something like this: define i32 @select_phi_same_condition_switch(i1 %cond, i32 %x, i32 %y) {
entry:
br i1 %cond, label %if.true, label %if.false
if.true:
switch i32 %x, label %exit [
i32 1, label %merge
i32 2, label %merge
]
exit:
ret i32 0
if.false:
br label %merge
merge:
%phi = phi i32 [0, %if.true], [0, %if.true], [%y, %if.false]
%s = select i1 %cond, i32 %x, i32 %phi
ret i32 %s
}
Comment Actions Two more test cases: define i32 @test1(i1 %cond, i1 %cond2) {
; CHECK-LABEL: @test1(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
; CHECK: if.true:
; CHECK-NEXT: br i1 [[COND2:%.*]], label [[IF_TRUE_1:%.*]], label [[IF_TRUE_2:%.*]]
; CHECK: if.true.1:
; CHECK-NEXT: br label [[MERGE:%.*]]
; CHECK: if.true.2:
; CHECK-NEXT: br label [[MERGE]]
; CHECK: if.false:
; CHECK-NEXT: br label [[MERGE]]
; CHECK: merge:
; CHECK-NEXT: [[S:%.*]] = phi i32 [ 3, [[IF_FALSE]] ], [ 2, [[IF_TRUE_2]] ], [ 1, [[IF_TRUE_1]] ]
; CHECK-NEXT: ret i32 [[S]]
; CHECK: exit:
; CHECK-NEXT: ret i32 0
;
entry:
br i1 %cond, label %if.true, label %if.false
if.true:
br i1 %cond2, label %if.true.1, label %if.true.2
if.true.1:
br label %merge
if.true.2:
br label %merge
if.false:
br label %merge
merge:
%p = phi i32 [ 1, %if.true.1 ], [ 2, %if.true.2 ], [ 4, %if.false ]
%s = select i1 %cond, i32 %p, i32 3
ret i32 %s
exit:
ret i32 0
}This shows that one select operand does not need to have the same incoming values in the phi (a weakness of the previous version of this patch, I believe). define i32 @test2(i1 %cond, i1 %cond2) {
; CHECK-LABEL: @test2(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[COND:%.*]], label [[LOOP:%.*]], label [[EXIT:%.*]]
; CHECK: loop:
; CHECK-NEXT: [[SELECT:%.*]] = phi i32 [ [[IV_INC:%.*]], [[LOOP]] ], [ 0, [[ENTRY:%.*]] ]
; CHECK-NEXT: [[IV_INC]] = add i32 [[SELECT]], 1
; CHECK-NEXT: br i1 [[COND2:%.*]], label [[LOOP]], label [[EXIT2:%.*]]
; CHECK: exit:
; CHECK-NEXT: ret i32 0
; CHECK: exit2:
; CHECK-NEXT: ret i32 [[IV_INC]]
;
entry:
br i1 %cond, label %loop, label %exit
loop:
%iv = phi i32 [ 0, %entry ], [ %iv.inc, %loop ]
%select = select i1 %cond, i32 %iv, i32 -1
%iv.inc = add i32 %select, 1
br i1 %cond2, label %loop, label %exit2
exit:
ret i32 0
exit2:
ret i32 %iv.inc
}This is a degenerate case where everything is dominated by the true edge, and the select is just equal to the phi.
Comment Actions LGTM with test additions.
Comment Actions commit 1eeb7147878edb7c0c0fbf54bc3dffd43db271b8
Author: Max Kazantsev <mkazantsev@azul.com>
Date: Thu Jun 25 10:42:16 2020 +0700
[InstCombine] Combine select & Phi by same condition
This patch transformsp = phi [x, y] s = select cond, z, p ``` with ``` s = phi[x, z] ``` if we can prove that the Phi node takes values basing on select's condition. Differential Revision: https://reviews.llvm.org/D82072 Reviewed By: nikic Extra tests added as commit 4c6548222b3c41d024581d28f42b3f02510bcfe3 [Test] Add more tests for selects & phis | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
It would help to add a code comment and/or example that describes what we are doing in this block.