@@ -1091,15 +1091,25 @@ bool InstrRefBasedLDV::transferDebugInstrRef(MachineInstr &MI,
1091
1091
if (L)
1092
1092
NewID = ValueIDNum (BlockNo, InstrIt->second .second , *L);
1093
1093
} else if (OpNo != MachineFunction::DebugOperandMemNumber) {
1094
- assert (OpNo < TargetInstr.getNumOperands ());
1095
- const MachineOperand &MO = TargetInstr.getOperand (OpNo);
1096
-
1097
- // Today, this can only be a register.
1098
- assert (MO.isReg () && MO.isDef ());
1094
+ // Permit the debug-info to be completely wrong: identifying a nonexistant
1095
+ // operand, or one that is not a register definition, means something
1096
+ // unexpected happened during optimisation. Broken debug-info, however,
1097
+ // shouldn't crash the compiler -- instead leave the variable value as
1098
+ // None, which will make it appear "optimised out".
1099
+ if (OpNo < TargetInstr.getNumOperands ()) {
1100
+ const MachineOperand &MO = TargetInstr.getOperand (OpNo);
1101
+
1102
+ if (MO.isReg () && MO.isDef () && MO.getReg ()) {
1103
+ unsigned LocID = MTracker->getLocID (MO.getReg ());
1104
+ LocIdx L = MTracker->LocIDToLocIdx [LocID];
1105
+ NewID = ValueIDNum (BlockNo, InstrIt->second .second , L);
1106
+ }
1107
+ }
1099
1108
1100
- unsigned LocID = MTracker->getLocID (MO.getReg ());
1101
- LocIdx L = MTracker->LocIDToLocIdx [LocID];
1102
- NewID = ValueIDNum (BlockNo, InstrIt->second .second , L);
1109
+ if (!NewID) {
1110
+ LLVM_DEBUG (
1111
+ { dbgs () << " Seen instruction reference to illegal operand\n " ; });
1112
+ }
1103
1113
}
1104
1114
// else: NewID is left as None.
1105
1115
} else if (PHIIt != DebugPHINumToValue.end () && PHIIt->InstrNum == InstNo) {
@@ -1249,7 +1259,16 @@ bool InstrRefBasedLDV::transferDebugPHI(MachineInstr &MI) {
1249
1259
const MachineOperand &MO = MI.getOperand (0 );
1250
1260
unsigned InstrNum = MI.getOperand (1 ).getImm ();
1251
1261
1252
- if (MO.isReg ()) {
1262
+ auto EmitBadPHI = [this , &MI, InstrNum](void ) -> bool {
1263
+ // Helper lambda to do any accounting when we fail to find a location for
1264
+ // a DBG_PHI. This can happen if DBG_PHIs are malformed, or refer to a
1265
+ // dead stack slot, for example.
1266
+ // Record a DebugPHIRecord with an empty value + location.
1267
+ DebugPHINumToValue.push_back ({InstrNum, MI.getParent (), None, None});
1268
+ return true ;
1269
+ };
1270
+
1271
+ if (MO.isReg () && MO.getReg ()) {
1253
1272
// The value is whatever's currently in the register. Read and record it,
1254
1273
// to be analysed later.
1255
1274
Register Reg = MO.getReg ();
@@ -1261,15 +1280,14 @@ bool InstrRefBasedLDV::transferDebugPHI(MachineInstr &MI) {
1261
1280
// Ensure this register is tracked.
1262
1281
for (MCRegAliasIterator RAI (MO.getReg (), TRI, true ); RAI.isValid (); ++RAI)
1263
1282
MTracker->lookupOrTrackRegister (*RAI);
1264
- } else {
1283
+ } else if (MO. isFI ()) {
1265
1284
// The value is whatever's in this stack slot.
1266
- assert (MO.isFI ());
1267
1285
unsigned FI = MO.getIndex ();
1268
1286
1269
1287
// If the stack slot is dead, then this was optimized away.
1270
1288
// FIXME: stack slot colouring should account for slots that get merged.
1271
1289
if (MFI->isDeadObjectIndex (FI))
1272
- return true ;
1290
+ return EmitBadPHI () ;
1273
1291
1274
1292
// Identify this spill slot, ensure it's tracked.
1275
1293
Register Base;
@@ -1280,7 +1298,7 @@ bool InstrRefBasedLDV::transferDebugPHI(MachineInstr &MI) {
1280
1298
// We might be able to find a value, but have chosen not to, to avoid
1281
1299
// tracking too much stack information.
1282
1300
if (!SpillNo)
1283
- return true ;
1301
+ return EmitBadPHI () ;
1284
1302
1285
1303
// Problem: what value should we extract from the stack? LLVM does not
1286
1304
// record what size the last store to the slot was, and it would become
@@ -1315,8 +1333,17 @@ bool InstrRefBasedLDV::transferDebugPHI(MachineInstr &MI) {
1315
1333
}
1316
1334
1317
1335
// Record this DBG_PHI for later analysis.
1318
- auto DbgPHI = DebugPHIRecord ({InstrNum, MI.getParent (), *Result, *SpillLoc});
1336
+ auto DbgPHI =
1337
+ DebugPHIRecord ({InstrNum, MI.getParent (), *Result, *SpillLoc});
1319
1338
DebugPHINumToValue.push_back (DbgPHI);
1339
+ } else {
1340
+ // Else: if the operand is neither a legal register or a stack slot, then
1341
+ // we're being fed illegal debug-info. Record an empty PHI, so that any
1342
+ // debug users trying to read this number will be put off trying to
1343
+ // interpret the value.
1344
+ LLVM_DEBUG (
1345
+ { dbgs () << " Seen DBG_PHI with unrecognised operand format\n " ; });
1346
+ return EmitBadPHI ();
1320
1347
}
1321
1348
1322
1349
return true ;
@@ -3165,7 +3192,10 @@ bool InstrRefBasedLDV::ExtendRanges(MachineFunction &MF,
3165
3192
// either live-through machine values, or PHIs.
3166
3193
for (auto &DBG_PHI : DebugPHINumToValue) {
3167
3194
// Identify unresolved block-live-ins.
3168
- ValueIDNum &Num = DBG_PHI.ValueRead ;
3195
+ if (!DBG_PHI.ValueRead )
3196
+ continue ;
3197
+
3198
+ ValueIDNum &Num = *DBG_PHI.ValueRead ;
3169
3199
if (!Num.isPHI ())
3170
3200
continue ;
3171
3201
@@ -3566,17 +3596,24 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
3566
3596
if (LowerIt == UpperIt)
3567
3597
return None;
3568
3598
3599
+ // If any DBG_PHIs referred to a location we didn't understand, don't try to
3600
+ // compute a value. There might be scenarios where we could recover a value
3601
+ // for some range of DBG_INSTR_REFs, but at this point we can have high
3602
+ // confidence that we've seen a bug.
3603
+ auto DBGPHIRange = make_range (LowerIt, UpperIt);
3604
+ for (const DebugPHIRecord &DBG_PHI : DBGPHIRange)
3605
+ if (!DBG_PHI.ValueRead )
3606
+ return None;
3607
+
3569
3608
// If there's only one DBG_PHI, then that is our value number.
3570
3609
if (std::distance (LowerIt, UpperIt) == 1 )
3571
- return LowerIt->ValueRead ;
3572
-
3573
- auto DBGPHIRange = make_range (LowerIt, UpperIt);
3610
+ return *LowerIt->ValueRead ;
3574
3611
3575
3612
// Pick out the location (physreg, slot) where any PHIs must occur. It's
3576
3613
// technically possible for us to merge values in different registers in each
3577
3614
// block, but highly unlikely that LLVM will generate such code after register
3578
3615
// allocation.
3579
- LocIdx Loc = LowerIt->ReadLoc ;
3616
+ LocIdx Loc = * LowerIt->ReadLoc ;
3580
3617
3581
3618
// We have several DBG_PHIs, and a use position (the Here inst). All each
3582
3619
// DBG_PHI does is identify a value at a program position. We can treat each
@@ -3595,7 +3632,7 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
3595
3632
// for the SSAUpdater.
3596
3633
for (const auto &DBG_PHI : DBGPHIRange) {
3597
3634
LDVSSABlock *Block = Updater.getSSALDVBlock (DBG_PHI.MBB );
3598
- const ValueIDNum &Num = DBG_PHI.ValueRead ;
3635
+ const ValueIDNum &Num = * DBG_PHI.ValueRead ;
3599
3636
AvailableValues.insert (std::make_pair (Block, Num.asU64 ()));
3600
3637
}
3601
3638
@@ -3629,7 +3666,7 @@ Optional<ValueIDNum> InstrRefBasedLDV::resolveDbgPHIsImpl(
3629
3666
// Define all the input DBG_PHI values in ValidatedValues.
3630
3667
for (const auto &DBG_PHI : DBGPHIRange) {
3631
3668
LDVSSABlock *Block = Updater.getSSALDVBlock (DBG_PHI.MBB );
3632
- const ValueIDNum &Num = DBG_PHI.ValueRead ;
3669
+ const ValueIDNum &Num = * DBG_PHI.ValueRead ;
3633
3670
ValidatedValues.insert (std::make_pair (Block, Num));
3634
3671
}
3635
3672
0 commit comments