[cfe-commits] r119678 - in /cfe/trunk: lib/CodeGen/CGExprScalar.cpp lib/Sema/SemaExpr.cpp test/CodeGen/builtins-ppc-altivec.c test/Parser/altivec.c test/Parser/cxx-altivec.cpp test/Sema/altivec-init.c test/SemaCXX/altivec.cpp

Anton Yartsev anton.yartsev at gmail.com
Wed Nov 17 19:19:30 PST 2010


Author: ayartsev
Date: Wed Nov 17 21:19:30 2010
New Revision: 119678

URL: http://llvm.org/viewvc/llvm-project?rev=119678&view=rev
Log:
comparison of AltiVec vectors now gives bool result (fix for 7533)

Modified:
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
    cfe/trunk/lib/Sema/SemaExpr.cpp
    cfe/trunk/test/CodeGen/builtins-ppc-altivec.c
    cfe/trunk/test/Parser/altivec.c
    cfe/trunk/test/Parser/cxx-altivec.cpp
    cfe/trunk/test/Sema/altivec-init.c
    cfe/trunk/test/SemaCXX/altivec.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=119678&r1=119677&r2=119678&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Wed Nov 17 21:19:30 2010
@@ -1981,6 +1981,48 @@
   return Builder.CreateAShr(Ops.LHS, RHS, "shr");
 }
 
+enum IntrinsicType { VCMPEQ, VCMPGT };
+// return corresponding comparison intrinsic for given vector type
+static llvm::Intrinsic::ID GetIntrinsic(IntrinsicType IT,
+                                        BuiltinType::Kind ElemKind) {
+  switch (ElemKind) {
+  default: assert(0 && "unexpected element type");
+  case BuiltinType::Char_U:
+  case BuiltinType::UChar:
+    return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p :
+                            llvm::Intrinsic::ppc_altivec_vcmpgtub_p;
+    break;
+  case BuiltinType::Char_S:
+  case BuiltinType::SChar:
+    return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequb_p :
+                            llvm::Intrinsic::ppc_altivec_vcmpgtsb_p;
+    break;
+  case BuiltinType::UShort:
+    return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p :
+                            llvm::Intrinsic::ppc_altivec_vcmpgtuh_p;
+    break;
+  case BuiltinType::Short:
+    return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequh_p :
+                            llvm::Intrinsic::ppc_altivec_vcmpgtsh_p;
+    break;
+  case BuiltinType::UInt:
+  case BuiltinType::ULong:
+    return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p :
+                            llvm::Intrinsic::ppc_altivec_vcmpgtuw_p;
+    break;
+  case BuiltinType::Int:
+  case BuiltinType::Long:
+    return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpequw_p :
+                            llvm::Intrinsic::ppc_altivec_vcmpgtsw_p;
+    break;
+  case BuiltinType::Float:
+    return (IT == VCMPEQ) ? llvm::Intrinsic::ppc_altivec_vcmpeqfp_p :
+                            llvm::Intrinsic::ppc_altivec_vcmpgtfp_p;
+    break;
+  }
+  return llvm::Intrinsic::not_intrinsic;
+}
+
 Value *ScalarExprEmitter::EmitCompare(const BinaryOperator *E,unsigned UICmpOpc,
                                       unsigned SICmpOpc, unsigned FCmpOpc) {
   TestAndClearIgnoreResultAssign();
@@ -1997,6 +2039,72 @@
     Value *LHS = Visit(E->getLHS());
     Value *RHS = Visit(E->getRHS());
 
+    // If AltiVec, the comparison results in a numeric type, so we use
+    // intrinsics comparing vectors and giving 0 or 1 as a result
+    if (LHSTy->isVectorType() && CGF.getContext().getLangOptions().AltiVec) {
+      // constants for mapping CR6 register bits to predicate result
+      enum { CR6_EQ=0, CR6_EQ_REV, CR6_LT, CR6_LT_REV } CR6;
+
+      llvm::Intrinsic::ID ID = llvm::Intrinsic::not_intrinsic;
+
+      // in several cases vector arguments order will be reversed
+      Value *FirstVecArg = LHS,
+            *SecondVecArg = RHS;
+
+      QualType ElTy = LHSTy->getAs<VectorType>()->getElementType();
+      Type *Ty = CGF.getContext().getCanonicalType(ElTy).getTypePtr();
+      const BuiltinType *BTy = dyn_cast<BuiltinType>(Ty);
+      BuiltinType::Kind ElementKind = BTy->getKind();
+
+      switch(E->getOpcode()) {
+      default: assert(0 && "is not a comparison operation");
+      case BO_EQ:
+        CR6 = CR6_LT;
+        ID = GetIntrinsic(VCMPEQ, ElementKind);
+        break;
+      case BO_NE:
+        CR6 = CR6_EQ;
+        ID = GetIntrinsic(VCMPEQ, ElementKind);
+        break;
+      case BO_LT:
+        CR6 = CR6_LT;
+        ID = GetIntrinsic(VCMPGT, ElementKind);
+        std::swap(FirstVecArg, SecondVecArg);
+        break;
+      case BO_GT:
+        CR6 = CR6_LT;
+        ID = GetIntrinsic(VCMPGT, ElementKind);
+        break;
+      case BO_LE:
+        if (ElementKind == BuiltinType::Float) {
+          CR6 = CR6_LT;
+          ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p;
+          std::swap(FirstVecArg, SecondVecArg);
+        }
+        else {
+          CR6 = CR6_EQ;
+          ID = GetIntrinsic(VCMPGT, ElementKind);
+        }
+        break;
+      case BO_GE:
+        if (ElementKind == BuiltinType::Float) {
+          CR6 = CR6_LT;
+          ID = llvm::Intrinsic::ppc_altivec_vcmpgefp_p;
+        }
+        else {
+          CR6 = CR6_EQ;
+          ID = GetIntrinsic(VCMPGT, ElementKind);
+          std::swap(FirstVecArg, SecondVecArg);
+        }
+        break;
+      }
+
+      Value *CR6Param = llvm::ConstantInt::get(CGF.Int32Ty, CR6);
+      llvm::Function *F = CGF.CGM.getIntrinsic(ID);
+      Result = Builder.CreateCall3(F, CR6Param, FirstVecArg, SecondVecArg, "");
+      return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType());
+    }
+
     if (LHS->getType()->isFPOrFPVectorTy()) {
       Result = Builder.CreateFCmp((llvm::CmpInst::Predicate)FCmpOpc,
                                   LHS, RHS, "cmp");

Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=119678&r1=119677&r2=119678&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Wed Nov 17 21:19:30 2010
@@ -6423,6 +6423,11 @@
   if (vType.isNull())
     return vType;
 
+  // If AltiVec, the comparison results in a numeric type, i.e.
+  // bool for C++, int for C
+  if (getLangOptions().AltiVec)
+    return (getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy);
+
   QualType lType = lex->getType();
   QualType rType = rex->getType();
 

Modified: cfe/trunk/test/CodeGen/builtins-ppc-altivec.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/builtins-ppc-altivec.c?rev=119678&r1=119677&r2=119678&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/builtins-ppc-altivec.c (original)
+++ cfe/trunk/test/CodeGen/builtins-ppc-altivec.c Wed Nov 17 21:19:30 2010
@@ -3052,3 +3052,64 @@
   /* vec_any_out */
   res_i = vec_any_out(vf, vf);                  // CHECK: @llvm.ppc.altivec.vcmpbfp.p
 }
