llvm.org GIT mirror llvm / 4547fff
Revert "Reapply: [ADCE][Dominators] Teach ADCE to preserve dominators" Summary: This partially reverts commit r311057 since it breaks ADCE. See PR34258. Reviewers: kuhar Subscribers: mcrosier, david2050, llvm-commits Differential Revision: https://reviews.llvm.org/D36979 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@311381 91177308-0d34-0410-b5e6-96231b3b80d8 Sanjoy Das 2 years ago
3 changed file(s) with 7 addition(s) and 103 deletion(s). Raw diff Collapse all Expand all
2626 #include "llvm/IR/BasicBlock.h"
2727 #include "llvm/IR/CFG.h"
2828 #include "llvm/IR/DebugInfoMetadata.h"
29 #include "llvm/IR/Dominators.h"
3029 #include "llvm/IR/IRBuilder.h"
3130 #include "llvm/IR/InstIterator.h"
3231 #include "llvm/IR/Instructions.h"
8988
9089 class AggressiveDeadCodeElimination {
9190 Function &F;
92
93 // ADCE does not use DominatorTree per se, but it updates it to preserve the
94 // analysis.
95 DominatorTree &DT;
9691 PostDominatorTree &PDT;
9792
9893 /// Mapping of blocks to associated information, an element in BlockInfoVec.
161156 void makeUnconditional(BasicBlock *BB, BasicBlock *Target);
162157
163158 public:
164 AggressiveDeadCodeElimination(Function &F, DominatorTree &DT,
165 PostDominatorTree &PDT)
166 : F(F), DT(DT), PDT(PDT) {}
167 bool performDeadCodeElimination();
159 AggressiveDeadCodeElimination(Function &F, PostDominatorTree &PDT)
160 : F(F), PDT(PDT) {}
161 bool performDeadCodeElimination();
168162 };
169163 }
170164
562556 }
563557 assert((PreferredSucc && PreferredSucc->PostOrder > 0) &&
564558 "Failed to find safe successor for dead branch");
565
566 // Collect removed successors to update the (Post)DominatorTrees.
567 SmallPtrSet RemovedSuccessors;
568559 bool First = true;
569560 for (auto *Succ : successors(BB)) {
570 if (!First || Succ != PreferredSucc->BB) {
561 if (!First || Succ != PreferredSucc->BB)
571562 Succ->removePredecessor(BB);
572 RemovedSuccessors.insert(Succ);
573 } else
563 else
574564 First = false;
575565 }
576566 makeUnconditional(BB, PreferredSucc->BB);
577
578 // Inform the dominators about the deleted CFG edges.
579 for (auto *Succ : RemovedSuccessors) {
580 // It might have happened that the same successor appeared multiple times
581 // and the CFG edge wasn't really removed.
582 if (Succ != PreferredSucc->BB) {
583 DEBUG(dbgs() << "ADCE: Removing (Post)DomTree edge " << BB->getName()
584 << " -> " << Succ->getName() << "\n");
585 DT.deleteEdge(BB, Succ);
586 PDT.deleteEdge(BB, Succ);
587 }
588 }
589
590567 NumBranchesRemoved += 1;
591568 }
592569 }
631608 InstInfo[NewTerm].Live = true;
632609 if (const DILocation *DL = PredTerm->getDebugLoc())
633610 NewTerm->setDebugLoc(DL);
634
635 InstInfo.erase(PredTerm);
636 PredTerm->eraseFromParent();
637611 }
638612
639613 //===----------------------------------------------------------------------===//
642616 //
643617 //===----------------------------------------------------------------------===//
644618 PreservedAnalyses ADCEPass::run(Function &F, FunctionAnalysisManager &FAM) {
645 auto &DT = FAM.getResult(F);
646619 auto &PDT = FAM.getResult(F);
647 if (!AggressiveDeadCodeElimination(F, DT, PDT).performDeadCodeElimination())
620 if (!AggressiveDeadCodeElimination(F, PDT).performDeadCodeElimination())
648621 return PreservedAnalyses::all();
649622
650623 PreservedAnalyses PA;
651624 PA.preserveSet();
652625 PA.preserve();
653 PA.preserve();
654 PA.preserve();
655626 return PA;
656627 }
657628
665636 bool runOnFunction(Function &F) override {
666637 if (skipFunction(F))
667638 return false;
668
669 auto &DT = getAnalysis().getDomTree();
670639 auto &PDT = getAnalysis().getPostDomTree();
671 return AggressiveDeadCodeElimination(F, DT, PDT)
672 .performDeadCodeElimination();
640 return AggressiveDeadCodeElimination(F, PDT).performDeadCodeElimination();
673641 }
674642
675643 void getAnalysisUsage(AnalysisUsage &AU) const override {
676 // We require DominatorTree here only to update and thus preserve it.
677 AU.addRequired();
678644 AU.addRequired();
679645 if (!RemoveControlFlowFlag)
680646 AU.setPreservesCFG();
681 else {
682 AU.addPreserved();
683 AU.addPreserved();
684 }
685647 AU.addPreserved();
686648 }
687649 };
690652 char ADCELegacyPass::ID = 0;
691653 INITIALIZE_PASS_BEGIN(ADCELegacyPass, "adce",
692654 "Aggressive Dead Code Elimination", false, false)
693 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
694655 INITIALIZE_PASS_DEPENDENCY(PostDominatorTreeWrapperPass)
695656 INITIALIZE_PASS_END(ADCELegacyPass, "adce", "Aggressive Dead Code Elimination",
696657 false, false)
+0
-39
test/Transforms/ADCE/domtree-DoubleDeletion.ll less more
None ; RUN: opt < %s -gvn -simplifycfg -adce | llvm-dis
1 ; RUN: opt < %s -gvn -simplifycfg -adce -verify-dom-info | llvm-dis
2
3 ; This test makes sure that the DominatorTree properly handles
4 ; deletion of edges that go to forward-unreachable regions.
5 ; In this case, %land.end is already forward unreachable when
6 ; the DT gets informed about the deletion of %entry -> %land.end.
7
8 @a = common global i32 0, align 4
9
10 define i32 @main() {
11 entry:
12 %retval = alloca i32, align 4
13 store i32 0, i32* %retval, align 4
14 %0 = load i32, i32* @a, align 4
15 %cmp = icmp ne i32 %0, 1
16 br i1 %cmp, label %land.rhs, label %land.end4
17
18 land.rhs: ; preds = %entry
19 %1 = load i32, i32* @a, align 4
20 %tobool = icmp ne i32 %1, 0
21 br i1 %tobool, label %land.rhs1, label %land.end
22
23 land.rhs1: ; preds = %land.rhs
24 br label %land.end
25
26 land.end: ; preds = %land.rhs1, %land.rhs
27 %2 = phi i1 [ false, %land.rhs ], [ true, %land.rhs1 ]
28 %land.ext = zext i1 %2 to i32
29 %conv = trunc i32 %land.ext to i16
30 %conv2 = sext i16 %conv to i32
31 %tobool3 = icmp ne i32 %conv2, 0
32 br label %land.end4
33
34 land.end4: ; preds = %land.end, %entry
35 %3 = phi i1 [ false, %entry ], [ %tobool3, %land.end ]
36 %land.ext5 = zext i1 %3 to i32
37 ret i32 0
38 }
+0
-18
test/Transforms/ADCE/unreachable.ll less more
None ; RUN: opt < %s -adce -simplifycfg | llvm-dis
1 ; RUN: opt < %s -passes=adce | llvm-dis
2
3 define i32 @Test(i32 %A, i32 %B) {
4 BB1:
5 br label %BB4
6
7 BB2: ; No predecessors!
8 br label %BB3
9
10 BB3: ; preds = %BB4, %BB2
11 %ret = phi i32 [ %X, %BB4 ], [ %B, %BB2 ] ; [#uses=1]
12 ret i32 %ret
13
14 BB4: ; preds = %BB1
15 %X = phi i32 [ %A, %BB1 ] ; [#uses=1]
16 br label %BB3
17 }