[llvm] [IR] Add BooleanMap matcher (PR #184463)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 4 01:22:03 PST 2026
================
@@ -2380,6 +2333,50 @@ m_ZExtOrTruncOrSelf(const OpTy &Op) {
return m_CombineOr(m_CombineOr(m_ZExt(Op), m_Trunc(Op)), Op);
}
+struct BooleanMap_match {
+ Value *&Cond;
+ Constant *&TrueC;
+ Constant *&FalseC;
+
+ BooleanMap_match(Value *&C, Constant *&TC, Constant *&FC)
+ : Cond(C), TrueC(TC), FalseC(FC) {}
+
+ template <typename OpTy> bool match(OpTy *V) const {
+ // select(Cond, TrueC, FalseC) — captures both constants directly
+ if (PatternMatch::match(
+ V, m_Select(m_Value(Cond), m_Constant(TrueC), m_Constant(FalseC))))
+ return true;
+
+ Type *Ty = V->getType();
+ Value *CondV;
+
+ // zext(i1 Cond) is equivalent to select(Cond, 1, 0)
+ if (PatternMatch::match(V, m_ZExt(m_Value(CondV))) &&
+ CondV->getType()->isIntOrIntVectorTy(1)) {
+ Cond = CondV;
+ TrueC = ConstantInt::get(Ty, 1);
+ FalseC = ConstantInt::get(Ty, 0);
+ return true;
+ }
+
+ // sext(i1 Cond) is equivalent to select(Cond, -1, 0)
+ if (PatternMatch::match(V, m_SExt(m_Value(CondV))) &&
+ CondV->getType()->isIntOrIntVectorTy(1)) {
+ Cond = CondV;
+ TrueC = Constant::getAllOnesValue(Ty);
+ FalseC = ConstantInt::get(Ty, 0);
+ return true;
+ }
+
+ return false;
+ }
+};
+
+inline BooleanMap_match m_BooleanMap(Value *&C, Constant *&TrueC,
----------------
nikic wrote:
```suggestion
inline BooleanMap_match m_SelectLike(Value *&C, Constant *&TrueC,
```
Is what these usually get called. Also add a doc comment here that explains which patterns this matches.
https://github.com/llvm/llvm-project/pull/184463
More information about the llvm-commits
mailing list