[cfe-commits] r72428 - in /cfe/trunk: include/clang/AST/Decl.h lib/AST/Decl.cpp lib/AST/Expr.cpp lib/AST/ExprConstant.cpp lib/AST/StmtIterator.cpp lib/Frontend/PCHReaderDecl.cpp lib/Sema/SemaDecl.cpp lib/Sema/SemaDeclCXX.cpp

Douglas Gregor dgregor at apple.com
Tue May 26 11:54:04 PDT 2009


Author: dgregor
Date: Tue May 26 13:54:04 2009
New Revision: 72428

URL: http://llvm.org/viewvc/llvm-project?rev=72428&view=rev
Log:
When evaluating a VarDecl as a constant or determining whether it is
an integral constant expression, maintain a cache of the value and the
is-an-ICE flag within the VarDecl itself. This eliminates
exponential-time behavior of the Fibonacci template metaprogram.


Modified:
    cfe/trunk/include/clang/AST/Decl.h
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/AST/ExprConstant.cpp
    cfe/trunk/lib/AST/StmtIterator.cpp
    cfe/trunk/lib/Frontend/PCHReaderDecl.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp

Modified: cfe/trunk/include/clang/AST/Decl.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Decl.h?rev=72428&r1=72427&r2=72428&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Decl.h (original)
+++ cfe/trunk/include/clang/AST/Decl.h Tue May 26 13:54:04 2009
@@ -14,6 +14,7 @@
 #ifndef LLVM_CLANG_AST_DECL_H
 #define LLVM_CLANG_AST_DECL_H
 
+#include "clang/AST/APValue.h"
 #include "clang/AST/DeclBase.h"
 #include "clang/AST/DeclarationName.h"
 #include "clang/AST/ExternalASTSource.h"
@@ -186,6 +187,27 @@
   static bool classof(const ValueDecl *D) { return true; }
 };
 
+/// \brief Structure used to store a statement, the constant value to
+/// which it was evaluated (if any), and whether or not the statement
+/// is an integral constant expression (if known).
+struct EvaluatedStmt {
+  EvaluatedStmt() : WasEvaluated(false), CheckedICE(false), IsICE(false) { }
+
+  /// \brief Whether this statement was already evaluated.
+  bool WasEvaluated : 1;
+
+  /// \brief Whether we already checked whether this statement was an
+  /// integral constant expression.
+  bool CheckedICE : 1;
+
+  /// \brief Whether this statement is an integral constant
+  /// expression. Only valid if CheckedICE is true.
+  bool IsICE : 1;
+
+  Stmt *Value;
+  APValue Evaluated;
+};
+
 /// VarDecl - An instance of this class is created to represent a variable
 /// declaration or definition.
 class VarDecl : public ValueDecl {
@@ -201,7 +223,7 @@
   static const char *getStorageClassSpecifierString(StorageClass SC);
 
 private:
-  Stmt *Init;
+  mutable llvm::PointerUnion<Stmt *, EvaluatedStmt *> Init;
   // FIXME: This can be packed into the bitfields in Decl.
   unsigned SClass : 3;
   bool ThreadSpecified : 1;
@@ -220,7 +242,7 @@
 protected:
   VarDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
           QualType T, StorageClass SC, SourceLocation TSSL = SourceLocation())
-    : ValueDecl(DK, DC, L, Id, T), Init(0),
+    : ValueDecl(DK, DC, L, Id, T), Init(),
       ThreadSpecified(false), HasCXXDirectInit(false),
       DeclaredInCondition(false), PreviousDeclaration(0), 
       TypeSpecStartLoc(TSSL) { 
@@ -243,10 +265,95 @@
     TypeSpecStartLoc = SL;
   }
 
