[cfe-commits] r89934 - /cfe/trunk/lib/CodeGen/CGExprScalar.cpp

Eli Friedman eli.friedman at gmail.com
Wed Nov 25 19:22:21 PST 2009


Author: efriedma
Date: Wed Nov 25 21:22:21 2009
New Revision: 89934

URL: http://llvm.org/viewvc/llvm-project?rev=89934&view=rev
Log:
Use the more general Evaluate infrastructure for folding constant DeclRefs
instead of checking explicitly for an EnumConstantDecl.  This folds references
to constant integer VarDecls, which has two benefits:

1. Slightly smaller emitted code from emitting a constant instead of a load,
and skipping emitting some constant globals.
2. Some code forgets to define static constant member variables; emitting a
load instead of the value in that case leads to a link error. (Such programs
are technically not well-formed, but in practice build with g++.)


Modified:
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=89934&r1=89933&r2=89934&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Wed Nov 25 21:22:21 2009
@@ -141,8 +141,11 @@
 
   // l-values.
   Value *VisitDeclRefExpr(DeclRefExpr *E) {
-    if (const EnumConstantDecl *EC = dyn_cast<EnumConstantDecl>(E->getDecl()))
-      return llvm::ConstantInt::get(VMContext, EC->getInitVal());
+    Expr::EvalResult Result;
+    if (E->Evaluate(Result, CGF.getContext()) && Result.Val.isInt()) {
+      assert(!Result.HasSideEffects && "Constant declref with side-effect?!");
+      return llvm::ConstantInt::get(VMContext, Result.Val.getInt());
+    }
     return EmitLoadOfLValue(E);
   }
   Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
@@ -167,21 +170,7 @@
 
   Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E);
   Value *VisitShuffleVectorExpr(ShuffleVectorExpr *E);
-  Value *VisitMemberExpr(MemberExpr *E) { 
-    if (const EnumConstantDecl *EC = 
-          dyn_cast<EnumConstantDecl>(E->getMemberDecl())) {
-      
-      // We still need to emit the base.
-      if (E->isArrow())
-        CGF.EmitScalarExpr(E->getBase());
-      else
-        CGF.EmitLValue(E->getBase());
-      return llvm::ConstantInt::get(VMContext, EC->getInitVal());
-    }
-    
-    return EmitLoadOfLValue(E);
-  }
-    
+  Value *VisitMemberExpr(MemberExpr *E);
   Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); }
   Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
     return EmitLoadOfLValue(E);
@@ -572,6 +561,17 @@
   Value* SV = llvm::ConstantVector::get(indices.begin(), indices.size());
   return Builder.CreateShuffleVector(V1, V2, SV, "shuffle");
 }
+Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) {
+  Expr::EvalResult Result;
+  if (E->Evaluate(Result, CGF.getContext()) && Result.Val.isInt()) {
+    if (E->isArrow())
+      CGF.EmitScalarExpr(E->getBase());
+    else
+      EmitLValue(E->getBase());
+    return llvm::ConstantInt::get(VMContext, Result.Val.getInt());
+  }
+  return EmitLoadOfLValue(E);
+}
 
 Value *ScalarExprEmitter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) {
   TestAndClearIgnoreResultAssign();





More information about the cfe-commits mailing list