Mercurial > hg > Members > tobaru > cbc > CbC_llvm
annotate lib/Transforms/Scalar/TailRecursionElimination.cpp @ 85:5e5d649e25d2
Update LLVM 3.7
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 19 Feb 2015 15:19:25 +0900 |
parents | e218c19a8176 60c9769439b8 |
children | b0dd3743370f |
rev | line source |
---|---|
0 | 1 //===- TailRecursionElimination.cpp - Eliminate Tail Calls ----------------===// |
2 // | |
3 // The LLVM Compiler Infrastructure | |
4 // | |
5 // This file is distributed under the University of Illinois Open Source | |
6 // License. See LICENSE.TXT for details. | |
7 // | |
8 //===----------------------------------------------------------------------===// | |
9 // | |
10 // This file transforms calls of the current function (self recursion) followed | |
11 // by a return instruction with a branch to the entry of the function, creating | |
12 // a loop. This pass also implements the following extensions to the basic | |
13 // algorithm: | |
14 // | |
15 // 1. Trivial instructions between the call and return do not prevent the | |
16 // transformation from taking place, though currently the analysis cannot | |
17 // support moving any really useful instructions (only dead ones). | |
18 // 2. This pass transforms functions that are prevented from being tail | |
19 // recursive by an associative and commutative expression to use an | |
20 // accumulator variable, thus compiling the typical naive factorial or | |
21 // 'fib' implementation into efficient code. | |
22 // 3. TRE is performed if the function returns void, if the return | |
23 // returns the result returned by the call, or if the function returns a | |
24 // run-time constant on all exits from the function. It is possible, though | |
25 // unlikely, that the return returns something else (like constant 0), and | |
26 // can still be TRE'd. It can be TRE'd if ALL OTHER return instructions in | |
27 // the function return the exact same value. | |
28 // 4. If it can prove that callees do not access their caller stack frame, | |
29 // they are marked as eligible for tail call elimination (by the code | |
30 // generator). | |
31 // | |
32 // There are several improvements that could be made: | |
33 // | |
34 // 1. If the function has any alloca instructions, these instructions will be | |
35 // moved out of the entry block of the function, causing them to be | |
36 // evaluated each time through the tail recursion. Safely keeping allocas | |
37 // in the entry block requires analysis to proves that the tail-called | |
38 // function does not read or write the stack object. | |
39 // 2. Tail recursion is only performed if the call immediately precedes the | |
40 // return instruction. It's possible that there could be a jump between | |
41 // the call and the return. | |
42 // 3. There can be intervening operations between the call and the return that | |
43 // prevent the TRE from occurring. For example, there could be GEP's and | |
44 // stores to memory that will not be read or written by the call. This | |
45 // requires some substantial analysis (such as with DSA) to prove safe to | |
46 // move ahead of the call, but doing so could allow many more TREs to be | |
47 // performed, for example in TreeAdd/TreeAlloc from the treeadd benchmark. | |
48 // 4. The algorithm we use to detect if callees access their caller stack | |
49 // frames is very primitive. | |
50 // | |
51 //===----------------------------------------------------------------------===// | |
52 | |
53 #include "llvm/Transforms/Scalar.h" | |
54 #include "llvm/ADT/STLExtras.h" | |
55 #include "llvm/ADT/SmallPtrSet.h" | |
56 #include "llvm/ADT/Statistic.h" | |
57 #include "llvm/Analysis/CaptureTracking.h" | |
77 | 58 #include "llvm/Analysis/CFG.h" |
0 | 59 #include "llvm/Analysis/InlineCost.h" |
60 #include "llvm/Analysis/InstructionSimplify.h" | |
61 #include "llvm/Analysis/Loads.h" | |
62 #include "llvm/Analysis/TargetTransformInfo.h" | |
77 | 63 #include "llvm/IR/CFG.h" |
64 #include "llvm/IR/CallSite.h" | |
0 | 65 #include "llvm/IR/Constants.h" |
83 | 66 #include "llvm/IR/DataLayout.h" |
0 | 67 #include "llvm/IR/DerivedTypes.h" |
77 | 68 #include "llvm/IR/DiagnosticInfo.h" |
0 | 69 #include "llvm/IR/Function.h" |
70 #include "llvm/IR/Instructions.h" | |
71 #include "llvm/IR/IntrinsicInst.h" | |
72 #include "llvm/IR/Module.h" | |
77 | 73 #include "llvm/IR/ValueHandle.h" |
0 | 74 #include "llvm/Pass.h" |
75 #include "llvm/Support/Debug.h" | |
76 #include "llvm/Support/raw_ostream.h" | |
77 #include "llvm/Transforms/Utils/BasicBlockUtils.h" | |
78 #include "llvm/Transforms/Utils/Local.h" | |
79 using namespace llvm; | |
80 | |
77 | 81 #define DEBUG_TYPE "tailcallelim" |
82 | |
0 | 83 STATISTIC(NumEliminated, "Number of tail calls removed"); |
84 STATISTIC(NumRetDuped, "Number of return duplicated"); | |
85 STATISTIC(NumAccumAdded, "Number of accumulators introduced"); | |
86 | |
87 namespace { | |
88 struct TailCallElim : public FunctionPass { | |
89 const TargetTransformInfo *TTI; | |
83 | 90 const DataLayout *DL; |
0 | 91 |
92 static char ID; // Pass identification, replacement for typeid | |
93 TailCallElim() : FunctionPass(ID) { | |
94 initializeTailCallElimPass(*PassRegistry::getPassRegistry()); | |
95 } | |
35
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
96 #ifndef noCbC |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
97 TailCallElim(bool f) : FunctionPass(ID) { |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
98 initializeTailCallElimPass(*PassRegistry::getPassRegistry()); |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
99 onlyForCbC = f; |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
100 } |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
101 #endif |
0 | 102 |
77 | 103 void getAnalysisUsage(AnalysisUsage &AU) const override; |
0 | 104 |
77 | 105 bool runOnFunction(Function &F) override; |
0 | 106 |
107 private: | |
77 | 108 bool runTRE(Function &F); |
109 bool markTails(Function &F, bool &AllCallsAreTailCalls); | |
110 | |
0 | 111 CallInst *FindTRECandidate(Instruction *I, |
112 bool CannotTailCallElimCallsMarkedTail); | |
113 bool EliminateRecursiveTailCall(CallInst *CI, ReturnInst *Ret, | |
114 BasicBlock *&OldEntry, | |
115 bool &TailCallsAreMarkedTail, | |
116 SmallVectorImpl<PHINode *> &ArgumentPHIs, | |
117 bool CannotTailCallElimCallsMarkedTail); | |
118 bool FoldReturnAndProcessPred(BasicBlock *BB, | |
119 ReturnInst *Ret, BasicBlock *&OldEntry, | |
120 bool &TailCallsAreMarkedTail, | |
121 SmallVectorImpl<PHINode *> &ArgumentPHIs, | |
122 bool CannotTailCallElimCallsMarkedTail); | |
123 bool ProcessReturningBlock(ReturnInst *RI, BasicBlock *&OldEntry, | |
124 bool &TailCallsAreMarkedTail, | |
125 SmallVectorImpl<PHINode *> &ArgumentPHIs, | |
126 bool CannotTailCallElimCallsMarkedTail); | |
127 bool CanMoveAboveCall(Instruction *I, CallInst *CI); | |
128 Value *CanTransformAccumulatorRecursion(Instruction *I, CallInst *CI); | |
35
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
129 #ifndef noCbC |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
130 bool onlyForCbC; |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
131 public: |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
132 bool isOnlyForCbC(); |
82
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
133 bool markTailToCodeSegments(Function &F); |
35
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
134 #endif |
0 | 135 }; |
136 } | |
137 | |
138 char TailCallElim::ID = 0; | |
139 INITIALIZE_PASS_BEGIN(TailCallElim, "tailcallelim", | |
140 "Tail Call Elimination", false, false) | |
83 | 141 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) |
0 | 142 INITIALIZE_PASS_END(TailCallElim, "tailcallelim", |
143 "Tail Call Elimination", false, false) | |
144 | |
145 // Public interface to the TailCallElimination pass | |
35
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
146 #ifndef noCbC |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
147 // Public interface to the TailCallElimination pass |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
148 FunctionPass *llvm::createTailCallEliminationPass(bool isOnlyForCbC) { |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
149 return new TailCallElim(isOnlyForCbC); |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
150 } |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
151 #else |
0 | 152 FunctionPass *llvm::createTailCallEliminationPass() { |
153 return new TailCallElim(); | |
154 } | |
35
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
155 #endif |
0 | 156 |
157 void TailCallElim::getAnalysisUsage(AnalysisUsage &AU) const { | |
83 | 158 AU.addRequired<TargetTransformInfoWrapperPass>(); |
0 | 159 } |
160 | |
77 | 161 /// \brief Scan the specified function for alloca instructions. |
162 /// If it contains any dynamic allocas, returns false. | |
163 static bool CanTRE(Function &F) { | |
164 // Because of PR962, we don't TRE dynamic allocas. | |
165 for (auto &BB : F) { | |
166 for (auto &I : BB) { | |
167 if (AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { | |
168 if (!AI->isStaticAlloca()) | |
169 return false; | |
170 } | |
171 } | |
172 } | |
0 | 173 |
77 | 174 return true; |
175 } | |
176 | |
177 bool TailCallElim::runOnFunction(Function &F) { | |
178 if (skipOptnoneFunction(F)) | |
179 return false; | |
180 | |
83 | 181 DL = F.getParent()->getDataLayout(); |
182 | |
77 | 183 bool AllCallsAreTailCalls = false; |
184 bool Modified = markTails(F, AllCallsAreTailCalls); | |
185 if (AllCallsAreTailCalls) | |
186 Modified |= runTRE(F); | |
187 return Modified; | |
0 | 188 } |
189 | |
190 namespace { | |
77 | 191 struct AllocaDerivedValueTracker { |
192 // Start at a root value and walk its use-def chain to mark calls that use the | |
193 // value or a derived value in AllocaUsers, and places where it may escape in | |
194 // EscapePoints. | |
195 void walk(Value *Root) { | |
196 SmallVector<Use *, 32> Worklist; | |
197 SmallPtrSet<Use *, 32> Visited; | |
0 | 198 |
77 | 199 auto AddUsesToWorklist = [&](Value *V) { |
200 for (auto &U : V->uses()) { | |
83 | 201 if (!Visited.insert(&U).second) |
77 | 202 continue; |
203 Worklist.push_back(&U); | |
204 } | |
205 }; | |
206 | |
207 AddUsesToWorklist(Root); | |
208 | |
209 while (!Worklist.empty()) { | |
210 Use *U = Worklist.pop_back_val(); | |
211 Instruction *I = cast<Instruction>(U->getUser()); | |
0 | 212 |
77 | 213 switch (I->getOpcode()) { |
214 case Instruction::Call: | |
215 case Instruction::Invoke: { | |
216 CallSite CS(I); | |
217 bool IsNocapture = !CS.isCallee(U) && | |
218 CS.doesNotCapture(CS.getArgumentNo(U)); | |
219 callUsesLocalStack(CS, IsNocapture); | |
220 if (IsNocapture) { | |
221 // If the alloca-derived argument is passed in as nocapture, then it | |
222 // can't propagate to the call's return. That would be capturing. | |
223 continue; | |
224 } | |
225 break; | |
226 } | |
227 case Instruction::Load: { | |
228 // The result of a load is not alloca-derived (unless an alloca has | |
229 // otherwise escaped, but this is a local analysis). | |
230 continue; | |
231 } | |
232 case Instruction::Store: { | |
233 if (U->getOperandNo() == 0) | |
234 EscapePoints.insert(I); | |
235 continue; // Stores have no users to analyze. | |
236 } | |
237 case Instruction::BitCast: | |
238 case Instruction::GetElementPtr: | |
239 case Instruction::PHI: | |
240 case Instruction::Select: | |
241 case Instruction::AddrSpaceCast: | |
242 break; | |
243 default: | |
244 EscapePoints.insert(I); | |
245 break; | |
246 } | |
247 | |
248 AddUsesToWorklist(I); | |
249 } | |
250 } | |
251 | |
252 void callUsesLocalStack(CallSite CS, bool IsNocapture) { | |
253 // Add it to the list of alloca users. | |
254 AllocaUsers.insert(CS.getInstruction()); | |
255 | |
256 // If it's nocapture then it can't capture this alloca. | |
257 if (IsNocapture) | |
258 return; | |
259 | |
260 // If it can write to memory, it can leak the alloca value. | |
261 if (!CS.onlyReadsMemory()) | |
262 EscapePoints.insert(CS.getInstruction()); | |
263 } | |
264 | |
265 SmallPtrSet<Instruction *, 32> AllocaUsers; | |
266 SmallPtrSet<Instruction *, 32> EscapePoints; | |
267 }; | |
268 } | |
269 | |
270 bool TailCallElim::markTails(Function &F, bool &AllCallsAreTailCalls) { | |
271 if (F.callsFunctionThatReturnsTwice()) | |
272 return false; | |
273 AllCallsAreTailCalls = true; | |
274 | |
275 // The local stack holds all alloca instructions and all byval arguments. | |
276 AllocaDerivedValueTracker Tracker; | |
277 for (Argument &Arg : F.args()) { | |
278 if (Arg.hasByValAttr()) | |
279 Tracker.walk(&Arg); | |
280 } | |
281 for (auto &BB : F) { | |
282 for (auto &I : BB) | |
283 if (AllocaInst *AI = dyn_cast<AllocaInst>(&I)) | |
284 Tracker.walk(AI); | |
0 | 285 } |
286 | |
77 | 287 bool Modified = false; |
288 | |
82
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
289 #ifndef noCbC |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
290 if (F.getReturnType()->is__CodeTy()) |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
291 Modified = markTailToCodeSegments(F); |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
292 #endif |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
293 |
77 | 294 // Track whether a block is reachable after an alloca has escaped. Blocks that |
295 // contain the escaping instruction will be marked as being visited without an | |
296 // escaped alloca, since that is how the block began. | |
297 enum VisitType { | |
298 UNVISITED, | |
299 UNESCAPED, | |
300 ESCAPED | |
301 }; | |
302 DenseMap<BasicBlock *, VisitType> Visited; | |
303 | |
304 // We propagate the fact that an alloca has escaped from block to successor. | |
305 // Visit the blocks that are propagating the escapedness first. To do this, we | |
306 // maintain two worklists. | |
307 SmallVector<BasicBlock *, 32> WorklistUnescaped, WorklistEscaped; | |
308 | |
309 // We may enter a block and visit it thinking that no alloca has escaped yet, | |
310 // then see an escape point and go back around a loop edge and come back to | |
311 // the same block twice. Because of this, we defer setting tail on calls when | |
312 // we first encounter them in a block. Every entry in this list does not | |
313 // statically use an alloca via use-def chain analysis, but may find an alloca | |
314 // through other means if the block turns out to be reachable after an escape | |
315 // point. | |
316 SmallVector<CallInst *, 32> DeferredTails; | |
317 | |
318 BasicBlock *BB = &F.getEntryBlock(); | |
319 VisitType Escaped = UNESCAPED; | |
320 do { | |
321 for (auto &I : *BB) { | |
322 if (Tracker.EscapePoints.count(&I)) | |
323 Escaped = ESCAPED; | |
324 | |
325 CallInst *CI = dyn_cast<CallInst>(&I); | |
326 if (!CI || CI->isTailCall()) | |
327 continue; | |
328 | |
329 if (CI->doesNotAccessMemory()) { | |
330 // A call to a readnone function whose arguments are all things computed | |
331 // outside this function can be marked tail. Even if you stored the | |
332 // alloca address into a global, a readnone function can't load the | |
333 // global anyhow. | |
334 // | |
335 // Note that this runs whether we know an alloca has escaped or not. If | |
336 // it has, then we can't trust Tracker.AllocaUsers to be accurate. | |
337 bool SafeToTail = true; | |
338 for (auto &Arg : CI->arg_operands()) { | |
339 if (isa<Constant>(Arg.getUser())) | |
340 continue; | |
341 if (Argument *A = dyn_cast<Argument>(Arg.getUser())) | |
342 if (!A->hasByValAttr()) | |
343 continue; | |
344 SafeToTail = false; | |
345 break; | |
346 } | |
347 if (SafeToTail) { | |
348 emitOptimizationRemark( | |
349 F.getContext(), "tailcallelim", F, CI->getDebugLoc(), | |
350 "marked this readnone call a tail call candidate"); | |
351 CI->setTailCall(); | |
352 Modified = true; | |
353 continue; | |
354 } | |
355 } | |
356 | |
357 if (Escaped == UNESCAPED && !Tracker.AllocaUsers.count(CI)) { | |
358 DeferredTails.push_back(CI); | |
359 } else { | |
360 AllCallsAreTailCalls = false; | |
361 } | |
362 } | |
363 | |
364 for (auto *SuccBB : make_range(succ_begin(BB), succ_end(BB))) { | |
365 auto &State = Visited[SuccBB]; | |
366 if (State < Escaped) { | |
367 State = Escaped; | |
368 if (State == ESCAPED) | |
369 WorklistEscaped.push_back(SuccBB); | |
370 else | |
371 WorklistUnescaped.push_back(SuccBB); | |
372 } | |
373 } | |
374 | |
375 if (!WorklistEscaped.empty()) { | |
376 BB = WorklistEscaped.pop_back_val(); | |
377 Escaped = ESCAPED; | |
378 } else { | |
379 BB = nullptr; | |
380 while (!WorklistUnescaped.empty()) { | |
381 auto *NextBB = WorklistUnescaped.pop_back_val(); | |
382 if (Visited[NextBB] == UNESCAPED) { | |
383 BB = NextBB; | |
384 Escaped = UNESCAPED; | |
385 break; | |
386 } | |
387 } | |
388 } | |
389 } while (BB); | |
390 | |
391 for (CallInst *CI : DeferredTails) { | |
392 if (Visited[CI->getParent()] != ESCAPED) { | |
393 // If the escape point was part way through the block, calls after the | |
394 // escape point wouldn't have been put into DeferredTails. | |
395 emitOptimizationRemark(F.getContext(), "tailcallelim", F, | |
396 CI->getDebugLoc(), | |
397 "marked this call a tail call candidate"); | |
398 CI->setTailCall(); | |
399 Modified = true; | |
400 } else { | |
401 AllCallsAreTailCalls = false; | |
402 } | |
0 | 403 } |
404 | |
77 | 405 return Modified; |
406 } | |
0 | 407 |
77 | 408 bool TailCallElim::runTRE(Function &F) { |
0 | 409 // If this function is a varargs function, we won't be able to PHI the args |
410 // right, so don't even try to convert it... | |
411 if (F.getFunctionType()->isVarArg()) return false; | |
412 | |
83 | 413 TTI = &getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); |
77 | 414 BasicBlock *OldEntry = nullptr; |
0 | 415 bool TailCallsAreMarkedTail = false; |
416 SmallVector<PHINode*, 8> ArgumentPHIs; | |
417 bool MadeChange = false; | |
418 | |
419 // CanTRETailMarkedCall - If false, we cannot perform TRE on tail calls | |
420 // marked with the 'tail' attribute, because doing so would cause the stack | |
421 // size to increase (real TRE would deallocate variable sized allocas, TRE | |
422 // doesn't). | |
77 | 423 bool CanTRETailMarkedCall = CanTRE(F); |
0 | 424 |
77 | 425 // Change any tail recursive calls to loops. |
0 | 426 // |
427 // FIXME: The code generator produces really bad code when an 'escaping | |
428 // alloca' is changed from being a static alloca to being a dynamic alloca. | |
429 // Until this is resolved, disable this transformation if that would ever | |
430 // happen. This bug is PR962. | |
83 | 431 SmallVector<BasicBlock*, 8> BBToErase; |
77 | 432 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
433 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB->getTerminator())) { | |
434 bool Change = ProcessReturningBlock(Ret, OldEntry, TailCallsAreMarkedTail, | |
435 ArgumentPHIs, !CanTRETailMarkedCall); | |
83 | 436 if (!Change && BB->getFirstNonPHIOrDbg() == Ret) { |
77 | 437 Change = FoldReturnAndProcessPred(BB, Ret, OldEntry, |
438 TailCallsAreMarkedTail, ArgumentPHIs, | |
439 !CanTRETailMarkedCall); | |
83 | 440 // FoldReturnAndProcessPred may have emptied some BB. Remember to |
441 // erase them. | |
442 if (Change && BB->empty()) | |
443 BBToErase.push_back(BB); | |
444 | |
445 } | |
77 | 446 MadeChange |= Change; |
0 | 447 } |
448 } | |
449 | |
83 | 450 for (auto BB: BBToErase) |
451 BB->eraseFromParent(); | |
452 | |
0 | 453 // If we eliminated any tail recursions, it's possible that we inserted some |
454 // silly PHI nodes which just merge an initial value (the incoming operand) | |
455 // with themselves. Check to see if we did and clean up our mess if so. This | |
456 // occurs when a function passes an argument straight through to its tail | |
457 // call. | |
77 | 458 for (unsigned i = 0, e = ArgumentPHIs.size(); i != e; ++i) { |
459 PHINode *PN = ArgumentPHIs[i]; | |
0 | 460 |
77 | 461 // If the PHI Node is a dynamic constant, replace it with the value it is. |
462 if (Value *PNV = SimplifyInstruction(PN)) { | |
463 PN->replaceAllUsesWith(PNV); | |
464 PN->eraseFromParent(); | |
0 | 465 } |
466 } | |
467 | |
468 return MadeChange; | |
469 } | |
470 | |
471 | |
472 /// CanMoveAboveCall - Return true if it is safe to move the specified | |
473 /// instruction from after the call to before the call, assuming that all | |
474 /// instructions between the call and this instruction are movable. | |
475 /// | |
476 bool TailCallElim::CanMoveAboveCall(Instruction *I, CallInst *CI) { | |
477 // FIXME: We can move load/store/call/free instructions above the call if the | |
478 // call does not mod/ref the memory location being processed. | |
479 if (I->mayHaveSideEffects()) // This also handles volatile loads. | |
480 return false; | |
481 | |
482 if (LoadInst *L = dyn_cast<LoadInst>(I)) { | |
483 // Loads may always be moved above calls without side effects. | |
484 if (CI->mayHaveSideEffects()) { | |
485 // Non-volatile loads may be moved above a call with side effects if it | |
486 // does not write to memory and the load provably won't trap. | |
487 // FIXME: Writes to memory only matter if they may alias the pointer | |
488 // being loaded from. | |
489 if (CI->mayWriteToMemory() || | |
490 !isSafeToLoadUnconditionally(L->getPointerOperand(), L, | |
83 | 491 L->getAlignment(), DL)) |
0 | 492 return false; |
493 } | |
494 } | |
495 | |
496 // Otherwise, if this is a side-effect free instruction, check to make sure | |
497 // that it does not use the return value of the call. If it doesn't use the | |
498 // return value of the call, it must only use things that are defined before | |
499 // the call, or movable instructions between the call and the instruction | |
500 // itself. | |
501 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) | |
502 if (I->getOperand(i) == CI) | |
503 return false; | |
504 return true; | |
505 } | |
506 | |
507 // isDynamicConstant - Return true if the specified value is the same when the | |
508 // return would exit as it was when the initial iteration of the recursive | |
509 // function was executed. | |
510 // | |
511 // We currently handle static constants and arguments that are not modified as | |
512 // part of the recursion. | |
513 // | |
514 static bool isDynamicConstant(Value *V, CallInst *CI, ReturnInst *RI) { | |
515 if (isa<Constant>(V)) return true; // Static constants are always dyn consts | |
516 | |
517 // Check to see if this is an immutable argument, if so, the value | |
518 // will be available to initialize the accumulator. | |
519 if (Argument *Arg = dyn_cast<Argument>(V)) { | |
520 // Figure out which argument number this is... | |
521 unsigned ArgNo = 0; | |
522 Function *F = CI->getParent()->getParent(); | |
523 for (Function::arg_iterator AI = F->arg_begin(); &*AI != Arg; ++AI) | |
524 ++ArgNo; | |
525 | |
526 // If we are passing this argument into call as the corresponding | |
527 // argument operand, then the argument is dynamically constant. | |
528 // Otherwise, we cannot transform this function safely. | |
529 if (CI->getArgOperand(ArgNo) == Arg) | |
530 return true; | |
531 } | |
532 | |
533 // Switch cases are always constant integers. If the value is being switched | |
534 // on and the return is only reachable from one of its cases, it's | |
535 // effectively constant. | |
536 if (BasicBlock *UniquePred = RI->getParent()->getUniquePredecessor()) | |
537 if (SwitchInst *SI = dyn_cast<SwitchInst>(UniquePred->getTerminator())) | |
538 if (SI->getCondition() == V) | |
539 return SI->getDefaultDest() != RI->getParent(); | |
540 | |
541 // Not a constant or immutable argument, we can't safely transform. | |
542 return false; | |
543 } | |
544 | |
545 // getCommonReturnValue - Check to see if the function containing the specified | |
546 // tail call consistently returns the same runtime-constant value at all exit | |
547 // points except for IgnoreRI. If so, return the returned value. | |
548 // | |
549 static Value *getCommonReturnValue(ReturnInst *IgnoreRI, CallInst *CI) { | |
550 Function *F = CI->getParent()->getParent(); | |
77 | 551 Value *ReturnedValue = nullptr; |
0 | 552 |
553 for (Function::iterator BBI = F->begin(), E = F->end(); BBI != E; ++BBI) { | |
554 ReturnInst *RI = dyn_cast<ReturnInst>(BBI->getTerminator()); | |
77 | 555 if (RI == nullptr || RI == IgnoreRI) continue; |
0 | 556 |
557 // We can only perform this transformation if the value returned is | |
558 // evaluatable at the start of the initial invocation of the function, | |
559 // instead of at the end of the evaluation. | |
560 // | |
561 Value *RetOp = RI->getOperand(0); | |
562 if (!isDynamicConstant(RetOp, CI, RI)) | |
77 | 563 return nullptr; |
0 | 564 |
565 if (ReturnedValue && RetOp != ReturnedValue) | |
77 | 566 return nullptr; // Cannot transform if differing values are returned. |
0 | 567 ReturnedValue = RetOp; |
568 } | |
569 return ReturnedValue; | |
570 } | |
571 | |
572 /// CanTransformAccumulatorRecursion - If the specified instruction can be | |
573 /// transformed using accumulator recursion elimination, return the constant | |
574 /// which is the start of the accumulator value. Otherwise return null. | |
575 /// | |
576 Value *TailCallElim::CanTransformAccumulatorRecursion(Instruction *I, | |
577 CallInst *CI) { | |
77 | 578 if (!I->isAssociative() || !I->isCommutative()) return nullptr; |
0 | 579 assert(I->getNumOperands() == 2 && |
580 "Associative/commutative operations should have 2 args!"); | |
581 | |
582 // Exactly one operand should be the result of the call instruction. | |
583 if ((I->getOperand(0) == CI && I->getOperand(1) == CI) || | |
584 (I->getOperand(0) != CI && I->getOperand(1) != CI)) | |
77 | 585 return nullptr; |
0 | 586 |
587 // The only user of this instruction we allow is a single return instruction. | |
77 | 588 if (!I->hasOneUse() || !isa<ReturnInst>(I->user_back())) |
589 return nullptr; | |
0 | 590 |
591 // Ok, now we have to check all of the other return instructions in this | |
592 // function. If they return non-constants or differing values, then we cannot | |
593 // transform the function safely. | |
77 | 594 return getCommonReturnValue(cast<ReturnInst>(I->user_back()), CI); |
0 | 595 } |
596 | |
597 static Instruction *FirstNonDbg(BasicBlock::iterator I) { | |
598 while (isa<DbgInfoIntrinsic>(I)) | |
599 ++I; | |
600 return &*I; | |
601 } | |
602 | |
603 CallInst* | |
604 TailCallElim::FindTRECandidate(Instruction *TI, | |
605 bool CannotTailCallElimCallsMarkedTail) { | |
606 BasicBlock *BB = TI->getParent(); | |
607 Function *F = BB->getParent(); | |
608 | |
609 if (&BB->front() == TI) // Make sure there is something before the terminator. | |
77 | 610 return nullptr; |
0 | 611 |
612 // Scan backwards from the return, checking to see if there is a tail call in | |
613 // this block. If so, set CI to it. | |
77 | 614 CallInst *CI = nullptr; |
0 | 615 BasicBlock::iterator BBI = TI; |
616 while (true) { | |
617 CI = dyn_cast<CallInst>(BBI); | |
618 if (CI && CI->getCalledFunction() == F) | |
619 break; | |
620 | |
621 if (BBI == BB->begin()) | |
77 | 622 return nullptr; // Didn't find a potential tail call. |
0 | 623 --BBI; |
624 } | |
625 | |
626 // If this call is marked as a tail call, and if there are dynamic allocas in | |
627 // the function, we cannot perform this optimization. | |
628 if (CI->isTailCall() && CannotTailCallElimCallsMarkedTail) | |
77 | 629 return nullptr; |
0 | 630 |
631 // As a special case, detect code like this: | |
632 // double fabs(double f) { return __builtin_fabs(f); } // a 'fabs' call | |
633 // and disable this xform in this case, because the code generator will | |
634 // lower the call to fabs into inline code. | |
635 if (BB == &F->getEntryBlock() && | |
636 FirstNonDbg(BB->front()) == CI && | |
77 | 637 FirstNonDbg(std::next(BB->begin())) == TI && |
0 | 638 CI->getCalledFunction() && |
639 !TTI->isLoweredToCall(CI->getCalledFunction())) { | |
640 // A single-block function with just a call and a return. Check that | |
641 // the arguments match. | |
642 CallSite::arg_iterator I = CallSite(CI).arg_begin(), | |
643 E = CallSite(CI).arg_end(); | |
644 Function::arg_iterator FI = F->arg_begin(), | |
645 FE = F->arg_end(); | |
646 for (; I != E && FI != FE; ++I, ++FI) | |
647 if (*I != &*FI) break; | |
648 if (I == E && FI == FE) | |
77 | 649 return nullptr; |
0 | 650 } |
651 | |
652 return CI; | |
653 } | |
654 | |
655 bool TailCallElim::EliminateRecursiveTailCall(CallInst *CI, ReturnInst *Ret, | |
656 BasicBlock *&OldEntry, | |
657 bool &TailCallsAreMarkedTail, | |
658 SmallVectorImpl<PHINode *> &ArgumentPHIs, | |
659 bool CannotTailCallElimCallsMarkedTail) { | |
660 // If we are introducing accumulator recursion to eliminate operations after | |
661 // the call instruction that are both associative and commutative, the initial | |
662 // value for the accumulator is placed in this variable. If this value is set | |
663 // then we actually perform accumulator recursion elimination instead of | |
664 // simple tail recursion elimination. If the operation is an LLVM instruction | |
665 // (eg: "add") then it is recorded in AccumulatorRecursionInstr. If not, then | |
666 // we are handling the case when the return instruction returns a constant C | |
667 // which is different to the constant returned by other return instructions | |
668 // (which is recorded in AccumulatorRecursionEliminationInitVal). This is a | |
669 // special case of accumulator recursion, the operation being "return C". | |
77 | 670 Value *AccumulatorRecursionEliminationInitVal = nullptr; |
671 Instruction *AccumulatorRecursionInstr = nullptr; | |
0 | 672 |
673 // Ok, we found a potential tail call. We can currently only transform the | |
674 // tail call if all of the instructions between the call and the return are | |
675 // movable to above the call itself, leaving the call next to the return. | |
676 // Check that this is the case now. | |
677 BasicBlock::iterator BBI = CI; | |
678 for (++BBI; &*BBI != Ret; ++BBI) { | |
679 if (CanMoveAboveCall(BBI, CI)) continue; | |
680 | |
681 // If we can't move the instruction above the call, it might be because it | |
682 // is an associative and commutative operation that could be transformed | |
683 // using accumulator recursion elimination. Check to see if this is the | |
684 // case, and if so, remember the initial accumulator value for later. | |
685 if ((AccumulatorRecursionEliminationInitVal = | |
686 CanTransformAccumulatorRecursion(BBI, CI))) { | |
687 // Yes, this is accumulator recursion. Remember which instruction | |
688 // accumulates. | |
689 AccumulatorRecursionInstr = BBI; | |
690 } else { | |
691 return false; // Otherwise, we cannot eliminate the tail recursion! | |
692 } | |
693 } | |
694 | |
695 // We can only transform call/return pairs that either ignore the return value | |
696 // of the call and return void, ignore the value of the call and return a | |
697 // constant, return the value returned by the tail call, or that are being | |
698 // accumulator recursion variable eliminated. | |
699 if (Ret->getNumOperands() == 1 && Ret->getReturnValue() != CI && | |
700 !isa<UndefValue>(Ret->getReturnValue()) && | |
77 | 701 AccumulatorRecursionEliminationInitVal == nullptr && |
702 !getCommonReturnValue(nullptr, CI)) { | |
0 | 703 // One case remains that we are able to handle: the current return |
704 // instruction returns a constant, and all other return instructions | |
705 // return a different constant. | |
706 if (!isDynamicConstant(Ret->getReturnValue(), CI, Ret)) | |
707 return false; // Current return instruction does not return a constant. | |
708 // Check that all other return instructions return a common constant. If | |
709 // so, record it in AccumulatorRecursionEliminationInitVal. | |
710 AccumulatorRecursionEliminationInitVal = getCommonReturnValue(Ret, CI); | |
711 if (!AccumulatorRecursionEliminationInitVal) | |
712 return false; | |
713 } | |
714 | |
715 BasicBlock *BB = Ret->getParent(); | |
716 Function *F = BB->getParent(); | |
717 | |
77 | 718 emitOptimizationRemark(F->getContext(), "tailcallelim", *F, CI->getDebugLoc(), |
719 "transforming tail recursion to loop"); | |
720 | |
0 | 721 // OK! We can transform this tail call. If this is the first one found, |
722 // create the new entry block, allowing us to branch back to the old entry. | |
77 | 723 if (!OldEntry) { |
0 | 724 OldEntry = &F->getEntryBlock(); |
725 BasicBlock *NewEntry = BasicBlock::Create(F->getContext(), "", F, OldEntry); | |
726 NewEntry->takeName(OldEntry); | |
727 OldEntry->setName("tailrecurse"); | |
728 BranchInst::Create(OldEntry, NewEntry); | |
729 | |
730 // If this tail call is marked 'tail' and if there are any allocas in the | |
731 // entry block, move them up to the new entry block. | |
732 TailCallsAreMarkedTail = CI->isTailCall(); | |
733 if (TailCallsAreMarkedTail) | |
734 // Move all fixed sized allocas from OldEntry to NewEntry. | |
735 for (BasicBlock::iterator OEBI = OldEntry->begin(), E = OldEntry->end(), | |
736 NEBI = NewEntry->begin(); OEBI != E; ) | |
737 if (AllocaInst *AI = dyn_cast<AllocaInst>(OEBI++)) | |
738 if (isa<ConstantInt>(AI->getArraySize())) | |
739 AI->moveBefore(NEBI); | |
740 | |
741 // Now that we have created a new block, which jumps to the entry | |
742 // block, insert a PHI node for each argument of the function. | |
743 // For now, we initialize each PHI to only have the real arguments | |
744 // which are passed in. | |
745 Instruction *InsertPos = OldEntry->begin(); | |
746 for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); | |
747 I != E; ++I) { | |
748 PHINode *PN = PHINode::Create(I->getType(), 2, | |
749 I->getName() + ".tr", InsertPos); | |
750 I->replaceAllUsesWith(PN); // Everyone use the PHI node now! | |
751 PN->addIncoming(I, NewEntry); | |
752 ArgumentPHIs.push_back(PN); | |
753 } | |
754 } | |
755 | |
756 // If this function has self recursive calls in the tail position where some | |
757 // are marked tail and some are not, only transform one flavor or another. We | |
758 // have to choose whether we move allocas in the entry block to the new entry | |
759 // block or not, so we can't make a good choice for both. NOTE: We could do | |
760 // slightly better here in the case that the function has no entry block | |
761 // allocas. | |
762 if (TailCallsAreMarkedTail && !CI->isTailCall()) | |
763 return false; | |
764 | |
765 // Ok, now that we know we have a pseudo-entry block WITH all of the | |
766 // required PHI nodes, add entries into the PHI node for the actual | |
767 // parameters passed into the tail-recursive call. | |
768 for (unsigned i = 0, e = CI->getNumArgOperands(); i != e; ++i) | |
769 ArgumentPHIs[i]->addIncoming(CI->getArgOperand(i), BB); | |
770 | |
771 // If we are introducing an accumulator variable to eliminate the recursion, | |
772 // do so now. Note that we _know_ that no subsequent tail recursion | |
773 // eliminations will happen on this function because of the way the | |
774 // accumulator recursion predicate is set up. | |
775 // | |
776 if (AccumulatorRecursionEliminationInitVal) { | |
777 Instruction *AccRecInstr = AccumulatorRecursionInstr; | |
778 // Start by inserting a new PHI node for the accumulator. | |
779 pred_iterator PB = pred_begin(OldEntry), PE = pred_end(OldEntry); | |
780 PHINode *AccPN = | |
781 PHINode::Create(AccumulatorRecursionEliminationInitVal->getType(), | |
782 std::distance(PB, PE) + 1, | |
783 "accumulator.tr", OldEntry->begin()); | |
784 | |
785 // Loop over all of the predecessors of the tail recursion block. For the | |
786 // real entry into the function we seed the PHI with the initial value, | |
787 // computed earlier. For any other existing branches to this block (due to | |
788 // other tail recursions eliminated) the accumulator is not modified. | |
789 // Because we haven't added the branch in the current block to OldEntry yet, | |
790 // it will not show up as a predecessor. | |
791 for (pred_iterator PI = PB; PI != PE; ++PI) { | |
792 BasicBlock *P = *PI; | |
793 if (P == &F->getEntryBlock()) | |
794 AccPN->addIncoming(AccumulatorRecursionEliminationInitVal, P); | |
795 else | |
796 AccPN->addIncoming(AccPN, P); | |
797 } | |
798 | |
799 if (AccRecInstr) { | |
800 // Add an incoming argument for the current block, which is computed by | |
801 // our associative and commutative accumulator instruction. | |
802 AccPN->addIncoming(AccRecInstr, BB); | |
803 | |
804 // Next, rewrite the accumulator recursion instruction so that it does not | |
805 // use the result of the call anymore, instead, use the PHI node we just | |
806 // inserted. | |
807 AccRecInstr->setOperand(AccRecInstr->getOperand(0) != CI, AccPN); | |
808 } else { | |
809 // Add an incoming argument for the current block, which is just the | |
810 // constant returned by the current return instruction. | |
811 AccPN->addIncoming(Ret->getReturnValue(), BB); | |
812 } | |
813 | |
814 // Finally, rewrite any return instructions in the program to return the PHI | |
815 // node instead of the "initval" that they do currently. This loop will | |
816 // actually rewrite the return value we are destroying, but that's ok. | |
817 for (Function::iterator BBI = F->begin(), E = F->end(); BBI != E; ++BBI) | |
818 if (ReturnInst *RI = dyn_cast<ReturnInst>(BBI->getTerminator())) | |
819 RI->setOperand(0, AccPN); | |
820 ++NumAccumAdded; | |
821 } | |
822 | |
823 // Now that all of the PHI nodes are in place, remove the call and | |
824 // ret instructions, replacing them with an unconditional branch. | |
825 BranchInst *NewBI = BranchInst::Create(OldEntry, Ret); | |
826 NewBI->setDebugLoc(CI->getDebugLoc()); | |
827 | |
828 BB->getInstList().erase(Ret); // Remove return. | |
829 BB->getInstList().erase(CI); // Remove call. | |
830 ++NumEliminated; | |
831 return true; | |
832 } | |
833 | |
834 bool TailCallElim::FoldReturnAndProcessPred(BasicBlock *BB, | |
835 ReturnInst *Ret, BasicBlock *&OldEntry, | |
836 bool &TailCallsAreMarkedTail, | |
837 SmallVectorImpl<PHINode *> &ArgumentPHIs, | |
838 bool CannotTailCallElimCallsMarkedTail) { | |
839 bool Change = false; | |
840 | |
841 // If the return block contains nothing but the return and PHI's, | |
842 // there might be an opportunity to duplicate the return in its | |
843 // predecessors and perform TRC there. Look for predecessors that end | |
844 // in unconditional branch and recursive call(s). | |
845 SmallVector<BranchInst*, 8> UncondBranchPreds; | |
846 for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { | |
847 BasicBlock *Pred = *PI; | |
848 TerminatorInst *PTI = Pred->getTerminator(); | |
849 if (BranchInst *BI = dyn_cast<BranchInst>(PTI)) | |
850 if (BI->isUnconditional()) | |
851 UncondBranchPreds.push_back(BI); | |
852 } | |
853 | |
854 while (!UncondBranchPreds.empty()) { | |
855 BranchInst *BI = UncondBranchPreds.pop_back_val(); | |
856 BasicBlock *Pred = BI->getParent(); | |
857 if (CallInst *CI = FindTRECandidate(BI, CannotTailCallElimCallsMarkedTail)){ | |
858 DEBUG(dbgs() << "FOLDING: " << *BB | |
859 << "INTO UNCOND BRANCH PRED: " << *Pred); | |
83 | 860 ReturnInst *RI = FoldReturnIntoUncondBranch(Ret, BB, Pred); |
861 | |
862 // Cleanup: if all predecessors of BB have been eliminated by | |
863 // FoldReturnIntoUncondBranch, we would like to delete it, but we | |
864 // can not just nuke it as it is being used as an iterator by our caller. | |
865 // Just empty it, and the caller will erase it when it is safe to do so. | |
866 // It is important to empty it, because the ret instruction in there is | |
867 // still using a value which EliminateRecursiveTailCall will attempt | |
868 // to remove. | |
869 if (!BB->hasAddressTaken() && pred_begin(BB) == pred_end(BB)) | |
870 BB->getInstList().clear(); | |
871 | |
872 EliminateRecursiveTailCall(CI, RI, OldEntry, TailCallsAreMarkedTail, | |
873 ArgumentPHIs, | |
0 | 874 CannotTailCallElimCallsMarkedTail); |
875 ++NumRetDuped; | |
876 Change = true; | |
877 } | |
878 } | |
879 | |
880 return Change; | |
881 } | |
882 | |
883 bool | |
884 TailCallElim::ProcessReturningBlock(ReturnInst *Ret, BasicBlock *&OldEntry, | |
885 bool &TailCallsAreMarkedTail, | |
886 SmallVectorImpl<PHINode *> &ArgumentPHIs, | |
887 bool CannotTailCallElimCallsMarkedTail) { | |
888 CallInst *CI = FindTRECandidate(Ret, CannotTailCallElimCallsMarkedTail); | |
889 if (!CI) | |
890 return false; | |
891 | |
892 return EliminateRecursiveTailCall(CI, Ret, OldEntry, TailCallsAreMarkedTail, | |
893 ArgumentPHIs, | |
894 CannotTailCallElimCallsMarkedTail); | |
895 } | |
35
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
896 |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
897 #ifndef noCbC |
82
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
898 bool TailCallElim::isOnlyForCbC(){ |
35
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
899 return onlyForCbC; |
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
900 } |
82
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
901 |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
902 bool TailCallElim::markTailToCodeSegments(Function &F){ |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
903 bool Modified = false; |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
904 for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) { |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
905 for (auto &I : *BB) { |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
906 CallInst *CI = dyn_cast<CallInst>(&I); |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
907 Function* Called; |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
908 if (CI) |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
909 Called = CI->getCalledFunction(); |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
910 else |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
911 continue; |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
912 // We should touch only code segment call. |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
913 if (Called && Called->getReturnType()->is__CodeTy()) { |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
914 CI->setTailCall(); |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
915 Modified = true; |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
916 } |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
917 } |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
918 } |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
919 return Modified; |
e218c19a8176
markTailToCodeSegments ensure code segments are marked tail
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
80
diff
changeset
|
920 } |
35
503e14e069e4
update to LLVM 3.5
Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
parents:
0
diff
changeset
|
921 #endif |