-  const Expr *getInit() const { return (const Expr*) Init; }
-  Expr *getInit() { return (Expr*) Init; }
-  void setInit(Expr *I) { Init = (Stmt*) I; }
-      
+  const Expr *getInit() const { 
+    if (Init.isNull())
+      return 0;
+
+    const Stmt *S = Init.dyn_cast<Stmt *>();
+    if (!S)
+      S = Init.get<EvaluatedStmt *>()->Value;
+
+    return (const Expr*) S; 
+  }
+  Expr *getInit() { 
+    if (Init.isNull())
+      return 0;
+
+    Stmt *S = Init.dyn_cast<Stmt *>();
+    if (!S)
+      S = Init.get<EvaluatedStmt *>()->Value;
+
+    return (Expr*) S; 
+  }
+
+  /// \brief Retrieve the address of the initializer expression.
+  Stmt **getInitAddress() {
+    if (Init.is<Stmt *>())
+      return reinterpret_cast<Stmt **>(&Init); // FIXME: ugly hack
+    return &Init.get<EvaluatedStmt *>()->Value;
+  }
+
+  void setInit(ASTContext &C, Expr *I);
+   
+  /// \brief Note that constant evaluation has computed the given
+  /// value for this variable's initializer.
+  void setEvaluatedValue(ASTContext &C, const APValue &Value) const {
+    EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
+    if (!Eval) {
+      Stmt *S = Init.get<Stmt *>();
+      Eval = new (C) EvaluatedStmt;
+      Eval->Value = S;
+      Init = Eval;
+    }
+
+    Eval->WasEvaluated = true;
+    Eval->Evaluated = Value;
+  }
+   
+  /// \brief Return the already-evaluated value of this variable's
+  /// initializer, or NULL if the value is not yet known.
+  APValue *getEvaluatedValue() const {
+    if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
+      if (Eval->WasEvaluated)
+        return &Eval->Evaluated;
+
+    return 0;
+  }
+
+  /// \brief Determines whether it is already known whether the
+  /// initializer is an integral constant expression or not.
+  bool isInitKnownICE() const {
+    if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
+      return Eval->CheckedICE;
+
+    return false;
+  }
+
+  /// \brief Determines whether the initializer is an integral
+  /// constant expression.
+  ///
+  /// \pre isInitKnownICE()
+  bool isInitICE() const {
+    assert(isInitKnownICE() && 
+           "Check whether we already know that the initializer is an ICE");
+    return Init.get<EvaluatedStmt *>()->IsICE;
+  }
+
+  /// \brief Note that we now know whether the initializer is an
+  /// integral constant expression.
+  void setInitKnownICE(ASTContext &C, bool IsICE) const {
+    EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
+    if (!Eval) {
+      Stmt *S = Init.get<Stmt *>();
+      Eval = new (C) EvaluatedStmt;
+      Eval->Value = S;
+      Init = Eval;
+    }
+
+    Eval->CheckedICE = true;
+    Eval->IsICE = IsICE;
+  }
+
   /// \brief Retrieve the definition of this variable, which may come
   /// from a previous declaration. Def will be set to the VarDecl that
   /// contains the initializer, and the result will be that

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=72428&r1=72427&r2=72428&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Tue May 26 13:54:04 2009
@@ -89,6 +89,15 @@
   return getType();
 }
 
