@@ -715,18 +715,35 @@ class IRGenSILFunction :
715
715
isa<llvm::PHINode>(Storage);
716
716
}
717
717
718
+ // / Unconditionally emit a stack shadow copy of an \c llvm::Value.
719
+ llvm::Value *emitShadowCopy (llvm::Value *Storage, const SILDebugScope *Scope,
720
+ StringRef Name, unsigned ArgNo, Alignment Align) {
721
+ if (Align.isZero ())
722
+ Align = IGM.getPointerAlignment ();
723
+
724
+ auto &Alloca = ShadowStackSlots[{ArgNo, {Scope, Name}}];
725
+ if (!Alloca.isValid ())
726
+ Alloca = createAlloca (Storage->getType (), Align, Name+" .addr" );
727
+
728
+ ArtificialLocation AutoRestore (Scope, IGM.DebugInfo , Builder);
729
+ Builder.CreateStore (Storage, Alloca.getAddress (), Align);
730
+ return Alloca.getAddress ();
731
+ }
732
+
718
733
// / At -Onone, emit a shadow copy of an Address in an alloca, so the
719
734
// / register allocator doesn't elide the dbg.value intrinsic when
720
735
// / register pressure is high. There is a trade-off to this: With
721
736
// / shadow copies, we lose the precise lifetime.
722
- llvm::Value *emitShadowCopy (llvm::Value *Storage, const SILDebugScope *Scope,
723
- StringRef Name, unsigned ArgNo, bool IsAnonymous,
724
- Alignment Align = Alignment(0 )) {
725
- auto Ty = Storage->getType ();
737
+ llvm::Value *emitShadowCopyIfNeeded (llvm::Value *Storage,
738
+ const SILDebugScope *Scope,
739
+ StringRef Name, unsigned ArgNo,
740
+ bool IsAnonymous,
741
+ Alignment Align = Alignment(0 )) {
726
742
// Never emit shadow copies when optimizing, or if already on the stack.
743
+ // No debug info is emitted for refcounts either.
727
744
if (IGM.IRGen .Opts .shouldOptimize () || IsAnonymous ||
728
745
isa<llvm::AllocaInst>(Storage) || isa<llvm::UndefValue>(Storage) ||
729
- Ty == IGM.RefCountedPtrTy ) // No debug info is emitted for refcounts.
746
+ Storage-> getType () == IGM.RefCountedPtrTy )
730
747
return Storage;
731
748
732
749
// Always emit shadow copies for function arguments.
@@ -739,29 +756,23 @@ class IRGenSILFunction :
739
756
ValueDomPoints.push_back ({Value, getActiveDominancePoint ()});
740
757
return Storage;
741
758
}
742
-
743
- if (Align.isZero ())
744
- Align = IGM.getPointerAlignment ();
745
-
746
- auto &Alloca = ShadowStackSlots[{ArgNo, {Scope, Name}}];
747
- if (!Alloca.isValid ())
748
- Alloca = createAlloca (Ty, Align, Name+" .addr" );
749
-
750
- ArtificialLocation AutoRestore (Scope, IGM.DebugInfo , Builder);
751
- Builder.CreateStore (Storage, Alloca.getAddress (), Align);
752
- return Alloca.getAddress ();
759
+ return emitShadowCopy (Storage, Scope, Name, ArgNo, Align);
753
760
}
754
761
755
- llvm::Value *emitShadowCopy (Address Storage, const SILDebugScope *Scope,
756
- StringRef Name, unsigned ArgNo,
757
- bool IsAnonymous) {
758
- return emitShadowCopy (Storage.getAddress (), Scope, Name, ArgNo, IsAnonymous,
759
- Storage.getAlignment ());
762
+ // / Like \c emitShadowCopyIfNeeded() but takes an \c Address instead of an
763
+ // / \c llvm::Value.
764
+ llvm::Value *emitShadowCopyIfNeeded (Address Storage,
765
+ const SILDebugScope *Scope,
766
+ StringRef Name, unsigned ArgNo,
767
+ bool IsAnonymous) {
768
+ return emitShadowCopyIfNeeded (Storage.getAddress (), Scope, Name, ArgNo,
769
+ IsAnonymous, Storage.getAlignment ());
760
770
}
761
771
762
- void emitShadowCopy (SILValue &SILVal, const SILDebugScope *Scope,
763
- StringRef Name, unsigned ArgNo, bool IsAnonymous,
764
- llvm::SmallVectorImpl<llvm::Value *> ©) {
772
+ // / Like \c emitShadowCopyIfNeeded() but takes an exploded value.
773
+ void emitShadowCopyIfNeeded (SILValue &SILVal, const SILDebugScope *Scope,
774
+ StringRef Name, unsigned ArgNo, bool IsAnonymous,
775
+ llvm::SmallVectorImpl<llvm::Value *> ©) {
765
776
Explosion e = getLoweredExplosion (SILVal);
766
777
767
778
// Only do this at -O0.
@@ -775,7 +786,8 @@ class IRGenSILFunction :
775
786
if (e.size () <= 1 ) {
776
787
auto vals = e.claimAll ();
777
788
for (auto val : vals)
778
- copy.push_back (emitShadowCopy (val, Scope, Name, ArgNo, IsAnonymous));
789
+ copy.push_back (
790
+ emitShadowCopyIfNeeded (val, Scope, Name, ArgNo, IsAnonymous));
779
791
return ;
780
792
}
781
793
@@ -3578,8 +3590,9 @@ void IRGenSILFunction::emitErrorResultVar(SILResultInfo ErrorInfo,
3578
3590
return ;
3579
3591
auto ErrorResultSlot = getErrorResultSlot (IGM.silConv .getSILType (ErrorInfo));
3580
3592
SILDebugVariable Var = DbgValue->getVarInfo ();
3581
- auto Storage = emitShadowCopy (ErrorResultSlot.getAddress (), getDebugScope (),
3582
- Var.Name , Var.ArgNo , false );
3593
+ auto Storage =
3594
+ emitShadowCopyIfNeeded (ErrorResultSlot.getAddress (), getDebugScope (),
3595
+ Var.Name , Var.ArgNo , false );
3583
3596
DebugTypeInfo DTI (nullptr , nullptr , ErrorInfo.getType (),
3584
3597
ErrorResultSlot->getType (), IGM.getPointerSize (),
3585
3598
IGM.getPointerAlignment (), true );
@@ -3625,7 +3638,8 @@ void IRGenSILFunction::visitDebugValueInst(DebugValueInst *i) {
3625
3638
// Put the value into a stack slot at -Onone.
3626
3639
llvm::SmallVector<llvm::Value *, 8 > Copy;
3627
3640
unsigned ArgNo = i->getVarInfo ().ArgNo ;
3628
- emitShadowCopy (SILVal, i->getDebugScope (), Name, ArgNo, IsAnonymous, Copy);
3641
+ emitShadowCopyIfNeeded (SILVal, i->getDebugScope (), Name, ArgNo, IsAnonymous,
3642
+ Copy);
3629
3643
emitDebugVariableDeclaration (Copy, DbgTy, SILTy, i->getDebugScope (),
3630
3644
i->getDecl (), Name, ArgNo);
3631
3645
}
@@ -3665,8 +3679,9 @@ void IRGenSILFunction::visitDebugValueAddrInst(DebugValueAddrInst *i) {
3665
3679
// intrinsic.
3666
3680
unsigned ArgNo = i->getVarInfo ().ArgNo ;
3667
3681
emitDebugVariableDeclaration (
3668
- emitShadowCopy (Addr, i->getDebugScope (), Name, ArgNo, IsAnonymous), DbgTy,
3669
- SILType (), i->getDebugScope (), Decl, Name, ArgNo,
3682
+ emitShadowCopyIfNeeded (Addr, i->getDebugScope (), Name, ArgNo,
3683
+ IsAnonymous),
3684
+ DbgTy, SILType (), i->getDebugScope (), Decl, Name, ArgNo,
3670
3685
(IsLoadablyByAddress || DbgTy.isImplicitlyIndirect ()) ? DirectValue
3671
3686
: IndirectValue);
3672
3687
}
@@ -3871,6 +3886,31 @@ void IRGenSILFunction::emitDebugInfoForAllocStack(AllocStackInst *i,
3871
3886
const TypeInfo &type,
3872
3887
llvm::Value *addr) {
3873
3888
VarDecl *Decl = i->getDecl ();
3889
+ // Describe the underlying alloca. This way an llvm.dbg.declare instrinsic
3890
+ // is used, which is valid for the entire lifetime of the alloca.
3891
+ if (auto *BitCast = dyn_cast<llvm::BitCastInst>(addr))
3892
+ if (auto *Alloca = dyn_cast<llvm::AllocaInst>(BitCast->getOperand (0 )))
3893
+ addr = Alloca;
3894
+
3895
+ auto DS = i->getDebugScope ();
3896
+ if (!DS)
3897
+ return ;
3898
+
3899
+ bool IsAnonymous = false ;
3900
+ unsigned ArgNo = i->getVarInfo ().ArgNo ;
3901
+ StringRef Name = getVarName (i, IsAnonymous);
3902
+
3903
+ // At this point addr must be an alloca or an undef.
3904
+ assert (isa<llvm::AllocaInst>(addr) || isa<llvm::UndefValue>(addr));
3905
+ auto Indirection = DirectValue;
3906
+ if (!IGM.IRGen .Opts .shouldOptimize ())
3907
+ if (auto *Alloca = dyn_cast<llvm::AllocaInst>(addr))
3908
+ if (!Alloca->isStaticAlloca ()) {
3909
+ // Store the address of the dynamic alloca on the stack.
3910
+ addr = emitShadowCopy (addr, DS, Name, ArgNo, IGM.getPointerAlignment ());
3911
+ Indirection = IndirectValue;
3912
+ }
3913
+
3874
3914
if (IGM.DebugInfo && Decl) {
3875
3915
// Ignore compiler-generated patterns but not optional bindings.
3876
3916
if (auto *Pattern = Decl->getParentPattern ())
@@ -3883,11 +3923,13 @@ void IRGenSILFunction::emitDebugInfoForAllocStack(AllocStackInst *i,
3883
3923
auto DbgTy = DebugTypeInfo::getLocalVariable (
3884
3924
CurSILFn->getDeclContext (), CurSILFn->getGenericEnvironment (), Decl,
3885
3925
RealType, type, false );
3886
- bool IsAnonymous = false ;
3887
- StringRef Name = getVarName (i, IsAnonymous);
3888
- if (auto DS = i->getDebugScope ())
3889
- emitDebugVariableDeclaration (addr, DbgTy, SILTy, DS, Decl, Name,
3890
- i->getVarInfo ().ArgNo );
3926
+
3927
+ // FIXME: This is working around the inverse special case in LLDB.
3928
+ if (DbgTy.isImplicitlyIndirect ())
3929
+ Indirection = DirectValue;
3930
+
3931
+ emitDebugVariableDeclaration (addr, DbgTy, SILTy, DS, Decl, Name, ArgNo,
3932
+ Indirection);
3891
3933
}
3892
3934
}
3893
3935
@@ -4095,8 +4137,8 @@ void IRGenSILFunction::visitAllocBoxInst(swift::AllocBoxInst *i) {
4095
4137
4096
4138
IGM.DebugInfo ->emitVariableDeclaration (
4097
4139
Builder,
4098
- emitShadowCopy (boxWithAddr.getAddress (), i->getDebugScope (), Name, 0 ,
4099
- IsAnonymous),
4140
+ emitShadowCopyIfNeeded (boxWithAddr.getAddress (), i->getDebugScope (),
4141
+ Name, 0 , IsAnonymous),
4100
4142
DbgTy, i->getDebugScope (), Decl, Name, 0 ,
4101
4143
DbgTy.isImplicitlyIndirect () ? DirectValue : IndirectValue);
4102
4144
}
0 commit comments