Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
//===-- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ---------===//
|
|
|
|
//
|
|
|
|
// 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.
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass performs loop invariant code motion on machine instructions. We
|
|
|
|
// attempt to remove as much code from the body of a loop as possible.
|
|
|
|
//
|
2009-01-16 06:01:38 +08:00
|
|
|
// This pass is not intended to be a replacement or a complete alternative
|
|
|
|
// for the LLVM-IR-level LICM pass. It is only designed to hoist simple
|
|
|
|
// constructs that are not exposed before lowering and instruction selection.
|
|
|
|
//
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-04 14:41:45 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/SmallSet.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2010-04-07 08:41:17 +08:00
|
|
|
#include "llvm/CodeGen/MachineFrameInfo.h"
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2009-10-28 11:21:57 +08:00
|
|
|
#include "llvm/CodeGen/MachineMemOperand.h"
|
2008-01-03 03:32:43 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2009-10-28 11:21:57 +08:00
|
|
|
#include "llvm/CodeGen/PseudoSourceValue.h"
|
2015-06-13 11:42:11 +08:00
|
|
|
#include "llvm/CodeGen/TargetSchedule.h"
|
2011-10-13 05:33:49 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2008-01-04 14:41:45 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-25 08:23:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
#include "llvm/Target/TargetSubtargetInfo.h"
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "machine-licm"
|
|
|
|
|
2011-10-13 05:33:49 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
AvoidSpeculation("avoid-speculation",
|
|
|
|
cl::desc("MachineLICM should avoid speculation"),
|
2011-10-26 09:26:57 +08:00
|
|
|
cl::init(true), cl::Hidden);
|
2011-10-13 05:33:49 +08:00
|
|
|
|
2015-01-09 06:10:48 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
HoistCheapInsts("hoist-cheap-insts",
|
|
|
|
cl::desc("MachineLICM should hoist even cheap instructions"),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
2015-03-14 18:58:38 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
SinkInstsToAvoidSpills("sink-insts-to-avoid-spills",
|
|
|
|
cl::desc("MachineLICM should sink instructions into "
|
|
|
|
"loops to avoid register spills"),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
2010-10-16 10:20:26 +08:00
|
|
|
STATISTIC(NumHoisted,
|
|
|
|
"Number of machine instructions hoisted out of loops");
|
|
|
|
STATISTIC(NumLowRP,
|
|
|
|
"Number of instructions hoisted in low reg pressure situation");
|
|
|
|
STATISTIC(NumHighLatency,
|
|
|
|
"Number of high latency instructions hoisted");
|
|
|
|
STATISTIC(NumCSEed,
|
|
|
|
"Number of hoisted machine instructions CSEed");
|
2010-04-07 08:41:17 +08:00
|
|
|
STATISTIC(NumPostRAHoisted,
|
|
|
|
"Number of machine instructions hoisted out of loops post regalloc");
|
2007-12-08 09:47:01 +08:00
|
|
|
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
namespace {
|
2009-10-25 14:33:48 +08:00
|
|
|
class MachineLICM : public MachineFunctionPass {
|
2007-12-12 07:27:51 +08:00
|
|
|
const TargetInstrInfo *TII;
|
2013-01-12 04:05:37 +08:00
|
|
|
const TargetLoweringBase *TLI;
|
2009-09-26 07:58:45 +08:00
|
|
|
const TargetRegisterInfo *TRI;
|
2010-04-07 08:41:17 +08:00
|
|
|
const MachineFrameInfo *MFI;
|
2010-10-14 09:16:09 +08:00
|
|
|
MachineRegisterInfo *MRI;
|
2015-06-13 11:42:11 +08:00
|
|
|
TargetSchedModel SchedModel;
|
2012-02-09 05:23:03 +08:00
|
|
|
bool PreRegAlloc;
|
2007-12-12 03:40:06 +08:00
|
|
|
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
// Various analyses that we use...
|
2009-10-08 01:38:06 +08:00
|
|
|
AliasAnalysis *AA; // Alias analysis info.
|
2010-04-08 09:03:47 +08:00
|
|
|
MachineLoopInfo *MLI; // Current MachineLoopInfo
|
2008-05-13 03:38:32 +08:00
|
|
|
MachineDominatorTree *DT; // Machine dominator tree for the cur loop
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
|
|
|
// State that is updated as we process loops
|
2008-05-13 03:38:32 +08:00
|
|
|
bool Changed; // True if a loop is changed.
|
2010-05-29 08:06:36 +08:00
|
|
|
bool FirstInLoop; // True if it's the first LICM in the loop.
|
2008-05-13 03:38:32 +08:00
|
|
|
MachineLoop *CurLoop; // The current loop we are working on.
|
2009-01-16 06:01:38 +08:00
|
|
|
MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
|
2009-02-05 16:45:46 +08:00
|
|
|
|
2012-04-11 08:00:26 +08:00
|
|
|
// Exit blocks for CurLoop.
|
|
|
|
SmallVector<MachineBasicBlock*, 8> ExitBlocks;
|
|
|
|
|
|
|
|
bool isExitBlock(const MachineBasicBlock *MBB) const {
|
|
|
|
return std::find(ExitBlocks.begin(), ExitBlocks.end(), MBB) !=
|
|
|
|
ExitBlocks.end();
|
|
|
|
}
|
|
|
|
|
2010-10-14 09:16:09 +08:00
|
|
|
// Track 'estimated' register pressure.
|
2010-10-16 10:20:26 +08:00
|
|
|
SmallSet<unsigned, 32> RegSeen;
|
2010-10-14 09:16:09 +08:00
|
|
|
SmallVector<unsigned, 8> RegPressure;
|
2010-10-16 10:20:26 +08:00
|
|
|
|
2015-04-14 19:56:25 +08:00
|
|
|
// Register pressure "limit" per register pressure set. If the pressure
|
2010-10-16 10:20:26 +08:00
|
|
|
// is higher than the limit, then it's considered high.
|
2010-10-14 09:16:09 +08:00
|
|
|
SmallVector<unsigned, 8> RegLimit;
|
|
|
|
|
2010-10-16 10:20:26 +08:00
|
|
|
// Register pressure on path leading from loop preheader to current BB.
|
|
|
|
SmallVector<SmallVector<unsigned, 8>, 16> BackTrace;
|
|
|
|
|
2010-07-30 01:45:24 +08:00
|
|
|
// For each opcode, keep a list of potential CSE instructions.
|
2009-11-04 05:40:02 +08:00
|
|
|
DenseMap<unsigned, std::vector<const MachineInstr*> > CSEMap;
|
2010-04-07 08:41:17 +08:00
|
|
|
|
2011-10-12 07:48:44 +08:00
|
|
|
enum {
|
|
|
|
SpeculateFalse = 0,
|
|
|
|
SpeculateTrue = 1,
|
|
|
|
SpeculateUnknown = 2
|
|
|
|
};
|
|
|
|
|
2011-10-12 02:09:58 +08:00
|
|
|
// If a MBB does not dominate loop exiting blocks then it may not safe
|
|
|
|
// to hoist loads from this block.
|
2011-10-12 07:48:44 +08:00
|
|
|
// Tri-state: 0 - false, 1 - true, 2 - unknown
|
|
|
|
unsigned SpeculationState;
|
2011-10-12 02:09:58 +08:00
|
|
|
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-04-07 08:41:17 +08:00
|
|
|
MachineLICM() :
|
2010-10-20 01:21:58 +08:00
|
|
|
MachineFunctionPass(ID), PreRegAlloc(true) {
|
|
|
|
initializeMachineLICMPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-04-07 08:41:17 +08:00
|
|
|
|
|
|
|
explicit MachineLICM(bool PreRA) :
|
2010-10-20 01:21:58 +08:00
|
|
|
MachineFunctionPass(ID), PreRegAlloc(PreRA) {
|
|
|
|
initializeMachineLICMPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
AU.addRequired<MachineLoopInfo>();
|
|
|
|
AU.addRequired<MachineDominatorTree>();
|
[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>();
|
2008-01-04 16:48:49 +08:00
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
|
|
|
AU.addPreserved<MachineDominatorTree>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
}
|
2009-02-05 16:45:46 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
void releaseMemory() override {
|
2010-10-16 10:20:26 +08:00
|
|
|
RegSeen.clear();
|
2010-10-14 09:16:09 +08:00
|
|
|
RegPressure.clear();
|
|
|
|
RegLimit.clear();
|
2010-10-20 02:58:51 +08:00
|
|
|
BackTrace.clear();
|
2009-02-05 16:45:46 +08:00
|
|
|
CSEMap.clear();
|
|
|
|
}
|
|
|
|
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
private:
|
2010-04-08 09:03:47 +08:00
|
|
|
/// CandidateInfo - Keep track of information about hoisting candidates.
|
|
|
|
struct CandidateInfo {
|
|
|
|
MachineInstr *MI;
|
|
|
|
unsigned Def;
|
2010-04-14 02:16:00 +08:00
|
|
|
int FI;
|
|
|
|
CandidateInfo(MachineInstr *mi, unsigned def, int fi)
|
|
|
|
: MI(mi), Def(def), FI(fi) {}
|
2010-04-08 09:03:47 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop
|
|
|
|
/// invariants out to the preheader.
|
2010-04-17 15:07:11 +08:00
|
|
|
void HoistRegionPostRA();
|
2010-04-08 09:03:47 +08:00
|
|
|
|
|
|
|
/// HoistPostRA - When an instruction is found to only use loop invariant
|
|
|
|
/// operands that is safe to hoist, this instruction is called to do the
|
|
|
|
/// dirty work.
|
|
|
|
void HoistPostRA(MachineInstr *MI, unsigned Def);
|
|
|
|
|
|
|
|
/// ProcessMI - Examine the instruction for potentai LICM candidate. Also
|
|
|
|
/// gather register def and frame object update information.
|
2012-01-21 06:27:12 +08:00
|
|
|
void ProcessMI(MachineInstr *MI,
|
|
|
|
BitVector &PhysRegDefs,
|
|
|
|
BitVector &PhysRegClobbers,
|
2010-04-08 09:03:47 +08:00
|
|
|
SmallSet<int, 32> &StoredFIs,
|
2013-07-12 00:22:38 +08:00
|
|
|
SmallVectorImpl<CandidateInfo> &Candidates);
|
2010-04-08 09:03:47 +08:00
|
|
|
|
2010-04-17 15:07:11 +08:00
|
|
|
/// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the
|
|
|
|
/// current loop.
|
|
|
|
void AddToLiveIns(unsigned Reg);
|
2010-04-08 09:03:47 +08:00
|
|
|
|
2010-04-14 02:16:00 +08:00
|
|
|
/// IsLICMCandidate - Returns true if the instruction may be a suitable
|
2010-07-12 08:00:35 +08:00
|
|
|
/// candidate for LICM. e.g. If the instruction is a call, then it's
|
|
|
|
/// obviously not safe to hoist it.
|
2010-04-14 02:16:00 +08:00
|
|
|
bool IsLICMCandidate(MachineInstr &I);
|
|
|
|
|
2007-12-09 07:58:46 +08:00
|
|
|
/// IsLoopInvariantInst - Returns true if the instruction is loop
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
/// invariant. I.e., all virtual register operands are defined outside of
|
|
|
|
/// the loop, physical registers aren't accessed (explicitly or implicitly),
|
|
|
|
/// and the instruction is hoistable.
|
2012-02-09 05:23:00 +08:00
|
|
|
///
|
2007-12-09 07:58:46 +08:00
|
|
|
bool IsLoopInvariantInst(MachineInstr &I);
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2012-04-11 08:00:26 +08:00
|
|
|
/// HasLoopPHIUse - Return true if the specified instruction is used by any
|
|
|
|
/// phi node in the current loop.
|
|
|
|
bool HasLoopPHIUse(const MachineInstr *MI) const;
|
2011-04-12 05:09:18 +08:00
|
|
|
|
2010-10-20 02:58:51 +08:00
|
|
|
/// HasHighOperandLatency - Compute operand latency between a def of 'Reg'
|
|
|
|
/// and an use in the current loop, return true if the target considered
|
|
|
|
/// it 'high'.
|
2010-10-26 10:08:50 +08:00
|
|
|
bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx,
|
|
|
|
unsigned Reg) const;
|
|
|
|
|
|
|
|
bool IsCheapInstruction(MachineInstr &MI) const;
|
2010-10-14 09:16:09 +08:00
|
|
|
|
2010-10-21 06:03:58 +08:00
|
|
|
/// CanCauseHighRegPressure - Visit BBs from header to current BB,
|
|
|
|
/// check if hoisting an instruction of the given cost matrix can cause high
|
2010-10-16 10:20:26 +08:00
|
|
|
/// register pressure.
|
2015-04-04 00:19:48 +08:00
|
|
|
bool CanCauseHighRegPressure(const DenseMap<unsigned, int> &Cost,
|
|
|
|
bool Cheap);
|
2010-10-21 06:03:58 +08:00
|
|
|
|
|
|
|
/// UpdateBackTraceRegPressure - Traverse the back trace from header to
|
|
|
|
/// the current block and update their register pressures to reflect the
|
|
|
|
/// effect of hoisting MI from the current block to the preheader.
|
|
|
|
void UpdateBackTraceRegPressure(const MachineInstr *MI);
|
2010-10-16 10:20:26 +08:00
|
|
|
|
2009-02-04 17:19:56 +08:00
|
|
|
/// IsProfitableToHoist - Return true if it is potentially profitable to
|
|
|
|
/// hoist the given loop invariant.
|
2009-11-21 07:31:34 +08:00
|
|
|
bool IsProfitableToHoist(MachineInstr &MI);
|
2009-02-04 17:19:56 +08:00
|
|
|
|
2011-10-12 02:09:58 +08:00
|
|
|
/// IsGuaranteedToExecute - Check if this mbb is guaranteed to execute.
|
|
|
|
/// If not then a load from this mbb may not be safe to hoist.
|
|
|
|
bool IsGuaranteedToExecute(MachineBasicBlock *BB);
|
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
void EnterScope(MachineBasicBlock *MBB);
|
|
|
|
|
|
|
|
void ExitScope(MachineBasicBlock *MBB);
|
|
|
|
|
|
|
|
/// ExitScopeIfDone - Destroy scope for the MBB that corresponds to given
|
|
|
|
/// dominator tree node if its a leaf or all of its children are done. Walk
|
|
|
|
/// up the dominator tree to destroy ancestors which are now done.
|
|
|
|
void ExitScopeIfDone(MachineDomTreeNode *Node,
|
|
|
|
DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren,
|
|
|
|
DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap);
|
|
|
|
|
|
|
|
/// HoistOutOfLoop - Walk the specified loop in the CFG (defined by all
|
|
|
|
/// blocks dominated by the specified header block, and that are in the
|
|
|
|
/// current loop) in depth first order w.r.t the DominatorTree. This allows
|
|
|
|
/// us to visit definitions before uses, allowing us to hoist a loop body in
|
|
|
|
/// one pass without iteration.
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
///
|
2011-12-22 10:05:40 +08:00
|
|
|
void HoistOutOfLoop(MachineDomTreeNode *LoopHeaderNode);
|
|
|
|
void HoistRegion(MachineDomTreeNode *N, bool IsHeader);
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2015-03-14 18:58:38 +08:00
|
|
|
/// SinkIntoLoop - Sink instructions into loops if profitable. This
|
|
|
|
/// especially tries to prevent register spills caused by register pressure
|
|
|
|
/// if there is little to no overhead moving instructions into loops.
|
|
|
|
void SinkIntoLoop();
|
|
|
|
|
2010-10-16 10:20:26 +08:00
|
|
|
/// InitRegPressure - Find all virtual register references that are liveout
|
|
|
|
/// of the preheader to initialize the starting "register pressure". Note
|
|
|
|
/// this does not count live through (livein but not used) registers.
|
2010-10-14 09:16:09 +08:00
|
|
|
void InitRegPressure(MachineBasicBlock *BB);
|
|
|
|
|
2015-04-08 00:42:35 +08:00
|
|
|
/// calcRegisterCost - Calculate the additional register pressure that the
|
|
|
|
/// registers used in MI cause.
|
|
|
|
///
|
|
|
|
/// If 'ConsiderSeen' is true, updates 'RegSeen' and uses the information to
|
|
|
|
/// figure out which usages are live-ins.
|
|
|
|
/// FIXME: Figure out a way to consider 'RegSeen' from all code paths.
|
|
|
|
DenseMap<unsigned, int> calcRegisterCost(const MachineInstr *MI,
|
|
|
|
bool ConsiderSeen,
|
|
|
|
bool ConsiderUnseenAsDef);
|
|
|
|
|
2010-10-21 06:03:58 +08:00
|
|
|
/// UpdateRegPressure - Update estimate of register pressure after the
|
|
|
|
/// specified instruction.
|
2015-04-08 00:42:35 +08:00
|
|
|
void UpdateRegPressure(const MachineInstr *MI,
|
|
|
|
bool ConsiderUnseenAsDef = false);
|
2010-10-14 09:16:09 +08:00
|
|
|
|
2009-10-30 01:47:20 +08:00
|
|
|
/// ExtractHoistableLoad - Unfold a load from the given machineinstr if
|
|
|
|
/// the load itself could be hoisted. Return the unfolded and hoistable
|
|
|
|
/// load, or null if the load couldn't be unfolded or if it wouldn't
|
|
|
|
/// be hoistable.
|
|
|
|
MachineInstr *ExtractHoistableLoad(MachineInstr *MI);
|
|
|
|
|
2009-11-07 11:52:02 +08:00
|
|
|
/// LookForDuplicate - Find an instruction amount PrevMIs that is a
|
|
|
|
/// duplicate of MI. Return this instruction if it's found.
|
|
|
|
const MachineInstr *LookForDuplicate(const MachineInstr *MI,
|
|
|
|
std::vector<const MachineInstr*> &PrevMIs);
|
|
|
|
|
2009-11-05 08:51:13 +08:00
|
|
|
/// EliminateCSE - Given a LICM'ed instruction, look for an instruction on
|
|
|
|
/// the preheader that compute the same value. If it's found, do a RAU on
|
|
|
|
/// with the definition of the existing instruction rather than hoisting
|
|
|
|
/// the instruction to the preheader.
|
|
|
|
bool EliminateCSE(MachineInstr *MI,
|
|
|
|
DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI);
|
|
|
|
|
2011-10-12 08:09:14 +08:00
|
|
|
/// MayCSE - Return true if the given instruction will be CSE'd if it's
|
|
|
|
/// hoisted out of the loop.
|
|
|
|
bool MayCSE(MachineInstr *MI);
|
|
|
|
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
/// Hoist - When an instruction is found to only use loop invariant operands
|
|
|
|
/// that is safe to hoist, this instruction is called to do the dirty work.
|
2010-10-21 06:03:58 +08:00
|
|
|
/// It returns true if the instruction is hoisted.
|
|
|
|
bool Hoist(MachineInstr *MI, MachineBasicBlock *Preheader);
|
2009-11-04 05:40:02 +08:00
|
|
|
|
|
|
|
/// InitCSEMap - Initialize the CSE map with instructions that are in the
|
|
|
|
/// current loop preheader that may become duplicates of instructions that
|
|
|
|
/// are hoisted out of the loop.
|
|
|
|
void InitCSEMap(MachineBasicBlock *BB);
|
2010-06-23 01:25:57 +08:00
|
|
|
|
|
|
|
/// getCurPreheader - Get the preheader for the current loop, splitting
|
|
|
|
/// a critical edge if needed.
|
|
|
|
MachineBasicBlock *getCurPreheader();
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char MachineLICM::ID = 0;
|
2012-02-09 05:23:13 +08:00
|
|
|
char &llvm::MachineLICMID = MachineLICM::ID;
|
2010-10-13 03:48:12 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(MachineLICM, "machinelicm",
|
|
|
|
"Machine Loop Invariant Code Motion", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
|
[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(MachineLICM, "machinelicm",
|
2010-10-08 06:25:06 +08:00
|
|
|
"Machine Loop Invariant Code Motion", false, false)
|
2008-05-13 08:00:25 +08:00
|
|
|
|
2010-06-23 01:25:57 +08:00
|
|
|
/// LoopIsOuterMostWithPredecessor - Test if the given loop is the outer-most
|
|
|
|
/// loop that has a unique predecessor.
|
|
|
|
static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) {
|
2010-07-10 02:49:45 +08:00
|
|
|
// Check whether this loop even has a unique predecessor.
|
|
|
|
if (!CurLoop->getLoopPredecessor())
|
|
|
|
return false;
|
|
|
|
// Ok, now check to see if any of its outer loops do.
|
2009-01-16 06:01:38 +08:00
|
|
|
for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop())
|
2010-06-23 01:25:57 +08:00
|
|
|
if (L->getLoopPredecessor())
|
2009-01-16 06:01:38 +08:00
|
|
|
return false;
|
2010-07-10 02:49:45 +08:00
|
|
|
// None of them did, so this is the outermost with a unique predecessor.
|
2009-01-16 06:01:38 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
|
2014-04-01 01:43:35 +08:00
|
|
|
if (skipOptnoneFunction(*MF.getFunction()))
|
|
|
|
return false;
|
|
|
|
|
2010-05-29 08:06:36 +08:00
|
|
|
Changed = FirstInLoop = false;
|
2015-06-13 11:42:11 +08:00
|
|
|
const TargetSubtargetInfo &ST = MF.getSubtarget();
|
|
|
|
TII = ST.getInstrInfo();
|
|
|
|
TLI = ST.getTargetLowering();
|
|
|
|
TRI = ST.getRegisterInfo();
|
2010-04-07 08:41:17 +08:00
|
|
|
MFI = MF.getFrameInfo();
|
2010-10-14 09:16:09 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
2015-06-13 11:42:11 +08:00
|
|
|
SchedModel.init(ST.getSchedModel(), &ST, TII);
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2012-02-09 05:23:03 +08:00
|
|
|
PreRegAlloc = MRI->isSSA();
|
|
|
|
|
2012-02-11 08:40:36 +08:00
|
|
|
if (PreRegAlloc)
|
|
|
|
DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: ");
|
|
|
|
else
|
|
|
|
DEBUG(dbgs() << "******** Post-regalloc Machine LICM: ");
|
2012-08-22 14:07:19 +08:00
|
|
|
DEBUG(dbgs() << MF.getName() << " ********\n");
|
2012-02-11 08:40:36 +08:00
|
|
|
|
2010-10-14 09:16:09 +08:00
|
|
|
if (PreRegAlloc) {
|
|
|
|
// Estimate register pressure during pre-regalloc pass.
|
2015-04-14 19:56:25 +08:00
|
|
|
unsigned NumRPS = TRI->getNumRegPressureSets();
|
|
|
|
RegPressure.resize(NumRPS);
|
2010-10-14 09:16:09 +08:00
|
|
|
std::fill(RegPressure.begin(), RegPressure.end(), 0);
|
2015-04-14 19:56:25 +08:00
|
|
|
RegLimit.resize(NumRPS);
|
|
|
|
for (unsigned i = 0, e = NumRPS; i != e; ++i)
|
|
|
|
RegLimit[i] = TRI->getRegPressureSetLimit(MF, i);
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
|
|
|
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
// Get our Loop information...
|
2010-04-08 09:03:47 +08:00
|
|
|
MLI = &getAnalysis<MachineLoopInfo>();
|
|
|
|
DT = &getAnalysis<MachineDominatorTree>();
|
[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();
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2010-07-10 02:49:45 +08:00
|
|
|
SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
CurLoop = Worklist.pop_back_val();
|
2014-04-14 08:51:57 +08:00
|
|
|
CurPreheader = nullptr;
|
2012-04-11 08:00:26 +08:00
|
|
|
ExitBlocks.clear();
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2010-04-08 09:03:47 +08:00
|
|
|
// If this is done before regalloc, only visit outer-most preheader-sporting
|
|
|
|
// loops.
|
2010-07-10 02:49:45 +08:00
|
|
|
if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) {
|
|
|
|
Worklist.append(CurLoop->begin(), CurLoop->end());
|
2009-01-16 06:01:38 +08:00
|
|
|
continue;
|
2010-07-10 02:49:45 +08:00
|
|
|
}
|
2009-01-16 06:01:38 +08:00
|
|
|
|
2012-04-11 08:00:26 +08:00
|
|
|
CurLoop->getExitBlocks(ExitBlocks);
|
|
|
|
|
2010-04-07 08:41:17 +08:00
|
|
|
if (!PreRegAlloc)
|
2010-04-17 15:07:11 +08:00
|
|
|
HoistRegionPostRA();
|
2010-04-07 08:41:17 +08:00
|
|
|
else {
|
2010-04-17 15:07:11 +08:00
|
|
|
// CSEMap is initialized for loop header when the first instruction is
|
|
|
|
// being hoisted.
|
|
|
|
MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader());
|
2010-05-29 08:06:36 +08:00
|
|
|
FirstInLoop = true;
|
2011-12-22 10:05:40 +08:00
|
|
|
HoistOutOfLoop(N);
|
2010-04-07 08:41:17 +08:00
|
|
|
CSEMap.clear();
|
2015-03-14 18:58:38 +08:00
|
|
|
|
|
|
|
if (SinkInstsToAvoidSpills)
|
|
|
|
SinkIntoLoop();
|
2010-04-07 08:41:17 +08:00
|
|
|
}
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2010-04-08 09:03:47 +08:00
|
|
|
/// InstructionStoresToFI - Return true if instruction stores to the
|
|
|
|
/// specified frame.
|
|
|
|
static bool InstructionStoresToFI(const MachineInstr *MI, int FI) {
|
|
|
|
for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
|
|
|
|
oe = MI->memoperands_end(); o != oe; ++o) {
|
2014-04-15 15:22:52 +08:00
|
|
|
if (!(*o)->isStore() || !(*o)->getPseudoValue())
|
2010-04-08 09:03:47 +08:00
|
|
|
continue;
|
|
|
|
if (const FixedStackPseudoSourceValue *Value =
|
2014-04-15 15:22:52 +08:00
|
|
|
dyn_cast<FixedStackPseudoSourceValue>((*o)->getPseudoValue())) {
|
2010-04-08 09:03:47 +08:00
|
|
|
if (Value->getFrameIndex() == FI)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// ProcessMI - Examine the instruction for potentai LICM candidate. Also
|
|
|
|
/// gather register def and frame object update information.
|
|
|
|
void MachineLICM::ProcessMI(MachineInstr *MI,
|
2012-01-21 06:27:12 +08:00
|
|
|
BitVector &PhysRegDefs,
|
|
|
|
BitVector &PhysRegClobbers,
|
2010-04-08 09:03:47 +08:00
|
|
|
SmallSet<int, 32> &StoredFIs,
|
2013-07-12 00:22:38 +08:00
|
|
|
SmallVectorImpl<CandidateInfo> &Candidates) {
|
2010-04-08 09:03:47 +08:00
|
|
|
bool RuledOut = false;
|
2010-04-14 04:21:05 +08:00
|
|
|
bool HasNonInvariantUse = false;
|
2010-04-08 09:03:47 +08:00
|
|
|
unsigned Def = 0;
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (MO.isFI()) {
|
|
|
|
// Remember if the instruction stores to the frame index.
|
|
|
|
int FI = MO.getIndex();
|
|
|
|
if (!StoredFIs.count(FI) &&
|
|
|
|
MFI->isSpillSlotObjectIndex(FI) &&
|
|
|
|
InstructionStoresToFI(MI, FI))
|
|
|
|
StoredFIs.insert(FI);
|
2010-04-14 04:21:05 +08:00
|
|
|
HasNonInvariantUse = true;
|
2010-04-08 09:03:47 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-21 06:27:12 +08:00
|
|
|
// We can't hoist an instruction defining a physreg that is clobbered in
|
|
|
|
// the loop.
|
|
|
|
if (MO.isRegMask()) {
|
2012-02-03 07:52:57 +08:00
|
|
|
PhysRegClobbers.setBitsNotInMask(MO.getRegMask());
|
2012-01-21 06:27:12 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-08 09:03:47 +08:00
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
|
|
|
|
"Not expecting virtual register!");
|
|
|
|
|
2010-04-14 02:16:00 +08:00
|
|
|
if (!MO.isDef()) {
|
2012-01-21 06:27:12 +08:00
|
|
|
if (Reg && (PhysRegDefs.test(Reg) || PhysRegClobbers.test(Reg)))
|
2010-04-14 04:21:05 +08:00
|
|
|
// If it's using a non-loop-invariant register, then it's obviously not
|
|
|
|
// safe to hoist.
|
|
|
|
HasNonInvariantUse = true;
|
2010-04-08 09:03:47 +08:00
|
|
|
continue;
|
2010-04-14 02:16:00 +08:00
|
|
|
}
|
2010-04-08 09:03:47 +08:00
|
|
|
|
|
|
|
if (MO.isImplicit()) {
|
2012-06-02 07:28:30 +08:00
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
PhysRegClobbers.set(*AI);
|
2010-04-08 09:03:47 +08:00
|
|
|
if (!MO.isDead())
|
|
|
|
// Non-dead implicit def? This cannot be hoisted.
|
|
|
|
RuledOut = true;
|
|
|
|
// No need to check if a dead implicit def is also defined by
|
|
|
|
// another instruction.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: For now, avoid instructions with multiple defs, unless
|
|
|
|
// it's a dead implicit def.
|
|
|
|
if (Def)
|
|
|
|
RuledOut = true;
|
|
|
|
else
|
|
|
|
Def = Reg;
|
|
|
|
|
|
|
|
// If we have already seen another instruction that defines the same
|
2012-01-21 06:27:12 +08:00
|
|
|
// register, then this is not safe. Two defs is indicated by setting a
|
|
|
|
// PhysRegClobbers bit.
|
2012-06-02 07:28:30 +08:00
|
|
|
for (MCRegAliasIterator AS(Reg, TRI, true); AS.isValid(); ++AS) {
|
2012-01-24 05:01:15 +08:00
|
|
|
if (PhysRegDefs.test(*AS))
|
|
|
|
PhysRegClobbers.set(*AS);
|
|
|
|
PhysRegDefs.set(*AS);
|
2012-01-21 06:27:12 +08:00
|
|
|
}
|
2013-08-20 17:11:13 +08:00
|
|
|
if (PhysRegClobbers.test(Reg))
|
|
|
|
// MI defined register is seen defined by another instruction in
|
|
|
|
// the loop, it cannot be a LICM candidate.
|
|
|
|
RuledOut = true;
|
2010-04-08 09:03:47 +08:00
|
|
|
}
|
|
|
|
|
2010-04-14 02:16:00 +08:00
|
|
|
// Only consider reloads for now and remats which do not have register
|
|
|
|
// operands. FIXME: Consider unfold load folding instructions.
|
2010-04-08 09:03:47 +08:00
|
|
|
if (Def && !RuledOut) {
|
2010-04-14 02:16:00 +08:00
|
|
|
int FI = INT_MIN;
|
2010-04-14 04:21:05 +08:00
|
|
|
if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) ||
|
2010-04-14 02:16:00 +08:00
|
|
|
(TII->isLoadFromStackSlot(MI, FI) && MFI->isSpillSlotObjectIndex(FI)))
|
|
|
|
Candidates.push_back(CandidateInfo(MI, Def, FI));
|
2010-04-08 09:03:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HoistRegionPostRA - Walk the specified region of the CFG and hoist loop
|
|
|
|
/// invariants out to the preheader.
|
2010-04-17 15:07:11 +08:00
|
|
|
void MachineLICM::HoistRegionPostRA() {
|
2012-03-27 09:50:58 +08:00
|
|
|
MachineBasicBlock *Preheader = getCurPreheader();
|
|
|
|
if (!Preheader)
|
|
|
|
return;
|
|
|
|
|
2010-04-07 08:41:17 +08:00
|
|
|
unsigned NumRegs = TRI->getNumRegs();
|
2012-01-21 06:27:12 +08:00
|
|
|
BitVector PhysRegDefs(NumRegs); // Regs defined once in the loop.
|
|
|
|
BitVector PhysRegClobbers(NumRegs); // Regs defined more than once.
|
2010-04-07 08:41:17 +08:00
|
|
|
|
2010-04-08 09:03:47 +08:00
|
|
|
SmallVector<CandidateInfo, 32> Candidates;
|
2010-04-07 08:41:17 +08:00
|
|
|
SmallSet<int, 32> StoredFIs;
|
|
|
|
|
|
|
|
// Walk the entire region, count number of defs for each register, and
|
2010-04-17 15:07:11 +08:00
|
|
|
// collect potential LICM candidates.
|
2013-09-16 06:04:42 +08:00
|
|
|
const std::vector<MachineBasicBlock *> &Blocks = CurLoop->getBlocks();
|
2010-04-17 15:07:11 +08:00
|
|
|
for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
|
|
|
|
MachineBasicBlock *BB = Blocks[i];
|
2011-10-12 10:58:01 +08:00
|
|
|
|
|
|
|
// If the header of the loop containing this basic block is a landing pad,
|
|
|
|
// then don't try to hoist instructions out of this loop.
|
|
|
|
const MachineLoop *ML = MLI->getLoopFor(BB);
|
2015-08-28 07:27:47 +08:00
|
|
|
if (ML && ML->getHeader()->isEHPad()) continue;
|
2011-10-12 10:58:01 +08:00
|
|
|
|
2010-04-07 08:41:17 +08:00
|
|
|
// Conservatively treat live-in's as an external def.
|
2010-04-08 09:03:47 +08:00
|
|
|
// FIXME: That means a reload that're reused in successor block(s) will not
|
|
|
|
// be LICM'ed.
|
2015-09-10 02:08:03 +08:00
|
|
|
for (const auto &LI : BB->liveins()) {
|
|
|
|
for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI)
|
2012-06-02 07:28:30 +08:00
|
|
|
PhysRegDefs.set(*AI);
|
2010-04-07 08:41:17 +08:00
|
|
|
}
|
|
|
|
|
2011-10-12 07:48:44 +08:00
|
|
|
SpeculationState = SpeculateUnknown;
|
2010-04-07 08:41:17 +08:00
|
|
|
for (MachineBasicBlock::iterator
|
|
|
|
MII = BB->begin(), E = BB->end(); MII != E; ++MII) {
|
|
|
|
MachineInstr *MI = &*MII;
|
2012-01-21 06:27:12 +08:00
|
|
|
ProcessMI(MI, PhysRegDefs, PhysRegClobbers, StoredFIs, Candidates);
|
2010-04-07 08:41:17 +08:00
|
|
|
}
|
2010-04-17 15:07:11 +08:00
|
|
|
}
|
2010-04-07 08:41:17 +08:00
|
|
|
|
2012-03-27 09:50:58 +08:00
|
|
|
// Gather the registers read / clobbered by the terminator.
|
|
|
|
BitVector TermRegs(NumRegs);
|
|
|
|
MachineBasicBlock::iterator TI = Preheader->getFirstTerminator();
|
|
|
|
if (TI != Preheader->end()) {
|
|
|
|
for (unsigned i = 0, e = TI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = TI->getOperand(i);
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (!Reg)
|
|
|
|
continue;
|
2012-06-02 07:28:30 +08:00
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
TermRegs.set(*AI);
|
2012-03-27 09:50:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-07 08:41:17 +08:00
|
|
|
// Now evaluate whether the potential candidates qualify.
|
|
|
|
// 1. Check if the candidate defined register is defined by another
|
|
|
|
// instruction in the loop.
|
|
|
|
// 2. If the candidate is a load from stack slot (always true for now),
|
|
|
|
// check if the slot is stored anywhere in the loop.
|
2012-03-27 09:50:58 +08:00
|
|
|
// 3. Make sure candidate def should not clobber
|
|
|
|
// registers read by the terminator. Similarly its def should not be
|
|
|
|
// clobbered by the terminator.
|
2010-04-07 08:41:17 +08:00
|
|
|
for (unsigned i = 0, e = Candidates.size(); i != e; ++i) {
|
2010-04-14 02:16:00 +08:00
|
|
|
if (Candidates[i].FI != INT_MIN &&
|
|
|
|
StoredFIs.count(Candidates[i].FI))
|
2010-04-07 08:41:17 +08:00
|
|
|
continue;
|
|
|
|
|
2012-03-27 09:50:58 +08:00
|
|
|
unsigned Def = Candidates[i].Def;
|
|
|
|
if (!PhysRegClobbers.test(Def) && !TermRegs.test(Def)) {
|
2010-04-14 04:21:05 +08:00
|
|
|
bool Safe = true;
|
|
|
|
MachineInstr *MI = Candidates[i].MI;
|
2010-04-14 04:25:29 +08:00
|
|
|
for (unsigned j = 0, ee = MI->getNumOperands(); j != ee; ++j) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(j);
|
2010-04-14 06:13:34 +08:00
|
|
|
if (!MO.isReg() || MO.isDef() || !MO.getReg())
|
2010-04-14 04:21:05 +08:00
|
|
|
continue;
|
2012-03-27 09:50:58 +08:00
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (PhysRegDefs.test(Reg) ||
|
|
|
|
PhysRegClobbers.test(Reg)) {
|
2010-04-14 04:21:05 +08:00
|
|
|
// If it's using a non-loop-invariant register, then it's obviously
|
|
|
|
// not safe to hoist.
|
|
|
|
Safe = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Safe)
|
|
|
|
HoistPostRA(MI, Candidates[i].Def);
|
|
|
|
}
|
2010-04-07 08:41:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-21 02:45:47 +08:00
|
|
|
/// AddToLiveIns - Add register 'Reg' to the livein sets of BBs in the current
|
|
|
|
/// loop, and make sure it is not killed by any instructions in the loop.
|
2010-04-17 15:07:11 +08:00
|
|
|
void MachineLICM::AddToLiveIns(unsigned Reg) {
|
2013-09-16 06:04:42 +08:00
|
|
|
const std::vector<MachineBasicBlock *> &Blocks = CurLoop->getBlocks();
|
2010-04-21 02:45:47 +08:00
|
|
|
for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
|
|
|
|
MachineBasicBlock *BB = Blocks[i];
|
|
|
|
if (!BB->isLiveIn(Reg))
|
|
|
|
BB->addLiveIn(Reg);
|
|
|
|
for (MachineBasicBlock::iterator
|
|
|
|
MII = BB->begin(), E = BB->end(); MII != E; ++MII) {
|
|
|
|
MachineInstr *MI = &*MII;
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue;
|
|
|
|
if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg()))
|
|
|
|
MO.setIsKill(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-08 09:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// HoistPostRA - When an instruction is found to only use loop invariant
|
|
|
|
/// operands that is safe to hoist, this instruction is called to do the
|
|
|
|
/// dirty work.
|
|
|
|
void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) {
|
2010-06-23 01:25:57 +08:00
|
|
|
MachineBasicBlock *Preheader = getCurPreheader();
|
|
|
|
|
2010-04-07 08:41:17 +08:00
|
|
|
// Now move the instructions to the predecessor, inserting it before any
|
|
|
|
// terminator instructions.
|
2012-01-24 05:01:11 +08:00
|
|
|
DEBUG(dbgs() << "Hoisting to BB#" << Preheader->getNumber() << " from BB#"
|
|
|
|
<< MI->getParent()->getNumber() << ": " << *MI);
|
2010-04-07 08:41:17 +08:00
|
|
|
|
|
|
|
// Splice the instruction to the preheader.
|
2010-04-08 09:03:47 +08:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2010-06-23 01:25:57 +08:00
|
|
|
Preheader->splice(Preheader->getFirstTerminator(), MBB, MI);
|
2010-04-08 09:03:47 +08:00
|
|
|
|
2012-02-09 05:23:00 +08:00
|
|
|
// Add register to livein list to all the BBs in the current loop since a
|
2010-04-17 15:07:11 +08:00
|
|
|
// loop invariant must be kept live throughout the whole loop. This is
|
|
|
|
// important to ensure later passes do not scavenge the def register.
|
|
|
|
AddToLiveIns(Def);
|
2010-04-07 08:41:17 +08:00
|
|
|
|
|
|
|
++NumPostRAHoisted;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2011-10-12 02:09:58 +08:00
|
|
|
// IsGuaranteedToExecute - Check if this mbb is guaranteed to execute.
|
|
|
|
// If not then a load from this mbb may not be safe to hoist.
|
|
|
|
bool MachineLICM::IsGuaranteedToExecute(MachineBasicBlock *BB) {
|
2011-10-12 07:48:44 +08:00
|
|
|
if (SpeculationState != SpeculateUnknown)
|
|
|
|
return SpeculationState == SpeculateFalse;
|
2012-02-09 05:23:00 +08:00
|
|
|
|
2011-10-12 02:09:58 +08:00
|
|
|
if (BB != CurLoop->getHeader()) {
|
|
|
|
// Check loop exiting blocks.
|
|
|
|
SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks;
|
|
|
|
CurLoop->getExitingBlocks(CurrentLoopExitingBlocks);
|
|
|
|
for (unsigned i = 0, e = CurrentLoopExitingBlocks.size(); i != e; ++i)
|
|
|
|
if (!DT->dominates(BB, CurrentLoopExitingBlocks[i])) {
|
2011-10-13 09:09:50 +08:00
|
|
|
SpeculationState = SpeculateTrue;
|
|
|
|
return false;
|
2011-10-12 02:09:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-12 07:48:44 +08:00
|
|
|
SpeculationState = SpeculateFalse;
|
|
|
|
return true;
|
2011-10-12 02:09:58 +08:00
|
|
|
}
|
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
void MachineLICM::EnterScope(MachineBasicBlock *MBB) {
|
|
|
|
DEBUG(dbgs() << "Entering: " << MBB->getName() << '\n');
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
// Remember livein register pressure.
|
|
|
|
BackTrace.push_back(RegPressure);
|
|
|
|
}
|
2011-10-12 10:58:01 +08:00
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
void MachineLICM::ExitScope(MachineBasicBlock *MBB) {
|
|
|
|
DEBUG(dbgs() << "Exiting: " << MBB->getName() << '\n');
|
|
|
|
BackTrace.pop_back();
|
|
|
|
}
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
/// ExitScopeIfDone - Destroy scope for the MBB that corresponds to the given
|
|
|
|
/// dominator tree node if its a leaf or all of its children are done. Walk
|
|
|
|
/// up the dominator tree to destroy ancestors which are now done.
|
|
|
|
void MachineLICM::ExitScopeIfDone(MachineDomTreeNode *Node,
|
2012-01-11 06:27:32 +08:00
|
|
|
DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren,
|
|
|
|
DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap) {
|
2011-12-22 10:05:40 +08:00
|
|
|
if (OpenChildren[Node])
|
2010-10-16 10:20:26 +08:00
|
|
|
return;
|
2010-10-14 09:16:09 +08:00
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
// Pop scope.
|
|
|
|
ExitScope(Node->getBlock());
|
|
|
|
|
|
|
|
// Now traverse upwards to pop ancestors whose offsprings are all done.
|
|
|
|
while (MachineDomTreeNode *Parent = ParentMap[Node]) {
|
|
|
|
unsigned Left = --OpenChildren[Parent];
|
|
|
|
if (Left != 0)
|
|
|
|
break;
|
|
|
|
ExitScope(Parent->getBlock());
|
|
|
|
Node = Parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// HoistOutOfLoop - Walk the specified loop in the CFG (defined by all
|
|
|
|
/// blocks dominated by the specified header block, and that are in the
|
|
|
|
/// current loop) in depth first order w.r.t the DominatorTree. This allows
|
|
|
|
/// us to visit definitions before uses, allowing us to hoist a loop body in
|
|
|
|
/// one pass without iteration.
|
|
|
|
///
|
|
|
|
void MachineLICM::HoistOutOfLoop(MachineDomTreeNode *HeaderN) {
|
2015-02-06 06:39:46 +08:00
|
|
|
MachineBasicBlock *Preheader = getCurPreheader();
|
|
|
|
if (!Preheader)
|
|
|
|
return;
|
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
SmallVector<MachineDomTreeNode*, 32> Scopes;
|
|
|
|
SmallVector<MachineDomTreeNode*, 8> WorkList;
|
|
|
|
DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> ParentMap;
|
|
|
|
DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
|
|
|
|
|
|
|
|
// Perform a DFS walk to determine the order of visit.
|
|
|
|
WorkList.push_back(HeaderN);
|
2015-02-06 06:39:46 +08:00
|
|
|
while (!WorkList.empty()) {
|
2011-12-22 10:05:40 +08:00
|
|
|
MachineDomTreeNode *Node = WorkList.pop_back_val();
|
2014-04-14 08:51:57 +08:00
|
|
|
assert(Node && "Null dominator tree node?");
|
2011-12-22 10:05:40 +08:00
|
|
|
MachineBasicBlock *BB = Node->getBlock();
|
|
|
|
|
|
|
|
// If the header of the loop containing this basic block is a landing pad,
|
|
|
|
// then don't try to hoist instructions out of this loop.
|
|
|
|
const MachineLoop *ML = MLI->getLoopFor(BB);
|
2015-08-28 07:27:47 +08:00
|
|
|
if (ML && ML->getHeader()->isEHPad())
|
2011-12-22 10:05:40 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// If this subregion is not in the top level loop at all, exit.
|
|
|
|
if (!CurLoop->contains(BB))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Scopes.push_back(Node);
|
|
|
|
const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
|
|
|
|
unsigned NumChildren = Children.size();
|
|
|
|
|
|
|
|
// Don't hoist things out of a large switch statement. This often causes
|
|
|
|
// code to be hoisted that wasn't going to be executed, and increases
|
|
|
|
// register pressure in a situation where it's likely to matter.
|
|
|
|
if (BB->succ_size() >= 25)
|
|
|
|
NumChildren = 0;
|
|
|
|
|
|
|
|
OpenChildren[Node] = NumChildren;
|
|
|
|
// Add children in reverse order as then the next popped worklist node is
|
|
|
|
// the first child of this node. This means we ultimately traverse the
|
|
|
|
// DOM tree in exactly the same order as if we'd recursed.
|
|
|
|
for (int i = (int)NumChildren-1; i >= 0; --i) {
|
|
|
|
MachineDomTreeNode *Child = Children[i];
|
|
|
|
ParentMap[Child] = Node;
|
|
|
|
WorkList.push_back(Child);
|
|
|
|
}
|
2015-02-06 06:39:46 +08:00
|
|
|
}
|
2011-12-22 10:05:40 +08:00
|
|
|
|
2015-02-06 06:39:46 +08:00
|
|
|
if (Scopes.size() == 0)
|
|
|
|
return;
|
2011-12-22 10:05:40 +08:00
|
|
|
|
2015-02-06 06:39:46 +08:00
|
|
|
// Compute registers which are livein into the loop headers.
|
|
|
|
RegSeen.clear();
|
|
|
|
BackTrace.clear();
|
|
|
|
InitRegPressure(Preheader);
|
2010-10-19 08:55:07 +08:00
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
// Now perform LICM.
|
|
|
|
for (unsigned i = 0, e = Scopes.size(); i != e; ++i) {
|
|
|
|
MachineDomTreeNode *Node = Scopes[i];
|
|
|
|
MachineBasicBlock *MBB = Node->getBlock();
|
2010-10-20 02:58:51 +08:00
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
EnterScope(MBB);
|
2010-10-16 10:20:26 +08:00
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
// Process the block
|
|
|
|
SpeculationState = SpeculateUnknown;
|
|
|
|
for (MachineBasicBlock::iterator
|
|
|
|
MII = MBB->begin(), E = MBB->end(); MII != E; ) {
|
|
|
|
MachineBasicBlock::iterator NextMII = MII; ++NextMII;
|
|
|
|
MachineInstr *MI = &*MII;
|
|
|
|
if (!Hoist(MI, Preheader))
|
|
|
|
UpdateRegPressure(MI);
|
|
|
|
MII = NextMII;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a leaf node, it's done. Traverse upwards to pop ancestors.
|
|
|
|
ExitScopeIfDone(Node, OpenChildren, ParentMap);
|
|
|
|
}
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
}
|
|
|
|
|
2015-03-14 18:58:38 +08:00
|
|
|
void MachineLICM::SinkIntoLoop() {
|
|
|
|
MachineBasicBlock *Preheader = getCurPreheader();
|
|
|
|
if (!Preheader)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SmallVector<MachineInstr *, 8> Candidates;
|
|
|
|
for (MachineBasicBlock::instr_iterator I = Preheader->instr_begin();
|
|
|
|
I != Preheader->instr_end(); ++I) {
|
|
|
|
// We need to ensure that we can safely move this instruction into the loop.
|
|
|
|
// As such, it must not have side-effects, e.g. such as a call has.
|
2015-10-10 03:40:45 +08:00
|
|
|
if (IsLoopInvariantInst(*I) && !HasLoopPHIUse(&*I))
|
|
|
|
Candidates.push_back(&*I);
|
2015-03-14 18:58:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (MachineInstr *I : Candidates) {
|
|
|
|
const MachineOperand &MO = I->getOperand(0);
|
|
|
|
if (!MO.isDef() || !MO.isReg() || !MO.getReg())
|
|
|
|
continue;
|
|
|
|
if (!MRI->hasOneDef(MO.getReg()))
|
|
|
|
continue;
|
|
|
|
bool CanSink = true;
|
|
|
|
MachineBasicBlock *B = nullptr;
|
|
|
|
for (MachineInstr &MI : MRI->use_instructions(MO.getReg())) {
|
|
|
|
// FIXME: Come up with a proper cost model that estimates whether sinking
|
|
|
|
// the instruction (and thus possibly executing it on every loop
|
|
|
|
// iteration) is more expensive than a register.
|
|
|
|
// For now assumes that copies are cheap and thus almost always worth it.
|
|
|
|
if (!MI.isCopy()) {
|
|
|
|
CanSink = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!B) {
|
|
|
|
B = MI.getParent();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
B = DT->findNearestCommonDominator(B, MI.getParent());
|
|
|
|
if (!B) {
|
|
|
|
CanSink = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!CanSink || !B || B == Preheader)
|
|
|
|
continue;
|
|
|
|
B->splice(B->getFirstNonPHI(), Preheader, I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-21 06:03:58 +08:00
|
|
|
static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) {
|
|
|
|
return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg());
|
|
|
|
}
|
|
|
|
|
2010-10-16 10:20:26 +08:00
|
|
|
/// InitRegPressure - Find all virtual register references that are liveout of
|
|
|
|
/// the preheader to initialize the starting "register pressure". Note this
|
|
|
|
/// does not count live through (livein but not used) registers.
|
2010-10-14 09:16:09 +08:00
|
|
|
void MachineLICM::InitRegPressure(MachineBasicBlock *BB) {
|
|
|
|
std::fill(RegPressure.begin(), RegPressure.end(), 0);
|
2010-10-16 10:20:26 +08:00
|
|
|
|
2010-10-21 06:03:58 +08:00
|
|
|
// If the preheader has only a single predecessor and it ends with a
|
|
|
|
// fallthrough or an unconditional branch, then scan its predecessor for live
|
|
|
|
// defs as well. This happens whenever the preheader is created by splitting
|
|
|
|
// the critical edge from the loop predecessor to the loop header.
|
|
|
|
if (BB->pred_size() == 1) {
|
2014-04-14 08:51:57 +08:00
|
|
|
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
|
2010-10-21 06:03:58 +08:00
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
|
|
|
if (!TII->AnalyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty())
|
|
|
|
InitRegPressure(*BB->pred_begin());
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:42:35 +08:00
|
|
|
for (const MachineInstr &MI : *BB)
|
|
|
|
UpdateRegPressure(&MI, /*ConsiderUnseenAsDef=*/true);
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
|
|
|
|
2010-10-21 06:03:58 +08:00
|
|
|
/// UpdateRegPressure - Update estimate of register pressure after the
|
|
|
|
/// specified instruction.
|
2015-04-08 00:42:35 +08:00
|
|
|
void MachineLICM::UpdateRegPressure(const MachineInstr *MI,
|
|
|
|
bool ConsiderUnseenAsDef) {
|
|
|
|
auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/true, ConsiderUnseenAsDef);
|
2015-04-14 19:56:25 +08:00
|
|
|
for (const auto &RPIdAndCost : Cost) {
|
|
|
|
unsigned Class = RPIdAndCost.first;
|
|
|
|
if (static_cast<int>(RegPressure[Class]) < -RPIdAndCost.second)
|
2015-04-08 00:42:35 +08:00
|
|
|
RegPressure[Class] = 0;
|
|
|
|
else
|
2015-04-14 19:56:25 +08:00
|
|
|
RegPressure[Class] += RPIdAndCost.second;
|
2015-04-08 00:42:35 +08:00
|
|
|
}
|
|
|
|
}
|
2010-10-14 09:16:09 +08:00
|
|
|
|
2015-04-08 00:42:35 +08:00
|
|
|
DenseMap<unsigned, int>
|
|
|
|
MachineLICM::calcRegisterCost(const MachineInstr *MI, bool ConsiderSeen,
|
|
|
|
bool ConsiderUnseenAsDef) {
|
|
|
|
DenseMap<unsigned, int> Cost;
|
|
|
|
if (MI->isImplicitDef())
|
|
|
|
return Cost;
|
2010-10-14 09:16:09 +08:00
|
|
|
for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2010-10-20 02:58:51 +08:00
|
|
|
if (!MO.isReg() || MO.isImplicit())
|
2010-10-14 09:16:09 +08:00
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
2011-01-10 10:58:51 +08:00
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg))
|
2010-10-14 09:16:09 +08:00
|
|
|
continue;
|
|
|
|
|
2015-04-08 00:42:35 +08:00
|
|
|
// FIXME: It seems bad to use RegSeen only for some of these calculations.
|
|
|
|
bool isNew = ConsiderSeen ? RegSeen.insert(Reg).second : false;
|
2015-04-14 19:56:25 +08:00
|
|
|
const TargetRegisterClass *RC = MRI->getRegClass(Reg);
|
|
|
|
|
|
|
|
RegClassWeight W = TRI->getRegClassWeight(RC);
|
|
|
|
int RCCost = 0;
|
2010-10-20 02:58:51 +08:00
|
|
|
if (MO.isDef())
|
2015-04-14 19:56:25 +08:00
|
|
|
RCCost = W.RegWeight;
|
2015-04-08 00:42:35 +08:00
|
|
|
else {
|
|
|
|
bool isKill = isOperandKill(MO, MRI);
|
|
|
|
if (isNew && !isKill && ConsiderUnseenAsDef)
|
|
|
|
// Haven't seen this, it must be a livein.
|
2015-04-14 19:56:25 +08:00
|
|
|
RCCost = W.RegWeight;
|
2015-04-08 00:42:35 +08:00
|
|
|
else if (!isNew && isKill)
|
2015-04-14 19:56:25 +08:00
|
|
|
RCCost = -W.RegWeight;
|
|
|
|
}
|
|
|
|
if (RCCost == 0)
|
|
|
|
continue;
|
|
|
|
const int *PS = TRI->getRegClassPressureSets(RC);
|
|
|
|
for (; *PS != -1; ++PS) {
|
|
|
|
if (Cost.find(*PS) == Cost.end())
|
|
|
|
Cost[*PS] = RCCost;
|
|
|
|
else
|
|
|
|
Cost[*PS] += RCCost;
|
2010-10-16 10:20:26 +08:00
|
|
|
}
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
2015-04-08 00:42:35 +08:00
|
|
|
return Cost;
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
|
|
|
|
2012-02-09 05:23:00 +08:00
|
|
|
/// isLoadFromGOTOrConstantPool - Return true if this machine instruction
|
2011-10-21 01:42:23 +08:00
|
|
|
/// loads from global offset table or constant pool.
|
|
|
|
static bool isLoadFromGOTOrConstantPool(MachineInstr &MI) {
|
2011-12-07 15:15:52 +08:00
|
|
|
assert (MI.mayLoad() && "Expected MI that loads!");
|
2011-10-18 01:35:01 +08:00
|
|
|
for (MachineInstr::mmo_iterator I = MI.memoperands_begin(),
|
2012-02-09 05:23:00 +08:00
|
|
|
E = MI.memoperands_end(); I != E; ++I) {
|
2014-04-15 15:22:52 +08:00
|
|
|
if (const PseudoSourceValue *PSV = (*I)->getPseudoValue()) {
|
2015-08-12 07:09:45 +08:00
|
|
|
if (PSV->isGOT() || PSV->isConstantPool())
|
2014-04-15 15:22:52 +08:00
|
|
|
return true;
|
2011-10-18 01:35:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-04-14 02:16:00 +08:00
|
|
|
/// IsLICMCandidate - Returns true if the instruction may be a suitable
|
|
|
|
/// candidate for LICM. e.g. If the instruction is a call, then it's obviously
|
|
|
|
/// not safe to hoist it.
|
|
|
|
bool MachineLICM::IsLICMCandidate(MachineInstr &I) {
|
2010-07-12 08:00:35 +08:00
|
|
|
// Check if it's safe to move the instruction.
|
|
|
|
bool DontMoveAcrossStore = true;
|
2015-05-20 05:22:20 +08:00
|
|
|
if (!I.isSafeToMove(AA, DontMoveAcrossStore))
|
2008-01-11 07:08:24 +08:00
|
|
|
return false;
|
2011-10-12 02:09:58 +08:00
|
|
|
|
|
|
|
// If it is load then check if it is guaranteed to execute by making sure that
|
|
|
|
// it dominates all exiting blocks. If it doesn't, then there is a path out of
|
2011-10-21 01:31:18 +08:00
|
|
|
// the loop which does not execute this load, so we can't hoist it. Loads
|
|
|
|
// from constant memory are not safe to speculate all the time, for example
|
|
|
|
// indexed load from a jump table.
|
2011-10-12 02:09:58 +08:00
|
|
|
// Stores and side effects are already checked by isSafeToMove.
|
2012-02-09 05:23:00 +08:00
|
|
|
if (I.mayLoad() && !isLoadFromGOTOrConstantPool(I) &&
|
2011-10-18 01:35:01 +08:00
|
|
|
!IsGuaranteedToExecute(I.getParent()))
|
2011-10-12 02:09:58 +08:00
|
|
|
return false;
|
|
|
|
|
2010-04-14 02:16:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// IsLoopInvariantInst - Returns true if the instruction is loop
|
|
|
|
/// invariant. I.e., all virtual register operands are defined outside of the
|
|
|
|
/// loop, physical registers aren't accessed explicitly, and there are no side
|
|
|
|
/// effects that aren't captured by the operands or other flags.
|
2012-02-09 05:23:00 +08:00
|
|
|
///
|
2010-04-14 02:16:00 +08:00
|
|
|
bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
|
|
|
|
if (!IsLICMCandidate(I))
|
|
|
|
return false;
|
2008-03-10 16:13:01 +08:00
|
|
|
|
2008-05-13 03:38:32 +08:00
|
|
|
// The instruction is loop invariant if all of its operands are.
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = I.getOperand(i);
|
|
|
|
|
2008-10-03 23:45:36 +08:00
|
|
|
if (!MO.isReg())
|
2008-08-21 04:32:05 +08:00
|
|
|
continue;
|
|
|
|
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
unsigned Reg = MO.getReg();
|
2008-03-10 16:13:01 +08:00
|
|
|
if (Reg == 0) continue;
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2009-01-16 06:01:38 +08:00
|
|
|
// Don't hoist an instruction that uses or defines a physical register.
|
2009-09-26 07:58:45 +08:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
|
|
|
if (MO.isUse()) {
|
|
|
|
// If the physreg has no defs anywhere, it's just an ambient register
|
2009-09-26 10:34:00 +08:00
|
|
|
// and we can freely move its uses. Alternatively, if it's allocatable,
|
|
|
|
// it could get allocated to something with a def during allocation.
|
2012-01-17 06:34:08 +08:00
|
|
|
if (!MRI->isConstantPhysReg(Reg, *I.getParent()->getParent()))
|
2009-09-26 10:34:00 +08:00
|
|
|
return false;
|
2009-09-26 07:58:45 +08:00
|
|
|
// Otherwise it's safe to move.
|
|
|
|
continue;
|
|
|
|
} else if (!MO.isDead()) {
|
|
|
|
// A def that isn't dead. We can't move it.
|
|
|
|
return false;
|
2010-02-28 08:08:44 +08:00
|
|
|
} else if (CurLoop->getHeader()->isLiveIn(Reg)) {
|
|
|
|
// If the reg is live into the loop, we can't hoist an instruction
|
|
|
|
// which would clobber it.
|
|
|
|
return false;
|
2009-09-26 07:58:45 +08:00
|
|
|
}
|
|
|
|
}
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2009-01-16 06:01:38 +08:00
|
|
|
if (!MO.isUse())
|
|
|
|
continue;
|
|
|
|
|
2010-10-14 09:16:09 +08:00
|
|
|
assert(MRI->getVRegDef(Reg) &&
|
2008-05-13 03:38:32 +08:00
|
|
|
"Machine instr not mapped for this vreg?!");
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
|
|
|
// If the loop contains the definition of an operand, then the instruction
|
|
|
|
// isn't loop invariant.
|
2010-10-14 09:16:09 +08:00
|
|
|
if (CurLoop->contains(MRI->getVRegDef(Reg)))
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, the instruction is loop invariant!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-05 16:45:46 +08:00
|
|
|
|
2012-04-11 08:00:26 +08:00
|
|
|
/// HasLoopPHIUse - Return true if the specified instruction is used by a
|
|
|
|
/// phi node and hoisting it could cause a copy to be inserted.
|
|
|
|
bool MachineLICM::HasLoopPHIUse(const MachineInstr *MI) const {
|
|
|
|
SmallVector<const MachineInstr*, 8> Work(1, MI);
|
|
|
|
do {
|
|
|
|
MI = Work.pop_back_val();
|
2015-05-29 10:56:46 +08:00
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
|
|
|
if (!MO.isReg() || !MO.isDef())
|
2012-04-11 08:00:26 +08:00
|
|
|
continue;
|
2015-05-29 10:56:46 +08:00
|
|
|
unsigned Reg = MO.getReg();
|
2012-04-11 08:00:26 +08:00
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
continue;
|
2014-03-18 03:36:09 +08:00
|
|
|
for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
|
2012-04-11 08:00:26 +08:00
|
|
|
// A PHI may cause a copy to be inserted.
|
2014-03-18 03:36:09 +08:00
|
|
|
if (UseMI.isPHI()) {
|
2012-04-11 08:00:26 +08:00
|
|
|
// A PHI inside the loop causes a copy because the live range of Reg is
|
|
|
|
// extended across the PHI.
|
2014-03-18 03:36:09 +08:00
|
|
|
if (CurLoop->contains(&UseMI))
|
2012-04-11 08:00:26 +08:00
|
|
|
return true;
|
|
|
|
// A PHI in an exit block can cause a copy to be inserted if the PHI
|
|
|
|
// has multiple predecessors in the loop with different values.
|
|
|
|
// For now, approximate by rejecting all exit blocks.
|
2014-03-18 03:36:09 +08:00
|
|
|
if (isExitBlock(UseMI.getParent()))
|
2012-04-11 08:00:26 +08:00
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Look past copies as well.
|
2014-03-18 03:36:09 +08:00
|
|
|
if (UseMI.isCopy() && CurLoop->contains(&UseMI))
|
|
|
|
Work.push_back(&UseMI);
|
2012-04-11 08:00:26 +08:00
|
|
|
}
|
2011-04-12 05:09:18 +08:00
|
|
|
}
|
2012-04-11 08:00:26 +08:00
|
|
|
} while (!Work.empty());
|
2009-02-05 16:45:46 +08:00
|
|
|
return false;
|
2009-02-04 17:19:56 +08:00
|
|
|
}
|
|
|
|
|
2010-10-20 02:58:51 +08:00
|
|
|
/// HasHighOperandLatency - Compute operand latency between a def of 'Reg'
|
|
|
|
/// and an use in the current loop, return true if the target considered
|
|
|
|
/// it 'high'.
|
|
|
|
bool MachineLICM::HasHighOperandLatency(MachineInstr &MI,
|
2010-10-26 10:08:50 +08:00
|
|
|
unsigned DefIdx, unsigned Reg) const {
|
2015-06-13 11:42:11 +08:00
|
|
|
if (MRI->use_nodbg_empty(Reg))
|
2010-10-20 02:58:51 +08:00
|
|
|
return false;
|
2010-10-14 09:16:09 +08:00
|
|
|
|
2014-03-18 03:36:09 +08:00
|
|
|
for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) {
|
|
|
|
if (UseMI.isCopyLike())
|
2010-10-26 10:08:50 +08:00
|
|
|
continue;
|
2014-03-18 03:36:09 +08:00
|
|
|
if (!CurLoop->contains(UseMI.getParent()))
|
2010-10-14 09:16:09 +08:00
|
|
|
continue;
|
2014-03-18 03:36:09 +08:00
|
|
|
for (unsigned i = 0, e = UseMI.getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = UseMI.getOperand(i);
|
2010-10-14 09:16:09 +08:00
|
|
|
if (!MO.isReg() || !MO.isUse())
|
|
|
|
continue;
|
|
|
|
unsigned MOReg = MO.getReg();
|
|
|
|
if (MOReg != Reg)
|
|
|
|
continue;
|
|
|
|
|
2015-06-13 11:42:11 +08:00
|
|
|
if (TII->hasHighOperandLatency(SchedModel, MRI, &MI, DefIdx, &UseMI, i))
|
2010-10-20 02:58:51 +08:00
|
|
|
return true;
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
|
|
|
|
2010-10-20 02:58:51 +08:00
|
|
|
// Only look at the first in loop use.
|
|
|
|
break;
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
|
|
|
|
2010-10-20 02:58:51 +08:00
|
|
|
return false;
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
|
|
|
|
2010-10-26 10:08:50 +08:00
|
|
|
/// IsCheapInstruction - Return true if the instruction is marked "cheap" or
|
|
|
|
/// the operand latency between its def and a use is one or less.
|
|
|
|
bool MachineLICM::IsCheapInstruction(MachineInstr &MI) const {
|
2014-07-29 09:55:19 +08:00
|
|
|
if (TII->isAsCheapAsAMove(&MI) || MI.isCopyLike())
|
2010-10-26 10:08:50 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
bool isCheap = false;
|
|
|
|
unsigned NumDefs = MI.getDesc().getNumDefs();
|
|
|
|
for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) {
|
|
|
|
MachineOperand &DefMO = MI.getOperand(i);
|
|
|
|
if (!DefMO.isReg() || !DefMO.isDef())
|
|
|
|
continue;
|
|
|
|
--NumDefs;
|
|
|
|
unsigned Reg = DefMO.getReg();
|
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg))
|
|
|
|
continue;
|
|
|
|
|
2015-06-13 11:42:11 +08:00
|
|
|
if (!TII->hasLowDefLatency(SchedModel, &MI, i))
|
2010-10-26 10:08:50 +08:00
|
|
|
return false;
|
|
|
|
isCheap = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isCheap;
|
|
|
|
}
|
|
|
|
|
2010-10-21 06:03:58 +08:00
|
|
|
/// CanCauseHighRegPressure - Visit BBs from header to current BB, check
|
2010-10-16 10:20:26 +08:00
|
|
|
/// if hoisting an instruction of the given cost matrix can cause high
|
|
|
|
/// register pressure.
|
2015-04-04 00:19:48 +08:00
|
|
|
bool MachineLICM::CanCauseHighRegPressure(const DenseMap<unsigned, int>& Cost,
|
2012-04-11 08:00:28 +08:00
|
|
|
bool CheapInstr) {
|
2015-04-14 19:56:25 +08:00
|
|
|
for (const auto &RPIdAndCost : Cost) {
|
|
|
|
if (RPIdAndCost.second <= 0)
|
2010-10-21 06:03:58 +08:00
|
|
|
continue;
|
|
|
|
|
2015-04-14 19:56:25 +08:00
|
|
|
unsigned Class = RPIdAndCost.first;
|
2015-04-04 00:19:48 +08:00
|
|
|
int Limit = RegLimit[Class];
|
2012-04-11 08:00:28 +08:00
|
|
|
|
|
|
|
// Don't hoist cheap instructions if they would increase register pressure,
|
|
|
|
// even if we're under the limit.
|
2015-01-09 06:10:48 +08:00
|
|
|
if (CheapInstr && !HoistCheapInsts)
|
2012-04-11 08:00:28 +08:00
|
|
|
return true;
|
|
|
|
|
2015-04-04 00:19:48 +08:00
|
|
|
for (const auto &RP : BackTrace)
|
2015-04-14 19:56:25 +08:00
|
|
|
if (static_cast<int>(RP[Class]) + RPIdAndCost.second >= Limit)
|
2010-10-16 10:20:26 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-10-21 06:03:58 +08:00
|
|
|
/// UpdateBackTraceRegPressure - Traverse the back trace from header to the
|
|
|
|
/// current block and update their register pressures to reflect the effect
|
|
|
|
/// of hoisting MI from the current block to the preheader.
|
|
|
|
void MachineLICM::UpdateBackTraceRegPressure(const MachineInstr *MI) {
|
|
|
|
// First compute the 'cost' of the instruction, i.e. its contribution
|
|
|
|
// to register pressure.
|
2015-04-08 00:42:35 +08:00
|
|
|
auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/false,
|
|
|
|
/*ConsiderUnseenAsDef=*/false);
|
2010-10-21 06:03:58 +08:00
|
|
|
|
|
|
|
// Update register pressure of blocks from loop header to current block.
|
2015-04-08 00:42:35 +08:00
|
|
|
for (auto &RP : BackTrace)
|
2015-04-14 19:56:25 +08:00
|
|
|
for (const auto &RPIdAndCost : Cost)
|
|
|
|
RP[RPIdAndCost.first] += RPIdAndCost.second;
|
2010-10-21 06:03:58 +08:00
|
|
|
}
|
|
|
|
|
2009-02-04 17:19:56 +08:00
|
|
|
/// IsProfitableToHoist - Return true if it is potentially profitable to hoist
|
|
|
|
/// the given loop invariant.
|
2009-11-21 07:31:34 +08:00
|
|
|
bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) {
|
2010-10-14 09:16:09 +08:00
|
|
|
if (MI.isImplicitDef())
|
|
|
|
return true;
|
|
|
|
|
2012-04-11 08:00:28 +08:00
|
|
|
// Besides removing computation from the loop, hoisting an instruction has
|
|
|
|
// these effects:
|
|
|
|
//
|
|
|
|
// - The value defined by the instruction becomes live across the entire
|
|
|
|
// loop. This increases register pressure in the loop.
|
|
|
|
//
|
|
|
|
// - If the value is used by a PHI in the loop, a copy will be required for
|
|
|
|
// lowering the PHI after extending the live range.
|
|
|
|
//
|
|
|
|
// - When hoisting the last use of a value in the loop, that value no longer
|
|
|
|
// needs to be live in the loop. This lowers register pressure in the loop.
|
|
|
|
|
|
|
|
bool CheapInstr = IsCheapInstruction(MI);
|
|
|
|
bool CreatesCopy = HasLoopPHIUse(&MI);
|
|
|
|
|
|
|
|
// Don't hoist a cheap instruction if it would create a copy in the loop.
|
|
|
|
if (CheapInstr && CreatesCopy) {
|
|
|
|
DEBUG(dbgs() << "Won't hoist cheap instr with loop PHI use: " << MI);
|
|
|
|
return false;
|
|
|
|
}
|
2011-09-01 09:45:00 +08:00
|
|
|
|
2012-04-11 08:00:28 +08:00
|
|
|
// Rematerializable instructions should always be hoisted since the register
|
|
|
|
// allocator can just pull them down again when needed.
|
|
|
|
if (TII->isTriviallyReMaterializable(&MI, AA))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// FIXME: If there are long latency loop-invariant instructions inside the
|
|
|
|
// loop at this point, why didn't the optimizer's LICM hoist them?
|
|
|
|
for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI.getOperand(i);
|
|
|
|
if (!MO.isReg() || MO.isImplicit())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
continue;
|
2015-04-08 00:42:35 +08:00
|
|
|
if (MO.isDef() && HasHighOperandLatency(MI, i, Reg)) {
|
|
|
|
DEBUG(dbgs() << "Hoist High Latency: " << MI);
|
|
|
|
++NumHighLatency;
|
|
|
|
return true;
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
2012-04-11 08:00:28 +08:00
|
|
|
}
|
2010-10-14 09:16:09 +08:00
|
|
|
|
2015-04-08 00:42:35 +08:00
|
|
|
// Estimate register pressure to determine whether to LICM the instruction.
|
|
|
|
// In low register pressure situation, we can be more aggressive about
|
|
|
|
// hoisting. Also, favors hoisting long latency instructions even in
|
|
|
|
// moderately high pressure situation.
|
|
|
|
// Cheap instructions will only be hoisted if they don't increase register
|
|
|
|
// pressure at all.
|
|
|
|
auto Cost = calcRegisterCost(&MI, /*ConsiderSeen=*/false,
|
|
|
|
/*ConsiderUnseenAsDef=*/false);
|
|
|
|
|
2012-04-11 08:00:28 +08:00
|
|
|
// Visit BBs from header to current BB, if hoisting this doesn't cause
|
|
|
|
// high register pressure, then it's safe to proceed.
|
|
|
|
if (!CanCauseHighRegPressure(Cost, CheapInstr)) {
|
|
|
|
DEBUG(dbgs() << "Hoist non-reg-pressure: " << MI);
|
|
|
|
++NumLowRP;
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-14 09:16:09 +08:00
|
|
|
|
2012-04-11 08:00:28 +08:00
|
|
|
// Don't risk increasing register pressure if it would create copies.
|
|
|
|
if (CreatesCopy) {
|
|
|
|
DEBUG(dbgs() << "Won't hoist instr with loop PHI use: " << MI);
|
|
|
|
return false;
|
|
|
|
}
|
2011-10-13 05:33:49 +08:00
|
|
|
|
2012-04-11 08:00:28 +08:00
|
|
|
// Do not "speculate" in high register pressure situation. If an
|
|
|
|
// instruction is not guaranteed to be executed in the loop, it's best to be
|
|
|
|
// conservative.
|
|
|
|
if (AvoidSpeculation &&
|
|
|
|
(!IsGuaranteedToExecute(MI.getParent()) && !MayCSE(&MI))) {
|
|
|
|
DEBUG(dbgs() << "Won't speculate: " << MI);
|
|
|
|
return false;
|
2009-11-21 03:55:37 +08:00
|
|
|
}
|
2009-02-04 17:19:56 +08:00
|
|
|
|
2012-04-11 08:00:28 +08:00
|
|
|
// High register pressure situation, only hoist if the instruction is going
|
|
|
|
// to be remat'ed.
|
|
|
|
if (!TII->isTriviallyReMaterializable(&MI, AA) &&
|
|
|
|
!MI.isInvariantLoad(AA)) {
|
|
|
|
DEBUG(dbgs() << "Can't remat / high reg-pressure: " << MI);
|
2012-04-11 08:00:26 +08:00
|
|
|
return false;
|
2012-04-11 08:00:28 +08:00
|
|
|
}
|
2009-02-05 16:45:46 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-10-30 01:47:20 +08:00
|
|
|
MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) {
|
2010-10-09 02:59:19 +08:00
|
|
|
// Don't unfold simple loads.
|
2011-12-07 15:15:52 +08:00
|
|
|
if (MI->canFoldAsLoad())
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2010-10-09 02:59:19 +08:00
|
|
|
|
2009-10-30 01:47:20 +08:00
|
|
|
// If not, we may be able to unfold a load and hoist that.
|
|
|
|
// First test whether the instruction is loading from an amenable
|
|
|
|
// memory location.
|
2011-01-20 16:34:58 +08:00
|
|
|
if (!MI->isInvariantLoad(AA))
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2009-11-21 03:55:37 +08:00
|
|
|
|
2009-10-30 01:47:20 +08:00
|
|
|
// Next determine the register class for a temporary register.
|
2009-10-31 06:18:41 +08:00
|
|
|
unsigned LoadRegIndex;
|
2009-10-30 01:47:20 +08:00
|
|
|
unsigned NewOpc =
|
|
|
|
TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(),
|
|
|
|
/*UnfoldLoad=*/true,
|
2009-10-31 06:18:41 +08:00
|
|
|
/*UnfoldStore=*/false,
|
|
|
|
&LoadRegIndex);
|
2014-04-14 08:51:57 +08:00
|
|
|
if (NewOpc == 0) return nullptr;
|
2011-06-29 03:10:37 +08:00
|
|
|
const MCInstrDesc &MID = TII->get(NewOpc);
|
2014-04-14 08:51:57 +08:00
|
|
|
if (MID.getNumDefs() != 1) return nullptr;
|
2012-05-08 06:10:26 +08:00
|
|
|
MachineFunction &MF = *MI->getParent()->getParent();
|
|
|
|
const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI, MF);
|
2009-10-30 01:47:20 +08:00
|
|
|
// Ok, we're unfolding. Create a temporary register and do the unfold.
|
2010-10-14 09:16:09 +08:00
|
|
|
unsigned Reg = MRI->createVirtualRegister(RC);
|
2009-11-21 03:55:37 +08:00
|
|
|
|
2009-10-30 01:47:20 +08:00
|
|
|
SmallVector<MachineInstr *, 2> NewMIs;
|
|
|
|
bool Success =
|
|
|
|
TII->unfoldMemoryOperand(MF, MI, Reg,
|
|
|
|
/*UnfoldLoad=*/true, /*UnfoldStore=*/false,
|
|
|
|
NewMIs);
|
|
|
|
(void)Success;
|
|
|
|
assert(Success &&
|
|
|
|
"unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold "
|
|
|
|
"succeeded!");
|
|
|
|
assert(NewMIs.size() == 2 &&
|
|
|
|
"Unfolded a load into multiple instructions!");
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2011-12-07 06:12:01 +08:00
|
|
|
MachineBasicBlock::iterator Pos = MI;
|
|
|
|
MBB->insert(Pos, NewMIs[0]);
|
|
|
|
MBB->insert(Pos, NewMIs[1]);
|
2009-10-30 01:47:20 +08:00
|
|
|
// If unfolding produced a load that wasn't loop-invariant or profitable to
|
|
|
|
// hoist, discard the new instructions and bail.
|
2009-11-21 07:31:34 +08:00
|
|
|
if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) {
|
2009-10-30 01:47:20 +08:00
|
|
|
NewMIs[0]->eraseFromParent();
|
|
|
|
NewMIs[1]->eraseFromParent();
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2009-10-30 01:47:20 +08:00
|
|
|
}
|
2010-10-21 06:03:58 +08:00
|
|
|
|
|
|
|
// Update register pressure for the unfolded instruction.
|
|
|
|
UpdateRegPressure(NewMIs[1]);
|
|
|
|
|
2009-10-30 01:47:20 +08:00
|
|
|
// Otherwise we successfully unfolded a load that we can hoist.
|
|
|
|
MI->eraseFromParent();
|
|
|
|
return NewMIs[0];
|
|
|
|
}
|
|
|
|
|
2009-11-04 05:40:02 +08:00
|
|
|
void MachineLICM::InitCSEMap(MachineBasicBlock *BB) {
|
|
|
|
for (MachineBasicBlock::iterator I = BB->begin(),E = BB->end(); I != E; ++I) {
|
|
|
|
const MachineInstr *MI = &*I;
|
2011-01-20 16:34:58 +08:00
|
|
|
unsigned Opcode = MI->getOpcode();
|
2014-10-04 02:33:16 +08:00
|
|
|
CSEMap[Opcode].push_back(MI);
|
2009-11-04 05:40:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-11-07 11:52:02 +08:00
|
|
|
const MachineInstr*
|
|
|
|
MachineLICM::LookForDuplicate(const MachineInstr *MI,
|
|
|
|
std::vector<const MachineInstr*> &PrevMIs) {
|
2009-11-05 08:51:13 +08:00
|
|
|
for (unsigned i = 0, e = PrevMIs.size(); i != e; ++i) {
|
|
|
|
const MachineInstr *PrevMI = PrevMIs[i];
|
2014-04-14 08:51:57 +08:00
|
|
|
if (TII->produceSameValue(MI, PrevMI, (PreRegAlloc ? MRI : nullptr)))
|
2009-11-05 08:51:13 +08:00
|
|
|
return PrevMI;
|
|
|
|
}
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2009-11-05 08:51:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MachineLICM::EliminateCSE(MachineInstr *MI,
|
|
|
|
DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) {
|
2010-07-14 09:22:19 +08:00
|
|
|
// Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
|
|
|
|
// the undef property onto uses.
|
|
|
|
if (CI == CSEMap.end() || MI->isImplicitDef())
|
2009-11-07 11:52:02 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) {
|
2010-01-05 08:03:48 +08:00
|
|
|
DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup);
|
2010-02-28 09:33:43 +08:00
|
|
|
|
|
|
|
// Replace virtual registers defined by MI by their counterparts defined
|
|
|
|
// by Dup.
|
2011-10-18 03:50:12 +08:00
|
|
|
SmallVector<unsigned, 2> Defs;
|
2009-11-07 11:52:02 +08:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2010-02-28 09:33:43 +08:00
|
|
|
|
|
|
|
// Physical registers may not differ here.
|
|
|
|
assert((!MO.isReg() || MO.getReg() == 0 ||
|
|
|
|
!TargetRegisterInfo::isPhysicalRegister(MO.getReg()) ||
|
|
|
|
MO.getReg() == Dup->getOperand(i).getReg()) &&
|
|
|
|
"Instructions with different phys regs are not identical!");
|
|
|
|
|
|
|
|
if (MO.isReg() && MO.isDef() &&
|
2011-10-18 03:50:12 +08:00
|
|
|
!TargetRegisterInfo::isPhysicalRegister(MO.getReg()))
|
|
|
|
Defs.push_back(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<const TargetRegisterClass*, 2> OrigRCs;
|
|
|
|
for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
|
|
|
|
unsigned Idx = Defs[i];
|
|
|
|
unsigned Reg = MI->getOperand(Idx).getReg();
|
|
|
|
unsigned DupReg = Dup->getOperand(Idx).getReg();
|
|
|
|
OrigRCs.push_back(MRI->getRegClass(DupReg));
|
|
|
|
|
|
|
|
if (!MRI->constrainRegClass(DupReg, MRI->getRegClass(Reg))) {
|
|
|
|
// Restore old RCs if more than one defs.
|
|
|
|
for (unsigned j = 0; j != i; ++j)
|
|
|
|
MRI->setRegClass(Dup->getOperand(Defs[j]).getReg(), OrigRCs[j]);
|
|
|
|
return false;
|
2010-05-14 04:34:42 +08:00
|
|
|
}
|
2009-11-05 08:51:13 +08:00
|
|
|
}
|
2011-10-18 03:50:12 +08:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
|
|
|
|
unsigned Idx = Defs[i];
|
|
|
|
unsigned Reg = MI->getOperand(Idx).getReg();
|
|
|
|
unsigned DupReg = Dup->getOperand(Idx).getReg();
|
|
|
|
MRI->replaceRegWith(Reg, DupReg);
|
|
|
|
MRI->clearKillFlags(DupReg);
|
|
|
|
}
|
|
|
|
|
2009-11-07 11:52:02 +08:00
|
|
|
MI->eraseFromParent();
|
|
|
|
++NumCSEed;
|
|
|
|
return true;
|
2009-11-05 08:51:13 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-10-12 08:09:14 +08:00
|
|
|
/// MayCSE - Return true if the given instruction will be CSE'd if it's
|
|
|
|
/// hoisted out of the loop.
|
|
|
|
bool MachineLICM::MayCSE(MachineInstr *MI) {
|
|
|
|
unsigned Opcode = MI->getOpcode();
|
|
|
|
DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
|
|
|
|
CI = CSEMap.find(Opcode);
|
|
|
|
// Do not CSE implicit_def so ProcessImplicitDefs can properly propagate
|
|
|
|
// the undef property onto uses.
|
|
|
|
if (CI == CSEMap.end() || MI->isImplicitDef())
|
|
|
|
return false;
|
|
|
|
|
2014-04-14 08:51:57 +08:00
|
|
|
return LookForDuplicate(MI, CI->second) != nullptr;
|
2011-10-12 08:09:14 +08:00
|
|
|
}
|
|
|
|
|
2008-05-13 03:38:32 +08:00
|
|
|
/// Hoist - When an instruction is found to use only loop invariant operands
|
|
|
|
/// that are safe to hoist, this instruction is called to do the dirty work.
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
///
|
2010-10-21 06:03:58 +08:00
|
|
|
bool MachineLICM::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) {
|
2009-10-28 11:21:57 +08:00
|
|
|
// First check whether we should hoist this instruction.
|
2009-11-21 07:31:34 +08:00
|
|
|
if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) {
|
2009-10-30 01:47:20 +08:00
|
|
|
// If not, try unfolding a hoistable load.
|
|
|
|
MI = ExtractHoistableLoad(MI);
|
2010-10-21 06:03:58 +08:00
|
|
|
if (!MI) return false;
|
2009-10-28 11:21:57 +08:00
|
|
|
}
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2009-01-16 06:01:38 +08:00
|
|
|
// Now move the instructions to the predecessor, inserting it before any
|
|
|
|
// terminator instructions.
|
|
|
|
DEBUG({
|
2010-01-05 08:03:48 +08:00
|
|
|
dbgs() << "Hoisting " << *MI;
|
2010-06-23 01:25:57 +08:00
|
|
|
if (Preheader->getBasicBlock())
|
2010-01-05 08:03:48 +08:00
|
|
|
dbgs() << " to MachineBasicBlock "
|
2010-06-23 01:25:57 +08:00
|
|
|
<< Preheader->getName();
|
2009-10-28 11:21:57 +08:00
|
|
|
if (MI->getParent()->getBasicBlock())
|
2010-01-05 08:03:48 +08:00
|
|
|
dbgs() << " from MachineBasicBlock "
|
2009-11-20 09:17:03 +08:00
|
|
|
<< MI->getParent()->getName();
|
2010-01-05 08:03:48 +08:00
|
|
|
dbgs() << "\n";
|
2009-01-16 06:01:38 +08:00
|
|
|
});
|
2007-12-08 09:47:01 +08:00
|
|
|
|
2009-11-04 05:40:02 +08:00
|
|
|
// If this is the first instruction being hoisted to the preheader,
|
|
|
|
// initialize the CSE map with potential common expressions.
|
2010-05-29 08:06:36 +08:00
|
|
|
if (FirstInLoop) {
|
2010-06-23 01:25:57 +08:00
|
|
|
InitCSEMap(Preheader);
|
2010-05-29 08:06:36 +08:00
|
|
|
FirstInLoop = false;
|
|
|
|
}
|
2009-11-04 05:40:02 +08:00
|
|
|
|
2009-02-05 16:45:46 +08:00
|
|
|
// Look for opportunity to CSE the hoisted instruction.
|
2009-11-04 05:40:02 +08:00
|
|
|
unsigned Opcode = MI->getOpcode();
|
|
|
|
DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
|
|
|
|
CI = CSEMap.find(Opcode);
|
2009-11-05 08:51:13 +08:00
|
|
|
if (!EliminateCSE(MI, CI)) {
|
|
|
|
// Otherwise, splice the instruction to the preheader.
|
2010-06-23 01:25:57 +08:00
|
|
|
Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI);
|
2009-11-04 05:40:02 +08:00
|
|
|
|
2010-10-21 06:03:58 +08:00
|
|
|
// Update register pressure for BBs from header to this block.
|
|
|
|
UpdateBackTraceRegPressure(MI);
|
|
|
|
|
2010-05-14 04:34:42 +08:00
|
|
|
// Clear the kill flags of any register this instruction defines,
|
|
|
|
// since they may need to be live throughout the entire loop
|
|
|
|
// rather than just live for part of it.
|
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (MO.isReg() && MO.isDef() && !MO.isDead())
|
2010-10-14 09:16:09 +08:00
|
|
|
MRI->clearKillFlags(MO.getReg());
|
2010-05-14 04:34:42 +08:00
|
|
|
}
|
|
|
|
|
2009-02-05 16:45:46 +08:00
|
|
|
// Add to the CSE map.
|
|
|
|
if (CI != CSEMap.end())
|
2009-10-28 11:21:57 +08:00
|
|
|
CI->second.push_back(MI);
|
2014-10-04 02:33:16 +08:00
|
|
|
else
|
|
|
|
CSEMap[Opcode].push_back(MI);
|
2009-02-05 16:45:46 +08:00
|
|
|
}
|
2007-12-08 09:47:01 +08:00
|
|
|
|
2009-01-16 06:01:38 +08:00
|
|
|
++NumHoisted;
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
Changed = true;
|
2010-10-21 06:03:58 +08:00
|
|
|
|
|
|
|
return true;
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
}
|
2010-06-23 01:25:57 +08:00
|
|
|
|
|
|
|
MachineBasicBlock *MachineLICM::getCurPreheader() {
|
|
|
|
// Determine the block to which to hoist instructions. If we can't find a
|
|
|
|
// suitable loop predecessor, we can't do any hoisting.
|
|
|
|
|
|
|
|
// If we've tried to get a preheader and failed, don't try again.
|
|
|
|
if (CurPreheader == reinterpret_cast<MachineBasicBlock *>(-1))
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2010-06-23 01:25:57 +08:00
|
|
|
|
|
|
|
if (!CurPreheader) {
|
|
|
|
CurPreheader = CurLoop->getLoopPreheader();
|
|
|
|
if (!CurPreheader) {
|
|
|
|
MachineBasicBlock *Pred = CurLoop->getLoopPredecessor();
|
|
|
|
if (!Pred) {
|
|
|
|
CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2010-06-23 01:25:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), this);
|
|
|
|
if (!CurPreheader) {
|
|
|
|
CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2010-06-23 01:25:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CurPreheader;
|
|
|
|
}
|