llvm.org GIT mirror llvm / 302c35d
[PM] Port UnreachableBlockElim to the new Pass Manager Differential Revision: http://reviews.llvm.org/D22124 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@274824 91177308-0d34-0410-b5e6-96231b3b80d8 Wei Mi 4 years ago
9 changed file(s) with 102 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
0 //===-- UnreachableBlockElim.h - Remove unreachable blocks for codegen --===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass is an extremely simple version of the SimplifyCFG pass. Its sole
10 // job is to delete LLVM basic blocks that are not reachable from the entry
11 // node. To do this, it performs a simple depth first traversal of the CFG,
12 // then deletes any unvisited nodes.
13 //
14 // Note that this pass is really a hack. In particular, the instruction
15 // selectors for various targets should just not generate code for unreachable
16 // blocks. Until LLVM has a more systematic way of defining instruction
17 // selectors, however, we cannot really expect them to handle additional
18 // complexity.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_LIB_CODEGEN_UNREACHABLEBLOCKELIM_H
23 #define LLVM_LIB_CODEGEN_UNREACHABLEBLOCKELIM_H
24
25 #include "llvm/IR/PassManager.h"
26
27 namespace llvm {
28
29 class UnreachableBlockElimPass
30 : public PassInfoMixin {
31 public:
32 PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
33 };
34 } // end namespace llvm
35
36 #endif // LLVM_LIB_CODEGEN_UNREACHABLEBLOCKELIM_H
325325 void initializeTypeBasedAAWrapperPassPass(PassRegistry&);
326326 void initializeUnifyFunctionExitNodesPass(PassRegistry&);
327327 void initializeUnpackMachineBundlesPass(PassRegistry&);
328 void initializeUnreachableBlockElimPass(PassRegistry&);
328 void initializeUnreachableBlockElimLegacyPassPass(PassRegistry&);
329329 void initializeUnreachableMachineBlockElimPass(PassRegistry&);
330330 void initializeVerifierLegacyPassPass(PassRegistry&);
331331 void initializeVirtRegMapPass(PassRegistry&);
8080 initializeTargetPassConfigPass(Registry);
8181 initializeTwoAddressInstructionPassPass(Registry);
8282 initializeUnpackMachineBundlesPass(Registry);
83 initializeUnreachableBlockElimPass(Registry);
83 initializeUnreachableBlockElimLegacyPassPass(Registry);
8484 initializeUnreachableMachineBlockElimPass(Registry);
8585 initializeVirtRegMapPass(Registry);
8686 initializeVirtRegRewriterPass(Registry);
1919 //
2020 //===----------------------------------------------------------------------===//
2121
22 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/CodeGen/UnreachableBlockElim.h"
2323 #include "llvm/ADT/DepthFirstIterator.h"
2424 #include "llvm/ADT/SmallPtrSet.h"
2525 #include "llvm/CodeGen/MachineDominators.h"
2727 #include "llvm/CodeGen/MachineLoopInfo.h"
2828 #include "llvm/CodeGen/MachineModuleInfo.h"
2929 #include "llvm/CodeGen/MachineRegisterInfo.h"
30 #include "llvm/CodeGen/Passes.h"
3031 #include "llvm/IR/CFG.h"
3132 #include "llvm/IR/Constant.h"
3233 #include "llvm/IR/Dominators.h"
3738 #include "llvm/Target/TargetInstrInfo.h"
3839 using namespace llvm;
3940
40 namespace {
41 class UnreachableBlockElim : public FunctionPass {
42 bool runOnFunction(Function &F) override;
43 public:
44 static char ID; // Pass identification, replacement for typeid
45 UnreachableBlockElim() : FunctionPass(ID) {
46 initializeUnreachableBlockElimPass(*PassRegistry::getPassRegistry());
47 }
48
49 void getAnalysisUsage(AnalysisUsage &AU) const override {
50 AU.addPreserved();
51 }
52 };
53 }
54 char UnreachableBlockElim::ID = 0;
55 INITIALIZE_PASS(UnreachableBlockElim, "unreachableblockelim",
56 "Remove unreachable blocks from the CFG", false, false)
57
58 FunctionPass *llvm::createUnreachableBlockEliminationPass() {
59 return new UnreachableBlockElim();
60 }
61
62 bool UnreachableBlockElim::runOnFunction(Function &F) {
41 static bool eliminateUnreachableBlock(Function &F) {
6342 SmallPtrSet Reachable;
6443
6544 // Mark all reachable blocks.
9069 return !DeadBlocks.empty();
9170 }
9271
72 namespace {
73 class UnreachableBlockElimLegacyPass : public FunctionPass {
74 bool runOnFunction(Function &F) override {
75 return eliminateUnreachableBlock(F);
76 }
77
78 public:
79 static char ID; // Pass identification, replacement for typeid
80 UnreachableBlockElimLegacyPass() : FunctionPass(ID) {
81 initializeUnreachableBlockElimLegacyPassPass(
82 *PassRegistry::getPassRegistry());
83 }
84
85 void getAnalysisUsage(AnalysisUsage &AU) const override {
86 AU.addPreserved();
87 }
88 };
89 }
90 char UnreachableBlockElimLegacyPass::ID = 0;
91 INITIALIZE_PASS(UnreachableBlockElimLegacyPass, "unreachableblockelim",
92 "Remove unreachable blocks from the CFG", false, false)
93
94 FunctionPass *llvm::createUnreachableBlockEliminationPass() {
95 return new UnreachableBlockElimLegacyPass();
96 }
97
98 PreservedAnalyses UnreachableBlockElimPass::run(Function &F,
99 FunctionAnalysisManager &AM) {
100 bool Changed = eliminateUnreachableBlock(F);
101 if (!Changed)
102 return PreservedAnalyses::all();
103 PreservedAnalyses PA;
104 PA.preserve();
105 return PA;
106 }
93107
94108 namespace {
95109 class UnreachableMachineBlockElim : public MachineFunctionPass {
4646 #include "llvm/Analysis/TargetTransformInfo.h"
4747 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
4848 #include "llvm/CodeGen/PreISelIntrinsicLowering.h"
49 #include "llvm/CodeGen/UnreachableBlockElim.h"
4950 #include "llvm/IR/Dominators.h"
5051 #include "llvm/IR/IRPrintingPasses.h"
5152 #include "llvm/IR/PassManager.h"
164164 FUNCTION_PASS("slp-vectorizer", SLPVectorizerPass())
165165 FUNCTION_PASS("sroa", SROA())
166166 FUNCTION_PASS("tailcallelim", TailCallElimPass())
167 FUNCTION_PASS("unreachableblockelim", UnreachableBlockElimPass())
167168 FUNCTION_PASS("verify", VerifierPass())
168169 FUNCTION_PASS("verify", DominatorTreeVerifierPass())
169170 FUNCTION_PASS("verify", MemorySSAVerifierPass())
0 ; RUN: opt -S < %s -unreachableblockelim | FileCheck %s
1 ; RUN: opt -S < %s -passes=unreachableblockelim | FileCheck %s
2
3 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
4 target triple = "x86_64-unknown-linux-gnu"
5
6 declare void @abort()
7
8 ; CHECK-LABEL: @foo(
9 ; CHECK-NOT return:
10 define void @foo(i32* %p) {
11 entry:
12 %p.addr = alloca i32*, align 8
13 call void @abort()
14 unreachable
15
16 return: ; No predecessors!
17 store i32* %p, i32** %p.addr, align 8
18 ret void
19 }
20
240240 initializeCodeGen(*Registry);
241241 initializeLoopStrengthReducePass(*Registry);
242242 initializeLowerIntrinsicsPass(*Registry);
243 initializeUnreachableBlockElimPass(*Registry);
243 initializeUnreachableBlockElimLegacyPassPass(*Registry);
244244
245245 // Register the target printer for --version.
246246 cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion);
363363 initializePreISelIntrinsicLoweringLegacyPassPass(Registry);
364364 initializeGlobalMergePass(Registry);
365365 initializeInterleavedAccessPass(Registry);
366 initializeUnreachableBlockElimLegacyPassPass(Registry);
366367
367368 #ifdef LINK_POLLY_INTO_TOOLS
368369 polly::initializePollyPasses(Registry);