#include "llvm/CodeGen/DbgEntityHistoryCalculator.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/CodeGen/LexicalScopes.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
#include <map>
#include <utility>
using namespace llvm;
#define DEBUG_TYPE "dwarfdebug"
namespace {
using EntryIndex = DbgValueHistoryMap::EntryIndex;
}
void InstructionOrdering::initialize(const MachineFunction &MF) {
clear();
unsigned Position = 0;
for (const MachineBasicBlock &MBB : MF)
for (const MachineInstr &MI : MBB)
InstNumberMap[&MI] = MI.isMetaInstruction() ? Position : ++Position;
}
bool InstructionOrdering::isBefore(const MachineInstr *A,
const MachineInstr *B) const {
assert(A->getParent() && B->getParent() && "Operands must have a parent");
assert(A->getMF() == B->getMF() &&
"Operands must be in the same MachineFunction");
return InstNumberMap.lookup(A) < InstNumberMap.lookup(B);
}
bool DbgValueHistoryMap::startDbgValue(InlinedEntity Var,
const MachineInstr &MI,
EntryIndex &NewIndex) {
assert(MI.isDebugValue() && "not a DBG_VALUE");
auto &Entries = VarEntries[Var];
if (!Entries.empty() && Entries.back().isDbgValue() &&
!Entries.back().isClosed() &&
Entries.back().getInstr()->isIdenticalTo(MI)) {
LLVM_DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n"
<< "\t" << Entries.back().getInstr() << "\t" << MI
<< "\n");
return false;
}
Entries.emplace_back(&MI, Entry::DbgValue);
NewIndex = Entries.size() - 1;
return true;
}
EntryIndex DbgValueHistoryMap::startClobber(InlinedEntity Var,
const MachineInstr &MI) {
auto &Entries = VarEntries[Var];
if (Entries.back().isClobber() && Entries.back().getInstr() == &MI)
return Entries.size() - 1;
Entries.emplace_back(&MI, Entry::Clobber);
return Entries.size() - 1;
}
void DbgValueHistoryMap::Entry::endEntry(EntryIndex Index) {
assert(isDbgValue() && "Setting end index for non-debug value");
assert(!isClosed() && "End index has already been set");
EndIndex = Index;
}
static Optional<ArrayRef<InsnRange>::iterator>
intersects(const MachineInstr *StartMI, const MachineInstr *EndMI,
const ArrayRef<InsnRange> &Ranges,
const InstructionOrdering &Ordering) {
for (auto RangesI = Ranges.begin(), RangesE = Ranges.end();
RangesI != RangesE; ++RangesI) {
if (EndMI && Ordering.isBefore(EndMI, RangesI->first))
return None;
if (EndMI && !Ordering.isBefore(RangesI->second, EndMI))
return RangesI;
if (Ordering.isBefore(StartMI, RangesI->second))
return RangesI;
}
return None;
}
void DbgValueHistoryMap::trimLocationRanges(
const MachineFunction &MF, LexicalScopes &LScopes,
const InstructionOrdering &Ordering) {
SmallVector<EntryIndex, 4> ToRemove;
SmallVector<int, 4> ReferenceCount;
SmallVector<size_t, 4> Offsets;
for (auto &Record : VarEntries) {
auto &HistoryMapEntries = Record.second;
if (HistoryMapEntries.empty())
continue;
InlinedEntity Entity = Record.first;
const DILocalVariable *LocalVar = cast<DILocalVariable>(Entity.first);
LexicalScope *Scope = nullptr;
if (const DILocation *InlinedAt = Entity.second) {
Scope = LScopes.findInlinedScope(LocalVar->getScope(), InlinedAt);
} else {
Scope = LScopes.findLexicalScope(LocalVar->getScope());
if (Scope &&
(Scope->getScopeNode() == Scope->getScopeNode()->getSubprogram()) &&
(Scope->getScopeNode() == LocalVar->getScope()))
continue;
}
if (!Scope)
continue;
ToRemove.clear();
ReferenceCount.assign(HistoryMapEntries.size(), 0);
EntryIndex StartIndex = 0;
ArrayRef<InsnRange> ScopeRanges(Scope->getRanges());
for (auto EI = HistoryMapEntries.begin(), EE = HistoryMapEntries.end();
EI != EE; ++EI, ++StartIndex) {
if (!EI->isDbgValue())
continue;
EntryIndex EndIndex = EI->getEndIndex();
if (EndIndex != NoEntry)
ReferenceCount[EndIndex] += 1;
if (ReferenceCount[StartIndex] > 0)
continue;
const MachineInstr *StartMI = EI->getInstr();
const MachineInstr *EndMI = EndIndex != NoEntry
? HistoryMapEntries[EndIndex].getInstr()
: nullptr;
if (auto R = intersects(StartMI, EndMI, ScopeRanges, Ordering)) {
ScopeRanges = ArrayRef<InsnRange>(*R, ScopeRanges.end());
} else {
ToRemove.push_back(StartIndex);
if (EndIndex != NoEntry)
ReferenceCount[EndIndex] -= 1;
}
}
if (ToRemove.empty())
continue;
for (size_t i = 0; i < HistoryMapEntries.size(); ++i)
if (ReferenceCount[i] <= 0 && HistoryMapEntries[i].isClobber())
ToRemove.push_back(i);
llvm::sort(ToRemove);
Offsets.assign(HistoryMapEntries.size(), 0);
size_t CurOffset = 0;
auto ToRemoveItr = ToRemove.begin();
for (size_t EntryIdx = *ToRemoveItr; EntryIdx < HistoryMapEntries.size();
++EntryIdx) {
if (ToRemoveItr != ToRemove.end() && *ToRemoveItr == EntryIdx) {
++ToRemoveItr;
++CurOffset;
}
Offsets[EntryIdx] = CurOffset;
}
for (auto &Entry : HistoryMapEntries)
if (Entry.isClosed())
Entry.EndIndex -= Offsets[Entry.EndIndex];
for (EntryIndex Idx : llvm::reverse(ToRemove))
HistoryMapEntries.erase(HistoryMapEntries.begin() + Idx);
}
}
bool DbgValueHistoryMap::hasNonEmptyLocation(const Entries &Entries) const {
for (const auto &Entry : Entries) {
if (!Entry.isDbgValue())
continue;
const MachineInstr *MI = Entry.getInstr();
assert(MI->isDebugValue());
if (MI->getOperand(0).isReg() && MI->getOperand(0).getReg() == 0)
continue;
return true;
}
return false;
}
void DbgLabelInstrMap::addInstr(InlinedEntity Label, const MachineInstr &MI) {
assert(MI.isDebugLabel() && "not a DBG_LABEL");
LabelInstr[Label] = &MI;
}
namespace {
using InlinedEntity = DbgValueHistoryMap::InlinedEntity;
using RegDescribedVarsMap = std::map<unsigned, SmallVector<InlinedEntity, 1>>;
using DbgValueEntriesMap = std::map<InlinedEntity, SmallSet<EntryIndex, 1>>;
}
static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
InlinedEntity Var) {
const auto &I = RegVars.find(RegNo);
assert(RegNo != 0U && I != RegVars.end());
auto &VarSet = I->second;
const auto &VarPos = llvm::find(VarSet, Var);
assert(VarPos != VarSet.end());
VarSet.erase(VarPos);
if (VarSet.empty())
RegVars.erase(I);
}
static void addRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo,
InlinedEntity Var) {
assert(RegNo != 0U);
auto &VarSet = RegVars[RegNo];
assert(!is_contained(VarSet, Var));
VarSet.push_back(Var);
}
static void clobberRegEntries(InlinedEntity Var, unsigned RegNo,
const MachineInstr &ClobberingInstr,
DbgValueEntriesMap &LiveEntries,
DbgValueHistoryMap &HistMap,
SmallVectorImpl<Register> &FellowRegisters) {
EntryIndex ClobberIndex = HistMap.startClobber(Var, ClobberingInstr);
SmallVector<EntryIndex, 4> IndicesToErase;
SmallSet<Register, 4> MaybeRemovedRegisters;
SmallSet<Register, 4> KeepRegisters;
for (auto Index : LiveEntries[Var]) {
auto &Entry = HistMap.getEntry(Var, Index);
assert(Entry.isDbgValue() && "Not a DBG_VALUE in LiveEntries");
if (Entry.getInstr()->isDebugEntryValue())
continue;
if (Entry.getInstr()->hasDebugOperandForReg(RegNo)) {
IndicesToErase.push_back(Index);
Entry.endEntry(ClobberIndex);
for (const auto &MO : Entry.getInstr()->debug_operands())
if (MO.isReg() && MO.getReg() && MO.getReg() != RegNo)
MaybeRemovedRegisters.insert(MO.getReg());
} else {
for (const auto &MO : Entry.getInstr()->debug_operands())
if (MO.isReg() && MO.getReg())
KeepRegisters.insert(MO.getReg());
}
}
for (Register Reg : MaybeRemovedRegisters)
if (!KeepRegisters.contains(Reg))
FellowRegisters.push_back(Reg);
for (auto Index : IndicesToErase)
LiveEntries[Var].erase(Index);
}
static void handleNewDebugValue(InlinedEntity Var, const MachineInstr &DV,
RegDescribedVarsMap &RegVars,
DbgValueEntriesMap &LiveEntries,
DbgValueHistoryMap &HistMap) {
EntryIndex NewIndex;
if (HistMap.startDbgValue(Var, DV, NewIndex)) {
SmallDenseMap<unsigned, bool, 4> TrackedRegs;
SmallVector<EntryIndex, 4> IndicesToErase;
const DIExpression *DIExpr = DV.getDebugExpression();
for (auto Index : LiveEntries[Var]) {
auto &Entry = HistMap.getEntry(Var, Index);
assert(Entry.isDbgValue() && "Not a DBG_VALUE in LiveEntries");
const MachineInstr &DV = *Entry.getInstr();
bool Overlaps = DIExpr->fragmentsOverlap(DV.getDebugExpression());
if (Overlaps) {
IndicesToErase.push_back(Index);
Entry.endEntry(NewIndex);
}
if (!DV.isDebugEntryValue())
for (const MachineOperand &Op : DV.debug_operands())
if (Op.isReg() && Op.getReg())
TrackedRegs[Op.getReg()] |= !Overlaps;
}
if (!DV.isDebugEntryValue()) {
for (const MachineOperand &Op : DV.debug_operands()) {
if (Op.isReg() && Op.getReg()) {
Register NewReg = Op.getReg();
if (!TrackedRegs.count(NewReg))
addRegDescribedVar(RegVars, NewReg, Var);
LiveEntries[Var].insert(NewIndex);
TrackedRegs[NewReg] = true;
}
}
}
for (auto I : TrackedRegs)
if (!I.second)
dropRegDescribedVar(RegVars, I.first, Var);
for (auto Index : IndicesToErase)
LiveEntries[Var].erase(Index);
LiveEntries[Var].insert(NewIndex);
}
}
static void clobberRegisterUses(RegDescribedVarsMap &RegVars,
RegDescribedVarsMap::iterator I,
DbgValueHistoryMap &HistMap,
DbgValueEntriesMap &LiveEntries,
const MachineInstr &ClobberingInstr) {
for (const auto &Var : I->second) {
SmallVector<Register, 4> FellowRegisters;
clobberRegEntries(Var, I->first, ClobberingInstr, LiveEntries, HistMap,
FellowRegisters);
for (Register RegNo : FellowRegisters)
dropRegDescribedVar(RegVars, RegNo, Var);
}
RegVars.erase(I);
}
static void clobberRegisterUses(RegDescribedVarsMap &RegVars, unsigned RegNo,
DbgValueHistoryMap &HistMap,
DbgValueEntriesMap &LiveEntries,
const MachineInstr &ClobberingInstr) {
const auto &I = RegVars.find(RegNo);
if (I == RegVars.end())
return;
clobberRegisterUses(RegVars, I, HistMap, LiveEntries, ClobberingInstr);
}
void llvm::calculateDbgEntityHistory(const MachineFunction *MF,
const TargetRegisterInfo *TRI,
DbgValueHistoryMap &DbgValues,
DbgLabelInstrMap &DbgLabels) {
const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
Register SP = TLI->getStackPointerRegisterToSaveRestore();
Register FrameReg = TRI->getFrameRegister(*MF);
RegDescribedVarsMap RegVars;
DbgValueEntriesMap LiveEntries;
for (const auto &MBB : *MF) {
for (const auto &MI : MBB) {
if (MI.isDebugValue()) {
assert(MI.getNumOperands() > 1 && "Invalid DBG_VALUE instruction!");
const DILocalVariable *RawVar = MI.getDebugVariable();
assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
"Expected inlined-at fields to agree");
InlinedEntity Var(RawVar, MI.getDebugLoc()->getInlinedAt());
handleNewDebugValue(Var, MI, RegVars, LiveEntries, DbgValues);
} else if (MI.isDebugLabel()) {
assert(MI.getNumOperands() == 1 && "Invalid DBG_LABEL instruction!");
const DILabel *RawLabel = MI.getDebugLabel();
assert(RawLabel->isValidLocationForIntrinsic(MI.getDebugLoc()) &&
"Expected inlined-at fields to agree");
InlinedEntity L(RawLabel, MI.getDebugLoc()->getInlinedAt());
DbgLabels.addInstr(L, MI);
}
if (MI.isMetaInstruction())
continue;
for (const MachineOperand &MO : MI.operands()) {
if (MO.isReg() && MO.isDef() && MO.getReg()) {
if (MI.isCall() && MO.getReg() == SP)
continue;
if (Register::isVirtualRegister(MO.getReg()))
clobberRegisterUses(RegVars, MO.getReg(), DbgValues, LiveEntries,
MI);
else if (MO.getReg() != FrameReg ||
(!MI.getFlag(MachineInstr::FrameDestroy) &&
!MI.getFlag(MachineInstr::FrameSetup))) {
for (MCRegAliasIterator AI(MO.getReg(), TRI, true); AI.isValid();
++AI)
clobberRegisterUses(RegVars, *AI, DbgValues, LiveEntries, MI);
}
} else if (MO.isRegMask()) {
SmallVector<unsigned, 32> RegsToClobber;
for (auto It : RegVars) {
unsigned int Reg = It.first;
if (Reg != SP && Register::isPhysicalRegister(Reg) &&
MO.clobbersPhysReg(Reg))
RegsToClobber.push_back(Reg);
}
for (unsigned Reg : RegsToClobber) {
clobberRegisterUses(RegVars, Reg, DbgValues, LiveEntries, MI);
}
}
} }
if (!MBB.empty() && &MBB != &MF->back()) {
for (auto &Pair : LiveEntries) {
if (Pair.second.empty())
continue;
EntryIndex ClobIdx = DbgValues.startClobber(Pair.first, MBB.back());
for (EntryIndex Idx : Pair.second) {
DbgValueHistoryMap::Entry &Ent = DbgValues.getEntry(Pair.first, Idx);
assert(Ent.isDbgValue() && !Ent.isClosed());
Ent.endEntry(ClobIdx);
}
}
LiveEntries.clear();
RegVars.clear();
}
}
}
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD void DbgValueHistoryMap::dump() const {
dbgs() << "DbgValueHistoryMap:\n";
for (const auto &VarRangePair : *this) {
const InlinedEntity &Var = VarRangePair.first;
const Entries &Entries = VarRangePair.second;
const DILocalVariable *LocalVar = cast<DILocalVariable>(Var.first);
const DILocation *Location = Var.second;
dbgs() << " - " << LocalVar->getName() << " at ";
if (Location)
dbgs() << Location->getFilename() << ":" << Location->getLine() << ":"
<< Location->getColumn();
else
dbgs() << "<unknown location>";
dbgs() << " --\n";
for (const auto &E : enumerate(Entries)) {
const auto &Entry = E.value();
dbgs() << " Entry[" << E.index() << "]: ";
if (Entry.isDbgValue())
dbgs() << "Debug value\n";
else
dbgs() << "Clobber\n";
dbgs() << " Instr: " << *Entry.getInstr();
if (Entry.isDbgValue()) {
if (Entry.getEndIndex() == NoEntry)
dbgs() << " - Valid until end of function\n";
else
dbgs() << " - Closed by Entry[" << Entry.getEndIndex() << "]\n";
}
dbgs() << "\n";
}
}
}
#endif