[llvm] [InstCombine] Fold adds + shifts with nsw and nuw flags (PR #88193)

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 9 14:36:31 PDT 2024


================
@@ -1267,6 +1267,19 @@ Instruction *InstCombinerImpl::visitLShr(BinaryOperator &I) {
       match(Op1, m_SpecificIntAllowUndef(BitWidth - 1)))
     return new ZExtInst(Builder.CreateIsNotNeg(X, "isnotneg"), Ty);
 
+  // Special Case:
+  // if both the add and the shift are nuw, we can omit the AND entirely
+  // ((X << Y) nuw + Z nuw) >>u Z --> (X + (Y >>u Z))
+  Value *Y;
+  if (match(Op0, m_OneUse(m_c_NUWAdd((m_NUWShl(m_Value(X), m_Specific(Op1))),
+                                     m_Value(Y))))) {
+    Value *NewLshr = Builder.CreateLShr(Y, Op1, "", I.isExact());
----------------
AtariDreams wrote:

Sure: https://alive2.llvm.org/ce/z/wORNYm

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


More information about the llvm-commits mailing list