Advertisement
VictorHuangIBM

llvm::isInTailCallPosition TailCall Debug

May 15th, 2020
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. /// Test if the given instruction is in a position to be optimized
  2. /// with a tail-call. This roughly means that it's in a block with
  3. /// a return and there's nothing that needs to be scheduled
  4. /// between it and the return.
  5. ///
  6. /// This function only tests target-independent requirements.
  7. bool llvm::isInTailCallPosition(const CallBase &Call, const TargetMachine &TM) {
  8. const BasicBlock *ExitBB = Call.getParent();
  9. const Instruction *Term = ExitBB->getTerminator();
  10. const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
  11.  
  12. // The block must end in a return statement or unreachable.
  13. //
  14. // FIXME: Decline tailcall if it's not guaranteed and if the block ends in
  15. // an unreachable, for now. The way tailcall optimization is currently
  16. // implemented means it will add an epilogue followed by a jump. That is
  17. // not profitable. Also, if the callee is a special function (e.g.
  18. // longjmp on x86), it can end up causing miscompilation that has not
  19. // been fully understood.
  20. if (!Ret &&
  21. ((!TM.Options.GuaranteedTailCallOpt &&
  22. Call.getCallingConv() != CallingConv::Tail) || !isa<UnreachableInst>(Term)))
  23. return false;
  24.  
  25. // If I will have a chain, make sure no other instruction that will have a
  26. // chain interposes between I and the return.
  27. if (Call.mayHaveSideEffects() || Call.mayReadFromMemory() ||
  28. !isSafeToSpeculativelyExecute(&Call))
  29. for (BasicBlock::const_iterator BBI = std::prev(ExitBB->end(), 2);; --BBI) {
  30. if (&*BBI == &Call)
  31. break;
  32. // Debug info intrinsics do not get in the way of tail call optimization.
  33. if (isa<DbgInfoIntrinsic>(BBI))
  34. continue;
  35. // A lifetime end or assume intrinsic should not stop tail call
  36. // optimization.
  37. if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(BBI))
  38. if (II->getIntrinsicID() == Intrinsic::lifetime_end ||
  39. II->getIntrinsicID() == Intrinsic::assume)
  40. continue;
  41. if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
  42. !isSafeToSpeculativelyExecute(&*BBI))
  43. return false;
  44. }
  45.  
  46. const Function *F = ExitBB->getParent();
  47. return returnTypeIsEligibleForTailCall(
  48. F, &Call, Ret, *TM.getSubtargetImpl(*F)->getTargetLowering());
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement