45
45
#include " llvm/IR/BasicBlock.h"
46
46
#include " llvm/IR/Constant.h"
47
47
#include " llvm/IR/DataLayout.h"
48
+ #include " llvm/IR/DebugInfoMetadata.h"
48
49
#include " llvm/IR/DerivedTypes.h"
49
50
#include " llvm/IR/EHPersonalities.h"
50
51
#include " llvm/IR/Function.h"
@@ -1052,8 +1053,8 @@ void MachineFunction::substituteDebugValuesForInst(const MachineInstr &Old,
1052
1053
}
1053
1054
1054
1055
auto MachineFunction::salvageCopySSA (
1055
- MachineInstr &MI, DenseMap<Register, DebugInstrOperandPair > &DbgPHICache)
1056
- -> DebugInstrOperandPair {
1056
+ MachineInstr &MI, DenseMap<Register, SalvageCopySSAResult > &DbgPHICache)
1057
+ -> SalvageCopySSAResult {
1057
1058
const TargetInstrInfo &TII = *getSubtarget ().getInstrInfo ();
1058
1059
1059
1060
// Check whether this copy-like instruction has already been salvaged into
@@ -1077,7 +1078,7 @@ auto MachineFunction::salvageCopySSA(
1077
1078
}
1078
1079
1079
1080
auto MachineFunction::salvageCopySSAImpl (MachineInstr &MI)
1080
- -> DebugInstrOperandPair {
1081
+ -> SalvageCopySSAResult {
1081
1082
MachineRegisterInfo &MRI = getRegInfo ();
1082
1083
const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo ();
1083
1084
const TargetInstrInfo &TII = *getSubtarget ().getInstrInfo ();
@@ -1175,7 +1176,8 @@ auto MachineFunction::salvageCopySSAImpl(MachineInstr &MI)
1175
1176
for (auto &MO : Inst->all_defs ()) {
1176
1177
if (MO.getReg () != State.first )
1177
1178
continue ;
1178
- return ApplySubregisters ({Inst->getDebugInstrNum (), MO.getOperandNo ()});
1179
+ return {ApplySubregisters ({Inst->getDebugInstrNum (), MO.getOperandNo ()}),
1180
+ Inst};
1179
1181
}
1180
1182
1181
1183
llvm_unreachable (" Vreg def with no corresponding operand?" );
@@ -1195,8 +1197,9 @@ auto MachineFunction::salvageCopySSAImpl(MachineInstr &MI)
1195
1197
if (!TRI.regsOverlap (RegToSeek, MO.getReg ()))
1196
1198
continue ;
1197
1199
1198
- return ApplySubregisters (
1199
- {ToExamine.getDebugInstrNum (), MO.getOperandNo ()});
1200
+ return {
1201
+ ApplySubregisters ({ToExamine.getDebugInstrNum (), MO.getOperandNo ()}),
1202
+ &ToExamine};
1200
1203
}
1201
1204
}
1202
1205
@@ -1217,7 +1220,131 @@ auto MachineFunction::salvageCopySSAImpl(MachineInstr &MI)
1217
1220
Builder.addReg (State.first );
1218
1221
unsigned NewNum = getNewDebugInstrNum ();
1219
1222
Builder.addImm (NewNum);
1220
- return ApplySubregisters ({NewNum, 0u });
1223
+ return {ApplySubregisters ({NewNum, 0u }), nullptr };
1224
+ }
1225
+
1226
+ // / The Op operand to the DBG_INSTR_REF instruction DbgInstr is a virtual
1227
+ // / register defined by the REG_SEQUENCE instruction RegSeq. In order to
1228
+ // / finalize DbgInstr to use instruction references, find the defining
1229
+ // / instruction for each register in the sequence and compose them with a
1230
+ // / DIOpComposite.
1231
+ static bool finalizeInstrRefRegSequenceNew (
1232
+ MachineInstr &DbgInstr, MachineOperand &Op, MachineInstr &RegSeq,
1233
+ DenseMap<Register, MachineFunction::SalvageCopySSAResult> &DbgPHICache) {
1234
+
1235
+ const DIExpression *Expr = DbgInstr.getDebugExpression ();
1236
+ if (Expr->holdsOldElements ())
1237
+ return false ;
1238
+
1239
+ auto &MF = *DbgInstr.getParent ()->getParent ();
1240
+ auto &Ctx = Expr->getContext ();
1241
+ auto &TRI = *MF.getSubtarget ().getRegisterInfo ();
1242
+ auto &TII = *MF.getSubtarget ().getInstrInfo ();
1243
+ auto &DL = MF.getDataLayout ();
1244
+
1245
+ struct Part {
1246
+ MachineFunction::DebugInstrOperandPair DbgInstrNum;
1247
+ unsigned Size;
1248
+ unsigned Offset;
1249
+ };
1250
+ SmallVector<Part> Parts;
1251
+
1252
+ // Walk through the reg sequence, collecting debug-instr-numbers and
1253
+ // subregister piece sizes and offsets into Parts.
1254
+ for (unsigned I = 1 ; I < RegSeq.getNumOperands (); I += 2 ) {
1255
+ Register RegOp = RegSeq.getOperand (I).getReg ();
1256
+ if (!RegOp.isVirtual ())
1257
+ return false ;
1258
+
1259
+ unsigned SubReg = RegSeq.getOperand (I + 1 ).getImm ();
1260
+ unsigned SubSize = TRI.getSubRegIdxSize (SubReg);
1261
+ unsigned SubOffset = TRI.getSubRegIdxOffset (SubReg);
1262
+ MachineInstr &DefMI = *MF.getRegInfo ().def_instr_begin (RegOp);
1263
+
1264
+ if (DefMI.isCopyLike () || TII.isCopyInstr (DefMI)) {
1265
+ auto P = MF.salvageCopySSA (DefMI, DbgPHICache);
1266
+ Parts.push_back ({P.first , SubSize, SubOffset});
1267
+ continue ;
1268
+ }
1269
+
1270
+ // Otherwise, identify the operand number that the VReg refers to.
1271
+ unsigned OperandIdx = 0 ;
1272
+ for (const auto &DefMO : DefMI.operands ()) {
1273
+ if (DefMO.isReg () && DefMO.isDef () && DefMO.getReg () == RegOp)
1274
+ break ;
1275
+ ++OperandIdx;
1276
+ }
1277
+ assert (OperandIdx < DefMI.getNumOperands ());
1278
+
1279
+ // Morph this instr ref to point at the given instruction and operand.
1280
+ unsigned ID = DefMI.getDebugInstrNum ();
1281
+ MachineFunction::DebugInstrOperandPair P{ID, OperandIdx};
1282
+ Parts.push_back ({P, SubSize, SubOffset});
1283
+ }
1284
+
1285
+ // Line up the Parts and make sure there aren't any gaps, DIOpComposite can't
1286
+ // handle that easily.
1287
+ std::sort (Parts.begin (), Parts.end (),
1288
+ [](auto &LHS, auto &RHS) { return LHS.Offset < RHS.Offset ; });
1289
+ for (unsigned I = 1 , E = Parts.size (); I < E; ++I)
1290
+ if (Parts[I - 1 ].Offset + Parts[I - 1 ].Size != Parts[I].Offset )
1291
+ return false ;
1292
+ if (Parts.empty () || Parts[0 ].Offset )
1293
+ return false ;
1294
+
1295
+ unsigned ArgNoToReplace = 0 ;
1296
+ unsigned NumArgs = DbgInstr.getNumDebugOperands ();
1297
+ assert (NumArgs == Expr->getNewNumLocationOperands ());
1298
+ for (; ArgNoToReplace != NumArgs; ++ArgNoToReplace)
1299
+ if (&DbgInstr.getDebugOperand (ArgNoToReplace) == &Op)
1300
+ break ;
1301
+ if (ArgNoToReplace == NumArgs)
1302
+ return false ;
1303
+
1304
+ auto Elems = Expr->getNewElementsRef ();
1305
+ auto NewSize = TypeSize::getFixed (Parts.back ().Offset + Parts.back ().Size );
1306
+ for (DIOp::Variant Elem : *Elems) {
1307
+ // Only replace the argument with a composite if it has the same size as the
1308
+ // parts.
1309
+ if (auto *Arg = std::get_if<DIOp::Arg>(&Elem))
1310
+ if (Arg->getIndex () == ArgNoToReplace &&
1311
+ DL.getTypeSizeInBits (Arg->getResultType ()) != NewSize)
1312
+ return false ;
1313
+ }
1314
+
1315
+ Op.ChangeToDbgInstrRef (Parts[0 ].DbgInstrNum .first ,
1316
+ Parts[0 ].DbgInstrNum .second );
1317
+ if (Parts.size () == 1 )
1318
+ return true ;
1319
+
1320
+ // Split up the DIOpArg using a DIOpComposite.
1321
+ DIExprBuilder B{Ctx};
1322
+ for (DIOp::Variant Elem : *Elems) {
1323
+ auto *Arg = std::get_if<DIOp::Arg>(&Elem);
1324
+ if (!Arg || Arg->getIndex () != ArgNoToReplace) {
1325
+ B.append (Elem);
1326
+ continue ;
1327
+ }
1328
+ bool FirstPart = true ;
1329
+ for (const Part &P : Parts) {
1330
+ // Since these arguments have to line up with the order of the operands on
1331
+ // the DBG_INSTR_REF, recycle Arg's index first, it lines up with the Op
1332
+ // that was ChangeToDbgInstrRef'd above.
1333
+ unsigned ArgNo = FirstPart ? Arg->getIndex () : NumArgs++;
1334
+ FirstPart = false ;
1335
+ B.append <DIOp::Arg>(ArgNo, IntegerType::get (Ctx, P.Size ));
1336
+ }
1337
+ B.append <DIOp::Composite>(Parts.size (), Arg->getResultType ());
1338
+ }
1339
+
1340
+ auto *NewExpr = B.intoExpression ();
1341
+ for (const Part &P : drop_begin (Parts, 1 ))
1342
+ DbgInstr.addOperand (MachineOperand::CreateDbgInstrRef (
1343
+ P.DbgInstrNum .first , P.DbgInstrNum .second ));
1344
+ DbgInstr.getDebugExpressionOp () = MachineOperand::CreateMetadata (NewExpr);
1345
+ assert (NewExpr->getNewNumLocationOperands () ==
1346
+ DbgInstr.getNumDebugOperands ());
1347
+ return true ;
1221
1348
}
1222
1349
1223
1350
void MachineFunction::finalizeDebugInstrRefs () {
@@ -1229,15 +1356,16 @@ void MachineFunction::finalizeDebugInstrRefs() {
1229
1356
MI.setDebugValueUndef ();
1230
1357
};
1231
1358
1232
- DenseMap<Register, DebugInstrOperandPair > ArgDbgPHIs;
1359
+ DenseMap<Register, SalvageCopySSAResult > ArgDbgPHIs;
1233
1360
for (auto &MBB : *this ) {
1234
1361
for (auto &MI : MBB) {
1235
1362
if (!MI.isDebugRef ())
1236
1363
continue ;
1237
1364
1238
1365
bool IsValidRef = true ;
1239
1366
1240
- for (MachineOperand &MO : MI.debug_operands ()) {
1367
+ for (unsigned I = 0 ; I < MI.getNumDebugOperands (); ++I) {
1368
+ MachineOperand &MO = MI.getDebugOperand (I);
1241
1369
if (!MO.isReg ())
1242
1370
continue ;
1243
1371
@@ -1259,7 +1387,12 @@ void MachineFunction::finalizeDebugInstrRefs() {
1259
1387
// for why this is important.
1260
1388
if (DefMI.isCopyLike () || TII->isCopyInstr (DefMI)) {
1261
1389
auto Result = salvageCopySSA (DefMI, ArgDbgPHIs);
1262
- MO.ChangeToDbgInstrRef (Result.first , Result.second );
1390
+ if (!Result.second || !Result.second ->isRegSequence () ||
1391
+ !finalizeInstrRefRegSequenceNew (MI, MO, *Result.second ,
1392
+ ArgDbgPHIs))
1393
+ MO.ChangeToDbgInstrRef (Result.first .first , Result.first .second );
1394
+ } else if (DefMI.isRegSequence () &&
1395
+ finalizeInstrRefRegSequenceNew (MI, MO, DefMI, ArgDbgPHIs)) {
1263
1396
} else {
1264
1397
// Otherwise, identify the operand number that the VReg refers to.
1265
1398
unsigned OperandIdx = 0 ;
0 commit comments