@@ -5861,33 +5861,41 @@ void AArch64InstrInfo::decomposeStackOffsetForFrameOffsets(
5861
5861
}
5862
5862
}
5863
5863
5864
- // Convenience function to create a DWARF expression for
5865
- // Expr + NumBytes + NumVGScaledBytes * AArch64::VG
5866
- static void appendVGScaledOffsetExpr (SmallVectorImpl<char > &Expr, int NumBytes,
5867
- int NumVGScaledBytes, unsigned VG,
5868
- llvm::raw_string_ostream &Comment) {
5869
- uint8_t buffer[16 ];
5870
-
5871
- if (NumBytes) {
5864
+ // Convenience function to create a DWARF expression for: Constant `Operation`.
5865
+ // This helper emits compact sequences for common cases. For example, for`-15
5866
+ // DW_OP_plus`, this helper would create DW_OP_lit15 DW_OP_minus.
5867
+ static void appendConstantExpr (SmallVectorImpl<char > &Expr, int64_t Constant,
5868
+ dwarf::LocationAtom Operation) {
5869
+ if (Operation == dwarf::DW_OP_plus && Constant < 0 && -Constant <= 31 ) {
5870
+ // -Constant (1 to 31)
5871
+ Expr.push_back (dwarf::DW_OP_lit0 - Constant);
5872
+ Operation = dwarf::DW_OP_minus;
5873
+ } else if (Constant >= 0 && Constant <= 31 ) {
5874
+ // Literal value 0 to 31
5875
+ Expr.push_back (dwarf::DW_OP_lit0 + Constant);
5876
+ } else {
5877
+ // Signed constant
5872
5878
Expr.push_back (dwarf::DW_OP_consts);
5873
- Expr.append (buffer, buffer + encodeSLEB128 (NumBytes, buffer));
5874
- Expr.push_back ((uint8_t )dwarf::DW_OP_plus);
5875
- Comment << (NumBytes < 0 ? " - " : " + " ) << std::abs (NumBytes);
5879
+ appendLEB128<LEB128Sign::Signed>(Expr, Constant);
5876
5880
}
5881
+ return Expr.push_back (Operation);
5882
+ }
5877
5883
5878
- if (NumVGScaledBytes) {
5879
- Expr.push_back ((uint8_t )dwarf::DW_OP_consts);
5880
- Expr.append (buffer, buffer + encodeSLEB128 (NumVGScaledBytes, buffer));
5881
-
5882
- Expr.push_back ((uint8_t )dwarf::DW_OP_bregx);
5883
- Expr.append (buffer, buffer + encodeULEB128 (VG, buffer));
5884
- Expr.push_back (0 );
5885
-
5886
- Expr.push_back ((uint8_t )dwarf::DW_OP_mul);
5887
- Expr.push_back ((uint8_t )dwarf::DW_OP_plus);
5884
+ // Convenience function to create a DWARF expression for a register.
5885
+ static void appendReadRegExpr (SmallVectorImpl<char > &Expr, unsigned RegNum) {
5886
+ Expr.push_back (dwarf::DW_OP_bregx);
5887
+ appendLEB128<LEB128Sign::Unsigned>(Expr, RegNum);
5888
+ Expr.push_back (0 );
5889
+ }
5888
5890
5889
- Comment << (NumVGScaledBytes < 0 ? " - " : " + " )
5890
- << std::abs (NumVGScaledBytes) << " * VG" ;
5891
+ // Convenience function to create a comment for
5892
+ // (+/-) NumBytes (* RegScale)?
5893
+ static void appendOffsetComment (int NumBytes, llvm::raw_string_ostream &Comment,
5894
+ StringRef RegScale = {}) {
5895
+ if (NumBytes) {
5896
+ Comment << (NumBytes < 0 ? " - " : " + " ) << std::abs (NumBytes);
5897
+ if (!RegScale.empty ())
5898
+ Comment << ' ' << RegScale;
5891
5899
}
5892
5900
}
5893
5901
@@ -5909,19 +5917,26 @@ static MCCFIInstruction createDefCFAExpression(const TargetRegisterInfo &TRI,
5909
5917
else
5910
5918
Comment << printReg (Reg, &TRI);
5911
5919
5912
- // Build up the expression (Reg + NumBytes + NumVGScaledBytes * AArch64::VG )
5920
+ // Build up the expression (Reg + NumBytes + VG * NumVGScaledBytes )
5913
5921
SmallString<64 > Expr;
5914
5922
unsigned DwarfReg = TRI.getDwarfRegNum (Reg, true );
5915
- Expr.push_back ((uint8_t )(dwarf::DW_OP_breg0 + DwarfReg));
5916
- Expr.push_back (0 );
5917
- appendVGScaledOffsetExpr (Expr, NumBytes, NumVGScaledBytes,
5918
- TRI.getDwarfRegNum (AArch64::VG, true ), Comment);
5923
+ assert (DwarfReg >= 0 && DwarfReg <= 31 && " DwarfReg out of bounds (0..31)" );
5924
+ // Reg + NumBytes
5925
+ Expr.push_back (dwarf::DW_OP_breg0 + DwarfReg);
5926
+ appendLEB128<LEB128Sign::Signed>(Expr, NumBytes);
5927
+ appendOffsetComment (NumBytes, Comment);
5928
+ if (NumVGScaledBytes) {
5929
+ // + VG * NumVGScaledBytes
5930
+ appendOffsetComment (NumVGScaledBytes, Comment, " * VG" );
5931
+ appendReadRegExpr (Expr, TRI.getDwarfRegNum (AArch64::VG, true ));
5932
+ appendConstantExpr (Expr, NumVGScaledBytes, dwarf::DW_OP_mul);
5933
+ Expr.push_back (dwarf::DW_OP_plus);
5934
+ }
5919
5935
5920
5936
// Wrap this into DW_CFA_def_cfa.
5921
5937
SmallString<64 > DefCfaExpr;
5922
5938
DefCfaExpr.push_back (dwarf::DW_CFA_def_cfa_expression);
5923
- uint8_t buffer[16 ];
5924
- DefCfaExpr.append (buffer, buffer + encodeULEB128 (Expr.size (), buffer));
5939
+ appendLEB128<LEB128Sign::Unsigned>(DefCfaExpr, Expr.size ());
5925
5940
DefCfaExpr.append (Expr.str ());
5926
5941
return MCCFIInstruction::createEscape (nullptr , DefCfaExpr.str (), SMLoc (),
5927
5942
Comment.str ());
@@ -5958,17 +5973,25 @@ MCCFIInstruction llvm::createCFAOffset(const TargetRegisterInfo &TRI,
5958
5973
llvm::raw_string_ostream Comment (CommentBuffer);
5959
5974
Comment << printReg (Reg, &TRI) << " @ cfa" ;
5960
5975
5961
- // Build up expression (NumBytes + NumVGScaledBytes * AArch64::VG)
5976
+ // Build up expression (CFA + VG * NumVGScaledBytes + NumBytes)
5977
+ assert (NumVGScaledBytes && " Expected scalable offset" );
5962
5978
SmallString<64 > OffsetExpr;
5963
- appendVGScaledOffsetExpr (OffsetExpr, NumBytes, NumVGScaledBytes,
5964
- TRI.getDwarfRegNum (AArch64::VG, true ), Comment);
5979
+ // + VG * NumVGScaledBytes
5980
+ appendOffsetComment (NumVGScaledBytes, Comment, " * VG" );
5981
+ appendReadRegExpr (OffsetExpr, TRI.getDwarfRegNum (AArch64::VG, true ));
5982
+ appendConstantExpr (OffsetExpr, NumVGScaledBytes, dwarf::DW_OP_mul);
5983
+ OffsetExpr.push_back (dwarf::DW_OP_plus);
5984
+ if (NumBytes) {
5985
+ // + NumBytes
5986
+ appendOffsetComment (NumBytes, Comment);
5987
+ appendConstantExpr (OffsetExpr, NumBytes, dwarf::DW_OP_plus);
5988
+ }
5965
5989
5966
5990
// Wrap this into DW_CFA_expression
5967
5991
SmallString<64 > CfaExpr;
5968
5992
CfaExpr.push_back (dwarf::DW_CFA_expression);
5969
- uint8_t buffer[16 ];
5970
- CfaExpr.append (buffer, buffer + encodeULEB128 (DwarfReg, buffer));
5971
- CfaExpr.append (buffer, buffer + encodeULEB128 (OffsetExpr.size (), buffer));
5993
+ appendLEB128<LEB128Sign::Unsigned>(CfaExpr, DwarfReg);
5994
+ appendLEB128<LEB128Sign::Unsigned>(CfaExpr, OffsetExpr.size ());
5972
5995
CfaExpr.append (OffsetExpr.str ());
5973
5996
5974
5997
return MCCFIInstruction::createEscape (nullptr , CfaExpr.str (), SMLoc (),
0 commit comments