llvm.org GIT mirror llvm / 9e8e51f
All libcalls should be considered to be GC-leaf functions. Summary: It is possible for some passes to materialize a call to a libcall (ex: ldexp, exp2, etc), but these passes will not mark the call as a gc-leaf-function. All libcalls are actually gc-leaf-functions, so we change llvm::callsGCLeafFunction() to tell us that available libcalls are equivalent to gc-leaf-function calls. Reviewers: sanjoy, anna, reames Reviewed By: anna Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D35840 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@309291 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Neilson 2 years ago
6 changed file(s) with 90 addition(s) and 15 deletion(s). Raw diff Collapse all Expand all
377377 ///
378378 /// Most passes can and should ignore this information, and it is only used
379379 /// during lowering by the GC infrastructure.
380 bool callsGCLeafFunction(ImmutableCallSite CS);
380 bool callsGCLeafFunction(ImmutableCallSite CS, const TargetLibraryInfo &TLI);
381381
382382 /// Copy a nonnull metadata node to a new load instruction.
383383 ///
5353 #include "llvm/ADT/Statistic.h"
5454 #include "llvm/Analysis/CFG.h"
5555 #include "llvm/Analysis/ScalarEvolution.h"
56 #include "llvm/Analysis/TargetLibraryInfo.h"
5657 #include "llvm/IR/CallSite.h"
5758 #include "llvm/IR/Dominators.h"
5859 #include "llvm/IR/IntrinsicInst.h"
112113 ScalarEvolution *SE = nullptr;
113114 DominatorTree *DT = nullptr;
114115 LoopInfo *LI = nullptr;
116 TargetLibraryInfo *TLI = nullptr;
115117
116118 PlaceBackedgeSafepointsImpl(bool CallSafepoints = false)
117119 : FunctionPass(ID), CallSafepointsEnabled(CallSafepoints) {
130132 SE = &getAnalysis().getSE();
131133 DT = &getAnalysis().getDomTree();
132134 LI = &getAnalysis().getLoopInfo();
135 TLI = &getAnalysis().getTLI();
133136 for (Loop *I : *LI) {
134137 runOnLoopAndSubLoops(I);
135138 }
140143 AU.addRequired();
141144 AU.addRequired();
142145 AU.addRequired();
146 AU.addRequired();
143147 // We no longer modify the IR at all in this pass. Thus all
144148 // analysis are preserved.
145149 AU.setPreservesAll();
164168 // We modify the graph wholesale (inlining, block insertion, etc). We
165169 // preserve nothing at the moment. We could potentially preserve dom tree
166170 // if that was worth doing
171 AU.addRequired();
167172 }
168173 };
169174 }
173178 // callers job.
174179 static void
175180 InsertSafepointPoll(Instruction *InsertBefore,
176 std::vector &ParsePointsNeeded /*rval*/);
177
178 static bool needsStatepoint(const CallSite &CS) {
179 if (callsGCLeafFunction(CS))
181 std::vector &ParsePointsNeeded /*rval*/,
182 const TargetLibraryInfo &TLI);
183
184 static bool needsStatepoint(const CallSite &CS, const TargetLibraryInfo &TLI) {
185 if (callsGCLeafFunction(CS, TLI))
180186 return false;
181187 if (CS.isCall()) {
182188 CallInst *call = cast(CS.getInstruction());
193199 /// answer; i.e. false is always valid.
194200 static bool containsUnconditionalCallSafepoint(Loop *L, BasicBlock *Header,
195201 BasicBlock *Pred,
196 DominatorTree &DT) {
202 DominatorTree &DT,
203 const TargetLibraryInfo &TLI) {
197204 // In general, we're looking for any cut of the graph which ensures
198205 // there's a call safepoint along every edge between Header and Pred.
199206 // For the moment, we look only for the 'cuts' that consist of a single call
216223 // unconditional poll. In practice, this is only a theoretical concern
217224 // since we don't have any methods with conditional-only safepoint
218225 // polls.
219 if (needsStatepoint(CS))
226 if (needsStatepoint(CS, TLI))
220227 return true;
221228 }
222229
320327 continue;
321328 }
322329 if (CallSafepointsEnabled &&
323 containsUnconditionalCallSafepoint(L, Header, Pred, *DT)) {
330 containsUnconditionalCallSafepoint(L, Header, Pred, *DT, *TLI)) {
324331 // Note: This is only semantically legal since we won't do any further
325332 // IPO or inlining before the actual call insertion.. If we hadn't, we
326333 // might latter loose this call safepoint.
471478 if (!shouldRewriteFunction(F))
472479 return false;
473480
481 const TargetLibraryInfo &TLI =
482 getAnalysis().getTLI();
483
474484 bool Modified = false;
475485
476486 // In various bits below, we rely on the fact that uses are reachable from
577587 // safepoint polls themselves.
578588 for (Instruction *PollLocation : PollsNeeded) {
579589 std::vector RuntimeCalls;
580 InsertSafepointPoll(PollLocation, RuntimeCalls);
590 InsertSafepointPoll(PollLocation, RuntimeCalls, TLI);
581591 ParsePointNeeded.insert(ParsePointNeeded.end(), RuntimeCalls.begin(),
582592 RuntimeCalls.end());
583593 }
609619
610620 static void
611621 InsertSafepointPoll(Instruction *InsertBefore,
612 std::vector &ParsePointsNeeded /*rval*/) {
622 std::vector &ParsePointsNeeded /*rval*/,
623 const TargetLibraryInfo &TLI) {
613624 BasicBlock *OrigBB = InsertBefore->getParent();
614625 Module *M = InsertBefore->getModule();
615626 assert(M && "must be part of a module");
668679 assert(ParsePointsNeeded.empty());
669680 for (auto *CI : Calls) {
670681 // No safepoint needed or wanted
671 if (!needsStatepoint(CI))
682 if (!needsStatepoint(CI, TLI))
672683 continue;
673684
674685 // These are likely runtime calls. Should we assert that via calling
1818 #include "llvm/ADT/Statistic.h"
1919 #include "llvm/ADT/StringRef.h"
2020 #include "llvm/Analysis/CFG.h"
21 #include "llvm/Analysis/TargetLibraryInfo.h"
2122 #include "llvm/Analysis/TargetTransformInfo.h"
2223 #include "llvm/IR/BasicBlock.h"
2324 #include "llvm/IR/CallSite.h"
102103 // else. We could in theory preserve a lot more analyses here.
103104 AU.addRequired();
104105 AU.addRequired();
106 AU.addRequired();
105107 }
106108
107109 /// The IR fed into RewriteStatepointsForGC may have had attributes and
24182420 DominatorTree &DT = getAnalysis(F).getDomTree();
24192421 TargetTransformInfo &TTI =
24202422 getAnalysis().getTTI(F);
2421
2422 auto NeedsRewrite = [](Instruction &I) {
2423 const TargetLibraryInfo &TLI =
2424 getAnalysis().getTLI();
2425
2426 auto NeedsRewrite = [&TLI](Instruction &I) {
24232427 if (ImmutableCallSite CS = ImmutableCallSite(&I))
2424 return !callsGCLeafFunction(CS) && !isStatepoint(CS);
2428 return !callsGCLeafFunction(CS, TLI) && !isStatepoint(CS);
24252429 return false;
24262430 };
24272431
18311831 return ::replaceDominatedUsesWith(From, To, BB, ProperlyDominates);
18321832 }
18331833
1834 bool llvm::callsGCLeafFunction(ImmutableCallSite CS) {
1834 bool llvm::callsGCLeafFunction(ImmutableCallSite CS,
1835 const TargetLibraryInfo &TLI) {
18351836 // Check if the function is specifically marked as a gc leaf function.
18361837 if (CS.hasFnAttr("gc-leaf-function"))
18371838 return true;
18431844 // Most LLVM intrinsics do not take safepoints.
18441845 return IID != Intrinsic::experimental_gc_statepoint &&
18451846 IID != Intrinsic::experimental_deoptimize;
1847 }
1848
1849 // Lib calls can be materialized by some passes, and won't be
1850 // marked as 'gc-leaf-function.' All available Libcalls are
1851 // GC-leaf.
1852 LibFunc LF;
1853 if (TLI.getLibFunc(CS, LF)) {
1854 return TLI.has(LF);
18461855 }
18471856
18481857 return false;
0 ; RUN: opt -S -place-safepoints < %s | FileCheck %s
1
2 ; Libcalls will not contain a safepoint poll, so check that we insert
3 ; a safepoint in a loop containing a libcall.
4 declare double @ldexp(double %x, i32 %n) nounwind readnone
5 define double @test_libcall(double %x) gc "statepoint-example" {
6 ; CHECK-LABEL: test_libcall
7
8 entry:
9 ; CHECK: entry
10 ; CHECK-NEXT: call void @do_safepoint
11 ; CHECK-NEXT: br label %loop
12 br label %loop
13
14 loop:
15 ; CHECK: loop
16 ; CHECK-NEXT: %x_loop = phi double [ %x, %entry ], [ %x_exp, %loop ]
17 ; CHECK-NEXT: %x_exp = call double @ldexp(double %x_loop, i32 5)
18 ; CHECK-NEXT: %done = fcmp ogt double %x_exp, 1.5
19 ; CHECK-NEXT: call void @do_safepoint
20 %x_loop = phi double [ %x, %entry ], [ %x_exp, %loop ]
21 %x_exp = call double @ldexp(double %x_loop, i32 5) nounwind readnone
22 %done = fcmp ogt double %x_exp, 1.5
23 br i1 %done, label %end, label %loop
24 end:
25 %x_end = phi double [%x_exp, %loop]
26 ret double %x_end
27 }
28
29 ; This function is inlined when inserting a poll.
30 declare void @do_safepoint()
31 define void @gc.safepoint_poll() {
32 ; CHECK-LABEL: gc.safepoint_poll
33 entry:
34 call void @do_safepoint()
35 ret void
36 }
0 ; A call to a libcall function is not a statepoint.
1 ; This test verifies that calls to libcalls functions do not get converted to
2 ; statepoint calls.
3 ; RUN: opt -S -rewrite-statepoints-for-gc < %s | FileCheck %s
4
5 declare double @ldexp(double %x, i32 %n) nounwind readnone
6
7 define double @test_libcall(double %x) gc "statepoint-example" {
8 ; CHECK-LABEL: test_libcall
9 ; CHECK-NEXT: %res = call double @ldexp(double %x, i32 5)
10 ; CHECK-NEXT: ret double %res
11 %res = call double @ldexp(double %x, i32 5) nounwind readnone
12 ret double %res
13 }