From bd2b96a12dba4e333f49d1a04d2072aa3ec92478 Mon Sep 17 00:00:00 2001 From: Matt Arsenault Date: Fri, 19 Sep 2014 00:42:06 +0000 Subject: [PATCH] R600: Better fix for bug 20982 Just do the left shift as unsigned to avoid the UB. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218092 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/R600/AMDGPUISelLowering.cpp | 9 +++------ test/CodeGen/R600/llvm.AMDGPU.bfe.i32.ll | 7 +++---- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/Target/R600/AMDGPUISelLowering.cpp b/lib/Target/R600/AMDGPUISelLowering.cpp index 89dab098710..bf8daf96604 100644 --- a/lib/Target/R600/AMDGPUISelLowering.cpp +++ b/lib/Target/R600/AMDGPUISelLowering.cpp @@ -1896,7 +1896,8 @@ template 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(Src0) << (32 - Offset - Width); + IntTy Result = static_cast(Shl) >> (32 - Width); return DAG.getConstant(Result, MVT::i32); } @@ -2053,12 +2054,8 @@ SDValue AMDGPUTargetLowering::PerformDAGCombine(SDNode *N, // 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(DAG, - Val, + CVal->getSExtValue(), OffsetVal, WidthVal); } diff --git a/test/CodeGen/R600/llvm.AMDGPU.bfe.i32.ll b/test/CodeGen/R600/llvm.AMDGPU.bfe.i32.ll index 16a140cefa3..eb509423282 100644 --- a/test/CodeGen/R600/llvm.AMDGPU.bfe.i32.ll +++ b/test/CodeGen/R600/llvm.AMDGPU.bfe.i32.ll @@ -370,13 +370,12 @@ define void @bfe_i32_constant_fold_test_15(i32 addrspace(1)* %out) nounwind { 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 -- 2.34.1