Skip to content

Commit e808cab

Browse files
author
Max Kazantsev
committed
[InstCombine] Improve select -> phi canonicalization: consider more blocks
We can try to replace select with a Phi not in its parent block alone, but also in blocks of its arguments. We benefit from it when select's argument is a Phi. Differential Revision: https://reviews.llvm.org/D83284 Reviewed By: nikic
1 parent ac8dc52 commit e808cab

2 files changed

Lines changed: 184 additions & 6 deletions

File tree

‎llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2443,11 +2443,11 @@ Instruction *InstCombiner::foldVectorSelect(SelectInst &Sel) {
24432443
return nullptr;
24442444
}
24452445

2446-
static Instruction *foldSelectToPhi(SelectInst &Sel, const DominatorTree &DT,
2447-
InstCombiner::BuilderTy &Builder) {
2446+
static Instruction *foldSelectToPhiImpl(SelectInst &Sel, BasicBlock *BB,
2447+
const DominatorTree &DT,
2448+
InstCombiner::BuilderTy &Builder) {
24482449
// Find the block's immediate dominator that ends with a conditional branch
24492450
// that matches select's condition (maybe inverted).
2450-
BasicBlock *BB = Sel.getParent();
24512451
auto *IDomNode = DT[BB]->getIDom();
24522452
if (!IDomNode)
24532453
return nullptr;
@@ -2500,6 +2500,21 @@ static Instruction *foldSelectToPhi(SelectInst &Sel, const DominatorTree &DT,
25002500
return PN;
25012501
}
25022502

2503+
static Instruction *foldSelectToPhi(SelectInst &Sel, const DominatorTree &DT,
2504+
InstCombiner::BuilderTy &Builder) {
2505+
// Try to replace this select with Phi in one of these blocks.
2506+
SmallSetVector<BasicBlock *, 4> CandidateBlocks;
2507+
CandidateBlocks.insert(Sel.getParent());
2508+
for (Value *V : Sel.operands())
2509+
if (auto *I = dyn_cast<Instruction>(V))
2510+
CandidateBlocks.insert(I->getParent());
2511+
2512+
for (BasicBlock *BB : CandidateBlocks)
2513+
if (auto *PN = foldSelectToPhiImpl(Sel, BB, DT, Builder))
2514+
return PN;
2515+
return nullptr;
2516+
}
2517+
25032518
Instruction *InstCombiner::visitSelectInst(SelectInst &SI) {
25042519
Value *CondVal = SI.getCondition();
25052520
Value *TrueVal = SI.getTrueValue();

‎llvm/test/Transforms/InstCombine/select.ll‎

Lines changed: 166 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,11 +2250,40 @@ define i32 @test_select_into_phi_not_idom(i1 %cond, i32 %A, i32 %B) {
22502250
; CHECK: if.false:
22512251
; CHECK-NEXT: br label [[MERGE]]
22522252
; CHECK: merge:
2253-
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[A:%.*]], [[IF_TRUE]] ], [ [[B:%.*]], [[IF_FALSE]] ]
22542253
; CHECK-NEXT: br label [[EXIT:%.*]]
22552254
; CHECK: exit:
2256-
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[COND]], i32 [[PHI]], i32 [[A]]
2257-
; CHECK-NEXT: ret i32 [[SEL]]
2255+
; CHECK-NEXT: ret i32 [[A:%.*]]
2256+
;
2257+
entry:
2258+
br i1 %cond, label %if.true, label %if.false
2259+
2260+
if.true:
2261+
br label %merge
2262+
2263+
if.false:
2264+
br label %merge
2265+
2266+
merge:
2267+
%phi = phi i32 [%A, %if.true], [%B, %if.false]
2268+
br label %exit
2269+
2270+
exit:
2271+
%sel = select i1 %cond, i32 %phi, i32 %A
2272+
ret i32 %sel
2273+
}
2274+
2275+
define i32 @test_select_into_phi_not_idom_2(i1 %cond, i32 %A, i32 %B) {
2276+
; CHECK-LABEL: @test_select_into_phi_not_idom_2(
2277+
; CHECK-NEXT: entry:
2278+
; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2279+
; CHECK: if.true:
2280+
; CHECK-NEXT: br label [[MERGE:%.*]]
2281+
; CHECK: if.false:
2282+
; CHECK-NEXT: br label [[MERGE]]
2283+
; CHECK: merge:
2284+
; CHECK-NEXT: br label [[EXIT:%.*]]
2285+
; CHECK: exit:
2286+
; CHECK-NEXT: ret i32 [[B:%.*]]
22582287
;
22592288
entry:
22602289
br i1 %cond, label %if.true, label %if.false
@@ -2269,11 +2298,145 @@ merge:
22692298
%phi = phi i32 [%A, %if.true], [%B, %if.false]
22702299
br label %exit
22712300

2301+
exit:
2302+
%sel = select i1 %cond, i32 %B, i32 %phi
2303+
ret i32 %sel
2304+
}
2305+
2306+
define i32 @test_select_into_phi_not_idom_inverted(i1 %cond, i32 %A, i32 %B) {
2307+
; CHECK-LABEL: @test_select_into_phi_not_idom_inverted(
2308+
; CHECK-NEXT: entry:
2309+
; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_FALSE:%.*]], label [[IF_TRUE:%.*]]
2310+
; CHECK: if.true:
2311+
; CHECK-NEXT: br label [[MERGE:%.*]]
2312+
; CHECK: if.false:
2313+
; CHECK-NEXT: br label [[MERGE]]
2314+
; CHECK: merge:
2315+
; CHECK-NEXT: [[SEL:%.*]] = phi i32 [ [[B:%.*]], [[IF_FALSE]] ], [ [[A:%.*]], [[IF_TRUE]] ]
2316+
; CHECK-NEXT: br label [[EXIT:%.*]]
2317+
; CHECK: exit:
2318+
; CHECK-NEXT: ret i32 [[SEL]]
2319+
;
2320+
entry:
2321+
%inverted = xor i1 %cond, 1
2322+
br i1 %inverted, label %if.true, label %if.false
2323+
2324+
if.true:
2325+
br label %merge
2326+
2327+
if.false:
2328+
br label %merge
2329+
2330+
merge:
2331+
%phi = phi i32 [%A, %if.true], [%B, %if.false]
2332+
br label %exit
2333+
2334+
exit:
2335+
%sel = select i1 %cond, i32 %phi, i32 %A
2336+
ret i32 %sel
2337+
}
2338+
2339+
define i32 @test_select_into_phi_not_idom_inverted_2(i1 %cond, i32 %A, i32 %B) {
2340+
; CHECK-LABEL: @test_select_into_phi_not_idom_inverted_2(
2341+
; CHECK-NEXT: entry:
2342+
; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_FALSE:%.*]], label [[IF_TRUE:%.*]]
2343+
; CHECK: if.true:
2344+
; CHECK-NEXT: br label [[MERGE:%.*]]
2345+
; CHECK: if.false:
2346+
; CHECK-NEXT: br label [[MERGE]]
2347+
; CHECK: merge:
2348+
; CHECK-NEXT: [[SEL:%.*]] = phi i32 [ [[B:%.*]], [[IF_FALSE]] ], [ [[A:%.*]], [[IF_TRUE]] ]
2349+
; CHECK-NEXT: br label [[EXIT:%.*]]
2350+
; CHECK: exit:
2351+
; CHECK-NEXT: ret i32 [[SEL]]
2352+
;
2353+
entry:
2354+
%inverted = xor i1 %cond, 1
2355+
br i1 %inverted, label %if.true, label %if.false
2356+
2357+
if.true:
2358+
br label %merge
2359+
2360+
if.false:
2361+
br label %merge
2362+
2363+
merge:
2364+
%phi = phi i32 [%A, %if.true], [%B, %if.false]
2365+
br label %exit
2366+
2367+
exit:
2368+
%sel = select i1 %cond, i32 %B, i32 %phi
2369+
ret i32 %sel
2370+
}
2371+
2372+
define i32 @test_select_into_phi_not_idom_no_dom_input_1(i1 %cond, i32 %A, i32 %B, i32 *%p) {
2373+
; CHECK-LABEL: @test_select_into_phi_not_idom_no_dom_input_1(
2374+
; CHECK-NEXT: entry:
2375+
; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2376+
; CHECK: if.true:
2377+
; CHECK-NEXT: [[C:%.*]] = load i32, i32* [[P:%.*]], align 4
2378+
; CHECK-NEXT: br label [[MERGE:%.*]]
2379+
; CHECK: if.false:
2380+
; CHECK-NEXT: br label [[MERGE]]
2381+
; CHECK: merge:
2382+
; CHECK-NEXT: [[SEL:%.*]] = phi i32 [ [[A:%.*]], [[IF_FALSE]] ], [ [[C]], [[IF_TRUE]] ]
2383+
; CHECK-NEXT: br label [[EXIT:%.*]]
2384+
; CHECK: exit:
2385+
; CHECK-NEXT: ret i32 [[SEL]]
2386+
;
2387+
entry:
2388+
br i1 %cond, label %if.true, label %if.false
2389+
2390+
if.true:
2391+
%C = load i32, i32* %p
2392+
br label %merge
2393+
2394+
if.false:
2395+
br label %merge
2396+
2397+
merge:
2398+
%phi = phi i32 [%C, %if.true], [%B, %if.false]
2399+
br label %exit
2400+
22722401
exit:
22732402
%sel = select i1 %cond, i32 %phi, i32 %A
22742403
ret i32 %sel
22752404
}
22762405

2406+
define i32 @test_select_into_phi_not_idom_no_dom_input_2(i1 %cond, i32 %A, i32 %B, i32 *%p) {
2407+
; CHECK-LABEL: @test_select_into_phi_not_idom_no_dom_input_2(
2408+
; CHECK-NEXT: entry:
2409+
; CHECK-NEXT: br i1 [[COND:%.*]], label [[IF_TRUE:%.*]], label [[IF_FALSE:%.*]]
2410+
; CHECK: if.true:
2411+
; CHECK-NEXT: br label [[MERGE:%.*]]
2412+
; CHECK: if.false:
2413+
; CHECK-NEXT: [[C:%.*]] = load i32, i32* [[P:%.*]], align 4
2414+
; CHECK-NEXT: br label [[MERGE]]
2415+
; CHECK: merge:
2416+
; CHECK-NEXT: [[SEL:%.*]] = phi i32 [ [[C]], [[IF_FALSE]] ], [ [[B:%.*]], [[IF_TRUE]] ]
2417+
; CHECK-NEXT: br label [[EXIT:%.*]]
2418+
; CHECK: exit:
2419+
; CHECK-NEXT: ret i32 [[SEL]]
2420+
;
2421+
entry:
2422+
br i1 %cond, label %if.true, label %if.false
2423+
2424+
if.true:
2425+
br label %merge
2426+
2427+
if.false:
2428+
%C = load i32, i32* %p
2429+
br label %merge
2430+
2431+
merge:
2432+
%phi = phi i32 [%A, %if.true], [%C, %if.false]
2433+
br label %exit
2434+
2435+
exit:
2436+
%sel = select i1 %cond, i32 %B, i32 %phi
2437+
ret i32 %sel
2438+
}
2439+
22772440
; Negative tests to ensure we don't remove selects with undef true/false values.
22782441
; See https://bugs.llvm.org/show_bug.cgi?id=31633
22792442
; https://lists.llvm.org/pipermail/llvm-dev/2016-October/106182.html

0 commit comments

Comments
 (0)