llvm.org GIT mirror llvm / eb47fac
[TRE] Move to the new OptRemark API. Fixes PR33788. Differential Revision: https://reviews.llvm.org/D35570 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@308524 91177308-0d34-0410-b5e6-96231b3b80d8 Davide Italiano 2 years ago
4 changed file(s) with 63 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
5959 #include "llvm/Analysis/InlineCost.h"
6060 #include "llvm/Analysis/InstructionSimplify.h"
6161 #include "llvm/Analysis/Loads.h"
62 #include "llvm/Analysis/OptimizationDiagnosticInfo.h"
6263 #include "llvm/Analysis/TargetTransformInfo.h"
6364 #include "llvm/IR/CFG.h"
6465 #include "llvm/IR/CallSite.h"
176177 };
177178 }
178179
179 static bool markTails(Function &F, bool &AllCallsAreTailCalls) {
180 static bool markTails(Function &F, bool &AllCallsAreTailCalls,
181 OptimizationRemarkEmitter *ORE) {
180182 if (F.callsFunctionThatReturnsTwice())
181183 return false;
182184 AllCallsAreTailCalls = true;
251253 break;
252254 }
253255 if (SafeToTail) {
254 emitOptimizationRemark(
255 F.getContext(), "tailcallelim", F, CI->getDebugLoc(),
256 "marked this readnone call a tail call candidate");
256 using namespace ore;
257 ORE->emit(OptimizationRemark(DEBUG_TYPE, "tailcall-readnone", CI)
258 << "marked as tail call candidate (readnone)");
257259 CI->setTailCall();
258260 Modified = true;
259261 continue;
298300 if (Visited[CI->getParent()] != ESCAPED) {
299301 // If the escape point was part way through the block, calls after the
300302 // escape point wouldn't have been put into DeferredTails.
301 emitOptimizationRemark(F.getContext(), "tailcallelim", F,
302 CI->getDebugLoc(),
303 "marked this call a tail call candidate");
303 ORE->emit(OptimizationRemark(DEBUG_TYPE, "tailcall", CI)
304 << "marked as tail call candidate");
304305 CI->setTailCall();
305306 Modified = true;
306307 } else {
490491 BasicBlock *&OldEntry,
491492 bool &TailCallsAreMarkedTail,
492493 SmallVectorImpl &ArgumentPHIs,
493 AliasAnalysis *AA) {
494 AliasAnalysis *AA,
495 OptimizationRemarkEmitter *ORE) {
494496 // If we are introducing accumulator recursion to eliminate operations after
495497 // the call instruction that are both associative and commutative, the initial
496498 // value for the accumulator is placed in this variable. If this value is set
550552 BasicBlock *BB = Ret->getParent();
551553 Function *F = BB->getParent();
552554
553 emitOptimizationRemark(F->getContext(), "tailcallelim", *F, CI->getDebugLoc(),
554 "transforming tail recursion to loop");
555 using namespace ore;
556 ORE->emit(OptimizationRemark(DEBUG_TYPE, "tailcall-recursion", CI)
557 << "transforming tail recursion into loop");
555558
556559 // OK! We can transform this tail call. If this is the first one found,
557560 // create the new entry block, allowing us to branch back to the old entry.
665668 return true;
666669 }
667670
668 static bool foldReturnAndProcessPred(BasicBlock *BB, ReturnInst *Ret,
669 BasicBlock *&OldEntry,
670 bool &TailCallsAreMarkedTail,
671 SmallVectorImpl &ArgumentPHIs,
672 bool CannotTailCallElimCallsMarkedTail,
673 const TargetTransformInfo *TTI,
674 AliasAnalysis *AA) {
671 static bool foldReturnAndProcessPred(
672 BasicBlock *BB, ReturnInst *Ret, BasicBlock *&OldEntry,
673 bool &TailCallsAreMarkedTail, SmallVectorImpl &ArgumentPHIs,
674 bool CannotTailCallElimCallsMarkedTail, const TargetTransformInfo *TTI,
675 AliasAnalysis *AA, OptimizationRemarkEmitter *ORE) {
675676 bool Change = false;
676677
677678 // Make sure this block is a trivial return block.
707708 BB->eraseFromParent();
708709
709710 eliminateRecursiveTailCall(CI, RI, OldEntry, TailCallsAreMarkedTail,
710 ArgumentPHIs, AA);
711 ArgumentPHIs, AA, ORE);
711712 ++NumRetDuped;
712713 Change = true;
713714 }
721722 SmallVectorImpl &ArgumentPHIs,
722723 bool CannotTailCallElimCallsMarkedTail,
723724 const TargetTransformInfo *TTI,
724 AliasAnalysis *AA) {
725 AliasAnalysis *AA,
726 OptimizationRemarkEmitter *ORE) {
725727 CallInst *CI = findTRECandidate(Ret, CannotTailCallElimCallsMarkedTail, TTI);
726728 if (!CI)
727729 return false;
728730
729731 return eliminateRecursiveTailCall(CI, Ret, OldEntry, TailCallsAreMarkedTail,
730 ArgumentPHIs, AA);
732 ArgumentPHIs, AA, ORE);
731733 }
732734
733735 static bool eliminateTailRecursion(Function &F, const TargetTransformInfo *TTI,
734 AliasAnalysis *AA) {
736 AliasAnalysis *AA,
737 OptimizationRemarkEmitter *ORE) {
735738 if (F.getFnAttribute("disable-tail-calls").getValueAsString() == "true")
736739 return false;
737740
738741 bool MadeChange = false;
739742 bool AllCallsAreTailCalls = false;
740 MadeChange |= markTails(F, AllCallsAreTailCalls);
743 MadeChange |= markTails(F, AllCallsAreTailCalls, ORE);
741744 if (!AllCallsAreTailCalls)
742745 return MadeChange;
743746
764767 for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; /*in loop*/) {
765768 BasicBlock *BB = &*BBI++; // foldReturnAndProcessPred may delete BB.
766769 if (ReturnInst *Ret = dyn_cast(BB->getTerminator())) {
767 bool Change =
768 processReturningBlock(Ret, OldEntry, TailCallsAreMarkedTail,
769 ArgumentPHIs, !CanTRETailMarkedCall, TTI, AA);
770 bool Change = processReturningBlock(Ret, OldEntry, TailCallsAreMarkedTail,
771 ArgumentPHIs, !CanTRETailMarkedCall,
772 TTI, AA, ORE);
770773 if (!Change && BB->getFirstNonPHIOrDbg() == Ret)
771774 Change = foldReturnAndProcessPred(BB, Ret, OldEntry,
772775 TailCallsAreMarkedTail, ArgumentPHIs,
773 !CanTRETailMarkedCall, TTI, AA);
776 !CanTRETailMarkedCall, TTI, AA, ORE);
774777 MadeChange |= Change;
775778 }
776779 }
801804 void getAnalysisUsage(AnalysisUsage &AU) const override {
802805 AU.addRequired();
803806 AU.addRequired();
807 AU.addRequired();
804808 AU.addPreserved();
805809 }
806810
810814
811815 return eliminateTailRecursion(
812816 F, &getAnalysis().getTTI(F),
813 &getAnalysis().getAAResults());
817 &getAnalysis().getAAResults(),
818 &getAnalysis().getORE());
814819 }
815820 };
816821 }
819824 INITIALIZE_PASS_BEGIN(TailCallElim, "tailcallelim", "Tail Call Elimination",
820825 false, false)
821826 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
827 INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
822828 INITIALIZE_PASS_END(TailCallElim, "tailcallelim", "Tail Call Elimination",
823829 false, false)
824830
832838
833839 TargetTransformInfo &TTI = AM.getResult(F);
834840 AliasAnalysis &AA = AM.getResult(F);
835
836 bool Changed = eliminateTailRecursion(F, &TTI, &AA);
841 auto &ORE = AM.getResult(F);
842
843 bool Changed = eliminateTailRecursion(F, &TTI, &AA, &ORE);
837844
838845 if (!Changed)
839846 return PreservedAnalyses::all();
117117 ; CHECK-O3-NEXT: Running pass: LibCallsShrinkWrapPass
118118 ; CHECK-EP-PEEPHOLE-NEXT: Running pass: NoOpFunctionPass
119119 ; CHECK-O-NEXT: Running pass: TailCallElimPass
120 ; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
120121 ; CHECK-O-NEXT: Running pass: SimplifyCFGPass
121122 ; CHECK-O-NEXT: Running pass: ReassociatePass
122123 ; CHECK-O-NEXT: Running pass: RequireAnalysisPass<{{.*}}OptimizationRemarkEmitterAnalysis
123 ; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
124124 ; CHECK-O-NEXT: Running pass: FunctionToLoopPassAdaptor<{{.*}}LoopStandardAnalysisResults{{.*}}>
125125 ; CHECK-O-NEXT: Running analysis: LoopAnalysis
126126 ; CHECK-O-NEXT: Running analysis: ScalarEvolutionAnalysis
100100 ; CHECK-O2-NEXT: Running pass: LibCallsShrinkWrapPass
101101 ; CHECK-O3-NEXT: Running pass: LibCallsShrinkWrapPass
102102 ; CHECK-O-NEXT: Running pass: TailCallElimPass
103 ; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
103104 ; CHECK-O-NEXT: Running pass: SimplifyCFGPass
104105 ; CHECK-O-NEXT: Running pass: ReassociatePass
105106 ; CHECK-O-NEXT: Running pass: RequireAnalysisPass<{{.*}}OptimizationRemarkEmitterAnalysis
106 ; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
107107 ; CHECK-O-NEXT: Running pass: FunctionToLoopPassAdaptor<{{.*}}LoopStandardAnalysisResults{{.*}}>
108108 ; CHECK-O-NEXT: Running analysis: LoopAnalysis
109109 ; CHECK-O-NEXT: Running analysis: ScalarEvolutionAnalysis
0 ; RUN: opt %s -tailcallelim -pass-remarks=tailcallelim -o /dev/null 2>&1 | FileCheck %s
1 ; RUN: opt %s -o /dev/null -passes='require,tailcallelim' -pass-remarks=tailcallelim 2>&1 | FileCheck %s
2
3 ; CHECK: /home/davide/pat.c:2:20: marked as tail call candidate
4 define void @patatino() {
5 call void @tinky(), !dbg !8
6 ret void
7 }
8
9 declare void @tinky()
10
11
12 !llvm.dbg.cu = !{!0}
13 !llvm.module.flags = !{!3, !4}
14 !llvm.ident = !{!5}
15
16 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.9.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug, enums: !2)
17 !1 = !DIFile(filename: "/home/davide/pat.c", directory: "/tmp")
18 !2 = !{}
19 !3 = !{i32 2, !"Debug Info Version", i32 3}
20 !4 = !{i32 1, !"PIC Level", i32 2}
21 !5 = !{!"clang version 3.9.0 "}
22 !6 = distinct !DISubprogram(name: "success", scope: !1, file: !1, line: 1, type: !7, isLocal: false, isDefinition: true, scopeLine: 1, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !2)
23 !7 = !DISubroutineType(types: !2)
24 !8 = !DILocation(line: 2, column: 20, scope: !6)