2009-11-26 08:32:21 +08:00
|
|
|
//===-- TailDuplication.cpp - Duplicate blocks into predecessors' tails ---===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass duplicates basic blocks ending in unconditional branches into
|
2016-04-09 04:35:01 +08:00
|
|
|
// the tails of their predecessors, using the TailDuplicator utility class.
|
2009-11-26 08:32:21 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2016-04-09 04:35:01 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
|
|
|
#include "llvm/CodeGen/TailDuplicator.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2009-11-26 08:32:21 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "tailduplication"
|
|
|
|
|
2009-11-26 08:32:21 +08:00
|
|
|
namespace {
|
2016-04-09 04:35:01 +08:00
|
|
|
/// Perform tail duplication. Delegates to TailDuplicator
|
|
|
|
class TailDuplicatePass : public MachineFunctionPass {
|
|
|
|
TailDuplicator Duplicator;
|
2009-11-26 08:32:21 +08:00
|
|
|
|
2016-04-09 04:35:01 +08:00
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
explicit TailDuplicatePass() : MachineFunctionPass(ID) {}
|
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
|
|
|
|
2016-04-09 04:35:01 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override;
|
|
|
|
};
|
2014-02-13 02:09:18 +08:00
|
|
|
|
2016-04-09 04:35:01 +08:00
|
|
|
char TailDuplicatePass::ID = 0;
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2009-11-26 08:32:21 +08:00
|
|
|
|
2012-02-09 05:23:13 +08:00
|
|
|
char &llvm::TailDuplicateID = TailDuplicatePass::ID;
|
|
|
|
|
2016-04-09 04:35:01 +08:00
|
|
|
INITIALIZE_PASS(TailDuplicatePass, "tailduplication", "Tail Duplication", false,
|
|
|
|
false)
|
2009-11-26 08:32:21 +08:00
|
|
|
|
2009-11-27 05:38:41 +08:00
|
|
|
bool TailDuplicatePass::runOnMachineFunction(MachineFunction &MF) {
|
2016-04-23 06:06:11 +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>();
|
2014-02-13 02:09:18 +08:00
|
|
|
|
Codegen: Tail-duplicate during placement.
The tail duplication pass uses an assumed layout when making duplication
decisions. This is fine, but passes up duplication opportunities that
may arise when blocks are outlined. Because we want the updated CFG to
affect subsequent placement decisions, this change must occur during
placement.
In order to achieve this goal, TailDuplicationPass is split into a
utility class, TailDuplicator, and the pass itself. The pass delegates
nearly everything to the TailDuplicator object, except for looping over
the blocks in a function. This allows the same code to be used for tail
duplication in both places.
This change, in concert with outlining optional branches, allows
triangle shaped code to perform much better, esepecially when the
taken/untaken branches are correlated, as it creates a second spine when
the tests are small enough.
Issue from previous rollback fixed, and a new test was added for that
case as well. Issue was worklist/scheduling/taildup issue in layout.
Issue from 2nd rollback fixed, with 2 additional tests. Issue was
tail merging/loop info/tail-duplication causing issue with loops that share
a header block.
Issue with early tail-duplication of blocks that branch to a fallthrough
predecessor fixed with test case: tail-dup-branch-to-fallthrough.ll
Differential revision: https://reviews.llvm.org/D18226
llvm-svn: 283934
2016-10-12 04:36:43 +08:00
|
|
|
Duplicator.initMF(MF, 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;
|
|
|
|
}
|
|
|
|
|
2014-02-13 02:09:18 +08:00
|
|
|
void TailDuplicatePass::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.addRequired<MachineBranchProbabilityInfo>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|