2018-01-22 11:06:50 +01:00
|
|
|
//===---- ReachingDefAnalysis.cpp - Reaching Def Analysis ---*- C++ -*-----===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01: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 11:06:50 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2019-11-26 11:03:25 +01:00
|
|
|
#include "llvm/CodeGen/LivePhysRegs.h"
|
2018-01-22 11:06:50 +01:00
|
|
|
#include "llvm/CodeGen/ReachingDefAnalysis.h"
|
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2019-10-19 02:22:07 +02:00
|
|
|
#include "llvm/Support/Debug.h"
|
2018-01-22 11:06:50 +01:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "reaching-deps-analysis"
|
|
|
|
|
|
|
|
char ReachingDefAnalysis::ID = 0;
|
|
|
|
INITIALIZE_PASS(ReachingDefAnalysis, DEBUG_TYPE, "ReachingDefAnalysis", false,
|
|
|
|
true)
|
|
|
|
|
|
|
|
void ReachingDefAnalysis::enterBasicBlock(
|
|
|
|
const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
|
|
|
|
|
|
|
|
MachineBasicBlock *MBB = TraversedMBB.MBB;
|
2018-01-22 14:24:10 +01:00
|
|
|
unsigned MBBNumber = MBB->getNumber();
|
2018-01-22 11:06:50 +01: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-20 21:53:21 +01:00
|
|
|
LiveRegs.assign(NumRegUnits, ReachingDefDefaultVal);
|
2018-01-22 11:06:50 +01: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.
|
|
|
|
LiveRegs[*Unit] = -1;
|
|
|
|
MBBReachingDefs[MBBNumber][*Unit].push_back(LiveRegs[*Unit]);
|
|
|
|
}
|
|
|
|
}
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << printMBBReference(*MBB) << ": entry\n");
|
2018-01-22 11:06:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to coalesce live-out registers from predecessors.
|
|
|
|
for (MachineBasicBlock *pred : MBB->predecessors()) {
|
2018-01-22 14:24:10 +01:00
|
|
|
assert(unsigned(pred->getNumber()) < MBBOutRegsInfos.size() &&
|
2018-01-22 11:06:50 +01: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;
|
|
|
|
|
|
|
|
for (unsigned Unit = 0; Unit != NumRegUnits; ++Unit) {
|
|
|
|
// Use the most recent predecessor def for each register.
|
|
|
|
LiveRegs[Unit] = std::max(LiveRegs[Unit], Incoming[Unit]);
|
2018-03-20 21:53:21 +01:00
|
|
|
if ((LiveRegs[Unit] != ReachingDefDefaultVal))
|
2018-01-22 11:06:50 +01:00
|
|
|
MBBReachingDefs[MBBNumber][Unit].push_back(LiveRegs[Unit]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << printMBBReference(*MBB)
|
|
|
|
<< (!TraversedMBB.IsDone ? ": incomplete\n"
|
|
|
|
: ": all preds known\n"));
|
2018-01-22 11:06:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ReachingDefAnalysis::leaveBasicBlock(
|
|
|
|
const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
|
|
|
|
assert(!LiveRegs.empty() && "Must enter basic block first.");
|
2018-01-22 14:24:10 +01:00
|
|
|
unsigned MBBNumber = TraversedMBB.MBB->getNumber();
|
2018-01-22 11:06:50 +01: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])
|
|
|
|
OutLiveReg -= CurInstr;
|
|
|
|
LiveRegs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReachingDefAnalysis::processDefs(MachineInstr *MI) {
|
2018-05-09 04:42:00 +02:00
|
|
|
assert(!MI->isDebugInstr() && "Won't process debug instructions");
|
2018-01-22 11:06:50 +01:00
|
|
|
|
2018-01-22 14:24:10 +01:00
|
|
|
unsigned MBBNumber = MI->getParent()->getNumber();
|
2018-01-22 11:06:50 +01:00
|
|
|
assert(MBBNumber < MBBReachingDefs.size() &&
|
|
|
|
"Unexpected basic block number.");
|
|
|
|
const MCInstrDesc &MCID = MI->getDesc();
|
|
|
|
for (unsigned i = 0,
|
|
|
|
e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs();
|
|
|
|
i != e; ++i) {
|
|
|
|
MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (!MO.isReg() || !MO.getReg())
|
|
|
|
continue;
|
|
|
|
if (MO.isUse())
|
|
|
|
continue;
|
|
|
|
for (MCRegUnitIterator Unit(MO.getReg(), TRI); Unit.isValid(); ++Unit) {
|
|
|
|
// This instruction explicitly defines the current reg unit.
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << printReg(MO.getReg(), TRI) << ":\t" << CurInstr
|
|
|
|
<< '\t' << *MI);
|
2018-01-22 11:06:50 +01:00
|
|
|
|
|
|
|
// How many instructions since this reg unit was last written?
|
|
|
|
LiveRegs[*Unit] = CurInstr;
|
|
|
|
MBBReachingDefs[MBBNumber][*Unit].push_back(CurInstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
InstIds[MI] = CurInstr;
|
|
|
|
++CurInstr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReachingDefAnalysis::processBasicBlock(
|
|
|
|
const LoopTraversal::TraversedMBBInfo &TraversedMBB) {
|
|
|
|
enterBasicBlock(TraversedMBB);
|
|
|
|
for (MachineInstr &MI : *TraversedMBB.MBB) {
|
2018-05-09 04:42:00 +02:00
|
|
|
if (!MI.isDebugInstr())
|
2018-01-22 11:06:50 +01:00
|
|
|
processDefs(&MI);
|
|
|
|
}
|
|
|
|
leaveBasicBlock(TraversedMBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReachingDefAnalysis::runOnMachineFunction(MachineFunction &mf) {
|
|
|
|
MF = &mf;
|
|
|
|
TRI = MF->getSubtarget().getRegisterInfo();
|
|
|
|
|
|
|
|
LiveRegs.clear();
|
|
|
|
NumRegUnits = TRI->getNumRegUnits();
|
|
|
|
|
|
|
|
MBBReachingDefs.resize(mf.getNumBlockIDs());
|
|
|
|
|
2018-05-14 14:53:11 +02:00
|
|
|
LLVM_DEBUG(dbgs() << "********** REACHING DEFINITION ANALYSIS **********\n");
|
2018-01-22 11:06:50 +01:00
|
|
|
|
|
|
|
// Initialize the MBBOutRegsInfos
|
|
|
|
MBBOutRegsInfos.resize(mf.getNumBlockIDs());
|
|
|
|
|
|
|
|
// Traverse the basic blocks.
|
|
|
|
LoopTraversal Traversal;
|
|
|
|
LoopTraversal::TraversalOrder TraversedMBBOrder = Traversal.traverse(mf);
|
|
|
|
for (LoopTraversal::TraversedMBBInfo TraversedMBB : TraversedMBBOrder) {
|
|
|
|
processBasicBlock(TraversedMBB);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sorting all reaching defs found for a ceartin reg unit in a given BB.
|
|
|
|
for (MBBDefsInfo &MBBDefs : MBBReachingDefs) {
|
|
|
|
for (MBBRegUnitDefs &RegUnitDefs : MBBDefs)
|
llvm::sort(C.begin(), C.end(), ...) -> llvm::sort(C, ...)
Summary: The convenience wrapper in STLExtras is available since rL342102.
Reviewers: dblaikie, javed.absar, JDevlieghere, andreadb
Subscribers: MatzeB, sanjoy, arsenm, dschuff, mehdi_amini, sdardis, nemanjai, jvesely, nhaehnle, sbc100, jgravelle-google, eraman, aheejin, kbarton, JDevlieghere, javed.absar, gbedwell, jrtc27, mgrang, atanasyan, steven_wu, george.burgess.iv, dexonsmith, kristina, jsji, llvm-commits
Differential Revision: https://reviews.llvm.org/D52573
llvm-svn: 343163
2018-09-27 04:13:45 +02:00
|
|
|
llvm::sort(RegUnitDefs);
|
2018-01-22 11:06:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReachingDefAnalysis::releaseMemory() {
|
|
|
|
// Clear the internal vectors.
|
|
|
|
MBBOutRegsInfos.clear();
|
|
|
|
MBBReachingDefs.clear();
|
|
|
|
InstIds.clear();
|
|
|
|
}
|
|
|
|
|
2020-01-23 14:22:13 +01:00
|
|
|
int ReachingDefAnalysis::getReachingDef(MachineInstr *MI, int PhysReg) const {
|
2018-01-22 11:06:50 +01:00
|
|
|
assert(InstIds.count(MI) && "Unexpected machine instuction.");
|
2020-01-23 14:22:13 +01:00
|
|
|
int InstId = InstIds.lookup(MI);
|
2018-03-20 21:53:21 +01:00
|
|
|
int DefRes = ReachingDefDefaultVal;
|
2018-01-22 14:24:10 +01:00
|
|
|
unsigned MBBNumber = MI->getParent()->getNumber();
|
2018-01-22 11:06:50 +01:00
|
|
|
assert(MBBNumber < MBBReachingDefs.size() &&
|
|
|
|
"Unexpected basic block number.");
|
2018-03-20 21:53:21 +01:00
|
|
|
int LatestDef = ReachingDefDefaultVal;
|
2018-01-22 11:06:50 +01: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-01-23 14:22:13 +01:00
|
|
|
MachineInstr* ReachingDefAnalysis::getReachingMIDef(MachineInstr *MI,
|
|
|
|
int PhysReg) const {
|
2019-11-26 11:03:25 +01:00
|
|
|
return getInstFromId(MI->getParent(), getReachingDef(MI, PhysReg));
|
|
|
|
}
|
|
|
|
|
2019-11-26 11:25:04 +01:00
|
|
|
bool ReachingDefAnalysis::hasSameReachingDef(MachineInstr *A, MachineInstr *B,
|
2020-01-23 14:22:13 +01:00
|
|
|
int PhysReg) const {
|
2019-11-26 11:25:04 +01:00
|
|
|
MachineBasicBlock *ParentA = A->getParent();
|
|
|
|
MachineBasicBlock *ParentB = B->getParent();
|
|
|
|
if (ParentA != ParentB)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return getReachingDef(A, PhysReg) == getReachingDef(B, PhysReg);
|
|
|
|
}
|
|
|
|
|
2019-11-26 11:03:25 +01:00
|
|
|
MachineInstr *ReachingDefAnalysis::getInstFromId(MachineBasicBlock *MBB,
|
2020-01-23 14:22:13 +01:00
|
|
|
int InstId) const {
|
2019-11-26 11:25:04 +01:00
|
|
|
assert(static_cast<size_t>(MBB->getNumber()) < MBBReachingDefs.size() &&
|
2019-11-26 11:03:25 +01: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-01-23 14:22:13 +01:00
|
|
|
if (InstIds.count(&MI) && InstIds.lookup(&MI) == InstId)
|
2019-11-26 11:03:25 +01:00
|
|
|
return &MI;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-01-23 14:22:13 +01:00
|
|
|
int
|
|
|
|
ReachingDefAnalysis::getClearance(MachineInstr *MI, MCPhysReg PhysReg) const {
|
2018-01-22 11:06:50 +01:00
|
|
|
assert(InstIds.count(MI) && "Unexpected machine instuction.");
|
2020-01-23 14:22:13 +01:00
|
|
|
return InstIds.lookup(MI) - getReachingDef(MI, PhysReg);
|
2018-01-22 11:06:50 +01:00
|
|
|
}
|
2019-11-26 11:03:25 +01:00
|
|
|
|
2019-11-26 11:25:04 +01:00
|
|
|
void ReachingDefAnalysis::getReachingLocalUses(MachineInstr *Def, int PhysReg,
|
2020-01-28 14:06:07 +01:00
|
|
|
InstSet &Uses) const {
|
2019-11-26 11:25:04 +01:00
|
|
|
MachineBasicBlock *MBB = Def->getParent();
|
|
|
|
MachineBasicBlock::iterator MI = MachineBasicBlock::iterator(Def);
|
|
|
|
while (++MI != MBB->end()) {
|
2020-01-23 17:44:25 +01:00
|
|
|
if (MI->isDebugInstr())
|
|
|
|
continue;
|
|
|
|
|
2019-12-20 10:32:36 +01:00
|
|
|
// If/when we find a new reaching def, we know that there's no more uses
|
|
|
|
// of 'Def'.
|
|
|
|
if (getReachingMIDef(&*MI, PhysReg) != Def)
|
|
|
|
return;
|
|
|
|
|
2019-11-26 11:25:04 +01:00
|
|
|
for (auto &MO : MI->operands()) {
|
|
|
|
if (!MO.isReg() || !MO.isUse() || MO.getReg() != PhysReg)
|
|
|
|
continue;
|
|
|
|
|
2020-01-17 14:08:24 +01:00
|
|
|
Uses.insert(&*MI);
|
2019-11-26 11:25:04 +01:00
|
|
|
if (MO.isKill())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 11:03:25 +01:00
|
|
|
|
2020-01-23 14:22:13 +01:00
|
|
|
bool
|
|
|
|
ReachingDefAnalysis::getLiveInUses(MachineBasicBlock *MBB, int PhysReg,
|
2020-01-28 14:06:07 +01:00
|
|
|
InstSet &Uses) const {
|
2020-01-17 14:08:24 +01:00
|
|
|
for (auto &MI : *MBB) {
|
2020-01-23 17:44:25 +01:00
|
|
|
if (MI.isDebugInstr())
|
|
|
|
continue;
|
2020-01-17 14:08:24 +01:00
|
|
|
for (auto &MO : MI.operands()) {
|
|
|
|
if (!MO.isReg() || !MO.isUse() || MO.getReg() != PhysReg)
|
|
|
|
continue;
|
|
|
|
if (getReachingDef(&MI, PhysReg) >= 0)
|
|
|
|
return false;
|
|
|
|
Uses.insert(&MI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isReachingDefLiveOut(&MBB->back(), PhysReg);
|
|
|
|
}
|
|
|
|
|
2020-01-23 14:22:13 +01:00
|
|
|
void
|
|
|
|
ReachingDefAnalysis::getGlobalUses(MachineInstr *MI, int PhysReg,
|
2020-01-28 14:06:07 +01:00
|
|
|
InstSet &Uses) const {
|
2020-01-17 14:08:24 +01: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;
|
|
|
|
|
|
|
|
SmallVector<MachineBasicBlock*, 4> ToVisit;
|
|
|
|
ToVisit.insert(ToVisit.begin(), MBB->successors().begin(),
|
|
|
|
MBB->successors().end());
|
|
|
|
SmallPtrSet<MachineBasicBlock*, 4>Visited;
|
|
|
|
while (!ToVisit.empty()) {
|
|
|
|
MachineBasicBlock *MBB = ToVisit.back();
|
|
|
|
ToVisit.pop_back();
|
|
|
|
if (Visited.count(MBB) || !MBB->isLiveIn(PhysReg))
|
|
|
|
continue;
|
|
|
|
if (getLiveInUses(MBB, PhysReg, Uses))
|
|
|
|
ToVisit.insert(ToVisit.end(), MBB->successors().begin(),
|
|
|
|
MBB->successors().end());
|
|
|
|
Visited.insert(MBB);
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 11:03:25 +01:00
|
|
|
}
|
|
|
|
|
2020-01-23 14:22:13 +01:00
|
|
|
bool ReachingDefAnalysis::isRegUsedAfter(MachineInstr *MI, int PhysReg) const {
|
2019-11-26 11:03:25 +01:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
LivePhysRegs LiveRegs(*TRI);
|
|
|
|
LiveRegs.addLiveOuts(*MBB);
|
|
|
|
|
|
|
|
// Yes if the register is live out of the basic block.
|
|
|
|
if (LiveRegs.contains(PhysReg))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Walk backwards through the block to see if the register is live at some
|
|
|
|
// point.
|
|
|
|
for (auto Last = MBB->rbegin(), End = MBB->rend(); Last != End; ++Last) {
|
|
|
|
LiveRegs.stepBackward(*Last);
|
|
|
|
if (LiveRegs.contains(PhysReg))
|
2020-01-23 14:22:13 +01:00
|
|
|
return InstIds.lookup(&*Last) > InstIds.lookup(MI);
|
2019-11-26 11:03:25 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-23 14:22:13 +01:00
|
|
|
bool
|
|
|
|
ReachingDefAnalysis::isReachingDefLiveOut(MachineInstr *MI, int PhysReg) const {
|
2019-12-20 10:32:36 +01:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
|
|
|
LivePhysRegs LiveRegs(*TRI);
|
|
|
|
LiveRegs.addLiveOuts(*MBB);
|
|
|
|
if (!LiveRegs.contains(PhysReg))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
MachineInstr *Last = &MBB->back();
|
|
|
|
int Def = getReachingDef(MI, PhysReg);
|
|
|
|
if (getReachingDef(Last, PhysReg) != Def)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Finally check that the last instruction doesn't redefine the register.
|
|
|
|
for (auto &MO : Last->operands())
|
|
|
|
if (MO.isReg() && MO.isDef() && MO.getReg() == PhysReg)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr* ReachingDefAnalysis::getLocalLiveOutMIDef(MachineBasicBlock *MBB,
|
2020-01-23 14:22:13 +01:00
|
|
|
int PhysReg) const {
|
2019-12-20 10:32:36 +01:00
|
|
|
LivePhysRegs LiveRegs(*TRI);
|
|
|
|
LiveRegs.addLiveOuts(*MBB);
|
|
|
|
if (!LiveRegs.contains(PhysReg))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
MachineInstr *Last = &MBB->back();
|
|
|
|
int Def = getReachingDef(Last, PhysReg);
|
|
|
|
for (auto &MO : Last->operands())
|
|
|
|
if (MO.isReg() && MO.isDef() && MO.getReg() == PhysReg)
|
|
|
|
return Last;
|
|
|
|
|
|
|
|
return Def < 0 ? nullptr : getInstFromId(MBB, Def);
|
|
|
|
}
|