Merge remote-tracking branch 'origin/swift-4.1-branch' into stable
diff --git a/lib/ProfileData/Coverage/CoverageMapping.cpp b/lib/ProfileData/Coverage/CoverageMapping.cpp
index 5542cc8..9df37a4 100644
--- a/lib/ProfileData/Coverage/CoverageMapping.cpp
+++ b/lib/ProfileData/Coverage/CoverageMapping.cpp
@@ -697,12 +697,12 @@
   if (!Mapped)
     return;
 
-  // Pick the max count from the non-gap, region entry segments. If there
-  // aren't any, use the wrapped count.
-  if (!MinRegionCount) {
+  // Pick the max count from the non-gap, region entry segments and the
+  // wrapped count.
+  if (WrappedSegment)
     ExecutionCount = WrappedSegment->Count;
+  if (!MinRegionCount)
     return;
-  }
   for (const auto *LS : LineSegments)
     if (isStartOfRegion(LS))
       ExecutionCount = std::max(ExecutionCount, LS->Count);
diff --git a/test/tools/llvm-cov/Inputs/deferred-regions.covmapping b/test/tools/llvm-cov/Inputs/deferred-regions.covmapping
index d16db88..750dd81 100644
--- a/test/tools/llvm-cov/Inputs/deferred-regions.covmapping
+++ b/test/tools/llvm-cov/Inputs/deferred-regions.covmapping
Binary files differ
diff --git a/test/tools/llvm-cov/Inputs/highlightedRanges.covmapping b/test/tools/llvm-cov/Inputs/highlightedRanges.covmapping
index 5c1b1d6..5c3d67f 100644
--- a/test/tools/llvm-cov/Inputs/highlightedRanges.covmapping
+++ b/test/tools/llvm-cov/Inputs/highlightedRanges.covmapping
Binary files differ
diff --git a/test/tools/llvm-cov/Inputs/highlightedRanges.json b/test/tools/llvm-cov/Inputs/highlightedRanges.json
index 2948e86..cff6ab3 100644
--- a/test/tools/llvm-cov/Inputs/highlightedRanges.json
+++ b/test/tools/llvm-cov/Inputs/highlightedRanges.json
@@ -12,9 +12,9 @@
 
 // Verify the Summary Section for the first file
 // CHECK-SAME: "summary":{
-// CHECK-SAME: "lines":{"count":40,"covered":27,"percent":67},
+// CHECK-SAME: "lines":{"count":40,"covered":33,"percent":82},
 // CHECK-SAME: "functions":{"count":4,"covered":4,"percent":100},
-// CHECK-SAME: "regions":{"count":19,"covered":11,"notcovered":8,"percent":57}}}
+// CHECK-SAME: "regions":{"count":21,"covered":13,"notcovered":8,"percent":61}}}
 
 // Close Files Array
 // CHECK-SAME: ],
@@ -45,10 +45,7 @@
 
 // Full Export Summary
 // CHECK-SAME: "totals":{
-// CHECK-SAME: "lines":{"count":40,"covered":27,"percent":67},
+// CHECK-SAME: "lines":{"count":40,"covered":33,"percent":82},
 // CHECK-SAME: "functions":{"count":4,"covered":4,"percent":100},
 // CHECK-SAME: "instantiations":{"count":4,"covered":4,"percent":100},
-// CHECK-SAME: "regions":{"count":19,"covered":11,"notcovered":8,"percent":57}}
-
-// Close the export object, data array, and root object
-// CHECK-SAME: }]}
+// CHECK-SAME: "regions":{"count":21,"covered":13,"notcovered":8,"percent":61}}}
diff --git a/test/tools/llvm-cov/Inputs/instrprof-comdat.h b/test/tools/llvm-cov/Inputs/instrprof-comdat.h
index 5a6d72b..07941f9 100644
--- a/test/tools/llvm-cov/Inputs/instrprof-comdat.h
+++ b/test/tools/llvm-cov/Inputs/instrprof-comdat.h
@@ -12,7 +12,7 @@
   for (T I = 0; I < ti; I++) {            // HEADER: [[@LINE]]| 22|  for (T
     t += I;                               // HEADER: [[@LINE]]| 20|   t += I;
     if (I > ti / 2)                       // HEADER: [[@LINE]]| 20|   if (I > ti
-      t -= 1;                             // HEADER: [[@LINE]]|  8|     t -= 1;
+      t -= 1;                             // HEADER: [[@LINE]]| 20|     t -= 1;
   }                                       // HEADER: [[@LINE]]| 20| }
                                           // HEADER: [[@LINE]]|  2|
   return t;                               // HEADER: [[@LINE]]|  2|  return t;
@@ -22,3 +22,7 @@
 // compile a program with two calls to Foo<int>::DoIt(10) for each desired
 // architecture. Collect a raw profile from any one of these binaries, index
 // it, and check it in along with the executables.
