2017-06-08 07:53:32 +08:00
|
|
|
//===- TailDuplication.cpp - Duplicate blocks into predecessors' tails ----===//
|
2009-11-26 08:32:21 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2009-11-26 08:32:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2018-01-19 14:08:17 +08:00
|
|
|
/// \file This pass duplicates basic blocks ending in unconditional branches
|
|
|
|
/// into the tails of their predecessors, using the TailDuplicator utility
|
|
|
|
/// class.
|
2009-11-26 08:32:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-12-06 01:39:37 +08:00
|
|
|
#include "llvm/Analysis/ProfileSummaryInfo.h"
|
|
|
|
#include "llvm/CodeGen/LazyMachineBlockFrequencyInfo.h"
|
2017-06-08 07:53:32 +08:00
|
|
|
#include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2009-11-26 08:32:21 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2017-12-13 10:51:01 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2016-04-09 04:35:01 +08:00
|
|
|
#include "llvm/CodeGen/TailDuplicator.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2017-06-08 07:53:32 +08:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
|
2009-11-26 08:32:21 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "tailduplication"
|
|
|
|
|
2009-11-26 08:32:21 +08:00
|
|
|
namespace {
|
2017-06-08 07:53:32 +08:00
|
|
|
|
2018-01-19 14:08:17 +08:00
|
|
|
class TailDuplicateBase : public MachineFunctionPass {
|
2016-04-09 04:35:01 +08:00
|
|
|
TailDuplicator Duplicator;
|
2020-01-30 01:36:31 +08:00
|
|
|
std::unique_ptr<MBFIWrapper> MBFIW;
|
2018-01-19 14:08:17 +08:00
|
|
|
bool PreRegAlloc;
|
2016-04-09 04:35:01 +08:00
|
|
|
public:
|
2018-01-19 14:08:17 +08:00
|
|
|
TailDuplicateBase(char &PassID, bool PreRegAlloc)
|
|
|
|
: MachineFunctionPass(PassID), PreRegAlloc(PreRegAlloc) {}
|
2009-11-26 08:32:21 +08:00
|
|
|
|
2016-04-09 04:35:01 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
2009-11-26 08:32:21 +08:00
|
|
|
|
2018-01-19 14:08:17 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.addRequired<MachineBranchProbabilityInfo>();
|
2019-12-06 01:39:37 +08:00
|
|
|
AU.addRequired<LazyMachineBlockFrequencyInfoPass>();
|
|
|
|
AU.addRequired<ProfileSummaryInfoWrapperPass>();
|
2018-01-19 14:08:17 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class TailDuplicate : public TailDuplicateBase {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
TailDuplicate() : TailDuplicateBase(ID, false) {
|
|
|
|
initializeTailDuplicatePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class EarlyTailDuplicate : public TailDuplicateBase {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
EarlyTailDuplicate() : TailDuplicateBase(ID, true) {
|
|
|
|
initializeEarlyTailDuplicatePass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2019-12-28 00:26:24 +08:00
|
|
|
|
|
|
|
MachineFunctionProperties getClearedProperties() const override {
|
|
|
|
return MachineFunctionProperties()
|
|
|
|
.set(MachineFunctionProperties::Property::NoPHIs);
|
|
|
|
}
|
2016-04-09 04:35:01 +08:00
|
|
|
};
|
2014-02-13 02:09:18 +08:00
|
|
|
|
2017-06-08 07:53:32 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2018-01-19 14:08:17 +08:00
|
|
|
char TailDuplicate::ID;
|
|
|
|
char EarlyTailDuplicate::ID;
|
2009-11-26 08:32:21 +08:00
|
|
|
|
2018-01-19 14:08:17 +08:00
|
|
|
char &llvm::TailDuplicateID = TailDuplicate::ID;
|
|
|
|
char &llvm::EarlyTailDuplicateID = EarlyTailDuplicate::ID;
|
2012-02-09 05:23:13 +08:00
|
|
|
|
2018-01-19 14:08:17 +08:00
|
|
|
INITIALIZE_PASS(TailDuplicate, DEBUG_TYPE, "Tail Duplication", false, false)
|
|
|
|
INITIALIZE_PASS(EarlyTailDuplicate, "early-tailduplication",
|
|
|
|
"Early Tail Duplication", false, false)
|
2009-11-26 08:32:21 +08:00
|
|
|
|
2018-01-19 14:08:17 +08:00
|
|
|
bool TailDuplicateBase::runOnMachineFunction(MachineFunction &MF) {
|
2017-12-16 06:22:58 +08:00
|
|
|
if (skipFunction(MF.getFunction()))
|
2014-04-01 01:43:35 +08:00
|
|
|
return false;
|
|
|
|
|
2016-04-09 04:35:01 +08:00
|
|
|
auto MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
|
2019-12-06 01:39:37 +08:00
|
|
|
auto *PSI = &getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();
|
|
|
|
auto *MBFI = (PSI && PSI->hasProfileSummary()) ?
|
|
|
|
&getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI() :
|
|
|
|
nullptr;
|
2020-01-30 01:36:31 +08:00
|
|
|
if (MBFI)
|
|
|
|
MBFIW = std::make_unique<MBFIWrapper>(*MBFI);
|
|
|
|
Duplicator.initMF(MF, PreRegAlloc, MBPI, MBFI ? MBFIW.get() : nullptr, PSI,
|
|
|
|
/*LayoutMode=*/false);
|
2009-11-26 08:32:21 +08:00
|
|
|
|
|
|
|
bool MadeChange = false;
|
2016-08-25 09:37:03 +08:00
|
|
|
while (Duplicator.tailDuplicateBlocks())
|
2010-01-16 03:59:57 +08:00
|
|
|
MadeChange = true;
|
2009-11-26 08:32:21 +08:00
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|