+void VarDecl::setInit(ASTContext &C, Expr *I) { 
+    if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>()) {
+      Eval->~EvaluatedStmt();
+      C.Deallocate(Eval);
+    }
+
+    Init = I;
+  }
+
 bool VarDecl::isExternC(ASTContext &Context) const {
   if (!Context.getLangOptions().CPlusPlus)
     return (getDeclContext()->isTranslationUnit() && 
@@ -287,8 +296,13 @@
 
 void VarDecl::Destroy(ASTContext& C) {
   Expr *Init = getInit();
-  if (Init)
+  if (Init) {
     Init->Destroy(C);
+    if (EvaluatedStmt *Eval = this->Init.dyn_cast<EvaluatedStmt *>()) {
+      Eval->~EvaluatedStmt();
+      C.Deallocate(Eval);
+    }
+  }
   this->~VarDecl();
   C.Deallocate((void *)this);
 }

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=72428&r1=72427&r2=72428&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Tue May 26 13:54:04 2009
@@ -1210,8 +1210,21 @@
       //   type initialized by an ICE can be used in ICEs.
       if (const VarDecl *Dcl =
               dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) {
-        if (const Expr *Init = Dcl->getInit())
-          return CheckICE(Init, Ctx);
+        if (Dcl->isInitKnownICE()) {
+          // We have already checked whether this subexpression is an
+          // integral constant expression.
+          if (Dcl->isInitICE())
+            return NoDiag();
+          else
+            return ICEDiag(2, E->getLocStart());
+        }
+
+        if (const Expr *Init = Dcl->getInit()) {
+          ICEDiag Result = CheckICE(Init, Ctx);
+          // Cache the result of the ICE test.
+          Dcl->setInitKnownICE(Ctx, Result.Val == 0);
+          return Result;
+        }
       }
     }
     return ICEDiag(2, E->getLocStart());

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=72428&r1=72427&r2=72428&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Tue May 26 13:54:04 2009
@@ -700,8 +700,17 @@
   // In C, they can also be folded, although they are not ICEs.
   if (E->getType().getCVRQualifiers() == QualType::Const) {
     if (const VarDecl *D = dyn_cast<VarDecl>(E->getDecl())) {
-      if (const Expr *Init = D->getInit())
-        return Visit(const_cast<Expr*>(Init));
+      if (APValue *V = D->getEvaluatedValue())
+        return Success(V->getInt(), E);
+      if (const Expr *Init = D->getInit()) {
+        if (Visit(const_cast<Expr*>(Init))) {
+          // Cache the evaluated value in the variable declaration.
+          D->setEvaluatedValue(Info.Ctx, Result);
+          return true;
+        }
+
+        return false;
+      }
     }
   }
 

Modified: cfe/trunk/lib/AST/StmtIterator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/StmtIterator.cpp?rev=72428&r1=72427&r2=72428&view=diff

==============================================================================
--- cfe/trunk/lib/AST/StmtIterator.cpp (original)
+++ cfe/trunk/lib/AST/StmtIterator.cpp Tue May 26 13:54:04 2009
@@ -140,14 +140,14 @@
   
   if (inDeclGroup()) {
     VarDecl* VD = cast<VarDecl>(*DGI);
-    return VD->Init;
+    return *VD->getInitAddress();
   }
   
   assert (inDecl());
 
   if (VarDecl* VD = dyn_cast<VarDecl>(decl)) {
     assert (VD->Init);
-    return VD->Init;
+    return *VD->getInitAddress();
   }
 
   EnumConstantDecl* ECD = cast<EnumConstantDecl>(decl);

Modified: cfe/trunk/lib/Frontend/PCHReaderDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PCHReaderDecl.cpp?rev=72428&r1=72427&r2=72428&view=diff

==============================================================================
--- cfe/trunk/lib/Frontend/PCHReaderDecl.cpp (original)
+++ cfe/trunk/lib/Frontend/PCHReaderDecl.cpp Tue May 26 13:54:04 2009
@@ -342,7 +342,7 @@
                          cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
   VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
   if (Record[Idx++])
-    VD->setInit(Reader.ReadDeclExpr());
+    VD->setInit(*Reader.getContext(), Reader.ReadDeclExpr());
 }
 
 void PCHDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=72428&r1=72427&r2=72428&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Tue May 26 13:54:04 2009
@@ -2581,7 +2581,7 @@
     // };
 
     // Attach the initializer
-    VDecl->setInit(Init);
+    VDecl->setInit(Context, Init);
 
     // C++ [class.mem]p4:
     //   A member-declarator can contain a constant-initializer only
@@ -2644,7 +2644,7 @@
   }
     
   // Attach the initializer to the decl.
-  VDecl->setInit(Init);
+  VDecl->setInit(Context, Init);
 
   // If the previous declaration of VDecl was a tentative definition,
   // remove it from the set of tentative definitions.

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=72428&r1=72427&r2=72428&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Tue May 26 13:54:04 2009
@@ -1776,7 +1776,7 @@
                                         Expr **Exprs, unsigned NumExprs) {
   Expr *Temp = CXXConstructExpr::Create(Context, VD, DeclInitType, Constructor, 
                                         false, Exprs, NumExprs);
-  VD->setInit(Temp);
+  VD->setInit(Context, Temp);
 }
 
 /// AddCXXDirectInitializerToDecl - This action is called immediately after 





More information about the cfe-commits mailing list