Skip to content

Commit 43e7d9a

Browse files
committed
[InstCombine] Fold extractvalue of phi
Just as we do for most other operations, we should push extractvalue instructions through phis, if this does not increase unfolded instruction count.
1 parent 5b219dd commit 43e7d9a

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,11 @@ Instruction *InstCombinerImpl::foldOpIntoPhi(Instruction &I, PHINode *PN) {
12801280
InV = PN->getIncomingValue(i);
12811281
NewPN->addIncoming(InV, PN->getIncomingBlock(i));
12821282
}
1283+
} else if (auto *EV = dyn_cast<ExtractValueInst>(&I)) {
1284+
for (unsigned i = 0; i != NumPHIValues; ++i)
1285+
NewPN->addIncoming(Builder.CreateExtractValue(PN->getIncomingValue(i),
1286+
EV->getIndices(), "phi.ev"),
1287+
PN->getIncomingBlock(i));
12831288
} else {
12841289
CastInst *CI = cast<CastInst>(&I);
12851290
Type *RetTy = CI->getType();
@@ -3399,6 +3404,10 @@ Instruction *InstCombinerImpl::visitExtractValueInst(ExtractValueInst &EV) {
33993404
}
34003405
}
34013406

3407+
if (auto *PN = dyn_cast<PHINode>(Agg))
3408+
if (Instruction *Res = foldOpIntoPhi(EV, PN))
3409+
return Res;
3410+
34023411
// We could simplify extracts from other values. Note that nested extracts may
34033412
// already be simplified implicitly by the above: extract (extract (insert) )
34043413
// will be translated into extract ( insert ( extract ) ) first and then just

llvm/test/Transforms/InstCombine/phi-extractvalue.ll

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,8 @@ define i32 @extractvalue_of_constant_phi(i1 %c) {
393393
; CHECK: else:
394394
; CHECK-NEXT: br label [[JOIN]]
395395
; CHECK: join:
396-
; CHECK-NEXT: [[PHI:%.*]] = phi { i32, i32 } [ { i32 1, i32 2 }, [[IF]] ], [ { i32 3, i32 4 }, [[ELSE]] ]
397-
; CHECK-NEXT: [[EV:%.*]] = extractvalue { i32, i32 } [[PHI]], 0
398-
; CHECK-NEXT: ret i32 [[EV]]
396+
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ 1, [[IF]] ], [ 3, [[ELSE]] ]
397+
; CHECK-NEXT: ret i32 [[PHI]]
399398
;
400399
br i1 %c, label %if, label %else
401400

@@ -415,13 +414,13 @@ define i32 @extractvalue_of_one_constant_phi(i1 %c, { i32, i32 } %arg) {
415414
; CHECK-LABEL: @extractvalue_of_one_constant_phi(
416415
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
417416
; CHECK: if:
417+
; CHECK-NEXT: [[PHI_EV:%.*]] = extractvalue { i32, i32 } [[ARG:%.*]], 0
418418
; CHECK-NEXT: br label [[JOIN:%.*]]
419419
; CHECK: else:
420420
; CHECK-NEXT: br label [[JOIN]]
421421
; CHECK: join:
422-
; CHECK-NEXT: [[PHI:%.*]] = phi { i32, i32 } [ [[ARG:%.*]], [[IF]] ], [ { i32 3, i32 4 }, [[ELSE]] ]
423-
; CHECK-NEXT: [[EV:%.*]] = extractvalue { i32, i32 } [[PHI]], 0
424-
; CHECK-NEXT: ret i32 [[EV]]
422+
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[PHI_EV]], [[IF]] ], [ 3, [[ELSE]] ]
423+
; CHECK-NEXT: ret i32 [[PHI]]
425424
;
426425
br i1 %c, label %if, label %else
427426

@@ -445,9 +444,8 @@ define i32 @extractvalue_of_constant_phi_multi_index(i1 %c) {
445444
; CHECK: else:
446445
; CHECK-NEXT: br label [[JOIN]]
447446
; CHECK: join:
448-
; CHECK-NEXT: [[PHI:%.*]] = phi { i32, { i32, i32 } } [ { i32 1, { i32, i32 } { i32 2, i32 3 } }, [[IF]] ], [ { i32 4, { i32, i32 } { i32 5, i32 6 } }, [[ELSE]] ]
449-
; CHECK-NEXT: [[EV:%.*]] = extractvalue { i32, { i32, i32 } } [[PHI]], 1, 1
450-
; CHECK-NEXT: ret i32 [[EV]]
447+
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ 3, [[IF]] ], [ 6, [[ELSE]] ]
448+
; CHECK-NEXT: ret i32 [[PHI]]
451449
;
452450
br i1 %c, label %if, label %else
453451

@@ -467,13 +465,13 @@ define i32 @extractvalue_of_one_constant_phi_multi_index(i1 %c, { i32, { i32, i3
467465
; CHECK-LABEL: @extractvalue_of_one_constant_phi_multi_index(
468466
; CHECK-NEXT: br i1 [[C:%.*]], label [[IF:%.*]], label [[ELSE:%.*]]
469467
; CHECK: if:
468+
; CHECK-NEXT: [[PHI_EV:%.*]] = extractvalue { i32, { i32, i32 } } [[ARG:%.*]], 1, 1
470469
; CHECK-NEXT: br label [[JOIN:%.*]]
471470
; CHECK: else:
472471
; CHECK-NEXT: br label [[JOIN]]
473472
; CHECK: join:
474-
; CHECK-NEXT: [[PHI:%.*]] = phi { i32, { i32, i32 } } [ [[ARG:%.*]], [[IF]] ], [ { i32 4, { i32, i32 } { i32 5, i32 6 } }, [[ELSE]] ]
475-
; CHECK-NEXT: [[EV:%.*]] = extractvalue { i32, { i32, i32 } } [[PHI]], 1, 1
476-
; CHECK-NEXT: ret i32 [[EV]]
473+
; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[PHI_EV]], [[IF]] ], [ 6, [[ELSE]] ]
474+
; CHECK-NEXT: ret i32 [[PHI]]
477475
;
478476
br i1 %c, label %if, label %else
479477

0 commit comments

Comments
 (0)