2017-06-08 07:53:32 +08:00
|
|
|
//===- TailDuplication.cpp - Duplicate blocks into predecessors' tails ----===//
|
2009-11-26 08:32:21 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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"
|
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;
|
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>();
|
|
|
|
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());
|
|
|
|
}
|
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>();
|
2018-01-19 14:08:17 +08:00
|
|
|
Duplicator.initMF(MF, PreRegAlloc, MBPI, /*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;
|
|
|
|
}
|