OSDN Git Service

[Hexagon] Avoid UB when shifting unsigned integer left by 32
authorKrzysztof Parzyszek <kparzysz@codeaurora.org>
Fri, 1 Jun 2018 15:39:10 +0000 (15:39 +0000)
committerKrzysztof Parzyszek <kparzysz@codeaurora.org>
Fri, 1 Jun 2018 15:39:10 +0000 (15:39 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@333771 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/Hexagon/HexagonISelLoweringHVX.cpp

index 9d53a58..55b116f 100644 (file)
@@ -1169,16 +1169,17 @@ HexagonTargetLowering::LowerHvxCttz(SDValue Op, SelectionDAG &DAG) const {
   // Calculate the vectors of 1 and bitwidth(x).
   MVT ElemTy = ty(InpV).getVectorElementType();
   unsigned ElemWidth = ElemTy.getSizeInBits();
-  uint32_t Splat1 = 0, SplatW = 0;
+  // Using uint64_t because a shift by 32 can happen.
+  uint64_t Splat1 = 0, SplatW = 0;
   assert(isPowerOf2_32(ElemWidth) && ElemWidth <= 32);
   for (unsigned i = 0; i != 32/ElemWidth; ++i) {
     Splat1 = (Splat1 << ElemWidth) | 1;
     SplatW = (SplatW << ElemWidth) | ElemWidth;
   }
   SDValue Vec1 = DAG.getNode(HexagonISD::VSPLATW, dl, ResTy,
-                             DAG.getConstant(Splat1, dl, MVT::i32));
+                             DAG.getConstant(uint32_t(Splat1), dl, MVT::i32));
   SDValue VecW = DAG.getNode(HexagonISD::VSPLATW, dl, ResTy,
-                             DAG.getConstant(SplatW, dl, MVT::i32));
+                             DAG.getConstant(uint32_t(SplatW), dl, MVT::i32));
   SDValue VecN1 = DAG.getNode(HexagonISD::VSPLATW, dl, ResTy,
                               DAG.getConstant(-1, dl, MVT::i32));
   // Do not use DAG.getNOT, because that would create BUILD_VECTOR with