Skip to content

Commit

Permalink
[DSE] Relax constraint on isGuaranteedLoopInvariant
Browse files Browse the repository at this point in the history
If the location ptr to be killed is in no loop and the Function does not
have irreducible loops, then we can regard it as loop invariant.

Differential Revision: https://reviews.llvm.org/D135369
  • Loading branch information
luxufan committed Oct 6, 2022
1 parent 7329dc0 commit eaf6e2f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
6 changes: 4 additions & 2 deletions llvm/lib/Transforms/Scalar/DeadStoreElimination.cpp
Expand Up @@ -1215,8 +1215,10 @@ struct DSEState {
if (GEP->hasAllConstantIndices())
Ptr = GEP->getPointerOperand()->stripPointerCasts();

if (auto *I = dyn_cast<Instruction>(Ptr))
return I->getParent()->isEntryBlock();
if (auto *I = dyn_cast<Instruction>(Ptr)) {
return I->getParent()->isEntryBlock() ||
(!ContainsIrreducibleLoops && !LI.getLoopFor(I->getParent()));
}
return true;
}

Expand Down
27 changes: 26 additions & 1 deletion llvm/test/Transforms/DeadStoreElimination/multiblock-loops.ll
Expand Up @@ -617,6 +617,32 @@ exit:
ret i16 0
}

define i16 @irreducible_entryblock_def(i1 %c) {
; CHECK-LABEL: @irreducible_entryblock_def(
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[C:%.*]], label [[A:%.*]], label [[B:%.*]]
; CHECK: A:
; CHECK-NEXT: br label [[B]]
; CHECK: B:
; CHECK-NEXT: br i1 [[C]], label [[EXIT:%.*]], label [[A]]
; CHECK: exit:
; CHECK-NEXT: ret i16 0
;
entry:
%obj = alloca i32, align 4
br i1 %c, label %A, label %B

A:
store i32 1, ptr %obj, align 4
br label %B

B:
br i1 %c, label %exit, label %A

exit:
ret i16 0
}

; An irreducible loop inside another loop.
define i16 @irreducible_nested() {
; CHECK-LABEL: @irreducible_nested(
Expand Down Expand Up @@ -846,7 +872,6 @@ define i16 @partial_override_overloop(i1 %c, i32 %i) {
; CHECK-NEXT: br label [[FIRST:%.*]]
; CHECK: first:
; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [10 x i16], ptr @x, i16 0, i32 [[I:%.*]]
; CHECK-NEXT: store i16 1, ptr [[ARRAYIDX]], align 1
; CHECK-NEXT: br label [[DO_BODY:%.*]]
; CHECK: do.body:
; CHECK-NEXT: [[I_0:%.*]] = phi i16 [ 0, [[FIRST]] ], [ [[INC:%.*]], [[DO_BODY]] ]
Expand Down
Expand Up @@ -170,7 +170,6 @@ define void @test27() {
; CHECK-NEXT: br i1 true, label [[BB2:%.*]], label [[BB3:%.*]]
; CHECK: bb2:
; CHECK-NEXT: [[M:%.*]] = call noalias ptr @malloc(i64 10)
; CHECK-NEXT: store i8 1, ptr [[M]], align 1
; CHECK-NEXT: br label [[BB3]]
; CHECK: bb3:
; CHECK-NEXT: [[R:%.*]] = phi ptr [ null, [[BB1:%.*]] ], [ [[M]], [[BB2]] ]
Expand Down

0 comments on commit eaf6e2f

Please sign in to comment.