Skip to content

Commit 4b8c8ea

Browse files
committed
[TRE] Move to the new OptRemark API.
Fixes PR33788. Differential Revision: https://reviews.llvm.org/D35570 llvm-svn: 308524
1 parent 7eaf3d4 commit 4b8c8ea

File tree

4 files changed

+62
-30
lines changed

4 files changed

+62
-30
lines changed

llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include "llvm/Analysis/InlineCost.h"
6161
#include "llvm/Analysis/InstructionSimplify.h"
6262
#include "llvm/Analysis/Loads.h"
63+
#include "llvm/Analysis/OptimizationDiagnosticInfo.h"
6364
#include "llvm/Analysis/TargetTransformInfo.h"
6465
#include "llvm/IR/CFG.h"
6566
#include "llvm/IR/CallSite.h"
@@ -177,7 +178,8 @@ struct AllocaDerivedValueTracker {
177178
};
178179
}
179180

180-
static bool markTails(Function &F, bool &AllCallsAreTailCalls) {
181+
static bool markTails(Function &F, bool &AllCallsAreTailCalls,
182+
OptimizationRemarkEmitter *ORE) {
181183
if (F.callsFunctionThatReturnsTwice())
182184
return false;
183185
AllCallsAreTailCalls = true;
@@ -252,9 +254,9 @@ static bool markTails(Function &F, bool &AllCallsAreTailCalls) {
252254
break;
253255
}
254256
if (SafeToTail) {
255-
emitOptimizationRemark(
256-
F.getContext(), "tailcallelim", F, CI->getDebugLoc(),
257-
"marked this readnone call a tail call candidate");
257+
using namespace ore;
258+
ORE->emit(OptimizationRemark(DEBUG_TYPE, "tailcall-readnone", CI)
259+
<< "marked as tail call candidate (readnone)");
258260
CI->setTailCall();
259261
Modified = true;
260262
continue;
@@ -299,9 +301,8 @@ static bool markTails(Function &F, bool &AllCallsAreTailCalls) {
299301
if (Visited[CI->getParent()] != ESCAPED) {
300302
// If the escape point was part way through the block, calls after the
301303
// escape point wouldn't have been put into DeferredTails.
302-
emitOptimizationRemark(F.getContext(), "tailcallelim", F,
303-
CI->getDebugLoc(),
304-
"marked this call a tail call candidate");
304+
ORE->emit(OptimizationRemark(DEBUG_TYPE, "tailcall", CI)
305+
<< "marked as tail call candidate");
305306
CI->setTailCall();
306307
Modified = true;
307308
} else {
@@ -491,7 +492,8 @@ static bool eliminateRecursiveTailCall(CallInst *CI, ReturnInst *Ret,
491492
BasicBlock *&OldEntry,
492493
bool &TailCallsAreMarkedTail,
493494
SmallVectorImpl<PHINode *> &ArgumentPHIs,
494-
AliasAnalysis *AA) {
495+
AliasAnalysis *AA,
496+
OptimizationRemarkEmitter *ORE) {
495497
// If we are introducing accumulator recursion to eliminate operations after
496498
// the call instruction that are both associative and commutative, the initial
497499
// value for the accumulator is placed in this variable. If this value is set
@@ -551,8 +553,9 @@ static bool eliminateRecursiveTailCall(CallInst *CI, ReturnInst *Ret,
551553
BasicBlock *BB = Ret->getParent();
552554
Function *F = BB->getParent();
553555

554-
emitOptimizationRemark(F->getContext(), "tailcallelim", *F, CI->getDebugLoc(),
555-
"transforming tail recursion to loop");
556+
using namespace ore;
557+
ORE->emit(OptimizationRemark(DEBUG_TYPE, "tailcall-recursion", CI)
558+
<< "transforming tail recursion into loop");
556559

557560
// OK! We can transform this tail call. If this is the first one found,
558561
// create the new entry block, allowing us to branch back to the old entry.
@@ -666,13 +669,11 @@ static bool eliminateRecursiveTailCall(CallInst *CI, ReturnInst *Ret,
666669
return true;
667670
}
668671

669-
static bool foldReturnAndProcessPred(BasicBlock *BB, ReturnInst *Ret,
670-
BasicBlock *&OldEntry,
671-
bool &TailCallsAreMarkedTail,
672-
SmallVectorImpl<PHINode *> &ArgumentPHIs,
673-
bool CannotTailCallElimCallsMarkedTail,
674-
const TargetTransformInfo *TTI,
675-
AliasAnalysis *AA) {
672+
static bool foldReturnAndProcessPred(
673+
BasicBlock *BB, ReturnInst *Ret, BasicBlock *&OldEntry,
674+
bool &TailCallsAreMarkedTail, SmallVectorImpl<PHINode *> &ArgumentPHIs,
675+
bool CannotTailCallElimCallsMarkedTail, const TargetTransformInfo *TTI,
676+
AliasAnalysis *AA, OptimizationRemarkEmitter *ORE) {
676677
bool Change = false;
677678

678679
// Make sure this block is a trivial return block.
@@ -708,7 +709,7 @@ static bool foldReturnAndProcessPred(BasicBlock *BB, ReturnInst *Ret,
708709
BB->eraseFromParent();
709710

710711
eliminateRecursiveTailCall(CI, RI, OldEntry, TailCallsAreMarkedTail,
711-
ArgumentPHIs, AA);
712+
ArgumentPHIs, AA, ORE);
712713
++NumRetDuped;
713714
Change = true;
714715
}
@@ -722,23 +723,25 @@ static bool processReturningBlock(ReturnInst *Ret, BasicBlock *&OldEntry,
722723
SmallVectorImpl<PHINode *> &ArgumentPHIs,
723724
bool CannotTailCallElimCallsMarkedTail,
724725
const TargetTransformInfo *TTI,
725-
AliasAnalysis *AA) {
726+
AliasAnalysis *AA,
727+
OptimizationRemarkEmitter *ORE) {
726728
CallInst *CI = findTRECandidate(Ret, CannotTailCallElimCallsMarkedTail, TTI);
727729
if (!CI)
728730
return false;
729731

730732
return eliminateRecursiveTailCall(CI, Ret, OldEntry, TailCallsAreMarkedTail,
731-
ArgumentPHIs, AA);
733+
ArgumentPHIs, AA, ORE);
732734
}
733735

734736
static bool eliminateTailRecursion(Function &F, const TargetTransformInfo *TTI,
735-
AliasAnalysis *AA) {
737+
AliasAnalysis *AA,
738+
OptimizationRemarkEmitter *ORE) {
736739
if (F.getFnAttribute("disable-tail-calls").getValueAsString() == "true")
737740
return false;
738741

739742
bool MadeChange = false;
740743
bool AllCallsAreTailCalls = false;
741-
MadeChange |= markTails(F, AllCallsAreTailCalls);
744+
MadeChange |= markTails(F, AllCallsAreTailCalls, ORE);
742745
if (!AllCallsAreTailCalls)
743746
return MadeChange;
744747

@@ -765,13 +768,13 @@ static bool eliminateTailRecursion(Function &F, const TargetTransformInfo *TTI,
765768
for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; /*in loop*/) {
766769
BasicBlock *BB = &*BBI++; // foldReturnAndProcessPred may delete BB.
767770
if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB->getTerminator())) {
768-
bool Change =
769-
processReturningBlock(Ret, OldEntry, TailCallsAreMarkedTail,
770-
ArgumentPHIs, !CanTRETailMarkedCall, TTI, AA);
771+
bool Change = processReturningBlock(Ret, OldEntry, TailCallsAreMarkedTail,
772+
ArgumentPHIs, !CanTRETailMarkedCall,
773+
TTI, AA, ORE);
771774
if (!Change && BB->getFirstNonPHIOrDbg() == Ret)
772775
Change = foldReturnAndProcessPred(BB, Ret, OldEntry,
773776
TailCallsAreMarkedTail, ArgumentPHIs,
774-
!CanTRETailMarkedCall, TTI, AA);
777+
!CanTRETailMarkedCall, TTI, AA, ORE);
775778
MadeChange |= Change;
776779
}
777780
}
@@ -802,6 +805,7 @@ struct TailCallElim : public FunctionPass {
802805
void getAnalysisUsage(AnalysisUsage &AU) const override {
803806
AU.addRequired<TargetTransformInfoWrapperPass>();
804807
AU.addRequired<AAResultsWrapperPass>();
808+
AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
805809
AU.addPreserved<GlobalsAAWrapperPass>();
806810
}
807811

@@ -811,7 +815,8 @@ struct TailCallElim : public FunctionPass {
811815

812816
return eliminateTailRecursion(
813817
F, &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F),
814-
&getAnalysis<AAResultsWrapperPass>().getAAResults());
818+
&getAnalysis<AAResultsWrapperPass>().getAAResults(),
819+
&getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE());
815820
}
816821
};
817822
}
@@ -820,6 +825,7 @@ char TailCallElim::ID = 0;
820825
INITIALIZE_PASS_BEGIN(TailCallElim, "tailcallelim", "Tail Call Elimination",
821826
false, false)
822827
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
828+
INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
823829
INITIALIZE_PASS_END(TailCallElim, "tailcallelim", "Tail Call Elimination",
824830
false, false)
825831

@@ -833,8 +839,9 @@ PreservedAnalyses TailCallElimPass::run(Function &F,
833839

834840
TargetTransformInfo &TTI = AM.getResult<TargetIRAnalysis>(F);
835841
AliasAnalysis &AA = AM.getResult<AAManager>(F);
842+
auto &ORE = AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
836843

837-
bool Changed = eliminateTailRecursion(F, &TTI, &AA);
844+
bool Changed = eliminateTailRecursion(F, &TTI, &AA, &ORE);
838845

839846
if (!Changed)
840847
return PreservedAnalyses::all();

llvm/test/Other/new-pm-defaults.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,10 @@
118118
; CHECK-O3-NEXT: Running pass: LibCallsShrinkWrapPass
119119
; CHECK-EP-PEEPHOLE-NEXT: Running pass: NoOpFunctionPass
120120
; CHECK-O-NEXT: Running pass: TailCallElimPass
121+
; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
121122
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
122123
; CHECK-O-NEXT: Running pass: ReassociatePass
123124
; CHECK-O-NEXT: Running pass: RequireAnalysisPass<{{.*}}OptimizationRemarkEmitterAnalysis
124-
; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
125125
; CHECK-O-NEXT: Running pass: FunctionToLoopPassAdaptor<{{.*}}LoopStandardAnalysisResults{{.*}}>
126126
; CHECK-O-NEXT: Running analysis: LoopAnalysis
127127
; CHECK-O-NEXT: Running analysis: ScalarEvolutionAnalysis

llvm/test/Other/new-pm-thinlto-defaults.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@
101101
; CHECK-O2-NEXT: Running pass: LibCallsShrinkWrapPass
102102
; CHECK-O3-NEXT: Running pass: LibCallsShrinkWrapPass
103103
; CHECK-O-NEXT: Running pass: TailCallElimPass
104+
; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
104105
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
105106
; CHECK-O-NEXT: Running pass: ReassociatePass
106107
; CHECK-O-NEXT: Running pass: RequireAnalysisPass<{{.*}}OptimizationRemarkEmitterAnalysis
107-
; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
108108
; CHECK-O-NEXT: Running pass: FunctionToLoopPassAdaptor<{{.*}}LoopStandardAnalysisResults{{.*}}>
109109
; CHECK-O-NEXT: Running analysis: LoopAnalysis
110110
; CHECK-O-NEXT: Running analysis: ScalarEvolutionAnalysis
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: opt %s -tailcallelim -pass-remarks=tailcallelim -o /dev/null 2>&1 | FileCheck %s
2+
; RUN: opt %s -o /dev/null -passes='require<opt-remark-emit>,tailcallelim' -pass-remarks=tailcallelim 2>&1 | FileCheck %s
3+
4+
; CHECK: /home/davide/pat.c:2:20: marked as tail call candidate
5+
define void @patatino() {
6+
call void @tinky(), !dbg !8
7+
ret void
8+
}
9+
10+
declare void @tinky()
11+
12+
13+
!llvm.dbg.cu = !{!0}
14+
!llvm.module.flags = !{!3, !4}
15+
!llvm.ident = !{!5}
16+
17+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.9.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug, enums: !2)
18+
!1 = !DIFile(filename: "/home/davide/pat.c", directory: "/tmp")
19+
!2 = !{}
20+
!3 = !{i32 2, !"Debug Info Version", i32 3}
21+
!4 = !{i32 1, !"PIC Level", i32 2}
22+
!5 = !{!"clang version 3.9.0 "}
23+
!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)
24+
!7 = !DISubroutineType(types: !2)
25+
!8 = !DILocation(line: 2, column: 20, scope: !6)

0 commit comments

Comments
 (0)