1 //===-- llvm/CodeGen/DwarfExpression.cpp - Dwarf Debug Framework ----------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file contains support for writing dwarf debug info into asm files. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "DwarfExpression.h" 15 #include "DwarfDebug.h" 16 #include "llvm/ADT/SmallBitVector.h" 17 #include "llvm/CodeGen/AsmPrinter.h" 18 #include "llvm/Support/Dwarf.h" 19 #include "llvm/Target/TargetMachine.h" 20 #include "llvm/Target/TargetRegisterInfo.h" 21 #include "llvm/Target/TargetSubtargetInfo.h" 22 23 using namespace llvm; 24 25 void DwarfExpression::addReg(int DwarfReg, const char *Comment) { 26 assert(DwarfReg >= 0 && "invalid negative dwarf register number"); 27 assert((LocationKind == Unknown || LocationKind == Register) && 28 "location description already locked down"); 29 LocationKind = Register; 30 if (DwarfReg < 32) { 31 emitOp(dwarf::DW_OP_reg0 + DwarfReg, Comment); 32 } else { 33 emitOp(dwarf::DW_OP_regx, Comment); 34 emitUnsigned(DwarfReg); 35 } 36 } 37 38 void DwarfExpression::addBReg(int DwarfReg, int Offset) { 39 assert(DwarfReg >= 0 && "invalid negative dwarf register number"); 40 assert(LocationKind != Register && "location description already locked down"); 41 if (DwarfReg < 32) { 42 emitOp(dwarf::DW_OP_breg0 + DwarfReg); 43 } else { 44 emitOp(dwarf::DW_OP_bregx); 45 emitUnsigned(DwarfReg); 46 } 47 emitSigned(Offset); 48 } 49 50 void DwarfExpression::addFBReg(int Offset) { 51 emitOp(dwarf::DW_OP_fbreg); 52 emitSigned(Offset); 53 } 54 55 void DwarfExpression::addOpPiece(unsigned SizeInBits, unsigned OffsetInBits) { 56 if (!SizeInBits) 57 return; 58 59 const unsigned SizeOfByte = 8; 60 if (OffsetInBits > 0 || SizeInBits % SizeOfByte) { 61 emitOp(dwarf::DW_OP_bit_piece); 62 emitUnsigned(SizeInBits); 63 emitUnsigned(OffsetInBits); 64 } else { 65 emitOp(dwarf::DW_OP_piece); 66 unsigned ByteSize = SizeInBits / SizeOfByte; 67 emitUnsigned(ByteSize); 68 } 69 this->OffsetInBits += SizeInBits; 70 } 71 72 void DwarfExpression::addShr(unsigned ShiftBy) { 73 emitOp(dwarf::DW_OP_constu); 74 emitUnsigned(ShiftBy); 75 emitOp(dwarf::DW_OP_shr); 76 } 77 78 void DwarfExpression::addAnd(unsigned Mask) { 79 emitOp(dwarf::DW_OP_constu); 80 emitUnsigned(Mask); 81 emitOp(dwarf::DW_OP_and); 82 } 83 84 bool DwarfExpression::addMachineReg(const TargetRegisterInfo &TRI, 85 unsigned MachineReg, unsigned MaxSize) { 86 if (!TRI.isPhysicalRegister(MachineReg)) { 87 if (isFrameRegister(TRI, MachineReg)) { 88 DwarfRegs.push_back({-1, 0, nullptr}); 89 return true; 90 } 91 return false; 92 } 93 94 int Reg = TRI.getDwarfRegNum(MachineReg, false); 95 96 // If this is a valid register number, emit it. 97 if (Reg >= 0) { 98 DwarfRegs.push_back({Reg, 0, nullptr}); 99 return true; 100 } 101 102 // Walk up the super-register chain until we find a valid number. 103 // For example, EAX on x86_64 is a 32-bit fragment of RAX with offset 0. 104 for (MCSuperRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) { 105 Reg = TRI.getDwarfRegNum(*SR, false); 106 if (Reg >= 0) { 107 unsigned Idx = TRI.getSubRegIndex(*SR, MachineReg); 108 unsigned Size = TRI.getSubRegIdxSize(Idx); 109 unsigned RegOffset = TRI.getSubRegIdxOffset(Idx); 110 DwarfRegs.push_back({Reg, 0, "super-register"}); 111 // Use a DW_OP_bit_piece to describe the sub-register. 112 setSubRegisterPiece(Size, RegOffset); 113 return true; 114 } 115 } 116 117 // Otherwise, attempt to find a covering set of sub-register numbers. 118 // For example, Q0 on ARM is a composition of D0+D1. 119 unsigned CurPos = 0; 120 // The size of the register in bits, assuming 8 bits per byte. 121 unsigned RegSize = TRI.getMinimalPhysRegClass(MachineReg)->getSize() * 8; 122 // Keep track of the bits in the register we already emitted, so we 123 // can avoid emitting redundant aliasing subregs. 124 SmallBitVector Coverage(RegSize, false); 125 for (MCSubRegIterator SR(MachineReg, &TRI); SR.isValid(); ++SR) { 126 unsigned Idx = TRI.getSubRegIndex(MachineReg, *SR); 127 unsigned Size = TRI.getSubRegIdxSize(Idx); 128 unsigned Offset = TRI.getSubRegIdxOffset(Idx); 129 Reg = TRI.getDwarfRegNum(*SR, false); 130 131 // Intersection between the bits we already emitted and the bits 132 // covered by this subregister. 133 SmallBitVector Intersection(RegSize, false); 134 Intersection.set(Offset, Offset + Size); 135 Intersection ^= Coverage; 136 137 // If this sub-register has a DWARF number and we haven't covered 138 // its range, emit a DWARF piece for it. 139 if (Reg >= 0 && Intersection.any()) { 140 // Emit a piece for any gap in the coverage. 141 if (Offset > CurPos) 142 DwarfRegs.push_back({-1, Offset - CurPos, nullptr}); 143 DwarfRegs.push_back( 144 {Reg, std::min<unsigned>(Size, MaxSize - Offset), "sub-register"}); 145 if (Offset >= MaxSize) 146 break; 147 148 // Mark it as emitted. 149 Coverage.set(Offset, Offset + Size); 150 CurPos = Offset + Size; 151 } 152 } 153 154 return CurPos; 155 } 156 157 void DwarfExpression::addStackValue() { 158 if (DwarfVersion >= 4) 159 emitOp(dwarf::DW_OP_stack_value); 160 } 161 162 void DwarfExpression::addSignedConstant(int64_t Value) { 163 assert(LocationKind == Implicit || LocationKind == Unknown); 164 LocationKind = Implicit; 165 emitOp(dwarf::DW_OP_consts); 166 emitSigned(Value); 167 } 168 169 void DwarfExpression::addUnsignedConstant(uint64_t Value) { 170 assert(LocationKind == Implicit || LocationKind == Unknown); 171 LocationKind = Implicit; 172 emitOp(dwarf::DW_OP_constu); 173 emitUnsigned(Value); 174 } 175 176 void DwarfExpression::addUnsignedConstant(const APInt &Value) { 177 assert(LocationKind == Implicit || LocationKind == Unknown); 178 LocationKind = Implicit; 179 180 unsigned Size = Value.getBitWidth(); 181 const uint64_t *Data = Value.getRawData(); 182 183 // Chop it up into 64-bit pieces, because that's the maximum that 184 // addUnsignedConstant takes. 185 unsigned Offset = 0; 186 while (Offset < Size) { 187 addUnsignedConstant(*Data++); 188 if (Offset == 0 && Size <= 64) 189 break; 190 addStackValue(); 191 addOpPiece(std::min(Size - Offset, 64u), Offset); 192 Offset += 64; 193 } 194 } 195 196 bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI, 197 DIExpressionCursor &ExprCursor, 198 unsigned MachineReg, 199 unsigned FragmentOffsetInBits) { 200 auto Fragment = ExprCursor.getFragmentInfo(); 201 if (!addMachineReg(TRI, MachineReg, Fragment ? Fragment->SizeInBits : ~1U)) 202 return false; 203 204 bool HasComplexExpression = false; 205 auto Op = ExprCursor.peek(); 206 if (Op && Op->getOp() != dwarf::DW_OP_LLVM_fragment) 207 HasComplexExpression = true; 208 209 // If the register can only be described by a complex expression (i.e., 210 // multiple subregisters) it doesn't safely compose with another complex 211 // expression. For example, it is not possible to apply a DW_OP_deref 212 // operation to multiple DW_OP_pieces. 213 if (HasComplexExpression && DwarfRegs.size() > 1) { 214 DwarfRegs.clear(); 215 return false; 216 } 217 218 // Handle simple register locations. 219 if (LocationKind != Memory && !HasComplexExpression) { 220 for (auto &Reg : DwarfRegs) { 221 if (Reg.DwarfRegNo >= 0) 222 addReg(Reg.DwarfRegNo, Reg.Comment); 223 addOpPiece(Reg.Size); 224 } 225 DwarfRegs.clear(); 226 return true; 227 } 228 229 // Don't emit locations that cannot be expressed without DW_OP_stack_value. 230 if (DwarfVersion < 4) 231 if (std::any_of(ExprCursor.begin(), ExprCursor.end(), 232 [](DIExpression::ExprOperand Op) -> bool { 233 return Op.getOp() == dwarf::DW_OP_stack_value; 234 })) { 235 DwarfRegs.clear(); 236 return false; 237 } 238 239 assert(DwarfRegs.size() == 1); 240 auto Reg = DwarfRegs[0]; 241 bool FBReg = isFrameRegister(TRI, MachineReg); 242 int SignedOffset = 0; 243 assert(Reg.Size == 0 && "subregister has same size as superregister"); 244 245 // Pattern-match combinations for which more efficient representations exist. 246 // [Reg, Offset, DW_OP_plus] --> [DW_OP_breg, Offset]. 247 // [Reg, Offset, DW_OP_minus] --> [DW_OP_breg, -Offset]. 248 // If Reg is a subregister we need to mask it out before subtracting. 249 if (Op && ((Op->getOp() == dwarf::DW_OP_plus) || 250 (Op->getOp() == dwarf::DW_OP_minus && !SubRegisterSizeInBits))) { 251 int Offset = Op->getArg(0); 252 SignedOffset = (Op->getOp() == dwarf::DW_OP_plus) ? Offset : -Offset; 253 ExprCursor.take(); 254 } 255 if (FBReg) 256 addFBReg(SignedOffset); 257 else 258 addBReg(Reg.DwarfRegNo, SignedOffset); 259 DwarfRegs.clear(); 260 return true; 261 } 262 263 /// Assuming a well-formed expression, match "DW_OP_deref* DW_OP_LLVM_fragment?". 264 static bool isMemoryLocation(DIExpressionCursor ExprCursor) { 265 while (ExprCursor) { 266 auto Op = ExprCursor.take(); 267 switch (Op->getOp()) { 268 case dwarf::DW_OP_deref: 269 case dwarf::DW_OP_LLVM_fragment: 270 break; 271 default: 272 return false; 273 } 274 } 275 return true; 276 } 277 278 void DwarfExpression::addExpression(DIExpressionCursor &&ExprCursor, 279 unsigned FragmentOffsetInBits) { 280 // If we need to mask out a subregister, do it now, unless the next 281 // operation would emit an OpPiece anyway. 282 auto N = ExprCursor.peek(); 283 if (SubRegisterSizeInBits && N && (N->getOp() != dwarf::DW_OP_LLVM_fragment)) 284 maskSubRegister(); 285 286 while (ExprCursor) { 287 auto Op = ExprCursor.take(); 288 switch (Op->getOp()) { 289 case dwarf::DW_OP_LLVM_fragment: { 290 unsigned SizeInBits = Op->getArg(1); 291 unsigned FragmentOffset = Op->getArg(0); 292 // The fragment offset must have already been adjusted by emitting an 293 // empty DW_OP_piece / DW_OP_bit_piece before we emitted the base 294 // location. 295 assert(OffsetInBits >= FragmentOffset && "fragment offset not added?"); 296 297 // If addMachineReg already emitted DW_OP_piece operations to represent 298 // a super-register by splicing together sub-registers, subtract the size 299 // of the pieces that was already emitted. 300 SizeInBits -= OffsetInBits - FragmentOffset; 301 302 // If addMachineReg requested a DW_OP_bit_piece to stencil out a 303 // sub-register that is smaller than the current fragment's size, use it. 304 if (SubRegisterSizeInBits) 305 SizeInBits = std::min<unsigned>(SizeInBits, SubRegisterSizeInBits); 306 307 // Emit a DW_OP_stack_value for implicit location descriptions. 308 if (LocationKind == Implicit) 309 addStackValue(); 310 311 // Emit the DW_OP_piece. 312 addOpPiece(SizeInBits, SubRegisterOffsetInBits); 313 setSubRegisterPiece(0, 0); 314 // Reset the location description kind. 315 LocationKind = Unknown; 316 return; 317 } 318 case dwarf::DW_OP_plus: 319 assert(LocationKind != Register); 320 emitOp(dwarf::DW_OP_plus_uconst); 321 emitUnsigned(Op->getArg(0)); 322 break; 323 case dwarf::DW_OP_minus: 324 assert(LocationKind != Register); 325 // There is no DW_OP_minus_uconst. 326 emitOp(dwarf::DW_OP_constu); 327 emitUnsigned(Op->getArg(0)); 328 emitOp(dwarf::DW_OP_minus); 329 break; 330 case dwarf::DW_OP_deref: { 331 assert(LocationKind != Register); 332 if (LocationKind != Memory && isMemoryLocation(ExprCursor)) 333 // Turning this into a memory location description makes the deref 334 // implicit. 335 LocationKind = Memory; 336 else 337 emitOp(dwarf::DW_OP_deref); 338 break; 339 } 340 case dwarf::DW_OP_constu: 341 assert(LocationKind != Register); 342 emitOp(dwarf::DW_OP_constu); 343 emitUnsigned(Op->getArg(0)); 344 break; 345 case dwarf::DW_OP_stack_value: 346 assert(LocationKind == Unknown || LocationKind == Implicit); 347 LocationKind = Implicit; 348 break; 349 case dwarf::DW_OP_swap: 350 assert(LocationKind != Register); 351 emitOp(dwarf::DW_OP_swap); 352 break; 353 case dwarf::DW_OP_xderef: 354 assert(LocationKind != Register); 355 emitOp(dwarf::DW_OP_xderef); 356 break; 357 default: 358 llvm_unreachable("unhandled opcode found in expression"); 359 } 360 } 361 362 if (LocationKind == Implicit) 363 // Turn this into an implicit location description. 364 addStackValue(); 365 } 366 367 /// add masking operations to stencil out a subregister. 368 void DwarfExpression::maskSubRegister() { 369 assert(SubRegisterSizeInBits && "no subregister was registered"); 370 if (SubRegisterOffsetInBits > 0) 371 addShr(SubRegisterOffsetInBits); 372 uint64_t Mask = (1ULL << (uint64_t)SubRegisterSizeInBits) - 1ULL; 373 addAnd(Mask); 374 } 375 376 377 void DwarfExpression::finalize() { 378 assert(DwarfRegs.size() == 0 && "dwarf registers not emitted"); 379 // Emit any outstanding DW_OP_piece operations to mask out subregisters. 380 if (SubRegisterSizeInBits == 0) 381 return; 382 // Don't emit a DW_OP_piece for a subregister at offset 0. 383 if (SubRegisterOffsetInBits == 0) 384 return; 385 addOpPiece(SubRegisterSizeInBits, SubRegisterOffsetInBits); 386 } 387 388 void DwarfExpression::addFragmentOffset(const DIExpression *Expr) { 389 if (!Expr || !Expr->isFragment()) 390 return; 391 392 uint64_t FragmentOffset = Expr->getFragmentInfo()->OffsetInBits; 393 assert(FragmentOffset >= OffsetInBits && 394 "overlapping or duplicate fragments"); 395 if (FragmentOffset > OffsetInBits) 396 addOpPiece(FragmentOffset - OffsetInBits); 397 OffsetInBits = FragmentOffset; 398 } 399