@@ -2062,6 +2062,16 @@ void DwarfDebug::beginInstruction(const MachineInstr *MI) {
20622062 unsigned LastAsmLine =
20632063 Asm->OutStreamer ->getContext ().getCurrentDwarfLoc ().getLine ();
20642064
2065+ if (!DL && MI == PrologEndLoc) {
2066+ // In rare situations, we might want to place the end of the prologue
2067+ // somewhere that doesn't have a source location already. It should be in
2068+ // the entry block.
2069+ assert (MI->getParent () == &*MI->getMF ()->begin ());
2070+ recordSourceLine (SP->getScopeLine (), 0 , SP,
2071+ DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT);
2072+ return ;
2073+ }
2074+
20652075 bool PrevInstInSameSection =
20662076 (!PrevInstBB ||
20672077 PrevInstBB->getSectionID () == MI->getParent ()->getSectionID ());
@@ -2138,32 +2148,109 @@ static std::pair<const MachineInstr *, bool>
21382148findPrologueEndLoc (const MachineFunction *MF) {
21392149 // First known non-DBG_VALUE and non-frame setup location marks
21402150 // the beginning of the function body.
2141- const MachineInstr *LineZeroLoc = nullptr ;
2151+ const auto &TII = *MF->getSubtarget ().getInstrInfo ();
2152+ const MachineInstr *NonTrivialInst = nullptr ;
21422153 const Function &F = MF->getFunction ();
21432154
21442155 // Some instructions may be inserted into prologue after this function. Must
21452156 // keep prologue for these cases.
21462157 bool IsEmptyPrologue =
21472158 !(F.hasPrologueData () || F.getMetadata (LLVMContext::MD_func_sanitize));
2148- for (const auto &MBB : *MF) {
2149- for (const auto &MI : MBB) {
2150- if (!MI.isMetaInstruction ()) {
2151- if (!MI.getFlag (MachineInstr::FrameSetup) && MI.getDebugLoc ()) {
2152- // Scan forward to try to find a non-zero line number. The
2153- // prologue_end marks the first breakpoint in the function after the
2154- // frame setup, and a compiler-generated line 0 location is not a
2155- // meaningful breakpoint. If none is found, return the first
2156- // location after the frame setup.
2157- if (MI.getDebugLoc ().getLine ())
2158- return std::make_pair (&MI, IsEmptyPrologue);
2159-
2160- LineZeroLoc = &MI;
2161- }
2162- IsEmptyPrologue = false ;
2163- }
2159+
2160+ // Helper lambda to examine each instruction and potentially return it
2161+ // as the prologue_end point.
2162+ auto ExamineInst = [&](const MachineInstr &MI)
2163+ -> std::optional<std::pair<const MachineInstr *, bool >> {
2164+ // Is this instruction trivial data shuffling or frame-setup?
2165+ bool isCopy = (TII.isCopyInstr (MI) ? true : false );
2166+ bool isTrivRemat = TII.isTriviallyReMaterializable (MI);
2167+ bool isFrameSetup = MI.getFlag (MachineInstr::FrameSetup);
2168+
2169+ if (!isFrameSetup && MI.getDebugLoc ()) {
2170+ // Scan forward to try to find a non-zero line number. The
2171+ // prologue_end marks the first breakpoint in the function after the
2172+ // frame setup, and a compiler-generated line 0 location is not a
2173+ // meaningful breakpoint. If none is found, return the first
2174+ // location after the frame setup.
2175+ if (MI.getDebugLoc ().getLine ())
2176+ return std::make_pair (&MI, IsEmptyPrologue);
2177+ }
2178+
2179+ // Keep track of the first "non-trivial" instruction seen, i.e. anything
2180+ // that doesn't involve shuffling data around or is a frame-setup.
2181+ if (!isCopy && !isTrivRemat && !isFrameSetup && !NonTrivialInst)
2182+ NonTrivialInst = &MI;
2183+
2184+ IsEmptyPrologue = false ;
2185+ return std::nullopt ;
2186+ };
2187+
2188+ // Examine all the instructions at the start of the function. This doesn't
2189+ // necessarily mean just the entry block: unoptimised code can fall-through
2190+ // into an initial loop, and it makes sense to put the initial breakpoint on
2191+ // the first instruction of such a loop. However, if we pass branches, we're
2192+ // better off synthesising an early prologue_end.
2193+ auto CurBlock = MF->begin ();
2194+ auto CurInst = CurBlock->begin ();
2195+ while (true ) {
2196+ // Skip empty blocks, in rare cases the entry can be empty.
2197+ if (CurInst == CurBlock->end ()) {
2198+ ++CurBlock;
2199+ CurInst = CurBlock->begin ();
2200+ continue ;
21642201 }
2202+
2203+ // Check whether this non-meta instruction a good position for prologue_end.
2204+ if (!CurInst->isMetaInstruction ()) {
2205+ auto FoundInst = ExamineInst (*CurInst);
2206+ if (FoundInst)
2207+ return *FoundInst;
2208+ }
2209+
2210+ // Try to continue searching, but use a backup-location if substantive
2211+ // computation is happening.
2212+ auto NextInst = std::next (CurInst);
2213+ if (NextInst != CurInst->getParent ()->end ()) {
2214+ // Continue examining the current block.
2215+ CurInst = NextInst;
2216+ continue ;
2217+ }
2218+
2219+ // We've reached the end of the block. Did we just look at a terminator?
2220+ if (CurInst->isTerminator ()) {
2221+ // Some kind of "real" control flow is occurring. At the very least
2222+ // we would have to start exploring the CFG, a good signal that the
2223+ // prologue is over.
2224+ break ;
2225+ }
2226+
2227+ // If we've already fallen through into a loop, don't fall through
2228+ // further, use a backup-location.
2229+ if (CurBlock->pred_size () > 1 )
2230+ break ;
2231+
2232+ // Fall-through from entry to the next block. This is common at -O0 when
2233+ // there's no initialisation in the function. Bail if we're also at the
2234+ // end of the function.
2235+ if (++CurBlock == MF->end ())
2236+ break ;
2237+ CurInst = CurBlock->begin ();
2238+ }
2239+
2240+ // We couldn't find any source-location, suggesting all meaningful information
2241+ // got optimised away. Set the prologue_end to be the first non-trivial
2242+ // instruction, which will get the scope line number. This is better than
2243+ // nothing.
2244+ // Only do this in the entry block, as we'll be giving it the scope line for
2245+ // the function. Return IsEmptyPrologue==true if we've picked the first
2246+ // instruction.
2247+ if (NonTrivialInst && NonTrivialInst->getParent () == &*MF->begin ()) {
2248+ IsEmptyPrologue = NonTrivialInst == &*MF->begin ()->begin ();
2249+ return std::make_pair (NonTrivialInst, IsEmptyPrologue);
21652250 }
2166- return std::make_pair (LineZeroLoc, IsEmptyPrologue);
2251+
2252+ // If the entry path is empty, just don't have a prologue_end at all.
2253+ return std::make_pair (nullptr , IsEmptyPrologue);
21672254}
21682255
21692256// / Register a source line with debug info. Returns the unique label that was
@@ -2195,12 +2282,21 @@ DwarfDebug::emitInitialLocDirective(const MachineFunction &MF, unsigned CUID) {
21952282 bool IsEmptyPrologue = PrologEnd.second ;
21962283
21972284 // If the prolog is empty, no need to generate scope line for the proc.
2198- if (IsEmptyPrologue)
2199- // In degenerate cases, we can have functions with no source locations
2200- // at all. These want a scope line, to avoid a totally empty function.
2201- // Thus, only skip scope line if there's location to place prologue_end.
2202- if (PrologEndLoc)
2203- return PrologEndLoc;
2285+ if (IsEmptyPrologue) {
2286+ // If there's nowhere to put a prologue_end flag, emit a scope line in case
2287+ // there are simply no source locations anywhere in the function.
2288+ if (PrologEndLoc) {
2289+ // Avoid trying to assign prologue_end to a line-zero location.
2290+ // Instructions with no DebugLoc at all are fine, they'll be given the
2291+ // scope line nuumber.
2292+ const DebugLoc &DL = PrologEndLoc->getDebugLoc ();
2293+ if (!DL || DL->getLine () != 0 )
2294+ return PrologEndLoc;
2295+
2296+ // Later, don't place the prologue_end flag on this line-zero location.
2297+ PrologEndLoc = nullptr ;
2298+ }
2299+ }
22042300
22052301 // Ensure the compile unit is created if the function is called before
22062302 // beginFunction().
0 commit comments