Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / Transforms / Scalar / FlattenCFGPass.cpp
blob31670b1464e41f45da64e22d40b859ce45d5db32
1 //===- FlattenCFGPass.cpp - CFG Flatten Pass ----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements flattening of CFG.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Analysis/AliasAnalysis.h"
14 #include "llvm/Transforms/Utils/Local.h"
15 #include "llvm/IR/CFG.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Transforms/Scalar.h"
18 using namespace llvm;
20 #define DEBUG_TYPE "flattencfg"
22 namespace {
23 struct FlattenCFGPass : public FunctionPass {
24 static char ID; // Pass identification, replacement for typeid
25 public:
26 FlattenCFGPass() : FunctionPass(ID) {
27 initializeFlattenCFGPassPass(*PassRegistry::getPassRegistry());
29 bool runOnFunction(Function &F) override;
31 void getAnalysisUsage(AnalysisUsage &AU) const override {
32 AU.addRequired<AAResultsWrapperPass>();
35 private:
36 AliasAnalysis *AA;
40 char FlattenCFGPass::ID = 0;
41 INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
42 false)
43 INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
44 INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
45 false)
47 // Public interface to the FlattenCFG pass
48 FunctionPass *llvm::createFlattenCFGPass() { return new FlattenCFGPass(); }
50 /// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function,
51 /// iterating until no more changes are made.
52 static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) {
53 bool Changed = false;
54 bool LocalChange = true;
55 while (LocalChange) {
56 LocalChange = false;
58 // Loop over all of the basic blocks and remove them if they are unneeded...
60 for (Function::iterator BBIt = F.begin(); BBIt != F.end();) {
61 if (FlattenCFG(&*BBIt++, AA)) {
62 LocalChange = true;
65 Changed |= LocalChange;
67 return Changed;
70 bool FlattenCFGPass::runOnFunction(Function &F) {
71 AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
72 bool EverChanged = false;
73 // iterativelyFlattenCFG can make some blocks dead.
74 while (iterativelyFlattenCFG(F, AA)) {
75 removeUnreachableBlocks(F);
76 EverChanged = true;
78 return EverChanged;