2004-07-24 01:56:30 +08:00
|
|
|
//===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===//
|
2003-11-20 11:32:25 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2003-11-20 11:32:25 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the LiveInterval analysis pass which is used
|
|
|
|
// by the Linear Scan Register allocator. This pass linearizes the
|
|
|
|
// basic blocks of the function in DFS order and uses the
|
|
|
|
// LiveVariables pass to conservatively compute live intervals for
|
|
|
|
// each virtual and physical register.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2005-09-21 12:19:09 +08:00
|
|
|
#include "llvm/CodeGen/LiveIntervalAnalysis.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "LiveRangeCalc.h"
|
|
|
|
#include "llvm/ADT/DenseSet.h"
|
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2008-07-25 08:02:30 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2003-11-20 11:32:25 +08:00
|
|
|
#include "llvm/CodeGen/LiveVariables.h"
|
2013-12-14 08:53:32 +08:00
|
|
|
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
|
2012-06-06 06:02:15 +08:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2003-11-20 11:32:25 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2007-12-31 12:13:23 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2003-11-20 11:32:25 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2012-11-29 03:13:06 +08:00
|
|
|
#include "llvm/CodeGen/VirtRegMap.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Value.h"
|
2013-06-18 03:00:36 +08:00
|
|
|
#include "llvm/Support/BlockFrequency.h"
|
2012-07-28 04:58:46 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 21:10:19 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
2004-09-04 02:19:51 +08:00
|
|
|
#include <algorithm>
|
2006-12-02 10:22:01 +08:00
|
|
|
#include <cmath>
|
2012-12-04 00:50:05 +08:00
|
|
|
#include <limits>
|
2003-11-20 11:32:25 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "regalloc"
|
|
|
|
|
2007-05-03 09:11:54 +08:00
|
|
|
char LiveIntervals::ID = 0;
|
2012-08-04 06:12:54 +08:00
|
|
|
char &llvm::LiveIntervalsID = LiveIntervals::ID;
|
2010-10-13 03:48:12 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals",
|
|
|
|
"Live Interval Analysis", false, false)
|
[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_DEPENDENCY(LiveVariables)
|
2012-02-10 12:10:36 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
|
2010-10-13 03:48:12 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
|
|
|
|
INITIALIZE_PASS_END(LiveIntervals, "liveintervals",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Live Interval Analysis", false, false)
|
2003-11-20 11:32:25 +08:00
|
|
|
|
2013-06-22 02:33:23 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
static cl::opt<bool> EnablePrecomputePhysRegs(
|
|
|
|
"precompute-phys-liveness", cl::Hidden,
|
|
|
|
cl::desc("Eagerly compute live intervals for all physreg units."));
|
|
|
|
#else
|
|
|
|
static bool EnablePrecomputePhysRegs = false;
|
|
|
|
#endif // NDEBUG
|
|
|
|
|
2014-12-10 09:12:30 +08:00
|
|
|
static cl::opt<bool> EnableSubRegLiveness(
|
|
|
|
"enable-subreg-liveness", cl::Hidden, cl::init(true),
|
|
|
|
cl::desc("Enable subregister liveness tracking."));
|
|
|
|
|
2015-02-07 02:42:41 +08:00
|
|
|
namespace llvm {
|
|
|
|
cl::opt<bool> UseSegmentSetForPhysRegs(
|
|
|
|
"use-segment-set-for-physregs", cl::Hidden, cl::init(true),
|
|
|
|
cl::desc(
|
|
|
|
"Use segment set for the computation of the live ranges of physregs."));
|
|
|
|
}
|
|
|
|
|
2006-08-25 06:43:55 +08:00
|
|
|
void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const {
|
2009-08-01 07:37:33 +08:00
|
|
|
AU.setPreservesCFG();
|
[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>();
|
|
|
|
AU.addPreserved<AAResultsWrapperPass>();
|
2013-02-09 08:04:07 +08:00
|
|
|
// LiveVariables isn't really required by this analysis, it is only required
|
|
|
|
// here to make sure it is live during TwoAddressInstructionPass and
|
|
|
|
// PHIElimination. This is temporary.
|
2004-08-04 17:46:26 +08:00
|
|
|
AU.addRequired<LiveVariables>();
|
2010-08-18 05:00:37 +08:00
|
|
|
AU.addPreserved<LiveVariables>();
|
2012-02-14 04:44:42 +08:00
|
|
|
AU.addPreservedID(MachineLoopInfoID);
|
2012-06-21 07:31:34 +08:00
|
|
|
AU.addRequiredTransitiveID(MachineDominatorsID);
|
2008-01-05 04:54:55 +08:00
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
2009-11-04 07:52:08 +08:00
|
|
|
AU.addPreserved<SlotIndexes>();
|
|
|
|
AU.addRequiredTransitive<SlotIndexes>();
|
2004-08-04 17:46:26 +08:00
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
|
|
|
|
2012-06-06 06:02:15 +08:00
|
|
|
LiveIntervals::LiveIntervals() : MachineFunctionPass(ID),
|
2014-04-14 08:51:57 +08:00
|
|
|
DomTree(nullptr), LRCalc(nullptr) {
|
2012-06-06 06:02:15 +08:00
|
|
|
initializeLiveIntervalsPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
|
|
|
LiveIntervals::~LiveIntervals() {
|
|
|
|
delete LRCalc;
|
|
|
|
}
|
|
|
|
|
2006-08-25 06:43:55 +08:00
|
|
|
void LiveIntervals::releaseMemory() {
|
2008-08-14 05:49:13 +08:00
|
|
|
// Free the live intervals themselves.
|
2012-06-23 04:37:52 +08:00
|
|
|
for (unsigned i = 0, e = VirtRegIntervals.size(); i != e; ++i)
|
|
|
|
delete VirtRegIntervals[TargetRegisterInfo::index2VirtReg(i)];
|
|
|
|
VirtRegIntervals.clear();
|
2012-02-09 01:33:45 +08:00
|
|
|
RegMaskSlots.clear();
|
|
|
|
RegMaskBits.clear();
|
2012-02-10 09:26:29 +08:00
|
|
|
RegMaskBlocks.clear();
|
2009-07-09 11:57:02 +08:00
|
|
|
|
2013-10-11 05:29:02 +08:00
|
|
|
for (unsigned i = 0, e = RegUnitRanges.size(); i != e; ++i)
|
|
|
|
delete RegUnitRanges[i];
|
|
|
|
RegUnitRanges.clear();
|
2012-06-06 06:02:15 +08:00
|
|
|
|
2010-06-26 19:30:59 +08:00
|
|
|
// Release VNInfo memory regions, VNInfo objects don't need to be dtor'd.
|
|
|
|
VNInfoAllocator.Reset();
|
2006-05-11 15:29:24 +08:00
|
|
|
}
|
|
|
|
|
2013-08-15 01:28:46 +08:00
|
|
|
/// runOnMachineFunction - calculates LiveIntervals
|
2008-05-29 04:54:50 +08:00
|
|
|
///
|
|
|
|
bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) {
|
2012-06-05 06:39:14 +08:00
|
|
|
MF = &fn;
|
|
|
|
MRI = &MF->getRegInfo();
|
2014-10-14 14:26:53 +08:00
|
|
|
TRI = MF->getSubtarget().getRegisterInfo();
|
|
|
|
TII = MF->getSubtarget().getInstrInfo();
|
[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();
|
2012-06-05 06:39:14 +08:00
|
|
|
Indexes = &getAnalysis<SlotIndexes>();
|
2012-06-21 07:31:34 +08:00
|
|
|
DomTree = &getAnalysis<MachineDominatorTree>();
|
2014-12-10 09:12:30 +08:00
|
|
|
|
|
|
|
if (EnableSubRegLiveness && MF->getSubtarget().enableSubRegLiveness())
|
|
|
|
MRI->enableSubRegLiveness(true);
|
|
|
|
|
2012-06-21 07:31:34 +08:00
|
|
|
if (!LRCalc)
|
2012-06-06 06:02:15 +08:00
|
|
|
LRCalc = new LiveRangeCalc();
|
2003-11-20 11:32:25 +08:00
|
|
|
|
2012-07-28 04:58:46 +08:00
|
|
|
// Allocate space for all virtual registers.
|
|
|
|
VirtRegIntervals.resize(MRI->getNumVirtRegs());
|
|
|
|
|
2013-02-09 08:04:07 +08:00
|
|
|
computeVirtRegs();
|
|
|
|
computeRegMasks();
|
2012-06-21 07:31:34 +08:00
|
|
|
computeLiveInRegUnits();
|
2012-06-06 06:02:15 +08:00
|
|
|
|
2013-06-22 02:33:23 +08:00
|
|
|
if (EnablePrecomputePhysRegs) {
|
|
|
|
// For stress testing, precompute live ranges of all physical register
|
|
|
|
// units, including reserved registers.
|
|
|
|
for (unsigned i = 0, e = TRI->getNumRegUnits(); i != e; ++i)
|
|
|
|
getRegUnit(i);
|
|
|
|
}
|
2004-09-30 23:59:17 +08:00
|
|
|
DEBUG(dump());
|
2004-08-04 17:46:26 +08:00
|
|
|
return true;
|
2003-11-20 11:32:25 +08:00
|
|
|
}
|
|
|
|
|
2004-09-30 23:59:17 +08:00
|
|
|
/// print - Implement the dump method.
|
2009-08-23 14:03:38 +08:00
|
|
|
void LiveIntervals::print(raw_ostream &OS, const Module* ) const {
|
2009-08-23 11:41:05 +08:00
|
|
|
OS << "********** INTERVALS **********\n";
|
2012-02-15 07:46:21 +08:00
|
|
|
|
2012-06-06 06:02:15 +08:00
|
|
|
// Dump the regunits.
|
2013-10-11 05:29:02 +08:00
|
|
|
for (unsigned i = 0, e = RegUnitRanges.size(); i != e; ++i)
|
|
|
|
if (LiveRange *LR = RegUnitRanges[i])
|
2013-10-11 05:29:05 +08:00
|
|
|
OS << PrintRegUnit(i, TRI) << ' ' << *LR << '\n';
|
2012-06-06 06:02:15 +08:00
|
|
|
|
2012-02-15 07:46:21 +08:00
|
|
|
// Dump the virtregs.
|
2012-06-23 04:37:52 +08:00
|
|
|
for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
|
|
|
|
if (hasInterval(Reg))
|
2013-10-11 05:29:05 +08:00
|
|
|
OS << getInterval(Reg) << '\n';
|
2012-06-23 04:37:52 +08:00
|
|
|
}
|
2004-09-30 23:59:17 +08:00
|
|
|
|
2012-11-10 03:18:49 +08:00
|
|
|
OS << "RegMasks:";
|
|
|
|
for (unsigned i = 0, e = RegMaskSlots.size(); i != e; ++i)
|
|
|
|
OS << ' ' << RegMaskSlots[i];
|
|
|
|
OS << '\n';
|
|
|
|
|
2009-09-15 05:33:42 +08:00
|
|
|
printInstrs(OS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LiveIntervals::printInstrs(raw_ostream &OS) const {
|
2009-08-23 11:41:05 +08:00
|
|
|
OS << "********** MACHINEINSTRS **********\n";
|
2012-06-05 06:39:14 +08:00
|
|
|
MF->print(OS, Indexes);
|
2004-09-30 23:59:17 +08:00
|
|
|
}
|
|
|
|
|
2012-09-12 06:23:19 +08:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2009-09-15 05:33:42 +08:00
|
|
|
void LiveIntervals::dumpInstrs() const {
|
2010-01-05 06:49:02 +08:00
|
|
|
printInstrs(dbgs());
|
2009-09-15 05:33:42 +08:00
|
|
|
}
|
2012-09-07 03:06:06 +08:00
|
|
|
#endif
|
2009-09-15 05:33:42 +08:00
|
|
|
|
2008-08-14 05:49:13 +08:00
|
|
|
LiveInterval* LiveIntervals::createInterval(unsigned reg) {
|
2013-11-13 08:15:44 +08:00
|
|
|
float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ?
|
|
|
|
llvm::huge_valf : 0.0F;
|
2008-08-14 05:49:13 +08:00
|
|
|
return new LiveInterval(reg, Weight);
|
2004-04-10 02:07:57 +08:00
|
|
|
}
|
2007-11-12 14:35:08 +08:00
|
|
|
|
2012-06-06 06:02:15 +08:00
|
|
|
|
2012-07-28 04:58:46 +08:00
|
|
|
/// computeVirtRegInterval - Compute the live interval of a virtual register,
|
|
|
|
/// based on defs and uses.
|
2013-10-11 05:28:57 +08:00
|
|
|
void LiveIntervals::computeVirtRegInterval(LiveInterval &LI) {
|
2012-07-28 04:58:46 +08:00
|
|
|
assert(LRCalc && "LRCalc not initialized.");
|
2013-10-11 05:28:57 +08:00
|
|
|
assert(LI.empty() && "Should only compute empty intervals.");
|
2015-09-23 06:37:44 +08:00
|
|
|
bool ShouldTrackSubRegLiveness = MRI->shouldTrackSubRegLiveness(LI.reg);
|
2012-07-28 04:58:46 +08:00
|
|
|
LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator());
|
2015-09-23 06:37:44 +08:00
|
|
|
LRCalc->calculate(LI, ShouldTrackSubRegLiveness);
|
|
|
|
bool SeparatedComponents = computeDeadValues(LI, nullptr);
|
|
|
|
if (SeparatedComponents) {
|
|
|
|
assert(ShouldTrackSubRegLiveness
|
|
|
|
&& "Separated components should only occur for unused subreg defs");
|
|
|
|
SmallVector<LiveInterval*, 8> SplitLIs;
|
|
|
|
splitSeparateComponents(LI, SplitLIs);
|
|
|
|
}
|
2012-07-28 04:58:46 +08:00
|
|
|
}
|
|
|
|
|
2012-07-28 05:56:39 +08:00
|
|
|
void LiveIntervals::computeVirtRegs() {
|
|
|
|
for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
|
|
|
|
if (MRI->reg_nodbg_empty(Reg))
|
|
|
|
continue;
|
2013-08-15 07:50:16 +08:00
|
|
|
createAndComputeVirtRegInterval(Reg);
|
2012-07-28 05:56:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LiveIntervals::computeRegMasks() {
|
|
|
|
RegMaskBlocks.resize(MF->getNumBlockIDs());
|
|
|
|
|
|
|
|
// Find all instructions with regmask operands.
|
2015-11-06 10:01:02 +08:00
|
|
|
for (MachineBasicBlock &MBB : *MF) {
|
|
|
|
std::pair<unsigned, unsigned> &RMB = RegMaskBlocks[MBB.getNumber()];
|
2012-07-28 05:56:39 +08:00
|
|
|
RMB.first = RegMaskSlots.size();
|
2015-11-07 01:06:38 +08:00
|
|
|
|
|
|
|
// Some block starts, such as EH funclets, create masks.
|
|
|
|
if (const uint32_t *Mask = MBB.getBeginClobberMask(TRI)) {
|
|
|
|
RegMaskSlots.push_back(Indexes->getMBBStartIdx(&MBB));
|
|
|
|
RegMaskBits.push_back(Mask);
|
|
|
|
}
|
|
|
|
|
2015-11-06 10:01:02 +08:00
|
|
|
for (MachineInstr &MI : MBB) {
|
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
2015-05-29 10:56:46 +08:00
|
|
|
if (!MO.isRegMask())
|
2012-07-28 05:56:39 +08:00
|
|
|
continue;
|
2015-11-06 10:01:02 +08:00
|
|
|
RegMaskSlots.push_back(Indexes->getInstructionIndex(&MI).getRegSlot());
|
|
|
|
RegMaskBits.push_back(MO.getRegMask());
|
2012-07-28 05:56:39 +08:00
|
|
|
}
|
2015-11-06 10:01:02 +08:00
|
|
|
}
|
2015-11-07 01:06:38 +08:00
|
|
|
|
|
|
|
// Some block ends, such as funclet returns, create masks.
|
|
|
|
if (const uint32_t *Mask = MBB.getEndClobberMask(TRI)) {
|
|
|
|
RegMaskSlots.push_back(Indexes->getMBBEndIdx(&MBB));
|
|
|
|
RegMaskBits.push_back(Mask);
|
|
|
|
}
|
|
|
|
|
2012-07-28 05:56:39 +08:00
|
|
|
// Compute the number of register mask instructions in this block.
|
2012-09-11 05:26:47 +08:00
|
|
|
RMB.second = RegMaskSlots.size() - RMB.first;
|
2012-07-28 05:56:39 +08:00
|
|
|
}
|
|
|
|
}
|
2012-07-28 04:58:46 +08:00
|
|
|
|
2012-06-06 06:02:15 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Register Unit Liveness
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Fixed interference typically comes from ABI boundaries: Function arguments
|
|
|
|
// and return values are passed in fixed registers, and so are exception
|
|
|
|
// pointers entering landing pads. Certain instructions require values to be
|
|
|
|
// present in specific registers. That is also represented through fixed
|
|
|
|
// interference.
|
|
|
|
//
|
|
|
|
|
2013-10-11 05:29:02 +08:00
|
|
|
/// computeRegUnitInterval - Compute the live range of a register unit, based
|
|
|
|
/// on the uses and defs of aliasing registers. The range should be empty,
|
2012-06-06 06:02:15 +08:00
|
|
|
/// or contain only dead phi-defs from ABI blocks.
|
2013-10-11 05:29:02 +08:00
|
|
|
void LiveIntervals::computeRegUnitRange(LiveRange &LR, unsigned Unit) {
|
2012-06-06 06:02:15 +08:00
|
|
|
assert(LRCalc && "LRCalc not initialized.");
|
|
|
|
LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator());
|
|
|
|
|
|
|
|
// The physregs aliasing Unit are the roots and their super-registers.
|
|
|
|
// Create all values as dead defs before extending to uses. Note that roots
|
|
|
|
// may share super-registers. That's OK because createDeadDefs() is
|
|
|
|
// idempotent. It is very rare for a register unit to have multiple roots, so
|
|
|
|
// uniquing super-registers is probably not worthwhile.
|
2014-12-16 05:36:35 +08:00
|
|
|
for (MCRegUnitRootIterator Roots(Unit, TRI); Roots.isValid(); ++Roots) {
|
|
|
|
for (MCSuperRegIterator Supers(*Roots, TRI, /*IncludeSelf=*/true);
|
|
|
|
Supers.isValid(); ++Supers) {
|
|
|
|
if (!MRI->reg_empty(*Supers))
|
|
|
|
LRCalc->createDeadDefs(LR, *Supers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now extend LR to reach all uses.
|
|
|
|
// Ignore uses of reserved registers. We only track defs of those.
|
2012-06-06 06:02:15 +08:00
|
|
|
for (MCRegUnitRootIterator Roots(Unit, TRI); Roots.isValid(); ++Roots) {
|
2013-05-23 06:36:55 +08:00
|
|
|
for (MCSuperRegIterator Supers(*Roots, TRI, /*IncludeSelf=*/true);
|
|
|
|
Supers.isValid(); ++Supers) {
|
2012-06-06 06:02:15 +08:00
|
|
|
unsigned Reg = *Supers;
|
2014-12-16 05:36:35 +08:00
|
|
|
if (!MRI->isReserved(Reg) && !MRI->reg_empty(Reg))
|
|
|
|
LRCalc->extendToUses(LR, Reg);
|
2012-06-06 06:02:15 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-07 02:42:41 +08:00
|
|
|
|
|
|
|
// Flush the segment set to the segment vector.
|
|
|
|
if (UseSegmentSetForPhysRegs)
|
|
|
|
LR.flushSegmentSet();
|
2012-06-06 06:02:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// computeLiveInRegUnits - Precompute the live ranges of any register units
|
|
|
|
/// that are live-in to an ABI block somewhere. Register values can appear
|
|
|
|
/// without a corresponding def when entering the entry block or a landing pad.
|
|
|
|
///
|
|
|
|
void LiveIntervals::computeLiveInRegUnits() {
|
2013-10-11 05:29:02 +08:00
|
|
|
RegUnitRanges.resize(TRI->getNumRegUnits());
|
2012-06-06 06:02:15 +08:00
|
|
|
DEBUG(dbgs() << "Computing live-in reg-units in ABI blocks.\n");
|
|
|
|
|
2013-10-11 05:29:02 +08:00
|
|
|
// Keep track of the live range sets allocated.
|
|
|
|
SmallVector<unsigned, 8> NewRanges;
|
2012-06-06 06:02:15 +08:00
|
|
|
|
|
|
|
// Check all basic blocks for live-ins.
|
|
|
|
for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end();
|
|
|
|
MFI != MFE; ++MFI) {
|
2015-10-10 03:13:58 +08:00
|
|
|
const MachineBasicBlock *MBB = &*MFI;
|
2012-06-06 06:02:15 +08:00
|
|
|
|
|
|
|
// We only care about ABI blocks: Entry + landing pads.
|
2015-08-28 07:27:47 +08:00
|
|
|
if ((MFI != MF->begin() && !MBB->isEHPad()) || MBB->livein_empty())
|
2012-06-06 06:02:15 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Create phi-defs at Begin for all live-in registers.
|
|
|
|
SlotIndex Begin = Indexes->getMBBStartIdx(MBB);
|
|
|
|
DEBUG(dbgs() << Begin << "\tBB#" << MBB->getNumber());
|
2015-09-10 02:08:03 +08:00
|
|
|
for (const auto &LI : MBB->liveins()) {
|
|
|
|
for (MCRegUnitIterator Units(LI.PhysReg, TRI); Units.isValid(); ++Units) {
|
2012-06-06 06:02:15 +08:00
|
|
|
unsigned Unit = *Units;
|
2013-10-11 05:29:02 +08:00
|
|
|
LiveRange *LR = RegUnitRanges[Unit];
|
|
|
|
if (!LR) {
|
2015-02-07 02:42:41 +08:00
|
|
|
// Use segment set to speed-up initial computation of the live range.
|
|
|
|
LR = RegUnitRanges[Unit] = new LiveRange(UseSegmentSetForPhysRegs);
|
2013-10-11 05:29:02 +08:00
|
|
|
NewRanges.push_back(Unit);
|
2012-06-06 06:02:15 +08:00
|
|
|
}
|
2013-10-11 05:29:02 +08:00
|
|
|
VNInfo *VNI = LR->createDeadDef(Begin, getVNInfoAllocator());
|
2012-06-06 07:00:03 +08:00
|
|
|
(void)VNI;
|
2012-06-06 06:02:15 +08:00
|
|
|
DEBUG(dbgs() << ' ' << PrintRegUnit(Unit, TRI) << '#' << VNI->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEBUG(dbgs() << '\n');
|
|
|
|
}
|
2013-10-11 05:29:02 +08:00
|
|
|
DEBUG(dbgs() << "Created " << NewRanges.size() << " new intervals.\n");
|
2012-06-06 06:02:15 +08:00
|
|
|
|
2013-10-11 05:29:02 +08:00
|
|
|
// Compute the 'normal' part of the ranges.
|
|
|
|
for (unsigned i = 0, e = NewRanges.size(); i != e; ++i) {
|
|
|
|
unsigned Unit = NewRanges[i];
|
|
|
|
computeRegUnitRange(*RegUnitRanges[Unit], Unit);
|
|
|
|
}
|
2012-06-06 06:02:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-10 09:12:18 +08:00
|
|
|
static void createSegmentsForValues(LiveRange &LR,
|
|
|
|
iterator_range<LiveInterval::vni_iterator> VNIs) {
|
|
|
|
for (auto VNI : VNIs) {
|
|
|
|
if (VNI->isUnused())
|
|
|
|
continue;
|
|
|
|
SlotIndex Def = VNI->def;
|
|
|
|
LR.addSegment(LiveRange::Segment(Def, Def.getDeadSlot(), VNI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef SmallVector<std::pair<SlotIndex, VNInfo*>, 16> ShrinkToUsesWorkList;
|
|
|
|
|
|
|
|
static void extendSegmentsToUses(LiveRange &LR, const SlotIndexes &Indexes,
|
|
|
|
ShrinkToUsesWorkList &WorkList,
|
|
|
|
const LiveRange &OldRange) {
|
|
|
|
// Keep track of the PHIs that are in use.
|
|
|
|
SmallPtrSet<VNInfo*, 8> UsedPHIs;
|
|
|
|
// Blocks that have already been added to WorkList as live-out.
|
|
|
|
SmallPtrSet<MachineBasicBlock*, 16> LiveOut;
|
|
|
|
|
|
|
|
// Extend intervals to reach all uses in WorkList.
|
|
|
|
while (!WorkList.empty()) {
|
|
|
|
SlotIndex Idx = WorkList.back().first;
|
|
|
|
VNInfo *VNI = WorkList.back().second;
|
|
|
|
WorkList.pop_back();
|
|
|
|
const MachineBasicBlock *MBB = Indexes.getMBBFromIndex(Idx.getPrevSlot());
|
|
|
|
SlotIndex BlockStart = Indexes.getMBBStartIdx(MBB);
|
|
|
|
|
|
|
|
// Extend the live range for VNI to be live at Idx.
|
|
|
|
if (VNInfo *ExtVNI = LR.extendInBlock(BlockStart, Idx)) {
|
|
|
|
assert(ExtVNI == VNI && "Unexpected existing value number");
|
|
|
|
(void)ExtVNI;
|
|
|
|
// Is this a PHIDef we haven't seen before?
|
|
|
|
if (!VNI->isPHIDef() || VNI->def != BlockStart ||
|
|
|
|
!UsedPHIs.insert(VNI).second)
|
|
|
|
continue;
|
|
|
|
// The PHI is live, make sure the predecessors are live-out.
|
|
|
|
for (auto &Pred : MBB->predecessors()) {
|
|
|
|
if (!LiveOut.insert(Pred).second)
|
|
|
|
continue;
|
|
|
|
SlotIndex Stop = Indexes.getMBBEndIdx(Pred);
|
|
|
|
// A predecessor is not required to have a live-out value for a PHI.
|
|
|
|
if (VNInfo *PVNI = OldRange.getVNInfoBefore(Stop))
|
|
|
|
WorkList.push_back(std::make_pair(Stop, PVNI));
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// VNI is live-in to MBB.
|
|
|
|
DEBUG(dbgs() << " live-in at " << BlockStart << '\n');
|
|
|
|
LR.addSegment(LiveRange::Segment(BlockStart, Idx, VNI));
|
|
|
|
|
|
|
|
// Make sure VNI is live-out from the predecessors.
|
|
|
|
for (auto &Pred : MBB->predecessors()) {
|
|
|
|
if (!LiveOut.insert(Pred).second)
|
|
|
|
continue;
|
|
|
|
SlotIndex Stop = Indexes.getMBBEndIdx(Pred);
|
|
|
|
assert(OldRange.getVNInfoBefore(Stop) == VNI &&
|
|
|
|
"Wrong value out of predecessor");
|
|
|
|
WorkList.push_back(std::make_pair(Stop, VNI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-18 04:37:07 +08:00
|
|
|
bool LiveIntervals::shrinkToUses(LiveInterval *li,
|
2011-03-08 07:29:10 +08:00
|
|
|
SmallVectorImpl<MachineInstr*> *dead) {
|
2011-02-08 08:03:05 +08:00
|
|
|
DEBUG(dbgs() << "Shrink: " << *li << '\n');
|
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(li->reg)
|
2012-01-04 04:05:57 +08:00
|
|
|
&& "Can only shrink virtual registers");
|
2011-02-08 08:03:05 +08:00
|
|
|
|
2014-12-10 09:12:18 +08:00
|
|
|
// Shrink subregister live ranges.
|
2015-07-17 02:55:35 +08:00
|
|
|
bool NeedsCleanup = false;
|
2014-12-11 08:59:06 +08:00
|
|
|
for (LiveInterval::SubRange &S : li->subranges()) {
|
|
|
|
shrinkToUses(S, li->reg);
|
2015-07-17 02:55:35 +08:00
|
|
|
if (S.empty())
|
|
|
|
NeedsCleanup = true;
|
2014-12-10 09:12:18 +08:00
|
|
|
}
|
2015-07-17 02:55:35 +08:00
|
|
|
if (NeedsCleanup)
|
|
|
|
li->removeEmptySubRanges();
|
2014-12-10 09:12:18 +08:00
|
|
|
|
|
|
|
// Find all the values used, including PHI kills.
|
|
|
|
ShrinkToUsesWorkList WorkList;
|
2011-09-15 23:24:16 +08:00
|
|
|
|
2011-02-08 08:03:05 +08:00
|
|
|
// Visit all instructions reading li->reg.
|
2014-03-13 14:02:25 +08:00
|
|
|
for (MachineRegisterInfo::reg_instr_iterator
|
|
|
|
I = MRI->reg_instr_begin(li->reg), E = MRI->reg_instr_end();
|
|
|
|
I != E; ) {
|
|
|
|
MachineInstr *UseMI = &*(I++);
|
2011-02-08 08:03:05 +08:00
|
|
|
if (UseMI->isDebugValue() || !UseMI->readsVirtualRegister(li->reg))
|
|
|
|
continue;
|
2011-11-14 07:53:25 +08:00
|
|
|
SlotIndex Idx = getInstructionIndex(UseMI).getRegSlot();
|
2013-10-11 05:28:52 +08:00
|
|
|
LiveQueryResult LRQ = li->Query(Idx);
|
2012-05-20 10:54:52 +08:00
|
|
|
VNInfo *VNI = LRQ.valueIn();
|
2011-03-18 11:06:04 +08:00
|
|
|
if (!VNI) {
|
|
|
|
// This shouldn't happen: readsVirtualRegister returns true, but there is
|
|
|
|
// no live value. It is likely caused by a target getting <undef> flags
|
|
|
|
// wrong.
|
|
|
|
DEBUG(dbgs() << Idx << '\t' << *UseMI
|
|
|
|
<< "Warning: Instr claims to read non-existent value in "
|
|
|
|
<< *li << '\n');
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-15 02:45:38 +08:00
|
|
|
// Special case: An early-clobber tied operand reads and writes the
|
2012-05-20 10:54:52 +08:00
|
|
|
// register one slot early.
|
|
|
|
if (VNInfo *DefVNI = LRQ.valueDefined())
|
|
|
|
Idx = DefVNI->def;
|
|
|
|
|
2011-02-08 08:03:05 +08:00
|
|
|
WorkList.push_back(std::make_pair(Idx, VNI));
|
|
|
|
}
|
|
|
|
|
2013-10-11 05:28:47 +08:00
|
|
|
// Create new live ranges with only minimal live segments per def.
|
|
|
|
LiveRange NewLR;
|
2014-12-10 09:12:18 +08:00
|
|
|
createSegmentsForValues(NewLR, make_range(li->vni_begin(), li->vni_end()));
|
|
|
|
extendSegmentsToUses(NewLR, *Indexes, WorkList, *li);
|
2011-02-08 08:03:05 +08:00
|
|
|
|
2014-06-04 06:42:10 +08:00
|
|
|
// Move the trimmed segments back.
|
|
|
|
li->segments.swap(NewLR.segments);
|
2014-12-19 03:58:52 +08:00
|
|
|
|
|
|
|
// Handle dead values.
|
|
|
|
bool CanSeparate = computeDeadValues(*li, dead);
|
2014-06-04 06:42:10 +08:00
|
|
|
DEBUG(dbgs() << "Shrunk: " << *li << '\n');
|
|
|
|
return CanSeparate;
|
|
|
|
}
|
|
|
|
|
2014-12-19 03:58:52 +08:00
|
|
|
bool LiveIntervals::computeDeadValues(LiveInterval &LI,
|
2014-06-04 06:42:10 +08:00
|
|
|
SmallVectorImpl<MachineInstr*> *dead) {
|
2015-09-23 06:37:44 +08:00
|
|
|
bool MayHaveSplitComponents = false;
|
2014-12-19 03:58:52 +08:00
|
|
|
for (auto VNI : LI.valnos) {
|
2011-02-08 08:03:05 +08:00
|
|
|
if (VNI->isUnused())
|
|
|
|
continue;
|
2015-01-22 06:55:13 +08:00
|
|
|
SlotIndex Def = VNI->def;
|
|
|
|
LiveRange::iterator I = LI.FindSegmentContaining(Def);
|
2014-12-19 03:58:52 +08:00
|
|
|
assert(I != LI.end() && "Missing segment for VNI");
|
2015-01-22 06:55:13 +08:00
|
|
|
|
|
|
|
// Is the register live before? Otherwise we may have to add a read-undef
|
|
|
|
// flag for subregister defs.
|
2015-09-23 06:37:44 +08:00
|
|
|
bool DeadBeforeDef = false;
|
|
|
|
unsigned VReg = LI.reg;
|
|
|
|
if (MRI->shouldTrackSubRegLiveness(VReg)) {
|
2015-01-22 06:55:13 +08:00
|
|
|
if ((I == LI.begin() || std::prev(I)->end < Def) && !VNI->isPHIDef()) {
|
|
|
|
MachineInstr *MI = getInstructionFromIndex(Def);
|
2015-11-11 08:41:58 +08:00
|
|
|
MI->setRegisterDefReadUndef(VReg);
|
2015-09-23 06:37:44 +08:00
|
|
|
DeadBeforeDef = true;
|
2015-01-22 06:55:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I->end != Def.getDeadSlot())
|
2011-02-08 08:03:05 +08:00
|
|
|
continue;
|
2011-03-02 08:33:01 +08:00
|
|
|
if (VNI->isPHIDef()) {
|
2011-02-08 08:03:05 +08:00
|
|
|
// This is a dead PHI. Remove it.
|
2012-08-04 04:59:32 +08:00
|
|
|
VNI->markUnused();
|
2014-12-19 03:58:52 +08:00
|
|
|
LI.removeSegment(I);
|
2015-01-22 06:55:13 +08:00
|
|
|
DEBUG(dbgs() << "Dead PHI at " << Def << " may separate interval\n");
|
2015-09-23 06:37:44 +08:00
|
|
|
MayHaveSplitComponents = true;
|
2014-12-19 03:58:52 +08:00
|
|
|
} else {
|
2011-02-08 08:03:05 +08:00
|
|
|
// This is a dead def. Make sure the instruction knows.
|
2015-01-22 06:55:13 +08:00
|
|
|
MachineInstr *MI = getInstructionFromIndex(Def);
|
2011-02-08 08:03:05 +08:00
|
|
|
assert(MI && "No instruction defining live value");
|
2015-09-23 06:37:44 +08:00
|
|
|
MI->addRegisterDead(VReg, TRI);
|
|
|
|
|
|
|
|
// If we have a dead def that is completely separate from the rest of
|
|
|
|
// the liverange then we rewrite it to use a different VReg to not violate
|
|
|
|
// the rule that the liveness of a virtual register forms a connected
|
|
|
|
// component. This should only happen if subregister liveness is tracked.
|
|
|
|
if (DeadBeforeDef)
|
|
|
|
MayHaveSplitComponents = true;
|
|
|
|
|
2011-03-08 07:29:10 +08:00
|
|
|
if (dead && MI->allDefsAreDead()) {
|
2015-01-22 06:55:13 +08:00
|
|
|
DEBUG(dbgs() << "All defs dead: " << Def << '\t' << *MI);
|
2011-03-08 07:29:10 +08:00
|
|
|
dead->push_back(MI);
|
|
|
|
}
|
2011-02-08 08:03:05 +08:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 06:37:44 +08:00
|
|
|
return MayHaveSplitComponents;
|
2014-12-10 09:12:18 +08:00
|
|
|
}
|
|
|
|
|
2014-12-19 03:58:52 +08:00
|
|
|
void LiveIntervals::shrinkToUses(LiveInterval::SubRange &SR, unsigned Reg)
|
2014-12-10 09:12:18 +08:00
|
|
|
{
|
|
|
|
DEBUG(dbgs() << "Shrink: " << SR << '\n');
|
|
|
|
assert(TargetRegisterInfo::isVirtualRegister(Reg)
|
|
|
|
&& "Can only shrink virtual registers");
|
|
|
|
// Find all the values used, including PHI kills.
|
|
|
|
ShrinkToUsesWorkList WorkList;
|
|
|
|
|
|
|
|
// Visit all instructions reading Reg.
|
|
|
|
SlotIndex LastIdx;
|
|
|
|
for (MachineOperand &MO : MRI->reg_operands(Reg)) {
|
|
|
|
MachineInstr *UseMI = MO.getParent();
|
|
|
|
if (UseMI->isDebugValue())
|
|
|
|
continue;
|
|
|
|
// Maybe the operand is for a subregister we don't care about.
|
|
|
|
unsigned SubReg = MO.getSubReg();
|
|
|
|
if (SubReg != 0) {
|
2015-09-26 05:51:14 +08:00
|
|
|
LaneBitmask LaneMask = TRI->getSubRegIndexLaneMask(SubReg);
|
|
|
|
if ((LaneMask & SR.LaneMask) == 0)
|
2014-12-10 09:12:18 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// We only need to visit each instruction once.
|
|
|
|
SlotIndex Idx = getInstructionIndex(UseMI).getRegSlot();
|
|
|
|
if (Idx == LastIdx)
|
|
|
|
continue;
|
|
|
|
LastIdx = Idx;
|
|
|
|
|
|
|
|
LiveQueryResult LRQ = SR.Query(Idx);
|
|
|
|
VNInfo *VNI = LRQ.valueIn();
|
|
|
|
// For Subranges it is possible that only undef values are left in that
|
|
|
|
// part of the subregister, so there is no real liverange at the use
|
|
|
|
if (!VNI)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Special case: An early-clobber tied operand reads and writes the
|
|
|
|
// register one slot early.
|
|
|
|
if (VNInfo *DefVNI = LRQ.valueDefined())
|
|
|
|
Idx = DefVNI->def;
|
|
|
|
|
|
|
|
WorkList.push_back(std::make_pair(Idx, VNI));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new live ranges with only minimal live segments per def.
|
|
|
|
LiveRange NewLR;
|
|
|
|
createSegmentsForValues(NewLR, make_range(SR.vni_begin(), SR.vni_end()));
|
|
|
|
extendSegmentsToUses(NewLR, *Indexes, WorkList, SR);
|
|
|
|
|
|
|
|
// Move the trimmed ranges back.
|
|
|
|
SR.segments.swap(NewLR.segments);
|
2014-12-19 03:58:52 +08:00
|
|
|
|
|
|
|
// Remove dead PHI value numbers
|
|
|
|
for (auto VNI : SR.valnos) {
|
|
|
|
if (VNI->isUnused())
|
|
|
|
continue;
|
|
|
|
const LiveRange::Segment *Segment = SR.getSegmentContaining(VNI->def);
|
|
|
|
assert(Segment != nullptr && "Missing segment for VNI");
|
|
|
|
if (Segment->end != VNI->def.getDeadSlot())
|
|
|
|
continue;
|
|
|
|
if (VNI->isPHIDef()) {
|
|
|
|
// This is a dead PHI. Remove it.
|
|
|
|
VNI->markUnused();
|
|
|
|
SR.removeSegment(*Segment);
|
|
|
|
DEBUG(dbgs() << "Dead PHI at " << VNI->def << " may separate interval\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 09:12:18 +08:00
|
|
|
DEBUG(dbgs() << "Shrunk: " << SR << '\n');
|
2011-02-08 08:03:05 +08:00
|
|
|
}
|
|
|
|
|
2013-10-11 05:28:57 +08:00
|
|
|
void LiveIntervals::extendToIndices(LiveRange &LR,
|
2012-09-18 07:03:25 +08:00
|
|
|
ArrayRef<SlotIndex> Indices) {
|
|
|
|
assert(LRCalc && "LRCalc not initialized.");
|
|
|
|
LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator());
|
|
|
|
for (unsigned i = 0, e = Indices.size(); i != e; ++i)
|
2013-10-11 05:28:57 +08:00
|
|
|
LRCalc->extend(LR, Indices[i]);
|
2012-09-18 07:03:25 +08:00
|
|
|
}
|
|
|
|
|
2014-12-10 09:12:36 +08:00
|
|
|
void LiveIntervals::pruneValue(LiveRange &LR, SlotIndex Kill,
|
2012-09-18 07:03:25 +08:00
|
|
|
SmallVectorImpl<SlotIndex> *EndPoints) {
|
2014-12-10 09:12:36 +08:00
|
|
|
LiveQueryResult LRQ = LR.Query(Kill);
|
|
|
|
VNInfo *VNI = LRQ.valueOutOrDead();
|
2012-09-18 07:03:25 +08:00
|
|
|
if (!VNI)
|
|
|
|
return;
|
|
|
|
|
|
|
|
MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill);
|
2014-12-10 09:12:36 +08:00
|
|
|
SlotIndex MBBEnd = Indexes->getMBBEndIdx(KillMBB);
|
2012-09-18 07:03:25 +08:00
|
|
|
|
|
|
|
// If VNI isn't live out from KillMBB, the value is trivially pruned.
|
|
|
|
if (LRQ.endPoint() < MBBEnd) {
|
2014-12-10 09:12:36 +08:00
|
|
|
LR.removeSegment(Kill, LRQ.endPoint());
|
2012-09-18 07:03:25 +08:00
|
|
|
if (EndPoints) EndPoints->push_back(LRQ.endPoint());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// VNI is live out of KillMBB.
|
2014-12-10 09:12:36 +08:00
|
|
|
LR.removeSegment(Kill, MBBEnd);
|
2012-09-18 07:03:25 +08:00
|
|
|
if (EndPoints) EndPoints->push_back(MBBEnd);
|
|
|
|
|
2012-10-14 00:15:31 +08:00
|
|
|
// Find all blocks that are reachable from KillMBB without leaving VNI's live
|
|
|
|
// range. It is possible that KillMBB itself is reachable, so start a DFS
|
|
|
|
// from each successor.
|
|
|
|
typedef SmallPtrSet<MachineBasicBlock*, 9> VisitedTy;
|
|
|
|
VisitedTy Visited;
|
|
|
|
for (MachineBasicBlock::succ_iterator
|
|
|
|
SuccI = KillMBB->succ_begin(), SuccE = KillMBB->succ_end();
|
|
|
|
SuccI != SuccE; ++SuccI) {
|
|
|
|
for (df_ext_iterator<MachineBasicBlock*, VisitedTy>
|
|
|
|
I = df_ext_begin(*SuccI, Visited), E = df_ext_end(*SuccI, Visited);
|
|
|
|
I != E;) {
|
|
|
|
MachineBasicBlock *MBB = *I;
|
|
|
|
|
|
|
|
// Check if VNI is live in to MBB.
|
2014-12-10 09:12:36 +08:00
|
|
|
SlotIndex MBBStart, MBBEnd;
|
2014-03-02 21:30:33 +08:00
|
|
|
std::tie(MBBStart, MBBEnd) = Indexes->getMBBRange(MBB);
|
2014-12-10 09:12:36 +08:00
|
|
|
LiveQueryResult LRQ = LR.Query(MBBStart);
|
2012-10-14 00:15:31 +08:00
|
|
|
if (LRQ.valueIn() != VNI) {
|
2013-10-11 05:28:43 +08:00
|
|
|
// This block isn't part of the VNI segment. Prune the search.
|
2012-10-14 00:15:31 +08:00
|
|
|
I.skipChildren();
|
|
|
|
continue;
|
|
|
|
}
|
2012-09-18 07:03:25 +08:00
|
|
|
|
2012-10-14 00:15:31 +08:00
|
|
|
// Prune the search if VNI is killed in MBB.
|
|
|
|
if (LRQ.endPoint() < MBBEnd) {
|
2014-12-10 09:12:36 +08:00
|
|
|
LR.removeSegment(MBBStart, LRQ.endPoint());
|
2012-10-14 00:15:31 +08:00
|
|
|
if (EndPoints) EndPoints->push_back(LRQ.endPoint());
|
|
|
|
I.skipChildren();
|
|
|
|
continue;
|
|
|
|
}
|
2012-09-18 07:03:25 +08:00
|
|
|
|
2012-10-14 00:15:31 +08:00
|
|
|
// VNI is live through MBB.
|
2014-12-10 09:12:36 +08:00
|
|
|
LR.removeSegment(MBBStart, MBBEnd);
|
2012-10-14 00:15:31 +08:00
|
|
|
if (EndPoints) EndPoints->push_back(MBBEnd);
|
|
|
|
++I;
|
2012-09-18 07:03:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-08 08:03:05 +08:00
|
|
|
|
2007-11-12 14:35:08 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Register allocator hooks.
|
|
|
|
//
|
|
|
|
|
2012-09-07 02:15:18 +08:00
|
|
|
void LiveIntervals::addKillFlags(const VirtRegMap *VRM) {
|
|
|
|
// Keep track of regunit ranges.
|
2014-12-20 09:54:48 +08:00
|
|
|
SmallVector<std::pair<const LiveRange*, LiveRange::const_iterator>, 8> RU;
|
2014-12-20 09:54:50 +08:00
|
|
|
// Keep track of subregister ranges.
|
|
|
|
SmallVector<std::pair<const LiveInterval::SubRange*,
|
|
|
|
LiveRange::const_iterator>, 4> SRs;
|
2012-09-07 02:15:18 +08:00
|
|
|
|
2012-06-21 07:23:59 +08:00
|
|
|
for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) {
|
|
|
|
unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
|
2012-06-05 06:39:14 +08:00
|
|
|
if (MRI->reg_nodbg_empty(Reg))
|
2011-02-09 05:13:03 +08:00
|
|
|
continue;
|
2014-12-20 09:54:48 +08:00
|
|
|
const LiveInterval &LI = getInterval(Reg);
|
|
|
|
if (LI.empty())
|
2012-09-07 02:15:18 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Find the regunit intervals for the assigned register. They may overlap
|
|
|
|
// the virtual register live range, cancelling any kills.
|
|
|
|
RU.clear();
|
|
|
|
for (MCRegUnitIterator Units(VRM->getPhys(Reg), TRI); Units.isValid();
|
|
|
|
++Units) {
|
2014-12-20 09:54:48 +08:00
|
|
|
const LiveRange &RURange = getRegUnit(*Units);
|
|
|
|
if (RURange.empty())
|
2012-09-07 02:15:18 +08:00
|
|
|
continue;
|
2014-12-20 09:54:48 +08:00
|
|
|
RU.push_back(std::make_pair(&RURange, RURange.find(LI.begin()->end)));
|
2012-09-07 02:15:18 +08:00
|
|
|
}
|
2011-02-09 05:13:03 +08:00
|
|
|
|
2015-03-19 08:21:58 +08:00
|
|
|
if (MRI->subRegLivenessEnabled()) {
|
2014-12-20 09:54:50 +08:00
|
|
|
SRs.clear();
|
|
|
|
for (const LiveInterval::SubRange &SR : LI.subranges()) {
|
|
|
|
SRs.push_back(std::make_pair(&SR, SR.find(LI.begin()->end)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-11 05:28:43 +08:00
|
|
|
// Every instruction that kills Reg corresponds to a segment range end
|
|
|
|
// point.
|
2014-12-20 09:54:48 +08:00
|
|
|
for (LiveInterval::const_iterator RI = LI.begin(), RE = LI.end(); RI != RE;
|
2011-02-09 05:13:03 +08:00
|
|
|
++RI) {
|
2011-11-14 04:45:27 +08:00
|
|
|
// A block index indicates an MBB edge.
|
|
|
|
if (RI->end.isBlock())
|
2011-02-09 05:13:03 +08:00
|
|
|
continue;
|
|
|
|
MachineInstr *MI = getInstructionFromIndex(RI->end);
|
|
|
|
if (!MI)
|
|
|
|
continue;
|
2012-09-07 02:15:18 +08:00
|
|
|
|
2013-10-05 00:52:58 +08:00
|
|
|
// Check if any of the regunits are live beyond the end of RI. That could
|
2012-09-07 02:15:18 +08:00
|
|
|
// happen when a physreg is defined as a copy of a virtreg:
|
|
|
|
//
|
|
|
|
// %EAX = COPY %vreg5
|
|
|
|
// FOO %vreg5 <--- MI, cancel kill because %EAX is live.
|
|
|
|
// BAR %EAX<kill>
|
|
|
|
//
|
|
|
|
// There should be no kill flag on FOO when %vreg5 is rewritten as %EAX.
|
2014-12-20 09:54:48 +08:00
|
|
|
for (auto &RUP : RU) {
|
|
|
|
const LiveRange &RURange = *RUP.first;
|
2014-12-24 10:11:43 +08:00
|
|
|
LiveRange::const_iterator &I = RUP.second;
|
2014-12-20 09:54:48 +08:00
|
|
|
if (I == RURange.end())
|
2012-09-07 02:15:18 +08:00
|
|
|
continue;
|
2014-12-20 09:54:48 +08:00
|
|
|
I = RURange.advanceTo(I, RI->end);
|
|
|
|
if (I == RURange.end() || I->start >= RI->end)
|
2012-09-07 02:15:18 +08:00
|
|
|
continue;
|
|
|
|
// I is overlapping RI.
|
2014-12-20 09:54:50 +08:00
|
|
|
goto CancelKill;
|
2012-09-07 02:15:18 +08:00
|
|
|
}
|
2014-12-10 09:13:04 +08:00
|
|
|
|
2015-03-19 08:21:58 +08:00
|
|
|
if (MRI->subRegLivenessEnabled()) {
|
2014-12-20 09:54:50 +08:00
|
|
|
// When reading a partial undefined value we must not add a kill flag.
|
|
|
|
// The regalloc might have used the undef lane for something else.
|
|
|
|
// Example:
|
|
|
|
// %vreg1 = ... ; R32: %vreg1
|
|
|
|
// %vreg2:high16 = ... ; R64: %vreg2
|
|
|
|
// = read %vreg2<kill> ; R64: %vreg2
|
|
|
|
// = read %vreg1 ; R32: %vreg1
|
|
|
|
// The <kill> flag is correct for %vreg2, but the register allocator may
|
|
|
|
// assign R0L to %vreg1, and R0 to %vreg2 because the low 32bits of R0
|
|
|
|
// are actually never written by %vreg2. After assignment the <kill>
|
|
|
|
// flag at the read instruction is invalid.
|
2015-09-26 05:51:14 +08:00
|
|
|
LaneBitmask DefinedLanesMask;
|
2014-12-20 09:54:50 +08:00
|
|
|
if (!SRs.empty()) {
|
|
|
|
// Compute a mask of lanes that are defined.
|
|
|
|
DefinedLanesMask = 0;
|
|
|
|
for (auto &SRP : SRs) {
|
|
|
|
const LiveInterval::SubRange &SR = *SRP.first;
|
2014-12-24 10:11:43 +08:00
|
|
|
LiveRange::const_iterator &I = SRP.second;
|
2014-12-20 09:54:50 +08:00
|
|
|
if (I == SR.end())
|
|
|
|
continue;
|
|
|
|
I = SR.advanceTo(I, RI->end);
|
|
|
|
if (I == SR.end() || I->start >= RI->end)
|
|
|
|
continue;
|
|
|
|
// I is overlapping RI
|
|
|
|
DefinedLanesMask |= SR.LaneMask;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
DefinedLanesMask = ~0u;
|
|
|
|
|
|
|
|
bool IsFullWrite = false;
|
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
|
|
|
if (!MO.isReg() || MO.getReg() != Reg)
|
|
|
|
continue;
|
|
|
|
if (MO.isUse()) {
|
|
|
|
// Reading any undefined lanes?
|
2015-09-26 05:51:14 +08:00
|
|
|
LaneBitmask UseMask = TRI->getSubRegIndexLaneMask(MO.getSubReg());
|
2014-12-20 09:54:50 +08:00
|
|
|
if ((UseMask & ~DefinedLanesMask) != 0)
|
|
|
|
goto CancelKill;
|
|
|
|
} else if (MO.getSubReg() == 0) {
|
|
|
|
// Writing to the full register?
|
|
|
|
assert(MO.isDef());
|
|
|
|
IsFullWrite = true;
|
2014-12-10 09:13:04 +08:00
|
|
|
}
|
2014-12-20 09:54:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If an instruction writes to a subregister, a new segment starts in
|
|
|
|
// the LiveInterval. But as this is only overriding part of the register
|
|
|
|
// adding kill-flags is not correct here after registers have been
|
|
|
|
// assigned.
|
|
|
|
if (!IsFullWrite) {
|
|
|
|
// Next segment has to be adjacent in the subregister write case.
|
|
|
|
LiveRange::const_iterator N = std::next(RI);
|
|
|
|
if (N != LI.end() && N->start == RI->end)
|
|
|
|
goto CancelKill;
|
2014-12-10 09:13:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-20 09:54:50 +08:00
|
|
|
MI->addRegisterKilled(Reg, nullptr);
|
|
|
|
continue;
|
|
|
|
CancelKill:
|
|
|
|
MI->clearRegisterKills(Reg, nullptr);
|
2011-02-09 05:13:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-10 09:23:55 +08:00
|
|
|
MachineBasicBlock*
|
|
|
|
LiveIntervals::intervalIsInOneMBB(const LiveInterval &LI) const {
|
|
|
|
// A local live range must be fully contained inside the block, meaning it is
|
|
|
|
// defined and killed at instructions, not at block boundaries. It is not
|
|
|
|
// live in or or out of any block.
|
|
|
|
//
|
|
|
|
// It is technically possible to have a PHI-defined live range identical to a
|
|
|
|
// single block, but we are going to return false in that case.
|
|
|
|
|
|
|
|
SlotIndex Start = LI.beginIndex();
|
|
|
|
if (Start.isBlock())
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2012-02-10 09:23:55 +08:00
|
|
|
|
|
|
|
SlotIndex Stop = LI.endIndex();
|
|
|
|
if (Stop.isBlock())
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2012-02-10 09:23:55 +08:00
|
|
|
|
|
|
|
// getMBBFromIndex doesn't need to search the MBB table when both indexes
|
|
|
|
// belong to proper instructions.
|
2012-06-05 06:39:14 +08:00
|
|
|
MachineBasicBlock *MBB1 = Indexes->getMBBFromIndex(Start);
|
|
|
|
MachineBasicBlock *MBB2 = Indexes->getMBBFromIndex(Stop);
|
2014-04-14 08:51:57 +08:00
|
|
|
return MBB1 == MBB2 ? MBB1 : nullptr;
|
Live interval splitting:
When a live interval is being spilled, rather than creating short, non-spillable
intervals for every def / use, split the interval at BB boundaries. That is, for
every BB where the live interval is defined or used, create a new interval that
covers all the defs and uses in the BB.
This is designed to eliminate one common problem: multiple reloads of the same
value in a single basic block. Note, it does *not* decrease the number of spills
since no copies are inserted so the split intervals are *connected* through
spill and reloads (or rematerialization). The newly created intervals can be
spilled again, in that case, since it does not span multiple basic blocks, it's
spilled in the usual manner. However, it can reuse the same stack slot as the
previously split interval.
This is currently controlled by -split-intervals-at-bb.
llvm-svn: 44198
2007-11-17 08:40:40 +08:00
|
|
|
}
|
|
|
|
|
2012-08-04 04:10:24 +08:00
|
|
|
bool
|
|
|
|
LiveIntervals::hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const {
|
2014-12-11 07:07:54 +08:00
|
|
|
for (const VNInfo *PHI : LI.valnos) {
|
2012-08-04 04:10:24 +08:00
|
|
|
if (PHI->isUnused() || !PHI->isPHIDef())
|
|
|
|
continue;
|
|
|
|
const MachineBasicBlock *PHIMBB = getMBBFromIndex(PHI->def);
|
|
|
|
// Conservatively return true instead of scanning huge predecessor lists.
|
|
|
|
if (PHIMBB->pred_size() > 100)
|
|
|
|
return true;
|
|
|
|
for (MachineBasicBlock::const_pred_iterator
|
|
|
|
PI = PHIMBB->pred_begin(), PE = PHIMBB->pred_end(); PI != PE; ++PI)
|
|
|
|
if (VNI == LI.getVNInfoBefore(Indexes->getMBBEndIdx(*PI)))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-02 04:59:38 +08:00
|
|
|
float
|
2013-12-14 08:53:32 +08:00
|
|
|
LiveIntervals::getSpillWeight(bool isDef, bool isUse,
|
|
|
|
const MachineBlockFrequencyInfo *MBFI,
|
|
|
|
const MachineInstr *MI) {
|
|
|
|
BlockFrequency Freq = MBFI->getBlockFreq(MI->getParent());
|
2013-12-14 10:37:38 +08:00
|
|
|
const float Scale = 1.0f / MBFI->getEntryFreq();
|
2013-12-14 08:53:32 +08:00
|
|
|
return (isDef + isUse) * (Freq.getFrequency() * Scale);
|
2010-03-02 04:59:38 +08:00
|
|
|
}
|
|
|
|
|
2013-10-11 05:28:47 +08:00
|
|
|
LiveRange::Segment
|
2013-10-11 05:28:43 +08:00
|
|
|
LiveIntervals::addSegmentToEndOfBlock(unsigned reg, MachineInstr* startInst) {
|
2013-08-15 07:50:16 +08:00
|
|
|
LiveInterval& Interval = createEmptyInterval(reg);
|
2008-06-06 01:15:43 +08:00
|
|
|
VNInfo* VN = Interval.getNextValue(
|
2011-11-14 04:45:27 +08:00
|
|
|
SlotIndex(getInstructionIndex(startInst).getRegSlot()),
|
2012-02-04 13:20:49 +08:00
|
|
|
getVNInfoAllocator());
|
2013-10-11 05:28:47 +08:00
|
|
|
LiveRange::Segment S(
|
2011-11-14 04:45:27 +08:00
|
|
|
SlotIndex(getInstructionIndex(startInst).getRegSlot()),
|
2009-12-22 08:11:50 +08:00
|
|
|
getMBBEndIdx(startInst->getParent()), VN);
|
2013-10-11 05:28:43 +08:00
|
|
|
Interval.addSegment(S);
|
2010-08-13 04:01:23 +08:00
|
|
|
|
2013-10-11 05:28:43 +08:00
|
|
|
return S;
|
2008-06-06 01:15:43 +08:00
|
|
|
}
|
2012-02-09 01:33:45 +08:00
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Register mask functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
bool LiveIntervals::checkRegMaskInterference(LiveInterval &LI,
|
|
|
|
BitVector &UsableRegs) {
|
|
|
|
if (LI.empty())
|
|
|
|
return false;
|
2012-02-10 09:31:31 +08:00
|
|
|
LiveInterval::iterator LiveI = LI.begin(), LiveE = LI.end();
|
|
|
|
|
|
|
|
// Use a smaller arrays for local live ranges.
|
|
|
|
ArrayRef<SlotIndex> Slots;
|
|
|
|
ArrayRef<const uint32_t*> Bits;
|
|
|
|
if (MachineBasicBlock *MBB = intervalIsInOneMBB(LI)) {
|
|
|
|
Slots = getRegMaskSlotsInBlock(MBB->getNumber());
|
|
|
|
Bits = getRegMaskBitsInBlock(MBB->getNumber());
|
|
|
|
} else {
|
|
|
|
Slots = getRegMaskSlots();
|
|
|
|
Bits = getRegMaskBits();
|
|
|
|
}
|
2012-02-09 01:33:45 +08:00
|
|
|
|
|
|
|
// We are going to enumerate all the register mask slots contained in LI.
|
|
|
|
// Start with a binary search of RegMaskSlots to find a starting point.
|
|
|
|
ArrayRef<SlotIndex>::iterator SlotI =
|
|
|
|
std::lower_bound(Slots.begin(), Slots.end(), LiveI->start);
|
|
|
|
ArrayRef<SlotIndex>::iterator SlotE = Slots.end();
|
|
|
|
|
|
|
|
// No slots in range, LI begins after the last call.
|
|
|
|
if (SlotI == SlotE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool Found = false;
|
|
|
|
for (;;) {
|
|
|
|
assert(*SlotI >= LiveI->start);
|
|
|
|
// Loop over all slots overlapping this segment.
|
|
|
|
while (*SlotI < LiveI->end) {
|
|
|
|
// *SlotI overlaps LI. Collect mask bits.
|
|
|
|
if (!Found) {
|
|
|
|
// This is the first overlap. Initialize UsableRegs to all ones.
|
|
|
|
UsableRegs.clear();
|
2012-06-05 06:39:14 +08:00
|
|
|
UsableRegs.resize(TRI->getNumRegs(), true);
|
2012-02-09 01:33:45 +08:00
|
|
|
Found = true;
|
|
|
|
}
|
|
|
|
// Remove usable registers clobbered by this mask.
|
2012-02-10 09:31:31 +08:00
|
|
|
UsableRegs.clearBitsNotInMask(Bits[SlotI-Slots.begin()]);
|
2012-02-09 01:33:45 +08:00
|
|
|
if (++SlotI == SlotE)
|
|
|
|
return Found;
|
|
|
|
}
|
|
|
|
// *SlotI is beyond the current LI segment.
|
|
|
|
LiveI = LI.advanceTo(LiveI, *SlotI);
|
|
|
|
if (LiveI == LiveE)
|
|
|
|
return Found;
|
|
|
|
// Advance SlotI until it overlaps.
|
|
|
|
while (*SlotI < LiveI->start)
|
|
|
|
if (++SlotI == SlotE)
|
|
|
|
return Found;
|
|
|
|
}
|
|
|
|
}
|
2012-02-18 02:44:18 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// IntervalUpdate class.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-02-21 08:00:36 +08:00
|
|
|
// HMEditor is a toolkit used by handleMove to trim or extend live intervals.
|
2012-02-18 02:44:18 +08:00
|
|
|
class LiveIntervals::HMEditor {
|
|
|
|
private:
|
2012-02-18 07:43:40 +08:00
|
|
|
LiveIntervals& LIS;
|
|
|
|
const MachineRegisterInfo& MRI;
|
|
|
|
const TargetRegisterInfo& TRI;
|
2012-10-13 05:31:57 +08:00
|
|
|
SlotIndex OldIdx;
|
2012-02-18 07:43:40 +08:00
|
|
|
SlotIndex NewIdx;
|
2013-10-11 05:29:02 +08:00
|
|
|
SmallPtrSet<LiveRange*, 8> Updated;
|
2012-10-16 08:22:51 +08:00
|
|
|
bool UpdateFlags;
|
2012-02-19 15:13:05 +08:00
|
|
|
|
2012-02-18 02:44:18 +08:00
|
|
|
public:
|
2012-02-18 07:43:40 +08:00
|
|
|
HMEditor(LiveIntervals& LIS, const MachineRegisterInfo& MRI,
|
2012-10-13 05:31:57 +08:00
|
|
|
const TargetRegisterInfo& TRI,
|
2012-10-16 08:22:51 +08:00
|
|
|
SlotIndex OldIdx, SlotIndex NewIdx, bool UpdateFlags)
|
|
|
|
: LIS(LIS), MRI(MRI), TRI(TRI), OldIdx(OldIdx), NewIdx(NewIdx),
|
|
|
|
UpdateFlags(UpdateFlags) {}
|
|
|
|
|
|
|
|
// FIXME: UpdateFlags is a workaround that creates live intervals for all
|
|
|
|
// physregs, even those that aren't needed for regalloc, in order to update
|
|
|
|
// kill flags. This is wasteful. Eventually, LiveVariables will strip all kill
|
|
|
|
// flags, and postRA passes will use a live register utility instead.
|
2013-10-11 05:29:02 +08:00
|
|
|
LiveRange *getRegUnitLI(unsigned Unit) {
|
2012-10-16 08:22:51 +08:00
|
|
|
if (UpdateFlags)
|
|
|
|
return &LIS.getRegUnit(Unit);
|
|
|
|
return LIS.getCachedRegUnit(Unit);
|
|
|
|
}
|
2012-10-13 05:31:57 +08:00
|
|
|
|
|
|
|
/// Update all live ranges touched by MI, assuming a move from OldIdx to
|
|
|
|
/// NewIdx.
|
|
|
|
void updateAllRanges(MachineInstr *MI) {
|
|
|
|
DEBUG(dbgs() << "handleMove " << OldIdx << " -> " << NewIdx << ": " << *MI);
|
|
|
|
bool hasRegMask = false;
|
2015-05-29 10:56:46 +08:00
|
|
|
for (MachineOperand &MO : MI->operands()) {
|
|
|
|
if (MO.isRegMask())
|
2012-10-13 05:31:57 +08:00
|
|
|
hasRegMask = true;
|
2015-05-29 10:56:46 +08:00
|
|
|
if (!MO.isReg())
|
2012-02-22 06:29:38 +08:00
|
|
|
continue;
|
2012-10-13 05:31:57 +08:00
|
|
|
// Aggressively clear all kill flags.
|
|
|
|
// They are reinserted by VirtRegRewriter.
|
2015-05-29 10:56:46 +08:00
|
|
|
if (MO.isUse())
|
|
|
|
MO.setIsKill(false);
|
2012-02-19 15:13:05 +08:00
|
|
|
|
2015-05-29 10:56:46 +08:00
|
|
|
unsigned Reg = MO.getReg();
|
2012-10-13 05:31:57 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg)) {
|
2013-10-11 05:29:02 +08:00
|
|
|
LiveInterval &LI = LIS.getInterval(Reg);
|
2014-12-10 09:12:20 +08:00
|
|
|
if (LI.hasSubRanges()) {
|
2015-05-29 10:56:46 +08:00
|
|
|
unsigned SubReg = MO.getSubReg();
|
2015-09-26 05:51:14 +08:00
|
|
|
LaneBitmask LaneMask = TRI.getSubRegIndexLaneMask(SubReg);
|
2014-12-11 08:59:06 +08:00
|
|
|
for (LiveInterval::SubRange &S : LI.subranges()) {
|
|
|
|
if ((S.LaneMask & LaneMask) == 0)
|
2014-12-10 09:12:20 +08:00
|
|
|
continue;
|
2014-12-11 08:59:06 +08:00
|
|
|
updateRange(S, Reg, S.LaneMask);
|
2014-12-10 09:12:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
updateRange(LI, Reg, 0);
|
2012-10-13 05:31:57 +08:00
|
|
|
continue;
|
|
|
|
}
|
2012-02-22 06:29:38 +08:00
|
|
|
|
2012-10-13 05:31:57 +08:00
|
|
|
// For physregs, only update the regunits that actually have a
|
|
|
|
// precomputed live range.
|
|
|
|
for (MCRegUnitIterator Units(Reg, &TRI); Units.isValid(); ++Units)
|
2013-10-11 05:29:02 +08:00
|
|
|
if (LiveRange *LR = getRegUnitLI(*Units))
|
2014-12-10 09:12:20 +08:00
|
|
|
updateRange(*LR, *Units, 0);
|
2012-10-13 05:31:57 +08:00
|
|
|
}
|
|
|
|
if (hasRegMask)
|
|
|
|
updateRegMaskSlots();
|
2012-02-19 15:13:05 +08:00
|
|
|
}
|
|
|
|
|
2012-02-19 11:00:30 +08:00
|
|
|
private:
|
2012-10-13 05:31:57 +08:00
|
|
|
/// Update a single live range, assuming an instruction has been moved from
|
|
|
|
/// OldIdx to NewIdx.
|
2015-09-26 05:51:14 +08:00
|
|
|
void updateRange(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask) {
|
2014-11-19 15:49:26 +08:00
|
|
|
if (!Updated.insert(&LR).second)
|
2012-10-13 05:31:57 +08:00
|
|
|
return;
|
|
|
|
DEBUG({
|
|
|
|
dbgs() << " ";
|
2014-12-10 09:12:20 +08:00
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg)) {
|
2013-10-11 05:29:02 +08:00
|
|
|
dbgs() << PrintReg(Reg);
|
2014-12-10 09:12:20 +08:00
|
|
|
if (LaneMask != 0)
|
2015-09-26 05:51:24 +08:00
|
|
|
dbgs() << " L" << PrintLaneMask(LaneMask);
|
2014-12-10 09:12:20 +08:00
|
|
|
} else {
|
2013-10-11 05:29:02 +08:00
|
|
|
dbgs() << PrintRegUnit(Reg, &TRI);
|
2014-12-10 09:12:20 +08:00
|
|
|
}
|
2013-10-11 05:29:02 +08:00
|
|
|
dbgs() << ":\t" << LR << '\n';
|
2012-10-13 05:31:57 +08:00
|
|
|
});
|
|
|
|
if (SlotIndex::isEarlierInstr(OldIdx, NewIdx))
|
2013-10-11 05:29:02 +08:00
|
|
|
handleMoveDown(LR);
|
2012-10-13 05:31:57 +08:00
|
|
|
else
|
2014-12-10 09:12:20 +08:00
|
|
|
handleMoveUp(LR, Reg, LaneMask);
|
2013-10-11 05:29:02 +08:00
|
|
|
DEBUG(dbgs() << " -->\t" << LR << '\n');
|
|
|
|
LR.verify();
|
2012-10-13 05:31:57 +08:00
|
|
|
}
|
|
|
|
|
2013-10-11 05:29:02 +08:00
|
|
|
/// Update LR to reflect an instruction has been moved downwards from OldIdx
|
2012-10-13 05:31:57 +08:00
|
|
|
/// to NewIdx.
|
|
|
|
///
|
|
|
|
/// 1. Live def at OldIdx:
|
|
|
|
/// Move def to NewIdx, assert endpoint after NewIdx.
|
|
|
|
///
|
|
|
|
/// 2. Live def at OldIdx, killed at NewIdx:
|
|
|
|
/// Change to dead def at NewIdx.
|
|
|
|
/// (Happens when bundling def+kill together).
|
|
|
|
///
|
|
|
|
/// 3. Dead def at OldIdx:
|
|
|
|
/// Move def to NewIdx, possibly across another live value.
|
|
|
|
///
|
|
|
|
/// 4. Def at OldIdx AND at NewIdx:
|
2013-10-11 05:28:43 +08:00
|
|
|
/// Remove segment [OldIdx;NewIdx) and value defined at OldIdx.
|
2012-10-13 05:31:57 +08:00
|
|
|
/// (Happens when bundling multiple defs together).
|
|
|
|
///
|
|
|
|
/// 5. Value read at OldIdx, killed before NewIdx:
|
|
|
|
/// Extend kill to NewIdx.
|
|
|
|
///
|
2013-10-11 05:29:02 +08:00
|
|
|
void handleMoveDown(LiveRange &LR) {
|
2012-10-13 05:31:57 +08:00
|
|
|
// First look for a kill at OldIdx.
|
2013-10-11 05:29:02 +08:00
|
|
|
LiveRange::iterator I = LR.find(OldIdx.getBaseIndex());
|
|
|
|
LiveRange::iterator E = LR.end();
|
|
|
|
// Is LR even live at OldIdx?
|
2012-10-13 05:31:57 +08:00
|
|
|
if (I == E || SlotIndex::isEarlierInstr(OldIdx, I->start))
|
|
|
|
return;
|
2012-02-19 11:00:30 +08:00
|
|
|
|
2012-10-13 05:31:57 +08:00
|
|
|
// Handle a live-in value.
|
|
|
|
if (!SlotIndex::isSameInstr(I->start, OldIdx)) {
|
|
|
|
bool isKill = SlotIndex::isSameInstr(OldIdx, I->end);
|
|
|
|
// If the live-in value already extends to NewIdx, there is nothing to do.
|
|
|
|
if (!SlotIndex::isEarlierInstr(I->end, NewIdx))
|
2012-02-19 11:00:30 +08:00
|
|
|
return;
|
2012-10-13 05:31:57 +08:00
|
|
|
// Aggressively remove all kill flags from the old kill point.
|
|
|
|
// Kill flags shouldn't be used while live intervals exist, they will be
|
|
|
|
// reinserted by VirtRegRewriter.
|
|
|
|
if (MachineInstr *KillMI = LIS.getInstructionFromIndex(I->end))
|
|
|
|
for (MIBundleOperands MO(KillMI); MO.isValid(); ++MO)
|
|
|
|
if (MO->isReg() && MO->isUse())
|
|
|
|
MO->setIsKill(false);
|
2013-10-11 05:29:02 +08:00
|
|
|
// Adjust I->end to reach NewIdx. This may temporarily make LR invalid by
|
2012-10-13 05:31:57 +08:00
|
|
|
// overlapping ranges. Case 5 above.
|
|
|
|
I->end = NewIdx.getRegSlot(I->end.isEarlyClobber());
|
|
|
|
// If this was a kill, there may also be a def. Otherwise we're done.
|
|
|
|
if (!isKill)
|
2012-02-19 11:00:30 +08:00
|
|
|
return;
|
2012-10-13 05:31:57 +08:00
|
|
|
++I;
|
2012-06-20 07:50:18 +08:00
|
|
|
}
|
2012-02-19 11:00:30 +08:00
|
|
|
|
2012-10-13 05:31:57 +08:00
|
|
|
// Check for a def at OldIdx.
|
|
|
|
if (I == E || !SlotIndex::isSameInstr(OldIdx, I->start))
|
|
|
|
return;
|
|
|
|
// We have a def at OldIdx.
|
|
|
|
VNInfo *DefVNI = I->valno;
|
|
|
|
assert(DefVNI->def == I->start && "Inconsistent def");
|
|
|
|
DefVNI->def = NewIdx.getRegSlot(I->start.isEarlyClobber());
|
|
|
|
// If the defined value extends beyond NewIdx, just move the def down.
|
|
|
|
// This is case 1 above.
|
|
|
|
if (SlotIndex::isEarlierInstr(NewIdx, I->end)) {
|
|
|
|
I->start = DefVNI->def;
|
|
|
|
return;
|
2012-06-20 07:50:18 +08:00
|
|
|
}
|
2012-10-13 05:31:57 +08:00
|
|
|
// The remaining possibilities are now:
|
|
|
|
// 2. Live def at OldIdx, killed at NewIdx: isSameInstr(I->end, NewIdx).
|
|
|
|
// 3. Dead def at OldIdx: I->end = OldIdx.getDeadSlot().
|
|
|
|
// In either case, it is possible that there is an existing def at NewIdx.
|
|
|
|
assert((I->end == OldIdx.getDeadSlot() ||
|
|
|
|
SlotIndex::isSameInstr(I->end, NewIdx)) &&
|
|
|
|
"Cannot move def below kill");
|
2013-10-11 05:29:02 +08:00
|
|
|
LiveRange::iterator NewI = LR.advanceTo(I, NewIdx.getRegSlot());
|
2012-10-13 05:31:57 +08:00
|
|
|
if (NewI != E && SlotIndex::isSameInstr(NewI->start, NewIdx)) {
|
|
|
|
// There is an existing def at NewIdx, case 4 above. The def at OldIdx is
|
|
|
|
// coalesced into that value.
|
|
|
|
assert(NewI->valno != DefVNI && "Multiple defs of value?");
|
2013-10-11 05:29:02 +08:00
|
|
|
LR.removeValNo(DefVNI);
|
2012-10-13 05:31:57 +08:00
|
|
|
return;
|
2012-02-18 02:44:18 +08:00
|
|
|
}
|
2012-10-13 05:31:57 +08:00
|
|
|
// There was no existing def at NewIdx. Turn *I into a dead def at NewIdx.
|
2013-10-11 05:29:02 +08:00
|
|
|
// If the def at OldIdx was dead, we allow it to be moved across other LR
|
2012-10-13 05:31:57 +08:00
|
|
|
// values. The new range should be placed immediately before NewI, move any
|
|
|
|
// intermediate ranges up.
|
|
|
|
assert(NewI != I && "Inconsistent iterators");
|
2014-03-02 20:27:27 +08:00
|
|
|
std::copy(std::next(I), NewI, I);
|
|
|
|
*std::prev(NewI)
|
2013-10-11 05:28:47 +08:00
|
|
|
= LiveRange::Segment(DefVNI->def, NewIdx.getDeadSlot(), DefVNI);
|
2012-02-18 02:44:18 +08:00
|
|
|
}
|
|
|
|
|
2013-10-11 05:29:02 +08:00
|
|
|
/// Update LR to reflect an instruction has been moved upwards from OldIdx
|
2012-10-13 05:31:57 +08:00
|
|
|
/// to NewIdx.
|
|
|
|
///
|
|
|
|
/// 1. Live def at OldIdx:
|
|
|
|
/// Hoist def to NewIdx.
|
|
|
|
///
|
|
|
|
/// 2. Dead def at OldIdx:
|
|
|
|
/// Hoist def+end to NewIdx, possibly move across other values.
|
|
|
|
///
|
|
|
|
/// 3. Dead def at OldIdx AND existing def at NewIdx:
|
|
|
|
/// Remove value defined at OldIdx, coalescing it with existing value.
|
|
|
|
///
|
|
|
|
/// 4. Live def at OldIdx AND existing def at NewIdx:
|
|
|
|
/// Remove value defined at NewIdx, hoist OldIdx def to NewIdx.
|
|
|
|
/// (Happens when bundling multiple defs together).
|
|
|
|
///
|
|
|
|
/// 5. Value killed at OldIdx:
|
|
|
|
/// Hoist kill to NewIdx, then scan for last kill between NewIdx and
|
|
|
|
/// OldIdx.
|
|
|
|
///
|
2015-09-26 05:51:14 +08:00
|
|
|
void handleMoveUp(LiveRange &LR, unsigned Reg, LaneBitmask LaneMask) {
|
2012-10-13 05:31:57 +08:00
|
|
|
// First look for a kill at OldIdx.
|
2013-10-11 05:29:02 +08:00
|
|
|
LiveRange::iterator I = LR.find(OldIdx.getBaseIndex());
|
|
|
|
LiveRange::iterator E = LR.end();
|
|
|
|
// Is LR even live at OldIdx?
|
2012-10-13 05:31:57 +08:00
|
|
|
if (I == E || SlotIndex::isEarlierInstr(OldIdx, I->start))
|
|
|
|
return;
|
2012-02-19 15:13:05 +08:00
|
|
|
|
2012-10-13 05:31:57 +08:00
|
|
|
// Handle a live-in value.
|
|
|
|
if (!SlotIndex::isSameInstr(I->start, OldIdx)) {
|
|
|
|
// If the live-in value isn't killed here, there is nothing to do.
|
|
|
|
if (!SlotIndex::isSameInstr(OldIdx, I->end))
|
|
|
|
return;
|
|
|
|
// Adjust I->end to end at NewIdx. If we are hoisting a kill above
|
|
|
|
// another use, we need to search for that use. Case 5 above.
|
|
|
|
I->end = NewIdx.getRegSlot(I->end.isEarlyClobber());
|
|
|
|
++I;
|
|
|
|
// If OldIdx also defines a value, there couldn't have been another use.
|
|
|
|
if (I == E || !SlotIndex::isSameInstr(I->start, OldIdx)) {
|
|
|
|
// No def, search for the new kill.
|
|
|
|
// This can never be an early clobber kill since there is no def.
|
2014-12-10 09:12:20 +08:00
|
|
|
std::prev(I)->end = findLastUseBefore(Reg, LaneMask).getRegSlot();
|
2012-10-13 05:31:57 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-02-19 15:13:05 +08:00
|
|
|
}
|
|
|
|
|
2012-10-13 05:31:57 +08:00
|
|
|
// Now deal with the def at OldIdx.
|
|
|
|
assert(I != E && SlotIndex::isSameInstr(I->start, OldIdx) && "No def?");
|
|
|
|
VNInfo *DefVNI = I->valno;
|
|
|
|
assert(DefVNI->def == I->start && "Inconsistent def");
|
|
|
|
DefVNI->def = NewIdx.getRegSlot(I->start.isEarlyClobber());
|
|
|
|
|
|
|
|
// Check for an existing def at NewIdx.
|
2013-10-11 05:29:02 +08:00
|
|
|
LiveRange::iterator NewI = LR.find(NewIdx.getRegSlot());
|
2012-10-13 05:31:57 +08:00
|
|
|
if (SlotIndex::isSameInstr(NewI->start, NewIdx)) {
|
|
|
|
assert(NewI->valno != DefVNI && "Same value defined more than once?");
|
|
|
|
// There is an existing def at NewIdx.
|
|
|
|
if (I->end.isDead()) {
|
|
|
|
// Case 3: Remove the dead def at OldIdx.
|
2013-10-11 05:29:02 +08:00
|
|
|
LR.removeValNo(DefVNI);
|
2012-10-13 05:31:57 +08:00
|
|
|
return;
|
2012-02-19 15:13:05 +08:00
|
|
|
}
|
2012-10-13 05:31:57 +08:00
|
|
|
// Case 4: Replace def at NewIdx with live def at OldIdx.
|
|
|
|
I->start = DefVNI->def;
|
2013-10-11 05:29:02 +08:00
|
|
|
LR.removeValNo(NewI->valno);
|
2012-10-13 05:31:57 +08:00
|
|
|
return;
|
2012-02-19 15:13:05 +08:00
|
|
|
}
|
|
|
|
|
2012-10-13 05:31:57 +08:00
|
|
|
// There is no existing def at NewIdx. Hoist DefVNI.
|
|
|
|
if (!I->end.isDead()) {
|
|
|
|
// Leave the end point of a live def.
|
|
|
|
I->start = DefVNI->def;
|
|
|
|
return;
|
2012-02-19 15:13:05 +08:00
|
|
|
}
|
|
|
|
|
2013-10-11 05:29:02 +08:00
|
|
|
// DefVNI is a dead def. It may have been moved across other values in LR,
|
2012-10-13 05:31:57 +08:00
|
|
|
// so move I up to NewI. Slide [NewI;I) down one position.
|
2014-03-02 20:27:27 +08:00
|
|
|
std::copy_backward(NewI, I, std::next(I));
|
2013-10-11 05:28:47 +08:00
|
|
|
*NewI = LiveRange::Segment(DefVNI->def, NewIdx.getDeadSlot(), DefVNI);
|
2012-02-19 15:13:05 +08:00
|
|
|
}
|
|
|
|
|
2012-10-13 05:31:57 +08:00
|
|
|
void updateRegMaskSlots() {
|
2012-02-19 11:00:30 +08:00
|
|
|
SmallVectorImpl<SlotIndex>::iterator RI =
|
|
|
|
std::lower_bound(LIS.RegMaskSlots.begin(), LIS.RegMaskSlots.end(),
|
|
|
|
OldIdx);
|
2012-11-10 03:18:49 +08:00
|
|
|
assert(RI != LIS.RegMaskSlots.end() && *RI == OldIdx.getRegSlot() &&
|
|
|
|
"No RegMask at OldIdx.");
|
|
|
|
*RI = NewIdx.getRegSlot();
|
|
|
|
assert((RI == LIS.RegMaskSlots.begin() ||
|
2014-03-02 20:27:27 +08:00
|
|
|
SlotIndex::isEarlierInstr(*std::prev(RI), *RI)) &&
|
|
|
|
"Cannot move regmask instruction above another call");
|
|
|
|
assert((std::next(RI) == LIS.RegMaskSlots.end() ||
|
|
|
|
SlotIndex::isEarlierInstr(*RI, *std::next(RI))) &&
|
|
|
|
"Cannot move regmask instruction below another call");
|
2012-02-18 02:44:18 +08:00
|
|
|
}
|
|
|
|
|
2012-02-19 11:00:30 +08:00
|
|
|
// Return the last use of reg between NewIdx and OldIdx.
|
2015-09-26 05:51:14 +08:00
|
|
|
SlotIndex findLastUseBefore(unsigned Reg, LaneBitmask LaneMask) {
|
2012-09-12 14:56:16 +08:00
|
|
|
|
|
|
|
if (TargetRegisterInfo::isVirtualRegister(Reg)) {
|
2013-03-09 02:08:57 +08:00
|
|
|
SlotIndex LastUse = NewIdx;
|
2014-12-10 09:12:20 +08:00
|
|
|
for (MachineOperand &MO : MRI.use_nodbg_operands(Reg)) {
|
|
|
|
unsigned SubReg = MO.getSubReg();
|
|
|
|
if (SubReg != 0 && LaneMask != 0
|
|
|
|
&& (TRI.getSubRegIndexLaneMask(SubReg) & LaneMask) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const MachineInstr *MI = MO.getParent();
|
2012-09-12 14:56:16 +08:00
|
|
|
SlotIndex InstSlot = LIS.getSlotIndexes()->getInstructionIndex(MI);
|
|
|
|
if (InstSlot > LastUse && InstSlot < OldIdx)
|
|
|
|
LastUse = InstSlot;
|
|
|
|
}
|
2013-03-09 02:08:57 +08:00
|
|
|
return LastUse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is a regunit interval, so scanning the use list could be very
|
|
|
|
// expensive. Scan upwards from OldIdx instead.
|
|
|
|
assert(NewIdx < OldIdx && "Expected upwards move");
|
|
|
|
SlotIndexes *Indexes = LIS.getSlotIndexes();
|
|
|
|
MachineBasicBlock *MBB = Indexes->getMBBFromIndex(NewIdx);
|
|
|
|
|
|
|
|
// OldIdx may not correspond to an instruction any longer, so set MII to
|
|
|
|
// point to the next instruction after OldIdx, or MBB->end().
|
|
|
|
MachineBasicBlock::iterator MII = MBB->end();
|
|
|
|
if (MachineInstr *MI = Indexes->getInstructionFromIndex(
|
|
|
|
Indexes->getNextNonNullIndex(OldIdx)))
|
|
|
|
if (MI->getParent() == MBB)
|
|
|
|
MII = MI;
|
|
|
|
|
|
|
|
MachineBasicBlock::iterator Begin = MBB->begin();
|
|
|
|
while (MII != Begin) {
|
|
|
|
if ((--MII)->isDebugValue())
|
|
|
|
continue;
|
|
|
|
SlotIndex Idx = Indexes->getInstructionIndex(MII);
|
|
|
|
|
|
|
|
// Stop searching when NewIdx is reached.
|
|
|
|
if (!SlotIndex::isEarlierInstr(NewIdx, Idx))
|
|
|
|
return NewIdx;
|
|
|
|
|
|
|
|
// Check if MII uses Reg.
|
|
|
|
for (MIBundleOperands MO(MII); MO.isValid(); ++MO)
|
|
|
|
if (MO->isReg() &&
|
|
|
|
TargetRegisterInfo::isPhysicalRegister(MO->getReg()) &&
|
|
|
|
TRI.hasRegUnit(MO->getReg(), Reg))
|
|
|
|
return Idx;
|
2012-02-18 02:44:18 +08:00
|
|
|
}
|
2013-03-09 02:08:57 +08:00
|
|
|
// Didn't reach NewIdx. It must be the first instruction in the block.
|
|
|
|
return NewIdx;
|
2012-02-18 02:44:18 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-10-16 08:22:51 +08:00
|
|
|
void LiveIntervals::handleMove(MachineInstr* MI, bool UpdateFlags) {
|
2012-10-13 05:31:57 +08:00
|
|
|
assert(!MI->isBundled() && "Can't handle bundled instructions yet.");
|
2012-06-05 06:39:14 +08:00
|
|
|
SlotIndex OldIndex = Indexes->getInstructionIndex(MI);
|
|
|
|
Indexes->removeMachineInstrFromMaps(MI);
|
2012-10-13 05:31:57 +08:00
|
|
|
SlotIndex NewIndex = Indexes->insertMachineInstrInMaps(MI);
|
2012-02-18 07:43:40 +08:00
|
|
|
assert(getMBBStartIdx(MI->getParent()) <= OldIndex &&
|
|
|
|
OldIndex < getMBBEndIdx(MI->getParent()) &&
|
2012-02-18 02:44:18 +08:00
|
|
|
"Cannot handle moves across basic block boundaries.");
|
|
|
|
|
2012-10-16 08:22:51 +08:00
|
|
|
HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags);
|
2012-10-13 05:31:57 +08:00
|
|
|
HME.updateAllRanges(MI);
|
2012-02-22 06:29:38 +08:00
|
|
|
}
|
|
|
|
|
2012-06-20 06:50:53 +08:00
|
|
|
void LiveIntervals::handleMoveIntoBundle(MachineInstr* MI,
|
2012-10-16 08:22:51 +08:00
|
|
|
MachineInstr* BundleStart,
|
|
|
|
bool UpdateFlags) {
|
2012-10-13 05:31:57 +08:00
|
|
|
SlotIndex OldIndex = Indexes->getInstructionIndex(MI);
|
2012-06-05 06:39:14 +08:00
|
|
|
SlotIndex NewIndex = Indexes->getInstructionIndex(BundleStart);
|
2012-10-16 08:22:51 +08:00
|
|
|
HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags);
|
2012-10-13 05:31:57 +08:00
|
|
|
HME.updateAllRanges(MI);
|
2012-02-18 02:44:18 +08:00
|
|
|
}
|
2013-02-17 08:10:44 +08:00
|
|
|
|
2014-12-10 09:12:26 +08:00
|
|
|
void LiveIntervals::repairOldRegInRange(const MachineBasicBlock::iterator Begin,
|
|
|
|
const MachineBasicBlock::iterator End,
|
|
|
|
const SlotIndex endIdx,
|
|
|
|
LiveRange &LR, const unsigned Reg,
|
2015-09-26 05:51:14 +08:00
|
|
|
LaneBitmask LaneMask) {
|
2014-12-10 09:12:26 +08:00
|
|
|
LiveInterval::iterator LII = LR.find(endIdx);
|
|
|
|
SlotIndex lastUseIdx;
|
|
|
|
if (LII != LR.end() && LII->start < endIdx)
|
|
|
|
lastUseIdx = LII->end;
|
|
|
|
else
|
|
|
|
--LII;
|
|
|
|
|
|
|
|
for (MachineBasicBlock::iterator I = End; I != Begin;) {
|
|
|
|
--I;
|
|
|
|
MachineInstr *MI = I;
|
|
|
|
if (MI->isDebugValue())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SlotIndex instrIdx = getInstructionIndex(MI);
|
|
|
|
bool isStartValid = getInstructionFromIndex(LII->start);
|
|
|
|
bool isEndValid = getInstructionFromIndex(LII->end);
|
|
|
|
|
|
|
|
// FIXME: This doesn't currently handle early-clobber or multiple removed
|
|
|
|
// defs inside of the region to repair.
|
|
|
|
for (MachineInstr::mop_iterator OI = MI->operands_begin(),
|
|
|
|
OE = MI->operands_end(); OI != OE; ++OI) {
|
|
|
|
const MachineOperand &MO = *OI;
|
|
|
|
if (!MO.isReg() || MO.getReg() != Reg)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
unsigned SubReg = MO.getSubReg();
|
2015-09-26 05:51:14 +08:00
|
|
|
LaneBitmask Mask = TRI->getSubRegIndexLaneMask(SubReg);
|
2014-12-10 09:12:26 +08:00
|
|
|
if ((Mask & LaneMask) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (MO.isDef()) {
|
|
|
|
if (!isStartValid) {
|
|
|
|
if (LII->end.isDead()) {
|
|
|
|
SlotIndex prevStart;
|
|
|
|
if (LII != LR.begin())
|
|
|
|
prevStart = std::prev(LII)->start;
|
|
|
|
|
|
|
|
// FIXME: This could be more efficient if there was a
|
|
|
|
// removeSegment method that returned an iterator.
|
|
|
|
LR.removeSegment(*LII, true);
|
|
|
|
if (prevStart.isValid())
|
|
|
|
LII = LR.find(prevStart);
|
|
|
|
else
|
|
|
|
LII = LR.begin();
|
|
|
|
} else {
|
|
|
|
LII->start = instrIdx.getRegSlot();
|
|
|
|
LII->valno->def = instrIdx.getRegSlot();
|
|
|
|
if (MO.getSubReg() && !MO.isUndef())
|
|
|
|
lastUseIdx = instrIdx.getRegSlot();
|
|
|
|
else
|
|
|
|
lastUseIdx = SlotIndex();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lastUseIdx.isValid()) {
|
|
|
|
VNInfo *VNI = LR.getNextValue(instrIdx.getRegSlot(), VNInfoAllocator);
|
|
|
|
LiveRange::Segment S(instrIdx.getRegSlot(),
|
|
|
|
instrIdx.getDeadSlot(), VNI);
|
|
|
|
LII = LR.addSegment(S);
|
|
|
|
} else if (LII->start != instrIdx.getRegSlot()) {
|
|
|
|
VNInfo *VNI = LR.getNextValue(instrIdx.getRegSlot(), VNInfoAllocator);
|
|
|
|
LiveRange::Segment S(instrIdx.getRegSlot(), lastUseIdx, VNI);
|
|
|
|
LII = LR.addSegment(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (MO.getSubReg() && !MO.isUndef())
|
|
|
|
lastUseIdx = instrIdx.getRegSlot();
|
|
|
|
else
|
|
|
|
lastUseIdx = SlotIndex();
|
|
|
|
} else if (MO.isUse()) {
|
|
|
|
// FIXME: This should probably be handled outside of this branch,
|
|
|
|
// either as part of the def case (for defs inside of the region) or
|
|
|
|
// after the loop over the region.
|
|
|
|
if (!isEndValid && !LII->end.isBlock())
|
|
|
|
LII->end = instrIdx.getRegSlot();
|
|
|
|
if (!lastUseIdx.isValid())
|
|
|
|
lastUseIdx = instrIdx.getRegSlot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-17 08:10:44 +08:00
|
|
|
void
|
|
|
|
LiveIntervals::repairIntervalsInRange(MachineBasicBlock *MBB,
|
2013-02-17 19:09:00 +08:00
|
|
|
MachineBasicBlock::iterator Begin,
|
|
|
|
MachineBasicBlock::iterator End,
|
2013-02-17 11:48:23 +08:00
|
|
|
ArrayRef<unsigned> OrigRegs) {
|
2013-02-21 06:10:00 +08:00
|
|
|
// Find anchor points, which are at the beginning/end of blocks or at
|
|
|
|
// instructions that already have indexes.
|
|
|
|
while (Begin != MBB->begin() && !Indexes->hasIndex(Begin))
|
|
|
|
--Begin;
|
|
|
|
while (End != MBB->end() && !Indexes->hasIndex(End))
|
|
|
|
++End;
|
|
|
|
|
2013-02-20 14:46:48 +08:00
|
|
|
SlotIndex endIdx;
|
|
|
|
if (End == MBB->end())
|
|
|
|
endIdx = getMBBEndIdx(MBB).getPrevSlot();
|
2013-02-17 19:09:00 +08:00
|
|
|
else
|
2013-02-20 14:46:48 +08:00
|
|
|
endIdx = getInstructionIndex(End);
|
2013-02-17 19:09:00 +08:00
|
|
|
|
2013-02-20 14:46:41 +08:00
|
|
|
Indexes->repairIndexesInRange(MBB, Begin, End);
|
|
|
|
|
2013-02-20 14:46:48 +08:00
|
|
|
for (MachineBasicBlock::iterator I = End; I != Begin;) {
|
|
|
|
--I;
|
|
|
|
MachineInstr *MI = I;
|
2013-02-23 18:25:25 +08:00
|
|
|
if (MI->isDebugValue())
|
|
|
|
continue;
|
2013-02-20 14:46:48 +08:00
|
|
|
for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(),
|
|
|
|
MOE = MI->operands_end(); MOI != MOE; ++MOI) {
|
|
|
|
if (MOI->isReg() &&
|
|
|
|
TargetRegisterInfo::isVirtualRegister(MOI->getReg()) &&
|
|
|
|
!hasInterval(MOI->getReg())) {
|
2013-08-15 07:50:16 +08:00
|
|
|
createAndComputeVirtRegInterval(MOI->getReg());
|
2013-02-20 14:46:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-17 08:10:44 +08:00
|
|
|
for (unsigned i = 0, e = OrigRegs.size(); i != e; ++i) {
|
|
|
|
unsigned Reg = OrigRegs[i];
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LiveInterval &LI = getInterval(Reg);
|
2013-02-21 06:09:57 +08:00
|
|
|
// FIXME: Should we support undefs that gain defs?
|
|
|
|
if (!LI.hasAtLeastOneValue())
|
|
|
|
continue;
|
|
|
|
|
2014-12-11 08:59:06 +08:00
|
|
|
for (LiveInterval::SubRange &S : LI.subranges()) {
|
|
|
|
repairOldRegInRange(Begin, End, endIdx, S, Reg, S.LaneMask);
|
2013-02-17 08:10:44 +08:00
|
|
|
}
|
2014-12-10 09:12:26 +08:00
|
|
|
repairOldRegInRange(Begin, End, endIdx, LI, Reg);
|
2013-02-17 08:10:44 +08:00
|
|
|
}
|
|
|
|
}
|
2015-01-22 02:50:21 +08:00
|
|
|
|
|
|
|
void LiveIntervals::removePhysRegDefAt(unsigned Reg, SlotIndex Pos) {
|
|
|
|
for (MCRegUnitIterator Units(Reg, TRI); Units.isValid(); ++Units) {
|
|
|
|
if (LiveRange *LR = getCachedRegUnit(*Units))
|
|
|
|
if (VNInfo *VNI = LR->getVNInfoAt(Pos))
|
|
|
|
LR->removeValNo(VNI);
|
|
|
|
}
|
|
|
|
}
|
2015-01-22 03:02:30 +08:00
|
|
|
|
|
|
|
void LiveIntervals::removeVRegDefAt(LiveInterval &LI, SlotIndex Pos) {
|
|
|
|
VNInfo *VNI = LI.getVNInfoAt(Pos);
|
|
|
|
if (VNI == nullptr)
|
|
|
|
return;
|
|
|
|
LI.removeValNo(VNI);
|
|
|
|
|
|
|
|
// Also remove the value in subranges.
|
|
|
|
for (LiveInterval::SubRange &S : LI.subranges()) {
|
|
|
|
if (VNInfo *SVNI = S.getVNInfoAt(Pos))
|
|
|
|
S.removeValNo(SVNI);
|
|
|
|
}
|
|
|
|
LI.removeEmptySubRanges();
|
|
|
|
}
|
2015-09-22 11:44:41 +08:00
|
|
|
|
|
|
|
void LiveIntervals::splitSeparateComponents(LiveInterval &LI,
|
|
|
|
SmallVectorImpl<LiveInterval*> &SplitLIs) {
|
|
|
|
ConnectedVNInfoEqClasses ConEQ(*this);
|
|
|
|
unsigned NumComp = ConEQ.Classify(&LI);
|
|
|
|
if (NumComp <= 1)
|
|
|
|
return;
|
|
|
|
DEBUG(dbgs() << " Split " << NumComp << " components: " << LI << '\n');
|
|
|
|
unsigned Reg = LI.reg;
|
|
|
|
const TargetRegisterClass *RegClass = MRI->getRegClass(Reg);
|
|
|
|
for (unsigned I = 1; I < NumComp; ++I) {
|
|
|
|
unsigned NewVReg = MRI->createVirtualRegister(RegClass);
|
|
|
|
LiveInterval &NewLI = createEmptyInterval(NewVReg);
|
|
|
|
SplitLIs.push_back(&NewLI);
|
|
|
|
}
|
|
|
|
ConEQ.Distribute(LI, SplitLIs.data(), *MRI);
|
|
|
|
}
|