[llvm] [CGP] Permit tail call optimization on undefined return value (PR #82419)

Antonio Frighetto via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 21 10:44:56 PST 2024


================
@@ -2686,8 +2686,9 @@ bool CodeGenPrepare::dupRetToEnableTailCallOpts(BasicBlock *BB,
             attributesPermitTailCall(F, CI, RetI, *TLI)) {
           // Either we return void or the return value must be the first
           // argument of a known intrinsic or library function.
-          if (!V || (isIntrinsicOrLFToBeTailCalled(TLInfo, CI) &&
-                     V == CI->getArgOperand(0))) {
+          if (!V || isa<UndefValue>(V) ||
----------------
antoniofrighetto wrote:

Shall we ever hit the phi case? Looking at https://clang.godbolt.org/z/eq8vTxvzM, it seems that in the first case it suffices one non-undef incoming value to tail call, whereas the second case is refined by CGP as follows:
```llvm
define dso_local i32 @foo() local_unnamed_addr #0 {
  %1 = load i32, ptr @i, align 4
  switch i32 %1, label %6 [
    i32 2, label %2
    i32 5, label %4
  ]

2:                                                ; preds = %0
  %3 = tail call i32 @bar() #2
  br label %6

4:                                                ; preds = %0
  %5 = tail call i32 @qux() #2
  ret i32 %5

6:                                                ; preds = %2, %0
  ret i32 undef
}
```
Thus getting tail called as part of this change.

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


More information about the llvm-commits mailing list