Reapply "[SelectionDAG] Selection of DBG_VALUE using a PHI node result (pt 2)"

Summary:
This reverts SVN r331441 (reapplies r331337), together with a fix
in to handle an already existing fragment expression in the
dbg.value that must be fragmented due to a split PHI node.

This should solve the problem seen in PR37321, which was the
reason for the revert of r331337.

The situation in PR37321 is that we have a PHI node like this

   %u.sroa = phi i80 [ %u.sroa.x, %if.x ],
                     [ %u.sroa.y, %if.y ],
                     [ %u.sroa.z, %if.z ]

and a dbg.value like this

  call void @llvm.dbg.value(metadata i80 %u.sroa,
                            metadata !13,
                            metadata !DIExpression(DW_OP_LLVM_fragment, 0, 80))

The phi node is split into three 32-bit PHI nodes

  %30:gr32 = PHI %11:gr32, %bb.4, %14:gr32, %bb.5, %27:gr32, %bb.8
  %31:gr32 = PHI %12:gr32, %bb.4, %15:gr32, %bb.5, %28:gr32, %bb.8
  %32:gr32 = PHI %13:gr32, %bb.4, %16:gr32, %bb.5, %29:gr32, %bb.8

but since the original value only is 80 bits we need to adjust the size
of the last fragment expression, and with this patch we get

  DBG_VALUE debug-use %30:gr32, debug-use $noreg, !"u", !DIExpression(DW_OP_LLVM_fragment, 0, 32)
  DBG_VALUE debug-use %31:gr32, debug-use $noreg, !"u", !DIExpression(DW_OP_LLVM_fragment, 32, 32)
  DBG_VALUE debug-use %32:gr32, debug-use $noreg, !"u", !DIExpression(DW_OP_LLVM_fragment, 64, 16)

Reviewers: vsk, aprantl, mstorsjo

Reviewed By: aprantl

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D46384

llvm-svn: 331464
This commit is contained in:
Bjorn Pettersson 2018-05-03 17:04:16 +00:00
parent 52721286b2
commit 304877e5ec
4 changed files with 306 additions and 6 deletions

View File

@ -5257,12 +5257,48 @@ SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
// PHI nodes have already been selected, so we should know which VReg that
// is assigns to already.
if (isa<PHINode>(V)) {
auto It = FuncInfo.ValueMap.find(V);
if (It != FuncInfo.ValueMap.end()) {
unsigned Reg = It->second;
SDV = DAG.getVRegDbgValue(Variable, Expression, Reg, false, dl,
SDNodeOrder);
DAG.AddDbgValue(SDV, nullptr, false);
auto VMI = FuncInfo.ValueMap.find(V);
if (VMI != FuncInfo.ValueMap.end()) {
unsigned Reg = VMI->second;
// The PHI node may be split up into several MI PHI nodes (in
// FunctionLoweringInfo::set).
RegsForValue RFV(V->getContext(), TLI, DAG.getDataLayout(), Reg,
V->getType(), false);
if (RFV.occupiesMultipleRegs()) {
unsigned I = 0;
unsigned Offset = 0;
unsigned BitsToDescribe = 0;
if (auto VarSize = Variable->getSizeInBits())
BitsToDescribe = *VarSize;
if (auto Fragment = Expression->getFragmentInfo())
BitsToDescribe = Fragment->SizeInBits;
for (auto CountAndVT : zip_first(RFV.RegCount, RFV.RegVTs)) {
unsigned RegCount = std::get<0>(CountAndVT);
MVT RegisterVT = std::get<1>(CountAndVT);
unsigned RegisterSize = RegisterVT.getSizeInBits();
for (unsigned E = I + RegCount; I != E; ++I) {
// Bail out if all bits already are described.
if (Offset >= BitsToDescribe)
break;
unsigned FragmentSize = (Offset + RegisterSize > BitsToDescribe)
? BitsToDescribe - Offset
: RegisterSize;
auto FragmentExpr = DIExpression::createFragmentExpression(
Expression, Offset, FragmentSize);
if (!FragmentExpr)
continue;
// The vregs are guaranteed to be allocated in sequence.
SDV = DAG.getVRegDbgValue(Variable, *FragmentExpr, Reg + I,
false, dl, SDNodeOrder);
DAG.AddDbgValue(SDV, nullptr, false);
Offset += RegisterSize;
}
}
} else {
SDV = DAG.getVRegDbgValue(Variable, Expression, Reg, false, dl,
SDNodeOrder);
DAG.AddDbgValue(SDV, nullptr, false);
}
return nullptr;
}
}

View File

@ -1048,6 +1048,11 @@ struct RegsForValue {
void AddInlineAsmOperands(unsigned Kind, bool HasMatching,
unsigned MatchingIdx, const SDLoc &dl,
SelectionDAG &DAG, std::vector<SDValue> &Ops) const;
/// Check if the total RegCount is greater than one.
bool occupiesMultipleRegs() const {
return std::accumulate(RegCount.begin(), RegCount.end(), 0) > 1;
}
};
} // end namespace llvm