+
+// Note:
+// The line execution count on line 14 is incorrect because of a bug in the
+// checked-in coverage mappings, which has subsequently been fixed in clang.
diff --git a/test/tools/llvm-cov/Inputs/lineExecutionCounts.covmapping b/test/tools/llvm-cov/Inputs/lineExecutionCounts.covmapping
index f70ea05..0b2458a 100644
--- a/test/tools/llvm-cov/Inputs/lineExecutionCounts.covmapping
+++ b/test/tools/llvm-cov/Inputs/lineExecutionCounts.covmapping
Binary files differ
diff --git a/test/tools/llvm-cov/Inputs/prefer_used_to_unused.covmapping b/test/tools/llvm-cov/Inputs/prefer_used_to_unused.covmapping
index 4722129..c4e1c80 100644
--- a/test/tools/llvm-cov/Inputs/prefer_used_to_unused.covmapping
+++ b/test/tools/llvm-cov/Inputs/prefer_used_to_unused.covmapping
Binary files differ
diff --git a/test/tools/llvm-cov/Inputs/regionMarkers.covmapping b/test/tools/llvm-cov/Inputs/regionMarkers.covmapping
index 1c9b5dd..af920c2 100644
--- a/test/tools/llvm-cov/Inputs/regionMarkers.covmapping
+++ b/test/tools/llvm-cov/Inputs/regionMarkers.covmapping
Binary files differ
diff --git a/test/tools/llvm-cov/Inputs/regionMarkers.json b/test/tools/llvm-cov/Inputs/regionMarkers.json
index 0589f7b..c996433 100644
--- a/test/tools/llvm-cov/Inputs/regionMarkers.json
+++ b/test/tools/llvm-cov/Inputs/regionMarkers.json
@@ -12,9 +12,9 @@
 
 // Verify the Summary Section for the first file
 // CHECK-SAME: "summary":{
-// CHECK-SAME: "lines":{"count":21,"covered":18,"percent":85},
+// CHECK-SAME: "lines":{"count":21,"covered":19,"percent":90},
 // CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100},
-// CHECK-SAME: "regions":{"count":10,"covered":7,"notcovered":3,"percent":70}}
+// CHECK-SAME: "regions":{"count":11,"covered":8,"notcovered":3,"percent":72}}
 
 // Close Files Array
 // CHECK-SAME: ],
@@ -29,10 +29,7 @@
 
 // Full Export Summary
 // CHECK-SAME: "totals":{
-// CHECK-SAME: "lines":{"count":21,"covered":18,"percent":85},
+// CHECK-SAME: "lines":{"count":21,"covered":19,"percent":90},
 // CHECK-SAME: "functions":{"count":1,"covered":1,"percent":100},
 // CHECK-SAME: "instantiations":{"count":1,"covered":1,"percent":100},
-// CHECK-SAME: "regions":{"count":10,"covered":7,"notcovered":3,"percent":70}}
-
-// Close the export object, data array, and root object
-// CHECK-SAME: }]}
+// CHECK-SAME: "regions":{"count":11,"covered":8,"notcovered":3,"percent":72}}
diff --git a/test/tools/llvm-cov/Inputs/report.covmapping b/test/tools/llvm-cov/Inputs/report.covmapping
index 5d0bfc1..f9858ed 100644
--- a/test/tools/llvm-cov/Inputs/report.covmapping
+++ b/test/tools/llvm-cov/Inputs/report.covmapping
Binary files differ
diff --git a/test/tools/llvm-cov/Inputs/templateInstantiations.covmapping b/test/tools/llvm-cov/Inputs/templateInstantiations.covmapping
index 2dfc6cd..b604e06 100644
--- a/test/tools/llvm-cov/Inputs/templateInstantiations.covmapping
+++ b/test/tools/llvm-cov/Inputs/templateInstantiations.covmapping
Binary files differ
diff --git a/test/tools/llvm-cov/deferred-region.cpp b/test/tools/llvm-cov/deferred-region.cpp
index 8ff5c01..38090fb 100644
--- a/test/tools/llvm-cov/deferred-region.cpp
+++ b/test/tools/llvm-cov/deferred-region.cpp
@@ -1,8 +1,8 @@
-// RUN: llvm-cov show %S/Inputs/deferred-regions.covmapping -instr-profile %S/Inputs/deferred-regions.profdata -show-line-counts-or-regions -dump -path-equivalence=/Users/vk/src/llvm.org-coverage-braces/llvm/test/tools,%S/.. %s 2>&1 > %t.out && FileCheck %s -input-file %t.out && FileCheck %s -input-file %t.out -check-prefix=MARKER
+// RUN: llvm-cov show %S/Inputs/deferred-regions.covmapping -instr-profile %S/Inputs/deferred-regions.profdata -show-line-counts-or-regions -dump -path-equivalence=/tmp,%S %s 2>%t.markers > %t.out && FileCheck %s -input-file %t.out && FileCheck %s -input-file %t.markers -check-prefix=MARKER
 
 void foo(int x) {
   if (x == 0) { // CHECK: [[@LINE]]|{{ +}}2|
-    return; // CHECK: [[@LINE]]|{{ +}}1|
+    return; // CHECK-NEXT: [[@LINE]]|{{ +}}1|
   }
 
 } // CHECK: [[@LINE]]|{{ +}}1|
@@ -93,8 +93,6 @@
 // MARKER-NEXT: Marker at 19:27 = 1
 // MARKER-NEXT: Highlighted line 24, 7 -> 12
 // MARKER-NEXT: Highlighted line 36, 5 -> 11
