Index: include/llvm/CodeGen/MachineInstr.h =================================================================== --- include/llvm/CodeGen/MachineInstr.h +++ include/llvm/CodeGen/MachineInstr.h @@ -65,7 +65,9 @@ FrameSetup = 1 << 0, // Instruction is used as a part of // function frame setup code. BundledPred = 1 << 1, // Instruction has bundled predecessors. - BundledSucc = 1 << 2 // Instruction has bundled successors. + BundledSucc = 1 << 2, // Instruction has bundled successors. + PreserveDbgValLoc = 1 << 3 // Preserve the older location of the + // variable with this DBG_VALUE MI. }; private: const MCInstrDesc *MCID; // Instruction descriptor. @@ -153,10 +155,18 @@ /// Set a MI flag. void setFlag(MIFlag Flag) { + if (Flag == PreserveDbgValLoc) + assert (isDebugValue() && + "PreserveDbgValLoc flag cannot be set for a non-DBG_VALUE instruction!"); + Flags |= (uint8_t)Flag; } void setFlags(unsigned flags) { + if (flags & PreserveDbgValLoc) + assert (isDebugValue() && + "PreserveDbgValLoc flag cannot be set for a non-DBG_VALUE instruction!"); + // Filter out the automatically maintained flags. unsigned Mask = BundledPred | BundledSucc; Flags = (Flags & Mask) | (flags & ~Mask); Index: include/llvm/CodeGen/Passes.h =================================================================== --- include/llvm/CodeGen/Passes.h +++ include/llvm/CodeGen/Passes.h @@ -641,6 +641,9 @@ /// the intrinsic for later emission to the StackMap. extern char &StackMapLivenessID; + /// DebugValueFixup pass + extern char &DebugValueFixupID; + /// createJumpInstrTables - This pass creates jump-instruction tables. ModulePass *createJumpInstrTablesPass(); Index: include/llvm/InitializePasses.h =================================================================== --- include/llvm/InitializePasses.h +++ include/llvm/InitializePasses.h @@ -294,6 +294,7 @@ void initializeMachineFunctionPrinterPassPass(PassRegistry&); void initializeMIRPrintingPassPass(PassRegistry&); void initializeStackMapLivenessPass(PassRegistry&); +void initializeDebugValueFixupPass(PassRegistry&); void initializeMachineCombinerPass(PassRegistry &); void initializeLoadCombinePass(PassRegistry&); void initializeRewriteSymbolsPass(PassRegistry&); Index: include/llvm/Target/TargetInstrInfo.h =================================================================== --- include/llvm/Target/TargetInstrInfo.h +++ include/llvm/Target/TargetInstrInfo.h @@ -938,6 +938,11 @@ return false; } + /// Return true if the specified opcode is a move opcode + virtual bool isMoveOpcode(unsigned Opc) const { + return false; + } + /// Return true if the specified instruction can be predicated. /// By default, this returns true for every instruction with a /// PredicateOperand. Index: lib/CodeGen/AsmPrinter/DbgValueHistoryCalculator.h =================================================================== --- lib/CodeGen/AsmPrinter/DbgValueHistoryCalculator.h +++ lib/CodeGen/AsmPrinter/DbgValueHistoryCalculator.h @@ -38,14 +38,18 @@ typedef MapVector InstrRangesMap; private: + // The VarInstrRange map will have multiple open ranges because of the + // PreserveDbgValLoc flag. This means startInstrRange, endInstrRange and other + // related outines will need to handle multiple open ranges. InstrRangesMap VarInstrRanges; public: void startInstrRange(InlinedVariable Var, const MachineInstr &MI); - void endInstrRange(InlinedVariable Var, const MachineInstr &MI); - // Returns register currently describing @Var. If @Var is currently - // unaccessible or is not described by a register, returns 0. - unsigned getRegisterForVar(InlinedVariable Var) const; + void endInstrRange(InlinedVariable Var, unsigned Reg, const MachineInstr &MI); + // Returns registers currently describing @Var i.e @Var is currently + // unaccessible or is not described by a register will not be in @RegSet. + void getRegistersForVar(InlinedVariable Var, + SmallVectorImpl &RegSet) const; bool empty() const { return VarInstrRanges.empty(); } void clear() { VarInstrRanges.clear(); } Index: lib/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp +++ lib/CodeGen/AsmPrinter/DbgValueHistoryCalculator.cpp @@ -33,40 +33,58 @@ return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0; } +// \brief Start a new instr range from @MI for @Var void DbgValueHistoryMap::startInstrRange(InlinedVariable Var, const MachineInstr &MI) { // Instruction range should start with a DBG_VALUE instruction for the // variable. assert(MI.isDebugValue() && "not a DBG_VALUE"); auto &Ranges = VarInstrRanges[Var]; - if (!Ranges.empty() && Ranges.back().second == nullptr && - Ranges.back().first->isIdenticalTo(&MI)) { - DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n" - << "\t" << Ranges.back().first << "\t" << MI << "\n"); - return; + for (auto &R : Ranges) { + if ((R.second == nullptr) && (R.first->getParent() == MI.getParent()) && + (R.first->isIdenticalTo(&MI)) && + (R.first->getFlag(MachineInstr::PreserveDbgValLoc) == + MI.getFlag(MachineInstr::PreserveDbgValLoc)) ) { + DEBUG(dbgs() << "Coalescing identical DBG_VALUE entries:\n" + << R.first << "\t" << MI << "\n"); + return; + } } Ranges.push_back(std::make_pair(&MI, nullptr)); } -void DbgValueHistoryMap::endInstrRange(InlinedVariable Var, +// \brief End the instr range at @MI for @Var with @Reg +void DbgValueHistoryMap::endInstrRange(InlinedVariable Var, unsigned Reg, const MachineInstr &MI) { auto &Ranges = VarInstrRanges[Var]; - // Verify that the current instruction range is not yet closed. - assert(!Ranges.empty() && Ranges.back().second == nullptr); - // For now, instruction ranges are not allowed to cross basic block - // boundaries. - assert(Ranges.back().first->getParent() == MI.getParent()); - Ranges.back().second = &MI; + bool FoundRange = false; // Found atleast one range to end + // Search through Ranges whose start is Reg and is not yet closed + for (auto &R : Ranges) { + if ((Reg == isDescribedByReg(*R.first)) && (R.second == nullptr) && + (R.first->getParent() == MI.getParent()) ) { + // For now, instruction ranges are not allowed to cross basic block + // boundaries. A new DBG_VALUE instruction in the following basic block + // will start a new range. + R.second = &MI; + FoundRange = true; + } + } + assert(FoundRange && "No valid ranged to end!"); } -unsigned DbgValueHistoryMap::getRegisterForVar(InlinedVariable Var) const { +// \brief Build a set of registers which are in open ranges and return +// in \param RegSet. +void DbgValueHistoryMap::getRegistersForVar(InlinedVariable Var, + SmallVectorImpl &RegSet) const { const auto &I = VarInstrRanges.find(Var); if (I == VarInstrRanges.end()) - return 0; + return; + const auto &Ranges = I->second; - if (Ranges.empty() || Ranges.back().second != nullptr) - return 0; - return isDescribedByReg(*Ranges.back().first); + for (const auto R : Ranges) + if (R.second == nullptr) + if (unsigned Reg = isDescribedByReg(*R.first)) + RegSet.push_back(Reg); } namespace { @@ -75,14 +93,33 @@ typedef std::map> RegDescribedVarsMap; } +// Print @RegDescirbedVarsMap to ostream. +void printRegDescribedVarsMap(raw_ostream &Out, + const RegDescribedVarsMap &RegVars) { + Out << "RegDescribedVarsMap:"; + for (const auto &RV : RegVars) { + Out << "\nReg: " << RV.first << ":\t"; + for (const auto &IV : RV.second) { + if (IV.second != nullptr) + Out << "<" << IV.first->getName() << "," << *IV.second << ">, "; + else + Out << "<" << IV.first->getName() << ", ... " << ">, "; + } + } + Out << "\n\n"; +} + // \brief Claim that @Var is not described by @RegNo anymore. static void dropRegDescribedVar(RegDescribedVarsMap &RegVars, unsigned RegNo, InlinedVariable Var) { const auto &I = RegVars.find(RegNo); - assert(RegNo != 0U && I != RegVars.end()); + assert(RegNo != 0U); + if (I == RegVars.end()) + return; auto &VarSet = I->second; const auto &VarPos = std::find(VarSet.begin(), VarSet.end(), Var); - assert(VarPos != VarSet.end()); + if (VarPos == VarSet.end()) + return; VarSet.erase(VarPos); // Don't keep empty sets in a map to keep it as small as possible. if (VarSet.empty()) @@ -94,7 +131,8 @@ InlinedVariable Var) { assert(RegNo != 0U); auto &VarSet = RegVars[RegNo]; - assert(std::find(VarSet.begin(), VarSet.end(), Var) == VarSet.end()); + if (std::find(VarSet.begin(), VarSet.end(), Var) != VarSet.end()) + return; // Ignore if already found VarSet.push_back(Var); } @@ -107,7 +145,7 @@ // Iterate over all variables described by this register and add this // instruction to their history, clobbering it. for (const auto &Var : I->second) - HistMap.endInstrRange(Var, ClobberingInstr); + HistMap.endInstrRange(Var, I->first, ClobberingInstr); RegVars.erase(I); } @@ -181,21 +219,22 @@ } } +// \brief Routine to print @VarInstrRanges to ostream. void DbgValueHistoryMap::print(raw_ostream &Out) const { - Out << "DbgValueHistoryMap: \n"; - for (const auto VIRI : VarInstrRanges) { - const InlinedVariable &IV = VIRI.first; - const InstrRanges &IR = VIRI.second; + Out << "DbgValueHistoryMap:\n"; + for (const auto VIRi : VarInstrRanges) { + const InlinedVariable &IV = VIRi.first; + const InstrRanges &IR = VIRi.second; if (IV.second != nullptr) Out << "<" << IV.first->getName() << "," << *IV.second << "> = \n"; else Out << "<" << IV.first->getName() << ", ... " << "> = \n"; - for (const auto IRI : IR) { - assert(IRI.first != nullptr); - if (IRI.second != nullptr) - Out << "\tFrom: " << *IRI.first << "\tTo: " << *IRI.second; + for (const auto IRi : IR) { + assert(IRi.first != nullptr); + if (IRi.second != nullptr) + Out << "\tFrom: " << *IRi.first << "\tTo: " << *IRi.second; else - Out << "\tFrom: " << *IRI.first << "\tTo: ... " << "\n"; + Out << "\tFrom: " << *IRi.first << "\tTo: ... " << "\n"; Out << "\n"; } } @@ -210,6 +249,7 @@ RegDescribedVarsMap RegVars; for (const auto &MBB : *MF) { for (const auto &MI : MBB) { + DEBUG(dbgs() << "MBB: " << MBB.getName() << ": "; MI.dump();); if (!MI.isDebugValue()) { // Not a DBG_VALUE instruction. It may clobber registers which describe // some variables. @@ -217,6 +257,9 @@ if (ChangingRegs.test(RegNo)) clobberRegisterUses(RegVars, RegNo, Result, MI); }); + DEBUG(dbgs() << "After clobbering a non-DBG_VALUE instr: "; MI.dump(); + Result.print(dbgs()); + printRegDescribedVarsMap(dbgs(), RegVars)); continue; } @@ -229,13 +272,26 @@ "Expected inlined-at fields to agree"); InlinedVariable Var(RawVar, MI.getDebugLoc()->getInlinedAt()); - if (unsigned PrevReg = Result.getRegisterForVar(Var)) - dropRegDescribedVar(RegVars, PrevReg, Var); + if (!MI.getFlag(MachineInstr::PreserveDbgValLoc)) { + // This DBG_VALUE instr preserves previous locations + SmallVector RegSet; + Result.getRegistersForVar(Var, RegSet); + for (auto R : RegSet) { + dropRegDescribedVar(RegVars, R, Var); + } + + DEBUG(dbgs() << "After dropping RegDescirbedVars: "; MI.dump(); + Result.print(dbgs()); + printRegDescribedVarsMap(dbgs(), RegVars)); + } Result.startInstrRange(Var, MI); if (unsigned NewReg = isDescribedByReg(MI)) addRegDescribedVar(RegVars, NewReg, Var); + DEBUG(dbgs() << "After starting InstrRange: "; MI.dump(); + Result.print(dbgs()); + printRegDescribedVarsMap(dbgs(), RegVars)); } // Make sure locations for register-described variables are valid only @@ -244,11 +300,17 @@ if (!MBB.empty() && &MBB != &MF->back()) { for (auto I = RegVars.begin(), E = RegVars.end(); I != E;) { auto CurElem = I++; // CurElem can be erased below. - if (ChangingRegs.test(CurElem->first)) + if (ChangingRegs.test(CurElem->first)) { clobberRegisterUses(RegVars, CurElem, Result, MBB.back()); + } } } + DEBUG(dbgs() << "End of bb: "; MBB.back().dump(); + Result.print(dbgs()); + printRegDescribedVarsMap(dbgs(), RegVars)); } - DEBUG(Result.print(dbgs())); + DEBUG(dbgs() << "End of calculateDbgValueHistory():\n"; + Result.print(dbgs()); + printRegDescribedVarsMap(dbgs(), RegVars)); } Index: lib/CodeGen/AsmPrinter/DwarfDebug.cpp =================================================================== --- lib/CodeGen/AsmPrinter/DwarfDebug.cpp +++ lib/CodeGen/AsmPrinter/DwarfDebug.cpp @@ -852,10 +852,19 @@ const MCSymbol *EndLabel; if (End != nullptr) EndLabel = getLabelAfterInsn(End); - else if (std::next(I) == Ranges.end()) - EndLabel = Asm->getFunctionEnd(); - else - EndLabel = getLabelBeforeInsn(std::next(I)->first); + else { + // This is an open range - terminate at the next non-preserving DBG_VALUE + // instr or at function end + auto NPi = std::next(I), NPe = Ranges.end(); + for ( ;NPi != NPe; NPi++) { + if (!(NPi->first->getFlag(MachineInstr::PreserveDbgValLoc))) { + EndLabel = getLabelBeforeInsn(NPi->first); + break; + } + } + if (NPi == NPe) + EndLabel = Asm->getFunctionEnd(); + } assert(EndLabel && "Forgot label after instruction ending a range!"); DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n"); Index: lib/CodeGen/CMakeLists.txt =================================================================== --- lib/CodeGen/CMakeLists.txt +++ lib/CodeGen/CMakeLists.txt @@ -13,6 +13,7 @@ CriticalAntiDepBreaker.cpp DFAPacketizer.cpp DeadMachineInstructionElim.cpp + DebugValueFixup.cpp DwarfEHPrepare.cpp EarlyIfConversion.cpp EdgeBundles.cpp Index: lib/CodeGen/CodeGen.cpp =================================================================== --- lib/CodeGen/CodeGen.cpp +++ lib/CodeGen/CodeGen.cpp @@ -66,6 +66,7 @@ initializeSlotIndexesPass(Registry); initializeStackColoringPass(Registry); initializeStackMapLivenessPass(Registry); + initializeDebugValueFixupPass(Registry); initializeStackProtectorPass(Registry); initializeStackSlotColoringPass(Registry); initializeTailDuplicatePassPass(Registry); Index: lib/CodeGen/DebugValueFixup.cpp =================================================================== --- /dev/null +++ lib/CodeGen/DebugValueFixup.cpp @@ -0,0 +1,494 @@ +//===-- DebugValueFixup.cpp - Fixup Debug Value MIs -----------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// Fixup DBG_VALUE MachineInstructions +// a. Fixup debug ranges by inserting new DBG_VALUE instructions at bb join +// points. Current idea is to not build any true ranges here. Only temporary +// ranges are built to calculate DBG_VALUE at join points. True ranges are +// built in DbgValueHistoryCalculator. +// b. Fixup multiple locations problem. +// c. Add missing DBG_VALUE instructions, if possible (TODO). +// d. Any DBG_VALUE instruction related code. +// +// Initially this logic was supposed to be implemented inside +// DbgValueHistoryCalculator code. But there are situations where the +// MachineFunction(MF) is to be modified (like adding a new DBG_VALUE MI while +// handling multiple locations etc) and DbgValueHistoryCalculator has a `const' +// MF. Also it would be neat to do the above fixups in this separate pass and +// allow the existing DbgValueHistoryCalculator logic to take over. +// +//===----------------------------------------------------------------------===// + +#include "llvm/ADT/Statistic.h" +#include "llvm/ADT/SmallVector.h" +#include "llvm/CodeGen/MachineFunction.h" +#include "llvm/CodeGen/MachineFunctionPass.h" +#include "llvm/CodeGen/MachineInstrBuilder.h" +#include "llvm/CodeGen/Passes.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Debug.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/Target/TargetRegisterInfo.h" +#include "llvm/Target/TargetInstrInfo.h" +#include "llvm/Target/TargetSubtargetInfo.h" +#include +#include + +using namespace llvm; + +#define DEBUG_TYPE "dbgval-fixup" + +namespace { + +class DebugValueFixup : public MachineFunctionPass { + +public: + static char ID; + + /// \brief Default construct and initialize the pass. + DebugValueFixup(); + + /// \brief Tell the pass manager which passes we depend on and what + /// information we preserve. + void getAnalysisUsage(AnalysisUsage &AU) const override; + + /// \brief Print to ostream with a message + void print(const char *msg, raw_ostream &Out) const; + + /// \brief Calculate the liveness information for the given machine function. + bool runOnMachineFunction(MachineFunction &MF) override; + +private: + + const TargetRegisterInfo *TRI; + const TargetInstrInfo *TII; + + typedef std::pair + InlinedVariable; + + // Member variables and functions for Range Extension across basic blocks + // Var location in BB + typedef std::pair VarInBB; + // MachineInstr should be a DBG_VALUE instr + typedef std::pair VarLoc; + typedef std::list VarLocList; + + VarLocList OpenRanges; // Ranges that are open until end of bb + VarLocList OutgoingLocs; // Ranges that exist beyond bb + + bool OLChanged; // OutgoingLocs got changed for this bb + bool MBBJoined; // The MBB was joined + + void transfer(MachineInstr &MI, bool HandleMultipleLocations = false); + void join(MachineBasicBlock &MBB); + + void ExtendRanges(MachineFunction &MF); + + // Member variables and functions for Multiple Locations + void HandleMoveForMultipleLocations(MachineInstr &MI); + + void MultipleLocations(MachineFunction &MF); + + bool RemoveRedundantDbgVals(MachineInstr &MI); + void clearDebugValueFixupData(); +}; +} // namespace + + +//===----------------------------------------------------------------------===// +// Implementation +//===----------------------------------------------------------------------===// + +char DebugValueFixup::ID = 0; +char &llvm::DebugValueFixupID = DebugValueFixup::ID; +INITIALIZE_PASS(DebugValueFixup, "dbgval-fixup", + "DBG_VALUE Fixup Pass", false, false) + +/// Default construct and initialize the pass. +DebugValueFixup::DebugValueFixup() : MachineFunctionPass(ID) { + initializeDebugValueFixupPass(*PassRegistry::getPassRegistry()); +} + +/// Tell the pass manager which passes we depend on and what information we +/// preserve. +void DebugValueFixup::getAnalysisUsage(AnalysisUsage &AU) const { + MachineFunctionPass::getAnalysisUsage(AU); +} + +void DebugValueFixup::clearDebugValueFixupData() { + OLChanged = MBBJoined = false; + OpenRanges.clear(); + OutgoingLocs.clear(); +} + +// \brief If @MI is a DBG_VALUE with debug value described by a +// defined register, returns the number of this register. +// In the other case, returns 0. +static unsigned isDescribedByReg(const MachineInstr &MI) { + assert(MI.isDebugValue()); + assert(MI.getNumOperands() == 4); + // If location of variable is described using a register (directly or + // indirecltly), this register is always a first operand. + return MI.getOperand(0).isReg() ? MI.getOperand(0).getReg() : 0; +} + +// \brief Return true if @MI1 and @MI2 have equal offsets +static bool areOffsetsEqual(const MachineInstr &MI1, const MachineInstr &MI2) { + assert(MI1.isDebugValue()); + assert(MI1.getNumOperands() == 4); + + assert(MI2.isDebugValue()); + assert(MI2.getNumOperands() == 4); + + if (!MI1.isIndirectDebugValue() && !MI2.isIndirectDebugValue()) + return true; + + // Check if both MIs are indirect and they are equal + if (MI1.isIndirectDebugValue() && MI2.isIndirectDebugValue()) + return MI1.getOperand(1).getImm() == MI2.getOperand(1).getImm(); + + return false; +} + +// Look for redundant DBG_VALUE instrs starting from @MI and remove the +// redundant ones. The lookup is only till the first non-DBG_VALUE instr. +// Notify caller by returning true if any instr is removed. +bool DebugValueFixup::RemoveRedundantDbgVals(MachineInstr &MI) { + // Populate the consecutive DBG_VALUE instrs + DEBUG(dbgs() << "Remove redundant DBG_VALUEs from: " << MI << "\n"); + std::list DVs; + MachineBasicBlock *MBB = MI.getParent(); + for (auto i = MachineBasicBlock::iterator(MI); + i != MBB->end(); ++i) { + if (!i->isDebugValue()) + break; + DVs.push_back(i); + } + + bool Changed = false; + // Remove the latter instr - instrs inserted recently are at top + for (auto m = DVs.begin(); m != DVs.end(); ++m) { + auto n = m; + for (++n; n != DVs.end(); ++n) { + if ((*m)->isIdenticalTo(*n)) { + // Remove `n' if present in OutgoingLocs + for (VarLocList::iterator i = OutgoingLocs.begin(), + e = OutgoingLocs.end(); i != e; ++i) { + if (*n == i->second ) { // replace with `m' in OutgoingLocs + OutgoingLocs.push_back(std::make_pair(i->first, *m)); + OutgoingLocs.erase(i); + i = OutgoingLocs.begin(), e = OutgoingLocs.end(); + } + } // end OutgoingLoc + DEBUG(dbgs() << "Removing redundant: " << **n << "\n"); + DVs.erase(n); + (*n)->eraseFromParent(); // Remove `n' + Changed = true; + m = n = DVs.begin(); // Reset iterators + } // end if + } // end `n' + } // end `m' + return Changed; +} + +//===----------------------------------------------------------------------===// +// Debug Range Extension Implementation +//===----------------------------------------------------------------------===// + +// Print OpenRanges and OutgoingLocs +void DebugValueFixup::print(const char *msg, raw_ostream &Out) const { + Out << "\nPrint " << msg << "\n"; + Out << "Printing OpenRanges:\n"; + for (const auto &OR : OpenRanges) { + Out << "MBB: " << OR.first.first->getName() + << " Var: " << OR.first.second.first->getName(); + Out << " MI: "; (*OR.second).dump(); Out << "\n"; + } + Out << "Printing OutgoingLocs:\n"; + for (const auto &OL : OutgoingLocs) { + Out << "MBB: " << OL.first.first->getName() + << " Var: " << OL.first.second.first->getName(); + Out << " MI: "; (*OL.second).dump(); Out << "\n"; + } + Out << "\n"; +} + +// This is a common transfer function for extending debug ranges and tracking +// multiple locations for a variable. When HandleMultipleLocations is true, few +// extra tasks are performed for tracking multiple locations. +void DebugValueFixup::transfer(MachineInstr &MI, bool HandleMultipleLocations) { + DEBUG(dbgs() << "transfer MI: "; MI.dump()); + + if (MI.isDebugValue()) { + // Use the base variable (without any DW _OP_piece expressions) + // as index into History. The full variables including the + // piece expressions are attached to the MI. + const DILocalVariable *RawVar = MI.getDebugVariable(); + assert(RawVar->isValidLocationForIntrinsic(MI.getDebugLoc()) && + "Expected inlined-at fields to agree"); + InlinedVariable Var(RawVar, MI.getDebugLoc()->getInlinedAt()); + + // End all previous ranges of Var, if PreserveDbgValLoc is not set + if (!MI.getFlag(MachineInstr::PreserveDbgValLoc)) { + VarLocList::iterator ORi = OpenRanges.begin(), ORe = OpenRanges.end(); + while (ORi != ORe) { + VarLocList::iterator ORiSave = ORi; + ORi++; + if (Var == ORiSave->first.second) { + OpenRanges.erase(ORiSave); + ORi = OpenRanges.begin(), ORe = OpenRanges.end(); + } + } + } + // Add Var to OpenRanges from this DBG_VALUE + // TODO: Currently handles DBG_VALUE which has only reg as location + if (isDescribedByReg(MI)) + OpenRanges.push_back(std::make_pair( + std::make_pair(MI.getParent(), Var), &MI)); + } + + // A definition of a register may mark the end of a range + for (const MachineOperand &MO : MI.operands()) { + // Old register clobbering equivalent code + if (MO.isReg() && MO.isDef() && MO.getReg()) { + // Remove ranges of all aliased registers + for (MCRegAliasIterator RAI(MO.getReg(), TRI, true); + RAI.isValid(); ++RAI) { + VarLocList::iterator ORi = OpenRanges.begin(), ORe = OpenRanges.end(); + while(ORi != ORe) { + VarLocList::iterator ORiSave = ORi; + ORi++; + if (*RAI == isDescribedByReg(*ORiSave->second)) { + OpenRanges.erase(ORiSave); + ORi = OpenRanges.begin(), ORe = OpenRanges.end(); + } // end if + } // end while + } // end reg alias iter + } // end if + } // end for + + // Early return. Multiple locations need not track OutgoingLocs (code below). + if (HandleMultipleLocations) + return; + + // End all ranges at the end of the current basic block + if (MI.isTerminator() || (&MI == &MI.getParent()->instr_back())) { + VarLocList::iterator ORi = OpenRanges.begin(), ORe = OpenRanges.end(); + while (ORi != ORe) { + // Copy OpenRanges to OutgoingLocs, if not already present + if (std::find(OutgoingLocs.begin(), OutgoingLocs.end(), + std::make_pair(ORi->first, ORi->second)) == OutgoingLocs.end()) { + assert(ORi->second->isDebugValue()); + DEBUG(dbgs() << "Add to OutgoingLocs: "; ORi->second->dump();); + OutgoingLocs.push_back(std::make_pair(ORi->first, ORi->second)); + OLChanged = true; + } + + // Delete this open range + OpenRanges.erase(ORi); + ORi = OpenRanges.begin(), ORe = OpenRanges.end(); + } // end while + } // end terminator instr +} + +// This routine inserts a new DBG_VALUE instruction at the start of the MBB +// when the same source variable in all `n' predecessors of MBB reside in the +// same location. +// TODO: Currently handling when n = {1, 2}. Can mostly do better by having +// OutgoingLocs in bitvector +void DebugValueFixup::join(MachineBasicBlock &MBB) { + DEBUG(dbgs() << "join MBB: " << MBB.getName() << "\n"); + + MBBJoined = false; + if (MBB.pred_size() == 0 || MBB.pred_size() > 2) + return; + + if (MBB.pred_size() == 1) { + // Propagate all OutgoingLocs into the current MBB + for (auto &OL : OutgoingLocs) { + if (OL.first.first == *MBB.pred_begin()) { + const MachineInstr *DMI = OL.second; + MachineInstr *MI = BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), + DMI->getDesc(), DMI->isIndirectDebugValue(), + DMI->getOperand(0).getReg(), 0, + DMI->getDebugVariable(), DMI->getDebugExpression()); + if (DMI->isIndirectDebugValue()) + MI->getOperand(1).setImm(DMI->getOperand(1).getImm()); + DEBUG(dbgs() << "Inserted: "; MI->dump();); + MBBJoined = true; + } + } + return; + } + + MachineBasicBlock *pred1 = *MBB.pred_begin(); + MachineBasicBlock *pred2 = *(++MBB.pred_begin()); + + // Search through OutgoingLocs to match 2 entries: + // and TODO: use bitvectors + for (VarLocList::iterator OLi = OutgoingLocs.begin(), + OLe = OutgoingLocs.end(); OLi != OLe; OLi++) { + for (VarLocList::iterator OLii = OutgoingLocs.begin(), + OLie = OutgoingLocs.end(); OLii != OLie; OLii++) { + // Check for equality of the 2 entries + if ((OLi != OLii) && + (OLi->first.first == pred1 && OLii->first.first == pred2) && + (OLi->first.second == OLii->first.second)) { + // Now the two locations should be same - reg and offset + if ( (isDescribedByReg(*OLi->second) == + isDescribedByReg(*OLii->second) ) && + (areOffsetsEqual((*OLi->second), (*OLii->second))) ) { + // Currently a new range is started for the var from the bb's + // beginning by inserting a new DBG_VALUE + // transfer() will end this range however appropriate + const MachineInstr *DMI = OLi->second; + MachineInstr *MI = BuildMI(MBB, MBB.instr_begin(), DMI->getDebugLoc(), + DMI->getDesc(), DMI->isIndirectDebugValue(), + DMI->getOperand(0).getReg(), 0, + DMI->getDebugVariable(), DMI->getDebugExpression()); + if (DMI->isIndirectDebugValue()) + MI->getOperand(1).setImm(DMI->getOperand(1).getImm()); + DEBUG(dbgs() << "Inserted: "; MI->dump(); dbgs() << "\n";); + + MBBJoined = true; // Rerun transfer() function + } // end inner if + } // end if + } // end inner while OLi + } // end while OLii +} + +/// Calculate the liveness information for the given machine function and +/// extend ranges across basic blocks +void DebugValueFixup::ExtendRanges(MachineFunction &MF) { + + DEBUG(dbgs() << "\nDebug Range Extension\n"); + + clearDebugValueFixupData(); + + std::deque BBWorklist; + + // Initialize every mbb with OutgoingLocs + for (auto &MBB : MF) + for (auto &MI : MBB) + transfer(MI); + DEBUG(print("After OutgoingLocs Initialize", dbgs())); + + // Construct a worklist of MBBs + for (auto &MBB : MF) + BBWorklist.push_back(&MBB); + + // Perform join() and trasfer() using the worklist until the ranges converge + // Ranges have converged when the worklist is empty + while (!BBWorklist.empty()) { + MachineBasicBlock *MBB = BBWorklist.front(); + BBWorklist.pop_front(); + + join(*MBB); + + if (MBBJoined) { + RemoveRedundantDbgVals(*MBB->begin()); + for (auto &MI : *MBB) + transfer(MI); + DEBUG(print("After propagating joined MBB", dbgs())); + + if (OLChanged) { + OLChanged = false; + for (MachineBasicBlock::const_succ_iterator si = MBB->succ_begin(), + se = MBB->succ_end(); si != se; ++si) + if (std::find(BBWorklist.begin(), BBWorklist.end(), *si) + == BBWorklist.end()) // add if not already present + BBWorklist.push_back(*si); + } + } + } + DEBUG(print("Final OutgoingLocs", dbgs())); +} + + +//===----------------------------------------------------------------------===// +// Multiple Locations Implementation +//===----------------------------------------------------------------------===// + +// Insert a new DBG_VALUE instruction with PreserveDbgValLoc set when a copy +// instruction is seen. @MI is the move instruction. +void DebugValueFixup::HandleMoveForMultipleLocations(MachineInstr &MI) { + // Identify the source variable related to source operand and insert a + // DBG_VALUE instruction indicating the source variable to be in dst reg. + + if (MI.getNumOperands() != 2) { + DEBUG(dbgs() << "Expected 2 operands for `mov' instr.\n"); + return; + } + if (!(MI.getOperand(0).isReg() && MI.getOperand(1).isReg())) { + DEBUG(dbgs() << "Currently handles only register move instructions.\n"); + return; + } + + // TODO: Ignore creating a new DBG_VALUE instr if the dst reg is a isKill()? + + // If the source register is mapped to a Var then insert a new DBG_VALUE instr + // with preserve flag and new location as destination register. The source reg + // mapping is obtained from OpenRanges and there could be multiple Vars mapped + // to the source register - in which case, multiple DBG_VALUE instrs with + // preserve loc flag are inserted for each Var. + for (auto &OR : OpenRanges) { + if (MI.getOperand(1).getReg() == isDescribedByReg(*OR.second)) { + const MachineInstr *DMI = OR.second; + MachineBasicBlock::iterator MBBI(MI); + MachineInstr *DV = BuildMI(*MI.getParent(), ++MBBI, DMI->getDebugLoc(), + DMI->getDesc(), DMI->isIndirectDebugValue(), + MI.getOperand(0).getReg(), 0, + DMI->getDebugVariable(), DMI->getDebugExpression()); + if (DMI->isIndirectDebugValue()) + DV->getOperand(1).setImm(DMI->getOperand(1).getImm()); + DV->setFlag(MachineInstr::PreserveDbgValLoc); + DEBUG(dbgs() << "Inserted: "; DV->dump();); + + RemoveRedundantDbgVals(*DV); + } + } +} + +// Insert a DBG_VALUE instruction with PreserveDbgValLoc set, if the variable +// is found to be present in multiple locations. +// TODO: Currently, we insert only when we see copies +void DebugValueFixup::MultipleLocations(MachineFunction &MF) { + + DEBUG(dbgs() << "\nMultiple Locations\n"); + + clearDebugValueFixupData(); + + for (auto &MBB : MF) { + OpenRanges.clear(); + MachineBasicBlock::iterator MBBi = MBB.instr_begin(),MBBe = MBB.instr_end(); + while (MBBi != MBBe) { + MachineInstr &MI = *MBBi; + transfer(MI, true); // transfer MI through the bb + if (TII->isMoveOpcode(MI.getOpcode())) { + DEBUG(print("Before handling move in Multiple Locations", dbgs())); + HandleMoveForMultipleLocations(MI); + // Reset iterators + MBBi = MachineBasicBlock::iterator(MI), MBBe = MBB.instr_end(); + } + MBBi++; + } + DEBUG(print("Multiple Locations after an mbb", dbgs())); + } +} + +bool DebugValueFixup::runOnMachineFunction(MachineFunction &MF) { + TRI = MF.getSubtarget().getRegisterInfo(); + TII = MF.getSubtarget().getInstrInfo(); + + ExtendRanges(MF); + + MultipleLocations(MF); + + return true; +} Index: lib/CodeGen/MachineInstr.cpp =================================================================== --- lib/CodeGen/MachineInstr.cpp +++ lib/CodeGen/MachineInstr.cpp @@ -1706,13 +1706,15 @@ } bool HaveSemi = false; - const unsigned PrintableFlags = FrameSetup; + const unsigned PrintableFlags = FrameSetup | PreserveDbgValLoc; if (Flags & PrintableFlags) { if (!HaveSemi) OS << ";"; HaveSemi = true; OS << " flags: "; if (Flags & FrameSetup) OS << "FrameSetup"; + if (Flags & PreserveDbgValLoc) + OS << "PreserveDbgValLoc"; } if (!memoperands_empty()) { Index: lib/CodeGen/Passes.cpp =================================================================== --- lib/CodeGen/Passes.cpp +++ lib/CodeGen/Passes.cpp @@ -575,6 +575,7 @@ addPreEmitPass(); addPass(&StackMapLivenessID, false); + addPass(&DebugValueFixupID); AddingMachinePasses = false; } Index: lib/Target/X86/X86InstrInfo.h =================================================================== --- lib/Target/X86/X86InstrInfo.h +++ lib/Target/X86/X86InstrInfo.h @@ -390,6 +390,8 @@ bool ReverseBranchCondition(SmallVectorImpl &Cond) const override; + bool isMoveOpcode(unsigned Opc) const override; + /// isSafeToMoveRegClassDefs - Return true if it's safe to move a machine /// instruction that defines the specified register class. bool isSafeToMoveRegClassDefs(const TargetRegisterClass *RC) const override; Index: lib/Target/X86/X86InstrInfo.cpp =================================================================== --- lib/Target/X86/X86InstrInfo.cpp +++ lib/Target/X86/X86InstrInfo.cpp @@ -2331,6 +2331,32 @@ return true; } +// Return true if the specified opcode Opc is a move opcode +bool X86InstrInfo::isMoveOpcode(unsigned Opc) const { + switch (Opc) { + default: return false; + case X86::MOV16ri: case X86::MOV16mi: + case X86::MOV16rr: case X86::MOV16mr: + case X86::MOV32ri: case X86::MOV32mi: + case X86::MOV32rr: case X86::MOV32mr: + case X86::MOV64ri32: case X86::MOV64mi32: + case X86::MOV64rr: case X86::MOV64mr: + case X86::MOV8ri: case X86::MOV8mi: + case X86::MOV8rr: case X86::MOV8mr: + case X86::MOV8rr_NOREX: case X86::MOV8mr_NOREX: + case X86::MOVAPDrr: case X86::MOVAPDmr: + case X86::MOVAPSrr: case X86::MOVAPSmr: + case X86::MOVDQArr: case X86::MOVDQAmr: + case X86::MOVPDI2DIrr: case X86::MOVPDI2DImr: + case X86::MOVPQIto64rr: case X86::MOVPQI2QImr: + case X86::MOVSDto64rr: case X86::MOVSDto64mr: + case X86::MOVSS2DIrr: case X86::MOVSS2DImr: + case X86::MOVUPDrr: case X86::MOVUPDmr: + case X86::MOVUPSrr: case X86::MOVUPSmr: + return true; + } +} + bool X86InstrInfo::isSafeToClobberEFLAGS(MachineBasicBlock &MBB, MachineBasicBlock::iterator I) const { MachineBasicBlock::iterator E = MBB.end(); Index: test/DebugInfo/X86/array.ll =================================================================== --- test/DebugInfo/X86/array.ll +++ test/DebugInfo/X86/array.ll @@ -17,6 +17,8 @@ ; rdar://problem/14874886 ; ; CHECK: ##DEBUG_VALUE: main:array <- [R{{.*}}+0] +; CHECK: ##DEBUG_VALUE: main:array <- [R{{.*}}+0] +; CHECK: ##DEBUG_VALUE: main:array <- [R{{.*}}+0] ; CHECK-NOT: ##DEBUG_VALUE: main:array <- R{{.*}} target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.9.0" Index: test/DebugInfo/X86/fission-ranges.ll =================================================================== --- test/DebugInfo/X86/fission-ranges.ll +++ test/DebugInfo/X86/fission-ranges.ll @@ -29,16 +29,16 @@ ; CHECK-NEXT: Location description: 11 00 ; CHECK-NEXT: {{^$}} ; CHECK-NEXT: Beginning address index: 3 -; CHECK-NEXT: Length: 21 +; CHECK-NEXT: Length: 25 ; CHECK-NEXT: Location description: 50 93 04 ; CHECK: [[E]]: Beginning address index: 4 -; CHECK-NEXT: Length: 19 +; CHECK-NEXT: Length: 23 ; CHECK-NEXT: Location description: 50 93 04 ; CHECK: [[B]]: Beginning address index: 5 -; CHECK-NEXT: Length: 17 +; CHECK-NEXT: Length: 21 ; CHECK-NEXT: Location description: 50 93 04 ; CHECK: [[D]]: Beginning address index: 6 -; CHECK-NEXT: Length: 17 +; CHECK-NEXT: Length: 21 ; CHECK-NEXT: Location description: 50 93 04 ; Make sure we don't produce any relocations in any .dwo section (though in particular, debug_info.dwo) Index: test/DebugInfo/X86/sret.ll =================================================================== --- test/DebugInfo/X86/sret.ll +++ test/DebugInfo/X86/sret.ll @@ -3,8 +3,8 @@ ; Based on the debuginfo-tests/sret.cpp code. -; CHECK: DW_AT_GNU_dwo_id [DW_FORM_data8] (0x51ac5644b1937aa1) -; CHECK: DW_AT_GNU_dwo_id [DW_FORM_data8] (0x51ac5644b1937aa1) +; CHECK: DW_AT_GNU_dwo_id [DW_FORM_data8] ([[ADDR:0x[0-9a-z]*]]) +; CHECK: DW_AT_GNU_dwo_id [DW_FORM_data8] ([[ADDR]]) %class.A = type { i32 (...)**, i32 } %class.B = type { i8 } Index: test/DebugInfo/extend-debug-range.ll =================================================================== --- /dev/null +++ test/DebugInfo/extend-debug-range.ll @@ -0,0 +1,147 @@ +; RUN: llc -print-machineinstrs=dbgval-fixup %s 2>&1 | FileCheck %s + +; Test the extension of debug ranges from predecessors. +; Generated from the source file ExtendDebugRange.c: +; #include +; int m; +; extern int inc(int n); +; extern int change(int n); +; extern int modify(int n); +; int main(int argc, char **argv) { +; int n; +; if (argc != 2) +; n = 2; +; else +; n = atoi(argv[1]); +; n = change(n); +; if (n > 10) { +; m = modify(n); +; m = m + n; // var `m' doesn't has a dbg.value +; } +; else +; m = inc(n); // var `m' doesn't has a dbg.value +; printf("m(main): %d\n", m); +; return 0; +; } +; with clang -g -emit-llvm ExtendDebugRange.c -c -o edr.bc +; then opt -O3 -S -o extend-debug-range.ll edr.bc +; This case will also produce multiple locations but only the debug range +; extension is tested here. + +; DBG_VALUE for variable "n" is extended into BB#5 from its predecessors BB#3 +; and BB#4. +; +; CHECK: Predecessors according to CFG: BB#4 BB#3 +; CHECK-NEXT: DBG_VALUE %RSI, %noreg, !"argv", +; CHECK-NEXT: DBG_VALUE %EBX, %noreg, !"n", + + +; ModuleID = 'edr.bc' +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@m = common global i32 0, align 4 +@.str = private unnamed_addr constant [13 x i8] c"m(main): %d\0A\00", align 1 + +; Function Attrs: nounwind uwtable +define i32 @main(i32 %argc, i8** nocapture readonly %argv) #0 { +entry: + tail call void @llvm.dbg.value(metadata i32 %argc, i64 0, metadata !16, metadata !17), !dbg !18 + tail call void @llvm.dbg.value(metadata i8** %argv, i64 0, metadata !19, metadata !17), !dbg !20 + %cmp = icmp eq i32 %argc, 2, !dbg !21 + br i1 %cmp, label %if.else, label %if.end, !dbg !23 + +if.else: ; preds = %entry + %arrayidx = getelementptr inbounds i8*, i8** %argv, i64 1, !dbg !24 + %0 = load i8*, i8** %arrayidx, align 8, !dbg !24 + %call = tail call i32 (i8*, ...) bitcast (i32 (...)* @atoi to i32 (i8*, ...)*)(i8* %0) #4, !dbg !25 + tail call void @llvm.dbg.value(metadata i32 %call, i64 0, metadata !26, metadata !17), !dbg !27 + br label %if.end + +if.end: ; preds = %entry, %if.else + %n.0 = phi i32 [ %call, %if.else ], [ 2, %entry ] + %call1 = tail call i32 @change(i32 %n.0) #4, !dbg !28 + tail call void @llvm.dbg.value(metadata i32 %call1, i64 0, metadata !26, metadata !17), !dbg !27 + %cmp2 = icmp sgt i32 %call1, 10, !dbg !29 + br i1 %cmp2, label %if.then.3, label %if.else.5, !dbg !31 + +if.then.3: ; preds = %if.end + %call4 = tail call i32 @modify(i32 %call1) #4, !dbg !32 + %add = add nsw i32 %call4, %call1, !dbg !34 + br label %if.end.7, !dbg !35 + +if.else.5: ; preds = %if.end + %call6 = tail call i32 @inc(i32 %call1) #4, !dbg !36 + br label %if.end.7 + +if.end.7: ; preds = %if.else.5, %if.then.3 + %storemerge = phi i32 [ %call6, %if.else.5 ], [ %add, %if.then.3 ] + store i32 %storemerge, i32* @m, align 4, !dbg !37 + %call8 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %storemerge) #4, !dbg !38 + ret i32 0, !dbg !39 +} + +declare i32 @atoi(...) #1 + +declare i32 @change(i32) #1 + +declare i32 @modify(i32) #1 + +declare i32 @inc(i32) #1 + +; Function Attrs: nounwind +declare i32 @printf(i8* nocapture readonly, ...) #2 + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.value(metadata, i64, metadata, metadata) #3 + +attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #2 = { nounwind "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #3 = { nounwind readnone } +attributes #4 = { nounwind } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!13, !14} +!llvm.ident = !{!15} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.8.0 (trunk 243841)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3, globals: !11) +!1 = !DIFile(filename: "ExtendDebugRanges.c", directory: "/home/vt/vikram/newTests") +!2 = !{} +!3 = !{!4} +!4 = !DISubprogram(name: "main", scope: !1, file: !1, line: 6, type: !5, isLocal: false, isDefinition: true, scopeLine: 6, flags: DIFlagPrototyped, isOptimized: false, function: i32 (i32, i8**)* @main, variables: !2) +!5 = !DISubroutineType(types: !6) +!6 = !{!7, !7, !8} +!7 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!8 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !9, size: 64, align: 64) +!9 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !10, size: 64, align: 64) +!10 = !DIBasicType(name: "char", size: 8, align: 8, encoding: DW_ATE_signed_char) +!11 = !{!12} +!12 = !DIGlobalVariable(name: "m", scope: !0, file: !1, line: 2, type: !7, isLocal: false, isDefinition: true, variable: i32* @m) +!13 = !{i32 2, !"Dwarf Version", i32 4} +!14 = !{i32 2, !"Debug Info Version", i32 3} +!15 = !{!"clang version 3.8.0 (trunk 243841)"} +!16 = !DILocalVariable(name: "argc", arg: 1, scope: !4, file: !1, line: 6, type: !7) +!17 = !DIExpression() +!18 = !DILocation(line: 6, column: 14, scope: !4) +!19 = !DILocalVariable(name: "argv", arg: 2, scope: !4, file: !1, line: 6, type: !8) +!20 = !DILocation(line: 6, column: 27, scope: !4) +!21 = !DILocation(line: 8, column: 12, scope: !22) +!22 = distinct !DILexicalBlock(scope: !4, file: !1, line: 8, column: 7) +!23 = !DILocation(line: 8, column: 7, scope: !4) +!24 = !DILocation(line: 11, column: 14, scope: !22) +!25 = !DILocation(line: 11, column: 9, scope: !22) +!26 = !DILocalVariable(name: "n", scope: !4, file: !1, line: 7, type: !7) +!27 = !DILocation(line: 7, column: 7, scope: !4) +!28 = !DILocation(line: 12, column: 7, scope: !4) +!29 = !DILocation(line: 13, column: 9, scope: !30) +!30 = distinct !DILexicalBlock(scope: !4, file: !1, line: 13, column: 7) +!31 = !DILocation(line: 13, column: 7, scope: !4) +!32 = !DILocation(line: 14, column: 9, scope: !33) +!33 = distinct !DILexicalBlock(scope: !30, file: !1, line: 13, column: 15) +!34 = !DILocation(line: 15, column: 11, scope: !33) +!35 = !DILocation(line: 16, column: 3, scope: !33) +!36 = !DILocation(line: 18, column: 9, scope: !30) +!37 = !DILocation(line: 15, column: 7, scope: !33) +!38 = !DILocation(line: 19, column: 3, scope: !4) +!39 = !DILocation(line: 20, column: 3, scope: !4) Index: test/DebugInfo/multiple-locations-1.ll =================================================================== --- /dev/null +++ test/DebugInfo/multiple-locations-1.ll @@ -0,0 +1,71 @@ +; RUN: llc -print-machineinstrs=dbgval-fixup %s 2>&1 | FileCheck %s + +; Test the presence of a variable at multiple locations +; Generated from the source file inc.c: +; #include +; int inc(int n) { +; n++; +; printf("n(inc)2: %d\n", n); +; return n; +; } +; with clang -g -emit-llvm inc.c -c -o ml.bc +; then opt -O3 -S -o multiple-locations-1.ll ml.bc + +; Multiple located variable is inferred using move instruction and represented +; with a PreserveDbgValLoc flag for the DBG_VALUE instruction. +; +; CHECK: DBG_VALUE %EDI, %noreg, !"n", +; CHECK-NEXT: %EBX = MOV32rr %EDI +; CHECK-NEXT: DBG_VALUE %EBX, %noreg, !"n", +; CHECK-SAME: flags: PreserveDbgValLoc +; CHECK-NOT: DBG_VALUE %EBX, %noreg, !"n", + + +; ModuleID = 'ml.bc' +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@.str = private unnamed_addr constant [13 x i8] c"n(inc)2: %d\0A\00", align 1 + +; Function Attrs: nounwind uwtable +define i32 @inc(i32 %n) #0 { +entry: + tail call void @llvm.dbg.value(metadata i32 %n, i64 0, metadata !11, metadata !12), !dbg !13 + %inc = add nsw i32 %n, 1, !dbg !14 + tail call void @llvm.dbg.value(metadata i32 %inc, i64 0, metadata !11, metadata !12), !dbg !13 + %call = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %inc) #3, !dbg !15 + ret i32 %inc, !dbg !16 +} + +; Function Attrs: nounwind +declare i32 @printf(i8* nocapture readonly, ...) #1 + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.value(metadata, i64, metadata, metadata) #2 + +attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #2 = { nounwind readnone } +attributes #3 = { nounwind } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!8, !9} +!llvm.ident = !{!10} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.8.0 (trunk 243841)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3) +!1 = !DIFile(filename: "inc.c", directory: "/home/vt/vikram/newTests") +!2 = !{} +!3 = !{!4} +!4 = !DISubprogram(name: "inc", scope: !1, file: !1, line: 2, type: !5, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: false, function: i32 (i32)* @inc, variables: !2) +!5 = !DISubroutineType(types: !6) +!6 = !{!7, !7} +!7 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!8 = !{i32 2, !"Dwarf Version", i32 4} +!9 = !{i32 2, !"Debug Info Version", i32 3} +!10 = !{!"clang version 3.8.0 (trunk 243841)"} +!11 = !DILocalVariable(name: "n", arg: 1, scope: !4, file: !1, line: 2, type: !7) +!12 = !DIExpression() +!13 = !DILocation(line: 2, column: 13, scope: !4) +!14 = !DILocation(line: 3, column: 4, scope: !4) +!15 = !DILocation(line: 4, column: 3, scope: !4) +!16 = !DILocation(line: 5, column: 3, scope: !4) Index: test/DebugInfo/multiple-locations-2.ll =================================================================== --- /dev/null +++ test/DebugInfo/multiple-locations-2.ll @@ -0,0 +1,76 @@ +; RUN: llc -filetype=obj < %s | llvm-dwarfdump -debug-dump=loc - | FileCheck %s + +; Check the presence of a variable at multiple locations +; This is the same example as multiple-locations-1.ll but the .debug_loc section +; is tested here. +; Generated from the source file inc.c: +; #include +; int inc(int n) { +; n++; +; printf("n(inc)2: %d\n", n); +; return n; +; } +; with clang -g -emit-llvm inc.c -c -o ml.bc +; then opt -O3 -S -o multiple-locations-1.ll ml.bc + +; Test the .debug_loc section from dwarfdump. There will be two ranges for "n". +; +; Range for "n" in 55 from 0x0000000000000000 to 0x000000000000000f +; CHECK: 0x00000000: Beginning address offset: 0x0000000000000000 +; CHECK-NEXT: Ending address offset: 0x000000000000000f +; CHECK-NEXT: Location description: 55 93 04 +; +; Range for "n" in 53 from 0x0000000000000008 to 0x000000000000000a +; CHECK: Beginning address offset: 0x0000000000000008 +; CHECK-NEXT: Ending address offset: 0x000000000000000a +; CHECK-NEXT: Location description: 53 93 04 + + +; ModuleID = 'ml.bc' +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" +target triple = "x86_64-unknown-linux-gnu" + +@.str = private unnamed_addr constant [13 x i8] c"n(inc)2: %d\0A\00", align 1 + +; Function Attrs: nounwind uwtable +define i32 @inc(i32 %n) #0 { +entry: + tail call void @llvm.dbg.value(metadata i32 %n, i64 0, metadata !11, metadata !12), !dbg !13 + %inc = add nsw i32 %n, 1, !dbg !14 + tail call void @llvm.dbg.value(metadata i32 %inc, i64 0, metadata !11, metadata !12), !dbg !13 + %call = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %inc) #3, !dbg !15 + ret i32 %inc, !dbg !16 +} + +; Function Attrs: nounwind +declare i32 @printf(i8* nocapture readonly, ...) #1 + +; Function Attrs: nounwind readnone +declare void @llvm.dbg.value(metadata, i64, metadata, metadata) #2 + +attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #1 = { nounwind "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" } +attributes #2 = { nounwind readnone } +attributes #3 = { nounwind } + +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!8, !9} +!llvm.ident = !{!10} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.8.0 (trunk 243841)", isOptimized: false, runtimeVersion: 0, emissionKind: 1, enums: !2, subprograms: !3) +!1 = !DIFile(filename: "inc.c", directory: "/home/vt/vikram/newTests") +!2 = !{} +!3 = !{!4} +!4 = !DISubprogram(name: "inc", scope: !1, file: !1, line: 2, type: !5, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: false, function: i32 (i32)* @inc, variables: !2) +!5 = !DISubroutineType(types: !6) +!6 = !{!7, !7} +!7 = !DIBasicType(name: "int", size: 32, align: 32, encoding: DW_ATE_signed) +!8 = !{i32 2, !"Dwarf Version", i32 4} +!9 = !{i32 2, !"Debug Info Version", i32 3} +!10 = !{!"clang version 3.8.0 (trunk 243841)"} +!11 = !DILocalVariable(name: "n", arg: 1, scope: !4, file: !1, line: 2, type: !7) +!12 = !DIExpression() +!13 = !DILocation(line: 2, column: 13, scope: !4) +!14 = !DILocation(line: 3, column: 4, scope: !4) +!15 = !DILocation(line: 4, column: 3, scope: !4) +!16 = !DILocation(line: 5, column: 3, scope: !4)