[llvm] f53b38d - [InstSimplify] select Cond, true, false --> Cond

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 9 06:06:38 PST 2020


Author: Sanjay Patel
Date: 2020-01-09T09:04:20-05:00
New Revision: f53b38d12a7b9c6754d5bc91483efab935b5c012

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

LOG: [InstSimplify] select Cond, true, false --> Cond

This is step 1 of damage control assuming that we need to remove several
over-reaching folds for select-of-booleans because they can cause
miscompiles as shown in D72396.

The scalar case seems obviously safe:
https://rise4fun.com/Alive/jSj

And I don't think there's any danger for vectors either - if the
condition is poisoned, then the select must be poisoned too, so undef
elements don't make any difference.

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

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/select.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index afcca2ab1fa3..d7510c899101 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -3996,6 +3996,15 @@ static Value *SimplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
       return FalseVal;
   }
 
+  // select i1 Cond, i1 true, i1 false --> i1 Cond
+  assert(Cond->getType()->isIntOrIntVectorTy(1) &&
+         "Select must have bool or bool vector condition");
+  assert(TrueVal->getType() == FalseVal->getType() &&
+         "Select must have same types for true/false ops");
+  if (Cond->getType() == TrueVal->getType() &&
+      match(TrueVal, m_One()) && match(FalseVal, m_ZeroInt()))
+    return Cond;
+
   // select ?, X, X -> X
   if (TrueVal == FalseVal)
     return TrueVal;

diff  --git a/llvm/test/Transforms/InstSimplify/select.ll b/llvm/test/Transforms/InstSimplify/select.ll
index 5581a3bd7471..4fe499ad4a24 100644
--- a/llvm/test/Transforms/InstSimplify/select.ll
+++ b/llvm/test/Transforms/InstSimplify/select.ll
@@ -3,8 +3,7 @@
 
 define i1 @bool_true_or_false(i1 %cond) {
 ; CHECK-LABEL: @bool_true_or_false(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], i1 true, i1 false
-; CHECK-NEXT:    ret i1 [[S]]
+; CHECK-NEXT:    ret i1 [[COND:%.*]]
 ;
   %s = select i1 %cond, i1 true, i1 false
   ret i1 %s
@@ -12,8 +11,7 @@ define i1 @bool_true_or_false(i1 %cond) {
 
 define <2 x i1> @bool_true_or_false_vec(<2 x i1> %cond) {
 ; CHECK-LABEL: @bool_true_or_false_vec(
-; CHECK-NEXT:    [[S:%.*]] = select <2 x i1> [[COND:%.*]], <2 x i1> <i1 true, i1 true>, <2 x i1> zeroinitializer
-; CHECK-NEXT:    ret <2 x i1> [[S]]
+; CHECK-NEXT:    ret <2 x i1> [[COND:%.*]]
 ;
   %s = select <2 x i1> %cond, <2 x i1> <i1 true, i1 true>, <2 x i1> zeroinitializer
   ret <2 x i1> %s
@@ -21,8 +19,7 @@ define <2 x i1> @bool_true_or_false_vec(<2 x i1> %cond) {
 
 define <2 x i1> @bool_true_or_false_vec_undef(<2 x i1> %cond) {
 ; CHECK-LABEL: @bool_true_or_false_vec_undef(
-; CHECK-NEXT:    [[S:%.*]] = select <2 x i1> [[COND:%.*]], <2 x i1> <i1 undef, i1 true>, <2 x i1> <i1 false, i1 undef>
-; CHECK-NEXT:    ret <2 x i1> [[S]]
+; CHECK-NEXT:    ret <2 x i1> [[COND:%.*]]
 ;
   %s = select <2 x i1> %cond, <2 x i1> <i1 undef, i1 true>, <2 x i1> <i1 false, i1 undef>
   ret <2 x i1> %s


        


More information about the llvm-commits mailing list