-// MARKER-NEXT: Highlighted line 46, 1 -> ?
-// MARKER-NEXT: Highlighted line 47, 1 -> 7
 // MARKER-NEXT: Highlighted line 47, 7 -> 14
 // MARKER-NEXT: Highlighted line 47, 14 -> 21
 // MARKER-NEXT: Highlighted line 47, 21 -> 23
@@ -105,6 +103,3 @@
 // MARKER-NEXT: Highlighted line 63, 5 -> 13
 // MARKER-NEXT: Highlighted line 67, 1 -> ?
 // MARKER-NEXT: Highlighted line 68, 1 -> 8
-// MARKER-NEXT: Highlighted line 68, 8 -> ?
-// MARKER-NEXT: Highlighted line 69, 1 -> 2
-// MARKER-NEXT: Highlighted line 77, 1 -> 2
diff --git a/test/tools/llvm-cov/hideUnexecutedSubviews.test b/test/tools/llvm-cov/hideUnexecutedSubviews.test
index 20f0a43..e972a19 100644
--- a/test/tools/llvm-cov/hideUnexecutedSubviews.test
+++ b/test/tools/llvm-cov/hideUnexecutedSubviews.test
@@ -5,8 +5,8 @@
 RUN: llvm-cov show %S/Inputs/templateInstantiations.covmapping -instr-profile %t.profdata -format html -show-region-summary -show-instantiation-summary -o %t.html.dir -path-equivalence=/tmp,%S %S/showTemplateInstantiations.cpp
 RUN: FileCheck -check-prefix=FILE %s -input-file %t.html.dir/coverage/tmp/showTemplateInstantiations.cpp.html
 
-FILE: Unexecuted instantiation: _Z4funcIbEiT_
 FILE: Unexecuted instantiation: _Z4funcIiEiT_
+FILE: Unexecuted instantiation: _Z4funcIbEiT_
 FILE-NOT: Unexecuted instantiation
 
 RUN: FileCheck -check-prefix=INDEX %s -input-file %t.html.dir/index.html
@@ -23,4 +23,4 @@
 INDEX: <td class='column-entry-red'>
 INDEX: 41.67% (5/12)
 INDEX: <td class='column-entry-red'>
-INDEX: 20.00% (1/5)
+INDEX: 16.67% (1/6)
diff --git a/test/tools/llvm-cov/load-multiple-objects.test b/test/tools/llvm-cov/load-multiple-objects.test
index 55fed31..41eae19 100644
--- a/test/tools/llvm-cov/load-multiple-objects.test
+++ b/test/tools/llvm-cov/load-multiple-objects.test
@@ -4,8 +4,8 @@
 
 // RUN: llvm-cov report %S/Inputs/multiple-files.covmapping -object %S/Inputs/highlightedRanges.covmapping -instr-profile %t.profdata | FileCheck %s -check-prefixes=OBJ1,OBJ2
 
-// OBJ2: showHighlightedRanges.cpp
 // OBJ1: f2.c
 // OBJ1: f4.c
 // OBJ1: f3.c
 // OBJ1: f1.c
+// OBJ2: showHighlightedRanges.cpp
diff --git a/test/tools/llvm-cov/prefer_used_to_unused.h b/test/tools/llvm-cov/prefer_used_to_unused.h
index 852eb92..4d5e18c 100644
--- a/test/tools/llvm-cov/prefer_used_to_unused.h
+++ b/test/tools/llvm-cov/prefer_used_to_unused.h
@@ -9,14 +9,14 @@
 // llvm-cov convert-for-testing -o prefer_used_to_unused.covmapping tmp
 
 // RUN: llvm-profdata merge %S/Inputs/prefer_used_to_unused.proftext -o %t.profdata
