2018-01-22 18:06:50 +08:00
|
|
|
//===---- ReachingDefAnalysis.cpp - Reaching Def Analysis ---*- C++ -*-----===//
|
|
|
|
//
|
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
|
2018-01-22 18:06:50 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-01-29 16:26:11 +08:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2021-03-01 02:59:20 +08:00
|
|
|
#include "llvm/ADT/SetOperations.h"
|
2019-11-26 18:03:25 +08:00
|
|
|
#include "llvm/CodeGen/LivePhysRegs.h"
|
2018-01-22 18:06:50 +08:00
|
|
|
#include "llvm/CodeGen/ReachingDefAnalysis.h"
|
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2019-10-19 08:22:07 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2018-01-22 18:06:50 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "reaching-deps-analysis"
|
|
|
|
|
|
|
|
char ReachingDefAnalysis::ID = 0;
|
|
|
|
INITIALIZE_PASS(ReachingDefAnalysis, DEBUG_TYPE, "ReachingDefAnalysis", false,
|
|
|
|
true)
|
|
|
|
|
2020-03-11 01:24:04 +08:00
|
|
|
static bool isValidReg(const MachineOperand &MO) {
|
2020-02-28 19:14:42 +08:00
|
|
|
return MO.isReg() && MO.getReg();
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:24:04 +08:00
|
|
|
static bool isValidRegUse(const MachineOperand &MO) {
|
2020-02-28 19:14:42 +08:00
|
|
|
return isValidReg(MO) && MO.isUse();
|
|
|
|
}
|
|
|
|
|
2021-08-04 21:21:32 +08:00
|
|
|
static bool isValidRegUseOf(const MachineOperand &MO, MCRegister PhysReg,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
|
|
|
if (!isValidRegUse(MO))
|
|
|
|
return false;
|
2022-02-27 03:29:16 +08:00
|
|
|
return TRI->regsOverlap(MO.getReg(), PhysReg);
|
2020-02-28 19:14:42 +08:00
|
|
|
}
|
|
|
|
|
2020-03-11 01:24:04 +08:00
|
|
|
static bool isValidRegDef(const MachineOperand &MO) {
|
2020-02-28 19:14:42 +08:00
|
|
|
return isValidReg(MO) && MO.isDef();
|
|
|
|
}
|
|
|
|
|
2021-08-04 21:21:32 +08:00
|
|
|
static bool isValidRegDefOf(const MachineOperand &MO, MCRegister PhysReg,
|
|
|
|
const TargetRegisterInfo *TRI) {
|
|
|
|
if (!isValidRegDef(MO))
|
|
|
|
return false;
|
2022-02-27 03:29:16 +08:00
|
|
|
return TRI->regsOverlap(MO.getReg(), PhysReg);
|
2020-02-28 19:14:42 +08:00
|
|
|
}
|
|
|
|
|
2020-04-06 03:14:59 +08:00
|
|
|
void ReachingDefAnalysis::enterBasicBlock(MachineBasicBlock *MBB) {
|
2018-01-22 21:24:10 +08:00
|
|
|
unsigned MBBNumber = MBB->getNumber();
|
2018-01-22 18:06:50 +08:00
|
|
|
assert(MBBNumber < MBBReachingDefs.size() &&
|
|
|
|
"Unexpected basic block number.");
|
|
|
|
MBBReachingDefs[MBBNumber].resize(NumRegUnits);
|
|
|
|
|
|
|
|
// Reset instruction counter in each basic block.
|
|
|
|
CurInstr = 0;
|
|
|
|
|
|
|
|
// Set up LiveRegs to represent registers entering MBB.
|
|
|
|
// Default values are 'nothing happened a long time ago'.
|
|
|
|
if (LiveRegs.empty())
|
2018-03-21 04:53:21 +08:00
|
|
|
LiveRegs.assign(NumRegUnits, ReachingDefDefaultVal);
|
2018-01-22 18:06:50 +08:00
|
|
|
|
|
|
|
// This is the entry block.
|
|
|
|
if (MBB->pred_empty()) {
|
|
|
|
for (const auto &LI : MBB->liveins()) {
|
|
|
|
for (MCRegUnitIterator Unit(LI.PhysReg, TRI); Unit.isValid(); ++Unit) {
|
|
|
|
// Treat function live-ins as if they were defined just before the first
|
|
|
|
// instruction. Usually, function arguments are set up immediately
|
|
|
|
// before the call.
|
2020-04-06 03:09:49 +08:00
|
|
|
if (LiveRegs[*Unit] != -1) {
|
|
|
|
LiveRegs[*Unit] = -1;
|
|
|
|
MBBReachingDefs[MBBNumber][*Unit].push_back(-1);
|
|
|
|
}
|
2018-01-22 18:06:50 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << ": entry\n");
|
2018-01-22 18:06:50 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to coalesce live-out registers from predecessors.
|
|
|
|
for (MachineBasicBlock *pred : MBB->predecessors()) {
|
2018-01-22 21:24:10 +08:00
|
|
|
assert(unsigned(pred->getNumber()) < MBBOutRegsInfos.size() &&
|
2018-01-22 18:06:50 +08:00
|
|
|
"Should have pre-allocated MBBInfos for all MBBs");
|
|
|
|
const LiveRegsDefInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
|
|
|
|
// Incoming is null if this is a backedge from a BB
|
|
|
|
// we haven't processed yet
|
|
|
|
if (Incoming.empty())
|
|
|
|
continue;
|
|
|
|
|
[RDA] Only store most recent reaching def from predecessors (NFCI)
When entering a basic block, RDA inserts reaching definitions coming
from predecessor blocks (which will be negative numbers) in a rather
peculiar way. If you have incoming reaching definitions -4, -3, -2, -1,
it will insert those. If you have incoming reaching definitions
-1, -2, -3, -4, it will insert -1, -1, -1, -1, as the max is taken
at each step. That's probably not what was intended...
However, RDA only actually cares about the most recent reaching
definition from a predecessor (to calculate clearance), so this
ends up working fine as far as behavior is concerned. It does
waste memory on unnecessary reaching definitions though.
This patch changes the implementation to first compute the most
recent reaching definition in one loop, and then insert only that
one in a separate loop.
Differential Revision: https://reviews.llvm.org/D77508
2020-04-05 06:46:20 +08:00
|
|
|
// Find the most recent reaching definition from a predecessor.
|
|
|
|
for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit)
|
2018-01-22 18:06:50 +08:00
|
|
|
LiveRegs[Unit] = std::max(LiveRegs[Unit], Incoming[Unit]);
|
|
|
|
}
|
|
|
|
|
[RDA] Only store most recent reaching def from predecessors (NFCI)
When entering a basic block, RDA inserts reaching definitions coming
from predecessor blocks (which will be negative numbers) in a rather
peculiar way. If you have incoming reaching definitions -4, -3, -2, -1,
it will insert those. If you have incoming reaching definitions
-1, -2, -3, -4, it will insert -1, -1, -1, -1, as the max is taken
at each step. That's probably not what was intended...
However, RDA only actually cares about the most recent reaching
definition from a predecessor (to calculate clearance), so this
ends up working fine as far as behavior is concerned. It does
waste memory on unnecessary reaching definitions though.
This patch changes the implementation to first compute the most
recent reaching definition in one loop, and then insert only that
one in a separate loop.
Differential Revision: https://reviews.llvm.org/D77508
2020-04-05 06:46:20 +08:00
|
|
|
// Insert the most recent reaching definition we found.
|
|
|
|
for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit)
|
|
|
|
if (LiveRegs[Unit] != ReachingDefDefaultVal)
|
|
|
|
MBBReachingDefs[MBBNumber][Unit].push_back(LiveRegs[Unit]);
|
2018-01-22 18:06:50 +08:00
|
|
|
}
|
|
|
|
|
2020-04-06 03:14:59 +08:00
|
|
|
void ReachingDefAnalysis::leaveBasicBlock(MachineBasicBlock *MBB) {
|
2018-01-22 18:06:50 +08:00
|
|
|
assert(!LiveRegs.empty() && "Must enter basic block first.");
|
2020-04-06 03:14:59 +08:00
|
|
|
unsigned MBBNumber = MBB->getNumber();
|
2018-01-22 18:06:50 +08:00
|
|
|
assert(MBBNumber < MBBOutRegsInfos.size() &&
|
|
|
|
"Unexpected basic block number.");
|
|
|
|
// Save register clearances at end of MBB - used by enterBasicBlock().
|
|
|
|
MBBOutRegsInfos[MBBNumber] = LiveRegs;
|
|
|
|
|
|
|
|
// While processing the basic block, we kept `Def` relative to the start
|
|
|
|
// of the basic block for convenience. However, future use of this information
|
|
|
|
// only cares about the clearance from the end of the block, so adjust
|
|
|
|
// everything to be relative to the end of the basic block.
|
|
|
|
for (int &OutLiveReg : MBBOutRegsInfos[MBBNumber])
|
2020-04-06 01:16:56 +08:00
|
|
|
if (OutLiveReg != ReachingDefDefaultVal)
|
|
|
|
OutLiveReg -= CurInstr;
|
2018-01-22 18:06:50 +08:00
|
|
|
LiveRegs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReachingDefAnalysis::processDefs(MachineInstr *MI) {
|
2018-05-09 10:42:00 +08:00
|
|
|
assert(!MI->isDebugInstr() && "Won't process debug instructions");
|
2018-01-22 18:06:50 +08:00
|
|
|
|
2018-01-22 21:24:10 +08:00
|
|
|
unsigned MBBNumber = MI->getParent()->getNumber();
|
2018-01-22 18:06:50 +08:00
|
|
|
assert(MBBNumber < MBBReachingDefs.size() &&
|
|
|
|
"Unexpected basic block number.");
|
2020-02-28 19:14:42 +08:00
|
|
|
|
|
|
|
for (auto &MO : MI->operands()) {
|
|
|
|
if (!isValidRegDef(MO))
|
2018-01-22 18:06:50 +08:00
|
|
|
continue;
|
2020-10-22 04:59:45 +08:00
|
|
|
for (MCRegUnitIterator Unit(MO.getReg().asMCReg(), TRI); Unit.isValid();
|
|
|
|
++Unit) {
|
2018-01-22 18:06:50 +08:00
|
|
|
// This instruction explicitly defines the current reg unit.
|
2021-05-18 15:07:30 +08:00
|
|
|
LLVM_DEBUG(dbgs() << printRegUnit(*Unit, TRI) << ":\t" << CurInstr
|
2018-05-14 20:53:11 +08:00
|
|
|
<< '\t' << *MI);
|
2018-01-22 18:06:50 +08:00
|
|
|
|
|
|
|
// How many instructions since this reg unit was last written?
|
2020-04-06 03:09:49 +08:00
|
|
|
if (LiveRegs[*Unit] != CurInstr) {
|
|
|
|
LiveRegs[*Unit] = CurInstr;
|
|
|
|
MBBReachingDefs[MBBNumber][*Unit].push_back(CurInstr);
|
|
|
|
}
|
2018-01-22 18:06:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
InstIds[MI] = CurInstr;
|
|
|
|
++CurInstr;
|
|
|
|
}
|
|
|
|
|
2020-04-05 06:22:54 +08:00
|
|
|
void ReachingDefAnalysis::reprocessBasicBlock(MachineBasicBlock *MBB) {
|
|
|
|
unsigned MBBNumber = MBB->getNumber();
|
|
|
|
assert(MBBNumber < MBBReachingDefs.size() &&
|
|
|
|
"Unexpected basic block number.");
|
|
|
|
|
|
|
|
// Count number of non-debug instructions for end of block adjustment.
|
2020-08-07 16:49:09 +08:00
|
|
|
auto NonDbgInsts =
|
|
|
|
instructionsWithoutDebug(MBB->instr_begin(), MBB->instr_end());
|
|
|
|
int NumInsts = std::distance(NonDbgInsts.begin(), NonDbgInsts.end());
|
2020-04-05 06:22:54 +08:00
|
|
|
|
|
|
|
// When reprocessing a block, the only thing we need to do is check whether
|
|
|
|
// there is now a more recent incoming reaching definition from a predecessor.
|
|
|
|
for (MachineBasicBlock *pred : MBB->predecessors()) {
|
|
|
|
assert(unsigned(pred->getNumber()) < MBBOutRegsInfos.size() &&
|
|
|
|
"Should have pre-allocated MBBInfos for all MBBs");
|
|
|
|
const LiveRegsDefInfo &Incoming = MBBOutRegsInfos[pred->getNumber()];
|
|
|
|
// Incoming may be empty for dead predecessors.
|
|
|
|
if (Incoming.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) {
|
|
|
|
int Def = Incoming[Unit];
|
|
|
|
if (Def == ReachingDefDefaultVal)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto Start = MBBReachingDefs[MBBNumber][Unit].begin();
|
|
|
|
if (Start != MBBReachingDefs[MBBNumber][Unit].end() && *Start < 0) {
|
|
|
|
if (*Start >= Def)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Update existing reaching def from predecessor to a more recent one.
|
|
|
|
*Start = Def;
|
|
|
|
} else {
|
|
|
|
// Insert new reaching def from predecessor.
|
|
|
|
MBBReachingDefs[MBBNumber][Unit].insert(Start, Def);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update reaching def at end of of BB. Keep in mind that these are
|
|
|
|
// adjusted relative to the end of the basic block.
|
|
|
|
if (MBBOutRegsInfos[MBBNumber][Unit] < Def - NumInsts)
|
|
|
|
MBBOutRegsInfos[MBBNumber][Unit] = Def - NumInsts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-22 18:06:50 +08:00
|
|
|
void ReachingDefAnalysis::processBasicBlock(
|
|
|
|
const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
|
2020-04-06 03:14:59 +08:00
|
|
|
MachineBasicBlock *MBB = TraversedMBB.MBB;
|
|
|
|
LLVM_DEBUG(dbgs() << printMBBReference(*MBB)
|
|
|
|
<< (!TraversedMBB.IsDone ? ": incomplete\n"
|
|
|
|
: ": all preds known\n"));
|
|
|
|
|
2020-04-05 06:22:54 +08:00
|
|
|
if (!TraversedMBB.PrimaryPass) {
|
|
|
|
// Reprocess MBB that is part of a loop.
|
|
|
|
reprocessBasicBlock(MBB);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-06 03:14:59 +08:00
|
|
|
enterBasicBlock(MBB);
|
2020-08-07 16:49:09 +08:00
|
|
|
for (MachineInstr &MI :
|
|
|
|
instructionsWithoutDebug(MBB->instr_begin(), MBB->instr_end()))
|
|
|
|
processDefs(&MI);
|
2020-04-06 03:14:59 +08:00
|
|
|
leaveBasicBlock(MBB);
|
2018-01-22 18:06:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ReachingDefAnalysis::runOnMachineFunction(MachineFunction &mf) {
|
|
|
|
MF = &mf;
|
|
|
|
TRI = MF->getSubtarget().getRegisterInfo();
|
2020-02-20 22:58:08 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "********** REACHING DEFINITION ANALYSIS **********\n");
|
|
|
|
init();
|
|
|
|
traverse();
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-22 18:06:50 +08:00
|
|
|
|
2020-02-20 22:58:08 +08:00
|
|
|
void ReachingDefAnalysis::releaseMemory() {
|
|
|
|
// Clear the internal vectors.
|
|
|
|
MBBOutRegsInfos.clear();
|
|
|
|
MBBReachingDefs.clear();
|
|
|
|
InstIds.clear();
|
2018-01-22 18:06:50 +08:00
|
|
|
LiveRegs.clear();
|
2020-02-20 22:58:08 +08:00
|
|
|
}
|
2018-01-22 18:06:50 +08:00
|
|
|
|
2020-02-20 22:58:08 +08:00
|
|
|
void ReachingDefAnalysis::reset() {
|
|
|
|
releaseMemory();
|
|
|
|
init();
|
|
|
|
traverse();
|
|
|
|
}
|
2018-01-22 18:06:50 +08:00
|
|
|
|
2020-02-20 22:58:08 +08:00
|
|
|
void ReachingDefAnalysis::init() {
|
|
|
|
NumRegUnits = TRI->getNumRegUnits();
|
|
|
|
MBBReachingDefs.resize(MF->getNumBlockIDs());
|
2018-01-22 18:06:50 +08:00
|
|
|
// Initialize the MBBOutRegsInfos
|
2020-02-20 22:58:08 +08:00
|
|
|
MBBOutRegsInfos.resize(MF->getNumBlockIDs());
|
|
|
|
LoopTraversal Traversal;
|
|
|
|
TraversedMBBOrder = Traversal.traverse(*MF);
|
|
|
|
}
|
2018-01-22 18:06:50 +08:00
|
|
|
|
2020-02-20 22:58:08 +08:00
|
|
|
void ReachingDefAnalysis::traverse() {
|
2018-01-22 18:06:50 +08:00
|
|
|
// Traverse the basic blocks.
|
2020-02-20 22:58:08 +08:00
|
|
|
for (LoopTraversal::TraversedMBBInfo TraversedMBB : TraversedMBBOrder)
|
2018-01-22 18:06:50 +08:00
|
|
|
processBasicBlock(TraversedMBB);
|
2020-04-05 06:22:54 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
// Make sure reaching defs are sorted and unique.
|
2018-01-22 18:06:50 +08:00
|
|
|
for (MBBDefsInfo &MBBDefs : MBBReachingDefs) {
|
2020-04-05 06:22:54 +08:00
|
|
|
for (MBBRegUnitDefs &RegUnitDefs : MBBDefs) {
|
|
|
|
int LastDef = ReachingDefDefaultVal;
|
|
|
|
for (int Def : RegUnitDefs) {
|
|
|
|
assert(Def > LastDef && "Defs must be sorted and unique");
|
|
|
|
LastDef = Def;
|
|
|
|
}
|
|
|
|
}
|
2018-01-22 18:06:50 +08:00
|
|
|
}
|
2020-04-05 06:22:54 +08:00
|
|
|
#endif
|
2018-01-22 18:06:50 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
int ReachingDefAnalysis::getReachingDef(MachineInstr *MI,
|
|
|
|
MCRegister PhysReg) const {
|
2018-01-22 18:06:50 +08:00
|
|
|
assert(InstIds.count(MI) && "Unexpected machine instuction.");
|
2020-01-23 21:22:13 +08:00
|
|
|
int InstId = InstIds.lookup(MI);
|
2018-03-21 04:53:21 +08:00
|
|
|
int DefRes = ReachingDefDefaultVal;
|
2018-01-22 21:24:10 +08:00
|
|
|
unsigned MBBNumber = MI->getParent()->getNumber();
|
2018-01-22 18:06:50 +08:00
|
|
|
assert(MBBNumber < MBBReachingDefs.size() &&
|
|
|
|
"Unexpected basic block number.");
|
2018-03-21 04:53:21 +08:00
|
|
|
int LatestDef = ReachingDefDefaultVal;
|
2018-01-22 18:06:50 +08:00
|
|
|
for (MCRegUnitIterator Unit(PhysReg, TRI); Unit.isValid(); ++Unit) {
|
|
|
|
for (int Def : MBBReachingDefs[MBBNumber][*Unit]) {
|
|
|
|
if (Def >= InstId)
|
|
|
|
break;
|
|
|
|
DefRes = Def;
|
|
|
|
}
|
|
|
|
LatestDef = std::max(LatestDef, DefRes);
|
|
|
|
}
|
|
|
|
return LatestDef;
|
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
MachineInstr *
|
|
|
|
ReachingDefAnalysis::getReachingLocalMIDef(MachineInstr *MI,
|
|
|
|
MCRegister PhysReg) const {
|
2020-08-25 15:35:07 +08:00
|
|
|
return hasLocalDefBefore(MI, PhysReg)
|
|
|
|
? getInstFromId(MI->getParent(), getReachingDef(MI, PhysReg))
|
|
|
|
: nullptr;
|
2019-11-26 18:03:25 +08:00
|
|
|
}
|
|
|
|
|
2019-11-26 18:25:04 +08:00
|
|
|
bool ReachingDefAnalysis::hasSameReachingDef(MachineInstr *A, MachineInstr *B,
|
2020-10-22 04:59:45 +08:00
|
|
|
MCRegister PhysReg) const {
|
2019-11-26 18:25:04 +08:00
|
|
|
MachineBasicBlock *ParentA = A->getParent();
|
|
|
|
MachineBasicBlock *ParentB = B->getParent();
|
|
|
|
if (ParentA != ParentB)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return getReachingDef(A, PhysReg) == getReachingDef(B, PhysReg);
|
|
|
|
}
|
|
|
|
|
2019-11-26 18:03:25 +08:00
|
|
|
MachineInstr *ReachingDefAnalysis::getInstFromId(MachineBasicBlock *MBB,
|
2020-01-23 21:22:13 +08:00
|
|
|
int InstId) const {
|
2019-11-26 18:25:04 +08:00
|
|
|
assert(static_cast<size_t>(MBB->getNumber()) < MBBReachingDefs.size() &&
|
2019-11-26 18:03:25 +08:00
|
|
|
"Unexpected basic block number.");
|
|
|
|
assert(InstId < static_cast<int>(MBB->size()) &&
|
|
|
|
"Unexpected instruction id.");
|
|
|
|
|
|
|
|
if (InstId < 0)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
for (auto &MI : *MBB) {
|
2020-02-06 22:13:31 +08:00
|
|
|
auto F = InstIds.find(&MI);
|
|
|
|
if (F != InstIds.end() && F->second == InstId)
|
2019-11-26 18:03:25 +08:00
|
|
|
return &MI;
|
|
|
|
}
|
2020-02-06 22:13:31 +08:00
|
|
|
|
2019-11-26 18:03:25 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
int ReachingDefAnalysis::getClearance(MachineInstr *MI,
|
|
|
|
MCRegister PhysReg) const {
|
2018-01-22 18:06:50 +08:00
|
|
|
assert(InstIds.count(MI) && "Unexpected machine instuction.");
|
2020-01-23 21:22:13 +08:00
|
|
|
return InstIds.lookup(MI) - getReachingDef(MI, PhysReg);
|
2018-01-22 18:06:50 +08:00
|
|
|
}
|
2019-11-26 18:03:25 +08:00
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
bool ReachingDefAnalysis::hasLocalDefBefore(MachineInstr *MI,
|
|
|
|
MCRegister PhysReg) const {
|
2020-01-29 16:26:11 +08:00
|
|
|
return getReachingDef(MI, PhysReg) >= 0;
|
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
void ReachingDefAnalysis::getReachingLocalUses(MachineInstr *Def,
|
|
|
|
MCRegister PhysReg,
|
2020-01-28 21:06:07 +08:00
|
|
|
InstSet &Uses) const {
|
2019-11-26 18:25:04 +08:00
|
|
|
MachineBasicBlock *MBB = Def->getParent();
|
|
|
|
MachineBasicBlock::iterator MI = MachineBasicBlock::iterator(Def);
|
|
|
|
while (++MI != MBB->end()) {
|
2020-01-24 00:44:25 +08:00
|
|
|
if (MI->isDebugInstr())
|
|
|
|
continue;
|
|
|
|
|
2019-12-20 17:32:36 +08:00
|
|
|
// If/when we find a new reaching def, we know that there's no more uses
|
|
|
|
// of 'Def'.
|
2020-02-26 19:14:54 +08:00
|
|
|
if (getReachingLocalMIDef(&*MI, PhysReg) != Def)
|
2019-12-20 17:32:36 +08:00
|
|
|
return;
|
|
|
|
|
2019-11-26 18:25:04 +08:00
|
|
|
for (auto &MO : MI->operands()) {
|
2021-08-04 21:21:32 +08:00
|
|
|
if (!isValidRegUseOf(MO, PhysReg, TRI))
|
2019-11-26 18:25:04 +08:00
|
|
|
continue;
|
|
|
|
|
2020-01-17 21:08:24 +08:00
|
|
|
Uses.insert(&*MI);
|
2019-11-26 18:25:04 +08:00
|
|
|
if (MO.isKill())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 18:03:25 +08:00
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
bool ReachingDefAnalysis::getLiveInUses(MachineBasicBlock *MBB,
|
|
|
|
MCRegister PhysReg,
|
|
|
|
InstSet &Uses) const {
|
2020-08-07 16:49:09 +08:00
|
|
|
for (MachineInstr &MI :
|
|
|
|
instructionsWithoutDebug(MBB->instr_begin(), MBB->instr_end())) {
|
2020-01-17 21:08:24 +08:00
|
|
|
for (auto &MO : MI.operands()) {
|
2021-08-04 21:21:32 +08:00
|
|
|
if (!isValidRegUseOf(MO, PhysReg, TRI))
|
2020-01-17 21:08:24 +08:00
|
|
|
continue;
|
|
|
|
if (getReachingDef(&MI, PhysReg) >= 0)
|
|
|
|
return false;
|
|
|
|
Uses.insert(&MI);
|
|
|
|
}
|
|
|
|
}
|
2020-10-10 21:50:25 +08:00
|
|
|
auto Last = MBB->getLastNonDebugInstr();
|
|
|
|
if (Last == MBB->end())
|
|
|
|
return true;
|
|
|
|
return isReachingDefLiveOut(&*Last, PhysReg);
|
2020-01-17 21:08:24 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
void ReachingDefAnalysis::getGlobalUses(MachineInstr *MI, MCRegister PhysReg,
|
|
|
|
InstSet &Uses) const {
|
2020-01-17 21:08:24 +08:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
|
|
|
|
// Collect the uses that each def touches within the block.
|
|
|
|
getReachingLocalUses(MI, PhysReg, Uses);
|
|
|
|
|
|
|
|
// Handle live-out values.
|
|
|
|
if (auto *LiveOut = getLocalLiveOutMIDef(MI->getParent(), PhysReg)) {
|
|
|
|
if (LiveOut != MI)
|
|
|
|
return;
|
|
|
|
|
2021-01-01 01:39:11 +08:00
|
|
|
SmallVector<MachineBasicBlock *, 4> ToVisit(MBB->successors());
|
2020-01-17 21:08:24 +08:00
|
|
|
SmallPtrSet<MachineBasicBlock*, 4>Visited;
|
|
|
|
while (!ToVisit.empty()) {
|
2021-09-20 04:44:23 +08:00
|
|
|
MachineBasicBlock *MBB = ToVisit.pop_back_val();
|
2020-01-17 21:08:24 +08:00
|
|
|
if (Visited.count(MBB) || !MBB->isLiveIn(PhysReg))
|
|
|
|
continue;
|
|
|
|
if (getLiveInUses(MBB, PhysReg, Uses))
|
2020-12-29 11:55:16 +08:00
|
|
|
llvm::append_range(ToVisit, MBB->successors());
|
2020-01-17 21:08:24 +08:00
|
|
|
Visited.insert(MBB);
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 18:03:25 +08:00
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
void ReachingDefAnalysis::getGlobalReachingDefs(MachineInstr *MI,
|
|
|
|
MCRegister PhysReg,
|
|
|
|
InstSet &Defs) const {
|
2020-08-28 20:56:16 +08:00
|
|
|
if (auto *Def = getUniqueReachingMIDef(MI, PhysReg)) {
|
|
|
|
Defs.insert(Def);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto *MBB : MI->getParent()->predecessors())
|
|
|
|
getLiveOuts(MBB, PhysReg, Defs);
|
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
void ReachingDefAnalysis::getLiveOuts(MachineBasicBlock *MBB,
|
|
|
|
MCRegister PhysReg, InstSet &Defs) const {
|
2020-07-01 15:27:12 +08:00
|
|
|
SmallPtrSet<MachineBasicBlock*, 2> VisitedBBs;
|
|
|
|
getLiveOuts(MBB, PhysReg, Defs, VisitedBBs);
|
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
void ReachingDefAnalysis::getLiveOuts(MachineBasicBlock *MBB,
|
|
|
|
MCRegister PhysReg, InstSet &Defs,
|
|
|
|
BlockSet &VisitedBBs) const {
|
2020-02-26 19:14:54 +08:00
|
|
|
if (VisitedBBs.count(MBB))
|
|
|
|
return;
|
|
|
|
|
|
|
|
VisitedBBs.insert(MBB);
|
|
|
|
LivePhysRegs LiveRegs(*TRI);
|
|
|
|
LiveRegs.addLiveOuts(*MBB);
|
2021-08-04 21:21:32 +08:00
|
|
|
if (LiveRegs.available(MBB->getParent()->getRegInfo(), PhysReg))
|
2020-02-26 19:14:54 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (auto *Def = getLocalLiveOutMIDef(MBB, PhysReg))
|
|
|
|
Defs.insert(Def);
|
|
|
|
else
|
|
|
|
for (auto *Pred : MBB->predecessors())
|
|
|
|
getLiveOuts(Pred, PhysReg, Defs, VisitedBBs);
|
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
MachineInstr *
|
|
|
|
ReachingDefAnalysis::getUniqueReachingMIDef(MachineInstr *MI,
|
|
|
|
MCRegister PhysReg) const {
|
2020-02-26 19:14:54 +08:00
|
|
|
// If there's a local def before MI, return it.
|
|
|
|
MachineInstr *LocalDef = getReachingLocalMIDef(MI, PhysReg);
|
2020-03-03 23:19:57 +08:00
|
|
|
if (LocalDef && InstIds.lookup(LocalDef) < InstIds.lookup(MI))
|
2020-02-26 19:14:54 +08:00
|
|
|
return LocalDef;
|
|
|
|
|
|
|
|
SmallPtrSet<MachineInstr*, 2> Incoming;
|
2020-08-26 18:11:15 +08:00
|
|
|
MachineBasicBlock *Parent = MI->getParent();
|
|
|
|
for (auto *Pred : Parent->predecessors())
|
2020-09-16 19:42:58 +08:00
|
|
|
getLiveOuts(Pred, PhysReg, Incoming);
|
2020-02-26 19:14:54 +08:00
|
|
|
|
2020-09-16 19:42:58 +08:00
|
|
|
// Check that we have a single incoming value and that it does not
|
|
|
|
// come from the same block as MI - since it would mean that the def
|
|
|
|
// is executed after MI.
|
|
|
|
if (Incoming.size() == 1 && (*Incoming.begin())->getParent() != Parent)
|
2020-02-26 19:14:54 +08:00
|
|
|
return *Incoming.begin();
|
2020-09-16 19:42:58 +08:00
|
|
|
return nullptr;
|
2020-02-26 19:14:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *ReachingDefAnalysis::getMIOperand(MachineInstr *MI,
|
|
|
|
unsigned Idx) const {
|
|
|
|
assert(MI->getOperand(Idx).isReg() && "Expected register operand");
|
|
|
|
return getUniqueReachingMIDef(MI, MI->getOperand(Idx).getReg());
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *ReachingDefAnalysis::getMIOperand(MachineInstr *MI,
|
|
|
|
MachineOperand &MO) const {
|
|
|
|
assert(MO.isReg() && "Expected register operand");
|
|
|
|
return getUniqueReachingMIDef(MI, MO.getReg());
|
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
bool ReachingDefAnalysis::isRegUsedAfter(MachineInstr *MI,
|
|
|
|
MCRegister PhysReg) const {
|
2019-11-26 18:03:25 +08:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
LivePhysRegs LiveRegs(*TRI);
|
|
|
|
LiveRegs.addLiveOuts(*MBB);
|
|
|
|
|
|
|
|
// Yes if the register is live out of the basic block.
|
2021-08-04 21:21:32 +08:00
|
|
|
if (!LiveRegs.available(MBB->getParent()->getRegInfo(), PhysReg))
|
2019-11-26 18:03:25 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
// Walk backwards through the block to see if the register is live at some
|
|
|
|
// point.
|
2020-08-07 16:49:09 +08:00
|
|
|
for (MachineInstr &Last :
|
|
|
|
instructionsWithoutDebug(MBB->instr_rbegin(), MBB->instr_rend())) {
|
|
|
|
LiveRegs.stepBackward(Last);
|
2021-08-04 21:21:32 +08:00
|
|
|
if (!LiveRegs.available(MBB->getParent()->getRegInfo(), PhysReg))
|
2020-08-07 16:49:09 +08:00
|
|
|
return InstIds.lookup(&Last) > InstIds.lookup(MI);
|
2019-11-26 18:03:25 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-29 16:26:11 +08:00
|
|
|
bool ReachingDefAnalysis::isRegDefinedAfter(MachineInstr *MI,
|
2020-10-22 04:59:45 +08:00
|
|
|
MCRegister PhysReg) const {
|
2020-01-29 16:26:11 +08:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2020-10-10 21:50:25 +08:00
|
|
|
auto Last = MBB->getLastNonDebugInstr();
|
|
|
|
if (Last != MBB->end() &&
|
|
|
|
getReachingDef(MI, PhysReg) != getReachingDef(&*Last, PhysReg))
|
2020-01-29 16:26:11 +08:00
|
|
|
return true;
|
|
|
|
|
|
|
|
if (auto *Def = getLocalLiveOutMIDef(MBB, PhysReg))
|
2020-02-26 19:14:54 +08:00
|
|
|
return Def == getReachingLocalMIDef(MI, PhysReg);
|
2020-01-29 16:26:11 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
bool ReachingDefAnalysis::isReachingDefLiveOut(MachineInstr *MI,
|
|
|
|
MCRegister PhysReg) const {
|
2019-12-20 17:32:36 +08:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
LivePhysRegs LiveRegs(*TRI);
|
|
|
|
LiveRegs.addLiveOuts(*MBB);
|
2021-08-04 21:21:32 +08:00
|
|
|
if (LiveRegs.available(MBB->getParent()->getRegInfo(), PhysReg))
|
2019-12-20 17:32:36 +08:00
|
|
|
return false;
|
|
|
|
|
2020-10-10 21:50:25 +08:00
|
|
|
auto Last = MBB->getLastNonDebugInstr();
|
2019-12-20 17:32:36 +08:00
|
|
|
int Def = getReachingDef(MI, PhysReg);
|
2020-10-10 21:50:25 +08:00
|
|
|
if (Last != MBB->end() && getReachingDef(&*Last, PhysReg) != Def)
|
2019-12-20 17:32:36 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Finally check that the last instruction doesn't redefine the register.
|
|
|
|
for (auto &MO : Last->operands())
|
2021-08-04 21:21:32 +08:00
|
|
|
if (isValidRegDefOf(MO, PhysReg, TRI))
|
2019-12-20 17:32:36 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
MachineInstr *
|
|
|
|
ReachingDefAnalysis::getLocalLiveOutMIDef(MachineBasicBlock *MBB,
|
|
|
|
MCRegister PhysReg) const {
|
2019-12-20 17:32:36 +08:00
|
|
|
LivePhysRegs LiveRegs(*TRI);
|
|
|
|
LiveRegs.addLiveOuts(*MBB);
|
2021-08-04 21:21:32 +08:00
|
|
|
if (LiveRegs.available(MBB->getParent()->getRegInfo(), PhysReg))
|
2019-12-20 17:32:36 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2020-10-10 21:50:25 +08:00
|
|
|
auto Last = MBB->getLastNonDebugInstr();
|
|
|
|
if (Last == MBB->end())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
int Def = getReachingDef(&*Last, PhysReg);
|
2019-12-20 17:32:36 +08:00
|
|
|
for (auto &MO : Last->operands())
|
2021-08-04 21:21:32 +08:00
|
|
|
if (isValidRegDefOf(MO, PhysReg, TRI))
|
2020-10-10 21:50:25 +08:00
|
|
|
return &*Last;
|
2019-12-20 17:32:36 +08:00
|
|
|
|
|
|
|
return Def < 0 ? nullptr : getInstFromId(MBB, Def);
|
|
|
|
}
|
2020-01-29 16:26:11 +08:00
|
|
|
|
2020-02-06 21:53:09 +08:00
|
|
|
static bool mayHaveSideEffects(MachineInstr &MI) {
|
|
|
|
return MI.mayLoadOrStore() || MI.mayRaiseFPException() ||
|
|
|
|
MI.hasUnmodeledSideEffects() || MI.isTerminator() ||
|
|
|
|
MI.isCall() || MI.isBarrier() || MI.isBranch() || MI.isReturn();
|
|
|
|
}
|
|
|
|
|
2020-01-29 16:26:11 +08:00
|
|
|
// Can we safely move 'From' to just before 'To'? To satisfy this, 'From' must
|
|
|
|
// not define a register that is used by any instructions, after and including,
|
|
|
|
// 'To'. These instructions also must not redefine any of Froms operands.
|
|
|
|
template<typename Iterator>
|
|
|
|
bool ReachingDefAnalysis::isSafeToMove(MachineInstr *From,
|
|
|
|
MachineInstr *To) const {
|
2020-09-28 22:46:56 +08:00
|
|
|
if (From->getParent() != To->getParent() || From == To)
|
2020-01-29 16:26:11 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallSet<int, 2> Defs;
|
|
|
|
// First check that From would compute the same value if moved.
|
|
|
|
for (auto &MO : From->operands()) {
|
2020-02-28 19:14:42 +08:00
|
|
|
if (!isValidReg(MO))
|
2020-01-29 16:26:11 +08:00
|
|
|
continue;
|
|
|
|
if (MO.isDef())
|
|
|
|
Defs.insert(MO.getReg());
|
|
|
|
else if (!hasSameReachingDef(From, To, MO.getReg()))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now walk checking that the rest of the instructions will compute the same
|
2020-02-06 21:53:09 +08:00
|
|
|
// value and that we're not overwriting anything. Don't move the instruction
|
2020-04-29 05:50:52 +08:00
|
|
|
// past any memory, control-flow or other ambiguous instructions.
|
2020-01-29 16:26:11 +08:00
|
|
|
for (auto I = ++Iterator(From), E = Iterator(To); I != E; ++I) {
|
2020-02-06 21:53:09 +08:00
|
|
|
if (mayHaveSideEffects(*I))
|
|
|
|
return false;
|
2020-01-29 16:26:11 +08:00
|
|
|
for (auto &MO : I->operands())
|
2020-02-06 21:53:09 +08:00
|
|
|
if (MO.isReg() && MO.getReg() && Defs.count(MO.getReg()))
|
2020-01-29 16:26:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReachingDefAnalysis::isSafeToMoveForwards(MachineInstr *From,
|
|
|
|
MachineInstr *To) const {
|
2020-09-28 22:46:56 +08:00
|
|
|
using Iterator = MachineBasicBlock::iterator;
|
|
|
|
// Walk forwards until we find the instruction.
|
|
|
|
for (auto I = Iterator(From), E = From->getParent()->end(); I != E; ++I)
|
|
|
|
if (&*I == To)
|
|
|
|
return isSafeToMove<Iterator>(From, To);
|
|
|
|
return false;
|
2020-01-29 16:26:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ReachingDefAnalysis::isSafeToMoveBackwards(MachineInstr *From,
|
|
|
|
MachineInstr *To) const {
|
2020-09-28 22:46:56 +08:00
|
|
|
using Iterator = MachineBasicBlock::reverse_iterator;
|
|
|
|
// Walk backwards until we find the instruction.
|
|
|
|
for (auto I = Iterator(From), E = From->getParent()->rend(); I != E; ++I)
|
|
|
|
if (&*I == To)
|
|
|
|
return isSafeToMove<Iterator>(From, To);
|
|
|
|
return false;
|
2020-01-29 16:26:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ReachingDefAnalysis::isSafeToRemove(MachineInstr *MI,
|
|
|
|
InstSet &ToRemove) const {
|
|
|
|
SmallPtrSet<MachineInstr*, 1> Ignore;
|
|
|
|
SmallPtrSet<MachineInstr*, 2> Visited;
|
|
|
|
return isSafeToRemove(MI, Visited, ToRemove, Ignore);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ReachingDefAnalysis::isSafeToRemove(MachineInstr *MI, InstSet &ToRemove,
|
|
|
|
InstSet &Ignore) const {
|
|
|
|
SmallPtrSet<MachineInstr*, 2> Visited;
|
|
|
|
return isSafeToRemove(MI, Visited, ToRemove, Ignore);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ReachingDefAnalysis::isSafeToRemove(MachineInstr *MI, InstSet &Visited,
|
|
|
|
InstSet &ToRemove, InstSet &Ignore) const {
|
|
|
|
if (Visited.count(MI) || Ignore.count(MI))
|
|
|
|
return true;
|
2020-02-06 21:53:09 +08:00
|
|
|
else if (mayHaveSideEffects(*MI)) {
|
2020-01-29 16:26:11 +08:00
|
|
|
// Unless told to ignore the instruction, don't remove anything which has
|
|
|
|
// side effects.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Visited.insert(MI);
|
|
|
|
for (auto &MO : MI->operands()) {
|
2020-02-28 19:14:42 +08:00
|
|
|
if (!isValidRegDef(MO))
|
2020-01-29 16:26:11 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
SmallPtrSet<MachineInstr*, 4> Uses;
|
|
|
|
getGlobalUses(MI, MO.getReg(), Uses);
|
|
|
|
|
|
|
|
for (auto I : Uses) {
|
|
|
|
if (Ignore.count(I) || ToRemove.count(I))
|
|
|
|
continue;
|
|
|
|
if (!isSafeToRemove(I, Visited, ToRemove, Ignore))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ToRemove.insert(MI);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-03-03 23:19:57 +08:00
|
|
|
void ReachingDefAnalysis::collectKilledOperands(MachineInstr *MI,
|
|
|
|
InstSet &Dead) const {
|
2020-02-24 21:50:46 +08:00
|
|
|
Dead.insert(MI);
|
2020-10-22 04:59:45 +08:00
|
|
|
auto IsDead = [this, &Dead](MachineInstr *Def, MCRegister PhysReg) {
|
2020-09-30 16:36:57 +08:00
|
|
|
if (mayHaveSideEffects(*Def))
|
|
|
|
return false;
|
|
|
|
|
2020-02-24 21:50:46 +08:00
|
|
|
unsigned LiveDefs = 0;
|
2020-02-28 19:14:42 +08:00
|
|
|
for (auto &MO : Def->operands()) {
|
|
|
|
if (!isValidRegDef(MO))
|
|
|
|
continue;
|
2020-02-24 21:50:46 +08:00
|
|
|
if (!MO.isDead())
|
|
|
|
++LiveDefs;
|
2020-02-28 19:14:42 +08:00
|
|
|
}
|
2020-02-24 21:50:46 +08:00
|
|
|
|
|
|
|
if (LiveDefs > 1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
SmallPtrSet<MachineInstr*, 4> Uses;
|
|
|
|
getGlobalUses(Def, PhysReg, Uses);
|
2021-03-01 02:59:20 +08:00
|
|
|
return llvm::set_is_subset(Uses, Dead);
|
2020-02-24 21:50:46 +08:00
|
|
|
};
|
|
|
|
|
2020-02-28 19:14:42 +08:00
|
|
|
for (auto &MO : MI->operands()) {
|
2020-03-03 23:19:57 +08:00
|
|
|
if (!isValidRegUse(MO))
|
2020-02-24 21:50:46 +08:00
|
|
|
continue;
|
2020-03-03 23:19:57 +08:00
|
|
|
if (MachineInstr *Def = getMIOperand(MI, MO))
|
2020-02-24 21:50:46 +08:00
|
|
|
if (IsDead(Def, MO.getReg()))
|
2020-03-03 23:19:57 +08:00
|
|
|
collectKilledOperands(Def, Dead);
|
2020-02-24 21:50:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-29 16:26:11 +08:00
|
|
|
bool ReachingDefAnalysis::isSafeToDefRegAt(MachineInstr *MI,
|
2020-10-22 04:59:45 +08:00
|
|
|
MCRegister PhysReg) const {
|
2020-01-29 16:26:11 +08:00
|
|
|
SmallPtrSet<MachineInstr*, 1> Ignore;
|
|
|
|
return isSafeToDefRegAt(MI, PhysReg, Ignore);
|
|
|
|
}
|
|
|
|
|
2020-10-22 04:59:45 +08:00
|
|
|
bool ReachingDefAnalysis::isSafeToDefRegAt(MachineInstr *MI, MCRegister PhysReg,
|
2020-01-29 16:26:11 +08:00
|
|
|
InstSet &Ignore) const {
|
|
|
|
// Check for any uses of the register after MI.
|
|
|
|
if (isRegUsedAfter(MI, PhysReg)) {
|
2020-02-26 19:14:54 +08:00
|
|
|
if (auto *Def = getReachingLocalMIDef(MI, PhysReg)) {
|
2020-01-29 16:26:11 +08:00
|
|
|
SmallPtrSet<MachineInstr*, 2> Uses;
|
2020-09-30 19:25:06 +08:00
|
|
|
getGlobalUses(Def, PhysReg, Uses);
|
2021-03-01 02:59:20 +08:00
|
|
|
if (!llvm::set_is_subset(Uses, Ignore))
|
|
|
|
return false;
|
2020-01-29 16:26:11 +08:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
// Check for any defs after MI.
|
|
|
|
if (isRegDefinedAfter(MI, PhysReg)) {
|
|
|
|
auto I = MachineBasicBlock::iterator(MI);
|
|
|
|
for (auto E = MBB->end(); I != E; ++I) {
|
|
|
|
if (Ignore.count(&*I))
|
|
|
|
continue;
|
|
|
|
for (auto &MO : I->operands())
|
2021-08-04 21:21:32 +08:00
|
|
|
if (isValidRegDefOf(MO, PhysReg, TRI))
|
2020-01-29 16:26:11 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|