Skip to content

Commit 95848ea

Browse files
committed
[Value][InstCombine] Fix one-use checks in PHI-of-op -> Op-of-PHI[s] transforms to be one-user checks
As FIXME said, they really should be checking for a single user, not use, so let's do that. It is not *that* unusual to have the same value as incoming value in a PHI node, not unlike how a PHI may have the same incoming basic block more than once. There isn't a nice way to do that, Value::users() isn't uniqified, and Value only tracks it's uses, not Users, so the check is potentially costly since it does indeed potentially involes traversing the entire use list of a value.
1 parent c07a430 commit 95848ea

8 files changed

Lines changed: 55 additions & 37 deletions

File tree

‎llvm/include/llvm/IR/Value.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,16 @@ class Value {
442442
/// This is logically equivalent to getNumUses() >= N.
443443
bool hasNUsesOrMore(unsigned N) const;
444444

445+
/// Return true if there is exactly one user of this value.
446+
///
447+
/// Note that this is not the same as "has one use". If a value has one use,
448+
/// then there certainly is a single user. But if value has several uses,
449+
/// it is possible that all uses are in a single user, or not.
450+
///
451+
/// This check is potentially costly, since it requires traversing,
452+
/// in the worst case, the whole use list of a value.
453+
bool hasOneUser() const;
454+
445455
/// Return true if there is exactly one use of this value that cannot be
446456
/// dropped.
447457
///

‎llvm/lib/IR/Value.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ bool Value::hasNUsesOrMore(unsigned N) const {
147147
return hasNItemsOrMore(use_begin(), use_end(), N);
148148
}
149149

150+
bool Value::hasOneUser() const {
151+
if (use_empty())
152+
return false;
153+
if (hasOneUse())
154+
return true;
155+
return std::equal(++user_begin(), user_end(), user_begin());
156+
}
157+
150158
static bool isUnDroppableUser(const User *U) { return !U->isDroppable(); }
151159

152160
Use *Value::getSingleUndroppableUse() {

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ InstCombinerImpl::foldPHIArgInsertValueInstructionIntoPHI(PHINode &PN) {
308308
// and all have a single use.
309309
for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
310310
auto *I = dyn_cast<InsertValueInst>(PN.getIncomingValue(i));
311-
if (!I || !I->hasOneUse() || I->getIndices() != FirstIVI->getIndices())
311+
if (!I || !I->hasOneUser() || I->getIndices() != FirstIVI->getIndices())
312312
return nullptr;
313313
}
314314

@@ -348,7 +348,7 @@ InstCombinerImpl::foldPHIArgExtractValueInstructionIntoPHI(PHINode &PN) {
348348
// and all have a single use.
349349
for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
350350
auto *I = dyn_cast<ExtractValueInst>(PN.getIncomingValue(i));
351-
if (!I || !I->hasOneUse() || I->getIndices() != FirstEVI->getIndices() ||
351+
if (!I || !I->hasOneUser() || I->getIndices() != FirstEVI->getIndices() ||
352352
I->getAggregateOperand()->getType() !=
353353
FirstEVI->getAggregateOperand()->getType())
354354
return nullptr;
@@ -376,7 +376,7 @@ InstCombinerImpl::foldPHIArgExtractValueInstructionIntoPHI(PHINode &PN) {
376376
}
377377

378378
/// If we have something like phi [add (a,b), add(a,c)] and if a/b/c and the
379-
/// adds all have a single use, turn this into a phi and a single binop.
379+
/// adds all have a single user, turn this into a phi and a single binop.
380380
Instruction *InstCombinerImpl::foldPHIArgBinOpIntoPHI(PHINode &PN) {
381381
Instruction *FirstInst = cast<Instruction>(PN.getIncomingValue(0));
382382
assert(isa<BinaryOperator>(FirstInst) || isa<CmpInst>(FirstInst));
@@ -387,10 +387,10 @@ Instruction *InstCombinerImpl::foldPHIArgBinOpIntoPHI(PHINode &PN) {
387387
Type *LHSType = LHSVal->getType();
388388
Type *RHSType = RHSVal->getType();
389389

390-
// Scan to see if all operands are the same opcode, and all have one use.
390+
// Scan to see if all operands are the same opcode, and all have one user.
391391
for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
392392
Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i));
393-
if (!I || I->getOpcode() != Opc || !I->hasOneUse() ||
393+
if (!I || I->getOpcode() != Opc || !I->hasOneUser() ||
394394
// Verify type of the LHS matches so we don't fold cmp's of different
395395
// types.
396396
I->getOperand(0)->getType() != LHSType ||
@@ -486,11 +486,12 @@ Instruction *InstCombinerImpl::foldPHIArgGEPIntoPHI(PHINode &PN) {
486486

487487
bool AllInBounds = true;
488488

489-
// Scan to see if all operands are the same opcode, and all have one use.
489+
// Scan to see if all operands are the same opcode, and all have one user.
490490
for (unsigned i = 1; i != PN.getNumIncomingValues(); ++i) {
491-
GetElementPtrInst *GEP= dyn_cast<GetElementPtrInst>(PN.getIncomingValue(i));
492-
if (!GEP || !GEP->hasOneUse() || GEP->getType() != FirstInst->getType() ||
493-
GEP->getNumOperands() != FirstInst->getNumOperands())
491+
GetElementPtrInst *GEP =
492+
dyn_cast<GetElementPtrInst>(PN.getIncomingValue(i));
493+
if (!GEP || !GEP->hasOneUser() || GEP->getType() != FirstInst->getType() ||
494+
GEP->getNumOperands() != FirstInst->getNumOperands())
494495
return nullptr;
495496

496497
AllInBounds &= GEP->isInBounds();
@@ -657,7 +658,7 @@ Instruction *InstCombinerImpl::foldPHIArgLoadIntoPHI(PHINode &PN) {
657658
// Check to see if all arguments are the same operation.
658659
for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
659660
LoadInst *LI = dyn_cast<LoadInst>(PN.getIncomingValue(i));
660-
if (!LI || !LI->hasOneUse())
661+
if (!LI || !LI->hasOneUser())
661662
return nullptr;
662663

663664
// We can't sink the load if the loaded value could be modified between
@@ -770,8 +771,8 @@ Instruction *InstCombinerImpl::foldPHIArgZextsIntoPHI(PHINode &Phi) {
770771
unsigned NumConsts = 0;
771772
for (Value *V : Phi.incoming_values()) {
772773
if (auto *Zext = dyn_cast<ZExtInst>(V)) {
773-
// All zexts must be identical and have one use.
774-
if (Zext->getSrcTy() != NarrowType || !Zext->hasOneUse())
774+
// All zexts must be identical and have one user.
775+
if (Zext->getSrcTy() != NarrowType || !Zext->hasOneUser())
775776
return nullptr;
776777
NewIncoming.push_back(Zext->getOperand(0));
777778
NumZexts++;
@@ -859,7 +860,7 @@ Instruction *InstCombinerImpl::foldPHIArgOpIntoPHI(PHINode &PN) {
859860
// Check to see if all arguments are the same operation.
860861
for (unsigned i = 1, e = PN.getNumIncomingValues(); i != e; ++i) {
861862
Instruction *I = dyn_cast<Instruction>(PN.getIncomingValue(i));
862-
if (!I || !I->hasOneUse() || !I->isSameOperationAs(FirstInst))
863+
if (!I || !I->hasOneUser() || !I->isSameOperationAs(FirstInst))
863864
return nullptr;
864865
if (CastSrcTy) {
865866
if (I->getOperand(0)->getType() != CastSrcTy)
@@ -1303,10 +1304,8 @@ Instruction *InstCombinerImpl::visitPHINode(PHINode &PN) {
13031304
if (isa<Instruction>(PN.getIncomingValue(0)) &&
13041305
isa<Instruction>(PN.getIncomingValue(1)) &&
13051306
cast<Instruction>(PN.getIncomingValue(0))->getOpcode() ==
1306-
cast<Instruction>(PN.getIncomingValue(1))->getOpcode() &&
1307-
// FIXME: The hasOneUse check will fail for PHIs that use the value more
1308-
// than themselves more than once.
1309-
PN.getIncomingValue(0)->hasOneUse())
1307+
cast<Instruction>(PN.getIncomingValue(1))->getOpcode() &&
1308+
PN.getIncomingValue(0)->hasOneUser())
13101309
if (Instruction *Result = foldPHIArgOpIntoPHI(PN))
13111310
return Result;
13121311

‎llvm/test/Transforms/InstCombine/phi-aware-aggregate-reconstruction.ll‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,9 +450,14 @@ define { i32, i32 } @test8({ i32, i32 } %agg_left, { i32, i32 } %agg_right, i1 %
450450
; CHECK: impossible:
451451
; CHECK-NEXT: unreachable
452452
; CHECK: end:
453-
; CHECK-NEXT: [[I8_MERGED:%.*]] = phi { i32, i32 } [ [[AGG_RIGHT:%.*]], [[RIGHT]] ], [ [[AGG_RIGHT]], [[RIGHT]] ], [ [[AGG_LEFT:%.*]], [[LEFT]] ], [ [[AGG_LEFT]], [[LEFT]] ]
453+
; CHECK-NEXT: [[AGG_LEFT_PN:%.*]] = phi { i32, i32 } [ [[AGG_LEFT:%.*]], [[LEFT]] ], [ [[AGG_LEFT]], [[LEFT]] ], [ [[AGG_RIGHT:%.*]], [[RIGHT]] ], [ [[AGG_RIGHT]], [[RIGHT]] ]
454+
; CHECK-NEXT: [[AGG_LEFT_PN1:%.*]] = phi { i32, i32 } [ [[AGG_LEFT]], [[LEFT]] ], [ [[AGG_LEFT]], [[LEFT]] ], [ [[AGG_RIGHT]], [[RIGHT]] ], [ [[AGG_RIGHT]], [[RIGHT]] ]
455+
; CHECK-NEXT: [[I6:%.*]] = extractvalue { i32, i32 } [[AGG_LEFT_PN1]], 1
456+
; CHECK-NEXT: [[I5:%.*]] = extractvalue { i32, i32 } [[AGG_LEFT_PN]], 0
454457
; CHECK-NEXT: call void @baz()
455-
; CHECK-NEXT: ret { i32, i32 } [[I8_MERGED]]
458+
; CHECK-NEXT: [[I7:%.*]] = insertvalue { i32, i32 } undef, i32 [[I5]], 0
459+
; CHECK-NEXT: [[I8:%.*]] = insertvalue { i32, i32 } [[I7]], i32 [[I6]], 1
460+
; CHECK-NEXT: ret { i32, i32 } [[I8]]
456461
;
457462
entry:
458463
br i1 %c, label %left, label %right

‎llvm/test/Transforms/InstCombine/phi-of-extractvalues.ll‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,6 @@ end:
317317
define i32 @test10({ i32, i32 } %agg_left, { i32, i32 } %agg_right, i1 %c0, i1 %c1) {
318318
; CHECK-LABEL: @test10(
319319
; CHECK-NEXT: entry:
320-
; CHECK-NEXT: [[I0:%.*]] = extractvalue { i32, i32 } [[AGG_LEFT:%.*]], 0
321-
; CHECK-NEXT: [[I1:%.*]] = extractvalue { i32, i32 } [[AGG_RIGHT:%.*]], 0
322320
; CHECK-NEXT: br i1 [[C0:%.*]], label [[END:%.*]], label [[DISPATCH:%.*]]
323321
; CHECK: dispatch:
324322
; CHECK-NEXT: br i1 [[C1:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
@@ -327,7 +325,8 @@ define i32 @test10({ i32, i32 } %agg_left, { i32, i32 } %agg_right, i1 %c0, i1 %
327325
; CHECK: right:
328326
; CHECK-NEXT: br label [[END]]
329327
; CHECK: end:
330-
; CHECK-NEXT: [[R:%.*]] = phi i32 [ [[I0]], [[ENTRY:%.*]] ], [ [[I0]], [[LEFT]] ], [ [[I1]], [[RIGHT]] ]
328+
; CHECK-NEXT: [[AGG_LEFT_PN:%.*]] = phi { i32, i32 } [ [[AGG_LEFT:%.*]], [[ENTRY:%.*]] ], [ [[AGG_LEFT]], [[LEFT]] ], [ [[AGG_RIGHT:%.*]], [[RIGHT]] ]
329+
; CHECK-NEXT: [[R:%.*]] = extractvalue { i32, i32 } [[AGG_LEFT_PN]], 0
331330
; CHECK-NEXT: ret i32 [[R]]
332331
;
333332
entry:

‎llvm/test/Transforms/InstCombine/phi-of-insertvalues.ll‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,6 @@ end:
288288
define { i32, i32 } @test9({ i32, i32 } %agg, i32 %val_left, i32 %val_right, i1 %c0, i1 %c1) {
289289
; CHECK-LABEL: @test9(
290290
; CHECK-NEXT: entry:
291-
; CHECK-NEXT: [[I0:%.*]] = insertvalue { i32, i32 } [[AGG:%.*]], i32 [[VAL_LEFT:%.*]], 0
292-
; CHECK-NEXT: [[I1:%.*]] = insertvalue { i32, i32 } [[AGG]], i32 [[VAL_RIGHT:%.*]], 0
293291
; CHECK-NEXT: br i1 [[C0:%.*]], label [[END:%.*]], label [[DISPATCH:%.*]]
294292
; CHECK: dispatch:
295293
; CHECK-NEXT: br i1 [[C1:%.*]], label [[LEFT:%.*]], label [[RIGHT:%.*]]
@@ -298,7 +296,8 @@ define { i32, i32 } @test9({ i32, i32 } %agg, i32 %val_left, i32 %val_right, i1
298296
; CHECK: right:
299297
; CHECK-NEXT: br label [[END]]
300298
; CHECK: end:
301-
; CHECK-NEXT: [[R:%.*]] = phi { i32, i32 } [ [[I0]], [[ENTRY:%.*]] ], [ [[I0]], [[LEFT]] ], [ [[I1]], [[RIGHT]] ]
299+
; CHECK-NEXT: [[VAL_LEFT_PN:%.*]] = phi i32 [ [[VAL_LEFT:%.*]], [[ENTRY:%.*]] ], [ [[VAL_LEFT]], [[LEFT]] ], [ [[VAL_RIGHT:%.*]], [[RIGHT]] ]
300+
; CHECK-NEXT: [[R:%.*]] = insertvalue { i32, i32 } [[AGG:%.*]], i32 [[VAL_LEFT_PN]], 0
302301
; CHECK-NEXT: ret { i32, i32 } [[R]]
303302
;
304303
entry:

‎llvm/test/Transforms/LoopUnroll/runtime-loop-multiple-exits.ll‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,26 +404,26 @@ returnblock: ; preds = %latchExit, %entr
404404
define i64 @test5(i64 %trip, i64 %add, i1 %cond) {
405405
; EPILOG: test5(
406406
; EPILOG: exit1.loopexit:
407-
; EPILOG-NEXT: %result.ph = phi i64 [ %ivy, %loop_exiting ], [ %ivy, %loop_exiting ], [ %ivy.1, %loop_exiting.1 ], [ %ivy.1, %loop_exiting.1 ], [ %ivy.2, %loop_exiting.2 ],
407+
; EPILOG-NEXT: %iv.pn = phi i64 [ %iv, %loop_exiting ], [ %iv, %loop_exiting ], [ %iv_next, %loop_exiting.1 ], [ %iv_next, %loop_exiting.1 ], [ %iv_next.1, %loop_exiting.2 ], [ %iv_next.1, %loop_exiting.2 ], [ %iv_next.2, %loop_exiting.3 ], [ %iv_next.2, %loop_exiting.3 ], [ %iv_next.3, %loop_exiting.4 ], [ %iv_next.3, %loop_exiting.4 ], [ %iv_next.4, %loop_exiting.5 ], [ %iv_next.4, %loop_exiting.5 ], [ %iv_next.5, %loop_exiting.6 ], [ %iv_next.5, %loop_exiting.6 ], [ %iv_next.6, %loop_exiting.7 ], [ %iv_next.6, %loop_exiting.7 ]
408408
; EPILOG-NEXT: br label %exit1
409409
; EPILOG: exit1.loopexit2:
410-
; EPILOG-NEXT: %ivy.epil = add i64 %iv.epil, %add
411410
; EPILOG-NEXT: br label %exit1
412411
; EPILOG: exit1:
413-
; EPILOG-NEXT: %result = phi i64 [ %result.ph, %exit1.loopexit ], [ %ivy.epil, %exit1.loopexit2 ]
412+
; EPILOG-NEXT: %iv.pn.pn = phi i64 [ %iv.pn, %exit1.loopexit ], [ %iv.epil, %exit1.loopexit2 ]
413+
; EPILOG-NEXT: %result = add i64 %iv.pn.pn, %add
414414
; EPILOG-NEXT: ret i64 %result
415415
; EPILOG: loop_latch.7:
416416
; EPILOG: %niter.nsub.7 = add i64 %niter, -8
417417

418418
; PROLOG: test5(
419419
; PROLOG: exit1.loopexit:
420-
; PROLOG-NEXT: %result.ph = phi i64 [ %ivy, %loop_exiting ], [ %ivy, %loop_exiting ], [ %ivy.1, %loop_exiting.1 ], [ %ivy.1, %loop_exiting.1 ], [ %ivy.2, %loop_exiting.2 ],
420+
; PROLOG-NEXT: %iv.pn = phi i64 [ %iv, %loop_exiting ], [ %iv, %loop_exiting ], [ %iv_next, %loop_exiting.1 ], [ %iv_next, %loop_exiting.1 ], [ %iv_next.1, %loop_exiting.2 ], [ %iv_next.1, %loop_exiting.2 ], [ %iv_next.2, %loop_exiting.3 ], [ %iv_next.2, %loop_exiting.3 ], [ %iv_next.3, %loop_exiting.4 ], [ %iv_next.3, %loop_exiting.4 ], [ %iv_next.4, %loop_exiting.5 ], [ %iv_next.4, %loop_exiting.5 ], [ %iv_next.5, %loop_exiting.6 ], [ %iv_next.5, %loop_exiting.6 ], [ %iv_next.6, %loop_exiting.7 ], [ %iv_next.6, %loop_exiting.7 ]
421421
; PROLOG-NEXT: br label %exit1
422422
; PROLOG: exit1.loopexit1:
423-
; PROLOG-NEXT: %ivy.prol = add i64 %iv.prol, %add
424423
; PROLOG-NEXT: br label %exit1
425424
; PROLOG: exit1:
426-
; PROLOG-NEXT: %result = phi i64 [ %result.ph, %exit1.loopexit ], [ %ivy.prol, %exit1.loopexit1 ]
425+
; PROLOG-NEXT: %iv.pn.pn = phi i64 [ %iv.pn, %exit1.loopexit ], [ %iv.prol, %exit1.loopexit1 ]
426+
; PROLOG-NEXT: %result = add i64 %iv.pn.pn, %add
427427
; PROLOG-NEXT: ret i64 %result
428428
; PROLOG: loop_latch.7:
429429
; PROLOG: %iv_next.7 = add nuw nsw i64 %iv, 8

‎llvm/test/Transforms/PGOProfile/chr.ll‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,15 +1649,13 @@ define i32 @test_chr_18(i32* %i, i32 %sum0) !prof !14 {
16491649
; CHECK-NEXT: [[SUM1:%.*]] = add i32 [[SUM0:%.*]], 42
16501650
; CHECK-NEXT: [[TMP0:%.*]] = and i32 [[LI]], 5
16511651
; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[TMP0]], 5
1652-
; CHECK-NEXT: br i1 [[TMP1]], label [[BB0_SPLIT:%.*]], label [[BB0_SPLIT_NONCHR:%.*]], !prof !15
1653-
; CHECK: bb0.split:
1654-
; CHECK-NEXT: [[INC2:%.*]] = add i32 [[INC1]], 1
1652+
; CHECK-NEXT: br i1 [[TMP1]], label [[BB1:%.*]], label [[BB0_SPLIT_NONCHR:%.*]], !prof !15
1653+
; CHECK: bb1:
16551654
; CHECK-NEXT: [[SUM3:%.*]] = add i32 [[SUM0]], 86
16561655
; CHECK-NEXT: br label [[BB2]]
16571656
; CHECK: bb0.split.nonchr:
16581657
; CHECK-NEXT: [[A4_NONCHR:%.*]] = and i32 [[LI]], 4
16591658
; CHECK-NEXT: [[CMP4_NONCHR:%.*]] = icmp eq i32 [[A4_NONCHR]], 0
1660-
; CHECK-NEXT: [[INC2_NONCHR:%.*]] = add i32 [[INC1]], 1
16611659
; CHECK-NEXT: br i1 [[CMP4_NONCHR]], label [[BB2]], label [[BB1_NONCHR:%.*]], !prof !16
16621660
; CHECK: bb1.nonchr:
16631661
; CHECK-NEXT: [[A1:%.*]] = and i32 [[LI]], 1
@@ -1666,8 +1664,8 @@ define i32 @test_chr_18(i32* %i, i32 %sum0) !prof !14 {
16661664
; CHECK-NEXT: [[SUM3_NONCHR:%.*]] = add i32 [[SUM2_NONCHR]], 44
16671665
; CHECK-NEXT: br label [[BB2]]
16681666
; CHECK: bb2:
1669-
; CHECK-NEXT: [[TMP2]] = phi i32 [ [[INC2]], [[BB0_SPLIT]] ], [ [[INC2_NONCHR]], [[BB1_NONCHR]] ], [ [[INC2_NONCHR]], [[BB0_SPLIT_NONCHR]] ]
1670-
; CHECK-NEXT: [[SUM4:%.*]] = phi i32 [ [[SUM3]], [[BB0_SPLIT]] ], [ [[SUM3_NONCHR]], [[BB1_NONCHR]] ], [ [[SUM1]], [[BB0_SPLIT_NONCHR]] ]
1667+
; CHECK-NEXT: [[SUM4:%.*]] = phi i32 [ [[SUM3]], [[BB1]] ], [ [[SUM1]], [[BB0_SPLIT_NONCHR]] ], [ [[SUM3_NONCHR]], [[BB1_NONCHR]] ]
1668+
; CHECK-NEXT: [[TMP2]] = add i32 [[INC1]], 1
16711669
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[TMP2]], 100
16721670
; CHECK-NEXT: br i1 [[CMP]], label [[BB3:%.*]], label [[BB0]], !prof !16
16731671
; CHECK: bb3:

0 commit comments

Comments
 (0)