[DebugInfo][RemoveDIs] Use autoupgrader to convert old debug-info (#1… · llvm/llvm-project@3d7aa96 (original) (raw)

`@@ -1155,8 +1155,7 @@ static bool upgradeIntrinsicFunction1(Function *F, Function *&NewFn,

`

1155

1155

`case 'd':

`

1156

1156

`if (Name.consume_front("dbg.")) {

`

1157

1157

`// Mark debug intrinsics for upgrade to new debug format.

`

1158

``

`-

if (CanUpgradeDebugIntrinsicsToRecords &&

`

1159

``

`-

F->getParent()->IsNewDbgInfoFormat) {

`

``

1158

`+

if (CanUpgradeDebugIntrinsicsToRecords) {

`

1160

1159

`if (Name == "addr" || Name == "value" || Name == "assign" ||

`

1161

1160

` Name == "declare" || Name == "label") {

`

1162

1161

`// There's no function to replace these with.

`

`@@ -4395,39 +4394,66 @@ static Value *upgradeAMDGCNIntrinsicCall(StringRef Name, CallBase *CI,

`

4395

4394

`return Builder.CreateBitCast(RMW, RetTy);

`

4396

4395

`}

`

4397

4396

``

4398

``

`-

/// Helper to unwrap intrinsic call MetadataAsValue operands.

`

4399

``

`-

template

`

4400

``

`-

static MDType *unwrapMAVOp(CallBase *CI, unsigned Op) {

`

4401

``

`-

if (MetadataAsValue *MAV = dyn_cast(CI->getArgOperand(Op)))

`

4402

``

`-

return dyn_cast(MAV->getMetadata());

`

``

4397

`+

/// Helper to unwrap intrinsic call MetadataAsValue operands. Return as a

`

``

4398

`+

/// plain MDNode, as it's the verifier's job to check these are the correct

`

``

4399

`+

/// types later.

`

``

4400

`+

static MDNode *unwrapMAVOp(CallBase *CI, unsigned Op) {

`

``

4401

`+

if (Op < CI->arg_size()) {

`

``

4402

`+

if (MetadataAsValue *MAV =

`

``

4403

`+

dyn_cast(CI->getArgOperand(Op))) {

`

``

4404

`+

Metadata *MD = MAV->getMetadata();

`

``

4405

`+

return dyn_cast_if_present(MD);

`

``

4406

`+

}

`

``

4407

`+

}

`

``

4408

`+

return nullptr;

`

``

4409

`+

}

`

``

4410

+

``

4411

`+

/// Helper to unwrap Metadata MetadataAsValue operands, such as the Value field.

`

``

4412

`+

static Metadata *unwrapMAVMetadataOp(CallBase *CI, unsigned Op) {

`

``

4413

`+

if (Op < CI->arg_size())

`

``

4414

`+

if (MetadataAsValue *MAV = dyn_cast(CI->getArgOperand(Op)))

`

``

4415

`+

return MAV->getMetadata();

`

4403

4416

`return nullptr;

`

4404

4417

`}

`

4405

4418

``

``

4419

`+

static MDNode *getDebugLocSafe(const Instruction *I) {

`

``

4420

`+

// The MDNode attached to this instruction might not be the correct type,

`

``

4421

`+

// as the verifier has not yet be run. Fetch it as a bare MDNode.

`

``

4422

`+

return I->getDebugLoc().getAsMDNode();

`

``

4423

`+

}

`

``

4424

+

4406

4425

`/// Convert debug intrinsic calls to non-instruction debug records.

`

4407

4426

`/// \p Name - Final part of the intrinsic name, e.g. 'value' in llvm.dbg.value.

`

4408

4427

`/// \p CI - The debug intrinsic call.

`

4409

4428

`static void upgradeDbgIntrinsicToDbgRecord(StringRef Name, CallBase *CI) {

`

4410

4429

` DbgRecord *DR = nullptr;

`

4411

4430

`if (Name == "label") {

`

4412

``

`-

DR = new DbgLabelRecord(unwrapMAVOp(CI, 0), CI->getDebugLoc());

`

``

4431

`+

DR = DbgLabelRecord::createUnresolvedDbgLabelRecord(unwrapMAVOp(CI, 0),

`

``

4432

`+

CI->getDebugLoc());

`

4413

4433

` } else if (Name == "assign") {

`

4414

``

`-

DR = new DbgVariableRecord(

`

4415

``

`-

unwrapMAVOp(CI, 0), unwrapMAVOp(CI, 1),

`

4416

``

`-

unwrapMAVOp(CI, 2), unwrapMAVOp(CI, 3),

`

4417

``

`-

unwrapMAVOp(CI, 4), unwrapMAVOp(CI, 5),

`

4418

``

`-

CI->getDebugLoc());

`

``

4434

`+

DR = DbgVariableRecord::createUnresolvedDbgVariableRecord(

`

``

4435

`+

DbgVariableRecord::LocationType::Assign, unwrapMAVMetadataOp(CI, 0),

`

``

4436

`+

unwrapMAVOp(CI, 1), unwrapMAVOp(CI, 2), unwrapMAVOp(CI, 3),

`

``

4437

`+

unwrapMAVMetadataOp(CI, 4),

`

``

4438

`+

/*The address is a Value ref, it will be stored as a Metadata */

`

``

4439

`+

unwrapMAVOp(CI, 5), getDebugLocSafe(CI));

`

4419

4440

` } else if (Name == "declare") {

`

4420

``

`-

DR = new DbgVariableRecord(

`

4421

``

`-

unwrapMAVOp(CI, 0), unwrapMAVOp(CI, 1),

`

4422

``

`-

unwrapMAVOp(CI, 2), CI->getDebugLoc(),

`

4423

``

`-

DbgVariableRecord::LocationType::Declare);

`

``

4441

`+

DR = DbgVariableRecord::createUnresolvedDbgVariableRecord(

`

``

4442

`+

DbgVariableRecord::LocationType::Declare, unwrapMAVMetadataOp(CI, 0),

`

``

4443

`+

unwrapMAVOp(CI, 1), unwrapMAVOp(CI, 2), nullptr, nullptr, nullptr,

`

``

4444

`+

getDebugLocSafe(CI));

`

4424

4445

` } else if (Name == "addr") {

`

4425

4446

`// Upgrade dbg.addr to dbg.value with DW_OP_deref.

`

4426

``

`-

DIExpression *Expr = unwrapMAVOp(CI, 2);

`

4427

``

`-

Expr = DIExpression::append(Expr, dwarf::DW_OP_deref);

`

4428

``

`-

DR = new DbgVariableRecord(unwrapMAVOp(CI, 0),

`

4429

``

`-

unwrapMAVOp(CI, 1), Expr,

`

4430

``

`-

CI->getDebugLoc());

`

``

4447

`+

MDNode *ExprNode = unwrapMAVOp(CI, 2);

`

``

4448

`+

// Don't try to add something to the expression if it's not an expression.

`

``

4449

`+

// Instead, allow the verifier to fail later.

`

``

4450

`+

if (DIExpression *Expr = dyn_cast(ExprNode)) {

`

``

4451

`+

ExprNode = DIExpression::append(Expr, dwarf::DW_OP_deref);

`

``

4452

`+

}

`

``

4453

`+

DR = DbgVariableRecord::createUnresolvedDbgVariableRecord(

`

``

4454

`+

DbgVariableRecord::LocationType::Value, unwrapMAVMetadataOp(CI, 0),

`

``

4455

`+

unwrapMAVOp(CI, 1), ExprNode, nullptr, nullptr, nullptr,

`

``

4456

`+

getDebugLocSafe(CI));

`

4431

4457

` } else if (Name == "value") {

`

4432

4458

`// An old version of dbg.value had an extra offset argument.

`

4433

4459

`unsigned VarOp = 1;

`

`@@ -4440,9 +4466,10 @@ static void upgradeDbgIntrinsicToDbgRecord(StringRef Name, CallBase *CI) {

`

4440

4466

` VarOp = 2;

`

4441

4467

` ExprOp = 3;

`

4442

4468

` }

`

4443

``

`-

DR = new DbgVariableRecord(

`

4444

``

`-

unwrapMAVOp(CI, 0), unwrapMAVOp(CI, VarOp),

`

4445

``

`-

unwrapMAVOp(CI, ExprOp), CI->getDebugLoc());

`

``

4469

`+

DR = DbgVariableRecord::createUnresolvedDbgVariableRecord(

`

``

4470

`+

DbgVariableRecord::LocationType::Value, unwrapMAVMetadataOp(CI, 0),

`

``

4471

`+

unwrapMAVOp(CI, VarOp), unwrapMAVOp(CI, ExprOp), nullptr, nullptr,

`

``

4472

`+

nullptr, getDebugLocSafe(CI));

`

4446

4473

` }

`

4447

4474

`assert(DR && "Unhandled intrinsic kind in upgrade to DbgRecord");

`

4448

4475

` CI->getParent()->insertDbgRecordBefore(DR, CI->getIterator());

`