2010-05-07 23:40:13 +08:00
|
|
|
//===-- Sink.cpp - Code Sinking -------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass moves instructions into successor blocks, when possible, so that
|
|
|
|
// they aren't executed on paths where their results aren't needed.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2010-05-07 23:40:13 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2011-12-15 07:49:11 +08:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2014-03-04 19:45:46 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2014-07-11 00:07:11 +08:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-13 17:26:24 +08:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
2015-03-05 02:43:29 +08:00
|
|
|
#include "llvm/IR/Module.h"
|
2010-05-07 23:40:13 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "sink"
|
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
STATISTIC(NumSunk, "Number of instructions sunk");
|
2012-05-31 16:09:49 +08:00
|
|
|
STATISTIC(NumSinkIter, "Number of sinking iterations");
|
2010-05-07 23:40:13 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
class Sinking : public FunctionPass {
|
|
|
|
DominatorTree *DT;
|
|
|
|
LoopInfo *LI;
|
|
|
|
AliasAnalysis *AA;
|
|
|
|
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification
|
2010-10-20 01:21:58 +08:00
|
|
|
Sinking() : FunctionPass(ID) {
|
|
|
|
initializeSinkingPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnFunction(Function &F) override;
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2010-05-07 23:40:13 +08:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
FunctionPass::getAnalysisUsage(AU);
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
AU.addRequired<AAResultsWrapperPass>();
|
2014-01-13 21:07:17 +08:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2015-01-17 22:16:18 +08:00
|
|
|
AU.addRequired<LoopInfoWrapperPass>();
|
2014-01-13 21:07:17 +08:00
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
2015-01-17 22:16:18 +08:00
|
|
|
AU.addPreserved<LoopInfoWrapperPass>();
|
2010-05-07 23:40:13 +08:00
|
|
|
}
|
|
|
|
private:
|
|
|
|
bool ProcessBlock(BasicBlock &BB);
|
2014-08-21 13:55:13 +08:00
|
|
|
bool SinkInstruction(Instruction *I, SmallPtrSetImpl<Instruction*> &Stores);
|
2010-05-07 23:40:13 +08:00
|
|
|
bool AllUsesDominatedByBlock(Instruction *Inst, BasicBlock *BB) const;
|
2012-05-31 16:09:49 +08:00
|
|
|
bool IsAcceptableTarget(Instruction *Inst, BasicBlock *SuccToSinkTo) const;
|
2010-05-07 23:40:13 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
char Sinking::ID = 0;
|
2010-10-13 03:48:12 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(Sinking, "sink", "Code sinking", false, false)
|
2015-01-17 22:16:18 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
|
2014-01-13 21:07:17 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
|
2010-10-13 03:48:12 +08:00
|
|
|
INITIALIZE_PASS_END(Sinking, "sink", "Code sinking", false, false)
|
2010-05-07 23:40:13 +08:00
|
|
|
|
|
|
|
FunctionPass *llvm::createSinkingPass() { return new Sinking(); }
|
|
|
|
|
|
|
|
/// AllUsesDominatedByBlock - Return true if all uses of the specified value
|
|
|
|
/// occur in blocks dominated by the specified block.
|
2012-07-24 18:51:42 +08:00
|
|
|
bool Sinking::AllUsesDominatedByBlock(Instruction *Inst,
|
2010-05-07 23:40:13 +08:00
|
|
|
BasicBlock *BB) const {
|
|
|
|
// Ignoring debug uses is necessary so debug info doesn't affect the code.
|
|
|
|
// This may leave a referencing dbg_value in the original block, before
|
|
|
|
// the definition of the vreg. Dwarf generator handles this although the
|
|
|
|
// user might not get the right info at runtime.
|
2014-03-09 11:16:01 +08:00
|
|
|
for (Use &U : Inst->uses()) {
|
2010-05-07 23:40:13 +08:00
|
|
|
// Determine the block of the use.
|
2014-03-09 11:16:01 +08:00
|
|
|
Instruction *UseInst = cast<Instruction>(U.getUser());
|
2010-05-07 23:40:13 +08:00
|
|
|
BasicBlock *UseBlock = UseInst->getParent();
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(UseInst)) {
|
|
|
|
// PHI nodes use the operand in the predecessor block, not the block with
|
|
|
|
// the PHI.
|
2014-03-09 11:16:01 +08:00
|
|
|
unsigned Num = PHINode::getIncomingValueNumForOperand(U.getOperandNo());
|
2010-05-07 23:40:13 +08:00
|
|
|
UseBlock = PN->getIncomingBlock(Num);
|
|
|
|
}
|
|
|
|
// Check that it dominates.
|
|
|
|
if (!DT->dominates(BB, UseBlock))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sinking::runOnFunction(Function &F) {
|
2014-01-13 21:07:17 +08:00
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2015-01-17 22:16:18 +08:00
|
|
|
LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
|
2010-05-07 23:40:13 +08:00
|
|
|
|
2012-05-31 16:09:49 +08:00
|
|
|
bool MadeChange, EverMadeChange = false;
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2012-05-31 16:09:49 +08:00
|
|
|
do {
|
|
|
|
MadeChange = false;
|
|
|
|
DEBUG(dbgs() << "Sinking iteration " << NumSinkIter << "\n");
|
2010-05-07 23:40:13 +08:00
|
|
|
// Process all basic blocks.
|
2012-07-24 18:51:42 +08:00
|
|
|
for (Function::iterator I = F.begin(), E = F.end();
|
2010-05-07 23:40:13 +08:00
|
|
|
I != E; ++I)
|
|
|
|
MadeChange |= ProcessBlock(*I);
|
2012-05-31 16:09:49 +08:00
|
|
|
EverMadeChange |= MadeChange;
|
|
|
|
NumSinkIter++;
|
|
|
|
} while (MadeChange);
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
return EverMadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Sinking::ProcessBlock(BasicBlock &BB) {
|
|
|
|
// Can't sink anything out of a block that has less than two successors.
|
2015-10-23 04:29:08 +08:00
|
|
|
if (BB.getTerminator()->getNumSuccessors() <= 1) return false;
|
2010-05-07 23:40:13 +08:00
|
|
|
|
|
|
|
// Don't bother sinking code out of unreachable blocks. In addition to being
|
2012-07-24 18:51:42 +08:00
|
|
|
// unprofitable, it can also lead to infinite looping, because in an
|
|
|
|
// unreachable loop there may be nowhere to stop.
|
2010-05-07 23:40:13 +08:00
|
|
|
if (!DT->isReachableFromEntry(&BB)) return false;
|
|
|
|
|
|
|
|
bool MadeChange = false;
|
|
|
|
|
|
|
|
// Walk the basic block bottom-up. Remember if we saw a store.
|
|
|
|
BasicBlock::iterator I = BB.end();
|
|
|
|
--I;
|
|
|
|
bool ProcessedBegin = false;
|
|
|
|
SmallPtrSet<Instruction *, 8> Stores;
|
|
|
|
do {
|
2015-10-14 03:26:58 +08:00
|
|
|
Instruction *Inst = &*I; // The instruction to sink.
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
// Predecrement I (if it's not begin) so that it isn't invalidated by
|
|
|
|
// sinking.
|
|
|
|
ProcessedBegin = I == BB.begin();
|
|
|
|
if (!ProcessedBegin)
|
|
|
|
--I;
|
|
|
|
|
|
|
|
if (isa<DbgInfoIntrinsic>(Inst))
|
|
|
|
continue;
|
|
|
|
|
2016-02-19 06:09:30 +08:00
|
|
|
if (SinkInstruction(Inst, Stores)) {
|
|
|
|
++NumSunk;
|
|
|
|
MadeChange = true;
|
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
// If we just processed the first instruction in the block, we're done.
|
|
|
|
} while (!ProcessedBegin);
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
return MadeChange;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isSafeToMove(Instruction *Inst, AliasAnalysis *AA,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<Instruction *> &Stores) {
|
2010-05-07 23:40:13 +08:00
|
|
|
|
2011-09-02 05:21:24 +08:00
|
|
|
if (Inst->mayWriteToMemory()) {
|
|
|
|
Stores.insert(Inst);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LoadInst *L = dyn_cast<LoadInst>(Inst)) {
|
2015-06-17 15:18:54 +08:00
|
|
|
MemoryLocation Loc = MemoryLocation::get(L);
|
2014-08-25 07:23:06 +08:00
|
|
|
for (Instruction *S : Stores)
|
2015-07-23 07:15:57 +08:00
|
|
|
if (AA->getModRefInfo(S, Loc) & MRI_Mod)
|
2010-05-07 23:40:13 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
[IR] Reformulate LLVM's EH funclet IR
While we have successfully implemented a funclet-oriented EH scheme on
top of LLVM IR, our scheme has some notable deficiencies:
- catchendpad and cleanupendpad are necessary in the current design
but they are difficult to explain to others, even to seasoned LLVM
experts.
- catchendpad and cleanupendpad are optimization barriers. They cannot
be split and force all potentially throwing call-sites to be invokes.
This has a noticable effect on the quality of our code generation.
- catchpad, while similar in some aspects to invoke, is fairly awkward.
It is unsplittable, starts a funclet, and has control flow to other
funclets.
- The nesting relationship between funclets is currently a property of
control flow edges. Because of this, we are forced to carefully
analyze the flow graph to see if there might potentially exist illegal
nesting among funclets. While we have logic to clone funclets when
they are illegally nested, it would be nicer if we had a
representation which forbade them upfront.
Let's clean this up a bit by doing the following:
- Instead, make catchpad more like cleanuppad and landingpad: no control
flow, just a bunch of simple operands; catchpad would be splittable.
- Introduce catchswitch, a control flow instruction designed to model
the constraints of funclet oriented EH.
- Make funclet scoping explicit by having funclet instructions consume
the token produced by the funclet which contains them.
- Remove catchendpad and cleanupendpad. Their presence can be inferred
implicitly using coloring information.
N.B. The state numbering code for the CLR has been updated but the
veracity of it's output cannot be spoken for. An expert should take a
look to make sure the results are reasonable.
Reviewers: rnk, JosephTremoulet, andrew.w.kaylor
Differential Revision: http://reviews.llvm.org/D15139
llvm-svn: 255422
2015-12-12 13:38:55 +08:00
|
|
|
if (isa<TerminatorInst>(Inst) || isa<PHINode>(Inst) || Inst->isEHPad() ||
|
|
|
|
Inst->mayThrow())
|
2010-11-12 00:20:28 +08:00
|
|
|
return false;
|
|
|
|
|
2015-10-10 02:06:13 +08:00
|
|
|
// Convergent operations cannot be made control-dependent on additional
|
|
|
|
// values.
|
2015-06-02 01:20:31 +08:00
|
|
|
if (auto CS = CallSite(Inst)) {
|
|
|
|
if (CS.hasFnAttr(Attribute::Convergent))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-12 00:20:28 +08:00
|
|
|
return true;
|
2010-05-07 23:40:13 +08:00
|
|
|
}
|
|
|
|
|
2012-05-31 16:09:49 +08:00
|
|
|
/// IsAcceptableTarget - Return true if it is possible to sink the instruction
|
|
|
|
/// in the specified basic block.
|
2012-07-24 18:51:42 +08:00
|
|
|
bool Sinking::IsAcceptableTarget(Instruction *Inst,
|
|
|
|
BasicBlock *SuccToSinkTo) const {
|
2012-05-31 16:09:49 +08:00
|
|
|
assert(Inst && "Instruction to be sunk is null");
|
|
|
|
assert(SuccToSinkTo && "Candidate sink target is null");
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2012-05-31 16:09:49 +08:00
|
|
|
// It is not possible to sink an instruction into its own block. This can
|
|
|
|
// happen with loops.
|
|
|
|
if (Inst->getParent() == SuccToSinkTo)
|
|
|
|
return false;
|
2012-07-24 18:51:42 +08:00
|
|
|
|
[IR] Reformulate LLVM's EH funclet IR
While we have successfully implemented a funclet-oriented EH scheme on
top of LLVM IR, our scheme has some notable deficiencies:
- catchendpad and cleanupendpad are necessary in the current design
but they are difficult to explain to others, even to seasoned LLVM
experts.
- catchendpad and cleanupendpad are optimization barriers. They cannot
be split and force all potentially throwing call-sites to be invokes.
This has a noticable effect on the quality of our code generation.
- catchpad, while similar in some aspects to invoke, is fairly awkward.
It is unsplittable, starts a funclet, and has control flow to other
funclets.
- The nesting relationship between funclets is currently a property of
control flow edges. Because of this, we are forced to carefully
analyze the flow graph to see if there might potentially exist illegal
nesting among funclets. While we have logic to clone funclets when
they are illegally nested, it would be nicer if we had a
representation which forbade them upfront.
Let's clean this up a bit by doing the following:
- Instead, make catchpad more like cleanuppad and landingpad: no control
flow, just a bunch of simple operands; catchpad would be splittable.
- Introduce catchswitch, a control flow instruction designed to model
the constraints of funclet oriented EH.
- Make funclet scoping explicit by having funclet instructions consume
the token produced by the funclet which contains them.
- Remove catchendpad and cleanupendpad. Their presence can be inferred
implicitly using coloring information.
N.B. The state numbering code for the CLR has been updated but the
veracity of it's output cannot be spoken for. An expert should take a
look to make sure the results are reasonable.
Reviewers: rnk, JosephTremoulet, andrew.w.kaylor
Differential Revision: http://reviews.llvm.org/D15139
llvm-svn: 255422
2015-12-12 13:38:55 +08:00
|
|
|
// It's never legal to sink an instruction into a block which terminates in an
|
|
|
|
// EH-pad.
|
|
|
|
if (SuccToSinkTo->getTerminator()->isExceptional())
|
|
|
|
return false;
|
|
|
|
|
2012-07-24 18:51:42 +08:00
|
|
|
// If the block has multiple predecessors, this would introduce computation
|
2012-05-31 16:09:49 +08:00
|
|
|
// on different code paths. We could split the critical edge, but for now we
|
|
|
|
// just punt.
|
|
|
|
// FIXME: Split critical edges if not backedges.
|
|
|
|
if (SuccToSinkTo->getUniquePredecessor() != Inst->getParent()) {
|
|
|
|
// We cannot sink a load across a critical edge - there may be stores in
|
|
|
|
// other code paths.
|
2015-03-10 10:37:25 +08:00
|
|
|
if (!isSafeToSpeculativelyExecute(Inst))
|
2012-05-31 16:09:49 +08:00
|
|
|
return false;
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2012-05-31 16:09:49 +08:00
|
|
|
// We don't want to sink across a critical edge if we don't dominate the
|
|
|
|
// successor. We could be introducing calculations to new code paths.
|
|
|
|
if (!DT->dominates(Inst->getParent(), SuccToSinkTo))
|
|
|
|
return false;
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2012-05-31 16:09:49 +08:00
|
|
|
// Don't sink instructions into a loop.
|
2012-07-24 18:51:42 +08:00
|
|
|
Loop *succ = LI->getLoopFor(SuccToSinkTo);
|
|
|
|
Loop *cur = LI->getLoopFor(Inst->getParent());
|
2014-04-25 13:29:35 +08:00
|
|
|
if (succ != nullptr && succ != cur)
|
2012-05-31 16:09:49 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2012-05-31 16:09:49 +08:00
|
|
|
// Finally, check that all the uses of the instruction are actually
|
|
|
|
// dominated by the candidate
|
|
|
|
return AllUsesDominatedByBlock(Inst, SuccToSinkTo);
|
|
|
|
}
|
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
/// SinkInstruction - Determine whether it is safe to sink the specified machine
|
|
|
|
/// instruction out of its current block into a successor.
|
|
|
|
bool Sinking::SinkInstruction(Instruction *Inst,
|
2014-08-21 13:55:13 +08:00
|
|
|
SmallPtrSetImpl<Instruction *> &Stores) {
|
2014-03-21 23:51:51 +08:00
|
|
|
|
|
|
|
// Don't sink static alloca instructions. CodeGen assumes allocas outside the
|
|
|
|
// entry block are dynamically sized stack objects.
|
|
|
|
if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
|
|
|
|
if (AI->isStaticAlloca())
|
|
|
|
return false;
|
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
// Check if it's safe to move the instruction.
|
|
|
|
if (!isSafeToMove(Inst, AA, Stores))
|
|
|
|
return false;
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
// FIXME: This should include support for sinking instructions within the
|
|
|
|
// block they are currently in to shorten the live ranges. We often get
|
|
|
|
// instructions sunk into the top of a large block, but it would be better to
|
|
|
|
// also sink them down before their first use in the block. This xform has to
|
|
|
|
// be careful not to *increase* register pressure though, e.g. sinking
|
|
|
|
// "x = y + z" down if it kills y and z would increase the live ranges of y
|
|
|
|
// and z and only shrink the live range of x.
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
// SuccToSinkTo - This is the successor to sink this instruction to, once we
|
|
|
|
// decide.
|
2014-04-25 13:29:35 +08:00
|
|
|
BasicBlock *SuccToSinkTo = nullptr;
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
// Instructions can only be sunk if all their uses are in blocks
|
|
|
|
// dominated by one of the successors.
|
2012-05-31 16:09:49 +08:00
|
|
|
// Look at all the postdominators and see if we can sink it in one.
|
|
|
|
DomTreeNode *DTN = DT->getNode(Inst->getParent());
|
2012-07-24 18:51:42 +08:00
|
|
|
for (DomTreeNode::iterator I = DTN->begin(), E = DTN->end();
|
2014-04-25 13:29:35 +08:00
|
|
|
I != E && SuccToSinkTo == nullptr; ++I) {
|
2012-05-31 16:09:49 +08:00
|
|
|
BasicBlock *Candidate = (*I)->getBlock();
|
2012-07-24 18:51:42 +08:00
|
|
|
if ((*I)->getIDom()->getBlock() == Inst->getParent() &&
|
2012-05-31 16:09:49 +08:00
|
|
|
IsAcceptableTarget(Inst, Candidate))
|
|
|
|
SuccToSinkTo = Candidate;
|
|
|
|
}
|
|
|
|
|
2012-07-24 18:51:42 +08:00
|
|
|
// If no suitable postdominator was found, look at all the successors and
|
2012-05-31 16:09:49 +08:00
|
|
|
// decide which one we should sink to, if any.
|
2014-07-22 01:06:51 +08:00
|
|
|
for (succ_iterator I = succ_begin(Inst->getParent()),
|
|
|
|
E = succ_end(Inst->getParent()); I != E && !SuccToSinkTo; ++I) {
|
|
|
|
if (IsAcceptableTarget(Inst, *I))
|
|
|
|
SuccToSinkTo = *I;
|
2010-05-07 23:40:13 +08:00
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
// If we couldn't find a block to sink to, ignore this instruction.
|
2014-04-25 13:29:35 +08:00
|
|
|
if (!SuccToSinkTo)
|
2010-05-07 23:40:13 +08:00
|
|
|
return false;
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2012-05-31 16:09:49 +08:00
|
|
|
DEBUG(dbgs() << "Sink" << *Inst << " (";
|
2014-01-09 10:29:41 +08:00
|
|
|
Inst->getParent()->printAsOperand(dbgs(), false);
|
2012-05-31 16:09:49 +08:00
|
|
|
dbgs() << " -> ";
|
2014-01-09 10:29:41 +08:00
|
|
|
SuccToSinkTo->printAsOperand(dbgs(), false);
|
2012-05-31 16:09:49 +08:00
|
|
|
dbgs() << ")\n");
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2010-05-07 23:40:13 +08:00
|
|
|
// Move the instruction.
|
2015-10-14 03:26:58 +08:00
|
|
|
Inst->moveBefore(&*SuccToSinkTo->getFirstInsertionPt());
|
2010-05-07 23:40:13 +08:00
|
|
|
return true;
|
|
|
|
}
|