[PATCH] D83440: [InstSimplify] Re-enable select ?, undef, X -> X transform when X is provably not poison

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 8 15:41:14 PDT 2020


craig.topper created this revision.
craig.topper added reviewers: spatel, reames, nlopes, lebedev.ri, efriedma, majnemer.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

Follow up from the transform being removed in D83360 <https://reviews.llvm.org/D83360>. If X is probably not poison, then the transform is safe.

Still plan to remove or adjust the code from ConstantFolding after this. Also need to audit the partial undef vector handling in InstSimplify just below this code.


https://reviews.llvm.org/D83440

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


Index: llvm/test/Transforms/InstSimplify/select.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/select.ll
+++ llvm/test/Transforms/InstSimplify/select.ll
@@ -794,8 +794,7 @@
 ; These can be folded because the other value is guaranteed not to be poison.
 define i32 @false_undef_true_constant(i1 %cond) {
 ; CHECK-LABEL: @false_undef_true_constant(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], i32 10, i32 undef
-; CHECK-NEXT:    ret i32 [[S]]
+; CHECK-NEXT:    ret i32 10
 ;
   %s = select i1 %cond, i32 10, i32 undef
   ret i32 %s
@@ -803,8 +802,7 @@
 
 define i32 @true_undef_false_constant(i1 %cond) {
 ; CHECK-LABEL: @true_undef_false_constant(
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], i32 undef, i32 20
-; CHECK-NEXT:    ret i32 [[S]]
+; CHECK-NEXT:    ret i32 20
 ;
   %s = select i1 %cond, i32 undef, i32 20
   ret i32 %s
@@ -830,8 +828,7 @@
 define i32 @false_undef_true_freeze(i1 %cond, i32 %x) {
 ; CHECK-LABEL: @false_undef_true_freeze(
 ; CHECK-NEXT:    [[XF:%.*]] = freeze i32 [[X:%.*]]
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], i32 [[XF]], i32 undef
-; CHECK-NEXT:    ret i32 [[S]]
+; CHECK-NEXT:    ret i32 [[XF]]
 ;
   %xf = freeze i32 %x
   %s = select i1 %cond, i32 %xf, i32 undef
@@ -841,8 +838,7 @@
 define i32 @false_undef_false_freeze(i1 %cond, i32 %x) {
 ; CHECK-LABEL: @false_undef_false_freeze(
 ; CHECK-NEXT:    [[XF:%.*]] = freeze i32 [[X:%.*]]
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[COND:%.*]], i32 undef, i32 [[XF]]
-; CHECK-NEXT:    ret i32 [[S]]
+; CHECK-NEXT:    ret i32 [[XF]]
 ;
   %xf = freeze i32 %x
   %s = select i1 %cond, i32 undef, i32 %xf
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4118,6 +4118,15 @@
   if (TrueVal == FalseVal)
     return TrueVal;
 
+  // If the true or false value is undef, we can fold to the other value as
+  // long as the other value isn't poison.
+  // select ?, undef, X -> X
+  if (isa<UndefValue>(TrueVal) && isGuaranteedNotToBeUndefOrPoison(FalseVal))
+    return FalseVal;
+  // select ?, X, undef -> X
+  if (isa<UndefValue>(FalseVal) && isGuaranteedNotToBeUndefOrPoison(TrueVal))
+    return TrueVal;
+
   // Deal with partial undef vector constants: select ?, VecC, VecC' --> VecC''
   Constant *TrueC, *FalseC;
   if (TrueVal->getType()->isVectorTy() && match(TrueVal, m_Constant(TrueC)) &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D83440.276583.patch
Type: text/x-patch
Size: 2535 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200708/18a938cd/attachment.bin>


More information about the llvm-commits mailing list