[cfe-commits] r116138 - in /cfe/trunk: lib/AST/ExprConstant.cpp lib/CodeGen/CGDebugInfo.cpp lib/CodeGen/CGDebugInfo.h lib/CodeGen/CGExprScalar.cpp lib/CodeGen/CodeGenFunction.cpp lib/CodeGen/CodeGenFunction.h test/CodeGenCXX/const-init.cpp
John McCall
rjmccall at apple.com
Fri Oct 8 18:34:31 PDT 2010
Author: rjmccall
Date: Fri Oct 8 20:34:31 2010
New Revision: 116138
URL: http://llvm.org/viewvc/llvm-project?rev=116138&view=rev
Log:
Permit constant evaluation of const floating-point variables with
constant initializers.
Modified:
cfe/trunk/lib/AST/ExprConstant.cpp
cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
cfe/trunk/lib/CodeGen/CGDebugInfo.h
cfe/trunk/lib/CodeGen/CGExprScalar.cpp
cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
cfe/trunk/lib/CodeGen/CodeGenFunction.h
cfe/trunk/test/CodeGenCXX/const-init.cpp
Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=116138&r1=116137&r2=116138&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Fri Oct 8 20:34:31 2010
@@ -1780,6 +1780,8 @@
bool VisitUnaryReal(const UnaryOperator *E);
bool VisitUnaryImag(const UnaryOperator *E);
+ bool VisitDeclRefExpr(const DeclRefExpr *E);
+
// FIXME: Missing: array subscript of vector, member of vector,
// ImplicitValueInitExpr
};
@@ -1867,6 +1869,45 @@
}
}
+bool FloatExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
+ const Decl *D = E->getDecl();
+ if (!isa<VarDecl>(D) || isa<ParmVarDecl>(D)) return false;
+ const VarDecl *VD = cast<VarDecl>(D);
+
+ // Require the qualifiers to be const and not volatile.
+ CanQualType T = Info.Ctx.getCanonicalType(E->getType());
+ if (!T.isConstQualified() || T.isVolatileQualified())
+ return false;
+
+ const Expr *Init = VD->getAnyInitializer();
+ if (!Init) return false;
+
+ if (APValue *V = VD->getEvaluatedValue()) {
+ if (V->isFloat()) {
+ Result = V->getFloat();
+ return true;
+ }
+ return false;
+ }
+
+ if (VD->isEvaluatingValue())
+ return false;
+
+ VD->setEvaluatingValue();
+
+ Expr::EvalResult InitResult;
+ if (Init->Evaluate(InitResult, Info.Ctx) && !InitResult.HasSideEffects &&
+ InitResult.Val.isFloat()) {
+ // Cache the evaluated value in the variable declaration.
+ Result = InitResult.Val.getFloat();
+ VD->setEvaluatedValue(InitResult.Val);
+ return true;
+ }
+
+ VD->setEvaluatedValue(APValue());
+ return false;
+}
+
bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
if (E->getSubExpr()->getType()->isAnyComplexType()) {
ComplexValue CV;
Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.cpp?rev=116138&r1=116137&r2=116138&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDebugInfo.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.cpp Fri Oct 8 20:34:31 2010
@@ -1941,7 +1941,7 @@
/// EmitGlobalVariable - Emit global variable's debug info.
void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
- llvm::ConstantInt *Init) {
+ llvm::Constant *Init) {
// Create the descriptor for the variable.
llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
llvm::StringRef Name = VD->getName();
Modified: cfe/trunk/lib/CodeGen/CGDebugInfo.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGDebugInfo.h?rev=116138&r1=116137&r2=116138&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGDebugInfo.h (original)
+++ cfe/trunk/lib/CodeGen/CGDebugInfo.h Fri Oct 8 20:34:31 2010
@@ -186,7 +186,7 @@
void EmitGlobalVariable(llvm::GlobalVariable *GV, ObjCInterfaceDecl *Decl);
/// EmitGlobalVariable - Emit global variable's debug info.
- void EmitGlobalVariable(const ValueDecl *VD, llvm::ConstantInt *Init);
+ void EmitGlobalVariable(const ValueDecl *VD, llvm::Constant *Init);
/// getOrCreateRecordType - Emit record type's standalone debug info.
llvm::DIType getOrCreateRecordType(QualType Ty, SourceLocation L);
Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=116138&r1=116137&r2=116138&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Fri Oct 8 20:34:31 2010
@@ -161,18 +161,29 @@
// l-values.
Value *VisitDeclRefExpr(DeclRefExpr *E) {
Expr::EvalResult Result;
- if (E->Evaluate(Result, CGF.getContext()) && Result.Val.isInt()) {
- assert(!Result.HasSideEffects && "Constant declref with side-effect?!");
- llvm::ConstantInt *CI
- = llvm::ConstantInt::get(VMContext, Result.Val.getInt());
- if (VarDecl *VD = dyn_cast<VarDecl>((E->getDecl()))) {
- if (!CGF.getContext().DeclMustBeEmitted(VD))
- CGF.EmitDeclRefExprDbgValue(E, CI);
- } else if (isa<EnumConstantDecl>(E->getDecl()))
- CGF.EmitDeclRefExprDbgValue(E, CI);
- return CI;
+ if (!E->Evaluate(Result, CGF.getContext()))
+ return EmitLoadOfLValue(E);
+
+ assert(!Result.HasSideEffects && "Constant declref with side-effect?!");
+
+ llvm::Constant *C;
+ if (Result.Val.isInt()) {
+ C = llvm::ConstantInt::get(VMContext, Result.Val.getInt());
+ } else if (Result.Val.isFloat()) {
+ C = llvm::ConstantFP::get(VMContext, Result.Val.getFloat());
+ } else {
+ return EmitLoadOfLValue(E);
}
- return EmitLoadOfLValue(E);
+
+ // Make sure we emit a debug reference to the global variable.
+ if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
+ if (!CGF.getContext().DeclMustBeEmitted(VD))
+ CGF.EmitDeclRefExprDbgValue(E, C);
+ } else if (isa<EnumConstantDecl>(E->getDecl())) {
+ CGF.EmitDeclRefExprDbgValue(E, C);
+ }
+
+ return C;
}
Value *VisitObjCSelectorExpr(ObjCSelectorExpr *E) {
return CGF.EmitObjCSelectorExpr(E);
Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.cpp?rev=116138&r1=116137&r2=116138&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.cpp Fri Oct 8 20:34:31 2010
@@ -1547,7 +1547,7 @@
}
void CodeGenFunction::EmitDeclRefExprDbgValue(const DeclRefExpr *E,
- llvm::ConstantInt *Init) {
+ llvm::Constant *Init) {
assert (Init && "Invalid DeclRefExpr initializer!");
if (CGDebugInfo *Dbg = getDebugInfo())
Dbg->EmitGlobalVariable(E->getDecl(), Init);
Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=116138&r1=116137&r2=116138&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Fri Oct 8 20:34:31 2010
@@ -1443,7 +1443,7 @@
LValue EmitStmtExprLValue(const StmtExpr *E);
LValue EmitPointerToDataMemberBinaryExpr(const BinaryOperator *E);
LValue EmitObjCSelectorLValue(const ObjCSelectorExpr *E);
- void EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::ConstantInt *Init);
+ void EmitDeclRefExprDbgValue(const DeclRefExpr *E, llvm::Constant *Init);
//===--------------------------------------------------------------------===//
// Scalar Expression Emission
//===--------------------------------------------------------------------===//
Modified: cfe/trunk/test/CodeGenCXX/const-init.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/const-init.cpp?rev=116138&r1=116137&r2=116138&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/const-init.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/const-init.cpp Fri Oct 8 20:34:31 2010
@@ -24,3 +24,15 @@
// CHECK: @_ZN6PR55812g0E = global %1 { i32 1 }
C g0 = { C::e1 };
}
+
+namespace test2 {
+ struct A {
+ static const double d = 1.0;
+ static const float f = d / 2;
+ };
+
+ // CHECK: @_ZN5test22t0E = global double 1.000000e+00, align 8
+ // CHECK: @_ZN5test22t1E = global [2 x double] [double 1.000000e+00, double 5.000000e-01], align 16
+ double t0 = A::d;
+ double t1[] = { A::d, A::f };
+}
More information about the cfe-commits
mailing list