Skip to content

Commit 8bee4d4

Browse files
Bogdan Graursureyeaah
authored andcommitted
Revert "[LoopDeletion] Allows deletion of possibly infinite side-effect free loops"
Test clang/test/Misc/loop-opt-setup.c fails when executed in Release. This reverts commit 6f1503d. Reviewed By: SureYeaah Differential Revision: https://reviews.llvm.org/D93956
1 parent be76406 commit 8bee4d4

6 files changed

Lines changed: 33 additions & 281 deletions

File tree

‎clang/test/Misc/loop-opt-setup.c‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// RUN: %clang -O1 -fno-unroll-loops -S -o - %s -emit-llvm | FileCheck %s
2-
// RUN: %clang -std=c99 -O1 -fno-unroll-loops -S -o - %s -emit-llvm | FileCheck %s --check-prefix C99
32

43
extern int a[16];
54
int b = 0;
@@ -25,12 +24,7 @@ void Helper() {
2524
}
2625

2726
// Check br i1 to make sure the loop is gone, there will still be a label branch for the infinite loop.
28-
// In C99, there was no forward progress requirement, so we expect the infinite loop to still exist,
29-
// but for C11 and onwards, the infinite loop can be deleted.
3027
// CHECK-LABEL: Helper
31-
// C99: br label
32-
// C99-NOT: br i1
33-
// C99: br label
34-
// CHECK: entry:
35-
// CHECK-NOT: br i1
36-
// CHECK-NEXT: ret void
28+
// CHECK: br label
29+
// CHECK-NOT: br i1
30+
// CHECK: br label

‎llvm/include/llvm/Transforms/Utils/LoopUtils.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,6 @@ bool hasDisableAllTransformsHint(const Loop *L);
255255
/// Look for the loop attribute that disables the LICM transformation heuristics.
256256
bool hasDisableLICMTransformsHint(const Loop *L);
257257

