llvm.org GIT mirror llvm / 2a93bda
[CGP] Be less conservative about tail-duplicating a ret to allow tail calls CGP tail-duplicates rets into blocks that end with a call that feed the ret. This puts the call in tail position, potentially allowing the DAG builder to lower it as a tail call. To avoid tail duplication in cases where we won't form the tail call, CGP tried to predict whether this is going to be possible, and avoids doing it when lowering as a tail call will definitely fail. However, it was being too conservative by always throwing away calls to functions with a signext/zeroext attribute on the return type. Instead, we can use the same logic the builder uses to determine whether the attributes work out. Differential Revision: https://reviews.llvm.org/D24315 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@280894 91177308-0d34-0410-b5e6-96231b3b80d8 Michael Kuperstein 3 years ago
4 changed file(s) with 81 addition(s) and 43 deletion(s). Raw diff Collapse all Expand all
104104 /// This function only tests target-independent requirements.
105105 bool isInTailCallPosition(ImmutableCallSite CS, const TargetMachine &TM);
106106
107 /// Test if given that the input instruction is in the tail call position, if
108 /// there is an attribute mismatch between the caller and the callee that will
109 /// inhibit tail call optimizations.
110 /// \p AllowDifferingSizes is an output parameter which, if forming a tail call
111 /// is permitted, determines whether it's permitted only if the size of the
112 /// caller's and callee's return types match exactly.
113 bool attributesPermitTailCall(const Function *F, const Instruction *I,
114 const ReturnInst *Ret,
115 const TargetLoweringBase &TLI,
116 bool *AllowDifferingSizes = nullptr);
117
107118 /// Test if given that the input instruction is in the tail call position if the
108119 /// return type or any attributes of the function will inhibit tail call
109120 /// optimization.
110 bool returnTypeIsEligibleForTailCall(const Function *F,
111 const Instruction *I,
121 bool returnTypeIsEligibleForTailCall(const Function *F, const Instruction *I,
112122 const ReturnInst *Ret,
113123 const TargetLoweringBase &TLI);
114124
524524 F, I, Ret, *TM.getSubtargetImpl(*F)->getTargetLowering());
525525 }
526526
527 bool llvm::attributesPermitTailCall(const Function *F, const Instruction *I,
528 const ReturnInst *Ret,
529 const TargetLoweringBase &TLI,
530 bool *AllowDifferingSizes) {
531 // ADS may be null, so don't write to it directly.
532 bool DummyADS;
533 bool &ADS = AllowDifferingSizes ? *AllowDifferingSizes : DummyADS;
534 ADS = true;
535
536 AttrBuilder CallerAttrs(F->getAttributes(),
537 AttributeSet::ReturnIndex);
538 AttrBuilder CalleeAttrs(cast(I)->getAttributes(),
539 AttributeSet::ReturnIndex);
540
541 // Noalias is completely benign as far as calling convention goes, it
542 // shouldn't affect whether the call is a tail call.
543 CallerAttrs = CallerAttrs.removeAttribute(Attribute::NoAlias);
544 CalleeAttrs = CalleeAttrs.removeAttribute(Attribute::NoAlias);
545
546 if (CallerAttrs.contains(Attribute::ZExt)) {
547 if (!CalleeAttrs.contains(Attribute::ZExt))
548 return false;
549
550 ADS = false;
551 CallerAttrs.removeAttribute(Attribute::ZExt);
552 CalleeAttrs.removeAttribute(Attribute::ZExt);
553 } else if (CallerAttrs.contains(Attribute::SExt)) {
554 if (!CalleeAttrs.contains(Attribute::SExt))
555 return false;
556
557 ADS = false;
558 CallerAttrs.removeAttribute(Attribute::SExt);
559 CalleeAttrs.removeAttribute(Attribute::SExt);
560 }
561
562 // If they're still different, there's some facet we don't understand
563 // (currently only "inreg", but in future who knows). It may be OK but the
564 // only safe option is to reject the tail call.
565 return CallerAttrs == CalleeAttrs;
566 }
567
527568 bool llvm::returnTypeIsEligibleForTailCall(const Function *F,
528569 const Instruction *I,
529570 const ReturnInst *Ret,
537578 if (isa(Ret->getOperand(0))) return true;
538579
539580 // Make sure the attributes attached to each return are compatible.
540 AttrBuilder CallerAttrs(F->getAttributes(),
541 AttributeSet::ReturnIndex);
542 AttrBuilder CalleeAttrs(cast(I)->getAttributes(),
543 AttributeSet::ReturnIndex);
544
545 // Noalias is completely benign as far as calling convention goes, it
546 // shouldn't affect whether the call is a tail call.
547 CallerAttrs = CallerAttrs.removeAttribute(Attribute::NoAlias);
548 CalleeAttrs = CalleeAttrs.removeAttribute(Attribute::NoAlias);
549
550 bool AllowDifferingSizes = true;
551 if (CallerAttrs.contains(Attribute::ZExt)) {
552 if (!CalleeAttrs.contains(Attribute::ZExt))
553 return false;
554
555 AllowDifferingSizes = false;
556 CallerAttrs.removeAttribute(Attribute::ZExt);
557 CalleeAttrs.removeAttribute(Attribute::ZExt);
558 } else if (CallerAttrs.contains(Attribute::SExt)) {
559 if (!CalleeAttrs.contains(Attribute::SExt))
560 return false;
561
562 AllowDifferingSizes = false;
563 CallerAttrs.removeAttribute(Attribute::SExt);
564 CalleeAttrs.removeAttribute(Attribute::SExt);
565 }
566
567 // If they're still different, there's some facet we don't understand
568 // (currently only "inreg", but in future who knows). It may be OK but the
569 // only safe option is to reject the tail call.
570 if (CallerAttrs != CalleeAttrs)
581 bool AllowDifferingSizes;
582 if (!attributesPermitTailCall(F, I, Ret, TLI, &AllowDifferingSizes))
571583 return false;
572584
573585 const Value *RetVal = Ret->getOperand(0), *CallVal = I;
2222 #include "llvm/Analysis/TargetTransformInfo.h"
2323 #include "llvm/Analysis/ValueTracking.h"
2424 #include "llvm/Analysis/MemoryBuiltins.h"
25 #include "llvm/CodeGen/Analysis.h"
2526 #include "llvm/IR/CallSite.h"
2627 #include "llvm/IR/Constants.h"
2728 #include "llvm/IR/DataLayout.h"
19821983 if (PN && PN->getParent() != BB)
19831984 return false;
19841985
1985 // It's not safe to eliminate the sign / zero extension of the return value.
1986 // See llvm::isInTailCallPosition().
1987 const Function *F = BB->getParent();
1988 AttributeSet CallerAttrs = F->getAttributes();
1989 if (CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::ZExt) ||
1990 CallerAttrs.hasAttribute(AttributeSet::ReturnIndex, Attribute::SExt))
1991 return false;
1992
19931986 // Make sure there are no instructions between the PHI and return, or that the
19941987 // return is the first instruction in the block.
19951988 if (PN) {
20092002
20102003 /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail
20112004 /// call.
2005 const Function *F = BB->getParent();
20122006 SmallVector TailCalls;
20132007 if (PN) {
20142008 for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
20152009 CallInst *CI = dyn_cast(PN->getIncomingValue(I));
20162010 // Make sure the phi value is indeed produced by the tail call.
20172011 if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) &&
2018 TLI->mayBeEmittedAsTailCall(CI))
2012 TLI->mayBeEmittedAsTailCall(CI) &&
2013 attributesPermitTailCall(F, CI, RetI, *TLI))
20192014 TailCalls.push_back(CI);
20202015 }
20212016 } else {
20322027 continue;
20332028
20342029 CallInst *CI = dyn_cast(&*RI);
2035 if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI))
2030 if (CI && CI->use_empty() && TLI->mayBeEmittedAsTailCall(CI) &&
2031 attributesPermitTailCall(F, CI, RetI, *TLI))
20362032 TailCalls.push_back(CI);
20372033 }
20382034 }
8484 %retval.0.i = bitcast i8* %retval.0.in.i to %0*
8585 ret %0* %retval.0.i
8686 }
87
88
89 ; Correctly handle zext returns.
90 declare zeroext i1 @foo_i1()
91
92 ; CHECK-LABEL: zext_i1
93 ; CHECK: jmp _foo_i1
94 define zeroext i1 @zext_i1(i1 %k) {
95 entry:
96 br i1 %k, label %land.end, label %land.rhs
97
98 land.rhs: ; preds = %entry
99 %call1 = tail call zeroext i1 @foo_i1()
100 br label %land.end
101
102 land.end: ; preds = %entry, %land.rhs
103 %0 = phi i1 [ false, %entry ], [ %call1, %land.rhs ]
104 ret i1 %0
105 }
106