1 //===-- IPO/OpenMPOpt.cpp - Collection of OpenMP specific optimizations ---===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // OpenMP specific optimizations:
10 //
11 // - Deduplication of runtime calls, e.g., omp_get_thread_num.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Transforms/IPO/OpenMPOpt.h"
16 
17 #include "llvm/ADT/EnumeratedArray.h"
18 #include "llvm/ADT/Statistic.h"
19 #include "llvm/Analysis/CallGraph.h"
20 #include "llvm/Analysis/CallGraphSCCPass.h"
21 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
22 #include "llvm/Frontend/OpenMP/OMPConstants.h"
23 #include "llvm/Frontend/OpenMP/OMPIRBuilder.h"
24 #include "llvm/InitializePasses.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Transforms/IPO.h"
27 #include "llvm/Transforms/Utils/CallGraphUpdater.h"
28 
29 using namespace llvm;
30 using namespace omp;
31 using namespace types;
32 
33 #define DEBUG_TYPE "openmp-opt"
34 
35 static cl::opt<bool> DisableOpenMPOptimizations(
36     "openmp-opt-disable", cl::ZeroOrMore,
37     cl::desc("Disable OpenMP specific optimizations."), cl::Hidden,
38     cl::init(false));
39 
40 STATISTIC(NumOpenMPRuntimeCallsDeduplicated,
41           "Number of OpenMP runtime calls deduplicated");
42 STATISTIC(NumOpenMPRuntimeFunctionsIdentified,
43           "Number of OpenMP runtime functions identified");
44 STATISTIC(NumOpenMPRuntimeFunctionUsesIdentified,
45           "Number of OpenMP runtime function uses identified");
46 
47 #if !defined(NDEBUG)
48 static constexpr auto TAG = "[" DEBUG_TYPE "]";
49 #endif
50 
51 namespace {
52 struct OpenMPOpt {
53 
54   using OptimizationRemarkGetter =
55       function_ref<OptimizationRemarkEmitter &(Function *)>;
56 
57   OpenMPOpt(SmallVectorImpl<Function *> &SCC,
58             SmallPtrSetImpl<Function *> &ModuleSlice,
59             CallGraphUpdater &CGUpdater, OptimizationRemarkGetter OREGetter)
60       : M(*(*SCC.begin())->getParent()), SCC(SCC), ModuleSlice(ModuleSlice),
61         OMPBuilder(M), CGUpdater(CGUpdater), OREGetter(OREGetter) {
62     initializeTypes(M);
63     initializeRuntimeFunctions();
64     OMPBuilder.initialize();
65   }
66 
67   /// Generic information that describes a runtime function
68   struct RuntimeFunctionInfo {
69 
70     /// The kind, as described by the RuntimeFunction enum.
71     RuntimeFunction Kind;
72 
73     /// The name of the function.
74     StringRef Name;
75 
76     /// Flag to indicate a variadic function.
77     bool IsVarArg;
78 
79     /// The return type of the function.
80     Type *ReturnType;
81 
82     /// The argument types of the function.
83     SmallVector<Type *, 8> ArgumentTypes;
84 
85     /// The declaration if available.
86     Function *Declaration = nullptr;
87 
88     /// Uses of this runtime function per function containing the use.
89     using UseVector = SmallVector<Use *, 16>;
90 
91     /// Return the vector of uses in function \p F.
92     UseVector &getOrCreateUseVector(Function *F) {
93       std::unique_ptr<UseVector> &UV = UsesMap[F];
94       if (!UV)
95         UV = std::make_unique<UseVector>();
96       return *UV;
97     }
98 
99     /// Return the vector of uses in function \p F or `nullptr` if there are
100     /// none.
101     const UseVector *getUseVector(Function &F) const {
102       auto I = UsesMap.find(&F);
103       if (I != UsesMap.end())
104         return I->second.get();
105       return nullptr;
106     }
107 
108     /// Return how many functions contain uses of this runtime function.
109     size_t getNumFunctionsWithUses() const { return UsesMap.size(); }
110 
111     /// Return the number of arguments (or the minimal number for variadic
112     /// functions).
113     size_t getNumArgs() const { return ArgumentTypes.size(); }
114 
115     /// Run the callback \p CB on each use and forget the use if the result is
116     /// true. The callback will be fed the function in which the use was
117     /// encountered as second argument.
118     void foreachUse(function_ref<bool(Use &, Function &)> CB) {
119       SmallVector<unsigned, 8> ToBeDeleted;
120       for (auto &It : UsesMap) {
121         ToBeDeleted.clear();
122         unsigned Idx = 0;
123         UseVector &UV = *It.second;
124         for (Use *U : UV) {
125           if (CB(*U, *It.first))
126             ToBeDeleted.push_back(Idx);
127           ++Idx;
128         }
129 
130         // Remove the to-be-deleted indices in reverse order as prior
131         // modifcations will not modify the smaller indices.
132         while (!ToBeDeleted.empty()) {
133           unsigned Idx = ToBeDeleted.pop_back_val();
134           UV[Idx] = UV.back();
135           UV.pop_back();
136         }
137       }
138     }
139 
140   private:
141     /// Map from functions to all uses of this runtime function contained in
142     /// them.
143     DenseMap<Function *, std::unique_ptr<UseVector>> UsesMap;
144   };
145 
146   /// Run all OpenMP optimizations on the underlying SCC/ModuleSlice.
147   bool run() {
148     bool Changed = false;
149 
150     LLVM_DEBUG(dbgs() << TAG << "Run on SCC with " << SCC.size()
151                       << " functions in a slice with " << ModuleSlice.size()
152                       << " functions\n");
153 
154     Changed |= deduplicateRuntimeCalls();
155     Changed |= deleteParallelRegions();
156 
157     return Changed;
158   }
159 
160 private:
161   /// Try to delete parallel regions if possible.
162   bool deleteParallelRegions() {
163     const unsigned CallbackCalleeOperand = 2;
164 
165     RuntimeFunctionInfo &RFI = RFIs[OMPRTL___kmpc_fork_call];
166     if (!RFI.Declaration)
167       return false;
168 
169     bool Changed = false;
170     auto DeleteCallCB = [&](Use &U, Function &) {
171       CallInst *CI = getCallIfRegularCall(U);
172       if (!CI)
173         return false;
174       auto *Fn = dyn_cast<Function>(
175           CI->getArgOperand(CallbackCalleeOperand)->stripPointerCasts());
176       if (!Fn)
177         return false;
178       if (!Fn->onlyReadsMemory())
179         return false;
180       if (!Fn->hasFnAttribute(Attribute::WillReturn))
181         return false;
182 
183       LLVM_DEBUG(dbgs() << TAG << "Delete read-only parallel region in "
184                         << CI->getCaller()->getName() << "\n");
185 
186       auto Remark = [&](OptimizationRemark OR) {
187         return OR << "Parallel region in "
188                   << ore::NV("OpenMPParallelDelete", CI->getCaller()->getName())
189                   << " deleted";
190       };
191       emitRemark<OptimizationRemark>(CI, "OpenMPParallelRegionDeletion",
192                                      Remark);
193 
194       CGUpdater.removeCallSite(*CI);
195       CI->eraseFromParent();
196       Changed = true;
197       return true;
198     };
199 
200     RFI.foreachUse(DeleteCallCB);
201 
202     return Changed;
203   }
204 
205   /// Try to eliminiate runtime calls by reusing existing ones.
206   bool deduplicateRuntimeCalls() {
207     bool Changed = false;
208 
209     RuntimeFunction DeduplicableRuntimeCallIDs[] = {
210         OMPRTL_omp_get_num_threads,
211         OMPRTL_omp_in_parallel,
212         OMPRTL_omp_get_cancellation,
213         OMPRTL_omp_get_thread_limit,
214         OMPRTL_omp_get_supported_active_levels,
215         OMPRTL_omp_get_level,
216         OMPRTL_omp_get_ancestor_thread_num,
217         OMPRTL_omp_get_team_size,
218         OMPRTL_omp_get_active_level,
219         OMPRTL_omp_in_final,
220         OMPRTL_omp_get_proc_bind,
221         OMPRTL_omp_get_num_places,
222         OMPRTL_omp_get_num_procs,
223         OMPRTL_omp_get_place_num,
224         OMPRTL_omp_get_partition_num_places,
225         OMPRTL_omp_get_partition_place_nums};
226 
227     // Global-tid is handled separatly.
228     SmallSetVector<Value *, 16> GTIdArgs;
229     collectGlobalThreadIdArguments(GTIdArgs);
230     LLVM_DEBUG(dbgs() << TAG << "Found " << GTIdArgs.size()
231                       << " global thread ID arguments\n");
232 
233     for (Function *F : SCC) {
234       for (auto DeduplicableRuntimeCallID : DeduplicableRuntimeCallIDs)
235         deduplicateRuntimeCalls(*F, RFIs[DeduplicableRuntimeCallID]);
236 
237       // __kmpc_global_thread_num is special as we can replace it with an
238       // argument in enough cases to make it worth trying.
239       Value *GTIdArg = nullptr;
240       for (Argument &Arg : F->args())
241         if (GTIdArgs.count(&Arg)) {
242           GTIdArg = &Arg;
243           break;
244         }
245       Changed |= deduplicateRuntimeCalls(
246           *F, RFIs[OMPRTL___kmpc_global_thread_num], GTIdArg);
247     }
248 
249     return Changed;
250   }
251 
252   static Value *combinedIdentStruct(Value *CurrentIdent, Value *NextIdent,
253                                     bool GlobalOnly, bool &SingleChoice) {
254     if (CurrentIdent == NextIdent)
255       return CurrentIdent;
256 
257     // TODO: Figure out how to actually combine multiple debug locations. For
258     //       now we just keep an existing one if there is a single choice.
259     if (!GlobalOnly || isa<GlobalValue>(NextIdent)) {
260       SingleChoice = !CurrentIdent;
261       return NextIdent;
262     }
263     return nullptr;
264   }
265 
266   /// Return an `struct ident_t*` value that represents the ones used in the
267   /// calls of \p RFI inside of \p F. If \p GlobalOnly is true, we will not
268   /// return a local `struct ident_t*`. For now, if we cannot find a suitable
269   /// return value we create one from scratch. We also do not yet combine
270   /// information, e.g., the source locations, see combinedIdentStruct.
271   Value *getCombinedIdentFromCallUsesIn(RuntimeFunctionInfo &RFI, Function &F,
272                                         bool GlobalOnly) {
273     bool SingleChoice = true;
274     Value *Ident = nullptr;
275     auto CombineIdentStruct = [&](Use &U, Function &Caller) {
276       CallInst *CI = getCallIfRegularCall(U, &RFI);
277       if (!CI || &F != &Caller)
278         return false;
279       Ident = combinedIdentStruct(Ident, CI->getArgOperand(0),
280                                   /* GlobalOnly */ true, SingleChoice);
281       return false;
282     };
283     RFI.foreachUse(CombineIdentStruct);
284 
285     if (!Ident || !SingleChoice) {
286       // The IRBuilder uses the insertion block to get to the module, this is
287       // unfortunate but we work around it for now.
288       if (!OMPBuilder.getInsertionPoint().getBlock())
289         OMPBuilder.updateToLocation(OpenMPIRBuilder::InsertPointTy(
290             &F.getEntryBlock(), F.getEntryBlock().begin()));
291       // Create a fallback location if non was found.
292       // TODO: Use the debug locations of the calls instead.
293       Constant *Loc = OMPBuilder.getOrCreateDefaultSrcLocStr();
294       Ident = OMPBuilder.getOrCreateIdent(Loc);
295     }
296     return Ident;
297   }
298 
299   /// Try to eliminiate calls of \p RFI in \p F by reusing an existing one or
300   /// \p ReplVal if given.
301   bool deduplicateRuntimeCalls(Function &F, RuntimeFunctionInfo &RFI,
302                                Value *ReplVal = nullptr) {
303     auto *UV = RFI.getUseVector(F);
304     if (!UV || UV->size() + (ReplVal != nullptr) < 2)
305       return false;
306 
307     LLVM_DEBUG(dbgs() << TAG << "Deduplicate " << UV->size() << " uses of "
308                       << RFI.Name
309                       << (ReplVal ? " with an existing value\n" : "\n")
310                       << "\n");
311     assert((!ReplVal || (isa<Argument>(ReplVal) &&
312                          cast<Argument>(ReplVal)->getParent() == &F)) &&
313            "Unexpected replacement value!");
314 
315     // TODO: Use dominance to find a good position instead.
316     auto CanBeMoved = [](CallBase &CB) {
317       unsigned NumArgs = CB.getNumArgOperands();
318       if (NumArgs == 0)
319         return true;
320       if (CB.getArgOperand(0)->getType() != IdentPtr)
321         return false;
322       for (unsigned u = 1; u < NumArgs; ++u)
323         if (isa<Instruction>(CB.getArgOperand(u)))
324           return false;
325       return true;
326     };
327 
328     if (!ReplVal) {
329       for (Use *U : *UV)
330         if (CallInst *CI = getCallIfRegularCall(*U, &RFI)) {
331           if (!CanBeMoved(*CI))
332             continue;
333 
334           auto Remark = [&](OptimizationRemark OR) {
335             auto newLoc = &*F.getEntryBlock().getFirstInsertionPt();
336             return OR << "OpenMP runtime call "
337                       << ore::NV("OpenMPOptRuntime", RFI.Name) << " moved to "
338                       << ore::NV("OpenMPRuntimeMoves", newLoc->getDebugLoc());
339           };
340           emitRemark<OptimizationRemark>(CI, "OpenMPRuntimeCodeMotion", Remark);
341 
342           CI->moveBefore(&*F.getEntryBlock().getFirstInsertionPt());
343           ReplVal = CI;
344           break;
345         }
346       if (!ReplVal)
347         return false;
348     }
349 
350     // If we use a call as a replacement value we need to make sure the ident is
351     // valid at the new location. For now we just pick a global one, either
352     // existing and used by one of the calls, or created from scratch.
353     if (CallBase *CI = dyn_cast<CallBase>(ReplVal)) {
354       if (CI->getNumArgOperands() > 0 &&
355           CI->getArgOperand(0)->getType() == IdentPtr) {
356         Value *Ident = getCombinedIdentFromCallUsesIn(RFI, F,
357                                                       /* GlobalOnly */ true);
358         CI->setArgOperand(0, Ident);
359       }
360     }
361 
362     bool Changed = false;
363     auto ReplaceAndDeleteCB = [&](Use &U, Function &Caller) {
364       CallInst *CI = getCallIfRegularCall(U, &RFI);
365       if (!CI || CI == ReplVal || &F != &Caller)
366         return false;
367       assert(CI->getCaller() == &F && "Unexpected call!");
368 
369       auto Remark = [&](OptimizationRemark OR) {
370         return OR << "OpenMP runtime call "
371                   << ore::NV("OpenMPOptRuntime", RFI.Name) << " deduplicated";
372       };
373       emitRemark<OptimizationRemark>(CI, "OpenMPRuntimeDeduplicated", Remark);
374 
375       CGUpdater.removeCallSite(*CI);
376       CI->replaceAllUsesWith(ReplVal);
377       CI->eraseFromParent();
378       ++NumOpenMPRuntimeCallsDeduplicated;
379       Changed = true;
380       return true;
381     };
382     RFI.foreachUse(ReplaceAndDeleteCB);
383 
384     return Changed;
385   }
386 
387   /// Collect arguments that represent the global thread id in \p GTIdArgs.
388   void collectGlobalThreadIdArguments(SmallSetVector<Value *, 16> &GTIdArgs) {
389     // TODO: Below we basically perform a fixpoint iteration with a pessimistic
390     //       initialization. We could define an AbstractAttribute instead and
391     //       run the Attributor here once it can be run as an SCC pass.
392 
393     // Helper to check the argument \p ArgNo at all call sites of \p F for
394     // a GTId.
395     auto CallArgOpIsGTId = [&](Function &F, unsigned ArgNo, CallInst &RefCI) {
396       if (!F.hasLocalLinkage())
397         return false;
398       for (Use &U : F.uses()) {
399         if (CallInst *CI = getCallIfRegularCall(U)) {
400           Value *ArgOp = CI->getArgOperand(ArgNo);
401           if (CI == &RefCI || GTIdArgs.count(ArgOp) ||
402               getCallIfRegularCall(*ArgOp,
403                                    &RFIs[OMPRTL___kmpc_global_thread_num]))
404             continue;
405         }
406         return false;
407       }
408       return true;
409     };
410 
411     // Helper to identify uses of a GTId as GTId arguments.
412     auto AddUserArgs = [&](Value &GTId) {
413       for (Use &U : GTId.uses())
414         if (CallInst *CI = dyn_cast<CallInst>(U.getUser()))
415           if (CI->isArgOperand(&U))
416             if (Function *Callee = CI->getCalledFunction())
417               if (CallArgOpIsGTId(*Callee, U.getOperandNo(), *CI))
418                 GTIdArgs.insert(Callee->getArg(U.getOperandNo()));
419     };
420 
421     // The argument users of __kmpc_global_thread_num calls are GTIds.
422     RuntimeFunctionInfo &GlobThreadNumRFI =
423         RFIs[OMPRTL___kmpc_global_thread_num];
424     GlobThreadNumRFI.foreachUse([&](Use &U, Function &F) {
425       if (CallInst *CI = getCallIfRegularCall(U, &GlobThreadNumRFI))
426         AddUserArgs(*CI);
427       return false;
428     });
429 
430     // Transitively search for more arguments by looking at the users of the
431     // ones we know already. During the search the GTIdArgs vector is extended
432     // so we cannot cache the size nor can we use a range based for.
433     for (unsigned u = 0; u < GTIdArgs.size(); ++u)
434       AddUserArgs(*GTIdArgs[u]);
435   }
436 
437   /// Return the call if \p U is a callee use in a regular call. If \p RFI is
438   /// given it has to be the callee or a nullptr is returned.
439   CallInst *getCallIfRegularCall(Use &U, RuntimeFunctionInfo *RFI = nullptr) {
440     CallInst *CI = dyn_cast<CallInst>(U.getUser());
441     if (CI && CI->isCallee(&U) && !CI->hasOperandBundles() &&
442         (!RFI || CI->getCalledFunction() == RFI->Declaration))
443       return CI;
444     return nullptr;
445   }
446 
447   /// Return the call if \p V is a regular call. If \p RFI is given it has to be
448   /// the callee or a nullptr is returned.
449   CallInst *getCallIfRegularCall(Value &V, RuntimeFunctionInfo *RFI = nullptr) {
450     CallInst *CI = dyn_cast<CallInst>(&V);
451     if (CI && !CI->hasOperandBundles() &&
452         (!RFI || CI->getCalledFunction() == RFI->Declaration))
453       return CI;
454     return nullptr;
455   }
456 
457   /// Returns true if the function declaration \p F matches the runtime
458   /// function types, that is, return type \p RTFRetType, and argument types
459   /// \p RTFArgTypes.
460   static bool declMatchesRTFTypes(Function *F, Type *RTFRetType,
461                                   SmallVector<Type *, 8> &RTFArgTypes) {
462     // TODO: We should output information to the user (under debug output
463     //       and via remarks).
464 
465     if (!F)
466       return false;
467     if (F->getReturnType() != RTFRetType)
468       return false;
469     if (F->arg_size() != RTFArgTypes.size())
470       return false;
471 
472     auto RTFTyIt = RTFArgTypes.begin();
473     for (Argument &Arg : F->args()) {
474       if (Arg.getType() != *RTFTyIt)
475         return false;
476 
477       ++RTFTyIt;
478     }
479 
480     return true;
481   }
482 
483   /// Helper to initialize all runtime function information for those defined in
484   /// OpenMPKinds.def.
485   void initializeRuntimeFunctions() {
486     // Helper to collect all uses of the decleration in the UsesMap.
487     auto CollectUses = [&](RuntimeFunctionInfo &RFI) {
488       unsigned NumUses = 0;
489       if (!RFI.Declaration)
490         return NumUses;
491       OMPBuilder.addAttributes(RFI.Kind, *RFI.Declaration);
492 
493       NumOpenMPRuntimeFunctionsIdentified += 1;
494       NumOpenMPRuntimeFunctionUsesIdentified += RFI.Declaration->getNumUses();
495 
496       // TODO: We directly convert uses into proper calls and unknown uses.
497       for (Use &U : RFI.Declaration->uses()) {
498         if (Instruction *UserI = dyn_cast<Instruction>(U.getUser())) {
499           if (ModuleSlice.count(UserI->getFunction())) {
500             RFI.getOrCreateUseVector(UserI->getFunction()).push_back(&U);
501             ++NumUses;
502           }
503         } else {
504           RFI.getOrCreateUseVector(nullptr).push_back(&U);
505           ++NumUses;
506         }
507       }
508       return NumUses;
509     };
510 
511 #define OMP_RTL(_Enum, _Name, _IsVarArg, _ReturnType, ...)                     \
512   {                                                                            \
513     SmallVector<Type *, 8> ArgsTypes({__VA_ARGS__});                           \
514     Function *F = M.getFunction(_Name);                                        \
515     if (declMatchesRTFTypes(F, _ReturnType, ArgsTypes)) {                      \
516       auto &RFI = RFIs[_Enum];                                                 \
517       RFI.Kind = _Enum;                                                        \
518       RFI.Name = _Name;                                                        \
519       RFI.IsVarArg = _IsVarArg;                                                \
520       RFI.ReturnType = _ReturnType;                                            \
521       RFI.ArgumentTypes = std::move(ArgsTypes);                                \
522       RFI.Declaration = F;                                                     \
523       unsigned NumUses = CollectUses(RFI);                                     \
524       (void)NumUses;                                                           \
525       LLVM_DEBUG({                                                             \
526         dbgs() << TAG << RFI.Name << (RFI.Declaration ? "" : " not")           \
527                << " found\n";                                                  \
528         if (RFI.Declaration)                                                   \
529           dbgs() << TAG << "-> got " << NumUses << " uses in "                 \
530                  << RFI.getNumFunctionsWithUses()                              \
531                  << " different functions.\n";                                 \
532       });                                                                      \
533     }                                                                          \
534   }
535 #include "llvm/Frontend/OpenMP/OMPKinds.def"
536 
537     // TODO: We should attach the attributes defined in OMPKinds.def.
538   }
539 
540   /// Emit a remark generically
541   ///
542   /// This template function can be used to generically emit a remark. The
543   /// RemarkKind should be one of the following:
544   ///   - OptimizationRemark to indicate a successful optimization attempt
545   ///   - OptimizationRemarkMissed to report a failed optimization attempt
546   ///   - OptimizationRemarkAnalysis to provide additional information about an
547   ///     optimization attempt
548   ///
549   /// The remark is built using a callback function provided by the caller that
550   /// takes a RemarkKind as input and returns a RemarkKind.
551   template <typename RemarkKind,
552             typename RemarkCallBack = function_ref<RemarkKind(RemarkKind &&)>>
553   void emitRemark(Instruction *Inst, StringRef RemarkName,
554                   RemarkCallBack &&RemarkCB) {
555     Function *F = Inst->getParent()->getParent();
556     auto &ORE = OREGetter(F);
557 
558     ORE.emit([&]() {
559       return RemarkCB(RemarkKind(DEBUG_TYPE, RemarkName, Inst));
560     });
561   }
562 
563   /// The underyling module.
564   Module &M;
565 
566   /// The SCC we are operating on.
567   SmallVectorImpl<Function *> &SCC;
568 
569   /// The slice of the module we are allowed to look at.
570   SmallPtrSetImpl<Function *> &ModuleSlice;
571 
572   /// An OpenMP-IR-Builder instance
573   OpenMPIRBuilder OMPBuilder;
574 
575   /// Callback to update the call graph, the first argument is a removed call,
576   /// the second an optional replacement call.
577   CallGraphUpdater &CGUpdater;
578 
579   /// Callback to get an OptimizationRemarkEmitter from a Function *
580   OptimizationRemarkGetter OREGetter;
581 
582   /// Map from runtime function kind to the runtime function description.
583   EnumeratedArray<RuntimeFunctionInfo, RuntimeFunction,
584                   RuntimeFunction::OMPRTL___last>
585       RFIs;
586 };
587 } // namespace
588 
589 PreservedAnalyses OpenMPOptPass::run(LazyCallGraph::SCC &C,
590                                      CGSCCAnalysisManager &AM,
591                                      LazyCallGraph &CG, CGSCCUpdateResult &UR) {
592   if (!containsOpenMP(*C.begin()->getFunction().getParent(), OMPInModule))
593     return PreservedAnalyses::all();
594 
595   if (DisableOpenMPOptimizations)
596     return PreservedAnalyses::all();
597 
598   SmallPtrSet<Function *, 16> ModuleSlice;
599   SmallVector<Function *, 16> SCC;
600   for (LazyCallGraph::Node &N : C) {
601     SCC.push_back(&N.getFunction());
602     ModuleSlice.insert(SCC.back());
603   }
604 
605   if (SCC.empty())
606     return PreservedAnalyses::all();
607 
608   auto OREGetter = [&C, &CG, &AM](Function *F) -> OptimizationRemarkEmitter & {
609     FunctionAnalysisManager &FAM =
610         AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
611     return FAM.getResult<OptimizationRemarkEmitterAnalysis>(*F);
612   };
613 
614   CallGraphUpdater CGUpdater;
615   CGUpdater.initialize(CG, C, AM, UR);
616   // TODO: Compute the module slice we are allowed to look at.
617   OpenMPOpt OMPOpt(SCC, ModuleSlice, CGUpdater, OREGetter);
618   bool Changed = OMPOpt.run();
619   (void)Changed;
620   return PreservedAnalyses::all();
621 }
622 
623 namespace {
624 
625 struct OpenMPOptLegacyPass : public CallGraphSCCPass {
626   CallGraphUpdater CGUpdater;
627   OpenMPInModule OMPInModule;
628   static char ID;
629 
630   OpenMPOptLegacyPass() : CallGraphSCCPass(ID) {
631     initializeOpenMPOptLegacyPassPass(*PassRegistry::getPassRegistry());
632   }
633 
634   void getAnalysisUsage(AnalysisUsage &AU) const override {
635     CallGraphSCCPass::getAnalysisUsage(AU);
636   }
637 
638   bool doInitialization(CallGraph &CG) override {
639     // Disable the pass if there is no OpenMP (runtime call) in the module.
640     containsOpenMP(CG.getModule(), OMPInModule);
641     return false;
642   }
643 
644   bool runOnSCC(CallGraphSCC &CGSCC) override {
645     if (!containsOpenMP(CGSCC.getCallGraph().getModule(), OMPInModule))
646       return false;
647     if (DisableOpenMPOptimizations || skipSCC(CGSCC))
648       return false;
649 
650     SmallPtrSet<Function *, 16> ModuleSlice;
651     SmallVector<Function *, 16> SCC;
652     for (CallGraphNode *CGN : CGSCC)
653       if (Function *Fn = CGN->getFunction())
654         if (!Fn->isDeclaration()) {
655           SCC.push_back(Fn);
656           ModuleSlice.insert(Fn);
657         }
658 
659     if (SCC.empty())
660       return false;
661 
662     CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
663     CGUpdater.initialize(CG, CGSCC);
664 
665     // Maintain a map of functions to avoid rebuilding the ORE
666     DenseMap<Function *, std::unique_ptr<OptimizationRemarkEmitter>> OREMap;
667     auto OREGetter = [&OREMap](Function *F) -> OptimizationRemarkEmitter & {
668       std::unique_ptr<OptimizationRemarkEmitter> &ORE = OREMap[F];
669       if (!ORE)
670         ORE = std::make_unique<OptimizationRemarkEmitter>(F);
671       return *ORE;
672     };
673 
674     // TODO: Compute the module slice we are allowed to look at.
675     OpenMPOpt OMPOpt(SCC, ModuleSlice, CGUpdater, OREGetter);
676     return OMPOpt.run();
677   }
678 
679   bool doFinalization(CallGraph &CG) override { return CGUpdater.finalize(); }
680 };
681 
682 } // end anonymous namespace
683 
684 bool llvm::omp::containsOpenMP(Module &M, OpenMPInModule &OMPInModule) {
685   if (OMPInModule.isKnown())
686     return OMPInModule;
687 
688 #define OMP_RTL(_Enum, _Name, ...)                                             \
689   if (M.getFunction(_Name))                                                    \
690     return OMPInModule = true;
691 #include "llvm/Frontend/OpenMP/OMPKinds.def"
692   return OMPInModule = false;
693 }
694 
695 char OpenMPOptLegacyPass::ID = 0;
696 
697 INITIALIZE_PASS_BEGIN(OpenMPOptLegacyPass, "openmpopt",
698                       "OpenMP specific optimizations", false, false)
699 INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
700 INITIALIZE_PASS_END(OpenMPOptLegacyPass, "openmpopt",
701                     "OpenMP specific optimizations", false, false)
702 
703 Pass *llvm::createOpenMPOptLegacyPass() { return new OpenMPOptLegacyPass(); }
704