Skip to content

Commit

Permalink
[TRE] Move to the new OptRemark API.
Browse files Browse the repository at this point in the history
Fixes PR33788.

Differential Revision:  https://reviews.llvm.org/D35570

llvm-svn: 308524
  • Loading branch information
dcci committed Jul 19, 2017
1 parent 7eaf3d4 commit 4b8c8ea
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 30 deletions.
63 changes: 35 additions & 28 deletions llvm/lib/Transforms/Scalar/TailRecursionElimination.cpp
Expand Up @@ -60,6 +60,7 @@
#include "llvm/Analysis/InlineCost.h"
#include "llvm/Analysis/InstructionSimplify.h"
#include "llvm/Analysis/Loads.h"
#include "llvm/Analysis/OptimizationDiagnosticInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/CallSite.h"
Expand Down Expand Up @@ -177,7 +178,8 @@ struct AllocaDerivedValueTracker {
};
}

static bool markTails(Function &F, bool &AllCallsAreTailCalls) {
static bool markTails(Function &F, bool &AllCallsAreTailCalls,
OptimizationRemarkEmitter *ORE) {
if (F.callsFunctionThatReturnsTwice())
return false;
AllCallsAreTailCalls = true;
Expand Down Expand Up @@ -252,9 +254,9 @@ static bool markTails(Function &F, bool &AllCallsAreTailCalls) {
break;
}
if (SafeToTail) {
emitOptimizationRemark(
F.getContext(), "tailcallelim", F, CI->getDebugLoc(),
"marked this readnone call a tail call candidate");
using namespace ore;
ORE->emit(OptimizationRemark(DEBUG_TYPE, "tailcall-readnone", CI)
<< "marked as tail call candidate (readnone)");
CI->setTailCall();
Modified = true;
continue;
Expand Down Expand Up @@ -299,9 +301,8 @@ static bool markTails(Function &F, bool &AllCallsAreTailCalls) {
if (Visited[CI->getParent()] != ESCAPED) {
// If the escape point was part way through the block, calls after the
// escape point wouldn't have been put into DeferredTails.
emitOptimizationRemark(F.getContext(), "tailcallelim", F,
CI->getDebugLoc(),
"marked this call a tail call candidate");
ORE->emit(OptimizationRemark(DEBUG_TYPE, "tailcall", CI)
<< "marked as tail call candidate");
CI->setTailCall();
Modified = true;
} else {
Expand Down Expand Up @@ -491,7 +492,8 @@ static bool eliminateRecursiveTailCall(CallInst *CI, ReturnInst *Ret,
BasicBlock *&OldEntry,
bool &TailCallsAreMarkedTail,
SmallVectorImpl<PHINode *> &ArgumentPHIs,
AliasAnalysis *AA) {
AliasAnalysis *AA,
OptimizationRemarkEmitter *ORE) {
// If we are introducing accumulator recursion to eliminate operations after
// the call instruction that are both associative and commutative, the initial
// value for the accumulator is placed in this variable. If this value is set
Expand Down Expand Up @@ -551,8 +553,9 @@ static bool eliminateRecursiveTailCall(CallInst *CI, ReturnInst *Ret,
BasicBlock *BB = Ret->getParent();
Function *F = BB->getParent();

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

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

static bool foldReturnAndProcessPred(BasicBlock *BB, ReturnInst *Ret,
BasicBlock *&OldEntry,
bool &TailCallsAreMarkedTail,
SmallVectorImpl<PHINode *> &ArgumentPHIs,
bool CannotTailCallElimCallsMarkedTail,
const TargetTransformInfo *TTI,
AliasAnalysis *AA) {
static bool foldReturnAndProcessPred(
BasicBlock *BB, ReturnInst *Ret, BasicBlock *&OldEntry,
bool &TailCallsAreMarkedTail, SmallVectorImpl<PHINode *> &ArgumentPHIs,
bool CannotTailCallElimCallsMarkedTail, const TargetTransformInfo *TTI,
AliasAnalysis *AA, OptimizationRemarkEmitter *ORE) {
bool Change = false;

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

eliminateRecursiveTailCall(CI, RI, OldEntry, TailCallsAreMarkedTail,
ArgumentPHIs, AA);
ArgumentPHIs, AA, ORE);
++NumRetDuped;
Change = true;
}
Expand All @@ -722,23 +723,25 @@ static bool processReturningBlock(ReturnInst *Ret, BasicBlock *&OldEntry,
SmallVectorImpl<PHINode *> &ArgumentPHIs,
bool CannotTailCallElimCallsMarkedTail,
const TargetTransformInfo *TTI,
AliasAnalysis *AA) {
AliasAnalysis *AA,
OptimizationRemarkEmitter *ORE) {
CallInst *CI = findTRECandidate(Ret, CannotTailCallElimCallsMarkedTail, TTI);
if (!CI)
return false;

return eliminateRecursiveTailCall(CI, Ret, OldEntry, TailCallsAreMarkedTail,
ArgumentPHIs, AA);
ArgumentPHIs, AA, ORE);
}

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

bool MadeChange = false;
bool AllCallsAreTailCalls = false;
MadeChange |= markTails(F, AllCallsAreTailCalls);
MadeChange |= markTails(F, AllCallsAreTailCalls, ORE);
if (!AllCallsAreTailCalls)
return MadeChange;

Expand All @@ -765,13 +768,13 @@ static bool eliminateTailRecursion(Function &F, const TargetTransformInfo *TTI,
for (Function::iterator BBI = F.begin(), E = F.end(); BBI != E; /*in loop*/) {
BasicBlock *BB = &*BBI++; // foldReturnAndProcessPred may delete BB.
if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB->getTerminator())) {
bool Change =
processReturningBlock(Ret, OldEntry, TailCallsAreMarkedTail,
ArgumentPHIs, !CanTRETailMarkedCall, TTI, AA);
bool Change = processReturningBlock(Ret, OldEntry, TailCallsAreMarkedTail,
ArgumentPHIs, !CanTRETailMarkedCall,
TTI, AA, ORE);
if (!Change && BB->getFirstNonPHIOrDbg() == Ret)
Change = foldReturnAndProcessPred(BB, Ret, OldEntry,
TailCallsAreMarkedTail, ArgumentPHIs,
!CanTRETailMarkedCall, TTI, AA);
!CanTRETailMarkedCall, TTI, AA, ORE);
MadeChange |= Change;
}
}
Expand Down Expand Up @@ -802,6 +805,7 @@ struct TailCallElim : public FunctionPass {
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<TargetTransformInfoWrapperPass>();
AU.addRequired<AAResultsWrapperPass>();
AU.addRequired<OptimizationRemarkEmitterWrapperPass>();
AU.addPreserved<GlobalsAAWrapperPass>();
}

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

return eliminateTailRecursion(
F, &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F),
&getAnalysis<AAResultsWrapperPass>().getAAResults());
&getAnalysis<AAResultsWrapperPass>().getAAResults(),
&getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE());
}
};
}
Expand All @@ -820,6 +825,7 @@ char TailCallElim::ID = 0;
INITIALIZE_PASS_BEGIN(TailCallElim, "tailcallelim", "Tail Call Elimination",
false, false)
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
INITIALIZE_PASS_DEPENDENCY(OptimizationRemarkEmitterWrapperPass)
INITIALIZE_PASS_END(TailCallElim, "tailcallelim", "Tail Call Elimination",
false, false)

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

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

