[llvm] ba3198c - [IRBuilder] Migrate select-folding to value-based FoldSelect.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 15 03:27:08 PST 2022


Author: Florian Hahn
Date: 2022-01-15T11:26:44Z
New Revision: ba3198cfd11443b214eec43a03f292a66040fbca

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

LOG: [IRBuilder] Migrate select-folding to value-based FoldSelect.

Reviewed By: lebedev.ri

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

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/InstSimplifyFolder.h
    llvm/include/llvm/Analysis/TargetFolder.h
    llvm/include/llvm/IR/ConstantFolder.h
    llvm/include/llvm/IR/IRBuilderFolder.h
    llvm/include/llvm/IR/NoFolder.h
    llvm/lib/IR/IRBuilder.cpp
    llvm/test/Transforms/LoopVectorize/pr34681.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/InstSimplifyFolder.h b/llvm/include/llvm/Analysis/InstSimplifyFolder.h
index 26aa9a4fe0411..12a1a8348579c 100644
--- a/llvm/include/llvm/Analysis/InstSimplifyFolder.h
+++ b/llvm/include/llvm/Analysis/InstSimplifyFolder.h
@@ -65,6 +65,10 @@ class InstSimplifyFolder final : public IRBuilderFolder {
     return SimplifyGEPInst(Ty, Ptr, IdxList, IsInBounds, SQ);
   }
 
+  Value *FoldSelect(Value *C, Value *True, Value *False) const override {
+    return SimplifySelectInst(C, True, False, SQ);
+  }
+
   //===--------------------------------------------------------------------===//
   // Binary Operators
   //===--------------------------------------------------------------------===//