-// RUN: llvm-cov show %S/Inputs/prefer_used_to_unused.covmapping -instr-profile %t.profdata -path-equivalence=/tmp/uu/./,%S %s | FileCheck %s
+// RUN: llvm-cov show %S/Inputs/prefer_used_to_unused.covmapping -instr-profile %t.profdata -path-equivalence=/tmp/,%S %s | FileCheck %s
 
 // Coverage data for this function has a non-zero hash value if it is used in the translation unit.
 inline int sampleFunc(int A) { // CHECK:      [[@LINE]]| 1|inline int sampleFunc(int A) {
   if (A > 0)                   // CHECK-NEXT: [[@LINE]]| 1|  if (A > 0)
     return A;                  // CHECK-NEXT: [[@LINE]]| 1|    return A;
   return 0;                    // CHECK-NEXT: [[@LINE]]| 0|  return 0;
-}                              // CHECK-NEXT: [[@LINE]]| 1|}
+}                              // CHECK-NEXT: [[@LINE]]| 0|}
 
 // The hash for this function is zero in both cases, either it is used in the translation unit or not.
 inline int simpleFunc(int A) { // CHECK:      [[@LINE]]| 1|inline int simpleFunc(int A) {
diff --git a/test/tools/llvm-cov/report.cpp b/test/tools/llvm-cov/report.cpp
index be7b5ea..6676ed8 100644
--- a/test/tools/llvm-cov/report.cpp
+++ b/test/tools/llvm-cov/report.cpp
@@ -1,25 +1,25 @@
-// RUN: llvm-cov report %S/Inputs/report.covmapping -instr-profile %S/Inputs/report.profdata -path-equivalence=,%S 2>&1 -show-region-summary -show-instantiation-summary | FileCheck %s
-// RUN: llvm-cov report -show-functions %S/Inputs/report.covmapping -instr-profile %S/Inputs/report.profdata -path-equivalence=,%S %s 2>&1 | FileCheck -check-prefix=FILT %s
-// RUN: llvm-cov report -show-functions %S/Inputs/report.covmapping -instr-profile %S/Inputs/report.profdata -path-equivalence=,%S %s does-not-exist.cpp 2>&1 | FileCheck -check-prefix=FILT %s
-// RUN: not llvm-cov report -show-functions %S/Inputs/report.covmapping -instr-profile %S/Inputs/report.profdata -path-equivalence=,%S 2>&1 | FileCheck -check-prefix=NO_FILES %s
+// RUN: llvm-cov report %S/Inputs/report.covmapping -instr-profile %S/Inputs/report.profdata -path-equivalence=/tmp,%S 2>&1 -show-region-summary -show-instantiation-summary | FileCheck %s
+// RUN: llvm-cov report -show-functions %S/Inputs/report.covmapping -instr-profile %S/Inputs/report.profdata -path-equivalence=/tmp,%S %s 2>&1 | FileCheck -check-prefix=FILT %s
+// RUN: llvm-cov report -show-functions %S/Inputs/report.covmapping -instr-profile %S/Inputs/report.profdata -path-equivalence=/tmp,%S %s does-not-exist.cpp 2>&1 | FileCheck -check-prefix=FILT %s
+// RUN: not llvm-cov report -show-functions %S/Inputs/report.covmapping -instr-profile %S/Inputs/report.profdata -path-equivalence=/tmp,%S 2>&1 | FileCheck -check-prefix=NO_FILES %s
 
 // NO_FILES: Source files must be specified when -show-functions=true is specified
 
 // CHECK: Regions    Missed Regions     Cover   Functions  Missed Functions  Executed  Instantiations   Missed Insts.  Executed       Lines      Missed Lines     Cover
 // CHECK-NEXT: ---
-// CHECK-NEXT: report.cpp                          5                 2    60.00%           4                 1    75.00%               4               1    75.00%          13                 4    69.23%
+// CHECK-NEXT: report.cpp                          6                 2    66.67%           4                 1    75.00%               4               1    75.00%          13                 3    76.92%
 // CHECK-NEXT: ---
-// CHECK-NEXT: TOTAL                               5                 2    60.00%           4                 1    75.00%               4               1    75.00%          13                 4    69.23%
+// CHECK-NEXT: TOTAL                               6                 2    66.67%           4                 1    75.00%               4               1    75.00%          13                 3    76.92%
 
-// FILT: File 'report.cpp':
+// FILT: File '{{.*}}report.cpp':
 // FILT-NEXT: Name        Regions  Miss   Cover  Lines  Miss   Cover
 // FILT-NEXT: ---
-// FILT-NEXT: _Z3foob           2     1  50.00%      4     2  50.00%
+// FILT-NEXT: _Z3foob           3     1  66.67%      4     1  75.00%
 // FILT-NEXT: _Z3barv           1     0 100.00%      2     0 100.00%
 // FILT-NEXT: _Z4funcv          1     1   0.00%      2     2   0.00%
 // FILT-NEXT: main              1     0 100.00%      5     0 100.00%
 // FILT-NEXT: ---
-// FILT-NEXT: TOTAL             5     2  60.00%     13     4  69.23%
+// FILT-NEXT: TOTAL             6     2  66.67%     13     3  76.92%
 
 void foo(bool cond) {
   if (cond) {
@@ -39,8 +39,8 @@
 }
 
 // Test that listing multiple functions in a function view works.
-// RUN: llvm-cov show -o %t.dir %S/Inputs/report.covmapping -instr-profile=%S/Inputs/report.profdata -path-equivalence=,%S -name-regex=".*" %s
-// RUN: FileCheck -check-prefix=FUNCTIONS -input-file %t.dir/coverage/report.cpp.txt %s
+// RUN: llvm-cov show -o %t.dir %S/Inputs/report.covmapping -instr-profile=%S/Inputs/report.profdata -path-equivalence=/tmp,%S -name-regex=".*" %s
+// RUN: FileCheck -check-prefix=FUNCTIONS -input-file %t.dir/coverage/tmp/report.cpp.txt %s
 // FUNCTIONS: _Z3foob
 // FUNCTIONS: _Z3barv
 // FUNCTIONS: _Z4func
diff --git a/test/tools/llvm-cov/showHighlightedRanges.cpp b/test/tools/llvm-cov/showHighlightedRanges.cpp
index bfcdf76..f225816 100644
--- a/test/tools/llvm-cov/showHighlightedRanges.cpp
+++ b/test/tools/llvm-cov/showHighlightedRanges.cpp
@@ -1,4 +1,4 @@
-// RUN: llvm-cov show %S/Inputs/highlightedRanges.covmapping -instr-profile %S/Inputs/highlightedRanges.profdata -dump -path-equivalence=/Users/bogner/code/llvm/test/tools,%S/.. %s 2>&1 | FileCheck %s -check-prefixes=TEXT,SHARED
+// RUN: llvm-cov show %S/Inputs/highlightedRanges.covmapping -instr-profile %S/Inputs/highlightedRanges.profdata -dump -path-equivalence=/tmp,%S %s 2>&1 | FileCheck %s -check-prefixes=TEXT,SHARED
 
 void func() {
   return;
@@ -44,5 +44,5 @@
   return 0;
 }
 
-// RUN: llvm-cov show %S/Inputs/highlightedRanges.covmapping -instr-profile %S/Inputs/highlightedRanges.profdata -format html -dump -path-equivalence=/Users/bogner/code/llvm/test/tools,%S/.. %s 2>&1 | FileCheck %s -check-prefixes=HTML,SHARED
+// RUN: llvm-cov show %S/Inputs/highlightedRanges.covmapping -instr-profile %S/Inputs/highlightedRanges.profdata -format html -dump -path-equivalence=/tmp,%S %s 2>&1 | FileCheck %s -check-prefixes=HTML,SHARED
 // RUN: llvm-cov export %S/Inputs/highlightedRanges.covmapping -instr-profile %S/Inputs/highlightedRanges.profdata 2>&1 | FileCheck %S/Inputs/highlightedRanges.json
diff --git a/test/tools/llvm-cov/showRegionMarkers.cpp b/test/tools/llvm-cov/showRegionMarkers.cpp
index c6d1690..676d627 100644
--- a/test/tools/llvm-cov/showRegionMarkers.cpp
+++ b/test/tools/llvm-cov/showRegionMarkers.cpp
@@ -22,7 +22,7 @@
   return 0;
 }
 
-// RUN: llvm-cov show %S/Inputs/regionMarkers.covmapping -instr-profile %t.profdata -show-regions -dump -path-equivalence=/Users/bogner/code/llvm/test/tools,%S/.. %s 2>&1 | FileCheck %s
-// RUN: llvm-cov show %S/Inputs/regionMarkers.covmapping -instr-profile %t.profdata -show-regions -format=html -dump -path-equivalence=/Users/bogner/code/llvm/test/tools,%S/.. %s 2>&1 | FileCheck %s
+// RUN: llvm-cov show %S/Inputs/regionMarkers.covmapping -instr-profile %t.profdata -show-regions -dump -path-equivalence=/tmp,%S/ %s 2>&1 | FileCheck %s
+// RUN: llvm-cov show %S/Inputs/regionMarkers.covmapping -instr-profile %t.profdata -show-regions -format=html -dump -path-equivalence=/tmp,%S %s 2>&1 | FileCheck %s
 
 // RUN: llvm-cov export %S/Inputs/regionMarkers.covmapping -instr-profile %t.profdata 2>&1 | FileCheck %S/Inputs/regionMarkers.json
diff --git a/test/tools/llvm-cov/showTemplateInstantiations.cpp b/test/tools/llvm-cov/showTemplateInstantiations.cpp
index 428ceac..380e98b 100644
--- a/test/tools/llvm-cov/showTemplateInstantiations.cpp
+++ b/test/tools/llvm-cov/showTemplateInstantiations.cpp
@@ -7,29 +7,29 @@
 int func(T x) {      // ALL-NEXT:    [[@LINE]]| 2|int func(T x) {
   if(x)              // ALL-NEXT:    [[@LINE]]| 2|  if(x)
     return 0;        // ALL-NEXT:    [[@LINE]]| 1|    return 0;
-  else               // ALL-NEXT:    [[@LINE]]| 2|  else
+  else               // ALL-NEXT:    [[@LINE]]| 1|  else
     return 1;        // ALL-NEXT:    [[@LINE]]| 1|    return 1;
   int j = 1;         // ALL-NEXT:    [[@LINE]]| 0|  int j = 1;
-}                    // ALL-NEXT:    [[@LINE]]| 2|}
+}                    // ALL-NEXT:    [[@LINE]]| 0|}
+
+                     // ALL:         _Z4{{[a-z]+}}IiEiT_:
+                     // FILTER-NOT:  _Z4{{[a-z]+}}IiEiT_:
+                     // ALL:         [[@LINE-10]]| 1|int func(T x) {
+                     // ALL-NEXT:    [[@LINE-10]]| 1|  if(x)
+                     // ALL-NEXT:    [[@LINE-10]]| 0|    return 0;
+                     // ALL-NEXT:    [[@LINE-10]]| 1|  else
+                     // ALL-NEXT:    [[@LINE-10]]| 1|    return 1;
+                     // ALL-NEXT:    [[@LINE-10]]| 0|  int j = 1;
+                     // ALL-NEXT:    [[@LINE-10]]| 0|}
 
                      // SHARED:       {{^ *(\| )?}}_Z4funcIbEiT_:
