[cfe-commits] r62455 - in /cfe/trunk: lib/AST/APValue.cpp lib/AST/Expr.cpp lib/AST/ExprConstant.cpp lib/Sema/SemaDecl.cpp lib/Sema/SemaExpr.cpp test/CodeGen/ext-vector.c
Nate Begeman
natebegeman at mac.com
Sat Jan 17 19:20:47 PST 2009
Author: sampo
Date: Sat Jan 17 21:20:47 2009
New Revision: 62455
URL: http://llvm.org/viewvc/llvm-project?rev=62455&view=rev
Log:
Support evaluation of vector constant expressions, and codegen of same.
Modified:
cfe/trunk/lib/AST/APValue.cpp
cfe/trunk/lib/AST/Expr.cpp
cfe/trunk/lib/AST/ExprConstant.cpp
cfe/trunk/lib/Sema/SemaDecl.cpp
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/test/CodeGen/ext-vector.c
Modified: cfe/trunk/lib/AST/APValue.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/APValue.cpp?rev=62455&r1=62454&r2=62455&view=diff
==============================================================================
--- cfe/trunk/lib/AST/APValue.cpp (original)
+++ cfe/trunk/lib/AST/APValue.cpp Sat Jan 17 21:20:47 2009
@@ -91,7 +91,9 @@
OS << "Float: " << GetApproxValue(getFloat());
return;
case Vector:
- OS << "Vector: <todo>";
+ OS << "Vector: " << getVectorElt(0);
+ for (unsigned i = 1; i != getVectorLength(); ++i)
+ OS << ", " << getVectorElt(i);
return;
case ComplexInt:
OS << "ComplexInt: " << getComplexIntReal() << ", " << getComplexIntImag();
Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=62455&r1=62454&r2=62455&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Sat Jan 17 21:20:47 2009
@@ -487,6 +487,7 @@
// An assignment expression [...] is not an lvalue.
return LV_InvalidExpression;
}
+ // FIXME: OverloadExprClass
case CallExprClass:
case CXXOperatorCallExprClass:
case CXXMemberCallExprClass: {
@@ -701,6 +702,10 @@
break;
case StringLiteralClass:
return true;
+ case CompoundLiteralExprClass: {
+ const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer();
+ return Exp->isConstantExpr(Ctx, Loc);
+ }
case InitListExprClass: {
const InitListExpr *Exp = cast<InitListExpr>(this);
unsigned numInits = Exp->getNumInits();
Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=62455&r1=62454&r2=62455&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Sat Jan 17 21:20:47 2009
@@ -318,6 +318,78 @@
}
//===----------------------------------------------------------------------===//
+// Vector Evaluation
+//===----------------------------------------------------------------------===//
+
+namespace {
+ class VISIBILITY_HIDDEN VectorExprEvaluator
+ : public StmtVisitor<VectorExprEvaluator, APValue> {
+ EvalInfo &Info;
+ public:
+
+ VectorExprEvaluator(EvalInfo &info) : Info(info) {}
+
+ APValue VisitStmt(Stmt *S) {
+ return APValue();
+ }
+
+ APValue VisitParenExpr(ParenExpr *E) { return Visit(E->getSubExpr()); }
+ APValue VisitCastExpr(const CastExpr* E);
+ APValue VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
+ APValue VisitInitListExpr(const InitListExpr *E);
+ };
+} // end anonymous namespace
+
+static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
+ if (!E->getType()->isVectorType())
+ return false;
+ Result = VectorExprEvaluator(Info).Visit(const_cast<Expr*>(E));
+ return !Result.isUninit();
+}
+
+APValue VectorExprEvaluator::VisitCastExpr(const CastExpr* E) {
+ const Expr* SE = E->getSubExpr();
+
+ // Check for vector->vector bitcast.
+ if (SE->getType()->isVectorType())
+ return this->Visit(const_cast<Expr*>(SE));
+
+ return APValue();
+}
+
+APValue
+VectorExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
+ return this->Visit(const_cast<Expr*>(E->getInitializer()));
+}
+
+APValue
+VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
+ const VectorType *VT = E->getType()->getAsVectorType();
+ unsigned NumInits = E->getNumInits();
+
+ if (!VT || VT->getNumElements() != NumInits)
+ return APValue();
+
+ QualType EltTy = VT->getElementType();
+ llvm::SmallVector<APValue, 4> Elements;
+
+ for (unsigned i = 0; i < NumInits; i++) {
+ if (EltTy->isIntegerType()) {
+ llvm::APSInt sInt(32);
+ if (!EvaluateInteger(E->getInit(i), sInt, Info))
+ return APValue();
+ Elements.push_back(APValue(sInt));
+ } else {
+ llvm::APFloat f(0.0);
+ if (!EvaluateFloat(E->getInit(i), f, Info))
+ return APValue();
+ Elements.push_back(APValue(f));
+ }
+ }
+ return APValue(&Elements[0], Elements.size());
+}
+
+//===----------------------------------------------------------------------===//
// Integer Evaluation
//===----------------------------------------------------------------------===//
@@ -1067,6 +1139,7 @@
bool FloatExprEvaluator::VisitCastExpr(CastExpr *E) {
Expr* SubExpr = E->getSubExpr();
+
const llvm::fltSemantics& destSemantics =
Info.Ctx.getFloatTypeSemantics(E->getType());
if (SubExpr->getType()->isIntegralType()) {
@@ -1189,7 +1262,10 @@
bool Expr::Evaluate(EvalResult &Result, ASTContext &Ctx) const {
EvalInfo Info(Ctx, Result);
- if (getType()->isIntegerType()) {
+ if (getType()->isVectorType()) {
+ if (!EvaluateVector(this, Result.Val, Info))
+ return false;
+ } else if (getType()->isIntegerType()) {
llvm::APSInt sInt(32);
if (!EvaluateInteger(this, sInt, Info))
return false;
Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=62455&r1=62454&r2=62455&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Sat Jan 17 21:20:47 2009
@@ -2187,11 +2187,9 @@
}
bool Sema::CheckForConstantInitializer(Expr *Init, QualType DclT) {
- Expr::EvalResult Result;
-
Init = Init->IgnoreParens();
- if (Init->Evaluate(Result, Context) && !Result.HasSideEffects)
+ if (Init->isEvaluatable(Context))
return false;
// Look through CXXDefaultArgExprs; they have no meaning in this context.
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=62455&r1=62454&r2=62455&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Sat Jan 17 21:20:47 2009
@@ -2925,13 +2925,14 @@
return lType;
const VectorType *VTy = lType->getAsVectorType();
-
- // FIXME: need to deal with non-32b int / non-64b long long
unsigned TypeSize = Context.getTypeSize(VTy->getElementType());
- if (TypeSize == 32) {
+ if (TypeSize == Context.getTypeSize(Context.IntTy))
return Context.getExtVectorType(Context.IntTy, VTy->getNumElements());
- }
- assert(TypeSize == 64 && "Unhandled vector element size in vector compare");
+ else if (TypeSize == Context.getTypeSize(Context.LongTy))
+ return Context.getExtVectorType(Context.LongTy, VTy->getNumElements());
+
+ assert(TypeSize == Context.getTypeSize(Context.LongLongTy) &&
+ "Unhandled vector element size in vector compare");
return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements());
}
Modified: cfe/trunk/test/CodeGen/ext-vector.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/ext-vector.c?rev=62455&r1=62454&r2=62455&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/ext-vector.c (original)
+++ cfe/trunk/test/CodeGen/ext-vector.c Sat Jan 17 21:20:47 2009
@@ -6,6 +6,8 @@
float4 foo = (float4){ 1.0, 2.0, 3.0, 4.0 };
+const float4 bar = (float4){ 1.0, 2.0, 3.0, __builtin_inff() };
+
float4 test1(float4 V) {
return V.wzyx+V;
}
More information about the cfe-commits
mailing list