[PATCH] D77502: [clang][CodeGen] Handle throw expression in conditional operator constant folding

Raul Tambre via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 5 07:59:58 PDT 2020


tambre created this revision.
tambre added a reviewer: rsmith.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
tambre edited the summary of this revision.

We're smart and do constant folding when emitting conditional operators.
Thus we emit the live value as a lvalue. This doesn't work if the live value is a throw expression.
Handle this by emitting the throw and returning the dead value as the lvalue.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77502

Files:
  clang/lib/CodeGen/CGExpr.cpp
  clang/test/CodeGenCXX/throw-expressions.cpp


Index: clang/test/CodeGenCXX/throw-expressions.cpp
===================================================================
--- clang/test/CodeGenCXX/throw-expressions.cpp
+++ clang/test/CodeGenCXX/throw-expressions.cpp
@@ -79,6 +79,13 @@
   // CHECK-NOT: call {{.*}}@_ZN6DR15601AD1Ev
   // CHECK: call {{.*}} @__cxa_atexit({{.*}} @_ZN6DR15601AD1Ev {{.*}} @_ZGRN6DR15601rE
   // CHECK-NOT: call {{.*}}@_ZN6DR15601AD1Ev
+
+  // PR28184
+  void conditional_throw()
+  {
+    int a, b;
+    (true ? throw 0 : a) = 0;
+  }
 }
 
 // CHECK-LABEL: define void @_Z5test7b(
Index: clang/lib/CodeGen/CGExpr.cpp
===================================================================
--- clang/lib/CodeGen/CGExpr.cpp
+++ clang/lib/CodeGen/CGExpr.cpp
@@ -4331,6 +4331,11 @@
       // If the true case is live, we need to track its region.
       if (CondExprBool)
         incrementProfileCounter(expr);
+      // If a throw expression we need to return dead as lvalue.
+      if (auto *ThrowExpr = dyn_cast<CXXThrowExpr>(live->IgnoreParens())) {
+        EmitCXXThrowExpr(ThrowExpr);
+        return EmitLValue(dead);
+      }
       return EmitLValue(live);
     }
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77502.255166.patch
Type: text/x-patch
Size: 1151 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200405/b56bcceb/attachment.bin>


More information about the cfe-commits mailing list