-                     // SHARED:       [[@LINE-9]]| 1|int func(T x) {
-                     // SHARED-NEXT:  [[@LINE-9]]| 1|  if(x)
-                     // SHARED-NEXT:  [[@LINE-9]]| 1|    return 0;
-                     // SHARED-NEXT:  [[@LINE-9]]| 1|  else
-                     // SHARED-NEXT:  [[@LINE-9]]| 0|    return 1;
-                     // SHARED-NEXT:  [[@LINE-9]]| 0|  int j = 1;
-                     // SHARED-NEXT:  [[@LINE-9]]| 1|}
-
-                     // ALL:         {{^ *}}| _Z4funcIiEiT_:
-                     // FILTER-NOT:  {{^ *(\| )?}} _Z4funcIiEiT_:
-                     // ALL:         [[@LINE-19]]| 1|int func(T x) {
-                     // ALL-NEXT:    [[@LINE-19]]| 1|  if(x)
-                     // ALL-NEXT:    [[@LINE-19]]| 0|    return 0;
-                     // ALL-NEXT:    [[@LINE-19]]| 1|  else
-                     // ALL-NEXT:    [[@LINE-19]]| 1|    return 1;
-                     // ALL-NEXT:    [[@LINE-19]]| 0|  int j = 1;
-                     // ALL-NEXT:    [[@LINE-19]]| 1|}
+                     // SHARED:       [[@LINE-19]]| 1|int func(T x) {
+                     // SHARED-NEXT:  [[@LINE-19]]| 1|  if(x)
+                     // SHARED-NEXT:  [[@LINE-19]]| 1|    return 0;
+                     // SHARED-NEXT:  [[@LINE-19]]| 0|  else
+                     // SHARED-NEXT:  [[@LINE-19]]| 0|    return 1;
+                     // SHARED-NEXT:  [[@LINE-19]]| 0|  int j = 1;
+                     // SHARED-NEXT:  [[@LINE-19]]| 0|}
 
 int main() {         // ALL:         [[@LINE]]| 1|int main() {
   func<int>(0);      // ALL-NEXT:    [[@LINE]]| 1|  func<int>(0);
@@ -45,44 +45,16 @@
 // RUN: FileCheck -check-prefixes=HTML-SHARED,HTML-ALL -input-file=%t.html.dir/coverage/tmp/showTemplateInstantiations.cpp.html %s
 // RUN: FileCheck -check-prefixes=HTML-SHARED,HTML-FILTER -input-file=%t.html.filtered.dir/coverage/tmp/showTemplateInstantiations.cpp.html %s
 
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='uncovered-line'></td><td class='code'><pre>// before
-// HTML-FILTER-NOT: <td class='line-number'><a name='L[[@LINE-45]]' href='#L[[@LINE-45]]'><pre>[[@LINE-45]]</pre></a></td><td class='uncovered-line'></td><td class='code'><pre>// before
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='uncovered-line'></td><td class='code'><pre>template&lt;typename T&gt;
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>2</pre></td><td class='code'><pre>int func(T x) {
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>2</pre></td><td class='code'><pre>  if(x)
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>    ret
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>2</pre></td><td class='code'><pre>  else
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>    ret
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='uncovered-line'><pre>0</pre></td><td class='code'><pre>
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>2</pre></td><td class='code'><pre>}
-
-// HTML-SHARED: <div class='source-name-title'><pre>_Z4funcIbEiT_</pre></div>
-// HTML-SHARED: <td class='line-number'><a name='L[[@LINE-53]]' href='#L[[@LINE-53]]'><pre>[[@LINE-53]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>int func(T x) {
-// HTML-SHARED: <td class='line-number'><a name='L[[@LINE-53]]' href='#L[[@LINE-53]]'><pre>[[@LINE-53]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>  if(x)
-// HTML-SHARED: <td class='line-number'><a name='L[[@LINE-53]]' href='#L[[@LINE-53]]'><pre>[[@LINE-53]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>    ret
-// HTML-SHARED: <td class='line-number'><a name='L[[@LINE-53]]' href='#L[[@LINE-53]]'><pre>[[@LINE-53]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>  else
-// HTML-SHARED: <td class='line-number'><a name='L[[@LINE-53]]' href='#L[[@LINE-53]]'><pre>[[@LINE-53]]</pre></a></td><td class='uncovered-line'><pre>0</pre></td><td class='code'><pre>
-// HTML-SHARED: <td class='line-number'><a name='L[[@LINE-53]]' href='#L[[@LINE-53]]'><pre>[[@LINE-53]]</pre></a></td><td class='uncovered-line'><pre>0</pre></td><td class='code'><pre>
-// HTML-SHARED: <td class='line-number'><a name='L[[@LINE-53]]' href='#L[[@LINE-53]]'><pre>[[@LINE-53]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>}
+// HTML-ALL: <td class='line-number'><a name='L4' href='#L4'><pre>4</pre></a></td><td class='uncovered-line'></td><td class='code'><pre>// before
+// HTML-FILTER-NOT: <td class='line-number'><a name='L4' href='#L4'><pre>4</pre></a></td><td class='uncovered-line'></td><td class='code'><pre>// before
+// HTML-ALL: <td class='line-number'><a name='L6' href='#L6'><pre>6</pre></a></td><td class='uncovered-line'></td><td class='code'><pre>template&lt;typename T&gt;
 
 // HTML-ALL: <div class='source-name-title'><pre>_Z4funcIiEiT_</pre></div>
 // HTML-FILTER-NOT: <div class='source-name-title'><pre>_Z4funcIiEiT_</pre></div><table>
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-63]]' href='#L[[@LINE-63]]'><pre>[[@LINE-63]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>int func(T x) {
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-63]]' href='#L[[@LINE-63]]'><pre>[[@LINE-63]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>  if(x)
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-63]]' href='#L[[@LINE-63]]'><pre>[[@LINE-63]]</pre></a></td><td class='uncovered-line'><pre>0</pre></td><td class='code'><pre>
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-63]]' href='#L[[@LINE-63]]'><pre>[[@LINE-63]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>  else
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-63]]' href='#L[[@LINE-63]]'><pre>[[@LINE-63]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>    ret
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-63]]' href='#L[[@LINE-63]]'><pre>[[@LINE-63]]</pre></a></td><td class='uncovered-line'><pre>0</pre></td><td class='code'><pre>
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-63]]' href='#L[[@LINE-63]]'><pre>[[@LINE-63]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>}
+// HTML-ALL: <td class='line-number'><a name='L7' href='#L7'><pre>7</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>int func(T x) {
 
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>int main() {
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>  func&lt;int&gt;(0);
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>  func&lt;bool&gt;(true);
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>  return 0;
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-44]]' href='#L[[@LINE-44]]'><pre>[[@LINE-44]]</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>}
-
-// HTML-ALL: <td class='line-number'><a name='L[[@LINE-45]]' href='#L[[@LINE-45]]'><pre>[[@LINE-45]]</pre></a></td><td class='uncovered-line'></td><td class='code'><pre>// after
-// HTML-FILTER-NOT: <td class='line-number'><a name='L[[@LINE-46]]' href='#L[[@LINE-46]]'><pre>[[@LINE-46]]</pre></a></td><td class='uncovered-line'></td><td class='code'><pre>// after
+// HTML-SHARED: <div class='source-name-title'><pre>_Z4funcIbEiT_</pre></div>
+// HTML-SHARED: <td class='line-number'><a name='L7' href='#L7'><pre>7</pre></a></td><td class='covered-line'><pre>1</pre></td><td class='code'><pre>int func(T x) {
 
 // RUN: FileCheck -check-prefix=HTML-JUMP -input-file=%t.html.dir/coverage/tmp/showTemplateInstantiations.cpp.html %s
 // HTML-JUMP: <pre>Source (<a href='#L{{[0-9]+}}'>jump to first uncovered line</a>)</pre>
