[cfe-commits] r148254 - in /cfe/trunk: include/clang/Sema/Sema.h lib/CodeGen/CGExprScalar.cpp lib/Sema/SemaExpr.cpp test/CodeGen/ext-vector.c

Eli Friedman eli.friedman at gmail.com
Mon Jan 16 13:16:33 PST 2012


On Mon, Jan 16, 2012 at 1:02 PM, Tanya Lattner <tonic at nondot.org> wrote:
> Author: tbrethou
> Date: Mon Jan 16 15:02:28 2012
> New Revision: 148254
>
> URL: http://llvm.org/viewvc/llvm-project?rev=148254&view=rev
> Log:
> Add support for OpenCL 1.1 logical operations.
>
> Modified:
>    cfe/trunk/include/clang/Sema/Sema.h
>    cfe/trunk/lib/CodeGen/CGExprScalar.cpp
>    cfe/trunk/lib/Sema/SemaExpr.cpp
>    cfe/trunk/test/CodeGen/ext-vector.c
>
> Modified: cfe/trunk/include/clang/Sema/Sema.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Sema/Sema.h?rev=148254&r1=148253&r2=148254&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Sema/Sema.h (original)
> +++ cfe/trunk/include/clang/Sema/Sema.h Mon Jan 16 15:02:28 2012
> @@ -5925,8 +5925,11 @@
>   /// type checking for vector binary operators.
>   QualType CheckVectorOperands(ExprResult &LHS, ExprResult &RHS,
>                                SourceLocation Loc, bool IsCompAssign);
> +  QualType GetSignedVectorType(QualType V);
>   QualType CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS,
>                                       SourceLocation Loc, bool isRelational);
> +  QualType CheckVectorLogicalOperands(ExprResult LHS, ExprResult RHS,
> +                                      SourceLocation Loc);
>
>   /// type checking declaration initializers (C99 6.7.8)
>   bool CheckForConstantInitializer(Expr *e, QualType t);
>
> Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=148254&r1=148253&r2=148254&view=diff
> ==============================================================================
> --- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
> +++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Mon Jan 16 15:02:28 2012
> @@ -1478,6 +1478,15 @@
>  }
>
>  Value *ScalarExprEmitter::VisitUnaryLNot(const UnaryOperator *E) {
> +
> +  // Perform vector logical not on comparison with zero vector.
> +  if (E->getType()->isExtVectorType()) {
> +    Value *Oper = Visit(E->getSubExpr());
> +    Value *Zero = llvm::Constant::getNullValue(Oper->getType());
> +    Value *Result = Builder.CreateICmp(llvm::CmpInst::ICMP_EQ, Oper, Zero, "cmp");
> +    return Builder.CreateSExt(Result, ConvertType(E->getType()), "sext");
> +  }
> +
>   // Compare operand to zero.
>   Value *BoolVal = CGF.EvaluateExprAsBool(E->getSubExpr());
>
> @@ -2386,6 +2395,18 @@
>  }
>
>  Value *ScalarExprEmitter::VisitBinLAnd(const BinaryOperator *E) {
> +
> +  // Perform vector logical and on comparisons with zero vectors.
> +  if (E->getType()->isVectorType()) {
> +    Value *LHS = Visit(E->getLHS());
> +    Value *RHS = Visit(E->getRHS());
> +    Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType());
> +    LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp");
> +    RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp");
> +    Value *And = Builder.CreateAnd(LHS, RHS);
> +    return Builder.CreateSExt(And, Zero->getType(), "sext");
> +  }
> +
>   llvm::Type *ResTy = ConvertType(E->getType());
>
>   // If we have 0 && RHS, see if we can elide RHS, if so, just return 0.
> @@ -2441,6 +2462,18 @@
>  }
>
>  Value *ScalarExprEmitter::VisitBinLOr(const BinaryOperator *E) {
> +
> +  // Perform vector logical or on comparisons with zero vectors.
> +  if (E->getType()->isVectorType()) {
> +    Value *LHS = Visit(E->getLHS());
> +    Value *RHS = Visit(E->getRHS());
> +    Value *Zero = llvm::ConstantAggregateZero::get(LHS->getType());
> +    LHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, LHS, Zero, "cmp");
> +    RHS = Builder.CreateICmp(llvm::CmpInst::ICMP_NE, RHS, Zero, "cmp");
> +    Value *Or = Builder.CreateOr(LHS, RHS);
> +    return Builder.CreateSExt(Or, Zero->getType(), "sext");
> +  }
> +
>   llvm::Type *ResTy = ConvertType(E->getType());
>
>   // If we have 1 || RHS, see if we can elide RHS, if so, just return 1.
>
> Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=148254&r1=148253&r2=148254&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaExpr.cpp Mon Jan 16 15:02:28 2012
> @@ -6882,6 +6882,26 @@
>   return InvalidOperands(Loc, LHS, RHS);
>  }
>
> +
> +// Return a signed type that is of identical size and number of elements.
> +// For floating point vectors, return an integer type of identical size
> +// and number of elements.
> +QualType Sema::GetSignedVectorType(QualType V) {
> +  const VectorType *VTy = V->getAs<VectorType>();
> +  unsigned TypeSize = Context.getTypeSize(VTy->getElementType());
> +  if (TypeSize == Context.getTypeSize(Context.CharTy))
> +    return Context.getExtVectorType(Context.CharTy, VTy->getNumElements());
> +  else if (TypeSize == Context.getTypeSize(Context.ShortTy))
> +    return Context.getExtVectorType(Context.ShortTy, VTy->getNumElements());
> +  else if (TypeSize == Context.getTypeSize(Context.IntTy))
> +    return Context.getExtVectorType(Context.IntTy, VTy->getNumElements());
> +  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());
> +}
> +
>  /// CheckVectorCompareOperands - vector comparisons are a clang extension that
>  /// operates on extended vector types.  Instead of producing an IntTy result,
>  /// like a scalar comparison, a vector comparison produces a vector of integer
> @@ -6923,23 +6943,21 @@
>     assert (RHS.get()->getType()->hasFloatingRepresentation());
>     CheckFloatComparison(Loc, LHS.get(), RHS.get());
>   }
> +
> +  // Return a signed type for the vector.
> +  return GetSignedVectorType(LHSType);
> +}
>
> -  // Return a signed type that is of identical size and number of elements.
> -  // For floating point vectors, return an integer type of identical size
> -  // and number of elements.
> -  const VectorType *VTy = LHSType->getAs<VectorType>();
> -  unsigned TypeSize = Context.getTypeSize(VTy->getElementType());
> -  if (TypeSize == Context.getTypeSize(Context.CharTy))
> -    return Context.getExtVectorType(Context.CharTy, VTy->getNumElements());
> -  else if (TypeSize == Context.getTypeSize(Context.ShortTy))
> -    return Context.getExtVectorType(Context.ShortTy, VTy->getNumElements());
> -  else if (TypeSize == Context.getTypeSize(Context.IntTy))
> -    return Context.getExtVectorType(Context.IntTy, VTy->getNumElements());
> -  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());
> +QualType Sema::CheckVectorLogicalOperands(ExprResult LHS, ExprResult RHS,
> +                                                 SourceLocation Loc)
> +{
> +  // Ensure that either both operands are of the same vector type, or
> +  // one operand is of a vector type and the other is of its element type.
> +  QualType vType = CheckVectorOperands(LHS, RHS, Loc, false);
> +  if (vType.isNull() || vType->isFloatingType())
> +    return InvalidOperands(Loc, LHS, RHS);
> +
> +  return GetSignedVectorType(LHS.get()->getType());
>  }
>
>  inline QualType Sema::CheckBitwiseOperands(
> @@ -6972,6 +6990,10 @@
>  inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
>   ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc) {
>
> +  // Check vector operands differently.
> +  if (LHS.get()->getType()->isVectorType() || RHS.get()->getType()->isVectorType())
> +    return CheckVectorLogicalOperands(LHS, RHS, Loc);
> +
>   // Diagnose cases where the user write a logical and/or but probably meant a
>   // bitwise one.  We do this when the LHS is a non-bool integer and the RHS
>   // is a constant.
> @@ -8261,6 +8283,12 @@
>         Input = ImpCastExprToType(Input.take(), Context.BoolTy,
>                                   ScalarTypeToBooleanCastKind(resultType));
>       }
> +    }
> +    else if (resultType->isExtVectorType()) {

Nit: please put the else on the same line as the brace.

> +      // Handle vector types.
> +      // Vector logical not returns the signed variant of the operand type.
> +      resultType = GetSignedVectorType(resultType);
> +      break;
>     } else {
>       return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr)
>         << resultType << Input.get()->getSourceRange());
>
> Modified: cfe/trunk/test/CodeGen/ext-vector.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/ext-vector.c?rev=148254&r1=148253&r2=148254&view=diff
> ==============================================================================
> --- cfe/trunk/test/CodeGen/ext-vector.c (original)
> +++ cfe/trunk/test/CodeGen/ext-vector.c Mon Jan 16 15:02:28 2012
> @@ -277,3 +277,12 @@
>   d = a == b;
>   d = a != b;
>  }
> +
> +// CHECK: @test15
> +int4 test15(uint4 V0) {
> +  // CHECK: icmp eq <4 x i32>
> +  int4 V = !V0;
> +  V = V && V;
> +  V = V || V;
> +  return V;
> +}
> \ No newline at end of file

Newline?

-Eli




More information about the cfe-commits mailing list