[llvm] r218092 - R600: Better fix for bug 20982

Matt Arsenault Matthew.Arsenault at amd.com
Thu Sep 18 17:42:06 PDT 2014


Author: arsenm
Date: Thu Sep 18 19:42:06 2014
New Revision: 218092

URL: http://llvm.org/viewvc/llvm-project?rev=218092&view=rev
Log:
R600: Better fix for bug 20982

Just do the left shift as unsigned to avoid the UB.

Modified:
    llvm/trunk/lib/Target/R600/AMDGPUISelLowering.cpp
    llvm/trunk/test/CodeGen/R600/llvm.AMDGPU.bfe.i32.ll

Modified: llvm/trunk/lib/Target/R600/AMDGPUISelLowering.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/R600/AMDGPUISelLowering.cpp?rev=218092&r1=218091&r2=218092&view=diff
==============================================================================
--- llvm/trunk/lib/Target/R600/AMDGPUISelLowering.cpp (original)
+++ llvm/trunk/lib/Target/R600/AMDGPUISelLowering.cpp Thu Sep 18 19:42:06 2014
@@ -1896,7 +1896,8 @@ template <typename IntTy>
 static SDValue constantFoldBFE(SelectionDAG &DAG, IntTy Src0,
                                uint32_t Offset, uint32_t Width) {
   if (Width + Offset < 32) {
-    IntTy Result = (Src0 << (32 - Offset - Width)) >> (32 - Width);
+    uint32_t Shl = static_cast<uint32_t>(Src0) << (32 - Offset - Width);
+    IntTy Result = static_cast<IntTy>(Shl) >> (32 - Width);
     return DAG.getConstant(Result, MVT::i32);
   }
 
@@ -2053,12 +2054,8 @@ SDValue AMDGPUTargetLowering::PerformDAG
         // Avoid undefined left shift of a negative in the constant fold.
         // TODO: I'm not sure what the behavior of the hardware is, this should
         // probably follow that instead.
-        int32_t Val = CVal->getSExtValue();
-        if (Val < 0)
-          return SDValue();
-
         return constantFoldBFE<int32_t>(DAG,
-                                        Val,
+                                        CVal->getSExtValue(),
                                         OffsetVal,
                                         WidthVal);
       }

Modified: llvm/trunk/test/CodeGen/R600/llvm.AMDGPU.bfe.i32.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/R600/llvm.AMDGPU.bfe.i32.ll?rev=218092&r1=218091&r2=218092&view=diff
==============================================================================
--- llvm/trunk/test/CodeGen/R600/llvm.AMDGPU.bfe.i32.ll (original)
+++ llvm/trunk/test/CodeGen/R600/llvm.AMDGPU.bfe.i32.ll Thu Sep 18 19:42:06 2014
@@ -370,13 +370,12 @@ define void @bfe_i32_constant_fold_test_
   ret void
 }
 
-; FIXME: This should fold to something
 ; FUNC-LABEL: @bfe_i32_constant_fold_test_16
-; SI: S_BFE_I32 [[SREG:s[0-9]+]], -1, 0x70001
-; SI: V_MOV_B32_e32 [[VREG:v[0-9]+]], [[SREG]]
+; SI-NOT: BFE
+; SI: V_MOV_B32_e32 [[VREG:v[0-9]+]], -1
 ; SI: BUFFER_STORE_DWORD [[VREG]],
 ; SI: S_ENDPGM
-
+; EG-NOT: BFE
 define void @bfe_i32_constant_fold_test_16(i32 addrspace(1)* %out) nounwind {
   %bfe_i32 = call i32 @llvm.AMDGPU.bfe.i32(i32 4294967295, i32 1, i32 7) nounwind readnone
   store i32 %bfe_i32, i32 addrspace(1)* %out, align 4





More information about the llvm-commits mailing list