Skip to content

Commit 2896a07

Browse files
committed
Zero-extend 1-bit constantInt operands of the DbgValue
1 parent edd36e9 commit 2896a07

File tree

8 files changed

+30
-44
lines changed

8 files changed

+30
-44
lines changed

llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,6 @@ void DwarfCompileUnit::addLocationAttribute(
247247
DIELoc *Loc = nullptr;
248248
std::optional<unsigned> NVPTXAddressSpace;
249249
std::unique_ptr<DIEDwarfExpression> DwarfExpr;
250-
251-
// Check if this variable is of boolean type
252-
bool isBoolean = false;
253-
if (GV && GV->getType())
254-
if (auto *BasicType = dyn_cast<DIBasicType>(GV->getType()))
255-
isBoolean = BasicType->getEncoding() == dwarf::DW_ATE_boolean;
256-
257250
for (const auto &GE : GlobalExprs) {
258251
const GlobalVariable *Global = GE.Var;
259252
const DIExpression *Expr = GE.Expr;
@@ -264,17 +257,11 @@ void DwarfCompileUnit::addLocationAttribute(
264257
// DW_AT_const_value(X).
265258
if (GlobalExprs.size() == 1 && Expr && Expr->isConstant()) {
266259
addToAccelTable = true;
267-
268-
// Determine the value to use, normalizing booleans to 0 or 1
269-
int64_t valueToUse = Expr->getElement(1);
270-
if (isBoolean)
271-
valueToUse = valueToUse ? 1 : 0;
272-
273260
addConstantValue(
274261
*VariableDIE,
275262
DIExpression::SignedOrUnsignedConstant::UnsignedConstant ==
276263
*Expr->isConstant(),
277-
valueToUse);
264+
Expr->getElement(1));
278265
break;
279266
}
280267

@@ -835,22 +822,6 @@ void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
835822
}
836823
if (!DVal->isVariadic()) {
837824
const DbgValueLocEntry *Entry = DVal->getLocEntries().begin();
838-
839-
// Helper function to handle boolean constant values with type safety
840-
auto addConstantValueWithBooleanNormalization =
841-
[&](DIE &VariableDie, uint64_t intValue, const DIType *Type) {
842-
if (auto *BasicType = dyn_cast_or_null<DIBasicType>(Type)) {
843-
if (BasicType->getEncoding() == dwarf::DW_ATE_boolean) {
844-
// Normalize boolean values: any non-zero becomes 1, zero stays 0
845-
uint64_t normalizedBoolValue = (intValue) ? 1 : 0;
846-
addConstantValue(VariableDie, normalizedBoolValue, Type);
847-
return;
848-
}
849-
}
850-
// For non-boolean types, use the original constant value
851-
addConstantValue(VariableDie, intValue, Type);
852-
};
853-
854825
if (Entry->isLocation()) {
855826
addVariableAddress(DV, VariableDie, Entry->getLoc());
856827
} else if (Entry->isInt()) {
@@ -867,8 +838,7 @@ void DwarfCompileUnit::applyConcreteDbgVariableAttributes(
867838
addUInt(VariableDie, dwarf::DW_AT_LLVM_tag_offset,
868839
dwarf::DW_FORM_data1, *DwarfExpr.TagOffset);
869840
} else
870-
addConstantValueWithBooleanNormalization(VariableDie, Entry->getInt(),
871-
DV.getType());
841+
addConstantValue(VariableDie, Entry->getInt(), DV.getType());
872842
} else if (Entry->isConstantFP()) {
873843
addConstantFPValue(VariableDie, Entry->getConstantFP());
874844
} else if (Entry->isConstantInt()) {

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3110,8 +3110,10 @@ void DwarfDebug::emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
31103110
&AP](const DbgValueLocEntry &Entry,
31113111
DIExpressionCursor &Cursor) -> bool {
31123112
if (Entry.isInt()) {
3113-
if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
3114-
BT->getEncoding() == dwarf::DW_ATE_signed_char))
3113+
if (BT && (BT->getEncoding() == dwarf::DW_ATE_boolean))
3114+
DwarfExpr.addBooleanConstant(Entry.getInt());
3115+
else if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
3116+
BT->getEncoding() == dwarf::DW_ATE_signed_char))
31153117
DwarfExpr.addSignedConstant(Entry.getInt());
31163118
else
31173119
DwarfExpr.addUnsignedConstant(Entry.getInt());

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ void DwarfExpression::addStackValue() {
194194
emitOp(dwarf::DW_OP_stack_value);
195195
}
196196