+
+/* ------------------------------ Relational Operators------------------------------- */
+// CHECK: define void @test7
+void test7() {
+  vector signed char vsc1 = (vector signed char)(-1);
+  vector signed char vsc2 = (vector signed char)(-2);
+  res_i = (vsc1 == vsc2);                  // CHECK: @llvm.ppc.altivec.vcmpequb.p(i32 2
+  res_i = (vsc1 != vsc2);                  // CHECK: @llvm.ppc.altivec.vcmpequb.p(i32 0
+  res_i = (vsc1 <  vsc2);                  // CHECK: @llvm.ppc.altivec.vcmpgtsb.p(i32 2
+  res_i = (vsc1 >  vsc2);                  // CHECK: @llvm.ppc.altivec.vcmpgtsb.p(i32 2
+  res_i = (vsc1 <= vsc2);                  // CHECK: @llvm.ppc.altivec.vcmpgtsb.p(i32 0
+  res_i = (vsc1 >= vsc2);                  // CHECK: @llvm.ppc.altivec.vcmpgtsb.p(i32 0
+  vector unsigned char vuc1 = (vector unsigned char)(1);
+  vector unsigned char vuc2 = (vector unsigned char)(2);
+  res_i = (vuc1 == vuc2);                  // CHECK: @llvm.ppc.altivec.vcmpequb.p(i32 2
+  res_i = (vuc1 != vuc2);                  // CHECK: @llvm.ppc.altivec.vcmpequb.p(i32 0
+  res_i = (vuc1 <  vuc2);                  // CHECK: @llvm.ppc.altivec.vcmpgtub.p(i32 2
+  res_i = (vuc1 >  vuc2);                  // CHECK: @llvm.ppc.altivec.vcmpgtub.p(i32 2
+  res_i = (vuc1 <= vuc2);                  // CHECK: @llvm.ppc.altivec.vcmpgtub.p(i32 0
+  res_i = (vuc1 >= vuc2);                  // CHECK: @llvm.ppc.altivec.vcmpgtub.p(i32 0
+  vector short vs1 = (vector short)(-1);
+  vector short vs2 = (vector short)(-2);
+  res_i = (vs1 == vs2);                    // CHECK: @llvm.ppc.altivec.vcmpequh.p(i32 2
+  res_i = (vs1 != vs2);                    // CHECK: @llvm.ppc.altivec.vcmpequh.p(i32 0
+  res_i = (vs1 <  vs2);                    // CHECK: @llvm.ppc.altivec.vcmpgtsh.p(i32 2
+  res_i = (vs1 >  vs2);                    // CHECK: @llvm.ppc.altivec.vcmpgtsh.p(i32 2
+  res_i = (vs1 <= vs2);                    // CHECK: @llvm.ppc.altivec.vcmpgtsh.p(i32 0
+  res_i = (vs1 >= vs2);                    // CHECK: @llvm.ppc.altivec.vcmpgtsh.p(i32 0
+  vector unsigned short vus1 = (vector unsigned short)(1);
+  vector unsigned short vus2 = (vector unsigned short)(2);
+  res_i = (vus1 == vus2);                  // CHECK: @llvm.ppc.altivec.vcmpequh.p(i32 2
+  res_i = (vus1 != vus2);                  // CHECK: @llvm.ppc.altivec.vcmpequh.p(i32 0
+  res_i = (vus1 <  vus2);                  // CHECK: @llvm.ppc.altivec.vcmpgtuh.p(i32 2
+  res_i = (vus1 >  vus2);                  // CHECK: @llvm.ppc.altivec.vcmpgtuh.p(i32 2
+  res_i = (vus1 <= vus2);                  // CHECK: @llvm.ppc.altivec.vcmpgtuh.p(i32 0
+  res_i = (vus1 >= vus2);                  // CHECK: @llvm.ppc.altivec.vcmpgtuh.p(i32 0
+  vector int vi1 = (vector int)(-1);
+  vector int vi2 = (vector int)(-2);
+  res_i = (vi1 == vi2);                    // CHECK: @llvm.ppc.altivec.vcmpequw.p(i32 2
+  res_i = (vi1 != vi2);                    // CHECK: @llvm.ppc.altivec.vcmpequw.p(i32 0
+  res_i = (vi1 <  vi2);                    // CHECK: @llvm.ppc.altivec.vcmpgtsw.p(i32 2
+  res_i = (vi1 >  vi2);                    // CHECK: @llvm.ppc.altivec.vcmpgtsw.p(i32 2
+  res_i = (vi1 <= vi2);                    // CHECK: @llvm.ppc.altivec.vcmpgtsw.p(i32 0
+  res_i = (vi1 >= vi2);                    // CHECK: @llvm.ppc.altivec.vcmpgtsw.p(i32 0
+  vector unsigned int vui1 = (vector unsigned int)(1);
+  vector unsigned int vui2 = (vector unsigned int)(2);
+  res_i = (vui1 == vui2);                  // CHECK: @llvm.ppc.altivec.vcmpequw.p(i32 2
+  res_i = (vui1 != vui2);                  // CHECK: @llvm.ppc.altivec.vcmpequw.p(i32 0
+  res_i = (vui1 <  vui2);                  // CHECK: @llvm.ppc.altivec.vcmpgtuw.p(i32 2
+  res_i = (vui1 >  vui2);                  // CHECK: @llvm.ppc.altivec.vcmpgtuw.p(i32 2
+  res_i = (vui1 <= vui2);                  // CHECK: @llvm.ppc.altivec.vcmpgtuw.p(i32 0
+  res_i = (vui1 >= vui2);                  // CHECK: @llvm.ppc.altivec.vcmpgtuw.p(i32 0
+  vector float vf1 = (vector float)(1.0);
+  vector float vf2 = (vector float)(2.0);
+  res_i = (vf1 == vf2);                    // CHECK: @llvm.ppc.altivec.vcmpeqfp.p(i32 2
+  res_i = (vf1 != vf2);                    // CHECK: @llvm.ppc.altivec.vcmpeqfp.p(i32 0
+  res_i = (vf1 <  vf2);                    // CHECK: @llvm.ppc.altivec.vcmpgtfp.p(i32 2
+  res_i = (vf1 >  vf2);                    // CHECK: @llvm.ppc.altivec.vcmpgtfp.p(i32 2
+  res_i = (vf1 <= vf2);                    // CHECK: @llvm.ppc.altivec.vcmpgefp.p(i32 2
+  res_i = (vf1 >= vf2);                    // CHECK: @llvm.ppc.altivec.vcmpgefp.p(i32 2
+}

