@@ -162,7 +162,8 @@ class DILocationVerifier : public GISelChangeObserver {
162
162
// they could have originated from constants, and we don't want a jumpy
163
163
// debug experience.
164
164
assert ((CurrInst->getDebugLoc () == MI.getDebugLoc () ||
165
- (MI.getParent ()->isEntryBlock () && !MI.getDebugLoc ())) &&
165
+ (MI.getParent ()->isEntryBlock () && !MI.getDebugLoc ()) ||
166
+ (MI.isDebugInstr ())) &&
166
167
" Line info was not transferred to all instructions" );
167
168
}
168
169
};
@@ -2006,47 +2007,35 @@ std::optional<MCRegister> IRTranslator::getArgPhysReg(Argument &Arg) {
2006
2007
return VRegDef->getOperand (1 ).getReg ().asMCReg ();
2007
2008
}
2008
2009
2009
- bool IRTranslator::translateIfEntryValueArgument (const DbgValueInst &DebugInst,
2010
+ bool IRTranslator::translateIfEntryValueArgument (bool isDeclare, Value *Val,
2011
+ const DILocalVariable *Var,
2012
+ const DIExpression *Expr,
2013
+ const DebugLoc &DL,
2010
2014
MachineIRBuilder &MIRBuilder) {
2011
- auto *Arg = dyn_cast<Argument>(DebugInst. getValue () );
2015
+ auto *Arg = dyn_cast<Argument>(Val );
2012
2016
if (!Arg)
2013
2017
return false ;
2014
2018
2015
- const DIExpression *Expr = DebugInst.getExpression ();
2016
2019
if (!Expr->isEntryValue ())
2017
2020
return false ;
2018
2021
2019
2022
std::optional<MCRegister> PhysReg = getArgPhysReg (*Arg);
2020
2023
if (!PhysReg) {
2021
- LLVM_DEBUG (dbgs () << " Dropping dbg.value: expression is entry_value but "
2022
- " couldn't find a physical register\n "
2023
- << DebugInst << " \n " );
2024
+ LLVM_DEBUG (dbgs () << " Dropping dbg." << (isDeclare ? " declare" : " value" )
2025
+ << " : expression is entry_value but "
2026
+ << " couldn't find a physical register\n " );
2027
+ LLVM_DEBUG (dbgs () << *Var << " \n " );
2024
2028
return true ;
2025
2029
}
2026
2030
2027
- MIRBuilder.buildDirectDbgValue (*PhysReg, DebugInst.getVariable (),
2028
- DebugInst.getExpression ());
2029
- return true ;
2030
- }
2031
-
2032
- bool IRTranslator::translateIfEntryValueArgument (
2033
- const DbgDeclareInst &DebugInst) {
2034
- auto *Arg = dyn_cast<Argument>(DebugInst.getAddress ());
2035
- if (!Arg)
2036
- return false ;
2037
-
2038
- const DIExpression *Expr = DebugInst.getExpression ();
2039
- if (!Expr->isEntryValue ())
2040
- return false ;
2041
-
2042
- std::optional<MCRegister> PhysReg = getArgPhysReg (*Arg);
2043
- if (!PhysReg)
2044
- return false ;
2031
+ if (isDeclare) {
2032
+ // Append an op deref to account for the fact that this is a dbg_declare.
2033
+ Expr = DIExpression::append (Expr, dwarf::DW_OP_deref);
2034
+ MF->setVariableDbgInfo (Var, Expr, *PhysReg, DL);
2035
+ } else {
2036
+ MIRBuilder.buildDirectDbgValue (*PhysReg, Var, Expr);
2037
+ }
2045
2038
2046
- // Append an op deref to account for the fact that this is a dbg_declare.
2047
- Expr = DIExpression::append (Expr, dwarf::DW_OP_deref);
2048
- MF->setVariableDbgInfo (DebugInst.getVariable (), Expr, *PhysReg,
2049
- DebugInst.getDebugLoc ());
2050
2039
return true ;
2051
2040
}
2052
2041
@@ -2100,32 +2089,8 @@ bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
2100
2089
case Intrinsic::dbg_declare: {
2101
2090
const DbgDeclareInst &DI = cast<DbgDeclareInst>(CI);
2102
2091
assert (DI.getVariable () && " Missing variable" );
2103
-
2104
- const Value *Address = DI.getAddress ();
2105
- if (!Address || isa<UndefValue>(Address)) {
2106
- LLVM_DEBUG (dbgs () << " Dropping debug info for " << DI << " \n " );
2107
- return true ;
2108
- }
2109
-
2110
- assert (DI.getVariable ()->isValidLocationForIntrinsic (
2111
- MIRBuilder.getDebugLoc ()) &&
2112
- " Expected inlined-at fields to agree" );
2113
- auto AI = dyn_cast<AllocaInst>(Address);
2114
- if (AI && AI->isStaticAlloca ()) {
2115
- // Static allocas are tracked at the MF level, no need for DBG_VALUE
2116
- // instructions (in fact, they get ignored if they *do* exist).
2117
- MF->setVariableDbgInfo (DI.getVariable (), DI.getExpression (),
2118
- getOrCreateFrameIndex (*AI), DI.getDebugLoc ());
2119
- return true ;
2120
- }
2121
-
2122
- if (translateIfEntryValueArgument (DI))
2123
- return true ;
2124
-
2125
- // A dbg.declare describes the address of a source variable, so lower it
2126
- // into an indirect DBG_VALUE.
2127
- MIRBuilder.buildIndirectDbgValue (getOrCreateVReg (*Address),
2128
- DI.getVariable (), DI.getExpression ());
2092
+ translateDbgDeclareRecord (DI.getAddress (), DI.hasArgList (), DI.getVariable (),
2093
+ DI.getExpression (), DI.getDebugLoc (), MIRBuilder);
2129
2094
return true ;
2130
2095
}
2131
2096
case Intrinsic::dbg_label: {
@@ -2158,41 +2123,8 @@ bool IRTranslator::translateKnownIntrinsic(const CallInst &CI, Intrinsic::ID ID,
2158
2123
case Intrinsic::dbg_value: {
2159
2124
// This form of DBG_VALUE is target-independent.
2160
2125
const DbgValueInst &DI = cast<DbgValueInst>(CI);
2161
- const Value *V = DI.getValue ();
2162
- assert (DI.getVariable ()->isValidLocationForIntrinsic (
2163
- MIRBuilder.getDebugLoc ()) &&
2164
- " Expected inlined-at fields to agree" );
2165
- if (!V || DI.hasArgList ()) {
2166
- // DI cannot produce a valid DBG_VALUE, so produce an undef DBG_VALUE to
2167
- // terminate any prior location.
2168
- MIRBuilder.buildIndirectDbgValue (0 , DI.getVariable (), DI.getExpression ());
2169
- return true ;
2170
- }
2171
- if (const auto *CI = dyn_cast<Constant>(V)) {
2172
- MIRBuilder.buildConstDbgValue (*CI, DI.getVariable (), DI.getExpression ());
2173
- return true ;
2174
- }
2175
- if (auto *AI = dyn_cast<AllocaInst>(V);
2176
- AI && AI->isStaticAlloca () && DI.getExpression ()->startsWithDeref ()) {
2177
- // If the value is an alloca and the expression starts with a
2178
- // dereference, track a stack slot instead of a register, as registers
2179
- // may be clobbered.
2180
- auto ExprOperands = DI.getExpression ()->getElements ();
2181
- auto *ExprDerefRemoved =
2182
- DIExpression::get (AI->getContext (), ExprOperands.drop_front ());
2183
- MIRBuilder.buildFIDbgValue (getOrCreateFrameIndex (*AI), DI.getVariable (),
2184
- ExprDerefRemoved);
2185
- return true ;
2186
- }
2187
- if (translateIfEntryValueArgument (DI, MIRBuilder))
2188
- return true ;
2189
- for (Register Reg : getOrCreateVRegs (*V)) {
2190
- // FIXME: This does not handle register-indirect values at offset 0. The
2191
- // direct/indirect thing shouldn't really be handled by something as
2192
- // implicit as reg+noreg vs reg+imm in the first place, but it seems
2193
- // pretty baked in right now.
2194
- MIRBuilder.buildDirectDbgValue (Reg, DI.getVariable (), DI.getExpression ());
2195
- }
2126
+ translateDbgValueRecord (DI.getValue (), DI.hasArgList (), DI.getVariable (),
2127
+ DI.getExpression (), DI.getDebugLoc (), MIRBuilder);
2196
2128
return true ;
2197
2129
}
2198
2130
case Intrinsic::uadd_with_overflow:
@@ -3250,6 +3182,102 @@ void IRTranslator::finishPendingPhis() {
3250
3182
}
3251
3183
}
3252
3184
3185
+ void IRTranslator::translateDbgValueRecord (Value *V, bool HasArgList,
3186
+ const DILocalVariable *Variable,
3187
+ const DIExpression *Expression,
3188
+ const DebugLoc &DL,
3189
+ MachineIRBuilder &MIRBuilder) {
3190
+ assert (Variable->isValidLocationForIntrinsic (DL) &&
3191
+ " Expected inlined-at fields to agree" );
3192
+ // Act as if we're handling a debug intrinsic.
3193
+ MIRBuilder.setDebugLoc (DL);
3194
+
3195
+ if (!V || HasArgList) {
3196
+ // DI cannot produce a valid DBG_VALUE, so produce an undef DBG_VALUE to
3197
+ // terminate any prior location.
3198
+ MIRBuilder.buildIndirectDbgValue (0 , Variable, Expression);
3199
+ return ;
3200
+ }
3201
+
3202
+ if (const auto *CI = dyn_cast<Constant>(V)) {
3203
+ MIRBuilder.buildConstDbgValue (*CI, Variable, Expression);
3204
+ return ;
3205
+ }
3206
+
3207
+ if (auto *AI = dyn_cast<AllocaInst>(V);
3208
+ AI && AI->isStaticAlloca () && Expression->startsWithDeref ()) {
3209
+ // If the value is an alloca and the expression starts with a
3210
+ // dereference, track a stack slot instead of a register, as registers
3211
+ // may be clobbered.
3212
+ auto ExprOperands = Expression->getElements ();
3213
+ auto *ExprDerefRemoved =
3214
+ DIExpression::get (AI->getContext (), ExprOperands.drop_front ());
3215
+ MIRBuilder.buildFIDbgValue (getOrCreateFrameIndex (*AI), Variable,
3216
+ ExprDerefRemoved);
3217
+ return ;
3218
+ }
3219
+ if (translateIfEntryValueArgument (false , V, Variable, Expression, DL,
3220
+ MIRBuilder))
3221
+ return ;
3222
+ for (Register Reg : getOrCreateVRegs (*V)) {
3223
+ // FIXME: This does not handle register-indirect values at offset 0. The
3224
+ // direct/indirect thing shouldn't really be handled by something as
3225
+ // implicit as reg+noreg vs reg+imm in the first place, but it seems
3226
+ // pretty baked in right now.
3227
+ MIRBuilder.buildDirectDbgValue (Reg, Variable, Expression);
3228
+ }
3229
+ return ;
3230
+ }
3231
+
3232
+ void IRTranslator::translateDbgDeclareRecord (Value *Address, bool HasArgList,
3233
+ const DILocalVariable *Variable,
3234
+ const DIExpression *Expression,
3235
+ const DebugLoc &DL,
3236
+ MachineIRBuilder &MIRBuilder) {
3237
+ if (!Address || isa<UndefValue>(Address)) {
3238
+ LLVM_DEBUG (dbgs () << " Dropping debug info for " << *Variable << " \n " );
3239
+ return ;
3240
+ }
3241
+
3242
+ assert (Variable->isValidLocationForIntrinsic (DL) &&
3243
+ " Expected inlined-at fields to agree" );
3244
+ auto AI = dyn_cast<AllocaInst>(Address);
3245
+ if (AI && AI->isStaticAlloca ()) {
3246
+ // Static allocas are tracked at the MF level, no need for DBG_VALUE
3247
+ // instructions (in fact, they get ignored if they *do* exist).
3248
+ MF->setVariableDbgInfo (Variable, Expression,
3249
+ getOrCreateFrameIndex (*AI), DL);
3250
+ return ;
3251
+ }
3252
+
3253
+ if (translateIfEntryValueArgument (true , Address, Variable,
3254
+ Expression, DL,
3255
+ MIRBuilder))
3256
+ return ;
3257
+
3258
+ // A dbg.declare describes the address of a source variable, so lower it
3259
+ // into an indirect DBG_VALUE.
3260
+ MIRBuilder.setDebugLoc (DL);
3261
+ MIRBuilder.buildIndirectDbgValue (getOrCreateVReg (*Address),
3262
+ Variable, Expression);
3263
+ return ;
3264
+ }
3265
+
3266
+ void IRTranslator::translateDbgInfo (const Instruction &Inst,
3267
+ MachineIRBuilder &MIRBuilder) {
3268
+ for (DPValue &DPV : Inst.getDbgValueRange ()) {
3269
+ const DILocalVariable *Variable = DPV.getVariable ();
3270
+ const DIExpression *Expression = DPV.getExpression ();
3271
+ Value *V = DPV.getVariableLocationOp (0 );
3272
+ if (DPV.isDbgDeclare ())
3273
+ translateDbgDeclareRecord (V, DPV.hasArgList (), Variable,
3274
+ Expression, DPV.getDebugLoc (), MIRBuilder);
3275
+ else
3276
+ translateDbgValueRecord (V, DPV.hasArgList (), Variable,
3277
+ Expression, DPV.getDebugLoc (), MIRBuilder);
3278
+ }
3279
+ }
3280
+
3253
3281
bool IRTranslator::translate (const Instruction &Inst) {
3254
3282
CurBuilder->setDebugLoc (Inst.getDebugLoc ());
3255
3283
CurBuilder->setPCSections (Inst.getMetadata (LLVMContext::MD_pcsections));
@@ -3760,6 +3788,10 @@ bool IRTranslator::runOnMachineFunction(MachineFunction &CurMF) {
3760
3788
#ifndef NDEBUG
3761
3789
Verifier.setCurrentInst (&Inst);
3762
3790
#endif // ifndef NDEBUG
3791
+
3792
+ // Translate any debug-info attached to the instruction.
3793
+ translateDbgInfo (Inst, *CurBuilder.get ());
3794
+
3763
3795
if (translate (Inst))
3764
3796
continue ;
3765
3797
0 commit comments