r315358 - [CGExprScalar] In EmitCompare trunc the result if it has different type as E->getType()

Guozhi Wei via cfe-commits cfe-commits at lists.llvm.org
Tue Oct 10 13:31:27 PDT 2017


Author: carrot
Date: Tue Oct 10 13:31:27 2017
New Revision: 315358

URL: http://llvm.org/viewvc/llvm-project?rev=315358&view=rev
Log:
[CGExprScalar] In EmitCompare trunc the result if it has different type as E->getType()

Usually compare expression should return i1 type, so EmitScalarConversion is called before return

return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType(), E->getExprLoc());

But when ppc intrinsic is called to compare vectors, the ppc intrinsic can return i32 even E->getType() is BoolTy, in this case EmitScalarConversion does nothing, an i32 type result is returned and causes crash later.

This patch detects this case and truncates the result before return.

Differential Revision: https://reviews.llvm.org/D38656


Added:
    cfe/trunk/test/CodeGen/ppc-vector-compare.cc
Modified:
    cfe/trunk/lib/CodeGen/CGExprScalar.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprScalar.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprScalar.cpp?rev=315358&r1=315357&r2=315358&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprScalar.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprScalar.cpp Tue Oct 10 13:31:27 2017
@@ -3214,6 +3214,16 @@ Value *ScalarExprEmitter::EmitCompare(co
       Value *CR6Param = Builder.getInt32(CR6);
       llvm::Function *F = CGF.CGM.getIntrinsic(ID);
       Result = Builder.CreateCall(F, {CR6Param, FirstVecArg, SecondVecArg});
+
+      // The result type of intrinsic may not be same as E->getType().
+      // If E->getType() is not BoolTy, EmitScalarConversion will do the
+      // conversion work. If E->getType() is BoolTy, EmitScalarConversion will
+      // do nothing, if ResultTy is not i1 at the same time, it will cause
+      // crash later.
+      llvm::IntegerType *ResultTy = cast<llvm::IntegerType>(Result->getType());
+      if (ResultTy->getBitWidth() > 1 &&
+          E->getType() == CGF.getContext().BoolTy)
+        Result = Builder.CreateTrunc(Result, Builder.getInt1Ty());
       return EmitScalarConversion(Result, CGF.getContext().BoolTy, E->getType(),
                                   E->getExprLoc());
     }

Added: cfe/trunk/test/CodeGen/ppc-vector-compare.cc
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/ppc-vector-compare.cc?rev=315358&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/ppc-vector-compare.cc (added)
+++ cfe/trunk/test/CodeGen/ppc-vector-compare.cc Tue Oct 10 13:31:27 2017
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -target-feature +altivec -triple powerpc64-unknown-unknown -emit-llvm %s \
+// RUN:            -o - | FileCheck %s
+
+#include <altivec.h>
+
+// CHECK-LABEL: @_Z5test1Dv8_tS_
+// CHECK: @llvm.ppc.altivec.vcmpequh.p
+bool test1(vector unsigned short v1, vector unsigned short v2) {
+  return v1 == v2;
+}
+




More information about the cfe-commits mailing list