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