r235967 - InstrProf: Mark code regions after throw expressions as unreachable

Justin Bogner mail at justinbogner.com
Mon Apr 27 23:31:55 PDT 2015


Author: bogner
Date: Tue Apr 28 01:31:55 2015
New Revision: 235967

URL: http://llvm.org/viewvc/llvm-project?rev=235967&view=rev
Log:
InstrProf: Mark code regions after throw expressions as unreachable

We weren't setting regions as being unreachable after C++ throw
expressions, leading to incorrect count propagations.

Modified:
    cfe/trunk/lib/CodeGen/CodeGenPGO.cpp
    cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp
    cfe/trunk/test/CoverageMapping/trycatch.cpp
    cfe/trunk/test/Profile/Inputs/cxx-throws.proftext
    cfe/trunk/test/Profile/cxx-throws.cpp

Modified: cfe/trunk/lib/CodeGen/CodeGenPGO.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenPGO.cpp?rev=235967&r1=235966&r2=235967&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenPGO.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenPGO.cpp Tue Apr 28 01:31:55 2015
@@ -317,6 +317,14 @@ struct ComputeRegionCounts : public Cons
     RecordNextStmtCount = true;
   }
 
+  void VisitCXXThrowExpr(const CXXThrowExpr *E) {
+    RecordStmtCount(E);
+    if (E->getSubExpr())
+      Visit(E->getSubExpr());
+    PGO.setCurrentRegionUnreachable();
+    RecordNextStmtCount = true;
+  }
+
   void VisitGotoStmt(const GotoStmt *S) {
     RecordStmtCount(S);
     PGO.setCurrentRegionUnreachable();

Modified: cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp?rev=235967&r1=235966&r2=235967&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp (original)
+++ cfe/trunk/lib/CodeGen/CoverageMappingGen.cpp Tue Apr 28 01:31:55 2015
@@ -592,6 +592,13 @@ struct CounterCoverageMappingBuilder
     terminateRegion(S);
   }
 