@@ -220,11 +224,6 @@ class InstSimplifyFolder final : public IRBuilderFolder {
   // Other Instructions
   //===--------------------------------------------------------------------===//
 
-  Value *CreateSelect(Constant *C, Constant *True,
-                      Constant *False) const override {
-    return ConstFolder.CreateSelect(C, True, False);
-  }
-
   Value *CreateExtractElement(Constant *Vec, Constant *Idx) const override {
     return ConstFolder.CreateExtractElement(Vec, Idx);
   }

diff  --git a/llvm/include/llvm/Analysis/TargetFolder.h b/llvm/include/llvm/Analysis/TargetFolder.h
index 7944181ade678..e614285416508 100644
--- a/llvm/include/llvm/Analysis/TargetFolder.h
+++ b/llvm/include/llvm/Analysis/TargetFolder.h
@@ -87,6 +87,16 @@ class TargetFolder final : public IRBuilderFolder {
     return nullptr;
   }
 
+  Value *FoldSelect(Value *C, Value *True, Value *False) const override {
+    auto *CC = dyn_cast<Constant>(C);
+    auto *TC = dyn_cast<Constant>(True);
+    auto *FC = dyn_cast<Constant>(False);
+    if (CC && TC && FC)
+      return Fold(ConstantExpr::getSelect(CC, TC, FC));
+
+    return nullptr;
+  }
+
   //===--------------------------------------------------------------------===//
   // Binary Operators
   //===--------------------------------------------------------------------===//
@@ -242,11 +252,6 @@ class TargetFolder final : public IRBuilderFolder {
   // Other Instructions
   //===--------------------------------------------------------------------===//
 
-  Constant *CreateSelect(Constant *C, Constant *True,
-                         Constant *False) const override {
-    return Fold(ConstantExpr::getSelect(C, True, False));
-  }
-
   Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const override {
     return Fold(ConstantExpr::getExtractElement(Vec, Idx));
   }

diff  --git a/llvm/include/llvm/IR/ConstantFolder.h b/llvm/include/llvm/IR/ConstantFolder.h
index 8154c6442c4c1..07d26214af17a 100644
--- a/llvm/include/llvm/IR/ConstantFolder.h
+++ b/llvm/include/llvm/IR/ConstantFolder.h
@@ -78,6 +78,15 @@ class ConstantFolder final : public IRBuilderFolder {
     return nullptr;
   }
 
+  Value *FoldSelect(Value *C, Value *True, Value *False) const override {
+    auto *CC = dyn_cast<Constant>(C);
+    auto *TC = dyn_cast<Constant>(True);
+    auto *FC = dyn_cast<Constant>(False);
+    if (CC && TC && FC)
+      return ConstantExpr::getSelect(CC, TC, FC);
+    return nullptr;
+  }
+
   //===--------------------------------------------------------------------===//
   // Binary Operators
   //===--------------------------------------------------------------------===//
@@ -247,11 +256,6 @@ class ConstantFolder final : public IRBuilderFolder {
   // Other Instructions
   //===--------------------------------------------------------------------===//
 
-  Constant *CreateSelect(Constant *C, Constant *True,
-                         Constant *False) const override {
-    return ConstantExpr::getSelect(C, True, False);
-  }
-
   Constant *CreateExtractElement(Constant *Vec, Constant *Idx) const override {
     return ConstantExpr::getExtractElement(Vec, Idx);
   }

diff  --git a/llvm/include/llvm/IR/IRBuilderFolder.h b/llvm/include/llvm/IR/IRBuilderFolder.h
index 06712cffa110c..59523858f24a1 100644
--- a/llvm/include/llvm/IR/IRBuilderFolder.h
+++ b/llvm/include/llvm/IR/IRBuilderFolder.h
@@ -41,6 +41,8 @@ class IRBuilderFolder {
   virtual Value *FoldGEP(Type *Ty, Value *Ptr, ArrayRef<Value *> IdxList,
                          bool IsInBounds = false) const = 0;
 
+  virtual Value *FoldSelect(Value *C, Value *True, Value *False) const = 0;
+
   //===--------------------------------------------------------------------===//
   // Binary Operators
   //===--------------------------------------------------------------------===//
@@ -111,8 +113,6 @@ class IRBuilderFolder {
   // Other Instructions
   //===--------------------------------------------------------------------===//
 
-  virtual Value *CreateSelect(Constant *C, Constant *True,
-                              Constant *False) const = 0;
   virtual Value *CreateExtractElement(Constant *Vec, Constant *Idx) const = 0;
   virtual Value *CreateInsertElement(Constant *Vec, Constant *NewElt,
                                      Constant *Idx) const = 0;

diff  --git a/llvm/include/llvm/IR/NoFolder.h b/llvm/include/llvm/IR/NoFolder.h
index 6bf58d9dd9c94..32473bb8478d8 100644
--- a/llvm/include/llvm/IR/NoFolder.h
+++ b/llvm/include/llvm/IR/NoFolder.h
@@ -59,6 +59,10 @@ class NoFolder final : public IRBuilderFolder {
     return nullptr;
   }
 
+  Value *FoldSelect(Value *C, Value *True, Value *False) const override {
+    return nullptr;
+  }
+
   //===--------------------------------------------------------------------===//
   // Binary Operators
   //===--------------------------------------------------------------------===//
@@ -248,11 +252,6 @@ class NoFolder final : public IRBuilderFolder {
   // Other Instructions
   //===--------------------------------------------------------------------===//
 
-  Instruction *CreateSelect(Constant *C,
-                            Constant *True, Constant *False) const override {
-    return SelectInst::Create(C, True, False);
-  }
-
   Instruction *CreateExtractElement(Constant *Vec,
                                     Constant *Idx) const override {
     return ExtractElementInst::Create(Vec, Idx);

diff  --git a/llvm/lib/IR/IRBuilder.cpp b/llvm/lib/IR/IRBuilder.cpp
index 98f6ccf81973e..b1483a44ebf77 100644
--- a/llvm/lib/IR/IRBuilder.cpp
+++ b/llvm/lib/IR/IRBuilder.cpp
@@ -984,10 +984,8 @@ CallInst *IRBuilderBase::CreateConstrainedFPCall(
 
 Value *IRBuilderBase::CreateSelect(Value *C, Value *True, Value *False,
                                    const Twine &Name, Instruction *MDFrom) {
-  if (auto *CC = dyn_cast<Constant>(C))
-    if (auto *TC = dyn_cast<Constant>(True))
-      if (auto *FC = dyn_cast<Constant>(False))
-        return Insert(Folder.CreateSelect(CC, TC, FC), Name);
+  if (auto *V = Folder.FoldSelect(C, True, False))
+    return V;
 
   SelectInst *Sel = SelectInst::Create(C, True, False);
   if (MDFrom) {

diff  --git a/llvm/test/Transforms/LoopVectorize/pr34681.ll b/llvm/test/Transforms/LoopVectorize/pr34681.ll
index 4eaf4d0845555..a7f1942f80005 100644
--- a/llvm/test/Transforms/LoopVectorize/pr34681.ll
+++ b/llvm/test/Transforms/LoopVectorize/pr34681.ll
@@ -83,7 +83,6 @@ for.end:
 ; CHECK: vector.scevcheck:
 ; CHECK-NEXT:  add nsw i32 %conv, -1
 ; CHECK-NEXT:  [[NEG:%.+]] = sub i32 0, %conv
-; CHECK-NEXT:  = select i1 false, i32 [[NEG]], i32 %conv
 ; CHECK-NOT: %ident.check = icmp ne i16 %N, 1
 ; CHECK-NOT: %{{[0-9]+}} = or i1 false, %ident.check
 ; CHECK-NOT: br i1 %{{[0-9]+}}, label %scalar.ph, label %vector.ph


        


More information about the llvm-commits mailing list