blob: c944b8cc20dc3cb974dce74a00294fae92c6b8cb [file] [log] [blame]
Chris Lattneraa921452003-09-01 16:42:16 +00001//===- Parallelize.cpp - Auto parallelization using DS Graphs -------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Vikram S. Advee12c74c2002-12-10 00:43:34 +00009//
10// This file implements a pass that automatically parallelizes a program,
11// using the Cilk multi-threaded runtime system to execute parallel code.
12//
13// The pass uses the Program Dependence Graph (class PDGIterator) to
14// identify parallelizable function calls, i.e., calls whose instances
15// can be executed in parallel with instances of other function calls.
16// (In the future, this should also execute different instances of the same
17// function call in parallel, but that requires parallelizing across
18// loop iterations.)
19//
20// The output of the pass is LLVM code with:
21// (1) all parallelizable functions renamed to flag them as parallelizable;
22// (2) calls to a sync() function introduced at synchronization points.
23// The CWriter recognizes these functions and inserts the appropriate Cilk
24// keywords when writing out C code. This C code must be compiled with cilk2c.
25//
26// Current algorithmic limitations:
27// -- no array dependence analysis
28// -- no parallelization for function calls in different loop iterations
29// (except in unlikely trivial cases)
30//
31// Limitations of using Cilk:
32// -- No parallelism within a function body, e.g., in a loop;
33// -- Simplistic synchronization model requiring all parallel threads
34// created within a function to block at a sync().
35// -- Excessive overhead at "spawned" function calls, which has no benefit
36// once all threads are busy (especially common when the degree of
37// parallelism is low).
Chris Lattneraa921452003-09-01 16:42:16 +000038//
Vikram S. Advee12c74c2002-12-10 00:43:34 +000039//===----------------------------------------------------------------------===//
40
Chris Lattnera2dc7272004-03-14 02:13:38 +000041#include "llvm/DerivedTypes.h"
42#include "llvm/Instructions.h"
43#include "llvm/Module.h"
Chris Lattner71ef8f72004-06-28 00:20:04 +000044#include "PgmDependenceGraph.h"
Chris Lattner4dabb2c2004-07-07 06:32:21 +000045#include "llvm/Analysis/DataStructure/DataStructure.h"
46#include "llvm/Analysis/DataStructure/DSGraph.h"
Vikram S. Advee12c74c2002-12-10 00:43:34 +000047#include "llvm/Support/InstVisitor.h"
Chris Lattnera2dc7272004-03-14 02:13:38 +000048#include "llvm/Transforms/Utils/Local.h"
Vikram S. Advee12c74c2002-12-10 00:43:34 +000049#include "Support/Statistic.h"
50#include "Support/STLExtras.h"
51#include "Support/hash_set"
52#include "Support/hash_map"
Vikram S. Advee12c74c2002-12-10 00:43:34 +000053#include <functional>
54#include <algorithm>
Chris Lattner1e2385b2003-11-21 21:54:22 +000055using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000056
Vikram S. Advee12c74c2002-12-10 00:43:34 +000057//----------------------------------------------------------------------------
Chris Lattner09a67052003-09-01 16:49:38 +000058// Global constants used in marking Cilk functions and function calls.
59//----------------------------------------------------------------------------
60
61static const char * const CilkSuffix = ".llvm2cilk";
62static const char * const DummySyncFuncName = "__sync.llvm2cilk";
63
64//----------------------------------------------------------------------------
65// Routines to identify Cilk functions, calls to Cilk functions, and syncs.
66//----------------------------------------------------------------------------
67
68static bool isCilk(const Function& F) {
69 return (F.getName().rfind(CilkSuffix) ==
70 F.getName().size() - std::strlen(CilkSuffix));
71}
72
73static bool isCilkMain(const Function& F) {
74 return F.getName() == "main" + std::string(CilkSuffix);
75}
76
77
78static bool isCilk(const CallInst& CI) {
79 return CI.getCalledFunction() && isCilk(*CI.getCalledFunction());
80}
81
82static bool isSync(const CallInst& CI) {
83 return CI.getCalledFunction() &&
84 CI.getCalledFunction()->getName() == DummySyncFuncName;
85}
86
87
88//----------------------------------------------------------------------------
Vikram S. Advee12c74c2002-12-10 00:43:34 +000089// class Cilkifier
90//
91// Code generation pass that transforms code to identify where Cilk keywords
Misha Brukmancf00c4a2003-10-10 17:57:28 +000092// should be inserted. This relies on `llvm-dis -c' to print out the keywords.
Vikram S. Advee12c74c2002-12-10 00:43:34 +000093//----------------------------------------------------------------------------
Misha Brukman99cc88b2004-02-29 23:09:10 +000094class Cilkifier: public InstVisitor<Cilkifier> {
Vikram S. Advee12c74c2002-12-10 00:43:34 +000095 Function* DummySyncFunc;
96
97 // Data used when transforming each function.
98 hash_set<const Instruction*> stmtsVisited; // Flags for recursive DFS
99 hash_map<const CallInst*, hash_set<CallInst*> > spawnToSyncsMap;
100
101 // Input data for the transformation.
102 const hash_set<Function*>* cilkFunctions; // Set of parallel functions
103 PgmDependenceGraph* depGraph;
104
105 void DFSVisitInstr (Instruction* I,
106 Instruction* root,
107 hash_set<const Instruction*>& depsOfRoot);
108
109public:
110 /*ctor*/ Cilkifier (Module& M);
111
112 // Transform a single function including its name, its call sites, and syncs
113 //
114 void TransformFunc (Function* F,
115 const hash_set<Function*>& cilkFunctions,
116 PgmDependenceGraph& _depGraph);
117
118 // The visitor function that does most of the hard work, via DFSVisitInstr
119 //
120 void visitCallInst(CallInst& CI);
121};
122
123
Misha Brukman99cc88b2004-02-29 23:09:10 +0000124Cilkifier::Cilkifier(Module& M) {
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000125 // create the dummy Sync function and add it to the Module
Chris Lattner273328e2003-09-01 16:53:46 +0000126 DummySyncFunc = M.getOrInsertFunction(DummySyncFuncName, Type::VoidTy, 0);
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000127}
128
129void Cilkifier::TransformFunc(Function* F,
130 const hash_set<Function*>& _cilkFunctions,
Misha Brukman99cc88b2004-02-29 23:09:10 +0000131 PgmDependenceGraph& _depGraph) {
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000132 // Memoize the information for this function
133 cilkFunctions = &_cilkFunctions;
134 depGraph = &_depGraph;
135
136 // Add the marker suffix to the Function name
137 // This should automatically mark all calls to the function also!
138 F->setName(F->getName() + CilkSuffix);
139
140 // Insert sync operations for each separate spawn
141 visit(*F);
142
143 // Now traverse the CFG in rPostorder and eliminate redundant syncs, i.e.,
144 // two consecutive sync's on a straight-line path with no intervening spawn.
145
146}
147
148
149void Cilkifier::DFSVisitInstr(Instruction* I,
150 Instruction* root,
151 hash_set<const Instruction*>& depsOfRoot)
152{
153 assert(stmtsVisited.find(I) == stmtsVisited.end());
154 stmtsVisited.insert(I);
155
156 // If there is a dependence from root to I, insert Sync and return
Misha Brukman99cc88b2004-02-29 23:09:10 +0000157 if (depsOfRoot.find(I) != depsOfRoot.end()) {
158 // Insert a sync before I and stop searching along this path.
159 // If I is a Phi instruction, the dependence can only be an SSA dep.
160 // and we need to insert the sync in the predecessor on the appropriate
161 // incoming edge!
162 CallInst* syncI = 0;
163 if (PHINode* phiI = dyn_cast<PHINode>(I)) {
164 // check all operands of the Phi and insert before each one
165 for (unsigned i = 0, N = phiI->getNumIncomingValues(); i < N; ++i)
166 if (phiI->getIncomingValue(i) == root)
167 syncI = new CallInst(DummySyncFunc, std::vector<Value*>(), "",
168 phiI->getIncomingBlock(i)->getTerminator());
169 } else
170 syncI = new CallInst(DummySyncFunc, std::vector<Value*>(), "", I);
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000171
Misha Brukman99cc88b2004-02-29 23:09:10 +0000172 // Remember the sync for each spawn to eliminate redundant ones later
173 spawnToSyncsMap[cast<CallInst>(root)].insert(syncI);
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000174
Misha Brukman99cc88b2004-02-29 23:09:10 +0000175 return;
176 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000177
178 // else visit unvisited successors
Misha Brukman99cc88b2004-02-29 23:09:10 +0000179 if (BranchInst* brI = dyn_cast<BranchInst>(I)) {
180 // visit first instruction in each successor BB
181 for (unsigned i = 0, N = brI->getNumSuccessors(); i < N; ++i)
182 if (stmtsVisited.find(&brI->getSuccessor(i)->front())
183 == stmtsVisited.end())
184 DFSVisitInstr(&brI->getSuccessor(i)->front(), root, depsOfRoot);
185 } else
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000186 if (Instruction* nextI = I->getNext())
187 if (stmtsVisited.find(nextI) == stmtsVisited.end())
188 DFSVisitInstr(nextI, root, depsOfRoot);
189}
190
191
192void Cilkifier::visitCallInst(CallInst& CI)
193{
194 assert(CI.getCalledFunction() != 0 && "Only direct calls can be spawned.");
195 if (cilkFunctions->find(CI.getCalledFunction()) == cilkFunctions->end())
196 return; // not a spawn
197
198 // Find all the outgoing memory dependences.
199 hash_set<const Instruction*> depsOfRoot;
200 for (PgmDependenceGraph::iterator DI =
201 depGraph->outDepBegin(CI, MemoryDeps); ! DI.fini(); ++DI)
202 depsOfRoot.insert(&DI->getSink()->getInstr());
203
204 // Now find all outgoing SSA dependences to the eventual non-Phi users of
205 // the call value (i.e., direct users that are not phis, and for any
206 // user that is a Phi, direct non-Phi users of that Phi, and recursively).
Chris Lattneraa921452003-09-01 16:42:16 +0000207 std::vector<const PHINode*> phiUsers;
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000208 hash_set<const PHINode*> phisSeen; // ensures we don't visit a phi twice
209 for (Value::use_iterator UI=CI.use_begin(), UE=CI.use_end(); UI != UE; ++UI)
Misha Brukman99cc88b2004-02-29 23:09:10 +0000210 if (const PHINode* phiUser = dyn_cast<PHINode>(*UI)) {
211 if (phisSeen.find(phiUser) == phisSeen.end()) {
212 phiUsers.push_back(phiUser);
213 phisSeen.insert(phiUser);
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000214 }
Misha Brukman99cc88b2004-02-29 23:09:10 +0000215 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000216 else
217 depsOfRoot.insert(cast<Instruction>(*UI));
218
219 // Now we've found the non-Phi users and immediate phi users.
220 // Recursively walk the phi users and add their non-phi users.
Misha Brukman99cc88b2004-02-29 23:09:10 +0000221 for (const PHINode* phiUser; !phiUsers.empty(); phiUsers.pop_back()) {
222 phiUser = phiUsers.back();
223 for (Value::use_const_iterator UI=phiUser->use_begin(),
224 UE=phiUser->use_end(); UI != UE; ++UI)
225 if (const PHINode* pn = dyn_cast<PHINode>(*UI)) {
226 if (phisSeen.find(pn) == phisSeen.end()) {
227 phiUsers.push_back(pn);
228 phisSeen.insert(pn);
229 }
230 } else
231 depsOfRoot.insert(cast<Instruction>(*UI));
232 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000233
234 // Walk paths of the CFG starting at the call instruction and insert
235 // one sync before the first dependence on each path, if any.
Misha Brukman99cc88b2004-02-29 23:09:10 +0000236 if (! depsOfRoot.empty()) {
237 stmtsVisited.clear(); // start a new DFS for this CallInst
238 assert(CI.getNext() && "Call instruction cannot be a terminator!");
239 DFSVisitInstr(CI.getNext(), &CI, depsOfRoot);
240 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000241
242 // Now, eliminate all users of the SSA value of the CallInst, i.e.,
243 // if the call instruction returns a value, delete the return value
244 // register and replace it by a stack slot.
245 if (CI.getType() != Type::VoidTy)
246 DemoteRegToStack(CI);
247}
248
249
250//----------------------------------------------------------------------------
251// class FindParallelCalls
252//
253// Find all CallInst instructions that have at least one other CallInst
254// that is independent. These are the instructions that can produce
255// useful parallelism.
256//----------------------------------------------------------------------------
257
Chris Lattner80431272003-08-06 17:16:24 +0000258class FindParallelCalls : public InstVisitor<FindParallelCalls> {
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000259 typedef hash_set<CallInst*> DependentsSet;
260 typedef DependentsSet::iterator Dependents_iterator;
261 typedef DependentsSet::const_iterator Dependents_const_iterator;
262
263 PgmDependenceGraph& depGraph; // dependence graph for the function
264 hash_set<Instruction*> stmtsVisited; // flags for DFS walk of depGraph
265 hash_map<CallInst*, bool > completed; // flags marking if a CI is done
266 hash_map<CallInst*, DependentsSet> dependents; // dependent CIs for each CI
267
268 void VisitOutEdges(Instruction* I,
269 CallInst* root,
270 DependentsSet& depsOfRoot);
271
Chris Lattner80431272003-08-06 17:16:24 +0000272 FindParallelCalls(const FindParallelCalls &); // DO NOT IMPLEMENT
273 void operator=(const FindParallelCalls&); // DO NOT IMPLEMENT
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000274public:
275 std::vector<CallInst*> parallelCalls;
276
277public:
278 /*ctor*/ FindParallelCalls (Function& F, PgmDependenceGraph& DG);
279 void visitCallInst (CallInst& CI);
280};
281
282
283FindParallelCalls::FindParallelCalls(Function& F,
284 PgmDependenceGraph& DG)
285 : depGraph(DG)
286{
287 // Find all CallInsts reachable from each CallInst using a recursive DFS
288 visit(F);
289
290 // Now we've found all CallInsts reachable from each CallInst.
291 // Find those CallInsts that are parallel with at least one other CallInst
292 // by counting total inEdges and outEdges.
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000293 unsigned long totalNumCalls = completed.size();
294
Misha Brukman99cc88b2004-02-29 23:09:10 +0000295 if (totalNumCalls == 1) {
296 // Check first for the special case of a single call instruction not
297 // in any loop. It is not parallel, even if it has no dependences
298 // (this is why it is a special case).
299 //
300 // FIXME:
301 // THIS CASE IS NOT HANDLED RIGHT NOW, I.E., THERE IS NO
302 // PARALLELISM FOR CALLS IN DIFFERENT ITERATIONS OF A LOOP.
303 return;
304 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000305
306 hash_map<CallInst*, unsigned long> numDeps;
307 for (hash_map<CallInst*, DependentsSet>::iterator II = dependents.begin(),
Misha Brukman99cc88b2004-02-29 23:09:10 +0000308 IE = dependents.end(); II != IE; ++II) {
309 CallInst* fromCI = II->first;
310 numDeps[fromCI] += II->second.size();
311 for (Dependents_iterator DI = II->second.begin(), DE = II->second.end();
312 DI != DE; ++DI)
313 numDeps[*DI]++; // *DI can be reached from II->first
314 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000315
316 for (hash_map<CallInst*, DependentsSet>::iterator
317 II = dependents.begin(), IE = dependents.end(); II != IE; ++II)
318
319 // FIXME: Remove "- 1" when considering parallelism in loops
320 if (numDeps[II->first] < totalNumCalls - 1)
321 parallelCalls.push_back(II->first);
322}
323
324
325void FindParallelCalls::VisitOutEdges(Instruction* I,
326 CallInst* root,
327 DependentsSet& depsOfRoot)
328{
329 assert(stmtsVisited.find(I) == stmtsVisited.end() && "Stmt visited twice?");
330 stmtsVisited.insert(I);
331
332 if (CallInst* CI = dyn_cast<CallInst>(I))
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000333 // FIXME: Ignoring parallelism in a loop. Here we're actually *ignoring*
334 // a self-dependence in order to get the count comparison right above.
335 // When we include loop parallelism, self-dependences should be included.
Misha Brukman99cc88b2004-02-29 23:09:10 +0000336 if (CI != root) {
337 // CallInst root has a path to CallInst I and any calls reachable from I
338 depsOfRoot.insert(CI);
339 if (completed[CI]) {
340 // We have already visited I so we know all nodes it can reach!
341 DependentsSet& depsOfI = dependents[CI];
342 depsOfRoot.insert(depsOfI.begin(), depsOfI.end());
343 return;
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000344 }
Misha Brukman99cc88b2004-02-29 23:09:10 +0000345 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000346
347 // If we reach here, we need to visit all children of I
348 for (PgmDependenceGraph::iterator DI = depGraph.outDepBegin(*I);
Misha Brukman99cc88b2004-02-29 23:09:10 +0000349 ! DI.fini(); ++DI) {
350 Instruction* sink = &DI->getSink()->getInstr();
351 if (stmtsVisited.find(sink) == stmtsVisited.end())
352 VisitOutEdges(sink, root, depsOfRoot);
353 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000354}
355
356
Misha Brukman99cc88b2004-02-29 23:09:10 +0000357void FindParallelCalls::visitCallInst(CallInst& CI) {
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000358 if (completed[&CI])
359 return;
360 stmtsVisited.clear(); // clear flags to do a fresh DFS
361
362 // Visit all children of CI using a recursive walk through dep graph
363 DependentsSet& depsOfRoot = dependents[&CI];
364 for (PgmDependenceGraph::iterator DI = depGraph.outDepBegin(CI);
Misha Brukman99cc88b2004-02-29 23:09:10 +0000365 ! DI.fini(); ++DI) {
366 Instruction* sink = &DI->getSink()->getInstr();
367 if (stmtsVisited.find(sink) == stmtsVisited.end())
368 VisitOutEdges(sink, &CI, depsOfRoot);
369 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000370
371 completed[&CI] = true;
372}
373
374
375//----------------------------------------------------------------------------
376// class Parallelize
377//
378// (1) Find candidate parallel functions: any function F s.t.
379// there is a call C1 to the function F that is followed or preceded
380// by at least one other call C2 that is independent of this one
381// (i.e., there is no dependence path from C1 to C2 or C2 to C1)
382// (2) Label such a function F as a cilk function.
383// (3) Convert every call to F to a spawn
384// (4) For every function X, insert sync statements so that
385// every spawn is postdominated by a sync before any statements
386// with a data dependence to/from the call site for the spawn
387//
388//----------------------------------------------------------------------------
389
390namespace {
Misha Brukman99cc88b2004-02-29 23:09:10 +0000391 class Parallelize: public Pass {
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000392 public:
393 /// Driver functions to transform a program
394 ///
395 bool run(Module& M);
396
397 /// getAnalysisUsage - Modifies extensively so preserve nothing.
398 /// Uses the DependenceGraph and the Top-down DS Graph (only to find
399 /// all functions called via an indirect call).
400 ///
401 void getAnalysisUsage(AnalysisUsage &AU) const {
402 AU.addRequired<TDDataStructures>();
403 AU.addRequired<MemoryDepAnalysis>(); // force this not to be released
404 AU.addRequired<PgmDependenceGraph>(); // because it is needed by this
405 }
406 };
407
408 RegisterOpt<Parallelize> X("parallel", "Parallelize program using Cilk");
409}
410
411
Misha Brukman99cc88b2004-02-29 23:09:10 +0000412bool Parallelize::run(Module& M) {
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000413 hash_set<Function*> parallelFunctions;
414 hash_set<Function*> safeParallelFunctions;
415 hash_set<const GlobalValue*> indirectlyCalled;
416
417 // If there is no main (i.e., for an incomplete program), we can do nothing.
418 // If there is a main, mark main as a parallel function.
Misha Brukman99cc88b2004-02-29 23:09:10 +0000419 Function* mainFunc = M.getMainFunction();
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000420 if (!mainFunc)
421 return false;
422
423 // (1) Find candidate parallel functions and mark them as Cilk functions
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000424 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
Misha Brukman99cc88b2004-02-29 23:09:10 +0000425 if (! FI->isExternal()) {
426 Function* F = FI;
427 DSGraph& tdg = getAnalysis<TDDataStructures>().getDSGraph(*F);
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000428
Misha Brukman99cc88b2004-02-29 23:09:10 +0000429 // All the hard analysis work gets done here!
430 FindParallelCalls finder(*F,
431 getAnalysis<PgmDependenceGraph>().getGraph(*F));
432 /* getAnalysis<MemoryDepAnalysis>().getGraph(*F)); */
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000433
Misha Brukman99cc88b2004-02-29 23:09:10 +0000434 // Now we know which call instructions are useful to parallelize.
435 // Remember those callee functions.
436 for (std::vector<CallInst*>::iterator
437 CII = finder.parallelCalls.begin(),
438 CIE = finder.parallelCalls.end(); CII != CIE; ++CII) {
439 // Check if this is a direct call...
440 if ((*CII)->getCalledFunction() != NULL) {
441 // direct call: if this is to a non-external function,
442 // mark it as a parallelizable function
443 if (! (*CII)->getCalledFunction()->isExternal())
444 parallelFunctions.insert((*CII)->getCalledFunction());
445 } else {
446 // Indirect call: mark all potential callees as bad
447 std::vector<GlobalValue*> callees =
448 tdg.getNodeForValue((*CII)->getCalledValue())
449 .getNode()->getGlobals();
450 indirectlyCalled.insert(callees.begin(), callees.end());
451 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000452 }
Misha Brukman99cc88b2004-02-29 23:09:10 +0000453 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000454
455 // Remove all indirectly called functions from the list of Cilk functions.
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000456 for (hash_set<Function*>::iterator PFI = parallelFunctions.begin(),
457 PFE = parallelFunctions.end(); PFI != PFE; ++PFI)
458 if (indirectlyCalled.count(*PFI) == 0)
459 safeParallelFunctions.insert(*PFI);
460
461#undef CAN_USE_BIND1ST_ON_REFERENCE_TYPE_ARGS
462#ifdef CAN_USE_BIND1ST_ON_REFERENCE_TYPE_ARGS
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000463 // Use this indecipherable STLese because erase invalidates iterators.
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000464 // Otherwise we have to copy sets as above.
465 hash_set<Function*>::iterator extrasBegin =
466 std::remove_if(parallelFunctions.begin(), parallelFunctions.end(),
467 compose1(std::bind2nd(std::greater<int>(), 0),
468 bind_obj(&indirectlyCalled,
469 &hash_set<const GlobalValue*>::count)));
470 parallelFunctions.erase(extrasBegin, parallelFunctions.end());
471#endif
472
473 // If there are no parallel functions, we can just give up.
474 if (safeParallelFunctions.empty())
475 return false;
476
477 // Add main as a parallel function since Cilk requires this.
478 safeParallelFunctions.insert(mainFunc);
479
480 // (2,3) Transform each Cilk function and all its calls simply by
481 // adding a unique suffix to the function name.
482 // This should identify both functions and calls to such functions
483 // to the code generator.
484 // (4) Also, insert calls to sync at appropriate points.
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000485 Cilkifier cilkifier(M);
486 for (hash_set<Function*>::iterator CFI = safeParallelFunctions.begin(),
Misha Brukman99cc88b2004-02-29 23:09:10 +0000487 CFE = safeParallelFunctions.end(); CFI != CFE; ++CFI) {
488 cilkifier.TransformFunc(*CFI, safeParallelFunctions,
489 getAnalysis<PgmDependenceGraph>().getGraph(**CFI));
490 /* getAnalysis<MemoryDepAnalysis>().getGraph(**CFI)); */
491 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000492
493 return true;
494}
Brian Gaeked0fde302003-11-11 22:41:34 +0000495