@@ -93,5 +65,5 @@
 // NO_INSTS-NOT: {{^ *}}| _Z4funcIiEiT_:
 
 // RUN: llvm-cov report %S/Inputs/templateInstantiations.covmapping -dump -instr-profile %S/Inputs/templateInstantiations.profdata -path-equivalence=/tmp,%S %s | FileCheck -check-prefix=DUMP %s
-// DUMP: InstantiationGroup: Definition at line 7, column 30 with size = 2
+// DUMP: InstantiationGroup: Definition at line 7, column 15 with size = 2
 // DUMP: InstantiationGroup: main with size = 1
diff --git a/tools/llvm-cov/SourceCoverageView.cpp b/tools/llvm-cov/SourceCoverageView.cpp
index f2e8788..0572968 100644
--- a/tools/llvm-cov/SourceCoverageView.cpp
+++ b/tools/llvm-cov/SourceCoverageView.cpp
@@ -111,16 +111,19 @@
 }
 
 bool SourceCoverageView::shouldRenderRegionMarkers(
-    CoverageSegmentArray Segments) const {
+    const LineCoverageStats &LCS) const {
   if (!getOptions().ShowRegionMarkers)
     return false;
 
-  // Render the region markers if there's more than one count to show.
-  unsigned RegionCount = 0;
-  for (const auto *S : Segments)
-    if (S->IsRegionEntry)
-      if (++RegionCount > 1)
-        return true;
+  CoverageSegmentArray Segments = LCS.getLineSegments();
+  if (Segments.empty())
+    return false;
+  for (unsigned I = 0, E = Segments.size() - 1; I < E; ++I) {
+    const auto *CurSeg = Segments[I];
+    if (!CurSeg->IsRegionEntry || CurSeg->Count == LCS.getExecutionCount())
+      continue;
+    return true;
+  }
   return false;
 }
 
