blob: a12e323a9b5a9f56848dd01d63b750fdc13b2332 [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"
Jeff Cohen1d7b5de2005-01-09 20:42:52 +000045#include "llvm/Analysis/Passes.h"
Chris Lattner4dabb2c2004-07-07 06:32:21 +000046#include "llvm/Analysis/DataStructure/DataStructure.h"
47#include "llvm/Analysis/DataStructure/DSGraph.h"
Vikram S. Advee12c74c2002-12-10 00:43:34 +000048#include "llvm/Support/InstVisitor.h"
Chris Lattnera2dc7272004-03-14 02:13:38 +000049#include "llvm/Transforms/Utils/Local.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000050#include "llvm/ADT/Statistic.h"
51#include "llvm/ADT/STLExtras.h"
52#include "llvm/ADT/hash_set"
53#include "llvm/ADT/hash_map"
Vikram S. Advee12c74c2002-12-10 00:43:34 +000054#include <functional>
55#include <algorithm>
Chris Lattner1e2385b2003-11-21 21:54:22 +000056using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000057
Vikram S. Advee12c74c2002-12-10 00:43:34 +000058//----------------------------------------------------------------------------
Chris Lattner09a67052003-09-01 16:49:38 +000059// Global constants used in marking Cilk functions and function calls.
60//----------------------------------------------------------------------------
61
62static const char * const CilkSuffix = ".llvm2cilk";
63static const char * const DummySyncFuncName = "__sync.llvm2cilk";
64
65//----------------------------------------------------------------------------
66// Routines to identify Cilk functions, calls to Cilk functions, and syncs.
67//----------------------------------------------------------------------------
68
69static bool isCilk(const Function& F) {
70 return (F.getName().rfind(CilkSuffix) ==
71 F.getName().size() - std::strlen(CilkSuffix));
72}
73
74static bool isCilkMain(const Function& F) {
75 return F.getName() == "main" + std::string(CilkSuffix);
76}
77
78
79static bool isCilk(const CallInst& CI) {
80 return CI.getCalledFunction() && isCilk(*CI.getCalledFunction());
81}
82
83static bool isSync(const CallInst& CI) {
84 return CI.getCalledFunction() &&
85 CI.getCalledFunction()->getName() == DummySyncFuncName;
86}
87
88
89//----------------------------------------------------------------------------
Vikram S. Advee12c74c2002-12-10 00:43:34 +000090// class Cilkifier
91//
92// Code generation pass that transforms code to identify where Cilk keywords
Misha Brukmancf00c4a2003-10-10 17:57:28 +000093// should be inserted. This relies on `llvm-dis -c' to print out the keywords.
Vikram S. Advee12c74c2002-12-10 00:43:34 +000094//----------------------------------------------------------------------------
Misha Brukman99cc88b2004-02-29 23:09:10 +000095class Cilkifier: public InstVisitor<Cilkifier> {
Vikram S. Advee12c74c2002-12-10 00:43:34 +000096 Function* DummySyncFunc;
97
98 // Data used when transforming each function.
99 hash_set<const Instruction*> stmtsVisited; // Flags for recursive DFS
100 hash_map<const CallInst*, hash_set<CallInst*> > spawnToSyncsMap;
101
102 // Input data for the transformation.
103 const hash_set<Function*>* cilkFunctions; // Set of parallel functions
104 PgmDependenceGraph* depGraph;
105
106 void DFSVisitInstr (Instruction* I,
107 Instruction* root,
108 hash_set<const Instruction*>& depsOfRoot);
109
110public:
111 /*ctor*/ Cilkifier (Module& M);
112
113 // Transform a single function including its name, its call sites, and syncs
114 //
115 void TransformFunc (Function* F,
116 const hash_set<Function*>& cilkFunctions,
117 PgmDependenceGraph& _depGraph);
118
119 // The visitor function that does most of the hard work, via DFSVisitInstr
120 //
121 void visitCallInst(CallInst& CI);
122};
123
124
Misha Brukman99cc88b2004-02-29 23:09:10 +0000125Cilkifier::Cilkifier(Module& M) {
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000126 // create the dummy Sync function and add it to the Module
Chris Lattner273328e2003-09-01 16:53:46 +0000127 DummySyncFunc = M.getOrInsertFunction(DummySyncFuncName, Type::VoidTy, 0);
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000128}
129
130void Cilkifier::TransformFunc(Function* F,
131 const hash_set<Function*>& _cilkFunctions,
Misha Brukman99cc88b2004-02-29 23:09:10 +0000132 PgmDependenceGraph& _depGraph) {
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000133 // Memoize the information for this function
134 cilkFunctions = &_cilkFunctions;
135 depGraph = &_depGraph;
136
137 // Add the marker suffix to the Function name
138 // This should automatically mark all calls to the function also!
139 F->setName(F->getName() + CilkSuffix);
140
141 // Insert sync operations for each separate spawn
142 visit(*F);
143
144 // Now traverse the CFG in rPostorder and eliminate redundant syncs, i.e.,
145 // two consecutive sync's on a straight-line path with no intervening spawn.
146
147}
148
149
150void Cilkifier::DFSVisitInstr(Instruction* I,
151 Instruction* root,
152 hash_set<const Instruction*>& depsOfRoot)
153{
154 assert(stmtsVisited.find(I) == stmtsVisited.end());
155 stmtsVisited.insert(I);
156
157 // If there is a dependence from root to I, insert Sync and return
Misha Brukman99cc88b2004-02-29 23:09:10 +0000158 if (depsOfRoot.find(I) != depsOfRoot.end()) {
159 // Insert a sync before I and stop searching along this path.
160 // If I is a Phi instruction, the dependence can only be an SSA dep.
161 // and we need to insert the sync in the predecessor on the appropriate
162 // incoming edge!
163 CallInst* syncI = 0;
164 if (PHINode* phiI = dyn_cast<PHINode>(I)) {
165 // check all operands of the Phi and insert before each one
166 for (unsigned i = 0, N = phiI->getNumIncomingValues(); i < N; ++i)
167 if (phiI->getIncomingValue(i) == root)
168 syncI = new CallInst(DummySyncFunc, std::vector<Value*>(), "",
169 phiI->getIncomingBlock(i)->getTerminator());
170 } else
171 syncI = new CallInst(DummySyncFunc, std::vector<Value*>(), "", I);
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000172
Misha Brukman99cc88b2004-02-29 23:09:10 +0000173 // Remember the sync for each spawn to eliminate redundant ones later
174 spawnToSyncsMap[cast<CallInst>(root)].insert(syncI);
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000175
Misha Brukman99cc88b2004-02-29 23:09:10 +0000176 return;
177 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000178
179 // else visit unvisited successors
Misha Brukman99cc88b2004-02-29 23:09:10 +0000180 if (BranchInst* brI = dyn_cast<BranchInst>(I)) {
181 // visit first instruction in each successor BB
182 for (unsigned i = 0, N = brI->getNumSuccessors(); i < N; ++i)
183 if (stmtsVisited.find(&brI->getSuccessor(i)->front())
184 == stmtsVisited.end())
185 DFSVisitInstr(&brI->getSuccessor(i)->front(), root, depsOfRoot);
186 } else
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000187 if (Instruction* nextI = I->getNext())
188 if (stmtsVisited.find(nextI) == stmtsVisited.end())
189 DFSVisitInstr(nextI, root, depsOfRoot);
190}
191
192
193void Cilkifier::visitCallInst(CallInst& CI)
194{
195 assert(CI.getCalledFunction() != 0 && "Only direct calls can be spawned.");
196 if (cilkFunctions->find(CI.getCalledFunction()) == cilkFunctions->end())
197 return; // not a spawn
198
199 // Find all the outgoing memory dependences.
200 hash_set<const Instruction*> depsOfRoot;
201 for (PgmDependenceGraph::iterator DI =
202 depGraph->outDepBegin(CI, MemoryDeps); ! DI.fini(); ++DI)
203 depsOfRoot.insert(&DI->getSink()->getInstr());
204
205 // Now find all outgoing SSA dependences to the eventual non-Phi users of
206 // the call value (i.e., direct users that are not phis, and for any
207 // user that is a Phi, direct non-Phi users of that Phi, and recursively).
Chris Lattneraa921452003-09-01 16:42:16 +0000208 std::vector<const PHINode*> phiUsers;
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000209 hash_set<const PHINode*> phisSeen; // ensures we don't visit a phi twice
210 for (Value::use_iterator UI=CI.use_begin(), UE=CI.use_end(); UI != UE; ++UI)
Misha Brukman99cc88b2004-02-29 23:09:10 +0000211 if (const PHINode* phiUser = dyn_cast<PHINode>(*UI)) {
212 if (phisSeen.find(phiUser) == phisSeen.end()) {
213 phiUsers.push_back(phiUser);
214 phisSeen.insert(phiUser);
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000215 }
Misha Brukman99cc88b2004-02-29 23:09:10 +0000216 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000217 else
218 depsOfRoot.insert(cast<Instruction>(*UI));
219
220 // Now we've found the non-Phi users and immediate phi users.
221 // Recursively walk the phi users and add their non-phi users.
Misha Brukman99cc88b2004-02-29 23:09:10 +0000222 for (const PHINode* phiUser; !phiUsers.empty(); phiUsers.pop_back()) {
223 phiUser = phiUsers.back();
224 for (Value::use_const_iterator UI=phiUser->use_begin(),
225 UE=phiUser->use_end(); UI != UE; ++UI)
226 if (const PHINode* pn = dyn_cast<PHINode>(*UI)) {
227 if (phisSeen.find(pn) == phisSeen.end()) {
228 phiUsers.push_back(pn);
229 phisSeen.insert(pn);
230 }
231 } else
232 depsOfRoot.insert(cast<Instruction>(*UI));
233 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000234
235 // Walk paths of the CFG starting at the call instruction and insert
236 // one sync before the first dependence on each path, if any.
Misha Brukman99cc88b2004-02-29 23:09:10 +0000237 if (! depsOfRoot.empty()) {
238 stmtsVisited.clear(); // start a new DFS for this CallInst
239 assert(CI.getNext() && "Call instruction cannot be a terminator!");
240 DFSVisitInstr(CI.getNext(), &CI, depsOfRoot);
241 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000242
243 // Now, eliminate all users of the SSA value of the CallInst, i.e.,
244 // if the call instruction returns a value, delete the return value
245 // register and replace it by a stack slot.
246 if (CI.getType() != Type::VoidTy)
247 DemoteRegToStack(CI);
248}
249
250
251//----------------------------------------------------------------------------
252// class FindParallelCalls
253//
254// Find all CallInst instructions that have at least one other CallInst
255// that is independent. These are the instructions that can produce
256// useful parallelism.
257//----------------------------------------------------------------------------
258
Chris Lattner80431272003-08-06 17:16:24 +0000259class FindParallelCalls : public InstVisitor<FindParallelCalls> {
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000260 typedef hash_set<CallInst*> DependentsSet;
261 typedef DependentsSet::iterator Dependents_iterator;
262 typedef DependentsSet::const_iterator Dependents_const_iterator;
263
264 PgmDependenceGraph& depGraph; // dependence graph for the function
265 hash_set<Instruction*> stmtsVisited; // flags for DFS walk of depGraph
266 hash_map<CallInst*, bool > completed; // flags marking if a CI is done
267 hash_map<CallInst*, DependentsSet> dependents; // dependent CIs for each CI
268
269 void VisitOutEdges(Instruction* I,
270 CallInst* root,
271 DependentsSet& depsOfRoot);
272
Chris Lattner80431272003-08-06 17:16:24 +0000273 FindParallelCalls(const FindParallelCalls &); // DO NOT IMPLEMENT
274 void operator=(const FindParallelCalls&); // DO NOT IMPLEMENT
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000275public:
276 std::vector<CallInst*> parallelCalls;
277
278public:
279 /*ctor*/ FindParallelCalls (Function& F, PgmDependenceGraph& DG);
280 void visitCallInst (CallInst& CI);
281};
282
283
284FindParallelCalls::FindParallelCalls(Function& F,
285 PgmDependenceGraph& DG)
286 : depGraph(DG)
287{
288 // Find all CallInsts reachable from each CallInst using a recursive DFS
289 visit(F);
290
291 // Now we've found all CallInsts reachable from each CallInst.
292 // Find those CallInsts that are parallel with at least one other CallInst
293 // by counting total inEdges and outEdges.
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000294 unsigned long totalNumCalls = completed.size();
295
Misha Brukman99cc88b2004-02-29 23:09:10 +0000296 if (totalNumCalls == 1) {
297 // Check first for the special case of a single call instruction not
298 // in any loop. It is not parallel, even if it has no dependences
299 // (this is why it is a special case).
300 //
301 // FIXME:
302 // THIS CASE IS NOT HANDLED RIGHT NOW, I.E., THERE IS NO
303 // PARALLELISM FOR CALLS IN DIFFERENT ITERATIONS OF A LOOP.
304 return;
305 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000306
307 hash_map<CallInst*, unsigned long> numDeps;
308 for (hash_map<CallInst*, DependentsSet>::iterator II = dependents.begin(),
Misha Brukman99cc88b2004-02-29 23:09:10 +0000309 IE = dependents.end(); II != IE; ++II) {
310 CallInst* fromCI = II->first;
311 numDeps[fromCI] += II->second.size();
312 for (Dependents_iterator DI = II->second.begin(), DE = II->second.end();
313 DI != DE; ++DI)
314 numDeps[*DI]++; // *DI can be reached from II->first
315 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000316
317 for (hash_map<CallInst*, DependentsSet>::iterator
318 II = dependents.begin(), IE = dependents.end(); II != IE; ++II)
319
320 // FIXME: Remove "- 1" when considering parallelism in loops
321 if (numDeps[II->first] < totalNumCalls - 1)
322 parallelCalls.push_back(II->first);
323}
324
325
326void FindParallelCalls::VisitOutEdges(Instruction* I,
327 CallInst* root,
328 DependentsSet& depsOfRoot)
329{
330 assert(stmtsVisited.find(I) == stmtsVisited.end() && "Stmt visited twice?");
331 stmtsVisited.insert(I);
332
333 if (CallInst* CI = dyn_cast<CallInst>(I))
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000334 // FIXME: Ignoring parallelism in a loop. Here we're actually *ignoring*
335 // a self-dependence in order to get the count comparison right above.
336 // When we include loop parallelism, self-dependences should be included.
Misha Brukman99cc88b2004-02-29 23:09:10 +0000337 if (CI != root) {
338 // CallInst root has a path to CallInst I and any calls reachable from I
339 depsOfRoot.insert(CI);
340 if (completed[CI]) {
341 // We have already visited I so we know all nodes it can reach!
342 DependentsSet& depsOfI = dependents[CI];
343 depsOfRoot.insert(depsOfI.begin(), depsOfI.end());
344 return;
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000345 }
Misha Brukman99cc88b2004-02-29 23:09:10 +0000346 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000347
348 // If we reach here, we need to visit all children of I
349 for (PgmDependenceGraph::iterator DI = depGraph.outDepBegin(*I);
Misha Brukman99cc88b2004-02-29 23:09:10 +0000350 ! DI.fini(); ++DI) {
351 Instruction* sink = &DI->getSink()->getInstr();
352 if (stmtsVisited.find(sink) == stmtsVisited.end())
353 VisitOutEdges(sink, root, depsOfRoot);
354 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000355}
356
357
Misha Brukman99cc88b2004-02-29 23:09:10 +0000358void FindParallelCalls::visitCallInst(CallInst& CI) {
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000359 if (completed[&CI])
360 return;
361 stmtsVisited.clear(); // clear flags to do a fresh DFS
362
363 // Visit all children of CI using a recursive walk through dep graph
364 DependentsSet& depsOfRoot = dependents[&CI];
365 for (PgmDependenceGraph::iterator DI = depGraph.outDepBegin(CI);
Misha Brukman99cc88b2004-02-29 23:09:10 +0000366 ! DI.fini(); ++DI) {
367 Instruction* sink = &DI->getSink()->getInstr();
368 if (stmtsVisited.find(sink) == stmtsVisited.end())
369 VisitOutEdges(sink, &CI, depsOfRoot);
370 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000371
372 completed[&CI] = true;
373}
374
375
376//----------------------------------------------------------------------------
377// class Parallelize
378//
379// (1) Find candidate parallel functions: any function F s.t.
380// there is a call C1 to the function F that is followed or preceded
381// by at least one other call C2 that is independent of this one
382// (i.e., there is no dependence path from C1 to C2 or C2 to C1)
383// (2) Label such a function F as a cilk function.
384// (3) Convert every call to F to a spawn
385// (4) For every function X, insert sync statements so that
386// every spawn is postdominated by a sync before any statements
387// with a data dependence to/from the call site for the spawn
388//
389//----------------------------------------------------------------------------
390
391namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +0000392 class Parallelize : public ModulePass {
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000393 public:
394 /// Driver functions to transform a program
395 ///
Chris Lattnerb12914b2004-09-20 04:48:05 +0000396 bool runOnModule(Module& M);
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000397
398 /// getAnalysisUsage - Modifies extensively so preserve nothing.
399 /// Uses the DependenceGraph and the Top-down DS Graph (only to find
400 /// all functions called via an indirect call).
401 ///
402 void getAnalysisUsage(AnalysisUsage &AU) const {
403 AU.addRequired<TDDataStructures>();
404 AU.addRequired<MemoryDepAnalysis>(); // force this not to be released
405 AU.addRequired<PgmDependenceGraph>(); // because it is needed by this
406 }
407 };
408
409 RegisterOpt<Parallelize> X("parallel", "Parallelize program using Cilk");
410}
411
Jeff Cohen1d7b5de2005-01-09 20:42:52 +0000412ModulePass *llvm::createParallelizePass() { return new Parallelize(); }
413
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000414
Chris Lattnerb12914b2004-09-20 04:48:05 +0000415bool Parallelize::runOnModule(Module& M) {
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000416 hash_set<Function*> parallelFunctions;
417 hash_set<Function*> safeParallelFunctions;
418 hash_set<const GlobalValue*> indirectlyCalled;
419
420 // If there is no main (i.e., for an incomplete program), we can do nothing.
421 // If there is a main, mark main as a parallel function.
Misha Brukman99cc88b2004-02-29 23:09:10 +0000422 Function* mainFunc = M.getMainFunction();
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000423 if (!mainFunc)
424 return false;
425
426 // (1) Find candidate parallel functions and mark them as Cilk functions
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000427 for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
Misha Brukman99cc88b2004-02-29 23:09:10 +0000428 if (! FI->isExternal()) {
429 Function* F = FI;
430 DSGraph& tdg = getAnalysis<TDDataStructures>().getDSGraph(*F);
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000431
Misha Brukman99cc88b2004-02-29 23:09:10 +0000432 // All the hard analysis work gets done here!
433 FindParallelCalls finder(*F,
434 getAnalysis<PgmDependenceGraph>().getGraph(*F));
435 /* getAnalysis<MemoryDepAnalysis>().getGraph(*F)); */
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000436
Misha Brukman99cc88b2004-02-29 23:09:10 +0000437 // Now we know which call instructions are useful to parallelize.
438 // Remember those callee functions.
439 for (std::vector<CallInst*>::iterator
440 CII = finder.parallelCalls.begin(),
441 CIE = finder.parallelCalls.end(); CII != CIE; ++CII) {
442 // Check if this is a direct call...
443 if ((*CII)->getCalledFunction() != NULL) {
444 // direct call: if this is to a non-external function,
445 // mark it as a parallelizable function
446 if (! (*CII)->getCalledFunction()->isExternal())
447 parallelFunctions.insert((*CII)->getCalledFunction());
448 } else {
449 // Indirect call: mark all potential callees as bad
450 std::vector<GlobalValue*> callees =
451 tdg.getNodeForValue((*CII)->getCalledValue())
452 .getNode()->getGlobals();
453 indirectlyCalled.insert(callees.begin(), callees.end());
454 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000455 }
Misha Brukman99cc88b2004-02-29 23:09:10 +0000456 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000457
458 // Remove all indirectly called functions from the list of Cilk functions.
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000459 for (hash_set<Function*>::iterator PFI = parallelFunctions.begin(),
460 PFE = parallelFunctions.end(); PFI != PFE; ++PFI)
461 if (indirectlyCalled.count(*PFI) == 0)
462 safeParallelFunctions.insert(*PFI);
463
464#undef CAN_USE_BIND1ST_ON_REFERENCE_TYPE_ARGS
465#ifdef CAN_USE_BIND1ST_ON_REFERENCE_TYPE_ARGS
Misha Brukmancf00c4a2003-10-10 17:57:28 +0000466 // Use this indecipherable STLese because erase invalidates iterators.
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000467 // Otherwise we have to copy sets as above.
468 hash_set<Function*>::iterator extrasBegin =
469 std::remove_if(parallelFunctions.begin(), parallelFunctions.end(),
470 compose1(std::bind2nd(std::greater<int>(), 0),
471 bind_obj(&indirectlyCalled,
472 &hash_set<const GlobalValue*>::count)));
473 parallelFunctions.erase(extrasBegin, parallelFunctions.end());
474#endif
475
476 // If there are no parallel functions, we can just give up.
477 if (safeParallelFunctions.empty())
478 return false;
479
480 // Add main as a parallel function since Cilk requires this.
481 safeParallelFunctions.insert(mainFunc);
482
483 // (2,3) Transform each Cilk function and all its calls simply by
484 // adding a unique suffix to the function name.
485 // This should identify both functions and calls to such functions
486 // to the code generator.
487 // (4) Also, insert calls to sync at appropriate points.
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000488 Cilkifier cilkifier(M);
489 for (hash_set<Function*>::iterator CFI = safeParallelFunctions.begin(),
Misha Brukman99cc88b2004-02-29 23:09:10 +0000490 CFE = safeParallelFunctions.end(); CFI != CFE; ++CFI) {
491 cilkifier.TransformFunc(*CFI, safeParallelFunctions,
492 getAnalysis<PgmDependenceGraph>().getGraph(**CFI));
493 /* getAnalysis<MemoryDepAnalysis>().getGraph(**CFI)); */
494 }
Vikram S. Advee12c74c2002-12-10 00:43:34 +0000495
496 return true;
497}
Brian Gaeked0fde302003-11-11 22:41:34 +0000498