[llvm] [SelectOpt] Add handling for Select-like operations. (PR #77284)

David Green via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 16 07:19:55 PST 2024


================
@@ -129,6 +124,151 @@ class SelectOptimizeImpl {
     Scaled64 NonPredCost;
   };
 
+  /// SelectLike is an abstraction over SelectInst and other operations that can
+  /// act like selects. For example Or(Zext(icmp), X) can be treated like
+  /// select(icmp, X|1, X).
+  class SelectLike {
+  private:
+    SelectLike(Instruction *SI) : SI(SI) {}
+
+    Instruction *SI;
+
+  public:
+    /// Match a select or select-like instruction, returning a SelectLike.
+    static SelectLike match(Instruction *I) {
+      // Select instruction are what we are usually looking for. If the select
+      // is a logical-and/logical-or then it is better treated as a and/or by
+      // the backend.
+      if (isa<SelectInst>(I) &&
+          !PatternMatch::match(I,
----------------
davemgreen wrote:

A logical Or is `select i1 %a, i1 true, i1 %b` and a logical And is `select i1 %a, i1 %b, false`. They work the same as an Or/And, but have difference semantics with respect to poison propagation.

I was contemplating moving this to shouldTreatInstructionAsSelect, that is probably better, even if it does mean duplicating the check a little.

https://github.com/llvm/llvm-project/pull/77284


More information about the llvm-commits mailing list