@@ -220,7 +223,7 @@
     renderLine(OS, {*LI, LI.line_number()}, *LCI, ExpansionColumn, ViewDepth);
 
     // Show the region markers.
-    if (shouldRenderRegionMarkers(LCI->getLineSegments()))
+    if (shouldRenderRegionMarkers(*LCI))
       renderRegionMarkers(OS, *LCI, ViewDepth);
 
     // Show the expansions and instantiations for this line.
diff --git a/tools/llvm-cov/SourceCoverageView.h b/tools/llvm-cov/SourceCoverageView.h
index 35dea59..7f58ea5 100644
--- a/tools/llvm-cov/SourceCoverageView.h
+++ b/tools/llvm-cov/SourceCoverageView.h
@@ -225,7 +225,7 @@
   static std::string formatCount(uint64_t N);
 
   /// \brief Check if region marker output is expected for a line.
-  bool shouldRenderRegionMarkers(CoverageSegmentArray Segments) const;
+  bool shouldRenderRegionMarkers(const LineCoverageStats &LCS) const;
 
   /// \brief Check if there are any sub-views attached to this view.
   bool hasSubViews() const;
diff --git a/tools/llvm-cov/SourceCoverageViewHTML.cpp b/tools/llvm-cov/SourceCoverageViewHTML.cpp
index e839184..3140651 100644
--- a/tools/llvm-cov/SourceCoverageViewHTML.cpp
+++ b/tools/llvm-cov/SourceCoverageViewHTML.cpp
@@ -514,8 +514,9 @@
     return tag("span", Snippet, Color.getValue());
   };
 
