2017-06-08 01:53:32 +02:00
|
|
|
//===- llvm/CodeGen/TailDuplicator.h ----------------------------*- C++ -*-===//
|
2016-04-08 22:35:01 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01: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
|
2016-04-08 22:35:01 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines the TailDuplicator class. Used by the
|
|
|
|
// TailDuplication pass, and MachineBlockPlacement.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_CODEGEN_TAILDUPLICATOR_H
|
|
|
|
#define LLVM_CODEGEN_TAILDUPLICATOR_H
|
|
|
|
|
2017-06-08 01:53:32 +02:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/SetVector.h"
|
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2020-01-29 18:36:31 +01:00
|
|
|
#include "llvm/CodeGen/MBFIWrapper.h"
|
2017-11-08 02:01:31 +01:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-06-08 01:53:32 +02:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2016-04-08 22:35:01 +02:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2017-06-08 01:53:32 +02:00
|
|
|
class MachineBasicBlock;
|
2019-12-05 18:39:37 +01:00
|
|
|
class MachineBlockFrequencyInfo;
|
2017-06-08 01:53:32 +02:00
|
|
|
class MachineBranchProbabilityInfo;
|
|
|
|
class MachineFunction;
|
|
|
|
class MachineInstr;
|
|
|
|
class MachineModuleInfo;
|
|
|
|
class MachineRegisterInfo;
|
2019-12-05 18:39:37 +01:00
|
|
|
class ProfileSummaryInfo;
|
2017-06-08 01:53:32 +02:00
|
|
|
class TargetRegisterInfo;
|
2016-08-30 20:18:54 +02:00
|
|
|
|
2016-04-08 22:35:01 +02:00
|
|
|
/// Utility class to perform tail duplication.
|
|
|
|
class TailDuplicator {
|
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const MachineBranchProbabilityInfo *MBPI;
|
|
|
|
const MachineModuleInfo *MMI;
|
|
|
|
MachineRegisterInfo *MRI;
|
2016-08-25 03:37:03 +02:00
|
|
|
MachineFunction *MF;
|
2020-01-29 18:36:31 +01:00
|
|
|
MBFIWrapper *MBFI;
|
2019-12-05 18:39:37 +01:00
|
|
|
ProfileSummaryInfo *PSI;
|
2016-04-08 22:35:01 +02:00
|
|
|
bool PreRegAlloc;
|
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-11 22:36:43 +02:00
|
|
|
bool LayoutMode;
|
2016-08-17 23:07:35 +02:00
|
|
|
unsigned TailDupSize;
|
2016-04-08 22:35:01 +02:00
|
|
|
|
|
|
|
// A list of virtual registers for which to update SSA form.
|
2020-06-30 17:20:12 +02:00
|
|
|
SmallVector<Register, 16> SSAUpdateVRs;
|
2016-04-08 22:35:01 +02:00
|
|
|
|
|
|
|
// For each virtual register in SSAUpdateVals keep a list of source virtual
|
|
|
|
// registers.
|
2020-06-30 17:20:12 +02:00
|
|
|
using AvailableValsTy = std::vector<std::pair<MachineBasicBlock *, Register>>;
|
2016-04-08 22:35:01 +02:00
|
|
|
|
2020-06-30 17:20:12 +02:00
|
|
|
DenseMap<Register, AvailableValsTy> SSAUpdateVals;
|
2016-04-08 22:35:01 +02:00
|
|
|
|
|
|
|
public:
|
2016-08-17 23:07:35 +02:00
|
|
|
/// Prepare to run on a specific machine function.
|
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-11 22:36:43 +02:00
|
|
|
/// @param MF - Function that will be processed
|
2017-08-23 05:17:59 +02:00
|
|
|
/// @param PreRegAlloc - true if used before register allocation
|
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-11 22:36:43 +02:00
|
|
|
/// @param MBPI - Branch Probability Info. Used to propagate correct
|
|
|
|
/// probabilities when modifying the CFG.
|
|
|
|
/// @param LayoutMode - When true, don't use the existing layout to make
|
|
|
|
/// decisions.
|
|
|
|
/// @param TailDupSize - Maxmimum size of blocks to tail-duplicate. Zero
|
|
|
|
/// default implies using the command line value TailDupSize.
|
2017-08-23 05:17:59 +02:00
|
|
|
void initMF(MachineFunction &MF, bool PreRegAlloc,
|
2016-08-17 23:07:35 +02:00
|
|
|
const MachineBranchProbabilityInfo *MBPI,
|
2020-01-29 18:36:31 +01:00
|
|
|
MBFIWrapper *MBFI,
|
2019-12-05 18:39:37 +01:00
|
|
|
ProfileSummaryInfo *PSI,
|
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-11 22:36:43 +02:00
|
|
|
bool LayoutMode, unsigned TailDupSize = 0);
|
2017-06-08 01:53:32 +02:00
|
|
|
|
2016-08-25 03:37:03 +02:00
|
|
|
bool tailDuplicateBlocks();
|
2016-04-08 22:35:01 +02:00
|
|
|
static bool isSimpleBB(MachineBasicBlock *TailBB);
|
2016-08-25 03:37:03 +02:00
|
|
|
bool shouldTailDuplicate(bool IsSimple, MachineBasicBlock &TailBB);
|
2017-06-08 01:53:32 +02:00
|
|
|
|
2016-07-20 01:54:21 +02:00
|
|
|
/// Returns true if TailBB can successfully be duplicated into PredBB
|
|
|
|
bool canTailDuplicate(MachineBasicBlock *TailBB, MachineBasicBlock *PredBB);
|
2017-06-08 01:53:32 +02:00
|
|
|
|
2016-08-26 22:12:40 +02:00
|
|
|
/// Tail duplicate a single basic block into its predecessors, and then clean
|
|
|
|
/// up.
|
|
|
|
/// If \p DuplicatePreds is not null, it will be updated to contain the list
|
|
|
|
/// of predecessors that received a copy of \p MBB.
|
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-11 22:36:43 +02:00
|
|
|
/// If \p RemovalCallback is non-null. It will be called before MBB is
|
|
|
|
/// deleted.
|
2020-02-13 00:22:33 +01:00
|
|
|
/// If \p CandidatePtr is not null, duplicate into these blocks only.
|
2016-08-26 22:12:40 +02:00
|
|
|
bool tailDuplicateAndUpdate(
|
|
|
|
bool IsSimple, MachineBasicBlock *MBB,
|
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-11 22:36:43 +02:00
|
|
|
MachineBasicBlock *ForcedLayoutPred,
|
|
|
|
SmallVectorImpl<MachineBasicBlock*> *DuplicatedPreds = nullptr,
|
2020-02-13 00:22:33 +01:00
|
|
|
function_ref<void(MachineBasicBlock *)> *RemovalCallback = nullptr,
|
|
|
|
SmallVectorImpl<MachineBasicBlock *> *CandidatePtr = nullptr);
|
2016-04-08 22:35:01 +02:00
|
|
|
|
|
|
|
private:
|
2017-06-08 01:53:32 +02:00
|
|
|
using RegSubRegPair = TargetInstrInfo::RegSubRegPair;
|
2016-04-26 20:36:34 +02:00
|
|
|
|
2020-06-30 17:20:12 +02:00
|
|
|
void addSSAUpdateEntry(Register OrigReg, Register NewReg,
|
2016-04-08 22:35:01 +02:00
|
|
|
MachineBasicBlock *BB);
|
|
|
|
void processPHI(MachineInstr *MI, MachineBasicBlock *TailBB,
|
|
|
|
MachineBasicBlock *PredBB,
|
2020-06-30 17:20:12 +02:00
|
|
|
DenseMap<Register, RegSubRegPair> &LocalVRMap,
|
|
|
|
SmallVectorImpl<std::pair<Register, RegSubRegPair>> &Copies,
|
|
|
|
const DenseSet<Register> &UsedByPhi, bool Remove);
|
2016-04-08 22:35:01 +02:00
|
|
|
void duplicateInstruction(MachineInstr *MI, MachineBasicBlock *TailBB,
|
2016-08-25 03:37:03 +02:00
|
|
|
MachineBasicBlock *PredBB,
|
2020-06-30 17:20:12 +02:00
|
|
|
DenseMap<Register, RegSubRegPair> &LocalVRMap,
|
|
|
|
const DenseSet<Register> &UsedByPhi);
|
2016-04-08 22:35:01 +02:00
|
|
|
void updateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead,
|
|
|
|
SmallVectorImpl<MachineBasicBlock *> &TDBBs,
|
|
|
|
SmallSetVector<MachineBasicBlock *, 8> &Succs);
|
|
|
|
bool canCompletelyDuplicateBB(MachineBasicBlock &BB);
|
|
|
|
bool duplicateSimpleBB(MachineBasicBlock *TailBB,
|
|
|
|
SmallVectorImpl<MachineBasicBlock *> &TDBBs,
|
2020-06-30 17:20:12 +02:00
|
|
|
const DenseSet<Register> &RegsUsedByPhi,
|
2016-04-08 22:35:01 +02:00
|
|
|
SmallVectorImpl<MachineInstr *> &Copies);
|
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-11 22:36:43 +02:00
|
|
|
bool tailDuplicate(bool IsSimple,
|
|
|
|
MachineBasicBlock *TailBB,
|
|
|
|
MachineBasicBlock *ForcedLayoutPred,
|
2016-04-08 22:35:01 +02:00
|
|
|
SmallVectorImpl<MachineBasicBlock *> &TDBBs,
|
2020-02-13 00:22:33 +01:00
|
|
|
SmallVectorImpl<MachineInstr *> &Copies,
|
|
|
|
SmallVectorImpl<MachineBasicBlock *> *CandidatePtr);
|
2016-04-26 20:36:34 +02:00
|
|
|
void appendCopies(MachineBasicBlock *MBB,
|
2020-06-30 17:20:12 +02:00
|
|
|
SmallVectorImpl<std::pair<Register, RegSubRegPair>> &CopyInfos,
|
2016-04-26 20:36:34 +02:00
|
|
|
SmallVectorImpl<MachineInstr *> &Copies);
|
2016-04-08 22:35:01 +02: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-11 22:36:43 +02:00
|
|
|
void removeDeadBlock(
|
|
|
|
MachineBasicBlock *MBB,
|
2017-06-08 01:53:32 +02:00
|
|
|
function_ref<void(MachineBasicBlock *)> *RemovalCallback = nullptr);
|
2016-04-08 22:35:01 +02:00
|
|
|
};
|
|
|
|
|
2017-06-08 01:53:32 +02:00
|
|
|
} // end namespace llvm
|
2016-04-08 22:35:01 +02:00
|
|
|
|
2017-06-08 01:53:32 +02:00
|
|
|
#endif // LLVM_CODEGEN_TAILDUPLICATOR_H
|