#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/Passes.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetSubtargetInfo.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/InitializePasses.h"
#include "llvm/Transforms/Utils/Debugify.h"
#define DEBUG_TYPE "mir-debugify"
using namespace llvm;
namespace {
bool applyDebugifyMetadataToMachineFunction(MachineModuleInfo &MMI,
DIBuilder &DIB, Function &F) {
MachineFunction *MaybeMF = MMI.getMachineFunction(F);
if (!MaybeMF)
return false;
MachineFunction &MF = *MaybeMF;
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
DISubprogram *SP = F.getSubprogram();
assert(SP && "IR Debugify just created it?");
Module &M = *F.getParent();
LLVMContext &Ctx = M.getContext();
unsigned NextLine = SP->getLine();
for (MachineBasicBlock &MBB : MF) {
for (MachineInstr &MI : MBB) {
MI.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
}
}
Function *DbgValF = M.getFunction("llvm.dbg.value");
DbgValueInst *EarliestDVI = nullptr;
DenseMap<unsigned, DILocalVariable *> Line2Var;
DIExpression *Expr = nullptr;
if (DbgValF) {
for (const Use &U : DbgValF->uses()) {
auto *DVI = dyn_cast<DbgValueInst>(U.getUser());
if (!DVI || DVI->getFunction() != &F)
continue;
unsigned Line = DVI->getDebugLoc().getLine();
assert(Line != 0 && "debugify should not insert line 0 locations");
Line2Var[Line] = DVI->getVariable();
if (!EarliestDVI || Line < EarliestDVI->getDebugLoc().getLine())
EarliestDVI = DVI;
Expr = DVI->getExpression();
}
}
if (Line2Var.empty())
return true;
uint64_t NextImm = 0;
SmallSet<DILocalVariable *, 16> VarSet;
const MCInstrDesc &DbgValDesc = TII.get(TargetOpcode::DBG_VALUE);
for (MachineBasicBlock &MBB : MF) {
MachineBasicBlock::iterator FirstNonPHIIt = MBB.getFirstNonPHI();
for (auto I = MBB.begin(), E = MBB.end(); I != E;) {
MachineInstr &MI = *I;
++I;
if (MI.isDebugInstr())
continue;
if (MI.isTerminator())
continue;
auto InsertBeforeIt = MI.isPHI() ? FirstNonPHIIt : I;
unsigned Line = MI.getDebugLoc().getLine();
if (!Line2Var.count(Line))
Line = EarliestDVI->getDebugLoc().getLine();
DILocalVariable *LocalVar = Line2Var[Line];
assert(LocalVar && "No variable for current line?");
VarSet.insert(LocalVar);
SmallVector<MachineOperand *, 4> RegDefs;
for (MachineOperand &MO : MI.operands())
if (MO.isReg() && MO.isDef() && MO.getReg())
RegDefs.push_back(&MO);
for (MachineOperand *MO : RegDefs)
BuildMI(MBB, InsertBeforeIt, MI.getDebugLoc(), DbgValDesc,
false, *MO, LocalVar, Expr);
if (RegDefs.empty()) {
auto ImmOp = MachineOperand::CreateImm(NextImm++);
BuildMI(MBB, InsertBeforeIt, MI.getDebugLoc(), DbgValDesc,
false, ImmOp, LocalVar, Expr);
}
}
}
NamedMDNode *NMD = M.getNamedMetadata("llvm.mir.debugify");
IntegerType *Int32Ty = Type::getInt32Ty(Ctx);
if (!NMD) {
NMD = M.getOrInsertNamedMetadata("llvm.mir.debugify");
auto addDebugifyOperand = [&](unsigned N) {
NMD->addOperand(MDNode::get(
Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N))));
};
addDebugifyOperand(NextLine - 1);
addDebugifyOperand(VarSet.size());
} else {
assert(NMD->getNumOperands() == 2 &&
"llvm.mir.debugify should have exactly 2 operands!");
auto setDebugifyOperand = [&](unsigned Idx, unsigned N) {
NMD->setOperand(Idx, MDNode::get(Ctx, ValueAsMetadata::getConstant(
ConstantInt::get(Int32Ty, N))));
};
setDebugifyOperand(0, NextLine - 1);
setDebugifyOperand(1, VarSet.size());
}
return true;
}
struct DebugifyMachineModule : public ModulePass {
bool runOnModule(Module &M) override {
MachineModuleInfo &MMI =
getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
return applyDebugifyMetadata(
M, M.functions(),
"ModuleDebugify: ", [&](DIBuilder &DIB, Function &F) -> bool {
return applyDebugifyMetadataToMachineFunction(MMI, DIB, F);
});
}
DebugifyMachineModule() : ModulePass(ID) {}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<MachineModuleInfoWrapperPass>();
AU.addPreserved<MachineModuleInfoWrapperPass>();
AU.setPreservesCFG();
}
static char ID; };
char DebugifyMachineModule::ID = 0;
}
INITIALIZE_PASS_BEGIN(DebugifyMachineModule, DEBUG_TYPE,
"Machine Debugify Module", false, false)
INITIALIZE_PASS_END(DebugifyMachineModule, DEBUG_TYPE,
"Machine Debugify Module", false, false)
ModulePass *llvm::createDebugifyMachineModulePass() {
return new DebugifyMachineModule();
}