-  auto CheckIfUncovered = [](const CoverageSegment *S) {
-    return S && S->HasCount && S->Count == 0;
+  auto CheckIfUncovered = [&](const CoverageSegment *S) {
+    return S && (!S->IsGapRegion || (Color && *Color == "red")) &&
+           S->HasCount && S->Count == 0;
   };
 
   if (CheckIfUncovered(LCS.getWrappedSegment())) {
@@ -526,11 +527,10 @@
 
   for (unsigned I = 0, E = Segments.size(); I < E; ++I) {
     const auto *CurSeg = Segments[I];
-    if (CurSeg->Col == ExpansionCol)
-      Color = "cyan";
-    else if ((!CurSeg->IsGapRegion || (Color && *Color == "red")) &&
-             CheckIfUncovered(CurSeg))
+    if (CheckIfUncovered(CurSeg))
       Color = "red";
+    else if (CurSeg->Col == ExpansionCol)
+      Color = "cyan";
     else
       Color = None;
 
@@ -556,7 +556,7 @@
   // 4. Snippets[1:N+1] correspond to \p Segments[0:N]: use these to generate
   //    sub-line region count tooltips if needed.
 
-  if (shouldRenderRegionMarkers(Segments)) {
+  if (shouldRenderRegionMarkers(LCS)) {
     // Just consider the segments which start *and* end on this line.
     for (unsigned I = 0, E = Segments.size() - 1; I < E; ++I) {
       const auto *CurSeg = Segments[I];
diff --git a/tools/llvm-cov/SourceCoverageViewText.cpp b/tools/llvm-cov/SourceCoverageViewText.cpp
index 4b69b08..2480ee9 100644
--- a/tools/llvm-cov/SourceCoverageViewText.cpp
+++ b/tools/llvm-cov/SourceCoverageViewText.cpp
@@ -106,7 +106,8 @@
   SmallVector<std::pair<unsigned, unsigned>, 2> HighlightedRanges;
 
   // The first segment overlaps from a previous line, so we treat it specially.
-  if (WrappedSegment && WrappedSegment->HasCount && WrappedSegment->Count == 0)
+  if (WrappedSegment && !WrappedSegment->IsGapRegion &&
+      WrappedSegment->HasCount && WrappedSegment->Count == 0)
     Highlight = raw_ostream::RED;
 
   // Output each segment of the line, possibly highlighted.
@@ -120,11 +121,11 @@
     if (getOptions().Debug && Highlight)
       HighlightedRanges.push_back(std::make_pair(Col, End));
     Col = End;
-    if (Col == ExpansionCol)
-      Highlight = raw_ostream::CYAN;
-    else if ((!S->IsGapRegion || Highlight == raw_ostream::RED) &&
-             S->HasCount && S->Count == 0)
+    if ((!S->IsGapRegion || (Highlight && *Highlight == raw_ostream::RED)) &&
+        S->HasCount && S->Count == 0)
       Highlight = raw_ostream::RED;
+    else if (Col == ExpansionCol)
+      Highlight = raw_ostream::CYAN;
     else
       Highlight = None;
   }
diff --git a/unittests/ProfileData/CoverageMappingTest.cpp b/unittests/ProfileData/CoverageMappingTest.cpp
index 62dc0d6..74c515e5 100644
--- a/unittests/ProfileData/CoverageMappingTest.cpp
+++ b/unittests/ProfileData/CoverageMappingTest.cpp
@@ -648,9 +648,10 @@
   CoverageData Data = LoadedCoverage->getCoverageForFile("file1");
 
   unsigned Line = 0;
-  unsigned LineCounts[] = {20, 20, 20, 20, 10, 10, 10, 10, 10, 0, 0};
+  unsigned LineCounts[] = {20, 20, 20, 20, 30, 10, 10, 10, 10, 0, 0};
   for (const auto &LCS : getLineCoverageStats(Data)) {
     ASSERT_EQ(Line + 1, LCS.getLine());
+    errs() << "Line: " << Line + 1 << ", count = " << LCS.getExecutionCount() << "\n";
     ASSERT_EQ(LineCounts[Line], LCS.getExecutionCount());
     ++Line;
   }