diff options
author | Alex Lorenz <arphaman@gmail.com> | 2017-08-25 10:07:00 +0000 |
---|---|---|
committer | Alex Lorenz <arphaman@gmail.com> | 2017-08-25 10:07:00 +0000 |
commit | 6cc8317c38b8fd9b4debeabf5d28aa128bb61160 (patch) | |
tree | 30e22078c371f657330e03a0335151bbc9cf6f56 /clang/lib/CodeGen | |
parent | bc3e993c1f4c2fe229bf69d9984ac8d1f7a86146 (diff) | |
download | bcm5719-llvm-6cc8317c38b8fd9b4debeabf5d28aa128bb61160.tar.gz bcm5719-llvm-6cc8317c38b8fd9b4debeabf5d28aa128bb61160.zip |
[IRGen] Evaluate constant static variables referenced through member
expressions
C++ allows us to reference static variables through member expressions. Prior to
this commit, non-integer static variables that were referenced using a member
expression were always emitted using lvalue loads. The old behaviour introduced
an inconsistency between regular uses of static variables and member expressions
uses. For example, the following program compiled and linked successfully:
struct Foo {
constexpr static const char *name = "foo";
};
int main() {
return Foo::name[0] == 'f';
}
but this program failed to link because "Foo::name" wasn't found:
struct Foo {
constexpr static const char *name = "foo";
};
int main() {
Foo f;
return f.name[0] == 'f';
}
This commit ensures that constant static variables referenced through member
expressions are emitted in the same way as ordinary static variable references.
rdar://33942261
Differential Revision: https://reviews.llvm.org/D36876
llvm-svn: 311772
Diffstat (limited to 'clang/lib/CodeGen')
-rw-r--r-- | clang/lib/CodeGen/CGExpr.cpp | 27 | ||||
-rw-r--r-- | clang/lib/CodeGen/CGExprAgg.cpp | 19 | ||||
-rw-r--r-- | clang/lib/CodeGen/CGExprComplex.cpp | 31 | ||||
-rw-r--r-- | clang/lib/CodeGen/CGExprScalar.cpp | 33 | ||||
-rw-r--r-- | clang/lib/CodeGen/CodeGenFunction.h | 1 |
5 files changed, 67 insertions, 44 deletions
diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 93ca6011e16..5f8f34e9bcc 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -1339,6 +1339,25 @@ CodeGenFunction::tryEmitAsConstant(DeclRefExpr *refExpr) { return ConstantEmission::forValue(C); } +static DeclRefExpr *tryToConvertMemberExprToDeclRefExpr(CodeGenFunction &CGF, + const MemberExpr *ME) { + if (auto *VD = dyn_cast<VarDecl>(ME->getMemberDecl())) { + // Try to emit static variable member expressions as DREs. + return DeclRefExpr::Create( + CGF.getContext(), NestedNameSpecifierLoc(), SourceLocation(), VD, + /*RefersToEnclosingVariableOrCapture=*/false, ME->getExprLoc(), + ME->getType(), ME->getValueKind()); + } + return nullptr; +} + +CodeGenFunction::ConstantEmission +CodeGenFunction::tryEmitAsConstant(const MemberExpr *ME) { + if (DeclRefExpr *DRE = tryToConvertMemberExprToDeclRefExpr(*this, ME)) + return tryEmitAsConstant(DRE); + return ConstantEmission(); +} + llvm::Value *CodeGenFunction::EmitLoadOfScalar(LValue lvalue, SourceLocation Loc) { return EmitLoadOfScalar(lvalue.getAddress(), lvalue.isVolatile(), @@ -3540,6 +3559,11 @@ EmitExtVectorElementExpr(const ExtVectorElementExpr *E) { } LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { + if (DeclRefExpr *DRE = tryToConvertMemberExprToDeclRefExpr(*this, E)) { + EmitIgnoredExpr(E->getBase()); + return EmitDeclRefLValue(DRE); + } + Expr *BaseExpr = E->getBase(); // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. LValue BaseLV; @@ -3566,9 +3590,6 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { return LV; } - if (auto *VD = dyn_cast<VarDecl>(ND)) - return EmitGlobalVarDeclLValue(*this, E, VD); - if (const auto *FD = dyn_cast<FunctionDecl>(ND)) return EmitFunctionDeclLValue(*this, E, FD); diff --git a/clang/lib/CodeGen/CGExprAgg.cpp b/clang/lib/CodeGen/CGExprAgg.cpp index a05a088f091..1ab8433864c 100644 --- a/clang/lib/CodeGen/CGExprAgg.cpp +++ b/clang/lib/CodeGen/CGExprAgg.cpp @@ -124,24 +124,7 @@ public: } // l-values. - void VisitDeclRefExpr(DeclRefExpr *E) { - // For aggregates, we should always be able to emit the variable - // as an l-value unless it's a reference. This is due to the fact - // that we can't actually ever see a normal l2r conversion on an - // aggregate in C++, and in C there's no language standard - // actively preventing us from listing variables in the captures - // list of a block. - if (E->getDecl()->getType()->isReferenceType()) { - if (CodeGenFunction::ConstantEmission result - = CGF.tryEmitAsConstant(E)) { - EmitFinalDestCopy(E->getType(), result.getReferenceLValue(CGF, E)); - return; - } - } - - EmitAggLoadOfLValue(E); - } - + void VisitDeclRefExpr(DeclRefExpr *E) { EmitAggLoadOfLValue(E); } void VisitMemberExpr(MemberExpr *ME) { EmitAggLoadOfLValue(ME); } void VisitUnaryDeref(UnaryOperator *E) { EmitAggLoadOfLValue(E); } void VisitStringLiteral(StringLiteral *E) { EmitAggLoadOfLValue(E); } diff --git a/clang/lib/CodeGen/CGExprComplex.cpp b/clang/lib/CodeGen/CGExprComplex.cpp index 980972370dc..f019e3586e3 100644 --- a/clang/lib/CodeGen/CGExprComplex.cpp +++ b/clang/lib/CodeGen/CGExprComplex.cpp @@ -120,18 +120,22 @@ public: return Visit(E->getSubExpr()); } + ComplexPairTy emitConstant(const CodeGenFunction::ConstantEmission &Constant, + Expr *E) { + assert(Constant && "not a constant"); + if (Constant.isReference()) + return EmitLoadOfLValue(Constant.getReferenceLValue(CGF, E), + E->getExprLoc()); + + llvm::Constant *pair = Constant.getValue(); + return ComplexPairTy(pair->getAggregateElement(0U), + pair->getAggregateElement(1U)); + } // l-values. ComplexPairTy VisitDeclRefExpr(DeclRefExpr *E) { - if (CodeGenFunction::ConstantEmission result = CGF.tryEmitAsConstant(E)) { - if (result.isReference()) - return EmitLoadOfLValue(result.getReferenceLValue(CGF, E), - E->getExprLoc()); - - llvm::Constant *pair = result.getValue(); - return ComplexPairTy(pair->getAggregateElement(0U), - pair->getAggregateElement(1U)); - } + if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(E)) + return emitConstant(Constant, E); return EmitLoadOfLValue(E); } ComplexPairTy VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { @@ -141,7 +145,14 @@ public: return CGF.EmitObjCMessageExpr(E).getComplexVal(); } ComplexPairTy VisitArraySubscriptExpr(Expr *E) { return EmitLoadOfLValue(E); } - ComplexPairTy VisitMemberExpr(const Expr *E) { return EmitLoadOfLValue(E); } + ComplexPairTy VisitMemberExpr(MemberExpr *ME) { + if (CodeGenFunction::ConstantEmission Constant = + CGF.tryEmitAsConstant(ME)) { + CGF.EmitIgnoredExpr(ME->getBase()); + return emitConstant(Constant, ME); + } + return EmitLoadOfLValue(ME); + } ComplexPairTy VisitOpaqueValueExpr(OpaqueValueExpr *E) { if (E->isGLValue()) return EmitLoadOfLValue(CGF.getOpaqueLValueMapping(E), E->getExprLoc()); diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 1170b014ec7..46a01a3bb8b 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -428,14 +428,19 @@ public: return CGF.getOpaqueRValueMapping(E).getScalarVal(); } + Value *emitConstant(const CodeGenFunction::ConstantEmission &Constant, + Expr *E) { + assert(Constant && "not a constant"); + if (Constant.isReference()) + return EmitLoadOfLValue(Constant.getReferenceLValue(CGF, E), + E->getExprLoc()); + return Constant.getValue(); + } + // l-values. Value *VisitDeclRefExpr(DeclRefExpr *E) { - if (CodeGenFunction::ConstantEmission result = CGF.tryEmitAsConstant(E)) { - if (result.isReference()) - return EmitLoadOfLValue(result.getReferenceLValue(CGF, E), - E->getExprLoc()); - return result.getValue(); - } + if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(E)) + return emitConstant(Constant, E); return EmitLoadOfLValue(E); } @@ -1299,13 +1304,15 @@ Value *ScalarExprEmitter::VisitConvertVectorExpr(ConvertVectorExpr *E) { } Value *ScalarExprEmitter::VisitMemberExpr(MemberExpr *E) { - llvm::APSInt Value; - if (E->EvaluateAsInt(Value, CGF.getContext(), Expr::SE_AllowSideEffects)) { - if (E->isArrow()) - CGF.EmitScalarExpr(E->getBase()); - else - EmitLValue(E->getBase()); - return Builder.getInt(Value); + if (CodeGenFunction::ConstantEmission Constant = CGF.tryEmitAsConstant(E)) { + CGF.EmitIgnoredExpr(E->getBase()); + return emitConstant(Constant, E); + } else { + llvm::APSInt Value; + if (E->EvaluateAsInt(Value, CGF.getContext(), Expr::SE_AllowSideEffects)) { + CGF.EmitIgnoredExpr(E->getBase()); + return Builder.getInt(Value); + } } return EmitLoadOfLValue(E); diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index e334c315b8e..28253ff9852 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -3161,6 +3161,7 @@ public: }; ConstantEmission tryEmitAsConstant(DeclRefExpr *refExpr); + ConstantEmission tryEmitAsConstant(const MemberExpr *ME); RValue EmitPseudoObjectRValue(const PseudoObjectExpr *e, AggValueSlot slot = AggValueSlot::ignored()); |