+  void VisitCXXThrowExpr(const CXXThrowExpr *E) {
+    extendRegion(E);
+    if (E->getSubExpr())
+      Visit(E->getSubExpr());
+    terminateRegion(E);
+  }
+
   void VisitGotoStmt(const GotoStmt *S) { terminateRegion(S); }
 
   void VisitLabelStmt(const LabelStmt *S) {

Modified: cfe/trunk/test/CoverageMapping/trycatch.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CoverageMapping/trycatch.cpp?rev=235967&r1=235966&r2=235967&view=diff
==============================================================================
--- cfe/trunk/test/CoverageMapping/trycatch.cpp (original)
+++ cfe/trunk/test/CoverageMapping/trycatch.cpp Tue Apr 28 01:31:55 2015
@@ -10,17 +10,19 @@ class Warning {
 };
 
                                       // CHECK: func
-void func(int i) {                    // CHECK-NEXT: File 0, [[@LINE]]:18 -> [[@LINE+6]]:2 = #0
-  if(i % 2)                           // CHECK-NEXT: File 0, [[@LINE]]:6 -> [[@LINE]]:11 = #0
-    throw Error();                    // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:18 = #1
-                                      // CHECK-NEXT: File 0, [[@LINE+1]]:8 -> [[@LINE+2]]:27 = (#0 - #1)
-  else if(i == 8)                     // CHECK-NEXT: File 0, [[@LINE]]:11 -> [[@LINE]]:17 = (#0 - #1)
+void func(int i) {                    // CHECK-NEXT: File 0, [[@LINE]]:18 -> {{[0-9]+}}:2 = #0
+                                      // CHECK-NEXT: File 0, [[@LINE+1]]:6 -> [[@LINE+1]]:11 = #0
+  if(i % 2) {                         // CHECK-NEXT: File 0, [[@LINE]]:13 -> [[@LINE+4]]:4 = #1
+    throw Error();
+    int j = 0;                        // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE+2]]:4 = 0
+                                      // CHECK-NEXT: File 0, [[@LINE+1]]:10 -> [[@LINE+2]]:27 = (#0 - #1)
+  } else if(i == 8)                   // CHECK-NEXT: File 0, [[@LINE]]:13 -> [[@LINE]]:19 = (#0 - #1)
     throw ImportantError();           // CHECK-NEXT: File 0, [[@LINE]]:5 -> [[@LINE]]:27 = #2
 }
 
                                       // CHECK-NEXT: main
 int main() {                          // CHECK-NEXT: File 0, [[@LINE]]:12 -> [[@LINE+13]]:2 = #0
-  int j = 0;
+  int j = 1;
   try {
     func(j);
   } catch(const Error &e) {           // CHECK-NEXT: File 0, [[@LINE]]:27 -> [[@LINE+2]]:4 = #2

Modified: cfe/trunk/test/Profile/Inputs/cxx-throws.proftext
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Profile/Inputs/cxx-throws.proftext?rev=235967&r1=235966&r2=235967&view=diff
==============================================================================
--- cfe/trunk/test/Profile/Inputs/cxx-throws.proftext (original)
+++ cfe/trunk/test/Profile/Inputs/cxx-throws.proftext Tue Apr 28 01:31:55 2015
@@ -11,8 +11,16 @@ _Z6throwsv
 33
 100
 
-main
-0
+_Z11unreachablei
+0x28a
+3
 1
 1
+0
 
+main
+0x2cc
+3
+1
+1
+1

Modified: cfe/trunk/test/Profile/cxx-throws.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Profile/cxx-throws.cpp?rev=235967&r1=235966&r2=235967&view=diff
==============================================================================
--- cfe/trunk/test/Profile/cxx-throws.cpp (original)
+++ cfe/trunk/test/Profile/cxx-throws.cpp Tue Apr 28 01:31:55 2015
@@ -12,6 +12,7 @@
 
 // PGOGEN: @[[THC:__llvm_profile_counters__Z6throwsv]] = private global [9 x i64] zeroinitializer
 // PGOGEN-EXC: @[[THC:__llvm_profile_counters__Z6throwsv]] = private global [9 x i64] zeroinitializer
+// PGOGEN: @[[UNC:__llvm_profile_counters__Z11unreachablei]] = private global [3 x i64] zeroinitializer
 
 // PGOGEN-LABEL: @_Z6throwsv()
 // PGOUSE-LABEL: @_Z6throwsv()
@@ -60,14 +61,33 @@ void throws() {
   // PGOUSE: ret void
 }
 
+// PGOGEN-LABEL: @_Z11unreachablei(i32 %i)
+// PGOUSE-LABEL: @_Z11unreachablei(i32 %i)
+// PGOGEN: store {{.*}} @[[UNC]], i64 0, i64 0
+void unreachable(int i) {
+  // PGOGEN: store {{.*}} @[[UNC]], i64 0, i64 1
+  // PGOUSE: br {{.*}} !prof ![[UN1:[0-9]+]]
+  if (i)
+    throw i;
+
+  // PGOGEN: store {{.*}} @[[UNC]], i64 0, i64 2
+  // Since we never reach here, the weights should all be zero (and skipped)
+  // PGOUSE-NOT: br {{.*}} !prof !{{.*}}
+  if (i) {}
+}
+
 // PGOUSE-DAG: ![[TH1]] = !{!"branch_weights", i32 101, i32 2}
 // PGOUSE-DAG: ![[TH2]] = !{!"branch_weights", i32 67, i32 35}
 // PGOUSE-DAG: ![[TH3]] = !{!"branch_weights", i32 34, i32 34}
 // PGOUSE-DAG: ![[TH4]] = !{!"branch_weights", i32 18, i32 18}
 // PGOUSE-EXC: ![[TH5]] = !{!"branch_weights", i32 34, i32 18}
 // PGOUSE-DAG: ![[TH6]] = !{!"branch_weights", i32 101, i32 1}
+// PGOUSE-DAG: ![[UN1]] = !{!"branch_weights", i32 2, i32 1}
 
 int main(int argc, const char *argv[]) {
   throws();
+  try {
+    unreachable(1);
+  } catch (int) {}
   return 0;
 }





More information about the cfe-commits mailing list