#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/DomTreeUpdater.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/LoopIterator.h"
#include "llvm/Analysis/MemorySSA.h"
#include "llvm/Analysis/MemorySSAUpdater.h"
#include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/LoopPassManager.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
#include <optional>
usingnamespacellvm;
#define DEBUG_TYPE …
static cl::opt<bool> EnableTermFolding("enable-loop-simplifycfg-term-folding",
cl::init(true));
STATISTIC(NumTerminatorsFolded,
"Number of terminators folded to unconditional branches");
STATISTIC(NumLoopBlocksDeleted,
"Number of loop blocks deleted");
STATISTIC(NumLoopExitsDeleted,
"Number of loop exiting edges deleted");
static BasicBlock *getOnlyLiveSuccessor(BasicBlock *BB) { … }
static void removeBlockFromLoops(BasicBlock *BB, Loop *FirstLoop,
Loop *LastLoop = nullptr) { … }
static Loop *getInnermostLoopFor(SmallPtrSetImpl<BasicBlock *> &BBs,
Loop &L, LoopInfo &LI) { … }
namespace {
class ConstantTerminatorFoldingImpl { … };
}
static bool constantFoldTerminators(Loop &L, DominatorTree &DT, LoopInfo &LI,
ScalarEvolution &SE,
MemorySSAUpdater *MSSAU,
bool &IsLoopDeleted) { … }
static bool mergeBlocksIntoPredecessors(Loop &L, DominatorTree &DT,
LoopInfo &LI, MemorySSAUpdater *MSSAU,
ScalarEvolution &SE) { … }
static bool simplifyLoopCFG(Loop &L, DominatorTree &DT, LoopInfo &LI,
ScalarEvolution &SE, MemorySSAUpdater *MSSAU,
bool &IsLoopDeleted) { … }
PreservedAnalyses LoopSimplifyCFGPass::run(Loop &L, LoopAnalysisManager &AM,
LoopStandardAnalysisResults &AR,
LPMUpdater &LPMU) { … }