Skip to content

Commit 311d113

Browse files
authored
Do not emit right shift by 0 in DWARF expressions (#162738)
DW_OP_LLVM_extract_bits_zext and DW_OP_LLVM_extract_bits_sext can end up emitting "DW_OP_constu 0; DW_OP_shr" when given certain arguments. However, a shift by zero is not useful, and so it can be omitted.
1 parent 9922d8d commit 311d113

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,12 @@ bool DwarfExpression::addExpression(
587587
emitUnsigned(LeftShift);
588588
emitOp(dwarf::DW_OP_shl);
589589
}
590-
emitOp(dwarf::DW_OP_constu);
591-
emitUnsigned(RightShift);
592-
emitOp(OpNum == dwarf::DW_OP_LLVM_extract_bits_sext ? dwarf::DW_OP_shra
593-
: dwarf::DW_OP_shr);
590+
if (RightShift) {
591+
emitOp(dwarf::DW_OP_constu);
592+
emitUnsigned(RightShift);
593+
emitOp(OpNum == dwarf::DW_OP_LLVM_extract_bits_sext ? dwarf::DW_OP_shra
594+
: dwarf::DW_OP_shr);
595+
}
594596

595597
// The value is now at the top of the stack, so set the location to
596598
// implicit so that we get a stack_value at the end.

llvm/test/DebugInfo/X86/DW_OP_LLVM_extract_bits.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,16 @@ entry:
6767
; CHECK: DW_TAG_variable
6868
; CHECK: DW_AT_location (DW_OP_fbreg -4, DW_OP_plus_uconst 0x3, DW_OP_deref_size 0x1, DW_OP_constu 0x38, DW_OP_shl, DW_OP_constu 0x39, DW_OP_shr, DW_OP_stack_value)
6969
; CHECK: DW_AT_name ("z")
70+
; CHECK: DW_AT_location (DW_OP_fbreg -4, DW_OP_deref_size 0x8, DW_OP_stack_value)
71+
; CHECK: DW_AT_name ("q")
7072

7173
define i32 @test4() !dbg !28 {
7274
entry:
7375
%0 = alloca i32, align 4
7476
tail call void @llvm.dbg.declare(metadata ptr %0, metadata !29, metadata !DIExpression(DW_OP_LLVM_extract_bits_zext, 31, 1)), !dbg !30
7577
tail call void @llvm.dbg.declare(metadata ptr %0, metadata !31, metadata !DIExpression(DW_OP_LLVM_extract_bits_sext, 1, 31)), !dbg !30
7678
tail call void @llvm.dbg.declare(metadata ptr %0, metadata !32, metadata !DIExpression(DW_OP_plus_uconst, 3, DW_OP_LLVM_extract_bits_zext, 1, 7)), !dbg !30
79+
tail call void @llvm.dbg.declare(metadata ptr %0, metadata !33, metadata !DIExpression(DW_OP_LLVM_extract_bits_zext, 0, 64)), !dbg !30
7780
ret i32 0, !dbg !30
7881
}
7982

@@ -116,3 +119,5 @@ declare void @llvm.dbg.value(metadata, metadata, metadata)
116119
!30 = !DILocation(line: 0, scope: !28)
117120
!31 = !DILocalVariable(name: "y", scope: !28, file: !3, type: !19)
118121
!32 = !DILocalVariable(name: "z", scope: !28, file: !3, type: !9)
122+
!33 = !DILocalVariable(name: "q", scope: !28, file: !3, type: !34)
123+
!34 = !DIBasicType(name: "uint64_t", size: 64, encoding: DW_ATE_unsigned)

0 commit comments

Comments
 (0)