llvm.org GIT mirror llvm / 962a797
--- Merging r127981 into '.': U include/llvm/Target/TargetLowering.h U lib/Target/X86/X86ISelLowering.cpp U lib/Target/X86/X86ISelLowering.h U lib/Target/ARM/ARMISelLowering.h U lib/Target/ARM/ARMISelLowering.cpp U lib/Transforms/Scalar/CodeGenPrepare.cpp --- Merging r128194 into '.': G lib/Transforms/Scalar/CodeGenPrepare.cpp --- Merging r128196 into '.': G lib/Transforms/Scalar/CodeGenPrepare.cpp --- Merging r128197 into '.': A test/CodeGen/X86/tailcall-returndup-void.ll G lib/Transforms/Scalar/CodeGenPrepare.cpp git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_29@128200 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 9 years ago
7 changed file(s) with 215 addition(s) and 8 deletion(s). Raw diff Collapse all Expand all
12861286 return false;
12871287 }
12881288
1289 /// mayBeEmittedAsTailCall - Return true if the target may be able emit the
1290 /// call instruction as a tail call. This is used by optimization passes to
1291 /// determine if it's profitable to duplicate return instructions to enable
1292 /// tailcall optimization.
1293 virtual bool mayBeEmittedAsTailCall(CallInst *CI) const {
1294 return false;
1295 }
1296
12891297 /// LowerOperationWrapper - This callback is invoked by the type legalizer
12901298 /// to legalize nodes with an illegal operand type but legal result types.
12911299 /// It replaces the LowerOperation callback in the type Legalizer.
18021802 return HasRet;
18031803 }
18041804
1805 bool ARMTargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1806 if (!EnableARMTailCalls)
1807 return false;
1808
1809 if (!CI->isTailCall())
1810 return false;
1811
1812 return !Subtarget->isThumb1Only();
1813 }
1814
18051815 // ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
18061816 // their target counterpart wrapped in the ARMISD::Wrapper node. Suppose N is
18071817 // one of the above mentioned nodes. It has to be wrapped because otherwise
454454
455455 virtual bool isUsedByReturnOnly(SDNode *N) const;
456456
457 virtual bool mayBeEmittedAsTailCall(CallInst *CI) const;
458
457459 SDValue getARMCmp(SDValue LHS, SDValue RHS, ISD::CondCode CC,
458460 SDValue &ARMcc, SelectionDAG &DAG, DebugLoc dl) const;
459461 SDValue getVFPCmp(SDValue LHS, SDValue RHS,
4444 #include "llvm/ADT/Statistic.h"
4545 #include "llvm/ADT/StringExtras.h"
4646 #include "llvm/ADT/VectorExtras.h"
47 #include "llvm/Support/CallSite.h"
4748 #include "llvm/Support/Debug.h"
4849 #include "llvm/Support/Dwarf.h"
4950 #include "llvm/Support/ErrorHandling.h"
15771578 /// supports tail call optimization.
15781579 static bool IsTailCallConvention(CallingConv::ID CC) {
15791580 return (CC == CallingConv::Fast || CC == CallingConv::GHC);
1581 }
1582
1583 bool X86TargetLowering::mayBeEmittedAsTailCall(CallInst *CI) const {
1584 if (!CI->isTailCall())
1585 return false;
1586
1587 CallSite CS(CI);
1588 CallingConv::ID CalleeCC = CS.getCallingConv();
1589 if (!IsTailCallConvention(CalleeCC) && CalleeCC != CallingConv::C)
1590 return false;
1591
1592 return true;
15801593 }
15811594
15821595 /// FuncIsMadeTailCallSafe - Return true if the function is being made into
842842
843843 virtual bool isUsedByReturnOnly(SDNode *N) const;
844844
845 virtual bool mayBeEmittedAsTailCall(CallInst *CI) const;
846
845847 virtual bool
846848 CanLowerReturn(CallingConv::ID CallConv, bool isVarArg,
847849 const SmallVectorImpl &Outs,
4646 using namespace llvm::PatternMatch;
4747
4848 STATISTIC(NumBlocksElim, "Number of blocks eliminated");
49 STATISTIC(NumPHIsElim, "Number of trivial PHIs eliminated");
50 STATISTIC(NumGEPsElim, "Number of GEPs converted to casts");
49 STATISTIC(NumPHIsElim, "Number of trivial PHIs eliminated");
50 STATISTIC(NumGEPsElim, "Number of GEPs converted to casts");
5151 STATISTIC(NumCmpUses, "Number of uses of Cmp expressions replaced with uses of "
5252 "sunken Cmps");
5353 STATISTIC(NumCastUses, "Number of uses of Cast expressions replaced with uses "
5454 "of sunken Casts");
5555 STATISTIC(NumMemoryInsts, "Number of memory instructions whose address "
5656 "computations were sunk");
57 STATISTIC(NumExtsMoved, "Number of [s|z]ext instructions combined with loads");
58 STATISTIC(NumExtUses, "Number of uses of [s|z]ext instructions optimized");
57 STATISTIC(NumExtsMoved, "Number of [s|z]ext instructions combined with loads");
58 STATISTIC(NumExtUses, "Number of uses of [s|z]ext instructions optimized");
59 STATISTIC(NumRetsDup, "Number of return instructions duplicated");
5960
6061 namespace {
6162 class CodeGenPrepare : public FunctionPass {
7071 /// update it.
7172 BasicBlock::iterator CurInstIterator;
7273
73 // Keeps track of non-local addresses that have been sunk into a block. This
74 // allows us to avoid inserting duplicate code for blocks with multiple
75 // load/stores of the same address.
74 /// Keeps track of non-local addresses that have been sunk into a block.
75 /// This allows us to avoid inserting duplicate code for blocks with
76 /// multiple load/stores of the same address.
7677 DenseMap SunkAddrs;
78
79 /// UpdateDT - If CFG is modified in anyway, dominator tree may need to
80 /// be updated.
81 bool UpdateDT;
7782
7883 public:
7984 static char ID; // Pass identification, replacement for typeid
99104 bool OptimizeCallInst(CallInst *CI);
100105 bool MoveExtToFormExtLoad(Instruction *I);
101106 bool OptimizeExtUses(Instruction *I);
107 bool DupRetToEnableTailCallOpts(ReturnInst *RI);
102108 };
103109 }
104110
113119 bool CodeGenPrepare::runOnFunction(Function &F) {
114120 bool EverMadeChange = false;
115121
122 UpdateDT = false;
116123 DT = getAnalysisIfAvailable();
117124 PFI = getAnalysisIfAvailable();
125
118126 // First pass, eliminate blocks that contain only PHI nodes and an
119127 // unconditional branch.
120128 EverMadeChange |= EliminateMostlyEmptyBlocks(F);
122130 bool MadeChange = true;
123131 while (MadeChange) {
124132 MadeChange = false;
125 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB)
133 for (Function::iterator I = F.begin(), E = F.end(); I != E; ) {
134 BasicBlock *BB = I++;
126135 MadeChange |= OptimizeBlock(*BB);
136 }
127137 EverMadeChange |= MadeChange;
128138 }
129139
130140 SunkAddrs.clear();
141
142 if (UpdateDT && DT)
143 DT->DT->recalculate(F);
131144
132145 return EverMadeChange;
133146 }
532545 return Simplifier.fold(CI, TD);
533546 }
534547
548 /// DupRetToEnableTailCallOpts - Look for opportunities to duplicate return
549 /// instructions to the predecessor to enable tail call optimizations. The
550 /// case it is currently looking for is:
551 /// bb0:
552 /// %tmp0 = tail call i32 @f0()
553 /// br label %return
554 /// bb1:
555 /// %tmp1 = tail call i32 @f1()
556 /// br label %return
557 /// bb2:
558 /// %tmp2 = tail call i32 @f2()
559 /// br label %return
560 /// return:
561 /// %retval = phi i32 [ %tmp0, %bb0 ], [ %tmp1, %bb1 ], [ %tmp2, %bb2 ]
562 /// ret i32 %retval
563 ///
564 /// =>
565 ///
566 /// bb0:
567 /// %tmp0 = tail call i32 @f0()
568 /// ret i32 %tmp0
569 /// bb1:
570 /// %tmp1 = tail call i32 @f1()
571 /// ret i32 %tmp1
572 /// bb2:
573 /// %tmp2 = tail call i32 @f2()
574 /// ret i32 %tmp2
575 ///
576 bool CodeGenPrepare::DupRetToEnableTailCallOpts(ReturnInst *RI) {
577 if (!TLI)
578 return false;
579
580 Value *V = RI->getReturnValue();
581 PHINode *PN = V ? dyn_cast(V) : NULL;
582 if (V && !PN)
583 return false;
584
585 BasicBlock *BB = RI->getParent();
586 if (PN && PN->getParent() != BB)
587 return false;
588
589 // It's not safe to eliminate the sign / zero extension of the return value.
590 // See llvm::isInTailCallPosition().
591 const Function *F = BB->getParent();
592 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
593 if ((CallerRetAttr & Attribute::ZExt) || (CallerRetAttr & Attribute::SExt))
594 return false;
595
596 // Make sure there are no instructions between the PHI and return, or that the
597 // return is the first instruction in the block.
598 if (PN) {
599 BasicBlock::iterator BI = BB->begin();
600 do { ++BI; } while (isa(BI));
601 if (&*BI != RI)
602 return false;
603 } else {
604 if (&*BB->begin() != RI)
605 return false;
606 }
607
608 /// Only dup the ReturnInst if the CallInst is likely to be emitted as a tail
609 /// call.
610 SmallVector TailCalls;
611 if (PN) {
612 for (unsigned I = 0, E = PN->getNumIncomingValues(); I != E; ++I) {
613 CallInst *CI = dyn_cast(PN->getIncomingValue(I));
614 // Make sure the phi value is indeed produced by the tail call.
615 if (CI && CI->hasOneUse() && CI->getParent() == PN->getIncomingBlock(I) &&
616 TLI->mayBeEmittedAsTailCall(CI))
617 TailCalls.push_back(CI);
618 }
619 } else {
620 SmallPtrSet VisitedBBs;
621 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
622 if (!VisitedBBs.insert(*PI))
623 continue;
624
625 BasicBlock::InstListType &InstList = (*PI)->getInstList();
626 BasicBlock::InstListType::reverse_iterator RI = InstList.rbegin();
627 BasicBlock::InstListType::reverse_iterator RE = InstList.rend();
628 if (++RI == RE)
629 continue;
630 CallInst *CI = dyn_cast(&*RI);
631 if (CI && CI->getType()->isVoidTy() && TLI->mayBeEmittedAsTailCall(CI))
632 TailCalls.push_back(CI);
633 }
634 }
635
636 bool Changed = false;
637 for (unsigned i = 0, e = TailCalls.size(); i != e; ++i) {
638 CallInst *CI = TailCalls[i];
639 CallSite CS(CI);
640
641 // Conservatively require the attributes of the call to match those of the
642 // return. Ignore noalias because it doesn't affect the call sequence.
643 unsigned CalleeRetAttr = CS.getAttributes().getRetAttributes();
644 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
645 continue;
646
647 // Make sure the call instruction is followed by an unconditional branch to
648 // the return block.
649 BasicBlock *CallBB = CI->getParent();
650 BranchInst *BI = dyn_cast(CallBB->getTerminator());
651 if (!BI || !BI->isUnconditional() || BI->getSuccessor(0) != BB)
652 continue;
653
654 // Duplicate the return into CallBB.
655 (void)FoldReturnIntoUncondBranch(RI, BB, CallBB);
656 UpdateDT = Changed = true;
657 ++NumRetsDup;
658 }
659
660 // If we eliminated all predecessors of the block, delete the block now.
661 if (Changed && pred_begin(BB) == pred_end(BB))
662 BB->eraseFromParent();
663
664 return Changed;
665 }
666
535667 //===----------------------------------------------------------------------===//
536668 // Memory Optimization
537669 //===----------------------------------------------------------------------===//
9551087 if (CallInst *CI = dyn_cast(I))
9561088 return OptimizeCallInst(CI);
9571089
1090 if (ReturnInst *RI = dyn_cast(I))
1091 return DupRetToEnableTailCallOpts(RI);
1092
9581093 return false;
9591094 }
9601095
0 ; RUN: llc < %s -march=x86-64 | FileCheck %s
1 ; CHECK: rBM_info
2 ; CHECK-NOT: ret
3
4 @sES_closure = external global [0 x i64]
5 declare cc10 void @sEH_info(i64* noalias nocapture, i64* noalias nocapture, i64* noalias nocapture, i64, i64, i64) align 8
6
7 define cc10 void @rBM_info(i64* noalias nocapture %Base_Arg, i64* noalias nocapture %Sp_Arg, i64* noalias nocapture %Hp_Arg, i64 %R1_Arg, i64 %R2_Arg, i64 %R3_Arg) nounwind align 8 {
8 c263:
9 %ln265 = getelementptr inbounds i64* %Sp_Arg, i64 -2
10 %ln266 = ptrtoint i64* %ln265 to i64
11 %ln268 = icmp ult i64 %ln266, %R3_Arg
12 br i1 %ln268, label %c26a, label %n26p
13
14 n26p: ; preds = %c263
15 br i1 icmp ne (i64 and (i64 ptrtoint ([0 x i64]* @sES_closure to i64), i64 7), i64 0), label %c1ZP.i, label %n1ZQ.i
16
17 n1ZQ.i: ; preds = %n26p
18 %ln1ZT.i = load i64* getelementptr inbounds ([0 x i64]* @sES_closure, i64 0, i64 0), align 8
19 %ln1ZU.i = inttoptr i64 %ln1ZT.i to void (i64*, i64*, i64*, i64, i64, i64)*
20 tail call cc10 void %ln1ZU.i(i64* %Base_Arg, i64* %Sp_Arg, i64* %Hp_Arg, i64 ptrtoint ([0 x i64]* @sES_closure to i64), i64 ptrtoint ([0 x i64]* @sES_closure to i64), i64 %R3_Arg) nounwind
21 br label %rBL_info.exit
22
23 c1ZP.i: ; preds = %n26p
24 tail call cc10 void @sEH_info(i64* %Base_Arg, i64* %Sp_Arg, i64* %Hp_Arg, i64 ptrtoint ([0 x i64]* @sES_closure to i64), i64 ptrtoint ([0 x i64]* @sES_closure to i64), i64 %R3_Arg) nounwind
25 br label %rBL_info.exit
26
27 rBL_info.exit: ; preds = %c1ZP.i, %n1ZQ.i
28 ret void
29
30 c26a: ; preds = %c263
31 %ln27h = getelementptr inbounds i64* %Base_Arg, i64 -2
32 %ln27j = load i64* %ln27h, align 8
33 %ln27k = inttoptr i64 %ln27j to void (i64*, i64*, i64*, i64, i64, i64)*
34 tail call cc10 void %ln27k(i64* %Base_Arg, i64* %Sp_Arg, i64* %Hp_Arg, i64 %R1_Arg, i64 %R2_Arg, i64 %R3_Arg) nounwind
35 ret void
36 }