bool Changed = eliminateTailRecursion(F, &TTI, &AA);
bool Changed = eliminateTailRecursion(F, &TTI, &AA, &ORE);

if (!Changed)
return PreservedAnalyses::all();
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Other/new-pm-defaults.ll
Expand Up @@ -118,10 +118,10 @@
; CHECK-O3-NEXT: Running pass: LibCallsShrinkWrapPass
; CHECK-EP-PEEPHOLE-NEXT: Running pass: NoOpFunctionPass
; CHECK-O-NEXT: Running pass: TailCallElimPass
; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
; CHECK-O-NEXT: Running pass: ReassociatePass
; CHECK-O-NEXT: Running pass: RequireAnalysisPass<{{.*}}OptimizationRemarkEmitterAnalysis
; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
; CHECK-O-NEXT: Running pass: FunctionToLoopPassAdaptor<{{.*}}LoopStandardAnalysisResults{{.*}}>
; CHECK-O-NEXT: Running analysis: LoopAnalysis
; CHECK-O-NEXT: Running analysis: ScalarEvolutionAnalysis
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Other/new-pm-thinlto-defaults.ll
Expand Up @@ -101,10 +101,10 @@
; CHECK-O2-NEXT: Running pass: LibCallsShrinkWrapPass
; CHECK-O3-NEXT: Running pass: LibCallsShrinkWrapPass
; CHECK-O-NEXT: Running pass: TailCallElimPass
; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
; CHECK-O-NEXT: Running pass: SimplifyCFGPass
; CHECK-O-NEXT: Running pass: ReassociatePass
; CHECK-O-NEXT: Running pass: RequireAnalysisPass<{{.*}}OptimizationRemarkEmitterAnalysis
; CHECK-O-NEXT: Running analysis: OptimizationRemarkEmitterAnalysis
; CHECK-O-NEXT: Running pass: FunctionToLoopPassAdaptor<{{.*}}LoopStandardAnalysisResults{{.*}}>
; CHECK-O-NEXT: Running analysis: LoopAnalysis
; CHECK-O-NEXT: Running analysis: ScalarEvolutionAnalysis
Expand Down
25 changes: 25 additions & 0 deletions llvm/test/Transforms/TailCallElim/opt-remarks.ll
@@ -0,0 +1,25 @@
; RUN: opt %s -tailcallelim -pass-remarks=tailcallelim -o /dev/null 2>&1 | FileCheck %s
; RUN: opt %s -o /dev/null -passes='require<opt-remark-emit>,tailcallelim' -pass-remarks=tailcallelim 2>&1 | FileCheck %s

; CHECK: /home/davide/pat.c:2:20: marked as tail call candidate
define void @patatino() {
call void @tinky(), !dbg !8
ret void
}

declare void @tinky()


!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!3, !4}
!llvm.ident = !{!5}

!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 3.9.0 ", isOptimized: true, runtimeVersion: 0, emissionKind: NoDebug, enums: !2)
!1 = !DIFile(filename: "/home/davide/pat.c", directory: "/tmp")
!2 = !{}
!3 = !{i32 2, !"Debug Info Version", i32 3}
!4 = !{i32 1, !"PIC Level", i32 2}
!5 = !{!"clang version 3.9.0 "}
!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)
!7 = !DISubroutineType(types: !2)
!8 = !DILocation(line: 2, column: 20, scope: !6)

0 comments on commit 4b8c8ea

Please sign in to comment.