[llvm] 3a27b51 - [InstCombine] reduce code for freeze of undef

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 18 12:14:16 PDT 2022


Author: Sanjay Patel
Date: 2022-04-18T15:14:02-04:00
New Revision: 3a27b51b2751dba1b3fbef68d647833ab65aa45b

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

LOG: [InstCombine] reduce code for freeze of undef

The description was ambiguous about the behavior
when boths select arms are constant or both arms
are not constant. I don't think there's any
evidence to support either way, but this matches
the code with a more specified description.

We can extend this to deal with vector constants
with undef/poison elements. Currently, those don't
get folded anywhere.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
    llvm/test/Transforms/InstCombine/select.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index b9957a947392c..b1f2e8bbc85ee 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3779,23 +3779,20 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {
     return replaceInstUsesWith(I, NI);
 
   if (match(Op0, m_Undef())) {
-    // If I is freeze(undef), see its uses and fold it to the best constant.
+    // If I is freeze(undef), check its uses and fold it to a fixed constant.
     // - or: pick -1
-    // - select's condition: pick the value that leads to choosing a constant
-    // - other ops: pick 0
+    // - select's condition: if the true value is constant, choose it by making
+    //                       the condition true.
+    // - default: pick 0
     Constant *BestValue = nullptr;
     Constant *NullValue = Constant::getNullValue(I.getType());
     for (const auto *U : I.users()) {
       Constant *C = NullValue;
 
       if (match(U, m_Or(m_Value(), m_Value())))
-        C = Constant::getAllOnesValue(I.getType());
-      else if (const auto *SI = dyn_cast<SelectInst>(U)) {
-        if (SI->getCondition() == &I) {
-          APInt CondVal(1, isa<Constant>(SI->getFalseValue()) ? 0 : 1);
-          C = Constant::getIntegerValue(I.getType(), CondVal);
-        }
-      }
+        C = ConstantInt::getAllOnesValue(I.getType());
+      else if (match(U, m_Select(m_Specific(&I), m_Constant(), m_Value())))
+        C = ConstantInt::getTrue(I.getType());
 
       if (!BestValue)
         BestValue = C;

diff  --git a/llvm/test/Transforms/InstCombine/select.ll b/llvm/test/Transforms/InstCombine/select.ll
index 06ed583981710..170591378ea40 100644
--- a/llvm/test/Transforms/InstCombine/select.ll
+++ b/llvm/test/Transforms/InstCombine/select.ll
@@ -2588,7 +2588,7 @@ define <2 x i32> @true_undef_vec(i1 %cond, <2 x i32> %x) {
 
 define i8 @cond_freeze(i8 %x, i8 %y) {
 ; CHECK-LABEL: @cond_freeze(
-; CHECK-NEXT:    ret i8 [[X:%.*]]
+; CHECK-NEXT:    ret i8 [[Y:%.*]]
 ;
   %cond.fr = freeze i1 undef
   %s = select i1 %cond.fr, i8 %x, i8 %y
@@ -2615,7 +2615,7 @@ define i8 @cond_freeze_constant_true_val(i8 %x) {
 
 define i8 @cond_freeze_both_arms_constant() {
 ; CHECK-LABEL: @cond_freeze_both_arms_constant(
-; CHECK-NEXT:    ret i8 3
+; CHECK-NEXT:    ret i8 42
 ;
   %cond.fr = freeze i1 undef
   %s = select i1 %cond.fr, i8 42, i8 3
@@ -2646,7 +2646,7 @@ declare void @foo2(i8, i8)
 
 define void @cond_freeze_multipleuses(i8 %x, i8 %y) {
 ; CHECK-LABEL: @cond_freeze_multipleuses(
-; CHECK-NEXT:    call void @foo2(i8 [[X:%.*]], i8 [[Y:%.*]])
+; CHECK-NEXT:    call void @foo2(i8 [[Y:%.*]], i8 [[X:%.*]])
 ; CHECK-NEXT:    ret void
 ;
   %cond.fr = freeze i1 undef


        


More information about the llvm-commits mailing list