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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
//===-- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ---------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00: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-15 22:01:38 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-01-04 06:41:45 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2012-12-03 16:50:05 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
#include "llvm/CodeGen/MachineDominators.h"
|
2010-04-07 00:41:17 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
#include "llvm/CodeGen/MachineLoopInfo.h"
|
2009-10-28 03:21:57 +00:00
|
|
|
#include "llvm/CodeGen/MachineMemOperand.h"
|
2008-01-02 19:32:43 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2009-10-28 03:21:57 +00:00
|
|
|
#include "llvm/CodeGen/PseudoSourceValue.h"
|
2015-06-13 03:42:11 +00:00
|
|
|
#include "llvm/CodeGen/TargetSchedule.h"
|
2011-10-12 21:33:49 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2008-01-04 06:41:45 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-25 00:23:56 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/Target/TargetInstrInfo.h"
|
|
|
|
#include "llvm/Target/TargetLowering.h"
|
|
|
|
#include "llvm/Target/TargetMachine.h"
|
|
|
|
#include "llvm/Target/TargetRegisterInfo.h"
|
2014-08-04 21:25:23 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#define DEBUG_TYPE "machine-licm"
|
|
|
|
|
2011-10-12 21:33:49 +00:00
|
|
|
static cl::opt<bool>
|
|
|
|
AvoidSpeculation("avoid-speculation",
|
|
|
|
cl::desc("MachineLICM should avoid speculation"),
|
2011-10-26 01:26:57 +00:00
|
|
|
cl::init(true), cl::Hidden);
|
2011-10-12 21:33:49 +00:00
|
|
|
|
2015-01-08 22:10:48 +00: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 10:58:38 +00: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 02:20:26 +00: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 00:41:17 +00:00
|
|
|
STATISTIC(NumPostRAHoisted,
|
|
|
|
"Number of machine instructions hoisted out of loops post regalloc");
|
2007-12-08 01:47:01 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
class MachineLICM : public MachineFunctionPass {
|
2007-12-11 23:27:51 +00:00
|
|
|
const TargetInstrInfo *TII;
|
2013-01-11 20:05:37 +00:00
|
|
|
const TargetLoweringBase *TLI;
|
2009-09-25 23:58:45 +00:00
|
|
|
const TargetRegisterInfo *TRI;
|
2010-04-07 00:41:17 +00:00
|
|
|
const MachineFrameInfo *MFI;
|
2010-10-14 01:16:09 +00:00
|
|
|
MachineRegisterInfo *MRI;
|
2015-06-13 03:42:11 +00:00
|
|
|
TargetSchedModel SchedModel;
|
2012-02-08 21:23:03 +00:00
|
|
|
bool PreRegAlloc;
|
2007-12-11 19:40:06 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
// Various analyses that we use...
|
2009-10-07 17:38:06 +00:00
|
|
|
AliasAnalysis *AA; // Alias analysis info.
|
2010-04-08 01:03:47 +00:00
|
|
|
MachineLoopInfo *MLI; // Current MachineLoopInfo
|
2008-05-12 19:38:32 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
|
|
|
// State that is updated as we process loops
|
2008-05-12 19:38:32 +00:00
|
|
|
bool Changed; // True if a loop is changed.
|
2010-05-29 00:06:36 +00:00
|
|
|
bool FirstInLoop; // True if it's the first LICM in the loop.
|
2008-05-12 19:38:32 +00:00
|
|
|
MachineLoop *CurLoop; // The current loop we are working on.
|
2009-01-15 22:01:38 +00:00
|
|
|
MachineBasicBlock *CurPreheader; // The preheader for CurLoop.
|
2009-02-05 08:45:46 +00:00
|
|
|
|
2012-04-11 00:00:26 +00:00
|
|
|
// Exit blocks for CurLoop.
|
|
|
|
SmallVector<MachineBasicBlock*, 8> ExitBlocks;
|
|
|
|
|
|
|
|
bool isExitBlock(const MachineBasicBlock *MBB) const {
|
2016-08-11 22:21:41 +00:00
|
|
|
return is_contained(ExitBlocks, MBB);
|
2012-04-11 00:00:26 +00:00
|
|
|
}
|
|
|
|
|
2010-10-14 01:16:09 +00:00
|
|
|
// Track 'estimated' register pressure.
|
2010-10-16 02:20:26 +00:00
|
|
|
SmallSet<unsigned, 32> RegSeen;
|
2010-10-14 01:16:09 +00:00
|
|
|
SmallVector<unsigned, 8> RegPressure;
|
2010-10-16 02:20:26 +00:00
|
|
|
|
2015-04-14 11:56:25 +00:00
|
|
|
// Register pressure "limit" per register pressure set. If the pressure
|
2010-10-16 02:20:26 +00:00
|
|
|
// is higher than the limit, then it's considered high.
|
2010-10-14 01:16:09 +00:00
|
|
|
SmallVector<unsigned, 8> RegLimit;
|
|
|
|
|
2010-10-16 02:20:26 +00:00
|
|
|
// Register pressure on path leading from loop preheader to current BB.
|
|
|
|
SmallVector<SmallVector<unsigned, 8>, 16> BackTrace;
|
|
|
|
|
2010-07-29 17:45:24 +00:00
|
|
|
// For each opcode, keep a list of potential CSE instructions.
|
2009-11-03 21:40:02 +00:00
|
|
|
DenseMap<unsigned, std::vector<const MachineInstr*> > CSEMap;
|
2010-04-07 00:41:17 +00:00
|
|
|
|
2011-10-11 23:48:44 +00:00
|
|
|
enum {
|
|
|
|
SpeculateFalse = 0,
|
|
|
|
SpeculateTrue = 1,
|
|
|
|
SpeculateUnknown = 2
|
|
|
|
};
|
|
|
|
|
2011-10-11 18:09:58 +00:00
|
|
|
// If a MBB does not dominate loop exiting blocks then it may not safe
|
|
|
|
// to hoist loads from this block.
|
2011-10-11 23:48:44 +00:00
|
|
|
// Tri-state: 0 - false, 1 - true, 2 - unknown
|
|
|
|
unsigned SpeculationState;
|
2011-10-11 18:09:58 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-04-07 00:41:17 +00:00
|
|
|
MachineLICM() :
|
2010-10-19 17:21:58 +00:00
|
|
|
MachineFunctionPass(ID), PreRegAlloc(true) {
|
|
|
|
initializeMachineLICMPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2010-04-07 00:41:17 +00:00
|
|
|
|
|
|
|
explicit MachineLICM(bool PreRA) :
|
2010-10-19 17:21:58 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
2014-03-07 09:26:03 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
2014-03-07 09:26:03 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00: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
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@247167 91177308-0d34-0410-b5e6-96231b3b80d8
2015-09-09 17:55:00 +00:00
|
|
|
AU.addRequired<AAResultsWrapperPass>();
|
2008-01-04 08:48:49 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
}
|
2009-02-05 08:45:46 +00:00
|
|
|
|
2014-03-07 09:26:03 +00:00
|
|
|
void releaseMemory() override {
|
2010-10-16 02:20:26 +00:00
|
|
|
RegSeen.clear();
|
2010-10-14 01:16:09 +00:00
|
|
|
RegPressure.clear();
|
|
|
|
RegLimit.clear();
|
2010-10-19 18:58:51 +00:00
|
|
|
BackTrace.clear();
|
2009-02-05 08:45:46 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
private:
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Keep track of information about hoisting candidates.
|
2010-04-08 01:03:47 +00:00
|
|
|
struct CandidateInfo {
|
|
|
|
MachineInstr *MI;
|
|
|
|
unsigned Def;
|
2010-04-13 18:16:00 +00:00
|
|
|
int FI;
|
|
|
|
CandidateInfo(MachineInstr *mi, unsigned def, int fi)
|
|
|
|
: MI(mi), Def(def), FI(fi) {}
|
2010-04-08 01:03:47 +00:00
|
|
|
};
|
|
|
|
|
2010-04-17 07:07:11 +00:00
|
|
|
void HoistRegionPostRA();
|
2010-04-08 01:03:47 +00:00
|
|
|
|
|
|
|
void HoistPostRA(MachineInstr *MI, unsigned Def);
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
void ProcessMI(MachineInstr *MI, BitVector &PhysRegDefs,
|
|
|
|
BitVector &PhysRegClobbers, SmallSet<int, 32> &StoredFIs,
|
2013-07-11 16:22:38 +00:00
|
|
|
SmallVectorImpl<CandidateInfo> &Candidates);
|
2010-04-08 01:03:47 +00:00
|
|
|
|
2010-04-17 07:07:11 +00:00
|
|
|
void AddToLiveIns(unsigned Reg);
|
2010-04-08 01:03:47 +00:00
|
|
|
|
2010-04-13 18:16:00 +00:00
|
|
|
bool IsLICMCandidate(MachineInstr &I);
|
|
|
|
|
2007-12-08 23:58:46 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
2012-04-11 00:00:26 +00:00
|
|
|
bool HasLoopPHIUse(const MachineInstr *MI) const;
|
2011-04-11 21:09:18 +00:00
|
|
|
|
2010-10-26 02:08:50 +00:00
|
|
|
bool HasHighOperandLatency(MachineInstr &MI, unsigned DefIdx,
|
|
|
|
unsigned Reg) const;
|
|
|
|
|
|
|
|
bool IsCheapInstruction(MachineInstr &MI) const;
|
2010-10-14 01:16:09 +00:00
|
|
|
|
2015-04-03 16:19:48 +00:00
|
|
|
bool CanCauseHighRegPressure(const DenseMap<unsigned, int> &Cost,
|
|
|
|
bool Cheap);
|
2010-10-20 22:03:58 +00:00
|
|
|
|
|
|
|
void UpdateBackTraceRegPressure(const MachineInstr *MI);
|
2010-10-16 02:20:26 +00:00
|
|
|
|
2009-11-20 23:31:34 +00:00
|
|
|
bool IsProfitableToHoist(MachineInstr &MI);
|
2009-02-04 09:19:56 +00:00
|
|
|
|
2011-10-11 18:09:58 +00:00
|
|
|
bool IsGuaranteedToExecute(MachineBasicBlock *BB);
|
|
|
|
|
2011-12-22 02:05:40 +00:00
|
|
|
void EnterScope(MachineBasicBlock *MBB);
|
|
|
|
|
|
|
|
void ExitScope(MachineBasicBlock *MBB);
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
void ExitScopeIfDone(
|
|
|
|
MachineDomTreeNode *Node,
|
|
|
|
DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren,
|
|
|
|
DenseMap<MachineDomTreeNode *, MachineDomTreeNode *> &ParentMap);
|
|
|
|
|
2011-12-22 02:05:40 +00:00
|
|
|
void HoistOutOfLoop(MachineDomTreeNode *LoopHeaderNode);
|
2015-12-10 16:34:21 +00:00
|
|
|
|
2011-12-22 02:05:40 +00:00
|
|
|
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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
2015-03-14 10:58:38 +00:00
|
|
|
void SinkIntoLoop();
|
|
|
|
|
2010-10-14 01:16:09 +00:00
|
|
|
void InitRegPressure(MachineBasicBlock *BB);
|
|
|
|
|
2015-04-07 16:42:35 +00:00
|
|
|
DenseMap<unsigned, int> calcRegisterCost(const MachineInstr *MI,
|
|
|
|
bool ConsiderSeen,
|
|
|
|
bool ConsiderUnseenAsDef);
|
|
|
|
|
|
|
|
void UpdateRegPressure(const MachineInstr *MI,
|
|
|
|
bool ConsiderUnseenAsDef = false);
|
2010-10-14 01:16:09 +00:00
|
|
|
|
2009-10-29 17:47:20 +00:00
|
|
|
MachineInstr *ExtractHoistableLoad(MachineInstr *MI);
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
const MachineInstr *
|
|
|
|
LookForDuplicate(const MachineInstr *MI,
|
|
|
|
std::vector<const MachineInstr *> &PrevMIs);
|
2009-11-07 03:52:02 +00:00
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
bool EliminateCSE(
|
|
|
|
MachineInstr *MI,
|
|
|
|
DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator &CI);
|
2009-11-05 00:51:13 +00:00
|
|
|
|
2011-10-12 00:09:14 +00:00
|
|
|
bool MayCSE(MachineInstr *MI);
|
|
|
|
|
2010-10-20 22:03:58 +00:00
|
|
|
bool Hoist(MachineInstr *MI, MachineBasicBlock *Preheader);
|
2009-11-03 21:40:02 +00:00
|
|
|
|
|
|
|
void InitCSEMap(MachineBasicBlock *BB);
|
2010-06-22 17:25:57 +00:00
|
|
|
|
|
|
|
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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char MachineLICM::ID = 0;
|
2012-02-08 21:23:13 +00:00
|
|
|
char &llvm::MachineLICMID = MachineLICM::ID;
|
2010-10-12 19:48:12 +00: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
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@247167 91177308-0d34-0410-b5e6-96231b3b80d8
2015-09-09 17:55:00 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_END(MachineLICM, "machinelicm",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Machine Loop Invariant Code Motion", false, false)
|
2008-05-13 00:00:25 +00:00
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Test if the given loop is the outer-most loop that has a unique predecessor.
|
2010-06-22 17:25:57 +00:00
|
|
|
static bool LoopIsOuterMostWithPredecessor(MachineLoop *CurLoop) {
|
2010-07-09 18:49:45 +00: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-15 22:01:38 +00:00
|
|
|
for (MachineLoop *L = CurLoop->getParentLoop(); L; L = L->getParentLoop())
|
2010-06-22 17:25:57 +00:00
|
|
|
if (L->getLoopPredecessor())
|
2009-01-15 22:01:38 +00:00
|
|
|
return false;
|
2010-07-09 18:49:45 +00:00
|
|
|
// None of them did, so this is the outermost with a unique predecessor.
|
2009-01-15 22:01:38 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
bool MachineLICM::runOnMachineFunction(MachineFunction &MF) {
|
2016-04-22 22:06:11 +00:00
|
|
|
if (skipFunction(*MF.getFunction()))
|
2014-03-31 17:43:35 +00:00
|
|
|
return false;
|
|
|
|
|
2010-05-29 00:06:36 +00:00
|
|
|
Changed = FirstInLoop = false;
|
2015-06-13 03:42:11 +00:00
|
|
|
const TargetSubtargetInfo &ST = MF.getSubtarget();
|
|
|
|
TII = ST.getInstrInfo();
|
|
|
|
TLI = ST.getTargetLowering();
|
|
|
|
TRI = ST.getRegisterInfo();
|
2016-07-28 18:40:00 +00:00
|
|
|
MFI = &MF.getFrameInfo();
|
2010-10-14 01:16:09 +00:00
|
|
|
MRI = &MF.getRegInfo();
|
2015-06-13 03:42:11 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
2012-02-08 21:23:03 +00:00
|
|
|
PreRegAlloc = MRI->isSSA();
|
|
|
|
|
2012-02-11 00:40:36 +00:00
|
|
|
if (PreRegAlloc)
|
|
|
|
DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: ");
|
|
|
|
else
|
|
|
|
DEBUG(dbgs() << "******** Post-regalloc Machine LICM: ");
|
2012-08-22 06:07:19 +00:00
|
|
|
DEBUG(dbgs() << MF.getName() << " ********\n");
|
2012-02-11 00:40:36 +00:00
|
|
|
|
2010-10-14 01:16:09 +00:00
|
|
|
if (PreRegAlloc) {
|
|
|
|
// Estimate register pressure during pre-regalloc pass.
|
2015-04-14 11:56:25 +00:00
|
|
|
unsigned NumRPS = TRI->getNumRegPressureSets();
|
|
|
|
RegPressure.resize(NumRPS);
|
2010-10-14 01:16:09 +00:00
|
|
|
std::fill(RegPressure.begin(), RegPressure.end(), 0);
|
2015-04-14 11:56:25 +00:00
|
|
|
RegLimit.resize(NumRPS);
|
|
|
|
for (unsigned i = 0, e = NumRPS; i != e; ++i)
|
|
|
|
RegLimit[i] = TRI->getRegPressureSetLimit(MF, i);
|
2010-10-14 01:16:09 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
// Get our Loop information...
|
2010-04-08 01:03:47 +00: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
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@247167 91177308-0d34-0410-b5e6-96231b3b80d8
2015-09-09 17:55:00 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
2010-07-09 18:49:45 +00:00
|
|
|
SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
CurLoop = Worklist.pop_back_val();
|
2014-04-14 00:51:57 +00:00
|
|
|
CurPreheader = nullptr;
|
2012-04-11 00:00:26 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
2010-04-08 01:03:47 +00:00
|
|
|
// If this is done before regalloc, only visit outer-most preheader-sporting
|
|
|
|
// loops.
|
2010-07-09 18:49:45 +00:00
|
|
|
if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) {
|
|
|
|
Worklist.append(CurLoop->begin(), CurLoop->end());
|
2009-01-15 22:01:38 +00:00
|
|
|
continue;
|
2010-07-09 18:49:45 +00:00
|
|
|
}
|
2009-01-15 22:01:38 +00:00
|
|
|
|
2012-04-11 00:00:26 +00:00
|
|
|
CurLoop->getExitBlocks(ExitBlocks);
|
|
|
|
|
2010-04-07 00:41:17 +00:00
|
|
|
if (!PreRegAlloc)
|
2010-04-17 07:07:11 +00:00
|
|
|
HoistRegionPostRA();
|
2010-04-07 00:41:17 +00:00
|
|
|
else {
|
2010-04-17 07:07:11 +00:00
|
|
|
// CSEMap is initialized for loop header when the first instruction is
|
|
|
|
// being hoisted.
|
|
|
|
MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader());
|
2010-05-29 00:06:36 +00:00
|
|
|
FirstInLoop = true;
|
2011-12-22 02:05:40 +00:00
|
|
|
HoistOutOfLoop(N);
|
2010-04-07 00:41:17 +00:00
|
|
|
CSEMap.clear();
|
2015-03-14 10:58:38 +00:00
|
|
|
|
|
|
|
if (SinkInstsToAvoidSpills)
|
|
|
|
SinkIntoLoop();
|
2010-04-07 00:41:17 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Return true if instruction stores to the specified frame.
|
2010-04-08 01:03:47 +00:00
|
|
|
static bool InstructionStoresToFI(const MachineInstr *MI, int FI) {
|
2015-12-23 17:05:57 +00:00
|
|
|
// If we lost memory operands, conservatively assume that the instruction
|
|
|
|
// writes to all slots.
|
|
|
|
if (MI->memoperands_empty())
|
|
|
|
return true;
|
2016-01-06 23:45:05 +00:00
|
|
|
for (const MachineMemOperand *MemOp : MI->memoperands()) {
|
|
|
|
if (!MemOp->isStore() || !MemOp->getPseudoValue())
|
2010-04-08 01:03:47 +00:00
|
|
|
continue;
|
|
|
|
if (const FixedStackPseudoSourceValue *Value =
|
2016-01-06 23:45:05 +00:00
|
|
|
dyn_cast<FixedStackPseudoSourceValue>(MemOp->getPseudoValue())) {
|
2010-04-08 01:03:47 +00:00
|
|
|
if (Value->getFrameIndex() == FI)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Examine the instruction for potentai LICM candidate. Also
|
2010-04-08 01:03:47 +00:00
|
|
|
/// gather register def and frame object update information.
|
|
|
|
void MachineLICM::ProcessMI(MachineInstr *MI,
|
2012-01-20 22:27:12 +00:00
|
|
|
BitVector &PhysRegDefs,
|
|
|
|
BitVector &PhysRegClobbers,
|
2010-04-08 01:03:47 +00:00
|
|
|
SmallSet<int, 32> &StoredFIs,
|
2013-07-11 16:22:38 +00:00
|
|
|
SmallVectorImpl<CandidateInfo> &Candidates) {
|
2010-04-08 01:03:47 +00:00
|
|
|
bool RuledOut = false;
|
2010-04-13 20:21:05 +00:00
|
|
|
bool HasNonInvariantUse = false;
|
2010-04-08 01:03:47 +00:00
|
|
|
unsigned Def = 0;
|
2016-01-06 23:45:05 +00:00
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
2010-04-08 01:03:47 +00:00
|
|
|
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-13 20:21:05 +00:00
|
|
|
HasNonInvariantUse = true;
|
2010-04-08 01:03:47 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-20 22:27:12 +00:00
|
|
|
// We can't hoist an instruction defining a physreg that is clobbered in
|
|
|
|
// the loop.
|
|
|
|
if (MO.isRegMask()) {
|
2012-02-02 23:52:57 +00:00
|
|
|
PhysRegClobbers.setBitsNotInMask(MO.getRegMask());
|
2012-01-20 22:27:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2010-04-08 01:03:47 +00:00
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (!Reg)
|
|
|
|
continue;
|
|
|
|
assert(TargetRegisterInfo::isPhysicalRegister(Reg) &&
|
|
|
|
"Not expecting virtual register!");
|
|
|
|
|
2010-04-13 18:16:00 +00:00
|
|
|
if (!MO.isDef()) {
|
2012-01-20 22:27:12 +00:00
|
|
|
if (Reg && (PhysRegDefs.test(Reg) || PhysRegClobbers.test(Reg)))
|
2010-04-13 20:21:05 +00:00
|
|
|
// If it's using a non-loop-invariant register, then it's obviously not
|
|
|
|
// safe to hoist.
|
|
|
|
HasNonInvariantUse = true;
|
2010-04-08 01:03:47 +00:00
|
|
|
continue;
|
2010-04-13 18:16:00 +00:00
|
|
|
}
|
2010-04-08 01:03:47 +00:00
|
|
|
|
|
|
|
if (MO.isImplicit()) {
|
2012-06-01 23:28:30 +00:00
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
PhysRegClobbers.set(*AI);
|
2010-04-08 01:03:47 +00: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-20 22:27:12 +00:00
|
|
|
// register, then this is not safe. Two defs is indicated by setting a
|
|
|
|
// PhysRegClobbers bit.
|
2012-06-01 23:28:30 +00:00
|
|
|
for (MCRegAliasIterator AS(Reg, TRI, true); AS.isValid(); ++AS) {
|
2012-01-23 21:01:15 +00:00
|
|
|
if (PhysRegDefs.test(*AS))
|
|
|
|
PhysRegClobbers.set(*AS);
|
|
|
|
PhysRegDefs.set(*AS);
|
2012-01-20 22:27:12 +00:00
|
|
|
}
|
2013-08-20 09:11:13 +00: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 01:03:47 +00:00
|
|
|
}
|
|
|
|
|
2010-04-13 18:16:00 +00:00
|
|
|
// Only consider reloads for now and remats which do not have register
|
|
|
|
// operands. FIXME: Consider unfold load folding instructions.
|
2010-04-08 01:03:47 +00:00
|
|
|
if (Def && !RuledOut) {
|
2010-04-13 18:16:00 +00:00
|
|
|
int FI = INT_MIN;
|
2010-04-13 20:21:05 +00:00
|
|
|
if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) ||
|
2016-06-30 00:01:54 +00:00
|
|
|
(TII->isLoadFromStackSlot(*MI, FI) && MFI->isSpillSlotObjectIndex(FI)))
|
2010-04-13 18:16:00 +00:00
|
|
|
Candidates.push_back(CandidateInfo(MI, Def, FI));
|
2010-04-08 01:03:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Walk the specified region of the CFG and hoist loop invariants out to the
|
|
|
|
/// preheader.
|
2010-04-17 07:07:11 +00:00
|
|
|
void MachineLICM::HoistRegionPostRA() {
|
2012-03-27 01:50:58 +00:00
|
|
|
MachineBasicBlock *Preheader = getCurPreheader();
|
|
|
|
if (!Preheader)
|
|
|
|
return;
|
|
|
|
|
2010-04-07 00:41:17 +00:00
|
|
|
unsigned NumRegs = TRI->getNumRegs();
|
2012-01-20 22:27:12 +00:00
|
|
|
BitVector PhysRegDefs(NumRegs); // Regs defined once in the loop.
|
|
|
|
BitVector PhysRegClobbers(NumRegs); // Regs defined more than once.
|
2010-04-07 00:41:17 +00:00
|
|
|
|
2010-04-08 01:03:47 +00:00
|
|
|
SmallVector<CandidateInfo, 32> Candidates;
|
2010-04-07 00:41:17 +00:00
|
|
|
SmallSet<int, 32> StoredFIs;
|
|
|
|
|
|
|
|
// Walk the entire region, count number of defs for each register, and
|
2010-04-17 07:07:11 +00:00
|
|
|
// collect potential LICM candidates.
|
2013-09-15 22:04:42 +00:00
|
|
|
const std::vector<MachineBasicBlock *> &Blocks = CurLoop->getBlocks();
|
2016-01-06 23:45:05 +00:00
|
|
|
for (MachineBasicBlock *BB : Blocks) {
|
2011-10-12 02:58:01 +00: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-27 23:27:47 +00:00
|
|
|
if (ML && ML->getHeader()->isEHPad()) continue;
|
2011-10-12 02:58:01 +00:00
|
|
|
|
2010-04-07 00:41:17 +00:00
|
|
|
// Conservatively treat live-in's as an external def.
|
2010-04-08 01:03:47 +00:00
|
|
|
// FIXME: That means a reload that're reused in successor block(s) will not
|
|
|
|
// be LICM'ed.
|
2015-09-09 18:08:03 +00:00
|
|
|
for (const auto &LI : BB->liveins()) {
|
|
|
|
for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI)
|
2012-06-01 23:28:30 +00:00
|
|
|
PhysRegDefs.set(*AI);
|
2010-04-07 00:41:17 +00:00
|
|
|
}
|
|
|
|
|
2011-10-11 23:48:44 +00:00
|
|
|
SpeculationState = SpeculateUnknown;
|
2016-01-06 23:45:05 +00:00
|
|
|
for (MachineInstr &MI : *BB)
|
|
|
|
ProcessMI(&MI, PhysRegDefs, PhysRegClobbers, StoredFIs, Candidates);
|
2010-04-17 07:07:11 +00:00
|
|
|
}
|
2010-04-07 00:41:17 +00:00
|
|
|
|
2012-03-27 01:50:58 +00:00
|
|
|
// Gather the registers read / clobbered by the terminator.
|
|
|
|
BitVector TermRegs(NumRegs);
|
|
|
|
MachineBasicBlock::iterator TI = Preheader->getFirstTerminator();
|
|
|
|
if (TI != Preheader->end()) {
|
2016-01-06 23:45:05 +00:00
|
|
|
for (const MachineOperand &MO : TI->operands()) {
|
2012-03-27 01:50:58 +00:00
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (!Reg)
|
|
|
|
continue;
|
2012-06-01 23:28:30 +00:00
|
|
|
for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
|
|
|
|
TermRegs.set(*AI);
|
2012-03-27 01:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-07 00:41:17 +00: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 01:50:58 +00:00
|
|
|
// 3. Make sure candidate def should not clobber
|
|
|
|
// registers read by the terminator. Similarly its def should not be
|
|
|
|
// clobbered by the terminator.
|
2016-01-06 23:45:05 +00:00
|
|
|
for (CandidateInfo &Candidate : Candidates) {
|
|
|
|
if (Candidate.FI != INT_MIN &&
|
|
|
|
StoredFIs.count(Candidate.FI))
|
2010-04-07 00:41:17 +00:00
|
|
|
continue;
|
|
|
|
|
2016-01-06 23:45:05 +00:00
|
|
|
unsigned Def = Candidate.Def;
|
2012-03-27 01:50:58 +00:00
|
|
|
if (!PhysRegClobbers.test(Def) && !TermRegs.test(Def)) {
|
2010-04-13 20:21:05 +00:00
|
|
|
bool Safe = true;
|
2016-01-06 23:45:05 +00:00
|
|
|
MachineInstr *MI = Candidate.MI;
|
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
2010-04-13 22:13:34 +00:00
|
|
|
if (!MO.isReg() || MO.isDef() || !MO.getReg())
|
2010-04-13 20:21:05 +00:00
|
|
|
continue;
|
2012-03-27 01:50:58 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
|
|
|
if (PhysRegDefs.test(Reg) ||
|
|
|
|
PhysRegClobbers.test(Reg)) {
|
2010-04-13 20:21:05 +00:00
|
|
|
// If it's using a non-loop-invariant register, then it's obviously
|
|
|
|
// not safe to hoist.
|
|
|
|
Safe = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Safe)
|
2016-01-06 23:45:05 +00:00
|
|
|
HoistPostRA(MI, Candidate.Def);
|
2010-04-13 20:21:05 +00:00
|
|
|
}
|
2010-04-07 00:41:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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 07:07:11 +00:00
|
|
|
void MachineLICM::AddToLiveIns(unsigned Reg) {
|
2013-09-15 22:04:42 +00:00
|
|
|
const std::vector<MachineBasicBlock *> &Blocks = CurLoop->getBlocks();
|
2016-01-06 23:45:05 +00:00
|
|
|
for (MachineBasicBlock *BB : Blocks) {
|
2010-04-20 18:45:47 +00:00
|
|
|
if (!BB->isLiveIn(Reg))
|
|
|
|
BB->addLiveIn(Reg);
|
2016-01-06 23:45:05 +00:00
|
|
|
for (MachineInstr &MI : *BB) {
|
|
|
|
for (MachineOperand &MO : MI.operands()) {
|
2010-04-20 18:45:47 +00:00
|
|
|
if (!MO.isReg() || !MO.getReg() || MO.isDef()) continue;
|
|
|
|
if (MO.getReg() == Reg || TRI->isSuperRegister(Reg, MO.getReg()))
|
|
|
|
MO.setIsKill(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-04-08 01:03:47 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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-04-08 01:03:47 +00:00
|
|
|
void MachineLICM::HoistPostRA(MachineInstr *MI, unsigned Def) {
|
2010-06-22 17:25:57 +00:00
|
|
|
MachineBasicBlock *Preheader = getCurPreheader();
|
|
|
|
|
2010-04-07 00:41:17 +00:00
|
|
|
// Now move the instructions to the predecessor, inserting it before any
|
|
|
|
// terminator instructions.
|
2012-01-23 21:01:11 +00:00
|
|
|
DEBUG(dbgs() << "Hoisting to BB#" << Preheader->getNumber() << " from BB#"
|
|
|
|
<< MI->getParent()->getNumber() << ": " << *MI);
|
2010-04-07 00:41:17 +00:00
|
|
|
|
|
|
|
// Splice the instruction to the preheader.
|
2010-04-08 01:03:47 +00:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2010-06-22 17:25:57 +00:00
|
|
|
Preheader->splice(Preheader->getFirstTerminator(), MBB, MI);
|
2010-04-08 01:03:47 +00:00
|
|
|
|
2012-02-08 21:23:00 +00:00
|
|
|
// Add register to livein list to all the BBs in the current loop since a
|
2010-04-17 07:07:11 +00: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 00:41:17 +00:00
|
|
|
|
|
|
|
++NumPostRAHoisted;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Check if this mbb is guaranteed to execute. If not then a load from this mbb
|
|
|
|
/// may not be safe to hoist.
|
2011-10-11 18:09:58 +00:00
|
|
|
bool MachineLICM::IsGuaranteedToExecute(MachineBasicBlock *BB) {
|
2011-10-11 23:48:44 +00:00
|
|
|
if (SpeculationState != SpeculateUnknown)
|
|
|
|
return SpeculationState == SpeculateFalse;
|
2012-02-08 21:23:00 +00:00
|
|
|
|
2011-10-11 18:09:58 +00:00
|
|
|
if (BB != CurLoop->getHeader()) {
|
|
|
|
// Check loop exiting blocks.
|
|
|
|
SmallVector<MachineBasicBlock*, 8> CurrentLoopExitingBlocks;
|
|
|
|
CurLoop->getExitingBlocks(CurrentLoopExitingBlocks);
|
2016-01-06 23:45:05 +00:00
|
|
|
for (MachineBasicBlock *CurrentLoopExitingBlock : CurrentLoopExitingBlocks)
|
|
|
|
if (!DT->dominates(BB, CurrentLoopExitingBlock)) {
|
2011-10-13 01:09:50 +00:00
|
|
|
SpeculationState = SpeculateTrue;
|
|
|
|
return false;
|
2011-10-11 18:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-11 23:48:44 +00:00
|
|
|
SpeculationState = SpeculateFalse;
|
|
|
|
return true;
|
2011-10-11 18:09:58 +00:00
|
|
|
}
|
|
|
|
|
2011-12-22 02:05:40 +00:00
|
|
|
void MachineLICM::EnterScope(MachineBasicBlock *MBB) {
|
2016-05-23 18:56:07 +00:00
|
|
|
DEBUG(dbgs() << "Entering BB#" << MBB->getNumber() << '\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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
2011-12-22 02:05:40 +00:00
|
|
|
// Remember livein register pressure.
|
|
|
|
BackTrace.push_back(RegPressure);
|
|
|
|
}
|
2011-10-12 02:58:01 +00:00
|
|
|
|
2011-12-22 02:05:40 +00:00
|
|
|
void MachineLICM::ExitScope(MachineBasicBlock *MBB) {
|
2016-05-23 18:56:07 +00:00
|
|
|
DEBUG(dbgs() << "Exiting BB#" << MBB->getNumber() << '\n');
|
2011-12-22 02:05:40 +00:00
|
|
|
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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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.
|
2011-12-22 02:05:40 +00:00
|
|
|
void MachineLICM::ExitScopeIfDone(MachineDomTreeNode *Node,
|
2012-01-10 22:27:32 +00:00
|
|
|
DenseMap<MachineDomTreeNode*, unsigned> &OpenChildren,
|
|
|
|
DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> &ParentMap) {
|
2011-12-22 02:05:40 +00:00
|
|
|
if (OpenChildren[Node])
|
2010-10-16 02:20:26 +00:00
|
|
|
return;
|
2010-10-14 01:16:09 +00:00
|
|
|
|
2011-12-22 02:05:40 +00: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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.
|
2011-12-22 02:05:40 +00:00
|
|
|
///
|
|
|
|
void MachineLICM::HoistOutOfLoop(MachineDomTreeNode *HeaderN) {
|
2015-02-05 22:39:46 +00:00
|
|
|
MachineBasicBlock *Preheader = getCurPreheader();
|
|
|
|
if (!Preheader)
|
|
|
|
return;
|
|
|
|
|
2011-12-22 02:05:40 +00: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-05 22:39:46 +00:00
|
|
|
while (!WorkList.empty()) {
|
2011-12-22 02:05:40 +00:00
|
|
|
MachineDomTreeNode *Node = WorkList.pop_back_val();
|
2014-04-14 00:51:57 +00:00
|
|
|
assert(Node && "Null dominator tree node?");
|
2011-12-22 02:05:40 +00: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-27 23:27:47 +00:00
|
|
|
if (ML && ML->getHeader()->isEHPad())
|
2011-12-22 02:05:40 +00: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-05 22:39:46 +00:00
|
|
|
}
|
2011-12-22 02:05:40 +00:00
|
|
|
|
2015-02-05 22:39:46 +00:00
|
|
|
if (Scopes.size() == 0)
|
|
|
|
return;
|
2011-12-22 02:05:40 +00:00
|
|
|
|
2015-02-05 22:39:46 +00:00
|
|
|
// Compute registers which are livein into the loop headers.
|
|
|
|
RegSeen.clear();
|
|
|
|
BackTrace.clear();
|
|
|
|
InitRegPressure(Preheader);
|
2010-10-19 00:55:07 +00:00
|
|
|
|
2011-12-22 02:05:40 +00:00
|
|
|
// Now perform LICM.
|
2016-01-06 23:45:05 +00:00
|
|
|
for (MachineDomTreeNode *Node : Scopes) {
|
2011-12-22 02:05:40 +00:00
|
|
|
MachineBasicBlock *MBB = Node->getBlock();
|
2010-10-19 18:58:51 +00:00
|
|
|
|
2011-12-22 02:05:40 +00:00
|
|
|
EnterScope(MBB);
|
2010-10-16 02:20:26 +00:00
|
|
|
|
2011-12-22 02:05:40 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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.
|
2015-03-14 10:58:38 +00: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-09 19:40:45 +00:00
|
|
|
if (IsLoopInvariantInst(*I) && !HasLoopPHIUse(&*I))
|
|
|
|
Candidates.push_back(&*I);
|
2015-03-14 10:58:38 +00: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-20 22:03:58 +00:00
|
|
|
static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) {
|
|
|
|
return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg());
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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 01:16:09 +00:00
|
|
|
void MachineLICM::InitRegPressure(MachineBasicBlock *BB) {
|
|
|
|
std::fill(RegPressure.begin(), RegPressure.end(), 0);
|
2010-10-16 02:20:26 +00:00
|
|
|
|
2010-10-20 22:03:58 +00: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 00:51:57 +00:00
|
|
|
MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
|
2010-10-20 22:03:58 +00:00
|
|
|
SmallVector<MachineOperand, 4> Cond;
|
2016-07-15 14:41:04 +00:00
|
|
|
if (!TII->analyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty())
|
2010-10-20 22:03:58 +00:00
|
|
|
InitRegPressure(*BB->pred_begin());
|
|
|
|
}
|
|
|
|
|
2015-04-07 16:42:35 +00:00
|
|
|
for (const MachineInstr &MI : *BB)
|
|
|
|
UpdateRegPressure(&MI, /*ConsiderUnseenAsDef=*/true);
|
2010-10-14 01:16:09 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Update estimate of register pressure after the specified instruction.
|
2015-04-07 16:42:35 +00:00
|
|
|
void MachineLICM::UpdateRegPressure(const MachineInstr *MI,
|
|
|
|
bool ConsiderUnseenAsDef) {
|
|
|
|
auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/true, ConsiderUnseenAsDef);
|
2015-04-14 11:56:25 +00:00
|
|
|
for (const auto &RPIdAndCost : Cost) {
|
|
|
|
unsigned Class = RPIdAndCost.first;
|
|
|
|
if (static_cast<int>(RegPressure[Class]) < -RPIdAndCost.second)
|
2015-04-07 16:42:35 +00:00
|
|
|
RegPressure[Class] = 0;
|
|
|
|
else
|
2015-04-14 11:56:25 +00:00
|
|
|
RegPressure[Class] += RPIdAndCost.second;
|
2015-04-07 16:42:35 +00:00
|
|
|
}
|
|
|
|
}
|
2010-10-14 01:16:09 +00:00
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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.
|
2015-04-07 16:42:35 +00: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 01:16:09 +00:00
|
|
|
for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2010-10-19 18:58:51 +00:00
|
|
|
if (!MO.isReg() || MO.isImplicit())
|
2010-10-14 01:16:09 +00:00
|
|
|
continue;
|
|
|
|
unsigned Reg = MO.getReg();
|
2011-01-10 02:58:51 +00:00
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg))
|
2010-10-14 01:16:09 +00:00
|
|
|
continue;
|
|
|
|
|
2015-04-07 16:42:35 +00: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 11:56:25 +00:00
|
|
|
const TargetRegisterClass *RC = MRI->getRegClass(Reg);
|
|
|
|
|
|
|
|
RegClassWeight W = TRI->getRegClassWeight(RC);
|
|
|
|
int RCCost = 0;
|
2010-10-19 18:58:51 +00:00
|
|
|
if (MO.isDef())
|
2015-04-14 11:56:25 +00:00
|
|
|
RCCost = W.RegWeight;
|
2015-04-07 16:42:35 +00:00
|
|
|
else {
|
|
|
|
bool isKill = isOperandKill(MO, MRI);
|
|
|
|
if (isNew && !isKill && ConsiderUnseenAsDef)
|
|
|
|
// Haven't seen this, it must be a livein.
|
2015-04-14 11:56:25 +00:00
|
|
|
RCCost = W.RegWeight;
|
2015-04-07 16:42:35 +00:00
|
|
|
else if (!isNew && isKill)
|
2015-04-14 11:56:25 +00: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 02:20:26 +00:00
|
|
|
}
|
2010-10-14 01:16:09 +00:00
|
|
|
}
|
2015-04-07 16:42:35 +00:00
|
|
|
return Cost;
|
2010-10-14 01:16:09 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Return true if this machine instruction loads from global offset table or
|
|
|
|
/// constant pool.
|
2015-12-23 17:05:57 +00:00
|
|
|
static bool mayLoadFromGOTOrConstantPool(MachineInstr &MI) {
|
2011-12-07 07:15:52 +00:00
|
|
|
assert (MI.mayLoad() && "Expected MI that loads!");
|
2015-12-23 17:05:57 +00:00
|
|
|
|
|
|
|
// If we lost memory operands, conservatively assume that the instruction
|
|
|
|
// reads from everything..
|
|
|
|
if (MI.memoperands_empty())
|
|
|
|
return true;
|
|
|
|
|
2016-01-06 23:45:05 +00:00
|
|
|
for (MachineMemOperand *MemOp : MI.memoperands())
|
|
|
|
if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
|
2015-08-11 23:09:45 +00:00
|
|
|
if (PSV->isGOT() || PSV->isConstantPool())
|
2014-04-15 07:22:52 +00:00
|
|
|
return true;
|
2016-01-06 23:45:05 +00:00
|
|
|
|
2011-10-17 17:35:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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.
|
2010-04-13 18:16:00 +00:00
|
|
|
bool MachineLICM::IsLICMCandidate(MachineInstr &I) {
|
2010-07-12 00:00:35 +00:00
|
|
|
// Check if it's safe to move the instruction.
|
|
|
|
bool DontMoveAcrossStore = true;
|
2015-05-19 21:22:20 +00:00
|
|
|
if (!I.isSafeToMove(AA, DontMoveAcrossStore))
|
2008-01-10 23:08:24 +00:00
|
|
|
return false;
|
2011-10-11 18:09:58 +00: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-20 17:31:18 +00: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-11 18:09:58 +00:00
|
|
|
// Stores and side effects are already checked by isSafeToMove.
|
2015-12-23 17:05:57 +00:00
|
|
|
if (I.mayLoad() && !mayLoadFromGOTOrConstantPool(I) &&
|
2011-10-17 17:35:01 +00:00
|
|
|
!IsGuaranteedToExecute(I.getParent()))
|
2011-10-11 18:09:58 +00:00
|
|
|
return false;
|
|
|
|
|
2010-04-13 18:16:00 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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
|
2010-04-13 18:16:00 +00:00
|
|
|
/// effects that aren't captured by the operands or other flags.
|
2012-02-08 21:23:00 +00:00
|
|
|
///
|
2010-04-13 18:16:00 +00:00
|
|
|
bool MachineLICM::IsLoopInvariantInst(MachineInstr &I) {
|
|
|
|
if (!IsLICMCandidate(I))
|
|
|
|
return false;
|
2008-03-10 08:13:01 +00:00
|
|
|
|
2008-05-12 19:38:32 +00:00
|
|
|
// The instruction is loop invariant if all of its operands are.
|
2016-01-06 23:45:05 +00:00
|
|
|
for (const MachineOperand &MO : I.operands()) {
|
2008-10-03 15:45:36 +00:00
|
|
|
if (!MO.isReg())
|
2008-08-20 20:32:05 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
2008-03-10 08:13:01 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
2009-01-15 22:01:38 +00:00
|
|
|
// Don't hoist an instruction that uses or defines a physical register.
|
2009-09-25 23:58:45 +00:00
|
|
|
if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
|
|
|
|
if (MO.isUse()) {
|
|
|
|
// If the physreg has no defs anywhere, it's just an ambient register
|
2009-09-26 02:34:00 +00:00
|
|
|
// and we can freely move its uses. Alternatively, if it's allocatable,
|
|
|
|
// it could get allocated to something with a def during allocation.
|
2016-10-28 18:05:09 +00:00
|
|
|
if (!MRI->isConstantPhysReg(Reg))
|
2009-09-26 02:34:00 +00:00
|
|
|
return false;
|
2009-09-25 23:58:45 +00: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 00:08:44 +00: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-25 23:58:45 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
2009-01-15 22:01:38 +00:00
|
|
|
if (!MO.isUse())
|
|
|
|
continue;
|
|
|
|
|
2010-10-14 01:16:09 +00:00
|
|
|
assert(MRI->getVRegDef(Reg) &&
|
2008-05-12 19:38:32 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
|
|
|
// If the loop contains the definition of an operand, then the instruction
|
|
|
|
// isn't loop invariant.
|
2010-10-14 01:16:09 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we got this far, the instruction is loop invariant!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-05 08:45:46 +00:00
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Return true if the specified instruction is used by a phi node and hoisting
|
|
|
|
/// it could cause a copy to be inserted.
|
2012-04-11 00:00:26 +00:00
|
|
|
bool MachineLICM::HasLoopPHIUse(const MachineInstr *MI) const {
|
|
|
|
SmallVector<const MachineInstr*, 8> Work(1, MI);
|
|
|
|
do {
|
|
|
|
MI = Work.pop_back_val();
|
2015-05-29 02:56:46 +00:00
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
|
|
|
if (!MO.isReg() || !MO.isDef())
|
2012-04-11 00:00:26 +00:00
|
|
|
continue;
|
2015-05-29 02:56:46 +00:00
|
|
|
unsigned Reg = MO.getReg();
|
2012-04-11 00:00:26 +00:00
|
|
|
if (!TargetRegisterInfo::isVirtualRegister(Reg))
|
|
|
|
continue;
|
2014-03-17 19:36:09 +00:00
|
|
|
for (MachineInstr &UseMI : MRI->use_instructions(Reg)) {
|
2012-04-11 00:00:26 +00:00
|
|
|
// A PHI may cause a copy to be inserted.
|
2014-03-17 19:36:09 +00:00
|
|
|
if (UseMI.isPHI()) {
|
2012-04-11 00:00:26 +00:00
|
|
|
// A PHI inside the loop causes a copy because the live range of Reg is
|
|
|
|
// extended across the PHI.
|
2014-03-17 19:36:09 +00:00
|
|
|
if (CurLoop->contains(&UseMI))
|
2012-04-11 00:00:26 +00: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-17 19:36:09 +00:00
|
|
|
if (isExitBlock(UseMI.getParent()))
|
2012-04-11 00:00:26 +00:00
|
|
|
return true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Look past copies as well.
|
2014-03-17 19:36:09 +00:00
|
|
|
if (UseMI.isCopy() && CurLoop->contains(&UseMI))
|
|
|
|
Work.push_back(&UseMI);
|
2012-04-11 00:00:26 +00:00
|
|
|
}
|
2011-04-11 21:09:18 +00:00
|
|
|
}
|
2012-04-11 00:00:26 +00:00
|
|
|
} while (!Work.empty());
|
2009-02-05 08:45:46 +00:00
|
|
|
return false;
|
2009-02-04 09:19:56 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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-19 18:58:51 +00:00
|
|
|
bool MachineLICM::HasHighOperandLatency(MachineInstr &MI,
|
2010-10-26 02:08:50 +00:00
|
|
|
unsigned DefIdx, unsigned Reg) const {
|
2015-06-13 03:42:11 +00:00
|
|
|
if (MRI->use_nodbg_empty(Reg))
|
2010-10-19 18:58:51 +00:00
|
|
|
return false;
|
2010-10-14 01:16:09 +00:00
|
|
|
|
2014-03-17 19:36:09 +00:00
|
|
|
for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) {
|
|
|
|
if (UseMI.isCopyLike())
|
2010-10-26 02:08:50 +00:00
|
|
|
continue;
|
2014-03-17 19:36:09 +00:00
|
|
|
if (!CurLoop->contains(UseMI.getParent()))
|
2010-10-14 01:16:09 +00:00
|
|
|
continue;
|
2014-03-17 19:36:09 +00:00
|
|
|
for (unsigned i = 0, e = UseMI.getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = UseMI.getOperand(i);
|
2010-10-14 01:16:09 +00:00
|
|
|
if (!MO.isReg() || !MO.isUse())
|
|
|
|
continue;
|
|
|
|
unsigned MOReg = MO.getReg();
|
|
|
|
if (MOReg != Reg)
|
|
|
|
continue;
|
|
|
|
|
2016-06-30 00:01:54 +00:00
|
|
|
if (TII->hasHighOperandLatency(SchedModel, MRI, MI, DefIdx, UseMI, i))
|
2010-10-19 18:58:51 +00:00
|
|
|
return true;
|
2010-10-14 01:16:09 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 18:58:51 +00:00
|
|
|
// Only look at the first in loop use.
|
|
|
|
break;
|
2010-10-14 01:16:09 +00:00
|
|
|
}
|
|
|
|
|
2010-10-19 18:58:51 +00:00
|
|
|
return false;
|
2010-10-14 01:16:09 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Return true if the instruction is marked "cheap" or the operand latency
|
|
|
|
/// between its def and a use is one or less.
|
2010-10-26 02:08:50 +00:00
|
|
|
bool MachineLICM::IsCheapInstruction(MachineInstr &MI) const {
|
2016-06-30 00:01:54 +00:00
|
|
|
if (TII->isAsCheapAsAMove(MI) || MI.isCopyLike())
|
2010-10-26 02:08:50 +00: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;
|
|
|
|
|
2016-06-30 00:01:54 +00:00
|
|
|
if (!TII->hasLowDefLatency(SchedModel, MI, i))
|
2010-10-26 02:08:50 +00:00
|
|
|
return false;
|
|
|
|
isCheap = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isCheap;
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Visit BBs from header to current BB, check if hoisting an instruction of the
|
|
|
|
/// given cost matrix can cause high register pressure.
|
2015-04-03 16:19:48 +00:00
|
|
|
bool MachineLICM::CanCauseHighRegPressure(const DenseMap<unsigned, int>& Cost,
|
2012-04-11 00:00:28 +00:00
|
|
|
bool CheapInstr) {
|
2015-04-14 11:56:25 +00:00
|
|
|
for (const auto &RPIdAndCost : Cost) {
|
|
|
|
if (RPIdAndCost.second <= 0)
|
2010-10-20 22:03:58 +00:00
|
|
|
continue;
|
|
|
|
|
2015-04-14 11:56:25 +00:00
|
|
|
unsigned Class = RPIdAndCost.first;
|
2015-04-03 16:19:48 +00:00
|
|
|
int Limit = RegLimit[Class];
|
2012-04-11 00:00:28 +00:00
|
|
|
|
|
|
|
// Don't hoist cheap instructions if they would increase register pressure,
|
|
|
|
// even if we're under the limit.
|
2015-01-08 22:10:48 +00:00
|
|
|
if (CheapInstr && !HoistCheapInsts)
|
2012-04-11 00:00:28 +00:00
|
|
|
return true;
|
|
|
|
|
2015-04-03 16:19:48 +00:00
|
|
|
for (const auto &RP : BackTrace)
|
2015-04-14 11:56:25 +00:00
|
|
|
if (static_cast<int>(RP[Class]) + RPIdAndCost.second >= Limit)
|
2010-10-16 02:20:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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.
|
2010-10-20 22:03:58 +00:00
|
|
|
void MachineLICM::UpdateBackTraceRegPressure(const MachineInstr *MI) {
|
|
|
|
// First compute the 'cost' of the instruction, i.e. its contribution
|
|
|
|
// to register pressure.
|
2015-04-07 16:42:35 +00:00
|
|
|
auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/false,
|
|
|
|
/*ConsiderUnseenAsDef=*/false);
|
2010-10-20 22:03:58 +00:00
|
|
|
|
|
|
|
// Update register pressure of blocks from loop header to current block.
|
2015-04-07 16:42:35 +00:00
|
|
|
for (auto &RP : BackTrace)
|
2015-04-14 11:56:25 +00:00
|
|
|
for (const auto &RPIdAndCost : Cost)
|
|
|
|
RP[RPIdAndCost.first] += RPIdAndCost.second;
|
2010-10-20 22:03:58 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Return true if it is potentially profitable to hoist the given loop
|
|
|
|
/// invariant.
|
2009-11-20 23:31:34 +00:00
|
|
|
bool MachineLICM::IsProfitableToHoist(MachineInstr &MI) {
|
2010-10-14 01:16:09 +00:00
|
|
|
if (MI.isImplicitDef())
|
|
|
|
return true;
|
|
|
|
|
2012-04-11 00:00:28 +00: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 01:45:00 +00:00
|
|
|
|
2012-04-11 00:00:28 +00:00
|
|
|
// Rematerializable instructions should always be hoisted since the register
|
|
|
|
// allocator can just pull them down again when needed.
|
2016-06-30 00:01:54 +00:00
|
|
|
if (TII->isTriviallyReMaterializable(MI, AA))
|
2012-04-11 00:00:28 +00:00
|
|
|
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-07 16:42:35 +00:00
|
|
|
if (MO.isDef() && HasHighOperandLatency(MI, i, Reg)) {
|
|
|
|
DEBUG(dbgs() << "Hoist High Latency: " << MI);
|
|
|
|
++NumHighLatency;
|
|
|
|
return true;
|
2010-10-14 01:16:09 +00:00
|
|
|
}
|
2012-04-11 00:00:28 +00:00
|
|
|
}
|
2010-10-14 01:16:09 +00:00
|
|
|
|
2015-04-07 16:42:35 +00: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 00:00:28 +00: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 01:16:09 +00:00
|
|
|
|
2012-04-11 00:00:28 +00: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-12 21:33:49 +00:00
|
|
|
|
2012-04-11 00:00:28 +00: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-20 19:55:37 +00:00
|
|
|
}
|
2009-02-04 09:19:56 +00:00
|
|
|
|
2012-04-11 00:00:28 +00:00
|
|
|
// High register pressure situation, only hoist if the instruction is going
|
|
|
|
// to be remat'ed.
|
2016-09-10 01:03:20 +00:00
|
|
|
if (!TII->isTriviallyReMaterializable(MI, AA) &&
|
|
|
|
!MI.isDereferenceableInvariantLoad(AA)) {
|
2012-04-11 00:00:28 +00:00
|
|
|
DEBUG(dbgs() << "Can't remat / high reg-pressure: " << MI);
|
2012-04-11 00:00:26 +00:00
|
|
|
return false;
|
2012-04-11 00:00:28 +00:00
|
|
|
}
|
2009-02-05 08:45:46 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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.
|
2009-10-29 17:47:20 +00:00
|
|
|
MachineInstr *MachineLICM::ExtractHoistableLoad(MachineInstr *MI) {
|
2010-10-08 18:59:19 +00:00
|
|
|
// Don't unfold simple loads.
|
2011-12-07 07:15:52 +00:00
|
|
|
if (MI->canFoldAsLoad())
|
2014-04-14 00:51:57 +00:00
|
|
|
return nullptr;
|
2010-10-08 18:59:19 +00:00
|
|
|
|
2009-10-29 17:47:20 +00: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.
|
2016-09-10 01:03:20 +00:00
|
|
|
if (!MI->isDereferenceableInvariantLoad(AA))
|
2014-04-14 00:51:57 +00:00
|
|
|
return nullptr;
|
2009-11-20 19:55:37 +00:00
|
|
|
|
2009-10-29 17:47:20 +00:00
|
|
|
// Next determine the register class for a temporary register.
|
2009-10-30 22:18:41 +00:00
|
|
|
unsigned LoadRegIndex;
|
2009-10-29 17:47:20 +00:00
|
|
|
unsigned NewOpc =
|
|
|
|
TII->getOpcodeAfterMemoryUnfold(MI->getOpcode(),
|
|
|
|
/*UnfoldLoad=*/true,
|
2009-10-30 22:18:41 +00:00
|
|
|
/*UnfoldStore=*/false,
|
|
|
|
&LoadRegIndex);
|
2014-04-14 00:51:57 +00:00
|
|
|
if (NewOpc == 0) return nullptr;
|
2011-06-28 19:10:37 +00:00
|
|
|
const MCInstrDesc &MID = TII->get(NewOpc);
|
2012-05-07 22:10:26 +00:00
|
|
|
MachineFunction &MF = *MI->getParent()->getParent();
|
|
|
|
const TargetRegisterClass *RC = TII->getRegClass(MID, LoadRegIndex, TRI, MF);
|
2009-10-29 17:47:20 +00:00
|
|
|
// Ok, we're unfolding. Create a temporary register and do the unfold.
|
2010-10-14 01:16:09 +00:00
|
|
|
unsigned Reg = MRI->createVirtualRegister(RC);
|
2009-11-20 19:55:37 +00:00
|
|
|
|
2009-10-29 17:47:20 +00:00
|
|
|
SmallVector<MachineInstr *, 2> NewMIs;
|
2016-06-30 00:01:54 +00:00
|
|
|
bool Success = TII->unfoldMemoryOperand(MF, *MI, Reg,
|
|
|
|
/*UnfoldLoad=*/true,
|
|
|
|
/*UnfoldStore=*/false, NewMIs);
|
2009-10-29 17:47:20 +00:00
|
|
|
(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-06 22:12:01 +00:00
|
|
|
MachineBasicBlock::iterator Pos = MI;
|
|
|
|
MBB->insert(Pos, NewMIs[0]);
|
|
|
|
MBB->insert(Pos, NewMIs[1]);
|
2009-10-29 17:47:20 +00:00
|
|
|
// If unfolding produced a load that wasn't loop-invariant or profitable to
|
|
|
|
// hoist, discard the new instructions and bail.
|
2009-11-20 23:31:34 +00:00
|
|
|
if (!IsLoopInvariantInst(*NewMIs[0]) || !IsProfitableToHoist(*NewMIs[0])) {
|
2009-10-29 17:47:20 +00:00
|
|
|
NewMIs[0]->eraseFromParent();
|
|
|
|
NewMIs[1]->eraseFromParent();
|
2014-04-14 00:51:57 +00:00
|
|
|
return nullptr;
|
2009-10-29 17:47:20 +00:00
|
|
|
}
|
2010-10-20 22:03:58 +00:00
|
|
|
|
|
|
|
// Update register pressure for the unfolded instruction.
|
|
|
|
UpdateRegPressure(NewMIs[1]);
|
|
|
|
|
2009-10-29 17:47:20 +00:00
|
|
|
// Otherwise we successfully unfolded a load that we can hoist.
|
|
|
|
MI->eraseFromParent();
|
|
|
|
return NewMIs[0];
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// 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.
|
2009-11-03 21:40:02 +00:00
|
|
|
void MachineLICM::InitCSEMap(MachineBasicBlock *BB) {
|
2016-01-06 23:45:05 +00:00
|
|
|
for (MachineInstr &MI : *BB)
|
|
|
|
CSEMap[MI.getOpcode()].push_back(&MI);
|
2009-11-03 21:40:02 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Find an instruction amount PrevMIs that is a duplicate of MI.
|
|
|
|
/// Return this instruction if it's found.
|
2009-11-07 03:52:02 +00:00
|
|
|
const MachineInstr*
|
|
|
|
MachineLICM::LookForDuplicate(const MachineInstr *MI,
|
|
|
|
std::vector<const MachineInstr*> &PrevMIs) {
|
2016-01-06 23:45:05 +00:00
|
|
|
for (const MachineInstr *PrevMI : PrevMIs)
|
2016-06-30 00:01:54 +00:00
|
|
|
if (TII->produceSameValue(*MI, *PrevMI, (PreRegAlloc ? MRI : nullptr)))
|
2009-11-05 00:51:13 +00:00
|
|
|
return PrevMI;
|
2016-01-06 23:45:05 +00:00
|
|
|
|
2014-04-14 00:51:57 +00:00
|
|
|
return nullptr;
|
2009-11-05 00:51:13 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Given a LICM'ed instruction, look for an instruction on the preheader that
|
|
|
|
/// computes 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.
|
2009-11-05 00:51:13 +00:00
|
|
|
bool MachineLICM::EliminateCSE(MachineInstr *MI,
|
|
|
|
DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator &CI) {
|
2010-07-14 01:22:19 +00: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 03:52:02 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (const MachineInstr *Dup = LookForDuplicate(MI, CI->second)) {
|
2010-01-05 00:03:48 +00:00
|
|
|
DEBUG(dbgs() << "CSEing " << *MI << " with " << *Dup);
|
2010-02-28 01:33:43 +00:00
|
|
|
|
|
|
|
// Replace virtual registers defined by MI by their counterparts defined
|
|
|
|
// by Dup.
|
2011-10-17 19:50:12 +00:00
|
|
|
SmallVector<unsigned, 2> Defs;
|
2009-11-07 03:52:02 +00:00
|
|
|
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2010-02-28 01:33:43 +00: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-17 19:50:12 +00: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-13 20:34:42 +00:00
|
|
|
}
|
2009-11-05 00:51:13 +00:00
|
|
|
}
|
2011-10-17 19:50:12 +00:00
|
|
|
|
2016-01-06 23:45:05 +00:00
|
|
|
for (unsigned Idx : Defs) {
|
2011-10-17 19:50:12 +00:00
|
|
|
unsigned Reg = MI->getOperand(Idx).getReg();
|
|
|
|
unsigned DupReg = Dup->getOperand(Idx).getReg();
|
|
|
|
MRI->replaceRegWith(Reg, DupReg);
|
|
|
|
MRI->clearKillFlags(DupReg);
|
|
|
|
}
|
|
|
|
|
2009-11-07 03:52:02 +00:00
|
|
|
MI->eraseFromParent();
|
|
|
|
++NumCSEed;
|
|
|
|
return true;
|
2009-11-05 00:51:13 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Return true if the given instruction will be CSE'd if it's hoisted out of
|
|
|
|
/// the loop.
|
2011-10-12 00:09:14 +00:00
|
|
|
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 00:51:57 +00:00
|
|
|
return LookForDuplicate(MI, CI->second) != nullptr;
|
2011-10-12 00:09:14 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// When an instruction is found to use only loop invariant operands
|
2008-05-12 19:38:32 +00:00
|
|
|
/// that are safe to hoist, this instruction is called to do the dirty work.
|
2015-12-10 16:34:21 +00:00
|
|
|
/// It returns true if the instruction is hoisted.
|
2010-10-20 22:03:58 +00:00
|
|
|
bool MachineLICM::Hoist(MachineInstr *MI, MachineBasicBlock *Preheader) {
|
2009-10-28 03:21:57 +00:00
|
|
|
// First check whether we should hoist this instruction.
|
2009-11-20 23:31:34 +00:00
|
|
|
if (!IsLoopInvariantInst(*MI) || !IsProfitableToHoist(*MI)) {
|
2009-10-29 17:47:20 +00:00
|
|
|
// If not, try unfolding a hoistable load.
|
|
|
|
MI = ExtractHoistableLoad(MI);
|
2010-10-20 22:03:58 +00:00
|
|
|
if (!MI) return false;
|
2009-10-28 03:21:57 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
|
2009-01-15 22:01:38 +00:00
|
|
|
// Now move the instructions to the predecessor, inserting it before any
|
|
|
|
// terminator instructions.
|
|
|
|
DEBUG({
|
2010-01-05 00:03:48 +00:00
|
|
|
dbgs() << "Hoisting " << *MI;
|
2009-10-28 03:21:57 +00:00
|
|
|
if (MI->getParent()->getBasicBlock())
|
2016-05-23 18:56:07 +00:00
|
|
|
dbgs() << " from BB#" << MI->getParent()->getNumber();
|
|
|
|
if (Preheader->getBasicBlock())
|
|
|
|
dbgs() << " to BB#" << Preheader->getNumber();
|
2010-01-05 00:03:48 +00:00
|
|
|
dbgs() << "\n";
|
2009-01-15 22:01:38 +00:00
|
|
|
});
|
2007-12-08 01:47:01 +00:00
|
|
|
|
2009-11-03 21:40:02 +00:00
|
|
|
// If this is the first instruction being hoisted to the preheader,
|
|
|
|
// initialize the CSE map with potential common expressions.
|
2010-05-29 00:06:36 +00:00
|
|
|
if (FirstInLoop) {
|
2010-06-22 17:25:57 +00:00
|
|
|
InitCSEMap(Preheader);
|
2010-05-29 00:06:36 +00:00
|
|
|
FirstInLoop = false;
|
|
|
|
}
|
2009-11-03 21:40:02 +00:00
|
|
|
|
2009-02-05 08:45:46 +00:00
|
|
|
// Look for opportunity to CSE the hoisted instruction.
|
2009-11-03 21:40:02 +00:00
|
|
|
unsigned Opcode = MI->getOpcode();
|
|
|
|
DenseMap<unsigned, std::vector<const MachineInstr*> >::iterator
|
|
|
|
CI = CSEMap.find(Opcode);
|
2009-11-05 00:51:13 +00:00
|
|
|
if (!EliminateCSE(MI, CI)) {
|
|
|
|
// Otherwise, splice the instruction to the preheader.
|
2010-06-22 17:25:57 +00:00
|
|
|
Preheader->splice(Preheader->getFirstTerminator(),MI->getParent(),MI);
|
2009-11-03 21:40:02 +00:00
|
|
|
|
2010-10-20 22:03:58 +00:00
|
|
|
// Update register pressure for BBs from header to this block.
|
|
|
|
UpdateBackTraceRegPressure(MI);
|
|
|
|
|
2010-05-13 20:34:42 +00: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.
|
2016-01-06 23:45:05 +00:00
|
|
|
for (MachineOperand &MO : MI->operands())
|
2010-05-13 20:34:42 +00:00
|
|
|
if (MO.isReg() && MO.isDef() && !MO.isDead())
|
2010-10-14 01:16:09 +00:00
|
|
|
MRI->clearKillFlags(MO.getReg());
|
2010-05-13 20:34:42 +00:00
|
|
|
|
2009-02-05 08:45:46 +00:00
|
|
|
// Add to the CSE map.
|
|
|
|
if (CI != CSEMap.end())
|
2009-10-28 03:21:57 +00:00
|
|
|
CI->second.push_back(MI);
|
2014-10-03 18:33:16 +00:00
|
|
|
else
|
|
|
|
CSEMap[Opcode].push_back(MI);
|
2009-02-05 08:45:46 +00:00
|
|
|
}
|
2007-12-08 01:47:01 +00:00
|
|
|
|
2009-01-15 22:01:38 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
Changed = true;
|
2010-10-20 22:03:58 +00: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...
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44687 91177308-0d34-0410-b5e6-96231b3b80d8
2007-12-07 21:42:31 +00:00
|
|
|
}
|
2010-06-22 17:25:57 +00:00
|
|
|
|
2015-12-10 16:34:21 +00:00
|
|
|
/// Get the preheader for the current loop, splitting a critical edge if needed.
|
2010-06-22 17:25:57 +00: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 00:51:57 +00:00
|
|
|
return nullptr;
|
2010-06-22 17:25:57 +00:00
|
|
|
|
|
|
|
if (!CurPreheader) {
|
|
|
|
CurPreheader = CurLoop->getLoopPreheader();
|
|
|
|
if (!CurPreheader) {
|
|
|
|
MachineBasicBlock *Pred = CurLoop->getLoopPredecessor();
|
|
|
|
if (!Pred) {
|
|
|
|
CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
|
2014-04-14 00:51:57 +00:00
|
|
|
return nullptr;
|
2010-06-22 17:25:57 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 21:01:13 +00:00
|
|
|
CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), *this);
|
2010-06-22 17:25:57 +00:00
|
|
|
if (!CurPreheader) {
|
|
|
|
CurPreheader = reinterpret_cast<MachineBasicBlock *>(-1);
|
2014-04-14 00:51:57 +00:00
|
|
|
return nullptr;
|
2010-06-22 17:25:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CurPreheader;
|
|
|
|
}
|