[NewPM] Port FlattenCFGPass to NPM

Reviewed By: aeubanks

Differential Revision: https://reviews.llvm.org/D115361
This commit is contained in:
Fraser Cormack 2021-12-08 17:54:30 +00:00
parent d69f5e197c
commit eb87f668fe
7 changed files with 63 additions and 18 deletions

View File

@ -164,7 +164,7 @@ void initializeFinalizeISelPass(PassRegistry&);
void initializeFinalizeMachineBundlesPass(PassRegistry&);
void initializeFixIrreduciblePass(PassRegistry &);
void initializeFixupStatepointCallerSavedPass(PassRegistry&);
void initializeFlattenCFGPassPass(PassRegistry&);
void initializeFlattenCFGLegacyPassPass(PassRegistry &);
void initializeFloat2IntLegacyPassPass(PassRegistry&);
void initializeForceFunctionAttrsLegacyPassPass(PassRegistry&);
void initializeForwardControlFlowIntegrityPass(PassRegistry&);

View File

@ -0,0 +1,25 @@
//===- FlattenCFG.h -------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// The FlattenCFG pass flattens a function's CFG using the FlattenCFG utility
// function, iteratively flattening until no further changes are made.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_TRANSFORMS_SCALAR_FLATTENCFG_H
#define LLVM_TRANSFORMS_SCALAR_FLATTENCFG_H
#include "llvm/IR/PassManager.h"
namespace llvm {
struct FlattenCFGPass : PassInfoMixin<FlattenCFGPass> {
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
};
} // namespace llvm
#endif // LLVM_TRANSFORMS_SCALAR_FLATTENCFG_H

View File

@ -151,6 +151,7 @@
#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
#include "llvm/Transforms/Scalar/DivRemPairs.h"
#include "llvm/Transforms/Scalar/EarlyCSE.h"
#include "llvm/Transforms/Scalar/FlattenCFG.h"
#include "llvm/Transforms/Scalar/Float2Int.h"
#include "llvm/Transforms/Scalar/GVN.h"
#include "llvm/Transforms/Scalar/GuardWidening.h"

View File

@ -253,6 +253,7 @@ FUNCTION_PASS("dse", DSEPass())
FUNCTION_PASS("dot-cfg", CFGPrinterPass())
FUNCTION_PASS("dot-cfg-only", CFGOnlyPrinterPass())
FUNCTION_PASS("fix-irreducible", FixIrreduciblePass())
FUNCTION_PASS("flattencfg", FlattenCFGPass())
FUNCTION_PASS("make-guards-explicit", MakeGuardsExplicitPass())
FUNCTION_PASS("gvn-hoist", GVNHoistPass())
FUNCTION_PASS("gvn-sink", GVNSinkPass())

View File

@ -13,10 +13,12 @@
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/PassManager.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/FlattenCFG.h"
#include "llvm/Transforms/Utils/Local.h"
using namespace llvm;
@ -24,11 +26,11 @@ using namespace llvm;
#define DEBUG_TYPE "flattencfg"
namespace {
struct FlattenCFGPass : public FunctionPass {
struct FlattenCFGLegacyPass : public FunctionPass {
static char ID; // Pass identification, replacement for typeid
public:
FlattenCFGPass() : FunctionPass(ID) {
initializeFlattenCFGPassPass(*PassRegistry::getPassRegistry());
FlattenCFGLegacyPass() : FunctionPass(ID) {
initializeFlattenCFGLegacyPassPass(*PassRegistry::getPassRegistry());
}
bool runOnFunction(Function &F) override;
@ -39,21 +41,10 @@ public:
private:
AliasAnalysis *AA;
};
}
char FlattenCFGPass::ID = 0;
INITIALIZE_PASS_BEGIN(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
false)
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
INITIALIZE_PASS_END(FlattenCFGPass, "flattencfg", "Flatten the CFG", false,
false)
// Public interface to the FlattenCFG pass
FunctionPass *llvm::createFlattenCFGPass() { return new FlattenCFGPass(); }
/// iterativelyFlattenCFG - Call FlattenCFG on all the blocks in the function,
/// iterating until no more changes are made.
static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) {
bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) {
bool Changed = false;
bool LocalChange = true;
@ -78,8 +69,22 @@ static bool iterativelyFlattenCFG(Function &F, AliasAnalysis *AA) {
}
return Changed;
}
} // namespace
bool FlattenCFGPass::runOnFunction(Function &F) {
char FlattenCFGLegacyPass::ID = 0;
INITIALIZE_PASS_BEGIN(FlattenCFGLegacyPass, "flattencfg", "Flatten the CFG",
false, false)
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
INITIALIZE_PASS_END(FlattenCFGLegacyPass, "flattencfg", "Flatten the CFG",
false, false)
// Public interface to the FlattenCFG pass
FunctionPass *llvm::createFlattenCFGPass() {
return new FlattenCFGLegacyPass();
}
bool FlattenCFGLegacyPass::runOnFunction(Function &F) {
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
bool EverChanged = false;
// iterativelyFlattenCFG can make some blocks dead.
@ -89,3 +94,15 @@ bool FlattenCFGPass::runOnFunction(Function &F) {
}
return EverChanged;
}
PreservedAnalyses FlattenCFGPass::run(Function &F,
FunctionAnalysisManager &AM) {
bool EverChanged = false;
AliasAnalysis *AA = &AM.getResult<AAManager>(F);
// iterativelyFlattenCFG can make some blocks dead.
while (iterativelyFlattenCFG(F, AA)) {
removeUnreachableBlocks(F);
EverChanged = true;
}
return EverChanged ? PreservedAnalyses::none() : PreservedAnalyses::all();
}

View File

@ -54,7 +54,7 @@ void llvm::initializeScalarOpts(PassRegistry &Registry) {
initializeMakeGuardsExplicitLegacyPassPass(Registry);
initializeGVNHoistLegacyPassPass(Registry);
initializeGVNSinkLegacyPassPass(Registry);
initializeFlattenCFGPassPass(Registry);
initializeFlattenCFGLegacyPassPass(Registry);
initializeIRCELegacyPassPass(Registry);
initializeIndVarSimplifyLegacyPassPass(Registry);
initializeInferAddressSpacesPass(Registry);

View File

@ -1,4 +1,5 @@
; RUN: opt -flattencfg -S < %s -enable-new-pm=0 | FileCheck %s
; RUN: opt -passes=flattencfg -S < %s | FileCheck %s
; This test checks whether the pass completes without a crash.