[llvm] [TailRecElim] Introduce support for shift accumulator optimization (PR #181331)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 4 07:02:32 PDT 2026


================
@@ -374,29 +374,136 @@ static bool canMoveAboveCall(Instruction *I, CallInst *CI, AliasAnalysis *AA) {
   return !is_contained(I->operands(), CI);
 }
 
-static bool canTransformAccumulatorRecursion(Instruction *I, CallInst *CI) {
-  if (!I->isAssociative() || !I->isCommutative())
+// While shifts are neither associative nor commutative, a chain of shifts by a
+// constant amount C is equivalent to a single shift by the sum of the amounts:
+//     ... (Base << C) << C) ... << C == Base << (C * Iterations)
+// This relation applies to left shifts as well as arithmetic/logical right
+// shifts when the shift amount is a constant.
+static bool isPseudoAssociative(Instruction *I) {
+  if (!I->isShift())
     return false;
+  return isa<ConstantInt>(I->getOperand(1));
+}
+
+// Find the base-case return value for function F: examine all
+// return instructions and pick return values that do not depend on a
+// recursive call to F. If there is exactly one distinct such value,
+// return it. If there are none or more than one distinct value, return
+// nullptr to indicate failure.
+//
+// FIXME: There is a room for improvement here in the future, e.g., consider
+// non-constant values and multiple base cases -- e.g., we want to be able to
+// handle code like:
+// ```
+// int f(int x) {
+//  if (x == 1) return 1;
+//  if (x == 10) return 10;
+//  return f(x-1) << 1;
+// }
+// ```
+static Constant *getReturnValue(Function &F) {
+  Constant *BaseCaseVal = nullptr;
+
+  // Local lambda with conservative bail-out: It isn't so trivial to perform TRE
+  // if the return value depends on the result of a recursive call, because the
+  // return value will be different for different iterations of the recursion.
+  // So we ignore return values that depend on recursive calls.
+  auto TryGetConstantBaseCase = [&](Value *RV) -> Constant * {
----------------
antoniofrighetto wrote:

`TryGetConstantBaseCase` here looks a bit too ad-hoc / pattern-specific to me. Maybe the previous approach using a worklist could be more principled. If we start with something a bit simpler as the current one, I'd suggest employing `PatternMatch`, see how InstCombine canonicalizes select with constant arms, how the IR looks like by the time TRE runs, etc. Perhaps something around the following lines to begin with:
```cpp
Value *RV = RI->getReturnValue();
Constant *Candidate = nullptr;

// Try to extract a constant base-case value from the return.
if (match(RV, m_Constant(Candidate))) {
  // Direct constant.
} else if (auto *PN = dyn_cast<PHINode>(RV)) {
  for (Value *Inc : PN->incoming_values()) {
    if (match(Inc, m_Constant(Candidate)))
      // Phi w/ one constant incoming value.
      break;
  }
} else if (match(RV, m_Select(m_Value(), m_Value(),
                              m_Constant(Candidate)))) {
  // Select w/ a constant arm.
}
```

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


More information about the llvm-commits mailing list