@@ -5861,33 +5861,41 @@ void AArch64InstrInfo::decomposeStackOffsetForFrameOffsets(
58615861 }
58625862}
58635863
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
58725878 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);
58765880 }
5881+ return Expr.push_back (Operation);
5882+ }
58775883
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+ }
58885890
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;
58915899 }
58925900}
58935901
@@ -5909,19 +5917,26 @@ static MCCFIInstruction createDefCFAExpression(const TargetRegisterInfo &TRI,
59095917 else
59105918 Comment << printReg (Reg, &TRI);
59115919
5912- // Build up the expression (Reg + NumBytes + NumVGScaledBytes * AArch64::VG )
5920+ // Build up the expression (Reg + NumBytes + VG * NumVGScaledBytes )
59135921 SmallString<64 > Expr;
59145922 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+ }
59195935
59205936 // Wrap this into DW_CFA_def_cfa.
59215937 SmallString<64 > DefCfaExpr;
59225938 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 ());
59255940 DefCfaExpr.append (Expr.str ());
59265941 return MCCFIInstruction::createEscape (nullptr , DefCfaExpr.str (), SMLoc (),
59275942 Comment.str ());
@@ -5958,17 +5973,25 @@ MCCFIInstruction llvm::createCFAOffset(const TargetRegisterInfo &TRI,
59585973 llvm::raw_string_ostream Comment (CommentBuffer);
59595974 Comment << printReg (Reg, &TRI) << " @ cfa" ;
59605975
5961- // Build up expression (NumBytes + NumVGScaledBytes * AArch64::VG)
5976+ // Build up expression (CFA + VG * NumVGScaledBytes + NumBytes)
5977+ assert (NumVGScaledBytes && " Expected scalable offset" );
59625978 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+ }
59655989
59665990 // Wrap this into DW_CFA_expression
59675991 SmallString<64 > CfaExpr;
59685992 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 ());
59725995 CfaExpr.append (OffsetExpr.str ());
59735996
59745997 return MCCFIInstruction::createEscape (nullptr , CfaExpr.str (), SMLoc (),
0 commit comments