[PATCH] D151572: [clang][ConstantEmitter] have tryEmitPrivate try ConstExprEmitter fast-path first

Nick Desaulniers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri May 26 10:42:52 PDT 2023


nickdesaulniers created this revision.
Herald added a project: All.
nickdesaulniers requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

As suggested by @efriedma in:
https://reviews.llvm.org/D76096#4370369

Some minor code style fixes as well in
ConstantEmitter::tryEmitPrivateForVarInit.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151572

Files:
  clang/lib/CodeGen/CGExprConstant.cpp


Index: clang/lib/CodeGen/CGExprConstant.cpp
===================================================================
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -1656,9 +1656,8 @@
 
   // Try to emit the initializer.  Note that this can allow some things that
   // are not allowed by tryEmitPrivateForMemory alone.
-  if (auto value = D.evaluateValue()) {
+  if (APValue *value = D.evaluateValue())
     return tryEmitPrivateForMemory(*value, destType);
-  }
 
   // FIXME: Implement C++11 [basic.start.init]p2: if the initializer of a
   // reference is a constant expression, and the reference binds to a temporary,
@@ -1672,10 +1671,10 @@
   const Expr *E = D.getInit();
   assert(E && "No initializer to emit");
 
-  auto nonMemoryDestType = getNonMemoryType(CGM, destType);
-  auto C =
-    ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), nonMemoryDestType);
-  return (C ? emitForMemory(C, destType) : nullptr);
+  QualType nonMemoryDestType = getNonMemoryType(CGM, destType);
+  llvm::Constant *C =
+      ConstExprEmitter(*this).Visit(const_cast<Expr *>(E), nonMemoryDestType);
+  return C ? emitForMemory(C, destType) : nullptr;
 }
 
 llvm::Constant *
@@ -1742,6 +1741,10 @@
                                                 QualType destType) {
   assert(!destType->isVoidType() && "can't emit a void constant");
 
+  if (llvm::Constant *C =
+          ConstExprEmitter(*this).Visit(const_cast<Expr *>(E), destType))
+    return C;
+
   Expr::EvalResult Result;
 
   bool Success = false;
@@ -1751,13 +1754,10 @@
   else
     Success = E->EvaluateAsRValue(Result, CGM.getContext(), InConstantContext);
 
-  llvm::Constant *C;
   if (Success && !Result.HasSideEffects)
-    C = tryEmitPrivate(Result.Val, destType);
-  else
-    C = ConstExprEmitter(*this).Visit(const_cast<Expr*>(E), destType);
+    return tryEmitPrivate(Result.Val, destType);
 
-  return C;
+  return nullptr;
 }
 
 llvm::Constant *CodeGenModule::getNullPointer(llvm::PointerType *T, QualType QT) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151572.526120.patch
Type: text/x-patch
Size: 2018 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230526/3e9d153c/attachment.bin>


More information about the cfe-commits mailing list