Modified: cfe/trunk/test/Parser/altivec.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/altivec.c?rev=119678&r1=119677&r2=119678&view=diff
==============================================================================
--- cfe/trunk/test/Parser/altivec.c (original)
+++ cfe/trunk/test/Parser/altivec.c Wed Nov 17 21:19:30 2010
@@ -100,6 +100,16 @@
   __vector unsigned int tv = gccv;
   gccv = v;
   gccvector unsigned int tgv = v;
+
+  int res_i;
+  // bug 7553 - Problem with '==' and vectors
+  res_i = (vv_sc == vv_sc);
+  res_i = (vv_uc != vv_uc);
+  res_i = (vv_s > vv_s);
+  res_i = (vv_us >= vv_us);
+  res_i = (vv_i < vv_i);
+  res_i = (vv_ui <= vv_ui);
+  res_i = (vv_f <= vv_f);
 }
 
 // bug 6895 - Vectorl literal casting confusion.

Modified: cfe/trunk/test/Parser/cxx-altivec.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Parser/cxx-altivec.cpp?rev=119678&r1=119677&r2=119678&view=diff
==============================================================================
--- cfe/trunk/test/Parser/cxx-altivec.cpp (original)
+++ cfe/trunk/test/Parser/cxx-altivec.cpp Wed Nov 17 21:19:30 2010
@@ -127,27 +127,17 @@
 vector float v5 = (vector float)(1.0f, 2.0f, 3.0f, 4.0f);
 vector char v6 = (vector char)((vector int)(1+2, -2, (int)(2.0 * 3), -(5-3)));
 