View File

@ -0,0 +1,169 @@
; RUN: llc -start-after=codegenprepare -stop-before expand-isel-pseudos -o - %s | FileCheck %s
; This test case was generated from the following phi-split.c program,
; using: clang phi-split.c -g -O1 -S -o - --target=i386 -emit-llvm
; --------------------------------------
; long long end = 10;
;
; int main() {
; long long x = 9;
; long long y = 13;
; for (long long u = 0; u < end; ++u) {
; x += y;
; y = y * 3;
; }
;
; volatile long long arr[80];
; for (long long q = 0; q < 64; ++q) {
; arr[q] = q + 3;
; }
;
; return x;
; }
; --------------------------------------
;
; ModuleID = 'phi-split.c'
source_filename = "phi-split.c"
target datalayout = "e-m:e-p:32:32-f64:32:64-f80:32-n8:16:32-S128"
target triple = "i386"
@end = dso_local local_unnamed_addr global i64 10, align 8, !dbg !0
; Function Attrs: nounwind
define dso_local i32 @main() local_unnamed_addr #0 !dbg !12 {
; CHECK-LABEL: name: main
entry:
%arr = alloca [80 x i64], align 8
call void @llvm.dbg.value(metadata i64 9, metadata !17, metadata !DIExpression()), !dbg !28
call void @llvm.dbg.value(metadata i64 13, metadata !18, metadata !DIExpression()), !dbg !29
call void @llvm.dbg.value(metadata i64 0, metadata !19, metadata !DIExpression()), !dbg !30
%0 = load i64, i64* @end, align 8, !dbg !31
%cmp20 = icmp sgt i64 %0, 0, !dbg !37
br i1 %cmp20, label %for.body.lr.ph, label %for.cond.cleanup, !dbg !38
for.body.lr.ph: ; preds = %entry
%1 = load i64, i64* @end, align 8
br label %for.body, !dbg !38
for.cond.cleanup.loopexit: ; preds = %for.body
%extract.t = trunc i64 %add to i32, !dbg !38
br label %for.cond.cleanup, !dbg !39
for.cond.cleanup: ; preds = %for.cond.cleanup.loopexit, %entry
%x.0.lcssa.off0 = phi i32 [ 9, %entry ], [ %extract.t, %for.cond.cleanup.loopexit ]
call void @llvm.dbg.value(metadata i64 undef, metadata !17, metadata !DIExpression()), !dbg !28
%2 = bitcast [80 x i64]* %arr to i8*, !dbg !39
call void @llvm.dbg.value(metadata i64 0, metadata !26, metadata !DIExpression()), !dbg !41
br label %for.body4, !dbg !42
for.body: ; preds = %for.body.lr.ph, %for.body
; CHECK-LABEL: bb.{{.*}}.for.body:
; CHECK: [[REG2:%[0-9]+]]:gr32 = PHI
; CHECK-NEXT: [[REG3:%[0-9]+]]:gr32 = PHI
; CHECK-NEXT: [[REG4:%[0-9]+]]:gr32 = PHI
; CHECK-NEXT: [[REG5:%[0-9]+]]:gr32_nosp = PHI
; CHECK-NEXT: [[REG6:%[0-9]+]]:gr32 = PHI
; CHECK-NEXT: [[REG7:%[0-9]+]]:gr32 = PHI
; CHECK-NEXT: DBG_VALUE debug-use [[REG2]], debug-use $noreg, !19, !DIExpression(DW_OP_LLVM_fragment, 0, 32)
; CHECK-NEXT: DBG_VALUE debug-use [[REG3]], debug-use $noreg, !19, !DIExpression(DW_OP_LLVM_fragment, 32, 32)
; CHECK-NEXT: DBG_VALUE debug-use [[REG4]], debug-use $noreg, !18, !DIExpression(DW_OP_LLVM_fragment, 0, 32)
; CHECK-NEXT: DBG_VALUE debug-use [[REG5]], debug-use $noreg, !18, !DIExpression(DW_OP_LLVM_fragment, 32, 32)
; CHECK-NEXT: DBG_VALUE debug-use [[REG6]], debug-use $noreg, !17, !DIExpression(DW_OP_LLVM_fragment, 0, 32)
; CHECK-NEXT: DBG_VALUE debug-use [[REG7]], debug-use $noreg, !17, !DIExpression(DW_OP_LLVM_fragment, 32, 32)
%u.023 = phi i64 [ 0, %for.body.lr.ph ], [ %inc, %for.body ]
%y.022 = phi i64 [ 13, %for.body.lr.ph ], [ %mul, %for.body ]
%x.021 = phi i64 [ 9, %for.body.lr.ph ], [ %add, %for.body ]
call void @llvm.dbg.value(metadata i64 %u.023, metadata !19, metadata !DIExpression()), !dbg !30
call void @llvm.dbg.value(metadata i64 %y.022, metadata !18, metadata !DIExpression()), !dbg !29
call void @llvm.dbg.value(metadata i64 %x.021, metadata !17, metadata !DIExpression()), !dbg !28
%add = add nuw nsw i64 %y.022, %x.021, !dbg !43
%mul = mul nsw i64 %y.022, 3, !dbg !45
%inc = add nuw nsw i64 %u.023, 1, !dbg !46
call void @llvm.dbg.value(metadata i64 %inc, metadata !19, metadata !DIExpression()), !dbg !30
call void @llvm.dbg.value(metadata i64 %mul, metadata !18, metadata !DIExpression()), !dbg !29
call void @llvm.dbg.value(metadata i64 %add, metadata !17, metadata !DIExpression()), !dbg !28
%cmp = icmp slt i64 %inc, %1, !dbg !37
br i1 %cmp, label %for.body, label %for.cond.cleanup.loopexit, !dbg !38, !llvm.loop !47
for.cond.cleanup3: ; preds = %for.body4
ret i32 %x.0.lcssa.off0, !dbg !50
for.body4: ; preds = %for.cond.cleanup, %for.body4
%q.019 = phi i64 [ 0, %for.cond.cleanup ], [ %inc7, %for.body4 ]
call void @llvm.dbg.value(metadata i64 %q.019, metadata !26, metadata !DIExpression()), !dbg !41
%add5 = add nuw nsw i64 %q.019, 3, !dbg !51
%idxprom = trunc i64 %q.019 to i32, !dbg !54
%arrayidx = getelementptr inbounds [80 x i64], [80 x i64]* %arr, i32 0, i32 %idxprom, !dbg !54
store volatile i64 %add5, i64* %arrayidx, align 8, !dbg !55
%inc7 = add nuw nsw i64 %q.019, 1, !dbg !56
call void @llvm.dbg.value(metadata i64 %inc7, metadata !26, metadata !DIExpression()), !dbg !41
%cmp2 = icmp ult i64 %inc7, 64, !dbg !57
br i1 %cmp2, label %for.body4, label %for.cond.cleanup3, !dbg !42, !llvm.loop !58
}
; Function Attrs: nounwind readnone speculatable
declare void @llvm.dbg.value(metadata, metadata, metadata) #1
attributes #0 = { nounwind }
attributes #1 = { nounwind readnone speculatable }
!llvm.dbg.cu = !{!2}
!llvm.module.flags = !{!7, !8, !9, !10}
!llvm.ident = !{!11}
!0 = !DIGlobalVariableExpression(var: !1, expr: !DIExpression())
!1 = distinct !DIGlobalVariable(name: "end", scope: !2, file: !3, line: 1, type: !6, isLocal: false, isDefinition: true)
!2 = distinct !DICompileUnit(language: DW_LANG_C99, file: !3, producer: "clang version 7.0.0 (x)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5)
!3 = !DIFile(filename: "phi-split.c", directory: "")
!4 = !{}
!5 = !{!0}
!6 = !DIBasicType(name: "long long int", size: 64, encoding: DW_ATE_signed)
!7 = !{i32 1, !"NumRegisterParameters", i32 0}
!8 = !{i32 2, !"Dwarf Version", i32 4}
!9 = !{i32 2, !"Debug Info Version", i32 3}
!10 = !{i32 1, !"wchar_size", i32 4}
!11 = !{!"clang version 7.0.0 (x)"}
!12 = distinct !DISubprogram(name: "main", scope: !3, file: !3, line: 3, type: !13, isLocal: false, isDefinition: true, scopeLine: 3, isOptimized: true, unit: !2, variables: !16)
!13 = !DISubroutineType(types: !14)
!14 = !{!15}
!15 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!16 = !{!17, !18, !19, !21, !26}
!17 = !DILocalVariable(name: "x", scope: !12, file: !3, line: 4, type: !6)
!18 = !DILocalVariable(name: "y", scope: !12, file: !3, line: 5, type: !6)
!19 = !DILocalVariable(name: "u", scope: !20, file: !3, line: 6, type: !6)
!20 = distinct !DILexicalBlock(scope: !12, file: !3, line: 6, column: 3)
!21 = !DILocalVariable(name: "arr", scope: !12, file: !3, line: 11, type: !22)
!22 = !DICompositeType(tag: DW_TAG_array_type, baseType: !23, size: 5120, elements: !24)
!23 = !DIDerivedType(tag: DW_TAG_volatile_type, baseType: !6)
!24 = !{!25}
!25 = !DISubrange(count: 80)
!26 = !DILocalVariable(name: "q", scope: !27, file: !3, line: 12, type: !6)
!27 = distinct !DILexicalBlock(scope: !12, file: !3, line: 12, column: 3)
!28 = !DILocation(line: 4, column: 13, scope: !12)
!29 = !DILocation(line: 5, column: 13, scope: !12)
!30 = !DILocation(line: 6, column: 18, scope: !20)
!31 = !DILocation(line: 6, column: 29, scope: !32)
!32 = distinct !DILexicalBlock(scope: !20, file: !3, line: 6, column: 3)
!37 = !DILocation(line: 6, column: 27, scope: !32)
!38 = !DILocation(line: 6, column: 3, scope: !20)
!39 = !DILocation(line: 11, column: 3, scope: !12)
!40 = !DILocation(line: 11, column: 22, scope: !12)
!41 = !DILocation(line: 12, column: 18, scope: !27)
!42 = !DILocation(line: 12, column: 3, scope: !27)
!43 = !DILocation(line: 7, column: 7, scope: !44)
!44 = distinct !DILexicalBlock(scope: !32, file: !3, line: 6, column: 39)
!45 = !DILocation(line: 8, column: 11, scope: !44)
!46 = !DILocation(line: 6, column: 34, scope: !32)
!47 = distinct !{!47, !38, !48}
!48 = !DILocation(line: 9, column: 3, scope: !20)
!50 = !DILocation(line: 16, column: 3, scope: !12)
!51 = !DILocation(line: 13, column: 16, scope: !52)
!52 = distinct !DILexicalBlock(scope: !53, file: !3, line: 12, column: 38)
!53 = distinct !DILexicalBlock(scope: !27, file: !3, line: 12, column: 3)
!54 = !DILocation(line: 13, column: 5, scope: !52)
!55 = !DILocation(line: 13, column: 12, scope: !52)
!56 = !DILocation(line: 12, column: 33, scope: !53)
!57 = !DILocation(line: 12, column: 27, scope: !53)
!58 = distinct !{!58, !42, !59}
!59 = !DILocation(line: 14, column: 3, scope: !27)

View File

@ -0,0 +1,90 @@
; RUN: llc -start-after=codegenprepare -stop-before expand-isel-pseudos -o - %s | FileCheck %s
; This is a reproducer based on the test case from PR37321.
; We verify that the fragment for the last DBG_VALUE is limited depending
; on the size of the original fragment (and that we do not emit more
; DBG_VALUE instructions than needed in case we cover the whole original
; fragment with just a few DBG_VALUE instructions).
; CHECK-LABEL: bb.{{.*}}.if.end36:
; CHECK: [[REG1:%[0-9]+]]:gr32 = PHI
; CHECK-NEXT: [[REG2:%[0-9]+]]:gr32 = PHI
; CHECK-NEXT: [[REG3:%[0-9]+]]:gr32 = PHI
; CHECK-NEXT: DBG_VALUE debug-use [[REG1]], debug-use $noreg, !13, !DIExpression(DW_OP_LLVM_fragment, 0, 32)
; CHECK-NEXT: DBG_VALUE debug-use [[REG2]], debug-use $noreg, !13, !DIExpression(DW_OP_LLVM_fragment, 32, 32)
; CHECK-NEXT: DBG_VALUE debug-use [[REG3]], debug-use $noreg, !13, !DIExpression(DW_OP_LLVM_fragment, 64, 16)
; CHECK-NEXT: DBG_VALUE debug-use [[REG1]], debug-use $noreg, !12, !DIExpression(DW_OP_LLVM_fragment, 10, 32)
; CHECK-NEXT: DBG_VALUE debug-use [[REG2]], debug-use $noreg, !12, !DIExpression(DW_OP_LLVM_fragment, 42, 13)
; CHECK-NOT: DBG_VALUE
target datalayout = "e-m:x-p:32:32-i64:64-f80:32-n8:16:32-a:0:32-S32"
target triple = "i686-w64-windows-gnu"
; Function Attrs: nounwind readnone
define dso_local i64 @nextafterl(i80 %a) local_unnamed_addr #0 !dbg !6 {
entry:
br i1 undef, label %if.else, label %if.then13, !dbg !28
if.then13: ; preds = %entry
%u.sroa.0.8.insert.insert = or i80 %a, 2222, !dbg !29
br label %if.end36, !dbg !33
if.else: ; preds = %entry
br label %if.end36
if.end36: ; preds = %if.else, %if.then13
%u.sroa.0.1.in = phi i80 [ %u.sroa.0.8.insert.insert, %if.then13 ], [ 1234567, %if.else ]
call void @llvm.dbg.value(metadata i80 %u.sroa.0.1.in, metadata !13, metadata !DIExpression(DW_OP_LLVM_fragment, 0, 80)), !dbg !34
call void @llvm.dbg.value(metadata i80 %u.sroa.0.1.in, metadata !12, metadata !DIExpression(DW_OP_LLVM_fragment, 10, 45)), !dbg !34
%u.sroa.0.0.extract.ashr = ashr i80 %u.sroa.0.1.in, 8, !dbg !35
%u.sroa.0.0.extract.trunc = trunc i80 %u.sroa.0.0.extract.ashr to i64, !dbg !35
ret i64 %u.sroa.0.0.extract.trunc
}
; Function Attrs: nounwind readnone speculatable
declare void @llvm.dbg.value(metadata, metadata, metadata) #1
attributes #0 = { nounwind readnone }
attributes #1 = { nounwind readnone speculatable }
!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!26, !27}
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 7.0.0 (trunk 330808) (llvm/trunk 330813)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3)
!1 = !DIFile(filename: "pr37321.c", directory: "")
!2 = !{}
!3 = !{!4}
!4 = !DIGlobalVariableExpression(var: !5, expr: !DIExpression())
!5 = distinct !DIGlobalVariable(name: "normal_bit", scope: !6, file: !1, line: 31, type: !25, isLocal: true, isDefinition: true)
!6 = distinct !DISubprogram(name: "nextafterl", scope: !1, file: !1, line: 17, type: !7, isLocal: false, isDefinition: true, scopeLine: 18, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !10)
!7 = !DISubroutineType(types: !8)
!8 = !{!9, !9, !9}
!9 = !DIBasicType(name: "long double", size: 96, encoding: DW_ATE_float)
!10 = !{!11, !12, !13}
!11 = !DILocalVariable(name: "x", arg: 1, scope: !6, file: !1, line: 17, type: !9)
!12 = !DILocalVariable(name: "y", arg: 2, scope: !6, file: !1, line: 17, type: !9)
!13 = !DILocalVariable(name: "u", scope: !6, file: !1, line: 27, type: !14)
!14 = distinct !DICompositeType(tag: DW_TAG_union_type, scope: !6, file: !1, line: 19, size: 128, elements: !15)
!15 = !{!16, !17}
!16 = !DIDerivedType(tag: DW_TAG_member, name: "ld", scope: !14, file: !1, line: 20, baseType: !9, size: 96)
!17 = !DIDerivedType(tag: DW_TAG_member, name: "parts", scope: !14, file: !1, line: 26, baseType: !18, size: 128)
!18 = distinct !DICompositeType(tag: DW_TAG_structure_type, scope: !14, file: !1, line: 21, size: 128, elements: !19)
!19 = !{!20, !22, !24}
!20 = !DIDerivedType(tag: DW_TAG_member, name: "mantissa", scope: !18, file: !1, line: 23, baseType: !21, size: 64)
!21 = !DIBasicType(name: "long long unsigned int", size: 64, encoding: DW_ATE_unsigned)
!22 = !DIDerivedType(tag: DW_TAG_member, name: "expn", scope: !18, file: !1, line: 24, baseType: !23, size: 16, offset: 64)
!23 = !DIBasicType(name: "unsigned short", size: 16, encoding: DW_ATE_unsigned)
!24 = !DIDerivedType(tag: DW_TAG_member, name: "pad", scope: !18, file: !1, line: 25, baseType: !23, size: 16, offset: 80)
!25 = !DIDerivedType(tag: DW_TAG_const_type, baseType: !21)
!26 = !{i32 2, !"Debug Info Version", i32 3}
!27 = !{i32 1, !"wchar_size", i32 2}
!28 = !DILocation(line: 47, column: 7, scope: !6)
!29 = !DILocation(line: 51, column: 14, scope: !30)
!30 = distinct !DILexicalBlock(scope: !31, file: !1, line: 50, column: 11)
!31 = distinct !DILexicalBlock(scope: !32, file: !1, line: 48, column: 5)
!32 = distinct !DILexicalBlock(scope: !6, file: !1, line: 47, column: 7)
!33 = !DILocation(line: 51, column: 2, scope: !30)
!34 = !DILocation(line: 27, column: 5, scope: !6)
!35 = !DILocation(line: 63, column: 22, scope: !36)
!36 = distinct !DILexicalBlock(scope: !6, file: !1, line: 62, column: 7)