258-
/// Look for the loop attribute that requires progress within the loop.
259-
bool hasMustProgress(const Loop *L);
260-
261258
/// The mode sets how eager a transformation should be applied.
262259
enum TransformationMode {
263260
/// The pass can use heuristics to determine whether a transformation should

‎llvm/lib/Transforms/Scalar/LoopDeletion.cpp‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,10 @@ static bool isLoopNeverExecuted(Loop *L) {
128128

129129
/// Remove a loop if it is dead.
130130
///
131-
/// A loop is considered dead either if it does not impact the observable
132-
/// behavior of the program other than finite running time, or if it is
133-
/// required to make progress by an attribute such as 'mustprogress' or
134-
/// 'llvm.loop.mustprogress' and does not make any. This may remove
135-
/// infinite loops that have been required to make progress.
131+
/// A loop is considered dead if it does not impact the observable behavior of
132+
/// the program other than finite running time. This never removes a loop that
133+
/// might be infinite (unless it is never executed), as doing so could change
134+
/// the halting/non-halting nature of a program.
136135
///
137136
/// This entire process relies pretty heavily on LoopSimplify form and LCSSA in
138137
/// order to make various safety checks work.
@@ -208,13 +207,11 @@ static LoopDeletionResult deleteLoopIfDead(Loop *L, DominatorTree &DT,
208207
: LoopDeletionResult::Unmodified;
209208
}
210209

211-
// Don't remove loops for which we can't solve the trip count unless the loop
212-
// was required to make progress but has been determined to be dead.
210+
// Don't remove loops for which we can't solve the trip count.
211+
// They could be infinite, in which case we'd be changing program behavior.
213212
const SCEV *S = SE.getConstantMaxBackedgeTakenCount(L);
214-
if (isa<SCEVCouldNotCompute>(S) &&
215-
!L->getHeader()->getParent()->mustProgress() && !hasMustProgress(L)) {
216-
LLVM_DEBUG(dbgs() << "Could not compute SCEV MaxBackedgeTakenCount and was "
217-
"not required to make progress.\n");
213+
if (isa<SCEVCouldNotCompute>(S)) {
214+
LLVM_DEBUG(dbgs() << "Could not compute SCEV MaxBackedgeTakenCount.\n");
218215
return Changed ? LoopDeletionResult::Modified
219216
: LoopDeletionResult::Unmodified;
220217
}

‎llvm/lib/Transforms/Utils/LoopUtils.cpp‎

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ static cl::opt<bool> ForceReductionIntrinsic(
6363

6464
static const char *LLVMLoopDisableNonforced = "llvm.loop.disable_nonforced";
6565
static const char *LLVMLoopDisableLICM = "llvm.licm.disable";
66-
static const char *LLVMLoopMustProgress = "llvm.loop.mustprogress";
6766

6867
bool llvm::formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI,
6968
MemorySSAUpdater *MSSAU,
@@ -420,10 +419,6 @@ bool llvm::hasDisableLICMTransformsHint(const Loop *L) {
420419
return getBooleanLoopAttribute(L, LLVMLoopDisableLICM);
421420
}
422421

423-
bool llvm::hasMustProgress(const Loop *L) {
424-
return getBooleanLoopAttribute(L, LLVMLoopMustProgress);
425-
}
426-
427422
TransformationMode llvm::hasUnrollTransformation(Loop *L) {
428423
if (getBooleanLoopAttribute(L, "llvm.loop.unroll.disable"))
429424
return TM_SuppressedByUser;
@@ -594,7 +589,6 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
594589
IRBuilder<> Builder(OldBr);
595590

596591
auto *ExitBlock = L->getUniqueExitBlock();
597-
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
598592
if (ExitBlock) {
599593
assert(ExitBlock && "Should have a unique exit block!");
600594
assert(L->hasDedicatedExits() && "Loop should have dedicated exits!");
@@ -626,6 +620,7 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
626620
"Should have exactly one value and that's from the preheader!");
627621
}
628622

623+
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Eager);
629624
if (DT) {
630625
DTU.applyUpdates({{DominatorTree::Insert, Preheader, ExitBlock}});
631626
if (MSSA) {
@@ -641,28 +636,27 @@ void llvm::deleteDeadLoop(Loop *L, DominatorTree *DT, ScalarEvolution *SE,
641636
Builder.CreateBr(ExitBlock);
642637
// Remove the old branch.
643638
Preheader->getTerminator()->eraseFromParent();
639+
640+
if (DT) {
641+
DTU.applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}});
642+
if (MSSA) {
643+
MSSAU->applyUpdates(
644+
{{DominatorTree::Delete, Preheader, L->getHeader()}}, *DT);
645+
SmallSetVector<BasicBlock *, 8> DeadBlockSet(L->block_begin(),
646+
L->block_end());
647+
MSSAU->removeBlocks(DeadBlockSet);
648+
if (VerifyMemorySSA)
649+
MSSA->verifyMemorySSA();
650+
}
651+
}
644652
} else {
645653
assert(L->hasNoExitBlocks() &&
646654
"Loop should have either zero or one exit blocks.");
647-
648655
Builder.SetInsertPoint(OldBr);
649656
Builder.CreateUnreachable();
650657
Preheader->getTerminator()->eraseFromParent();
651658
}
652659

653-
if (DT) {
654-
DTU.applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}});
655-
if (MSSA) {
656-
MSSAU->applyUpdates({{DominatorTree::Delete, Preheader, L->getHeader()}},
657-
*DT);
658-
SmallSetVector<BasicBlock *, 8> DeadBlockSet(L->block_begin(),
659-
L->block_end());
660-
MSSAU->removeBlocks(DeadBlockSet);
661-
if (VerifyMemorySSA)
662-
MSSA->verifyMemorySSA();
663-
}
664-
}
665-
666660
// Use a map to unique and a vector to guarantee deterministic ordering.
667661
llvm::SmallDenseSet<std::pair<DIVariable *, DIExpression *>, 4> DeadDebugSet;
668662
llvm::SmallVector<DbgVariableIntrinsic *, 4> DeadDebugInst;

‎llvm/test/Transforms/LoopDeletion/mustprogress.ll‎

Lines changed: 0 additions & 237 deletions
This file was deleted.

‎llvm/test/Transforms/LoopDeletion/no-exit-blocks.ll‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ define void @f() #0 {
55
; CHECK: Function Attrs: mustprogress
66
; CHECK-LABEL: define {{[^@]+}}@f
77
; CHECK-SAME: () [[ATTR0:#.*]] {
8-
; CHECK-NEXT: unreachable
8+
; CHECK-NEXT: br label [[TMP1:%.*]]
9+
; CHECK: 1:
10+
; CHECK-NEXT: [[DOT01:%.*]] = phi i32 [ 1, [[TMP0:%.*]] ], [ [[TMP3:%.*]], [[TMP2:%.*]] ]
11+
; CHECK-NEXT: [[DOT0:%.*]] = phi i32 [ 1, [[TMP0]] ], [ [[TMP3]], [[TMP2]] ]
12+
; CHECK-NEXT: br label [[TMP2]]
13+
; CHECK: 2:
14+
; CHECK-NEXT: [[TMP3]] = add nsw i32 [[DOT01]], [[DOT0]]
15+
; CHECK-NEXT: br label [[TMP1]]
916
;
1017
br label %1
1118

0 commit comments

Comments
 (0)