-#if 0 // Not ready yet.
 // bug 7553 - Problem with '==' and vectors
 void func() {
-  vector int v10i = (vector int)(1, 2, 3, 4);
-  vector int v11i = (vector int)(1, 2, 3, 4);
-  bool r10ieq = (v10i == v11i);
-  bool r10ine = (v10i != v11i);
-  bool r10igt = (v10i > v11i);
-  bool r10ige = (v10i >= v11i);
-  bool r10ilt = (v10i < v11i);
-  bool r10ile = (v10i <= v11i);
-  vector float v10f = (vector float)(1.0f, 2.0f, 3.0f, 4.0f);
-  vector float v11f = (vector float)(1.0f, 2.0f, 3.0f, 4.0f);
-  bool r10feq = (v10f == v11f);
-  bool r10fne = (v10f != v11f);
-  bool r10fgt = (v10f > v11f);
-  bool r10fge = (v10f >= v11f);
-  bool r10flt = (v10f < v11f);
-  bool r10fle = (v10f <= v11f);
+  bool res_b;
+  res_b = (vv_sc == vv_sc);
+  res_b = (vv_uc != vv_uc);
+  res_b = (vv_s > vv_s);
+  res_b = (vv_us >= vv_us);
+  res_b = (vv_i < vv_i);
+  res_b = (vv_ui <= vv_ui);
+  res_b = (vv_f <= vv_f);
 }
-#endif
 
 // vecreturn attribute test
 struct Vector

Modified: cfe/trunk/test/Sema/altivec-init.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/altivec-init.c?rev=119678&r1=119677&r2=119678&view=diff
==============================================================================
--- cfe/trunk/test/Sema/altivec-init.c (original)
+++ cfe/trunk/test/Sema/altivec-init.c Wed Nov 17 21:19:30 2010
@@ -30,6 +30,6 @@
 
   f(vAltiVec);
   vGCC = vAltiVec;
-  vGCC = vGCC > vAltiVec;
+  int res = vGCC > vAltiVec;
   vAltiVec = 0 ? vGCC : vGCC;
 }

Modified: cfe/trunk/test/SemaCXX/altivec.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/altivec.cpp?rev=119678&r1=119677&r2=119678&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/altivec.cpp (original)
+++ cfe/trunk/test/SemaCXX/altivec.cpp Wed Nov 17 21:19:30 2010
@@ -13,6 +13,6 @@
 
   f(vAltiVec);
   vGCC = vAltiVec;
-  vGCC = vGCC > vAltiVec;
+  bool res = vGCC > vAltiVec;
   vAltiVec = 0 ? vGCC : vGCC;
 }





More information about the cfe-commits mailing list