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.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@72428 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/Expr.cpp b/lib/AST/Expr.cpp
index 6711faf..aca5efe 100644
--- a/lib/AST/Expr.cpp
+++ b/lib/AST/Expr.cpp
@@ -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());