[llvm] 66281ba - [InstCombine] Fix type of constant in canonicalizeClampLike

David Green via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 30 01:06:27 PDT 2021


Author: David Green
Date: 2021-10-30T09:06:21+01:00
New Revision: 66281baea1dfb798dd583af236d4cf5abe4a73ec

URL: https://github.com/llvm/llvm-project/commit/66281baea1dfb798dd583af236d4cf5abe4a73ec
DIFF: https://github.com/llvm/llvm-project/commit/66281baea1dfb798dd583af236d4cf5abe4a73ec.diff

LOG: [InstCombine] Fix type of constant in canonicalizeClampLike

As a followup to D108049, one of the constants could now be generated
with an incorrect type, now that the input could be truncated.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
    llvm/test/Transforms/InstCombine/truncating-saturate.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index cf1276dd95a3f..c19da8a01c262 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -1353,7 +1353,7 @@ static Value *canonicalizeClampLike(SelectInst &Sel0, ICmpInst &Cmp0,
   // it should either be the X itself, or an addition of some constant to X.
   Constant *C1;
   if (Cmp00 == X)
-    C1 = ConstantInt::getNullValue(Sel0.getType());
+    C1 = ConstantInt::getNullValue(X->getType());
   else if (!match(Cmp00,
                   m_Add(m_Specific(X),
                         m_CombineAnd(m_AnyIntegralConstant(), m_Constant(C1)))))

diff  --git a/llvm/test/Transforms/InstCombine/truncating-saturate.ll b/llvm/test/Transforms/InstCombine/truncating-saturate.ll
index d83c2675758d9..0446b21a33d31 100644
--- a/llvm/test/Transforms/InstCombine/truncating-saturate.ll
+++ b/llvm/test/Transforms/InstCombine/truncating-saturate.ll
@@ -613,3 +613,22 @@ define <2 x i8> @C0zeroVu(<2 x i8> %X, <2 x i8> %y, <2 x i8> %z) {
   %r = select <2 x i1> %cmp, <2 x i8> %X, <2 x i8> %f
   ret <2 x i8> %r
 }
+
+define i8 @f(i32 %value, i8 %call.i) {
+; CHECK-LABEL: @f(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CMP_I:%.*]] = icmp slt i32 [[VALUE:%.*]], 0
+; CHECK-NEXT:    [[COND_I:%.*]] = select i1 [[CMP_I]], i8 [[CALL_I:%.*]], i8 0
+; CHECK-NEXT:    [[CMP_I_I:%.*]] = icmp ult i32 [[VALUE]], 256
+; CHECK-NEXT:    [[CONV4:%.*]] = trunc i32 [[VALUE]] to i8
+; CHECK-NEXT:    [[COND:%.*]] = select i1 [[CMP_I_I]], i8 [[CONV4]], i8 [[COND_I]]
+; CHECK-NEXT:    ret i8 [[COND]]
+;
+entry:
+  %cmp.i = icmp slt i32 %value, 0
+  %cond.i = select i1 %cmp.i, i8 %call.i, i8 0
+  %cmp.i.i = icmp ult i32 %value, 256
+  %conv4 = trunc i32 %value to i8
+  %cond = select i1 %cmp.i.i, i8 %conv4, i8 %cond.i
+  ret i8 %cond
+}


        


More information about the llvm-commits mailing list