[cfe-commits] r105824 - in /cfe/trunk: lib/AST/ExprConstant.cpp test/CodeGen/altivec.c

John McCall rjmccall at apple.com
Fri Jun 11 10:54:16 PDT 2010


Author: rjmccall
Date: Fri Jun 11 12:54:15 2010
New Revision: 105824

URL: http://llvm.org/viewvc/llvm-project?rev=105824&view=rev
Log:
Fix the constant evaluator for AltiVec-style vector literals so that the
vector is filled with the given constant;  we were just initializing the
first element.


Added:
    cfe/trunk/test/CodeGen/altivec.c
Modified:
    cfe/trunk/lib/AST/ExprConstant.cpp

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=105824&r1=105823&r2=105824&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Fri Jun 11 12:54:15 2010
@@ -746,25 +746,46 @@
   QualType EltTy = VT->getElementType();
   llvm::SmallVector<APValue, 4> Elements;
 
-  for (unsigned i = 0; i < NumElements; i++) {
+  // If a vector is initialized with a single element, that value
+  // becomes every element of the vector, not just the first.
+  // This is the behavior described in the IBM AltiVec documentation.
+  if (NumInits == 1) {
+    APValue InitValue;
     if (EltTy->isIntegerType()) {
       llvm::APSInt sInt(32);
-      if (i < NumInits) {
-        if (!EvaluateInteger(E->getInit(i), sInt, Info))
-          return APValue();
-      } else {
-        sInt = Info.Ctx.MakeIntValue(0, EltTy);
-      }
-      Elements.push_back(APValue(sInt));
+      if (!EvaluateInteger(E->getInit(0), sInt, Info))
+        return APValue();
+      InitValue = APValue(sInt);
     } else {
       llvm::APFloat f(0.0);
-      if (i < NumInits) {
-        if (!EvaluateFloat(E->getInit(i), f, Info))
-          return APValue();
+      if (!EvaluateFloat(E->getInit(0), f, Info))
+        return APValue();
+      InitValue = APValue(f);
+    }
+    for (unsigned i = 0; i < NumElements; i++) {
+      Elements.push_back(InitValue);
+    }
+  } else {
+    for (unsigned i = 0; i < NumElements; i++) {
+      if (EltTy->isIntegerType()) {
+        llvm::APSInt sInt(32);
+        if (i < NumInits) {
+          if (!EvaluateInteger(E->getInit(i), sInt, Info))
+            return APValue();
+        } else {
+          sInt = Info.Ctx.MakeIntValue(0, EltTy);
+        }
+        Elements.push_back(APValue(sInt));
       } else {
-        f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
+        llvm::APFloat f(0.0);
+        if (i < NumInits) {
+          if (!EvaluateFloat(E->getInit(i), f, Info))
+            return APValue();
+        } else {
+          f = APFloat::getZero(Info.Ctx.getFloatTypeSemantics(EltTy));
+        }
+        Elements.push_back(APValue(f));
       }
-      Elements.push_back(APValue(f));
     }
   }
   return APValue(&Elements[0], Elements.size());

Added: cfe/trunk/test/CodeGen/altivec.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/altivec.c?rev=105824&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/altivec.c (added)
+++ cfe/trunk/test/CodeGen/altivec.c Fri Jun 11 12:54:15 2010
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -faltivec -triple powerpc-unknown-unknown -emit-llvm %s -o - | FileCheck %s
+
+// CHECK: @test0 = global <4 x i32> <i32 1, i32 1, i32 1, i32 1>
+vector int test0 = (vector int)(1);





More information about the cfe-commits mailing list