197+
void DwarfExpression::addBooleanConstant(int64_t Value) {
198+
assert(isImplicitLocation() || isUnknownLocation());
199+
LocationKind = Implicit;
200+
if (Value == 0)
201+
emitOp(dwarf::DW_OP_lit0);
202+
else
203+
emitOp(dwarf::DW_OP_lit1);
204+
}
205+
197206
void DwarfExpression::addSignedConstant(int64_t Value) {
198207
assert(isImplicitLocation() || isUnknownLocation());
199208
LocationKind = Implicit;

llvm/lib/CodeGen/AsmPrinter/DwarfExpression.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@ class DwarfExpression {
229229
/// This needs to be called last to commit any pending changes.
230230
void finalize();
231231

232+
/// Emit a boolean constant.
233+
void addBooleanConstant(int64_t Value);
234+
232235
/// Emit a signed constant.
233236
void addSignedConstant(int64_t Value);
234237

llvm/lib/CodeGen/GlobalISel/MachineIRBuilder.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ MachineInstrBuilder MachineIRBuilder::buildConstDbgValue(const Constant &C,
109109
if (auto *CI = dyn_cast<ConstantInt>(NumericConstant)) {
110110
if (CI->getBitWidth() > 64)
111111
MIB.addCImm(CI);
112-
else
112+
else if (CI->getBitWidth() == 1)
113113
MIB.addImm(CI->getZExtValue());
114+
else
115+
MIB.addImm(CI->getSExtValue());
114116
} else if (auto *CFP = dyn_cast<ConstantFP>(NumericConstant)) {
115117
MIB.addFPImm(CFP);
116118
} else if (isa<ConstantPointerNull>(NumericConstant)) {

llvm/lib/CodeGen/SelectionDAG/InstrEmitter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,8 @@ MachineOperand GetMOForConstDbgOp(const SDDbgOperand &Op) {
733733
if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) {
734734
if (CI->getBitWidth() > 64)
735735
return MachineOperand::CreateCImm(CI);
736+
if (CI->getBitWidth() == 1)
737+
return MachineOperand::CreateImm(CI->getZExtValue());
736738
return MachineOperand::CreateImm(CI->getSExtValue());
737739
}
738740
if (const ConstantFP *CF = dyn_cast<ConstantFP>(V))

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3387,7 +3387,11 @@ DIExpression *llvm::getExpressionForConstant(DIBuilder &DIB, const Constant &C,
33873387
// Create integer constant expression.
33883388
auto createIntegerExpression = [&DIB](const Constant &CV) -> DIExpression * {
33893389
const APInt &API = cast<ConstantInt>(&CV)->getValue();
3390-
std::optional<int64_t> InitIntOpt = API.trySExtValue();
3390+
std::optional<int64_t> InitIntOpt;
3391+
if (API.getBitWidth() == 1)
3392+
InitIntOpt = API.tryZExtValue();
3393+
else
3394+
InitIntOpt = API.trySExtValue();
33913395
return InitIntOpt ? DIB.createConstantValueExpression(
33923396
static_cast<uint64_t>(*InitIntOpt))
33933397
: nullptr;

llvm/test/DebugInfo/debug-bool-const-value.ll

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
; REQUIRES: object-emission
22
; RUN: %llc_dwarf %s -filetype=obj -o - | llvm-dwarfdump - | FileCheck %s
33

4-
5-
; CHECK: {{.*}}DW_TAG_variable
6-
; CHECK-NEXT: {{.*}} DW_AT_name ("global_bool_const")
7-
; CHECK: {{.*}} DW_AT_const_value (1)
84
; CHECK: {{.*}}DW_TAG_variable
95
; CHECK-NEXT: {{.*}} DW_AT_const_value (1)
106
; CHECK-NEXT: {{.*}} DW_AT_name ("arg")
@@ -20,9 +16,9 @@ declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
2016

2117
!llvm.dbg.cu = !{ !2 }
2218
!llvm.module.flags = !{ !9, !10 }
23-
!0 = !{ !11 }
19+
2420
!1 = !DIFile(directory: "", filename: "test")
25-
!2 = distinct !DICompileUnit(emissionKind: FullDebug, file: !1, isOptimized: false, language: DW_LANG_C_plus_plus, runtimeVersion: 0, globals: !0)
21+
!2 = distinct !DICompileUnit(emissionKind: FullDebug, file: !1, isOptimized: false, language: DW_LANG_C_plus_plus, runtimeVersion: 0)
2622
!3 = !DIBasicType(encoding: DW_ATE_boolean, name: "bool", size: 8)
2723
!4 = !DISubroutineType(types: !{null})
2824
!5 = distinct !DISubprogram(file: !1, isDefinition: true, isLocal: false, isOptimized: false, line: 5, linkageName: "test", name: "test", scope: !1, scopeLine: 5, type: !4, unit: !2)
@@ -31,5 +27,3 @@ declare void @"llvm.dbg.value"(metadata %".1", metadata %".2", metadata %".3")
3127
!8 = !DIExpression()
3228
!9 = !{ i32 2, !"Dwarf Version", i32 4 }
3329
!10 = !{ i32 2, !"Debug Info Version", i32 3 }
34-
!11 = !DIGlobalVariableExpression(var: !12, expr: !DIExpression(DW_OP_consts, 18446744073709551615, DW_OP_stack_value))
35-
!12 = distinct !DIGlobalVariable(name: "global_bool_const", scope: !2, file: !1, line: 1, type: !3, isLocal: false, isDefinition: true)

0 commit comments

Comments
 (0)