[llvm] [Analysis]: Allow inlining recursive call IF recursion depth is 1. (PR #119677)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue May 6 03:47:59 PDT 2025


================
@@ -1676,6 +1680,79 @@ bool CallAnalyzer::visitGetElementPtr(GetElementPtrInst &I) {
   return isGEPFree(I);
 }
 
+// Simplify \p Cmp if RHS is const and we can ValueTrack LHS.
+// This handles the case only when the Cmp instruction is guarding a recursive
+// call that will cause the Cmp to fail/succeed for the recursive call.
+bool CallAnalyzer::simplifyCmpInstForRecCall(CmpInst &Cmp) {
+  // Bail out if LHS is not a function argument or RHS is NOT const:
+  if (!isa<Argument>(Cmp.getOperand(0)) || !isa<Constant>(Cmp.getOperand(1)))
+    return false;
+  auto *CmpOp = Cmp.getOperand(0);
+  Function *F = Cmp.getFunction();
+  // Iterate over the users of the function to check if it's a recursive
+  // function:
+  for (auto *U : F->users()) {
----------------
nikic wrote:

Why is this iterating over the uses of the function? Shouldn't this be inspecting just the CandidateCall in particular?

It looks like this checks if there is *any* recursive call of the right form, even if it's not the call-site being analyzed.

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


More information about the llvm-commits mailing list