2017-01-20 09:42:14 +01:00
|
|
|
//===-- LoopSink.cpp - Loop Sink Pass -------------------------------------===//
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +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
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass does the inverse transformation of what LICM does.
|
|
|
|
// It traverses all of the instructions in the loop's preheader and sinks
|
|
|
|
// them to the loop body where frequency is lower than the loop's preheader.
|
|
|
|
// This pass is a reverse-transformation of LICM. It differs from the Sink
|
|
|
|
// pass in the following ways:
|
|
|
|
//
|
|
|
|
// * It only handles sinking of instructions from the loop's preheader to the
|
|
|
|
// loop's body
|
|
|
|
// * It uses alias set tracker to get more accurate alias info
|
|
|
|
// * It uses block frequency info to find the optimal sinking locations
|
|
|
|
//
|
|
|
|
// Overall algorithm:
|
|
|
|
//
|
|
|
|
// For I in Preheader:
|
|
|
|
// InsertBBs = BBs that uses I
|
|
|
|
// For BB in sorted(LoopBBs):
|
|
|
|
// DomBBs = BBs in InsertBBs that are dominated by BB
|
|
|
|
// if freq(DomBBs) > freq(BB)
|
|
|
|
// InsertBBs = UseBBs - DomBBs + BB
|
|
|
|
// For BB in InsertBBs:
|
|
|
|
// Insert I at BB's beginning
|
2017-01-20 09:42:14 +01:00
|
|
|
//
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-01-20 09:42:19 +01:00
|
|
|
#include "llvm/Transforms/Scalar/LoopSink.h"
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/AliasSetTracker.h"
|
|
|
|
#include "llvm/Analysis/BasicAliasAnalysis.h"
|
|
|
|
#include "llvm/Analysis/BlockFrequencyInfo.h"
|
|
|
|
#include "llvm/Analysis/Loads.h"
|
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Metadata.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-13 22:15:01 +01:00
|
|
|
#include "llvm/InitializePasses.h"
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2017-01-11 10:43:56 +01:00
|
|
|
#include "llvm/Transforms/Scalar/LoopPassManager.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-13 22:15:01 +01:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
#include "llvm/Transforms/Utils/LoopUtils.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "loopsink"
|
|
|
|
|
|
|
|
STATISTIC(NumLoopSunk, "Number of instructions sunk into loop");
|
|
|
|
STATISTIC(NumLoopSunkCloned, "Number of cloned instructions sunk into loop");
|
|
|
|
|
|
|
|
static cl::opt<unsigned> SinkFrequencyPercentThreshold(
|
|
|
|
"sink-freq-percent-threshold", cl::Hidden, cl::init(90),
|
|
|
|
cl::desc("Do not sink instructions that require cloning unless they "
|
|
|
|
"execute less than this percent of the time."));
|
|
|
|
|
|
|
|
static cl::opt<unsigned> MaxNumberOfUseBBsForSinking(
|
|
|
|
"max-uses-for-sinking", cl::Hidden, cl::init(30),
|
|
|
|
cl::desc("Do not sink instructions that have too many uses."));
|
|
|
|
|
|
|
|
/// Return adjusted total frequency of \p BBs.
|
|
|
|
///
|
|
|
|
/// * If there is only one BB, sinking instruction will not introduce code
|
|
|
|
/// size increase. Thus there is no need to adjust the frequency.
|
|
|
|
/// * If there are more than one BB, sinking would lead to code size increase.
|
|
|
|
/// In this case, we add some "tax" to the total frequency to make it harder
|
|
|
|
/// to sink. E.g.
|
|
|
|
/// Freq(Preheader) = 100
|
|
|
|
/// Freq(BBs) = sum(50, 49) = 99
|
|
|
|
/// Even if Freq(BBs) < Freq(Preheader), we will not sink from Preheade to
|
|
|
|
/// BBs as the difference is too small to justify the code size increase.
|
|
|
|
/// To model this, The adjusted Freq(BBs) will be:
|
|
|
|
/// AdjustedFreq(BBs) = 99 / SinkFrequencyPercentThreshold%
|
|
|
|
static BlockFrequency adjustedSumFreq(SmallPtrSetImpl<BasicBlock *> &BBs,
|
|
|
|
BlockFrequencyInfo &BFI) {
|
|
|
|
BlockFrequency T = 0;
|
|
|
|
for (BasicBlock *B : BBs)
|
|
|
|
T += BFI.getBlockFreq(B);
|
|
|
|
if (BBs.size() > 1)
|
|
|
|
T /= BranchProbability(SinkFrequencyPercentThreshold, 100);
|
|
|
|
return T;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a set of basic blocks to insert sinked instructions.
|
|
|
|
///
|
|
|
|
/// The returned set of basic blocks (BBsToSinkInto) should satisfy:
|
|
|
|
///
|
|
|
|
/// * Inside the loop \p L
|
|
|
|
/// * For each UseBB in \p UseBBs, there is at least one BB in BBsToSinkInto
|
|
|
|
/// that domintates the UseBB
|
|
|
|
/// * Has minimum total frequency that is no greater than preheader frequency
|
|
|
|
///
|
|
|
|
/// The purpose of the function is to find the optimal sinking points to
|
|
|
|
/// minimize execution cost, which is defined as "sum of frequency of
|
|
|
|
/// BBsToSinkInto".
|
|
|
|
/// As a result, the returned BBsToSinkInto needs to have minimum total
|
|
|
|
/// frequency.
|
|
|
|
/// Additionally, if the total frequency of BBsToSinkInto exceeds preheader
|
|
|
|
/// frequency, the optimal solution is not sinking (return empty set).
|
|
|
|
///
|
|
|
|
/// \p ColdLoopBBs is used to help find the optimal sinking locations.
|
|
|
|
/// It stores a list of BBs that is:
|
|
|
|
///
|
|
|
|
/// * Inside the loop \p L
|
|
|
|
/// * Has a frequency no larger than the loop's preheader
|
|
|
|
/// * Sorted by BB frequency
|
|
|
|
///
|
|
|
|
/// The complexity of the function is O(UseBBs.size() * ColdLoopBBs.size()).
|
|
|
|
/// To avoid expensive computation, we cap the maximum UseBBs.size() in its
|
|
|
|
/// caller.
|
|
|
|
static SmallPtrSet<BasicBlock *, 2>
|
|
|
|
findBBsToSinkInto(const Loop &L, const SmallPtrSetImpl<BasicBlock *> &UseBBs,
|
|
|
|
const SmallVectorImpl<BasicBlock *> &ColdLoopBBs,
|
|
|
|
DominatorTree &DT, BlockFrequencyInfo &BFI) {
|
|
|
|
SmallPtrSet<BasicBlock *, 2> BBsToSinkInto;
|
|
|
|
if (UseBBs.size() == 0)
|
|
|
|
return BBsToSinkInto;
|
|
|
|
|
|
|
|
BBsToSinkInto.insert(UseBBs.begin(), UseBBs.end());
|
|
|
|
SmallPtrSet<BasicBlock *, 2> BBsDominatedByColdestBB;
|
|
|
|
|
|
|
|
// For every iteration:
|
|
|
|
// * Pick the ColdestBB from ColdLoopBBs
|
|
|
|
// * Find the set BBsDominatedByColdestBB that satisfy:
|
|
|
|
// - BBsDominatedByColdestBB is a subset of BBsToSinkInto
|
|
|
|
// - Every BB in BBsDominatedByColdestBB is dominated by ColdestBB
|
|
|
|
// * If Freq(ColdestBB) < Freq(BBsDominatedByColdestBB), remove
|
|
|
|
// BBsDominatedByColdestBB from BBsToSinkInto, add ColdestBB to
|
|
|
|
// BBsToSinkInto
|
|
|
|
for (BasicBlock *ColdestBB : ColdLoopBBs) {
|
|
|
|
BBsDominatedByColdestBB.clear();
|
|
|
|
for (BasicBlock *SinkedBB : BBsToSinkInto)
|
|
|
|
if (DT.dominates(ColdestBB, SinkedBB))
|
|
|
|
BBsDominatedByColdestBB.insert(SinkedBB);
|
|
|
|
if (BBsDominatedByColdestBB.size() == 0)
|
|
|
|
continue;
|
|
|
|
if (adjustedSumFreq(BBsDominatedByColdestBB, BFI) >
|
|
|
|
BFI.getBlockFreq(ColdestBB)) {
|
|
|
|
for (BasicBlock *DominatedBB : BBsDominatedByColdestBB) {
|
|
|
|
BBsToSinkInto.erase(DominatedBB);
|
|
|
|
}
|
|
|
|
BBsToSinkInto.insert(ColdestBB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 08:55:27 +02:00
|
|
|
// Can't sink into blocks that have no valid insertion point.
|
|
|
|
for (BasicBlock *BB : BBsToSinkInto) {
|
|
|
|
if (BB->getFirstInsertionPt() == BB->end()) {
|
|
|
|
BBsToSinkInto.clear();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
// If the total frequency of BBsToSinkInto is larger than preheader frequency,
|
|
|
|
// do not sink.
|
|
|
|
if (adjustedSumFreq(BBsToSinkInto, BFI) >
|
|
|
|
BFI.getBlockFreq(L.getLoopPreheader()))
|
|
|
|
BBsToSinkInto.clear();
|
|
|
|
return BBsToSinkInto;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sinks \p I from the loop \p L's preheader to its uses. Returns true if
|
|
|
|
// sinking is successful.
|
|
|
|
// \p LoopBlockNumber is used to sort the insertion blocks to ensure
|
|
|
|
// determinism.
|
|
|
|
static bool sinkInstruction(Loop &L, Instruction &I,
|
|
|
|
const SmallVectorImpl<BasicBlock *> &ColdLoopBBs,
|
|
|
|
const SmallDenseMap<BasicBlock *, int, 16> &LoopBlockNumber,
|
|
|
|
LoopInfo &LI, DominatorTree &DT,
|
|
|
|
BlockFrequencyInfo &BFI) {
|
|
|
|
// Compute the set of blocks in loop L which contain a use of I.
|
|
|
|
SmallPtrSet<BasicBlock *, 2> BBs;
|
|
|
|
for (auto &U : I.uses()) {
|
|
|
|
Instruction *UI = cast<Instruction>(U.getUser());
|
|
|
|
// We cannot sink I to PHI-uses.
|
|
|
|
if (dyn_cast<PHINode>(UI))
|
|
|
|
return false;
|
|
|
|
// We cannot sink I if it has uses outside of the loop.
|
|
|
|
if (!L.contains(LI.getLoopFor(UI->getParent())))
|
|
|
|
return false;
|
|
|
|
BBs.insert(UI->getParent());
|
|
|
|
}
|
|
|
|
|
|
|
|
// findBBsToSinkInto is O(BBs.size() * ColdLoopBBs.size()). We cap the max
|
|
|
|
// BBs.size() to avoid expensive computation.
|
|
|
|
// FIXME: Handle code size growth for min_size and opt_size.
|
|
|
|
if (BBs.size() > MaxNumberOfUseBBsForSinking)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Find the set of BBs that we should insert a copy of I.
|
|
|
|
SmallPtrSet<BasicBlock *, 2> BBsToSinkInto =
|
|
|
|
findBBsToSinkInto(L, BBs, ColdLoopBBs, DT, BFI);
|
|
|
|
if (BBsToSinkInto.empty())
|
|
|
|
return false;
|
|
|
|
|
2018-11-07 19:26:24 +01:00
|
|
|
// Return if any of the candidate blocks to sink into is non-cold.
|
|
|
|
if (BBsToSinkInto.size() > 1) {
|
|
|
|
for (auto *BB : BBsToSinkInto)
|
|
|
|
if (!LoopBlockNumber.count(BB))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
// Copy the final BBs into a vector and sort them using the total ordering
|
|
|
|
// of the loop block numbers as iterating the set doesn't give a useful
|
|
|
|
// order. No need to stable sort as the block numbers are a total ordering.
|
|
|
|
SmallVector<BasicBlock *, 2> SortedBBsToSinkInto;
|
|
|
|
SortedBBsToSinkInto.insert(SortedBBsToSinkInto.begin(), BBsToSinkInto.begin(),
|
|
|
|
BBsToSinkInto.end());
|
llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.
Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb
Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D52573
llvm-svn: 343163
2018-09-27 04:13:45 +02:00
|
|
|
llvm::sort(SortedBBsToSinkInto, [&](BasicBlock *A, BasicBlock *B) {
|
|
|
|
return LoopBlockNumber.find(A)->second < LoopBlockNumber.find(B)->second;
|
|
|
|
});
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
|
|
|
|
BasicBlock *MoveBB = *SortedBBsToSinkInto.begin();
|
|
|
|
// FIXME: Optimize the efficiency for cloned value replacement. The current
|
|
|
|
// implementation is O(SortedBBsToSinkInto.size() * I.num_uses()).
|
2018-07-06 16:20:58 +02:00
|
|
|
for (BasicBlock *N : makeArrayRef(SortedBBsToSinkInto).drop_front(1)) {
|
|
|
|
assert(LoopBlockNumber.find(N)->second >
|
|
|
|
LoopBlockNumber.find(MoveBB)->second &&
|
|
|
|
"BBs not sorted!");
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
// Clone I and replace its uses.
|
|
|
|
Instruction *IC = I.clone();
|
|
|
|
IC->setName(I.getName());
|
|
|
|
IC->insertBefore(&*N->getFirstInsertionPt());
|
|
|
|
// Replaces uses of I with IC in N
|
[IR] Value: add replaceUsesWithIf() utility
Summary:
While there is always a `Value::replaceAllUsesWith()`,
sometimes the replacement needs to be conditional.
I have only cleaned a few cases where `replaceUsesWithIf()`
could be used, to both add test coverage,
and show that it is actually useful.
Reviewers: jdoerfert, spatel, RKSimon, craig.topper
Reviewed By: jdoerfert
Subscribers: dschuff, sbc100, jgravelle-google, hiraditya, aheejin, george.burgess.iv, asbirlea, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65528
llvm-svn: 367548
2019-08-01 14:32:08 +02:00
|
|
|
I.replaceUsesWithIf(IC, [N](Use &U) {
|
|
|
|
return cast<Instruction>(U.getUser())->getParent() == N;
|
|
|
|
});
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
// Replaces uses of I with IC in blocks dominated by N
|
|
|
|
replaceDominatedUsesWith(&I, IC, DT, N);
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Sinking a clone of " << I << " To: " << N->getName()
|
|
|
|
<< '\n');
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
NumLoopSunkCloned++;
|
|
|
|
}
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "Sinking " << I << " To: " << MoveBB->getName() << '\n');
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
NumLoopSunk++;
|
|
|
|
I.moveBefore(&*MoveBB->getFirstInsertionPt());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sinks instructions from loop's preheader to the loop body if the
|
|
|
|
/// sum frequency of inserted copy is smaller than preheader's frequency.
|
|
|
|
static bool sinkLoopInvariantInstructions(Loop &L, AAResults &AA, LoopInfo &LI,
|
|
|
|
DominatorTree &DT,
|
|
|
|
BlockFrequencyInfo &BFI,
|
|
|
|
ScalarEvolution *SE) {
|
|
|
|
BasicBlock *Preheader = L.getLoopPreheader();
|
|
|
|
if (!Preheader)
|
|
|
|
return false;
|
|
|
|
|
2016-11-09 01:58:19 +01:00
|
|
|
// Enable LoopSink only when runtime profile is available.
|
|
|
|
// With static profile, the sinking decision may be sub-optimal.
|
2017-12-22 02:33:52 +01:00
|
|
|
if (!Preheader->getParent()->hasProfileData())
|
2016-11-09 01:58:19 +01:00
|
|
|
return false;
|
|
|
|
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
const BlockFrequency PreheaderFreq = BFI.getBlockFreq(Preheader);
|
|
|
|
// If there are no basic blocks with lower frequency than the preheader then
|
|
|
|
// we can avoid the detailed analysis as we will never find profitable sinking
|
|
|
|
// opportunities.
|
|
|
|
if (all_of(L.blocks(), [&](const BasicBlock *BB) {
|
|
|
|
return BFI.getBlockFreq(BB) > PreheaderFreq;
|
|
|
|
}))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
AliasSetTracker CurAST(AA);
|
|
|
|
|
|
|
|
// Compute alias set.
|
|
|
|
for (BasicBlock *BB : L.blocks())
|
|
|
|
CurAST.add(*BB);
|
2018-11-20 17:49:07 +01:00
|
|
|
CurAST.add(*Preheader);
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
|
|
|
|
// Sort loop's basic blocks by frequency
|
|
|
|
SmallVector<BasicBlock *, 10> ColdLoopBBs;
|
|
|
|
SmallDenseMap<BasicBlock *, int, 16> LoopBlockNumber;
|
|
|
|
int i = 0;
|
|
|
|
for (BasicBlock *B : L.blocks())
|
|
|
|
if (BFI.getBlockFreq(B) < BFI.getBlockFreq(L.getLoopPreheader())) {
|
|
|
|
ColdLoopBBs.push_back(B);
|
|
|
|
LoopBlockNumber[B] = ++i;
|
|
|
|
}
|
2019-04-23 16:51:27 +02:00
|
|
|
llvm::stable_sort(ColdLoopBBs, [&](BasicBlock *A, BasicBlock *B) {
|
|
|
|
return BFI.getBlockFreq(A) < BFI.getBlockFreq(B);
|
|
|
|
});
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
|
|
|
|
// Traverse preheader's instructions in reverse order becaue if A depends
|
|
|
|
// on B (A appears after B), A needs to be sinked first before B can be
|
|
|
|
// sinked.
|
|
|
|
for (auto II = Preheader->rbegin(), E = Preheader->rend(); II != E;) {
|
|
|
|
Instruction *I = &*II++;
|
2017-01-10 01:39:49 +01:00
|
|
|
// No need to check for instruction's operands are loop invariant.
|
|
|
|
assert(L.hasLoopInvariantOperands(I) &&
|
|
|
|
"Insts in a loop's preheader should have loop invariant operands!");
|
2019-04-19 19:46:50 +02:00
|
|
|
if (!canSinkOrHoistInst(*I, &AA, &DT, &L, &CurAST, nullptr, false))
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
continue;
|
|
|
|
if (sinkInstruction(L, *I, ColdLoopBBs, LoopBlockNumber, LI, DT, BFI))
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Changed && SE)
|
|
|
|
SE->forgetLoopDispositions(&L);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2017-01-20 09:42:19 +01:00
|
|
|
PreservedAnalyses LoopSinkPass::run(Function &F, FunctionAnalysisManager &FAM) {
|
|
|
|
LoopInfo &LI = FAM.getResult<LoopAnalysis>(F);
|
|
|
|
// Nothing to do if there are no loops.
|
|
|
|
if (LI.empty())
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
|
|
|
AAResults &AA = FAM.getResult<AAManager>(F);
|
|
|
|
DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
|
|
|
|
BlockFrequencyInfo &BFI = FAM.getResult<BlockFrequencyAnalysis>(F);
|
|
|
|
|
|
|
|
// We want to do a postorder walk over the loops. Since loops are a tree this
|
|
|
|
// is equivalent to a reversed preorder walk and preorder is easy to compute
|
|
|
|
// without recursion. Since we reverse the preorder, we will visit siblings
|
|
|
|
// in reverse program order. This isn't expected to matter at all but is more
|
|
|
|
// consistent with sinking algorithms which generally work bottom-up.
|
|
|
|
SmallVector<Loop *, 4> PreorderLoops = LI.getLoopsInPreorder();
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
do {
|
|
|
|
Loop &L = *PreorderLoops.pop_back_val();
|
|
|
|
|
|
|
|
// Note that we don't pass SCEV here because it is only used to invalidate
|
|
|
|
// loops in SCEV and we don't preserve (or request) SCEV at all making that
|
|
|
|
// unnecessary.
|
|
|
|
Changed |= sinkLoopInvariantInstructions(L, AA, LI, DT, BFI,
|
|
|
|
/*ScalarEvolution*/ nullptr);
|
|
|
|
} while (!PreorderLoops.empty());
|
|
|
|
|
|
|
|
if (!Changed)
|
|
|
|
return PreservedAnalyses::all();
|
|
|
|
|
|
|
|
PreservedAnalyses PA;
|
|
|
|
PA.preserveSet<CFGAnalyses>();
|
|
|
|
return PA;
|
|
|
|
}
|
|
|
|
|
Add Loop Sink pass to reverse the LICM based of basic block frequency.
Summary: LICM may hoist instructions to preheader speculatively. Before code generation, we need to sink down the hoisted instructions inside to loop if it's beneficial. This pass is a reverse of LICM: looking at instructions in preheader and sinks the instruction to basic blocks inside the loop body if basic block frequency is smaller than the preheader frequency.
Reviewers: hfinkel, davidxl, chandlerc
Subscribers: anna, modocache, mgorny, beanz, reames, dberlin, chandlerc, mcrosier, junbuml, sanjoy, mzolotukhin, llvm-commits
Differential Revision: https://reviews.llvm.org/D22778
llvm-svn: 285308
2016-10-27 18:30:08 +02:00
|
|
|
namespace {
|
|
|
|
struct LegacyLoopSinkPass : public LoopPass {
|
|
|
|
static char ID;
|
|
|
|
LegacyLoopSinkPass() : LoopPass(ID) {
|
|
|
|
initializeLegacyLoopSinkPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnLoop(Loop *L, LPPassManager &LPM) override {
|
|
|
|
if (skipLoop(L))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
auto *SE = getAnalysisIfAvailable<ScalarEvolutionWrapperPass>();
|
|
|
|
return sinkLoopInvariantInstructions(
|
|
|
|
*L, getAnalysis<AAResultsWrapperPass>().getAAResults(),
|
|
|
|
getAnalysis<LoopInfoWrapperPass>().getLoopInfo(),
|
|
|
|
getAnalysis<DominatorTreeWrapperPass>().getDomTree(),
|
|
|
|
getAnalysis<BlockFrequencyInfoWrapperPass>().getBFI(),
|
|
|
|
SE ? &SE->getSE() : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addRequired<BlockFrequencyInfoWrapperPass>();
|
|
|
|
getLoopAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char LegacyLoopSinkPass::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(LegacyLoopSinkPass, "loop-sink", "Loop Sink", false,
|
|
|
|
false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopPass)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(BlockFrequencyInfoWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(LegacyLoopSinkPass, "loop-sink", "Loop Sink", false, false)
|
|
|
|
|
|
|
|
Pass *llvm::createLoopSinkPass() { return new LegacyLoopSinkPass(); }
|