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

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 28 23:39:40 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;
----------------
antoniofrighetto wrote:

They do not, but they do both involve the ceil part, thus I thought it was nice to batch them together under a single `foldCeilIdioms`.

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


More information about the llvm-commits mailing list