[llvm] [InstCombine] Handle ceil division idiom (PR #100977)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 28 23:34:59 PDT 2024


================
@@ -1250,6 +1250,75 @@ static Instruction *foldToUnsignedSaturatedAdd(BinaryOperator &I) {
   return nullptr;
 }
 
+static Value *foldCeilIdioms(BinaryOperator &I, InstCombinerImpl &IC) {
+  assert(I.getOpcode() == Instruction::Add && "Expecting add instruction.");
+  Value *A, *B;
+  ICmpInst::Predicate Pred;
+  auto &ICB = IC.Builder;
+
+  // Fold the log2 ceil idiom:
+  // zext (ctpop(A) >u/!= 1) + (ctlz (A, true) ^ (BW - 1))
+  //      -> BW - ctlz (A - 1, false)
+  const APInt *XorC;
----------------
dtcxzyw wrote:

Why did you merge the log2 ceil part into this function? I don't think they share common code.


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


More information about the llvm-commits mailing list