[clang] ea25966 - [clang][Interp][NFC] Simplify generated code for references

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 25 23:55:31 PDT 2022


Author: Timm Bäder
Date: 2022-10-26T08:49:11+02:00
New Revision: ea2596692f5f15e911020c0779c7ee0df51382a9

URL: https://github.com/llvm/llvm-project/commit/ea2596692f5f15e911020c0779c7ee0df51382a9
DIFF: https://github.com/llvm/llvm-project/commit/ea2596692f5f15e911020c0779c7ee0df51382a9.diff

LOG: [clang][Interp][NFC] Simplify generated code for references

Instead of getting a pointer to a pointer to a value, followed by
dereferencing once, leaving us with a pointer to a value, we can instead
just get the pointer to the value (the reference in question) directly.
This simplifies (and shrinks) the generated bytecode somewhat.

Added: 
    

Modified: 
    clang/lib/AST/Interp/ByteCodeExprGen.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 64bd5172b7f8..9472f1aed917 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -1093,25 +1093,28 @@ bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
 template <class Emitter>
 bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
   const auto *Decl = E->getDecl();
-  bool FoundDecl = false;
+  // References are implemented via pointers, so when we see a DeclRefExpr
+  // pointing to a reference, we need to get its value directly (i.e. the
+  // pointer to the actual value) instead of a pointer to the pointer to the
+  // value.
+  bool IsReference = Decl->getType()->isReferenceType();
 
   if (auto It = Locals.find(Decl); It != Locals.end()) {
     const unsigned Offset = It->second.Offset;
-    if (!this->emitGetPtrLocal(Offset, E))
-      return false;
 
-    FoundDecl = true;
+    if (IsReference)
+      return this->emitGetLocal(PT_Ptr, Offset, E);
+    return this->emitGetPtrLocal(Offset, E);
   } else if (auto GlobalIndex = P.getGlobal(Decl)) {
-    if (!this->emitGetPtrGlobal(*GlobalIndex, E))
-      return false;
+    if (IsReference)
+      return this->emitGetGlobal(PT_Ptr, *GlobalIndex, E);
 
-    FoundDecl = true;
+    return this->emitGetPtrGlobal(*GlobalIndex, E);
   } else if (const auto *PVD = dyn_cast<ParmVarDecl>(Decl)) {
     if (auto It = this->Params.find(PVD); It != this->Params.end()) {
-      if (!this->emitGetPtrParam(It->second, E))
-        return false;
-
-      FoundDecl = true;
+      if (IsReference)
+        return this->emitGetParam(PT_Ptr, It->second, E);
+      return this->emitGetPtrParam(It->second, E);
     }
   } else if (const auto *ECD = dyn_cast<EnumConstantDecl>(Decl)) {
     PrimType T = *classify(ECD->getType());
@@ -1119,17 +1122,6 @@ bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
     return this->emitConst(T, ECD->getInitVal(), E);
   }
 
-  // References are implemented using pointers, so when we get here,
-  // we have a pointer to a pointer, which we need to de-reference once.
-  if (FoundDecl) {
-    if (Decl->getType()->isReferenceType()) {
-      if (!this->emitLoadPopPtr(E))
-        return false;
-    }
-
-    return true;
-  }
-
   return false;
 }
 


        


More information about the cfe-commits mailing list