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