2017-09-23 07:46:57 +08:00
|
|
|
//===- MachineLICM.cpp - Machine Loop Invariant Code Motion Pass ----------===//
|
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
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass performs loop invariant code motion on machine instructions. We
|
|
|
|
// attempt to remove as much code from the body of a loop as possible.
|
|
|
|
//
|
2009-01-16 06:01:38 +08:00
|
|
|
// This pass is not intended to be a replacement or a complete alternative
|
|
|
|
// for the LLVM-IR-level LICM pass. It is only designed to hoist simple
|
|
|
|
// constructs that are not exposed before lowering and instruction selection.
|
|
|
|
//
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-09-23 07:46:57 +08:00
|
|
|
#include "llvm/ADT/BitVector.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2017-09-23 07:46:57 +08:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2017-09-23 07:46:57 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2017-09-23 07:46:57 +08:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.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"
|
2017-09-23 07:46:57 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.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"
|
2017-09-23 07:46:57 +08:00
|
|
|
#include "llvm/CodeGen/MachineOperand.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"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetLowering.h"
|
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
2015-06-13 11:42:11 +08:00
|
|
|
#include "llvm/CodeGen/TargetSchedule.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2017-09-23 07:46:57 +08:00
|
|
|
#include "llvm/IR/DebugLoc.h"
|
|
|
|
#include "llvm/MC/MCInstrDesc.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/Casting.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"
|
2017-09-23 07:46:57 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <limits>
|
|
|
|
#include <vector>
|
|
|
|
|
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;
|
|
|
|
|
2017-05-26 05:26:32 +08:00
|
|
|
#define DEBUG_TYPE "machinelicm"
|
2014-04-22 10:02:50 +08:00
|
|
|
|
2011-10-13 05:33:49 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
AvoidSpeculation("avoid-speculation",
|
|
|
|
cl::desc("MachineLICM should avoid speculation"),
|
2011-10-26 09:26:57 +08:00
|
|
|
cl::init(true), cl::Hidden);
|
2011-10-13 05:33:49 +08:00
|
|
|
|
2015-01-09 06:10:48 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
HoistCheapInsts("hoist-cheap-insts",
|
|
|
|
cl::desc("MachineLICM should hoist even cheap instructions"),
|
|
|
|
cl::init(false), cl::Hidden);
|
|
|
|
|
2015-03-14 18:58:38 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
SinkInstsToAvoidSpills("sink-insts-to-avoid-spills",
|
|
|
|
cl::desc("MachineLICM should sink instructions into "
|
|
|
|
"loops to avoid register spills"),
|
|
|
|
cl::init(false), cl::Hidden);
|
2018-03-23 23:28:15 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
HoistConstStores("hoist-const-stores",
|
|
|
|
cl::desc("Hoist invariant stores"),
|
2018-04-09 22:50:02 +08:00
|
|
|
cl::init(true), cl::Hidden);
|
2015-03-14 18:58:38 +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");
|
2018-03-23 23:28:15 +08:00
|
|
|
STATISTIC(NumStoreConst,
|
|
|
|
"Number of stores of const phys reg hoisted out of loops");
|
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 {
|
2017-09-23 07:46:57 +08:00
|
|
|
|
2018-01-19 14:46:10 +08:00
|
|
|
class MachineLICMBase : public MachineFunctionPass {
|
2007-12-12 07:27:51 +08:00
|
|
|
const TargetInstrInfo *TII;
|
2013-01-12 04:05:37 +08:00
|
|
|
const TargetLoweringBase *TLI;
|
2009-09-26 07:58:45 +08:00
|
|
|
const TargetRegisterInfo *TRI;
|
2010-04-07 08:41:17 +08:00
|
|
|
const MachineFrameInfo *MFI;
|
2010-10-14 09:16:09 +08:00
|
|
|
MachineRegisterInfo *MRI;
|
2015-06-13 11:42:11 +08:00
|
|
|
TargetSchedModel SchedModel;
|
2018-01-19 14:46:10 +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.
|
2017-09-23 07:46:57 +08:00
|
|
|
SmallVector<MachineBasicBlock *, 8> ExitBlocks;
|
2012-04-11 08:00:26 +08:00
|
|
|
|
|
|
|
bool isExitBlock(const MachineBasicBlock *MBB) const {
|
2016-08-12 06:21:41 +08:00
|
|
|
return is_contained(ExitBlocks, MBB);
|
2012-04-11 08:00:26 +08:00
|
|
|
}
|
|
|
|
|
2010-10-14 09:16:09 +08:00
|
|
|
// Track 'estimated' register pressure.
|
2010-10-16 10:20:26 +08:00
|
|
|
SmallSet<unsigned, 32> RegSeen;
|
2010-10-14 09:16:09 +08:00
|
|
|
SmallVector<unsigned, 8> RegPressure;
|
2010-10-16 10:20:26 +08:00
|
|
|
|
2015-04-14 19:56:25 +08:00
|
|
|
// Register pressure "limit" per register pressure set. If the pressure
|
2010-10-16 10:20:26 +08:00
|
|
|
// is higher than the limit, then it's considered high.
|
2010-10-14 09:16:09 +08:00
|
|
|
SmallVector<unsigned, 8> RegLimit;
|
|
|
|
|
2010-10-16 10:20:26 +08:00
|
|
|
// Register pressure on path leading from loop preheader to current BB.
|
|
|
|
SmallVector<SmallVector<unsigned, 8>, 16> BackTrace;
|
|
|
|
|
2010-07-30 01:45:24 +08:00
|
|
|
// For each opcode, keep a list of potential CSE instructions.
|
2017-09-23 07:46:57 +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:
|
2018-01-19 14:46:10 +08:00
|
|
|
MachineLICMBase(char &PassID, bool PreRegAlloc)
|
|
|
|
: MachineFunctionPass(PassID), PreRegAlloc(PreRegAlloc) {}
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
AU.addRequired<MachineLoopInfo>();
|
|
|
|
AU.addRequired<MachineDominatorTree>();
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
AU.addRequired<AAResultsWrapperPass>();
|
2008-01-04 16:48:49 +08:00
|
|
|
AU.addPreserved<MachineLoopInfo>();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
}
|
2009-02-05 16:45:46 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
void releaseMemory() override {
|
2010-10-16 10:20:26 +08:00
|
|
|
RegSeen.clear();
|
2010-10-14 09:16:09 +08:00
|
|
|
RegPressure.clear();
|
|
|
|
RegLimit.clear();
|
2010-10-20 02:58:51 +08:00
|
|
|
BackTrace.clear();
|
2009-02-05 16:45:46 +08:00
|
|
|
CSEMap.clear();
|
|
|
|
}
|
|
|
|
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
private:
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Keep track of information about hoisting candidates.
|
2010-04-08 09:03:47 +08:00
|
|
|
struct CandidateInfo {
|
|
|
|
MachineInstr *MI;
|
|
|
|
unsigned Def;
|
2010-04-14 02:16:00 +08:00
|
|
|
int FI;
|
2017-09-23 07:46:57 +08:00
|
|
|
|
2010-04-14 02:16:00 +08:00
|
|
|
CandidateInfo(MachineInstr *mi, unsigned def, int fi)
|
|
|
|
: MI(mi), Def(def), FI(fi) {}
|
2010-04-08 09:03:47 +08:00
|
|
|
};
|
|
|
|
|
2010-04-17 15:07:11 +08:00
|
|
|
void HoistRegionPostRA();
|
2010-04-08 09:03:47 +08:00
|
|
|
|
|
|
|
void HoistPostRA(MachineInstr *MI, unsigned Def);
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
void ProcessMI(MachineInstr *MI, BitVector &PhysRegDefs,
|
|
|
|
BitVector &PhysRegClobbers, 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
|
|
|
void AddToLiveIns(unsigned Reg);
|
2010-04-08 09:03:47 +08:00
|
|
|
|
2010-04-14 02:16:00 +08:00
|
|
|
bool IsLICMCandidate(MachineInstr &I);
|
|
|
|
|
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
|
|
|
bool HasLoopPHIUse(const MachineInstr *MI) const;
|
2011-04-12 05:09:18 +08:00
|
|
|
|
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
|
|
|
|
2015-04-04 00:19:48 +08:00
|
|
|
bool CanCauseHighRegPressure(const DenseMap<unsigned, int> &Cost,
|
|
|
|
bool Cheap);
|
2010-10-21 06:03:58 +08:00
|
|
|
|
|
|
|
void UpdateBackTraceRegPressure(const MachineInstr *MI);
|
2010-10-16 10:20:26 +08:00
|
|
|
|
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
|
|
|
bool IsGuaranteedToExecute(MachineBasicBlock *BB);
|
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
void EnterScope(MachineBasicBlock *MBB);
|
|
|
|
|
|
|
|
void ExitScope(MachineBasicBlock *MBB);
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
void ExitScopeIfDone(
|
|
|
|
MachineDomTreeNode *Node,
|
|
|
|
DenseMap<MachineDomTreeNode *, unsigned> &OpenChildren,
|
|
|
|
DenseMap<MachineDomTreeNode *, MachineDomTreeNode *> &ParentMap);
|
|
|
|
|
2018-07-17 02:51:40 +08:00
|
|
|
void HoistOutOfLoop(MachineDomTreeNode *HeaderN);
|
2015-12-11 00:34:21 +08:00
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
void HoistRegion(MachineDomTreeNode *N, bool IsHeader);
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2015-03-14 18:58:38 +08:00
|
|
|
void SinkIntoLoop();
|
|
|
|
|
2010-10-14 09:16:09 +08:00
|
|
|
void InitRegPressure(MachineBasicBlock *BB);
|
|
|
|
|
2015-04-08 00:42:35 +08:00
|
|
|
DenseMap<unsigned, int> calcRegisterCost(const MachineInstr *MI,
|
|
|
|
bool ConsiderSeen,
|
|
|
|
bool ConsiderUnseenAsDef);
|
|
|
|
|
|
|
|
void UpdateRegPressure(const MachineInstr *MI,
|
|
|
|
bool ConsiderUnseenAsDef = false);
|
2010-10-14 09:16:09 +08:00
|
|
|
|
2009-10-30 01:47:20 +08:00
|
|
|
MachineInstr *ExtractHoistableLoad(MachineInstr *MI);
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
const MachineInstr *
|
|
|
|
LookForDuplicate(const MachineInstr *MI,
|
|
|
|
std::vector<const MachineInstr *> &PrevMIs);
|
2009-11-07 11:52:02 +08:00
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
bool EliminateCSE(
|
|
|
|
MachineInstr *MI,
|
|
|
|
DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator &CI);
|
2009-11-05 08:51:13 +08:00
|
|
|
|
2011-10-12 08:09:14 +08:00
|
|
|
bool MayCSE(MachineInstr *MI);
|
|
|
|
|
2010-10-21 06:03:58 +08:00
|
|
|
bool Hoist(MachineInstr *MI, MachineBasicBlock *Preheader);
|
2009-11-04 05:40:02 +08:00
|
|
|
|
|
|
|
void InitCSEMap(MachineBasicBlock *BB);
|
2010-06-23 01:25:57 +08:00
|
|
|
|
|
|
|
MachineBasicBlock *getCurPreheader();
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
};
|
2017-09-23 07:46:57 +08:00
|
|
|
|
2018-01-19 14:46:10 +08:00
|
|
|
class MachineLICM : public MachineLICMBase {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
MachineLICM() : MachineLICMBase(ID, false) {
|
|
|
|
initializeMachineLICMPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class EarlyMachineLICM : public MachineLICMBase {
|
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
EarlyMachineLICM() : MachineLICMBase(ID, true) {
|
|
|
|
initializeEarlyMachineLICMPass(*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
|
|
|
} // end anonymous namespace
|
|
|
|
|
2018-01-19 14:46:10 +08:00
|
|
|
char MachineLICM::ID;
|
|
|
|
char EarlyMachineLICM::ID;
|
2017-09-23 07:46:57 +08:00
|
|
|
|
2012-02-09 05:23:13 +08:00
|
|
|
char &llvm::MachineLICMID = MachineLICM::ID;
|
2018-01-19 14:46:10 +08:00
|
|
|
char &llvm::EarlyMachineLICMID = EarlyMachineLICM::ID;
|
2017-09-23 07:46:57 +08:00
|
|
|
|
2017-05-26 05:26:32 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(MachineLICM, DEBUG_TYPE,
|
|
|
|
"Machine Loop Invariant Code Motion", false, false)
|
2010-10-13 03:48:12 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
|
2017-05-26 05:26:32 +08:00
|
|
|
INITIALIZE_PASS_END(MachineLICM, DEBUG_TYPE,
|
|
|
|
"Machine Loop Invariant Code Motion", false, false)
|
2008-05-13 08:00:25 +08:00
|
|
|
|
2018-01-19 14:46:10 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(EarlyMachineLICM, "early-machinelicm",
|
|
|
|
"Early Machine Loop Invariant Code Motion", false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(AAResultsWrapperPass)
|
|
|
|
INITIALIZE_PASS_END(EarlyMachineLICM, "early-machinelicm",
|
|
|
|
"Early Machine Loop Invariant Code Motion", false, false)
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Test if the given loop is the outer-most loop that has a unique predecessor.
|
2010-06-23 01:25:57 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-01-19 14:46:10 +08:00
|
|
|
bool MachineLICMBase::runOnMachineFunction(MachineFunction &MF) {
|
2017-12-16 06:22:58 +08:00
|
|
|
if (skipFunction(MF.getFunction()))
|
2014-04-01 01:43:35 +08:00
|
|
|
return false;
|
|
|
|
|
2010-05-29 08:06:36 +08:00
|
|
|
Changed = FirstInLoop = false;
|
2015-06-13 11:42:11 +08:00
|
|
|
const TargetSubtargetInfo &ST = MF.getSubtarget();
|
|
|
|
TII = ST.getInstrInfo();
|
|
|
|
TLI = ST.getTargetLowering();
|
|
|
|
TRI = ST.getRegisterInfo();
|
2016-07-29 02:40:00 +08:00
|
|
|
MFI = &MF.getFrameInfo();
|
2010-10-14 09:16:09 +08:00
|
|
|
MRI = &MF.getRegInfo();
|
2018-04-09 03:56:04 +08:00
|
|
|
SchedModel.init(&ST);
|
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)
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "******** Pre-regalloc Machine LICM: ");
|
2012-02-11 08:40:36 +08:00
|
|
|
else
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "******** Post-regalloc Machine LICM: ");
|
|
|
|
LLVM_DEBUG(dbgs() << MF.getName() << " ********\n");
|
2012-02-11 08:40:36 +08:00
|
|
|
|
2010-10-14 09:16:09 +08:00
|
|
|
if (PreRegAlloc) {
|
|
|
|
// Estimate register pressure during pre-regalloc pass.
|
2015-04-14 19:56:25 +08:00
|
|
|
unsigned NumRPS = TRI->getNumRegPressureSets();
|
|
|
|
RegPressure.resize(NumRPS);
|
2010-10-14 09:16:09 +08:00
|
|
|
std::fill(RegPressure.begin(), RegPressure.end(), 0);
|
2015-04-14 19:56:25 +08:00
|
|
|
RegLimit.resize(NumRPS);
|
|
|
|
for (unsigned i = 0, e = NumRPS; i != e; ++i)
|
|
|
|
RegLimit[i] = TRI->getRegPressureSetLimit(MF, i);
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
|
|
|
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
// Get our Loop information...
|
2010-04-08 09:03:47 +08:00
|
|
|
MLI = &getAnalysis<MachineLoopInfo>();
|
|
|
|
DT = &getAnalysis<MachineDominatorTree>();
|
[PM/AA] Rebuild LLVM's alias analysis infrastructure in a way compatible
with the new pass manager, and no longer relying on analysis groups.
This builds essentially a ground-up new AA infrastructure stack for
LLVM. The core ideas are the same that are used throughout the new pass
manager: type erased polymorphism and direct composition. The design is
as follows:
- FunctionAAResults is a type-erasing alias analysis results aggregation
interface to walk a single query across a range of results from
different alias analyses. Currently this is function-specific as we
always assume that aliasing queries are *within* a function.
- AAResultBase is a CRTP utility providing stub implementations of
various parts of the alias analysis result concept, notably in several
cases in terms of other more general parts of the interface. This can
be used to implement only a narrow part of the interface rather than
the entire interface. This isn't really ideal, this logic should be
hoisted into FunctionAAResults as currently it will cause
a significant amount of redundant work, but it faithfully models the
behavior of the prior infrastructure.
- All the alias analysis passes are ported to be wrapper passes for the
legacy PM and new-style analysis passes for the new PM with a shared
result object. In some cases (most notably CFL), this is an extremely
naive approach that we should revisit when we can specialize for the
new pass manager.
- BasicAA has been restructured to reflect that it is much more
fundamentally a function analysis because it uses dominator trees and
loop info that need to be constructed for each function.
All of the references to getting alias analysis results have been
updated to use the new aggregation interface. All the preservation and
other pass management code has been updated accordingly.
The way the FunctionAAResultsWrapperPass works is to detect the
available alias analyses when run, and add them to the results object.
This means that we should be able to continue to respect when various
passes are added to the pipeline, for example adding CFL or adding TBAA
passes should just cause their results to be available and to get folded
into this. The exception to this rule is BasicAA which really needs to
be a function pass due to using dominator trees and loop info. As
a consequence, the FunctionAAResultsWrapperPass directly depends on
BasicAA and always includes it in the aggregation.
This has significant implications for preserving analyses. Generally,
most passes shouldn't bother preserving FunctionAAResultsWrapperPass
because rebuilding the results just updates the set of known AA passes.
The exception to this rule are LoopPass instances which need to preserve
all the function analyses that the loop pass manager will end up
needing. This means preserving both BasicAAWrapperPass and the
aggregating FunctionAAResultsWrapperPass.
Now, when preserving an alias analysis, you do so by directly preserving
that analysis. This is only necessary for non-immutable-pass-provided
alias analyses though, and there are only three of interest: BasicAA,
GlobalsAA (formerly GlobalsModRef), and SCEVAA. Usually BasicAA is
preserved when needed because it (like DominatorTree and LoopInfo) is
marked as a CFG-only pass. I've expanded GlobalsAA into the preserved
set everywhere we previously were preserving all of AliasAnalysis, and
I've added SCEVAA in the intersection of that with where we preserve
SCEV itself.
One significant challenge to all of this is that the CGSCC passes were
actually using the alias analysis implementations by taking advantage of
a pretty amazing set of loop holes in the old pass manager's analysis
management code which allowed analysis groups to slide through in many
cases. Moving away from analysis groups makes this problem much more
obvious. To fix it, I've leveraged the flexibility the design of the new
PM components provides to just directly construct the relevant alias
analyses for the relevant functions in the IPO passes that need them.
This is a bit hacky, but should go away with the new pass manager, and
is already in many ways cleaner than the prior state.
Another significant challenge is that various facilities of the old
alias analysis infrastructure just don't fit any more. The most
significant of these is the alias analysis 'counter' pass. That pass
relied on the ability to snoop on AA queries at different points in the
analysis group chain. Instead, I'm planning to build printing
functionality directly into the aggregation layer. I've not included
that in this patch merely to keep it smaller.
Note that all of this needs a nearly complete rewrite of the AA
documentation. I'm planning to do that, but I'd like to make sure the
new design settles, and to flesh out a bit more of what it looks like in
the new pass manager first.
Differential Revision: http://reviews.llvm.org/D12080
llvm-svn: 247167
2015-09-10 01:55:00 +08:00
|
|
|
AA = &getAnalysis<AAResultsWrapperPass>().getAAResults();
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2010-07-10 02:49:45 +08:00
|
|
|
SmallVector<MachineLoop *, 8> Worklist(MLI->begin(), MLI->end());
|
|
|
|
while (!Worklist.empty()) {
|
|
|
|
CurLoop = Worklist.pop_back_val();
|
2014-04-14 08:51:57 +08:00
|
|
|
CurPreheader = nullptr;
|
2012-04-11 08:00:26 +08:00
|
|
|
ExitBlocks.clear();
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2010-04-08 09:03:47 +08:00
|
|
|
// If this is done before regalloc, only visit outer-most preheader-sporting
|
|
|
|
// loops.
|
2010-07-10 02:49:45 +08:00
|
|
|
if (PreRegAlloc && !LoopIsOuterMostWithPredecessor(CurLoop)) {
|
|
|
|
Worklist.append(CurLoop->begin(), CurLoop->end());
|
2009-01-16 06:01:38 +08:00
|
|
|
continue;
|
2010-07-10 02:49:45 +08:00
|
|
|
}
|
2009-01-16 06:01:38 +08:00
|
|
|
|
2012-04-11 08:00:26 +08:00
|
|
|
CurLoop->getExitBlocks(ExitBlocks);
|
|
|
|
|
2010-04-07 08:41:17 +08:00
|
|
|
if (!PreRegAlloc)
|
2010-04-17 15:07:11 +08:00
|
|
|
HoistRegionPostRA();
|
2010-04-07 08:41:17 +08:00
|
|
|
else {
|
2010-04-17 15:07:11 +08:00
|
|
|
// CSEMap is initialized for loop header when the first instruction is
|
|
|
|
// being hoisted.
|
|
|
|
MachineDomTreeNode *N = DT->getNode(CurLoop->getHeader());
|
2010-05-29 08:06:36 +08:00
|
|
|
FirstInLoop = true;
|
2011-12-22 10:05:40 +08:00
|
|
|
HoistOutOfLoop(N);
|
2010-04-07 08:41:17 +08:00
|
|
|
CSEMap.clear();
|
2015-03-14 18:58:38 +08:00
|
|
|
|
|
|
|
if (SinkInstsToAvoidSpills)
|
|
|
|
SinkIntoLoop();
|
2010-04-07 08:41:17 +08:00
|
|
|
}
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Return true if instruction stores to the specified frame.
|
2010-04-08 09:03:47 +08:00
|
|
|
static bool InstructionStoresToFI(const MachineInstr *MI, int FI) {
|
2018-05-05 03:25:09 +08:00
|
|
|
// Check mayStore before memory operands so that e.g. DBG_VALUEs will return
|
|
|
|
// true since they have no memory operands.
|
|
|
|
if (!MI->mayStore())
|
|
|
|
return false;
|
2015-12-24 01:05:57 +08:00
|
|
|
// If we lost memory operands, conservatively assume that the instruction
|
2017-04-26 13:27:20 +08:00
|
|
|
// writes to all slots.
|
2015-12-24 01:05:57 +08:00
|
|
|
if (MI->memoperands_empty())
|
|
|
|
return true;
|
2016-01-07 07:45:05 +08:00
|
|
|
for (const MachineMemOperand *MemOp : MI->memoperands()) {
|
|
|
|
if (!MemOp->isStore() || !MemOp->getPseudoValue())
|
2010-04-08 09:03:47 +08:00
|
|
|
continue;
|
|
|
|
if (const FixedStackPseudoSourceValue *Value =
|
2016-01-07 07:45:05 +08:00
|
|
|
dyn_cast<FixedStackPseudoSourceValue>(MemOp->getPseudoValue())) {
|
2010-04-08 09:03:47 +08:00
|
|
|
if (Value->getFrameIndex() == FI)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Examine the instruction for potentai LICM candidate. Also
|
2010-04-08 09:03:47 +08:00
|
|
|
/// gather register def and frame object update information.
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::ProcessMI(MachineInstr *MI,
|
|
|
|
BitVector &PhysRegDefs,
|
|
|
|
BitVector &PhysRegClobbers,
|
|
|
|
SmallSet<int, 32> &StoredFIs,
|
|
|
|
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;
|
2016-01-07 07:45:05 +08:00
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
2010-04-08 09:03:47 +08:00
|
|
|
if (MO.isFI()) {
|
|
|
|
// Remember if the instruction stores to the frame index.
|
|
|
|
int FI = MO.getIndex();
|
|
|
|
if (!StoredFIs.count(FI) &&
|
|
|
|
MFI->isSpillSlotObjectIndex(FI) &&
|
|
|
|
InstructionStoresToFI(MI, FI))
|
|
|
|
StoredFIs.insert(FI);
|
2010-04-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;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2010-04-08 09:03:47 +08:00
|
|
|
if (!Reg)
|
|
|
|
continue;
|
2019-08-02 07:27:28 +08:00
|
|
|
assert(Register::isPhysicalRegister(Reg) &&
|
2010-04-08 09:03:47 +08:00
|
|
|
"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);
|
2012-01-21 06:27:12 +08:00
|
|
|
}
|
2018-12-05 11:41:26 +08:00
|
|
|
// Need a second loop because MCRegAliasIterator can visit the same
|
|
|
|
// register twice.
|
|
|
|
for (MCRegAliasIterator AS(Reg, TRI, true); AS.isValid(); ++AS)
|
|
|
|
PhysRegDefs.set(*AS);
|
|
|
|
|
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) {
|
2017-09-23 07:46:57 +08:00
|
|
|
int FI = std::numeric_limits<int>::min();
|
2010-04-14 04:21:05 +08:00
|
|
|
if ((!HasNonInvariantUse && IsLICMCandidate(*MI)) ||
|
2016-06-30 08:01:54 +08:00
|
|
|
(TII->isLoadFromStackSlot(*MI, FI) && MFI->isSpillSlotObjectIndex(FI)))
|
2010-04-14 02:16:00 +08:00
|
|
|
Candidates.push_back(CandidateInfo(MI, Def, FI));
|
2010-04-08 09:03:47 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Walk the specified region of the CFG and hoist loop invariants out to the
|
|
|
|
/// preheader.
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::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.
|
2018-09-10 20:32:06 +08:00
|
|
|
for (MachineBasicBlock *BB : CurLoop->getBlocks()) {
|
2011-10-12 10:58:01 +08:00
|
|
|
// If the header of the loop containing this basic block is a landing pad,
|
|
|
|
// then don't try to hoist instructions out of this loop.
|
|
|
|
const MachineLoop *ML = MLI->getLoopFor(BB);
|
2015-08-28 07:27:47 +08:00
|
|
|
if (ML && ML->getHeader()->isEHPad()) continue;
|
2011-10-12 10:58:01 +08:00
|
|
|
|
2010-04-07 08:41:17 +08:00
|
|
|
// Conservatively treat live-in's as an external def.
|
2010-04-08 09:03:47 +08:00
|
|
|
// FIXME: That means a reload that're reused in successor block(s) will not
|
|
|
|
// be LICM'ed.
|
2015-09-10 02:08:03 +08:00
|
|
|
for (const auto &LI : BB->liveins()) {
|
|
|
|
for (MCRegAliasIterator AI(LI.PhysReg, TRI, true); AI.isValid(); ++AI)
|
2012-06-02 07:28:30 +08:00
|
|
|
PhysRegDefs.set(*AI);
|
2010-04-07 08:41:17 +08:00
|
|
|
}
|
|
|
|
|
2011-10-12 07:48:44 +08:00
|
|
|
SpeculationState = SpeculateUnknown;
|
2016-01-07 07:45:05 +08:00
|
|
|
for (MachineInstr &MI : *BB)
|
|
|
|
ProcessMI(&MI, PhysRegDefs, PhysRegClobbers, StoredFIs, Candidates);
|
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()) {
|
2016-01-07 07:45:05 +08:00
|
|
|
for (const MachineOperand &MO : TI->operands()) {
|
2012-03-27 09:50:58 +08:00
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2012-03-27 09:50:58 +08:00
|
|
|
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.
|
2016-01-07 07:45:05 +08:00
|
|
|
for (CandidateInfo &Candidate : Candidates) {
|
2017-09-23 07:46:57 +08:00
|
|
|
if (Candidate.FI != std::numeric_limits<int>::min() &&
|
2016-01-07 07:45:05 +08:00
|
|
|
StoredFIs.count(Candidate.FI))
|
2010-04-07 08:41:17 +08:00
|
|
|
continue;
|
|
|
|
|
2016-01-07 07:45:05 +08:00
|
|
|
unsigned Def = Candidate.Def;
|
2012-03-27 09:50:58 +08:00
|
|
|
if (!PhysRegClobbers.test(Def) && !TermRegs.test(Def)) {
|
2010-04-14 04:21:05 +08:00
|
|
|
bool Safe = true;
|
2016-01-07 07:45:05 +08:00
|
|
|
MachineInstr *MI = Candidate.MI;
|
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
2010-04-14 06:13:34 +08:00
|
|
|
if (!MO.isReg() || MO.isDef() || !MO.getReg())
|
2010-04-14 04:21:05 +08:00
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2012-03-27 09:50:58 +08:00
|
|
|
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)
|
2016-01-07 07:45:05 +08:00
|
|
|
HoistPostRA(MI, Candidate.Def);
|
2010-04-14 04:21:05 +08:00
|
|
|
}
|
2010-04-07 08:41:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Add register 'Reg' to the livein sets of BBs in the current loop, and make
|
|
|
|
/// sure it is not killed by any instructions in the loop.
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::AddToLiveIns(unsigned Reg) {
|
2018-09-10 20:32:06 +08:00
|
|
|
for (MachineBasicBlock *BB : CurLoop->getBlocks()) {
|
2010-04-21 02:45:47 +08:00
|
|
|
if (!BB->isLiveIn(Reg))
|
|
|
|
BB->addLiveIn(Reg);
|
2016-01-07 07:45:05 +08:00
|
|
|
for (MachineInstr &MI : *BB) {
|
|
|
|
for (MachineOperand &MO : MI.operands()) {
|
2010-04-21 02:45:47 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// When an instruction is found to only use loop invariant operands that is
|
|
|
|
/// safe to hoist, this instruction is called to do the dirty work.
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::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.
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Hoisting to " << printMBBReference(*Preheader)
|
|
|
|
<< " from " << printMBBReference(*MI->getParent()) << ": "
|
|
|
|
<< *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;
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Check if this mbb is guaranteed to execute. If not then a load from this mbb
|
|
|
|
/// may not be safe to hoist.
|
2018-01-19 14:46:10 +08:00
|
|
|
bool MachineLICMBase::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);
|
2016-01-07 07:45:05 +08:00
|
|
|
for (MachineBasicBlock *CurrentLoopExitingBlock : CurrentLoopExitingBlocks)
|
|
|
|
if (!DT->dominates(BB, CurrentLoopExitingBlock)) {
|
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
|
|
|
}
|
|
|
|
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::EnterScope(MachineBasicBlock *MBB) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Entering " << printMBBReference(*MBB) << '\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
|
|
|
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::ExitScope(MachineBasicBlock *MBB) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Exiting " << printMBBReference(*MBB) << '\n');
|
2011-12-22 10:05:40 +08:00
|
|
|
BackTrace.pop_back();
|
|
|
|
}
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Destroy scope for the MBB that corresponds to the given dominator tree node
|
|
|
|
/// if its a leaf or all of its children are done. Walk up the dominator tree to
|
|
|
|
/// destroy ancestors which are now done.
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::ExitScopeIfDone(MachineDomTreeNode *Node,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Walk the specified loop in the CFG (defined by all blocks dominated by the
|
|
|
|
/// specified header block, and that are in the current loop) in depth first
|
|
|
|
/// order w.r.t the DominatorTree. This allows us to visit definitions before
|
|
|
|
/// uses, allowing us to hoist a loop body in one pass without iteration.
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::HoistOutOfLoop(MachineDomTreeNode *HeaderN) {
|
2015-02-06 06:39:46 +08:00
|
|
|
MachineBasicBlock *Preheader = getCurPreheader();
|
|
|
|
if (!Preheader)
|
|
|
|
return;
|
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
SmallVector<MachineDomTreeNode*, 32> Scopes;
|
|
|
|
SmallVector<MachineDomTreeNode*, 8> WorkList;
|
|
|
|
DenseMap<MachineDomTreeNode*, MachineDomTreeNode*> ParentMap;
|
|
|
|
DenseMap<MachineDomTreeNode*, unsigned> OpenChildren;
|
|
|
|
|
|
|
|
// Perform a DFS walk to determine the order of visit.
|
|
|
|
WorkList.push_back(HeaderN);
|
2015-02-06 06:39:46 +08:00
|
|
|
while (!WorkList.empty()) {
|
2011-12-22 10:05:40 +08:00
|
|
|
MachineDomTreeNode *Node = WorkList.pop_back_val();
|
2014-04-14 08:51:57 +08:00
|
|
|
assert(Node && "Null dominator tree node?");
|
2011-12-22 10:05:40 +08:00
|
|
|
MachineBasicBlock *BB = Node->getBlock();
|
|
|
|
|
|
|
|
// If the header of the loop containing this basic block is a landing pad,
|
|
|
|
// then don't try to hoist instructions out of this loop.
|
|
|
|
const MachineLoop *ML = MLI->getLoopFor(BB);
|
2015-08-28 07:27:47 +08:00
|
|
|
if (ML && ML->getHeader()->isEHPad())
|
2011-12-22 10:05:40 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// If this subregion is not in the top level loop at all, exit.
|
|
|
|
if (!CurLoop->contains(BB))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Scopes.push_back(Node);
|
|
|
|
const std::vector<MachineDomTreeNode*> &Children = Node->getChildren();
|
|
|
|
unsigned NumChildren = Children.size();
|
|
|
|
|
|
|
|
// Don't hoist things out of a large switch statement. This often causes
|
|
|
|
// code to be hoisted that wasn't going to be executed, and increases
|
|
|
|
// register pressure in a situation where it's likely to matter.
|
|
|
|
if (BB->succ_size() >= 25)
|
|
|
|
NumChildren = 0;
|
|
|
|
|
|
|
|
OpenChildren[Node] = NumChildren;
|
|
|
|
// Add children in reverse order as then the next popped worklist node is
|
|
|
|
// the first child of this node. This means we ultimately traverse the
|
|
|
|
// DOM tree in exactly the same order as if we'd recursed.
|
|
|
|
for (int i = (int)NumChildren-1; i >= 0; --i) {
|
|
|
|
MachineDomTreeNode *Child = Children[i];
|
|
|
|
ParentMap[Child] = Node;
|
|
|
|
WorkList.push_back(Child);
|
|
|
|
}
|
2015-02-06 06:39:46 +08:00
|
|
|
}
|
2011-12-22 10:05:40 +08:00
|
|
|
|
2015-02-06 06:39:46 +08:00
|
|
|
if (Scopes.size() == 0)
|
|
|
|
return;
|
2011-12-22 10:05:40 +08:00
|
|
|
|
2015-02-06 06:39:46 +08:00
|
|
|
// Compute registers which are livein into the loop headers.
|
|
|
|
RegSeen.clear();
|
|
|
|
BackTrace.clear();
|
|
|
|
InitRegPressure(Preheader);
|
2010-10-19 08:55:07 +08:00
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
// Now perform LICM.
|
2016-01-07 07:45:05 +08:00
|
|
|
for (MachineDomTreeNode *Node : Scopes) {
|
2011-12-22 10:05:40 +08:00
|
|
|
MachineBasicBlock *MBB = Node->getBlock();
|
2010-10-20 02:58:51 +08:00
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
EnterScope(MBB);
|
2010-10-16 10:20:26 +08:00
|
|
|
|
2011-12-22 10:05:40 +08:00
|
|
|
// Process the block
|
|
|
|
SpeculationState = SpeculateUnknown;
|
|
|
|
for (MachineBasicBlock::iterator
|
|
|
|
MII = MBB->begin(), E = MBB->end(); MII != E; ) {
|
|
|
|
MachineBasicBlock::iterator NextMII = MII; ++NextMII;
|
|
|
|
MachineInstr *MI = &*MII;
|
|
|
|
if (!Hoist(MI, Preheader))
|
|
|
|
UpdateRegPressure(MI);
|
2018-03-23 23:28:15 +08:00
|
|
|
// If we have hoisted an instruction that may store, it can only be a
|
|
|
|
// constant store.
|
2011-12-22 10:05:40 +08:00
|
|
|
MII = NextMII;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's a leaf node, it's done. Traverse upwards to pop ancestors.
|
|
|
|
ExitScopeIfDone(Node, OpenChildren, ParentMap);
|
|
|
|
}
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Sink instructions into loops if profitable. This especially tries to prevent
|
|
|
|
/// register spills caused by register pressure if there is little to no
|
|
|
|
/// overhead moving instructions into loops.
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::SinkIntoLoop() {
|
2015-03-14 18:58:38 +08:00
|
|
|
MachineBasicBlock *Preheader = getCurPreheader();
|
|
|
|
if (!Preheader)
|
|
|
|
return;
|
|
|
|
|
|
|
|
SmallVector<MachineInstr *, 8> Candidates;
|
|
|
|
for (MachineBasicBlock::instr_iterator I = Preheader->instr_begin();
|
|
|
|
I != Preheader->instr_end(); ++I) {
|
|
|
|
// We need to ensure that we can safely move this instruction into the loop.
|
2017-04-26 13:27:20 +08:00
|
|
|
// As such, it must not have side-effects, e.g. such as a call has.
|
2015-10-10 03:40:45 +08:00
|
|
|
if (IsLoopInvariantInst(*I) && !HasLoopPHIUse(&*I))
|
|
|
|
Candidates.push_back(&*I);
|
2015-03-14 18:58:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (MachineInstr *I : Candidates) {
|
|
|
|
const MachineOperand &MO = I->getOperand(0);
|
|
|
|
if (!MO.isDef() || !MO.isReg() || !MO.getReg())
|
|
|
|
continue;
|
|
|
|
if (!MRI->hasOneDef(MO.getReg()))
|
|
|
|
continue;
|
|
|
|
bool CanSink = true;
|
|
|
|
MachineBasicBlock *B = nullptr;
|
|
|
|
for (MachineInstr &MI : MRI->use_instructions(MO.getReg())) {
|
|
|
|
// FIXME: Come up with a proper cost model that estimates whether sinking
|
|
|
|
// the instruction (and thus possibly executing it on every loop
|
|
|
|
// iteration) is more expensive than a register.
|
|
|
|
// For now assumes that copies are cheap and thus almost always worth it.
|
|
|
|
if (!MI.isCopy()) {
|
|
|
|
CanSink = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!B) {
|
|
|
|
B = MI.getParent();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
B = DT->findNearestCommonDominator(B, MI.getParent());
|
|
|
|
if (!B) {
|
|
|
|
CanSink = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!CanSink || !B || B == Preheader)
|
|
|
|
continue;
|
|
|
|
B->splice(B->getFirstNonPHI(), Preheader, I);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-21 06:03:58 +08:00
|
|
|
static bool isOperandKill(const MachineOperand &MO, MachineRegisterInfo *MRI) {
|
|
|
|
return MO.isKill() || MRI->hasOneNonDBGUse(MO.getReg());
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Find all virtual register references that are liveout of the preheader to
|
|
|
|
/// initialize the starting "register pressure". Note this does not count live
|
|
|
|
/// through (livein but not used) registers.
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::InitRegPressure(MachineBasicBlock *BB) {
|
2010-10-14 09:16:09 +08:00
|
|
|
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;
|
2016-07-15 22:41:04 +08:00
|
|
|
if (!TII->analyzeBranch(*BB, TBB, FBB, Cond, false) && Cond.empty())
|
2010-10-21 06:03:58 +08:00
|
|
|
InitRegPressure(*BB->pred_begin());
|
|
|
|
}
|
|
|
|
|
2015-04-08 00:42:35 +08:00
|
|
|
for (const MachineInstr &MI : *BB)
|
|
|
|
UpdateRegPressure(&MI, /*ConsiderUnseenAsDef=*/true);
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Update estimate of register pressure after the specified instruction.
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::UpdateRegPressure(const MachineInstr *MI,
|
|
|
|
bool ConsiderUnseenAsDef) {
|
2015-04-08 00:42:35 +08:00
|
|
|
auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/true, ConsiderUnseenAsDef);
|
2015-04-14 19:56:25 +08:00
|
|
|
for (const auto &RPIdAndCost : Cost) {
|
|
|
|
unsigned Class = RPIdAndCost.first;
|
|
|
|
if (static_cast<int>(RegPressure[Class]) < -RPIdAndCost.second)
|
2015-04-08 00:42:35 +08:00
|
|
|
RegPressure[Class] = 0;
|
|
|
|
else
|
2015-04-14 19:56:25 +08:00
|
|
|
RegPressure[Class] += RPIdAndCost.second;
|
2015-04-08 00:42:35 +08:00
|
|
|
}
|
|
|
|
}
|
2010-10-14 09:16:09 +08:00
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Calculate the additional register pressure that the registers used in MI
|
|
|
|
/// cause.
|
|
|
|
///
|
|
|
|
/// If 'ConsiderSeen' is true, updates 'RegSeen' and uses the information to
|
|
|
|
/// figure out which usages are live-ins.
|
|
|
|
/// FIXME: Figure out a way to consider 'RegSeen' from all code paths.
|
2015-04-08 00:42:35 +08:00
|
|
|
DenseMap<unsigned, int>
|
2018-01-19 14:46:10 +08:00
|
|
|
MachineLICMBase::calcRegisterCost(const MachineInstr *MI, bool ConsiderSeen,
|
|
|
|
bool ConsiderUnseenAsDef) {
|
2015-04-08 00:42:35 +08:00
|
|
|
DenseMap<unsigned, int> Cost;
|
|
|
|
if (MI->isImplicitDef())
|
|
|
|
return Cost;
|
2010-10-14 09:16:09 +08:00
|
|
|
for (unsigned i = 0, e = MI->getDesc().getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
2010-10-20 02:58:51 +08:00
|
|
|
if (!MO.isReg() || MO.isImplicit())
|
2010-10-14 09:16:09 +08:00
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
if (!Register::isVirtualRegister(Reg))
|
2010-10-14 09:16:09 +08:00
|
|
|
continue;
|
|
|
|
|
2015-04-08 00:42:35 +08:00
|
|
|
// FIXME: It seems bad to use RegSeen only for some of these calculations.
|
|
|
|
bool isNew = ConsiderSeen ? RegSeen.insert(Reg).second : false;
|
2015-04-14 19:56:25 +08:00
|
|
|
const TargetRegisterClass *RC = MRI->getRegClass(Reg);
|
|
|
|
|
|
|
|
RegClassWeight W = TRI->getRegClassWeight(RC);
|
|
|
|
int RCCost = 0;
|
2010-10-20 02:58:51 +08:00
|
|
|
if (MO.isDef())
|
2015-04-14 19:56:25 +08:00
|
|
|
RCCost = W.RegWeight;
|
2015-04-08 00:42:35 +08:00
|
|
|
else {
|
|
|
|
bool isKill = isOperandKill(MO, MRI);
|
|
|
|
if (isNew && !isKill && ConsiderUnseenAsDef)
|
|
|
|
// Haven't seen this, it must be a livein.
|
2015-04-14 19:56:25 +08:00
|
|
|
RCCost = W.RegWeight;
|
2015-04-08 00:42:35 +08:00
|
|
|
else if (!isNew && isKill)
|
2015-04-14 19:56:25 +08:00
|
|
|
RCCost = -W.RegWeight;
|
|
|
|
}
|
|
|
|
if (RCCost == 0)
|
|
|
|
continue;
|
|
|
|
const int *PS = TRI->getRegClassPressureSets(RC);
|
|
|
|
for (; *PS != -1; ++PS) {
|
|
|
|
if (Cost.find(*PS) == Cost.end())
|
|
|
|
Cost[*PS] = RCCost;
|
|
|
|
else
|
|
|
|
Cost[*PS] += RCCost;
|
2010-10-16 10:20:26 +08:00
|
|
|
}
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
2015-04-08 00:42:35 +08:00
|
|
|
return Cost;
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Return true if this machine instruction loads from global offset table or
|
|
|
|
/// constant pool.
|
2015-12-24 01:05:57 +08:00
|
|
|
static bool mayLoadFromGOTOrConstantPool(MachineInstr &MI) {
|
2017-09-23 07:46:57 +08:00
|
|
|
assert(MI.mayLoad() && "Expected MI that loads!");
|
2017-04-26 13:27:20 +08:00
|
|
|
|
2015-12-24 01:05:57 +08:00
|
|
|
// If we lost memory operands, conservatively assume that the instruction
|
2017-04-26 13:27:20 +08:00
|
|
|
// reads from everything..
|
2015-12-24 01:05:57 +08:00
|
|
|
if (MI.memoperands_empty())
|
|
|
|
return true;
|
|
|
|
|
2016-01-07 07:45:05 +08:00
|
|
|
for (MachineMemOperand *MemOp : MI.memoperands())
|
|
|
|
if (const PseudoSourceValue *PSV = MemOp->getPseudoValue())
|
2015-08-12 07:09:45 +08:00
|
|
|
if (PSV->isGOT() || PSV->isConstantPool())
|
2014-04-15 15:22:52 +08:00
|
|
|
return true;
|
2016-01-07 07:45:05 +08:00
|
|
|
|
2011-10-18 01:35:01 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-23 23:28:15 +08:00
|
|
|
// This function iterates through all the operands of the input store MI and
|
|
|
|
// checks that each register operand statisfies isCallerPreservedPhysReg.
|
|
|
|
// This means, the value being stored and the address where it is being stored
|
|
|
|
// is constant throughout the body of the function (not including prologue and
|
|
|
|
// epilogue). When called with an MI that isn't a store, it returns false.
|
2018-04-09 22:50:02 +08:00
|
|
|
// A future improvement can be to check if the store registers are constant
|
|
|
|
// throughout the loop rather than throughout the funtion.
|
2018-03-23 23:28:15 +08:00
|
|
|
static bool isInvariantStore(const MachineInstr &MI,
|
|
|
|
const TargetRegisterInfo *TRI,
|
|
|
|
const MachineRegisterInfo *MRI) {
|
|
|
|
|
2018-04-09 22:50:02 +08:00
|
|
|
bool FoundCallerPresReg = false;
|
2018-03-23 23:28:15 +08:00
|
|
|
if (!MI.mayStore() || MI.hasUnmodeledSideEffects() ||
|
|
|
|
(MI.getNumOperands() == 0))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check that all register operands are caller-preserved physical registers.
|
|
|
|
for (const MachineOperand &MO : MI.operands()) {
|
|
|
|
if (MO.isReg()) {
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2018-03-23 23:28:15 +08:00
|
|
|
// If operand is a virtual register, check if it comes from a copy of a
|
|
|
|
// physical register.
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isVirtualRegister(Reg))
|
2018-03-23 23:28:15 +08:00
|
|
|
Reg = TRI->lookThruCopyLike(MO.getReg(), MRI);
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isVirtualRegister(Reg))
|
2018-03-23 23:28:15 +08:00
|
|
|
return false;
|
|
|
|
if (!TRI->isCallerPreservedPhysReg(Reg, *MI.getMF()))
|
|
|
|
return false;
|
2018-04-09 22:50:02 +08:00
|
|
|
else
|
|
|
|
FoundCallerPresReg = true;
|
|
|
|
} else if (!MO.isImm()) {
|
|
|
|
return false;
|
2018-03-23 23:28:15 +08:00
|
|
|
}
|
|
|
|
}
|
2018-04-09 22:50:02 +08:00
|
|
|
return FoundCallerPresReg;
|
2018-03-23 23:28:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the input MI is a copy instruction that feeds an invariant
|
|
|
|
// store instruction. This means that the src of the copy has to satisfy
|
|
|
|
// isCallerPreservedPhysReg and atleast one of it's users should satisfy
|
|
|
|
// isInvariantStore.
|
|
|
|
static bool isCopyFeedingInvariantStore(const MachineInstr &MI,
|
|
|
|
const MachineRegisterInfo *MRI,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
|
|
|
|
|
|
|
// FIXME: If targets would like to look through instructions that aren't
|
|
|
|
// pure copies, this can be updated to a query.
|
|
|
|
if (!MI.isCopy())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const MachineFunction *MF = MI.getMF();
|
|
|
|
// Check that we are copying a constant physical register.
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register CopySrcReg = MI.getOperand(1).getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isVirtualRegister(CopySrcReg))
|
2018-03-23 23:28:15 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!TRI->isCallerPreservedPhysReg(CopySrcReg, *MF))
|
|
|
|
return false;
|
|
|
|
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register CopyDstReg = MI.getOperand(0).getReg();
|
2018-03-23 23:28:15 +08:00
|
|
|
// Check if any of the uses of the copy are invariant stores.
|
2019-08-02 07:27:28 +08:00
|
|
|
assert(Register::isVirtualRegister(CopyDstReg) &&
|
|
|
|
"copy dst is not a virtual reg");
|
2018-03-23 23:28:15 +08:00
|
|
|
|
|
|
|
for (MachineInstr &UseMI : MRI->use_instructions(CopyDstReg)) {
|
|
|
|
if (UseMI.mayStore() && isInvariantStore(UseMI, TRI, MRI))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Returns true if the instruction may be a suitable candidate for LICM.
|
|
|
|
/// e.g. If the instruction is a call, then it's obviously not safe to hoist it.
|
2018-01-19 14:46:10 +08:00
|
|
|
bool MachineLICMBase::IsLICMCandidate(MachineInstr &I) {
|
2010-07-12 08:00:35 +08:00
|
|
|
// Check if it's safe to move the instruction.
|
|
|
|
bool DontMoveAcrossStore = true;
|
2018-03-23 23:28:15 +08:00
|
|
|
if ((!I.isSafeToMove(AA, DontMoveAcrossStore)) &&
|
|
|
|
!(HoistConstStores && isInvariantStore(I, TRI, MRI))) {
|
2008-01-11 07:08:24 +08:00
|
|
|
return false;
|
2018-03-23 23:28:15 +08:00
|
|
|
}
|
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.
|
2015-12-24 01:05:57 +08:00
|
|
|
if (I.mayLoad() && !mayLoadFromGOTOrConstantPool(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;
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Returns true if the instruction is loop invariant.
|
|
|
|
/// I.e., all virtual register operands are defined outside of the loop,
|
|
|
|
/// physical registers aren't accessed explicitly, and there are no side
|
2010-04-14 02:16:00 +08:00
|
|
|
/// effects that aren't captured by the operands or other flags.
|
2018-01-19 14:46:10 +08:00
|
|
|
bool MachineLICMBase::IsLoopInvariantInst(MachineInstr &I) {
|
2010-04-14 02:16:00 +08:00
|
|
|
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.
|
2016-01-07 07:45:05 +08:00
|
|
|
for (const MachineOperand &MO : I.operands()) {
|
2008-10-03 23:45:36 +08:00
|
|
|
if (!MO.isReg())
|
2008-08-21 04:32:05 +08:00
|
|
|
continue;
|
|
|
|
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register 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.
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isPhysicalRegister(Reg)) {
|
2009-09-26 07:58:45 +08:00
|
|
|
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.
|
2017-06-16 02:29:59 +08:00
|
|
|
// However, if the physreg is known to always be caller saved/restored
|
|
|
|
// then this use is safe to hoist.
|
|
|
|
if (!MRI->isConstantPhysReg(Reg) &&
|
2017-10-11 07:50:49 +08:00
|
|
|
!(TRI->isCallerPreservedPhysReg(Reg, *I.getMF())))
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Return true if the specified instruction is used by a phi node and hoisting
|
|
|
|
/// it could cause a copy to be inserted.
|
2018-01-19 14:46:10 +08:00
|
|
|
bool MachineLICMBase::HasLoopPHIUse(const MachineInstr *MI) const {
|
2012-04-11 08:00:26 +08:00
|
|
|
SmallVector<const MachineInstr*, 8> Work(1, MI);
|
|
|
|
do {
|
|
|
|
MI = Work.pop_back_val();
|
2015-05-29 10:56:46 +08:00
|
|
|
for (const MachineOperand &MO : MI->operands()) {
|
|
|
|
if (!MO.isReg() || !MO.isDef())
|
2012-04-11 08:00:26 +08:00
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
if (!Register::isVirtualRegister(Reg))
|
2012-04-11 08:00:26 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Compute operand latency between a def of 'Reg' and an use in the current
|
|
|
|
/// loop, return true if the target considered it high.
|
2018-01-19 14:46:10 +08:00
|
|
|
bool MachineLICMBase::HasHighOperandLatency(MachineInstr &MI,
|
|
|
|
unsigned DefIdx,
|
|
|
|
unsigned Reg) const {
|
2015-06-13 11:42:11 +08:00
|
|
|
if (MRI->use_nodbg_empty(Reg))
|
2010-10-20 02:58:51 +08:00
|
|
|
return false;
|
2010-10-14 09:16:09 +08:00
|
|
|
|
2014-03-18 03:36:09 +08:00
|
|
|
for (MachineInstr &UseMI : MRI->use_nodbg_instructions(Reg)) {
|
|
|
|
if (UseMI.isCopyLike())
|
2010-10-26 10:08:50 +08:00
|
|
|
continue;
|
2014-03-18 03:36:09 +08:00
|
|
|
if (!CurLoop->contains(UseMI.getParent()))
|
2010-10-14 09:16:09 +08:00
|
|
|
continue;
|
2014-03-18 03:36:09 +08:00
|
|
|
for (unsigned i = 0, e = UseMI.getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = UseMI.getOperand(i);
|
2010-10-14 09:16:09 +08:00
|
|
|
if (!MO.isReg() || !MO.isUse())
|
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register MOReg = MO.getReg();
|
2010-10-14 09:16:09 +08:00
|
|
|
if (MOReg != Reg)
|
|
|
|
continue;
|
|
|
|
|
2016-06-30 08:01:54 +08:00
|
|
|
if (TII->hasHighOperandLatency(SchedModel, MRI, MI, DefIdx, UseMI, i))
|
2010-10-20 02:58:51 +08:00
|
|
|
return true;
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
|
|
|
|
2010-10-20 02:58:51 +08:00
|
|
|
// Only look at the first in loop use.
|
|
|
|
break;
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
|
|
|
|
2010-10-20 02:58:51 +08:00
|
|
|
return false;
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Return true if the instruction is marked "cheap" or the operand latency
|
|
|
|
/// between its def and a use is one or less.
|
2018-01-19 14:46:10 +08:00
|
|
|
bool MachineLICMBase::IsCheapInstruction(MachineInstr &MI) const {
|
2016-06-30 08:01:54 +08:00
|
|
|
if (TII->isAsCheapAsAMove(MI) || MI.isCopyLike())
|
2010-10-26 10:08:50 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
bool isCheap = false;
|
|
|
|
unsigned NumDefs = MI.getDesc().getNumDefs();
|
|
|
|
for (unsigned i = 0, e = MI.getNumOperands(); NumDefs && i != e; ++i) {
|
|
|
|
MachineOperand &DefMO = MI.getOperand(i);
|
|
|
|
if (!DefMO.isReg() || !DefMO.isDef())
|
|
|
|
continue;
|
|
|
|
--NumDefs;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = DefMO.getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
if (Register::isPhysicalRegister(Reg))
|
2010-10-26 10:08:50 +08:00
|
|
|
continue;
|
|
|
|
|
2016-06-30 08:01:54 +08:00
|
|
|
if (!TII->hasLowDefLatency(SchedModel, MI, i))
|
2010-10-26 10:08:50 +08:00
|
|
|
return false;
|
|
|
|
isCheap = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isCheap;
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Visit BBs from header to current BB, check if hoisting an instruction of the
|
|
|
|
/// given cost matrix can cause high register pressure.
|
2018-01-19 14:46:10 +08:00
|
|
|
bool
|
|
|
|
MachineLICMBase::CanCauseHighRegPressure(const DenseMap<unsigned, int>& Cost,
|
|
|
|
bool CheapInstr) {
|
2015-04-14 19:56:25 +08:00
|
|
|
for (const auto &RPIdAndCost : Cost) {
|
|
|
|
if (RPIdAndCost.second <= 0)
|
2010-10-21 06:03:58 +08:00
|
|
|
continue;
|
|
|
|
|
2015-04-14 19:56:25 +08:00
|
|
|
unsigned Class = RPIdAndCost.first;
|
2015-04-04 00:19:48 +08:00
|
|
|
int Limit = RegLimit[Class];
|
2012-04-11 08:00:28 +08:00
|
|
|
|
|
|
|
// Don't hoist cheap instructions if they would increase register pressure,
|
|
|
|
// even if we're under the limit.
|
2015-01-09 06:10:48 +08:00
|
|
|
if (CheapInstr && !HoistCheapInsts)
|
2012-04-11 08:00:28 +08:00
|
|
|
return true;
|
|
|
|
|
2015-04-04 00:19:48 +08:00
|
|
|
for (const auto &RP : BackTrace)
|
2015-04-14 19:56:25 +08:00
|
|
|
if (static_cast<int>(RP[Class]) + RPIdAndCost.second >= Limit)
|
2010-10-16 10:20:26 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Traverse the back trace from header to the current block and update their
|
|
|
|
/// register pressures to reflect the effect of hoisting MI from the current
|
|
|
|
/// block to the preheader.
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::UpdateBackTraceRegPressure(const MachineInstr *MI) {
|
2010-10-21 06:03:58 +08:00
|
|
|
// First compute the 'cost' of the instruction, i.e. its contribution
|
|
|
|
// to register pressure.
|
2015-04-08 00:42:35 +08:00
|
|
|
auto Cost = calcRegisterCost(MI, /*ConsiderSeen=*/false,
|
|
|
|
/*ConsiderUnseenAsDef=*/false);
|
2010-10-21 06:03:58 +08:00
|
|
|
|
|
|
|
// Update register pressure of blocks from loop header to current block.
|
2015-04-08 00:42:35 +08:00
|
|
|
for (auto &RP : BackTrace)
|
2015-04-14 19:56:25 +08:00
|
|
|
for (const auto &RPIdAndCost : Cost)
|
|
|
|
RP[RPIdAndCost.first] += RPIdAndCost.second;
|
2010-10-21 06:03:58 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Return true if it is potentially profitable to hoist the given loop
|
|
|
|
/// invariant.
|
2018-01-19 14:46:10 +08:00
|
|
|
bool MachineLICMBase::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.
|
|
|
|
|
2018-03-23 23:28:15 +08:00
|
|
|
if (HoistConstStores && isCopyFeedingInvariantStore(MI, MRI, TRI))
|
|
|
|
return true;
|
|
|
|
|
2012-04-11 08:00:28 +08:00
|
|
|
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) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Won't hoist cheap instr with loop PHI use: " << MI);
|
2012-04-11 08:00:28 +08:00
|
|
|
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.
|
2016-06-30 08:01:54 +08:00
|
|
|
if (TII->isTriviallyReMaterializable(MI, AA))
|
2012-04-11 08:00:28 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// FIXME: If there are long latency loop-invariant instructions inside the
|
|
|
|
// loop at this point, why didn't the optimizer's LICM hoist them?
|
|
|
|
for (unsigned i = 0, e = MI.getDesc().getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI.getOperand(i);
|
|
|
|
if (!MO.isReg() || MO.isImplicit())
|
|
|
|
continue;
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2019-08-02 07:27:28 +08:00
|
|
|
if (!Register::isVirtualRegister(Reg))
|
2012-04-11 08:00:28 +08:00
|
|
|
continue;
|
2015-04-08 00:42:35 +08:00
|
|
|
if (MO.isDef() && HasHighOperandLatency(MI, i, Reg)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Hoist High Latency: " << MI);
|
2015-04-08 00:42:35 +08:00
|
|
|
++NumHighLatency;
|
|
|
|
return true;
|
2010-10-14 09:16:09 +08:00
|
|
|
}
|
2012-04-11 08:00:28 +08:00
|
|
|
}
|
2010-10-14 09:16:09 +08:00
|
|
|
|
2015-04-08 00:42:35 +08:00
|
|
|
// Estimate register pressure to determine whether to LICM the instruction.
|
|
|
|
// In low register pressure situation, we can be more aggressive about
|
|
|
|
// hoisting. Also, favors hoisting long latency instructions even in
|
|
|
|
// moderately high pressure situation.
|
|
|
|
// Cheap instructions will only be hoisted if they don't increase register
|
|
|
|
// pressure at all.
|
|
|
|
auto Cost = calcRegisterCost(&MI, /*ConsiderSeen=*/false,
|
|
|
|
/*ConsiderUnseenAsDef=*/false);
|
|
|
|
|
2012-04-11 08:00:28 +08:00
|
|
|
// Visit BBs from header to current BB, if hoisting this doesn't cause
|
|
|
|
// high register pressure, then it's safe to proceed.
|
|
|
|
if (!CanCauseHighRegPressure(Cost, CheapInstr)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Hoist non-reg-pressure: " << MI);
|
2012-04-11 08:00:28 +08:00
|
|
|
++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) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Won't hoist instr with loop PHI use: " << MI);
|
2012-04-11 08:00:28 +08:00
|
|
|
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))) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Won't speculate: " << MI);
|
2012-04-11 08:00:28 +08:00
|
|
|
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.
|
2016-09-10 09:03:20 +08:00
|
|
|
if (!TII->isTriviallyReMaterializable(MI, AA) &&
|
|
|
|
!MI.isDereferenceableInvariantLoad(AA)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_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;
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Unfold a load from the given machineinstr if the load itself could be
|
|
|
|
/// hoisted. Return the unfolded and hoistable load, or null if the load
|
|
|
|
/// couldn't be unfolded or if it wouldn't be hoistable.
|
2018-01-19 14:46:10 +08:00
|
|
|
MachineInstr *MachineLICMBase::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.
|
2016-09-10 09:03:20 +08:00
|
|
|
if (!MI->isDereferenceableInvariantLoad(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);
|
2017-10-11 07:50:49 +08:00
|
|
|
MachineFunction &MF = *MI->getMF();
|
2012-05-08 06:10:26 +08:00
|
|
|
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.
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MRI->createVirtualRegister(RC);
|
2009-11-21 03:55:37 +08:00
|
|
|
|
2009-10-30 01:47:20 +08:00
|
|
|
SmallVector<MachineInstr *, 2> NewMIs;
|
2016-06-30 08:01:54 +08:00
|
|
|
bool Success = TII->unfoldMemoryOperand(MF, *MI, Reg,
|
|
|
|
/*UnfoldLoad=*/true,
|
|
|
|
/*UnfoldStore=*/false, NewMIs);
|
2009-10-30 01:47:20 +08:00
|
|
|
(void)Success;
|
|
|
|
assert(Success &&
|
|
|
|
"unfoldMemoryOperand failed when getOpcodeAfterMemoryUnfold "
|
|
|
|
"succeeded!");
|
|
|
|
assert(NewMIs.size() == 2 &&
|
|
|
|
"Unfolded a load into multiple instructions!");
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2011-12-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];
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Initialize the CSE map with instructions that are in the current loop
|
|
|
|
/// preheader that may become duplicates of instructions that are hoisted
|
|
|
|
/// out of the loop.
|
2018-01-19 14:46:10 +08:00
|
|
|
void MachineLICMBase::InitCSEMap(MachineBasicBlock *BB) {
|
2016-01-07 07:45:05 +08:00
|
|
|
for (MachineInstr &MI : *BB)
|
|
|
|
CSEMap[MI.getOpcode()].push_back(&MI);
|
2009-11-04 05:40:02 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Find an instruction amount PrevMIs that is a duplicate of MI.
|
|
|
|
/// Return this instruction if it's found.
|
2009-11-07 11:52:02 +08:00
|
|
|
const MachineInstr*
|
2018-01-19 14:46:10 +08:00
|
|
|
MachineLICMBase::LookForDuplicate(const MachineInstr *MI,
|
|
|
|
std::vector<const MachineInstr*> &PrevMIs) {
|
2016-01-07 07:45:05 +08:00
|
|
|
for (const MachineInstr *PrevMI : PrevMIs)
|
2016-06-30 08:01:54 +08:00
|
|
|
if (TII->produceSameValue(*MI, *PrevMI, (PreRegAlloc ? MRI : nullptr)))
|
2009-11-05 08:51:13 +08:00
|
|
|
return PrevMI;
|
2016-01-07 07:45:05 +08:00
|
|
|
|
2014-04-14 08:51:57 +08:00
|
|
|
return nullptr;
|
2009-11-05 08:51:13 +08:00
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Given a LICM'ed instruction, look for an instruction on the preheader that
|
|
|
|
/// computes the same value. If it's found, do a RAU on with the definition of
|
|
|
|
/// the existing instruction rather than hoisting the instruction to the
|
|
|
|
/// preheader.
|
2018-01-19 14:46:10 +08:00
|
|
|
bool MachineLICMBase::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)) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_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 ||
|
2019-08-02 07:27:28 +08:00
|
|
|
!Register::isPhysicalRegister(MO.getReg()) ||
|
2010-02-28 09:33:43 +08:00
|
|
|
MO.getReg() == Dup->getOperand(i).getReg()) &&
|
|
|
|
"Instructions with different phys regs are not identical!");
|
|
|
|
|
|
|
|
if (MO.isReg() && MO.isDef() &&
|
2019-08-02 07:27:28 +08:00
|
|
|
!Register::isPhysicalRegister(MO.getReg()))
|
2011-10-18 03:50:12 +08:00
|
|
|
Defs.push_back(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
SmallVector<const TargetRegisterClass*, 2> OrigRCs;
|
|
|
|
for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
|
|
|
|
unsigned Idx = Defs[i];
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MI->getOperand(Idx).getReg();
|
|
|
|
Register DupReg = Dup->getOperand(Idx).getReg();
|
2011-10-18 03:50:12 +08:00
|
|
|
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
|
|
|
|
2016-01-07 07:45:05 +08:00
|
|
|
for (unsigned Idx : Defs) {
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MI->getOperand(Idx).getReg();
|
|
|
|
Register DupReg = Dup->getOperand(Idx).getReg();
|
2011-10-18 03:50:12 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Return true if the given instruction will be CSE'd if it's hoisted out of
|
|
|
|
/// the loop.
|
2018-01-19 14:46:10 +08:00
|
|
|
bool MachineLICMBase::MayCSE(MachineInstr *MI) {
|
2011-10-12 08:09:14 +08:00
|
|
|
unsigned Opcode = MI->getOpcode();
|
2017-09-23 07:46:57 +08:00
|
|
|
DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator
|
2011-10-12 08:09:14 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// When an instruction is found to use only loop invariant operands
|
2008-05-13 03:38:32 +08:00
|
|
|
/// that are safe to hoist, this instruction is called to do the dirty work.
|
2015-12-11 00:34:21 +08:00
|
|
|
/// It returns true if the instruction is hoisted.
|
2018-01-19 14:46:10 +08:00
|
|
|
bool MachineLICMBase::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
|
|
|
|
2018-03-23 23:28:15 +08:00
|
|
|
// If we have hoisted an instruction that may store, it can only be a constant
|
|
|
|
// store.
|
|
|
|
if (MI->mayStore())
|
|
|
|
NumStoreConst++;
|
|
|
|
|
2009-01-16 06:01:38 +08:00
|
|
|
// Now move the instructions to the predecessor, inserting it before any
|
|
|
|
// terminator instructions.
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
|
|
|
dbgs() << "Hoisting " << *MI;
|
|
|
|
if (MI->getParent()->getBasicBlock())
|
|
|
|
dbgs() << " from " << printMBBReference(*MI->getParent());
|
|
|
|
if (Preheader->getBasicBlock())
|
|
|
|
dbgs() << " to " << printMBBReference(*Preheader);
|
|
|
|
dbgs() << "\n";
|
|
|
|
});
|
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();
|
2017-09-23 07:46:57 +08:00
|
|
|
DenseMap<unsigned, std::vector<const MachineInstr *>>::iterator
|
2009-11-04 05:40:02 +08:00
|
|
|
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
|
|
|
|
2016-12-02 08:37:57 +08:00
|
|
|
// Since we are moving the instruction out of its basic block, we do not
|
2017-04-26 13:27:20 +08:00
|
|
|
// retain its debug location. Doing so would degrade the debugging
|
2016-12-02 08:37:57 +08:00
|
|
|
// experience and adversely affect the accuracy of profiling information.
|
|
|
|
MI->setDebugLoc(DebugLoc());
|
|
|
|
|
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.
|
2016-01-07 07:45:05 +08:00
|
|
|
for (MachineOperand &MO : MI->operands())
|
2010-05-14 04:34:42 +08:00
|
|
|
if (MO.isReg() && MO.isDef() && !MO.isDead())
|
2010-10-14 09:16:09 +08:00
|
|
|
MRI->clearKillFlags(MO.getReg());
|
2010-05-14 04:34:42 +08:00
|
|
|
|
2009-02-05 16:45:46 +08:00
|
|
|
// Add to the CSE map.
|
|
|
|
if (CI != CSEMap.end())
|
2009-10-28 11:21:57 +08:00
|
|
|
CI->second.push_back(MI);
|
2014-10-04 02:33:16 +08:00
|
|
|
else
|
|
|
|
CSEMap[Opcode].push_back(MI);
|
2009-02-05 16:45:46 +08:00
|
|
|
}
|
2007-12-08 09:47:01 +08:00
|
|
|
|
2009-01-16 06:01:38 +08:00
|
|
|
++NumHoisted;
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
Changed = true;
|
2010-10-21 06:03:58 +08:00
|
|
|
|
|
|
|
return true;
|
Initial commit of the machine code LICM pass. It successfully hoists this:
_foo:
li r2, 0
LBB1_1: ; bb
li r5, 0
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
to:
_foo:
li r2, 0
li r5, 0
LBB1_1: ; bb
stw r5, 0(r3)
addi r2, r2, 1
addi r3, r3, 4
cmplw cr0, r2, r4
bne cr0, LBB1_1 ; bb
LBB1_2: ; return
blr
ZOMG!! :-)
Moar to come...
llvm-svn: 44687
2007-12-08 05:42:31 +08:00
|
|
|
}
|
2010-06-23 01:25:57 +08:00
|
|
|
|
2015-12-11 00:34:21 +08:00
|
|
|
/// Get the preheader for the current loop, splitting a critical edge if needed.
|
2018-01-19 14:46:10 +08:00
|
|
|
MachineBasicBlock *MachineLICMBase::getCurPreheader() {
|
2010-06-23 01:25:57 +08:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2016-04-22 05:01:13 +08:00
|
|
|
CurPreheader = Pred->SplitCriticalEdge(CurLoop->getHeader(), *this);
|
2010-06-23 01:25:57 +08:00
|
|
|
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;
|
|
|
|
}
|