@@ -5766,16 +5766,28 @@ void CGDebugInfo::EmitPseudoVariable(CGBuilderTy &Builder,
5766
5766
// it is loaded upon use, so we identify such pattern here.
5767
5767
if (llvm::LoadInst *Load = dyn_cast<llvm::LoadInst>(Value)) {
5768
5768
llvm::Value *Var = Load->getPointerOperand ();
5769
- // There can be implicit type cast applied on a variable if it is an opaque
5770
- // ptr, in this case its debug info may not match the actual type of object
5771
- // being used as in the next instruction, so we will need to emit a pseudo
5772
- // variable for type-casted value.
5773
- auto DeclareTypeMatches = [&](auto *DbgDeclare) {
5774
- return DbgDeclare->getVariable ()->getType () == Type;
5775
- };
5776
- if (any_of (llvm::findDbgDeclares (Var), DeclareTypeMatches) ||
5777
- any_of (llvm::findDVRDeclares (Var), DeclareTypeMatches))
5778
- return ;
5769
+ if (llvm::Metadata *MDValue = llvm::ValueAsMetadata::getIfExists (Var)) {
5770
+ if (llvm::Value *DbgValue = llvm::MetadataAsValue::getIfExists (
5771
+ CGM.getLLVMContext (), MDValue)) {
5772
+ for (llvm::User *U : DbgValue->users ()) {
5773
+ if (llvm::CallInst *DbgDeclare = dyn_cast<llvm::CallInst>(U)) {
5774
+ if (DbgDeclare->getCalledFunction ()->getIntrinsicID () ==
5775
+ llvm::Intrinsic::dbg_declare &&
5776
+ DbgDeclare->getArgOperand (0 ) == DbgValue) {
5777
+ // There can be implicit type cast applied on a variable if it is
5778
+ // an opaque ptr, in this case its debug info may not match the
5779
+ // actual type of object being used as in the next instruction, so
5780
+ // we will need to emit a pseudo variable for type-casted value.
5781
+ llvm::DILocalVariable *MDNode = cast<llvm::DILocalVariable>(
5782
+ cast<llvm::MetadataAsValue>(DbgDeclare->getOperand (1 ))
5783
+ ->getMetadata ());
5784
+ if (MDNode->getType () == Type)
5785
+ return ;
5786
+ }
5787
+ }
5788
+ }
5789
+ }
5790
+ }
5779
5791
}
5780
5792
5781
5793
// Find the correct location to insert a sequence of instructions to
0 commit comments