1 //===- IslAst.cpp - isl code generator interface --------------------------===//
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 // The isl code generator interface takes a Scop and generates an isl_ast. This
11 // ist_ast can either be returned directly or it can be pretty printed to
12 // stdout.
13 //
14 // A typical isl_ast output looks like this:
15 //
16 // for (c2 = max(0, ceild(n + m, 2); c2 <= min(511, floord(5 * n, 3)); c2++) {
17 //   bb2(c2);
18 // }
19 //
20 // An in-depth discussion of our AST generation approach can be found in:
21 //
22 // Polyhedral AST generation is more than scanning polyhedra
23 // Tobias Grosser, Sven Verdoolaege, Albert Cohen
24 // ACM Transations on Programming Languages and Systems (TOPLAS),
25 // 37(4), July 2015
26 // http://www.grosser.es/#pub-polyhedral-AST-generation
27 //
28 //===----------------------------------------------------------------------===//
29 
30 #include "polly/CodeGen/IslAst.h"
31 #include "polly/CodeGen/CodeGeneration.h"
32 #include "polly/DependenceInfo.h"
33 #include "polly/LinkAllPasses.h"
34 #include "polly/Options.h"
35 #include "polly/ScopInfo.h"
36 #include "polly/Support/GICHelper.h"
37 #include "llvm/Analysis/RegionInfo.h"
38 #include "llvm/Support/Debug.h"
39 #include "isl/aff.h"
40 #include "isl/ast_build.h"
41 #include "isl/list.h"
42 #include "isl/map.h"
43 #include "isl/set.h"
44 #include "isl/union_map.h"
45 
46 #define DEBUG_TYPE "polly-ast"
47 
48 using namespace llvm;
49 using namespace polly;
50 
51 using IslAstUserPayload = IslAstInfo::IslAstUserPayload;
52 
53 static cl::opt<bool>
54     PollyParallel("polly-parallel",
55                   cl::desc("Generate thread parallel code (isl codegen only)"),
56                   cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
57 
58 static cl::opt<bool> PollyParallelForce(
59     "polly-parallel-force",
60     cl::desc(
61         "Force generation of thread parallel code ignoring any cost model"),
62     cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
63 
64 static cl::opt<bool> UseContext("polly-ast-use-context",
65                                 cl::desc("Use context"), cl::Hidden,
66                                 cl::init(false), cl::ZeroOrMore,
67                                 cl::cat(PollyCategory));
68 
69 static cl::opt<bool> DetectParallel("polly-ast-detect-parallel",
70                                     cl::desc("Detect parallelism"), cl::Hidden,
71                                     cl::init(false), cl::ZeroOrMore,
72                                     cl::cat(PollyCategory));
73 
74 /// @brief Free an IslAstUserPayload object pointed to by @p Ptr
75 static void freeIslAstUserPayload(void *Ptr) {
76   delete ((IslAstInfo::IslAstUserPayload *)Ptr);
77 }
78 
79 IslAstInfo::IslAstUserPayload::~IslAstUserPayload() {
80   isl_ast_build_free(Build);
81   isl_pw_aff_free(MinimalDependenceDistance);
82 }
83 
84 /// @brief Temporary information used when building the ast.
85 struct AstBuildUserInfo {
86   /// @brief Construct and initialize the helper struct for AST creation.
87   AstBuildUserInfo()
88       : Deps(nullptr), InParallelFor(false), LastForNodeId(nullptr) {}
89 
90   /// @brief The dependence information used for the parallelism check.
91   const Dependences *Deps;
92 
93   /// @brief Flag to indicate that we are inside a parallel for node.
94   bool InParallelFor;
95 
96   /// @brief The last iterator id created for the current SCoP.
97   isl_id *LastForNodeId;
98 };
99 
100 /// @brief Print a string @p str in a single line using @p Printer.
101 static isl_printer *printLine(__isl_take isl_printer *Printer,
102                               const std::string &str,
103                               __isl_keep isl_pw_aff *PWA = nullptr) {
104   Printer = isl_printer_start_line(Printer);
105   Printer = isl_printer_print_str(Printer, str.c_str());
106   if (PWA)
107     Printer = isl_printer_print_pw_aff(Printer, PWA);
108   return isl_printer_end_line(Printer);
109 }
110 
111 /// @brief Return all broken reductions as a string of clauses (OpenMP style).
112 static const std::string getBrokenReductionsStr(__isl_keep isl_ast_node *Node) {
113   IslAstInfo::MemoryAccessSet *BrokenReductions;
114   std::string str;
115 
116   BrokenReductions = IslAstInfo::getBrokenReductions(Node);
117   if (!BrokenReductions || BrokenReductions->empty())
118     return "";
119 
120   // Map each type of reduction to a comma separated list of the base addresses.
121   std::map<MemoryAccess::ReductionType, std::string> Clauses;
122   for (MemoryAccess *MA : *BrokenReductions)
123     if (MA->isWrite())
124       Clauses[MA->getReductionType()] +=
125           ", " + MA->getBaseAddr()->getName().str();
126 
127   // Now print the reductions sorted by type. Each type will cause a clause
128   // like:  reduction (+ : sum0, sum1, sum2)
129   for (const auto &ReductionClause : Clauses) {
130     str += " reduction (";
131     str += MemoryAccess::getReductionOperatorStr(ReductionClause.first);
132     // Remove the first two symbols (", ") to make the output look pretty.
133     str += " : " + ReductionClause.second.substr(2) + ")";
134   }
135 
136   return str;
137 }
138 
139 /// @brief Callback executed for each for node in the ast in order to print it.
140 static isl_printer *cbPrintFor(__isl_take isl_printer *Printer,
141                                __isl_take isl_ast_print_options *Options,
142                                __isl_keep isl_ast_node *Node, void *) {
143 
144   isl_pw_aff *DD = IslAstInfo::getMinimalDependenceDistance(Node);
145   const std::string BrokenReductionsStr = getBrokenReductionsStr(Node);
146   const std::string KnownParallelStr = "#pragma known-parallel";
147   const std::string DepDisPragmaStr = "#pragma minimal dependence distance: ";
148   const std::string SimdPragmaStr = "#pragma simd";
149   const std::string OmpPragmaStr = "#pragma omp parallel for";
150 
151   if (DD)
152     Printer = printLine(Printer, DepDisPragmaStr, DD);
153 
154   if (IslAstInfo::isInnermostParallel(Node))
155     Printer = printLine(Printer, SimdPragmaStr + BrokenReductionsStr);
156 
157   if (IslAstInfo::isExecutedInParallel(Node))
158     Printer = printLine(Printer, OmpPragmaStr);
159   else if (IslAstInfo::isOutermostParallel(Node))
160     Printer = printLine(Printer, KnownParallelStr + BrokenReductionsStr);
161 
162   isl_pw_aff_free(DD);
163   return isl_ast_node_for_print(Node, Printer, Options);
164 }
165 
166 /// @brief Check if the current scheduling dimension is parallel
167 ///
168 /// In case the dimension is parallel we also check if any reduction
169 /// dependences is broken when we exploit this parallelism. If so,
170 /// @p IsReductionParallel will be set to true. The reduction dependences we use
171 /// to check are actually the union of the transitive closure of the initial
172 /// reduction dependences together with their reveresal. Even though these
173 /// dependences connect all iterations with each other (thus they are cyclic)
174 /// we can perform the parallelism check as we are only interested in a zero
175 /// (or non-zero) dependence distance on the dimension in question.
176 static bool astScheduleDimIsParallel(__isl_keep isl_ast_build *Build,
177                                      const Dependences *D,
178                                      IslAstUserPayload *NodeInfo) {
179   if (!D->hasValidDependences())
180     return false;
181 
182   isl_union_map *Schedule = isl_ast_build_get_schedule(Build);
183   isl_union_map *Deps = D->getDependences(
184       Dependences::TYPE_RAW | Dependences::TYPE_WAW | Dependences::TYPE_WAR);
185 
186   if (!D->isParallel(Schedule, Deps, &NodeInfo->MinimalDependenceDistance) &&
187       !isl_union_map_free(Schedule))
188     return false;
189 
190   isl_union_map *RedDeps = D->getDependences(Dependences::TYPE_TC_RED);
191   if (!D->isParallel(Schedule, RedDeps))
192     NodeInfo->IsReductionParallel = true;
193 
194   if (!NodeInfo->IsReductionParallel && !isl_union_map_free(Schedule))
195     return true;
196 
197   // Annotate reduction parallel nodes with the memory accesses which caused the
198   // reduction dependences parallel execution of the node conflicts with.
199   for (const auto &MaRedPair : D->getReductionDependences()) {
200     if (!MaRedPair.second)
201       continue;
202     RedDeps = isl_union_map_from_map(isl_map_copy(MaRedPair.second));
203     if (!D->isParallel(Schedule, RedDeps))
204       NodeInfo->BrokenReductions.insert(MaRedPair.first);
205   }
206 
207   isl_union_map_free(Schedule);
208   return true;
209 }
210 
211 // This method is executed before the construction of a for node. It creates
212 // an isl_id that is used to annotate the subsequently generated ast for nodes.
213 //
214 // In this function we also run the following analyses:
215 //
216 // - Detection of openmp parallel loops
217 //
218 static __isl_give isl_id *astBuildBeforeFor(__isl_keep isl_ast_build *Build,
219                                             void *User) {
220   AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
221   IslAstUserPayload *Payload = new IslAstUserPayload();
222   isl_id *Id = isl_id_alloc(isl_ast_build_get_ctx(Build), "", Payload);
223   Id = isl_id_set_free_user(Id, freeIslAstUserPayload);
224   BuildInfo->LastForNodeId = Id;
225 
226   // Test for parallelism only if we are not already inside a parallel loop
227   if (!BuildInfo->InParallelFor)
228     BuildInfo->InParallelFor = Payload->IsOutermostParallel =
229         astScheduleDimIsParallel(Build, BuildInfo->Deps, Payload);
230 
231   return Id;
232 }
233 
234 // This method is executed after the construction of a for node.
235 //
236 // It performs the following actions:
237 //
238 // - Reset the 'InParallelFor' flag, as soon as we leave a for node,
239 //   that is marked as openmp parallel.
240 //
241 static __isl_give isl_ast_node *
242 astBuildAfterFor(__isl_take isl_ast_node *Node, __isl_keep isl_ast_build *Build,
243                  void *User) {
244   isl_id *Id = isl_ast_node_get_annotation(Node);
245   assert(Id && "Post order visit assumes annotated for nodes");
246   IslAstUserPayload *Payload = (IslAstUserPayload *)isl_id_get_user(Id);
247   assert(Payload && "Post order visit assumes annotated for nodes");
248 
249   AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
250   assert(!Payload->Build && "Build environment already set");
251   Payload->Build = isl_ast_build_copy(Build);
252   Payload->IsInnermost = (Id == BuildInfo->LastForNodeId);
253 
254   // Innermost loops that are surrounded by parallel loops have not yet been
255   // tested for parallelism. Test them here to ensure we check all innermost
256   // loops for parallelism.
257   if (Payload->IsInnermost && BuildInfo->InParallelFor) {
258     if (Payload->IsOutermostParallel) {
259       Payload->IsInnermostParallel = true;
260     } else {
261       if (PollyVectorizerChoice == VECTORIZER_NONE)
262         Payload->IsInnermostParallel =
263             astScheduleDimIsParallel(Build, BuildInfo->Deps, Payload);
264     }
265   }
266   if (Payload->IsOutermostParallel)
267     BuildInfo->InParallelFor = false;
268 
269   isl_id_free(Id);
270   return Node;
271 }
272 
273 static isl_stat astBuildBeforeMark(__isl_keep isl_id *MarkId,
274                                    __isl_keep isl_ast_build *Build,
275                                    void *User) {
276   if (!MarkId)
277     return isl_stat_error;
278 
279   AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
280   if (!strcmp(isl_id_get_name(MarkId), "SIMD"))
281     BuildInfo->InParallelFor = true;
282 
283   return isl_stat_ok;
284 }
285 
286 static __isl_give isl_ast_node *
287 astBuildAfterMark(__isl_take isl_ast_node *Node,
288                   __isl_keep isl_ast_build *Build, void *User) {
289   assert(isl_ast_node_get_type(Node) == isl_ast_node_mark);
290   AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User;
291   auto *Id = isl_ast_node_mark_get_id(Node);
292   if (!strcmp(isl_id_get_name(Id), "SIMD"))
293     BuildInfo->InParallelFor = false;
294   isl_id_free(Id);
295   return Node;
296 }
297 
298 static __isl_give isl_ast_node *AtEachDomain(__isl_take isl_ast_node *Node,
299                                              __isl_keep isl_ast_build *Build,
300                                              void *User) {
301   assert(!isl_ast_node_get_annotation(Node) && "Node already annotated");
302 
303   IslAstUserPayload *Payload = new IslAstUserPayload();
304   isl_id *Id = isl_id_alloc(isl_ast_build_get_ctx(Build), "", Payload);
305   Id = isl_id_set_free_user(Id, freeIslAstUserPayload);
306 
307   Payload->Build = isl_ast_build_copy(Build);
308 
309   return isl_ast_node_set_annotation(Node, Id);
310 }
311 
312 // Build alias check condition given a pair of minimal/maximal access.
313 static __isl_give isl_ast_expr *
314 buildCondition(__isl_keep isl_ast_build *Build, const Scop::MinMaxAccessTy *It0,
315                const Scop::MinMaxAccessTy *It1) {
316   isl_ast_expr *NonAliasGroup, *MinExpr, *MaxExpr;
317   MinExpr = isl_ast_expr_address_of(isl_ast_build_access_from_pw_multi_aff(
318       Build, isl_pw_multi_aff_copy(It0->first)));
319   MaxExpr = isl_ast_expr_address_of(isl_ast_build_access_from_pw_multi_aff(
320       Build, isl_pw_multi_aff_copy(It1->second)));
321   NonAliasGroup = isl_ast_expr_le(MaxExpr, MinExpr);
322   MinExpr = isl_ast_expr_address_of(isl_ast_build_access_from_pw_multi_aff(
323       Build, isl_pw_multi_aff_copy(It1->first)));
324   MaxExpr = isl_ast_expr_address_of(isl_ast_build_access_from_pw_multi_aff(
325       Build, isl_pw_multi_aff_copy(It0->second)));
326   NonAliasGroup =
327       isl_ast_expr_or(NonAliasGroup, isl_ast_expr_le(MaxExpr, MinExpr));
328 
329   return NonAliasGroup;
330 }
331 
332 __isl_give isl_ast_expr *
333 IslAst::buildRunCondition(Scop *S, __isl_keep isl_ast_build *Build) {
334   isl_ast_expr *RunCondition;
335 
336   // The conditions that need to be checked at run-time for this scop are
337   // available as an isl_set in the runtime check context from which we can
338   // directly derive a run-time condition.
339   auto *PosCond = isl_ast_build_expr_from_set(Build, S->getAssumedContext());
340   if (S->hasTrivialInvalidContext()) {
341     RunCondition = PosCond;
342   } else {
343     auto *ZeroV = isl_val_zero(isl_ast_build_get_ctx(Build));
344     auto *NegCond = isl_ast_build_expr_from_set(Build, S->getInvalidContext());
345     auto *NotNegCond = isl_ast_expr_eq(isl_ast_expr_from_val(ZeroV), NegCond);
346     RunCondition = isl_ast_expr_and(PosCond, NotNegCond);
347   }
348 
349   // Create the alias checks from the minimal/maximal accesses in each alias
350   // group which consists of read only and non read only (read write) accesses.
351   // This operation is by construction quadratic in the read-write pointers and
352   // linear int the read only pointers in each alias group.
353   for (const Scop::MinMaxVectorPairTy &MinMaxAccessPair : S->getAliasGroups()) {
354     auto &MinMaxReadWrite = MinMaxAccessPair.first;
355     auto &MinMaxReadOnly = MinMaxAccessPair.second;
356     auto RWAccEnd = MinMaxReadWrite.end();
357 
358     for (auto RWAccIt0 = MinMaxReadWrite.begin(); RWAccIt0 != RWAccEnd;
359          ++RWAccIt0) {
360       for (auto RWAccIt1 = RWAccIt0 + 1; RWAccIt1 != RWAccEnd; ++RWAccIt1)
361         RunCondition = isl_ast_expr_and(
362             RunCondition, buildCondition(Build, RWAccIt0, RWAccIt1));
363       for (const Scop::MinMaxAccessTy &ROAccIt : MinMaxReadOnly)
364         RunCondition = isl_ast_expr_and(
365             RunCondition, buildCondition(Build, RWAccIt0, &ROAccIt));
366     }
367   }
368 
369   return RunCondition;
370 }
371 
372 /// @brief Simple cost analysis for a given SCoP
373 ///
374 /// TODO: Improve this analysis and extract it to make it usable in other
375 ///       places too.
376 ///       In order to improve the cost model we could either keep track of
377 ///       performed optimizations (e.g., tiling) or compute properties on the
378 ///       original as well as optimized SCoP (e.g., #stride-one-accesses).
379 static bool benefitsFromPolly(Scop *Scop, bool PerformParallelTest) {
380 
381   if (PollyProcessUnprofitable)
382     return true;
383 
384   // Check if nothing interesting happened.
385   if (!PerformParallelTest && !Scop->isOptimized() &&
386       Scop->getAliasGroups().empty())
387     return false;
388 
389   // The default assumption is that Polly improves the code.
390   return true;
391 }
392 
393 IslAst::IslAst(Scop *Scop)
394     : S(Scop), Root(nullptr), RunCondition(nullptr),
395       Ctx(Scop->getSharedIslCtx()) {}
396 
397 void IslAst::init(const Dependences &D) {
398   bool PerformParallelTest = PollyParallel || DetectParallel ||
399                              PollyVectorizerChoice != VECTORIZER_NONE;
400 
401   // Skip AST and code generation if there was no benefit achieved.
402   if (!benefitsFromPolly(S, PerformParallelTest))
403     return;
404 
405   isl_ctx *Ctx = S->getIslCtx();
406   isl_options_set_ast_build_atomic_upper_bound(Ctx, true);
407   isl_ast_build *Build;
408   AstBuildUserInfo BuildInfo;
409 
410   if (UseContext)
411     Build = isl_ast_build_from_context(S->getContext());
412   else
413     Build = isl_ast_build_from_context(isl_set_universe(S->getParamSpace()));
414 
415   Build = isl_ast_build_set_at_each_domain(Build, AtEachDomain, nullptr);
416 
417   if (PerformParallelTest) {
418     BuildInfo.Deps = &D;
419     BuildInfo.InParallelFor = 0;
420 
421     Build = isl_ast_build_set_before_each_for(Build, &astBuildBeforeFor,
422                                               &BuildInfo);
423     Build =
424         isl_ast_build_set_after_each_for(Build, &astBuildAfterFor, &BuildInfo);
425 
426     Build = isl_ast_build_set_before_each_mark(Build, &astBuildBeforeMark,
427                                                &BuildInfo);
428 
429     Build = isl_ast_build_set_after_each_mark(Build, &astBuildAfterMark,
430                                               &BuildInfo);
431   }
432 
433   RunCondition = buildRunCondition(S, Build);
434 
435   Root = isl_ast_build_node_from_schedule(Build, S->getScheduleTree());
436 
437   isl_ast_build_free(Build);
438 }
439 
440 IslAst *IslAst::create(Scop *Scop, const Dependences &D) {
441   auto Ast = new IslAst(Scop);
442   Ast->init(D);
443   return Ast;
444 }
445 
446 IslAst::~IslAst() {
447   isl_ast_node_free(Root);
448   isl_ast_expr_free(RunCondition);
449 }
450 
451 __isl_give isl_ast_node *IslAst::getAst() { return isl_ast_node_copy(Root); }
452 __isl_give isl_ast_expr *IslAst::getRunCondition() {
453   return isl_ast_expr_copy(RunCondition);
454 }
455 
456 void IslAstInfo::releaseMemory() {
457   if (Ast) {
458     delete Ast;
459     Ast = nullptr;
460   }
461 }
462 
463 bool IslAstInfo::runOnScop(Scop &Scop) {
464   if (Ast)
465     delete Ast;
466 
467   S = &Scop;
468 
469   const Dependences &D =
470       getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement);
471 
472   Ast = IslAst::create(&Scop, D);
473 
474   DEBUG(printScop(dbgs(), Scop));
475   return false;
476 }
477 
478 __isl_give isl_ast_node *IslAstInfo::getAst() const { return Ast->getAst(); }
479 __isl_give isl_ast_expr *IslAstInfo::getRunCondition() const {
480   return Ast->getRunCondition();
481 }
482 
483 IslAstUserPayload *IslAstInfo::getNodePayload(__isl_keep isl_ast_node *Node) {
484   isl_id *Id = isl_ast_node_get_annotation(Node);
485   if (!Id)
486     return nullptr;
487   IslAstUserPayload *Payload = (IslAstUserPayload *)isl_id_get_user(Id);
488   isl_id_free(Id);
489   return Payload;
490 }
491 
492 bool IslAstInfo::isInnermost(__isl_keep isl_ast_node *Node) {
493   IslAstUserPayload *Payload = getNodePayload(Node);
494   return Payload && Payload->IsInnermost;
495 }
496 
497 bool IslAstInfo::isParallel(__isl_keep isl_ast_node *Node) {
498   return IslAstInfo::isInnermostParallel(Node) ||
499          IslAstInfo::isOutermostParallel(Node);
500 }
501 
502 bool IslAstInfo::isInnermostParallel(__isl_keep isl_ast_node *Node) {
503   IslAstUserPayload *Payload = getNodePayload(Node);
504   return Payload && Payload->IsInnermostParallel;
505 }
506 
507 bool IslAstInfo::isOutermostParallel(__isl_keep isl_ast_node *Node) {
508   IslAstUserPayload *Payload = getNodePayload(Node);
509   return Payload && Payload->IsOutermostParallel;
510 }
511 
512 bool IslAstInfo::isReductionParallel(__isl_keep isl_ast_node *Node) {
513   IslAstUserPayload *Payload = getNodePayload(Node);
514   return Payload && Payload->IsReductionParallel;
515 }
516 
517 bool IslAstInfo::isExecutedInParallel(__isl_keep isl_ast_node *Node) {
518 
519   if (!PollyParallel)
520     return false;
521 
522   // Do not parallelize innermost loops.
523   //
524   // Parallelizing innermost loops is often not profitable, especially if
525   // they have a low number of iterations.
526   //
527   // TODO: Decide this based on the number of loop iterations that will be
528   //       executed. This can possibly require run-time checks, which again
529   //       raises the question of both run-time check overhead and code size
530   //       costs.
531   if (!PollyParallelForce && isInnermost(Node))
532     return false;
533 
534   return isOutermostParallel(Node) && !isReductionParallel(Node);
535 }
536 
537 isl_union_map *IslAstInfo::getSchedule(__isl_keep isl_ast_node *Node) {
538   IslAstUserPayload *Payload = getNodePayload(Node);
539   return Payload ? isl_ast_build_get_schedule(Payload->Build) : nullptr;
540 }
541 
542 isl_pw_aff *
543 IslAstInfo::getMinimalDependenceDistance(__isl_keep isl_ast_node *Node) {
544   IslAstUserPayload *Payload = getNodePayload(Node);
545   return Payload ? isl_pw_aff_copy(Payload->MinimalDependenceDistance)
546                  : nullptr;
547 }
548 
549 IslAstInfo::MemoryAccessSet *
550 IslAstInfo::getBrokenReductions(__isl_keep isl_ast_node *Node) {
551   IslAstUserPayload *Payload = getNodePayload(Node);
552   return Payload ? &Payload->BrokenReductions : nullptr;
553 }
554 
555 isl_ast_build *IslAstInfo::getBuild(__isl_keep isl_ast_node *Node) {
556   IslAstUserPayload *Payload = getNodePayload(Node);
557   return Payload ? Payload->Build : nullptr;
558 }
559 
560 void IslAstInfo::printScop(raw_ostream &OS, Scop &S) const {
561   isl_ast_print_options *Options;
562   isl_ast_node *RootNode = getAst();
563   Function *F = S.getRegion().getEntry()->getParent();
564 
565   OS << ":: isl ast :: " << F->getName() << " :: " << S.getRegion().getNameStr()
566      << "\n";
567 
568   if (!RootNode) {
569     OS << ":: isl ast generation and code generation was skipped!\n\n";
570     OS << ":: This is either because no useful optimizations could be applied "
571           "(use -polly-process-unprofitable to enforce code generation) or "
572           "because earlier passes such as dependence analysis timed out (use "
573           "-polly-dependences-computeout=0 to set dependence analysis timeout "
574           "to infinity)\n\n";
575     return;
576   }
577 
578   isl_ast_expr *RunCondition = getRunCondition();
579   char *RtCStr, *AstStr;
580 
581   Options = isl_ast_print_options_alloc(S.getIslCtx());
582   Options = isl_ast_print_options_set_print_for(Options, cbPrintFor, nullptr);
583 
584   isl_printer *P = isl_printer_to_str(S.getIslCtx());
585   P = isl_printer_print_ast_expr(P, RunCondition);
586   RtCStr = isl_printer_get_str(P);
587   P = isl_printer_flush(P);
588   P = isl_printer_indent(P, 4);
589   P = isl_printer_set_output_format(P, ISL_FORMAT_C);
590   P = isl_ast_node_print(RootNode, P, Options);
591   AstStr = isl_printer_get_str(P);
592 
593   isl_union_map *Schedule =
594       isl_union_map_intersect_domain(S.getSchedule(), S.getDomains());
595 
596   DEBUG({
597     dbgs() << S.getContextStr() << "\n";
598     dbgs() << stringFromIslObj(Schedule);
599   });
600   OS << "\nif (" << RtCStr << ")\n\n";
601   OS << AstStr << "\n";
602   OS << "else\n";
603   OS << "    {  /* original code */ }\n\n";
604 
605   free(RtCStr);
606   free(AstStr);
607 
608   isl_ast_expr_free(RunCondition);
609   isl_union_map_free(Schedule);
610   isl_ast_node_free(RootNode);
611   isl_printer_free(P);
612 }
613 
614 void IslAstInfo::getAnalysisUsage(AnalysisUsage &AU) const {
615   // Get the Common analysis usage of ScopPasses.
616   ScopPass::getAnalysisUsage(AU);
617   AU.addRequired<ScopInfo>();
618   AU.addRequired<DependenceInfo>();
619 }
620 
621 char IslAstInfo::ID = 0;
622 
623 Pass *polly::createIslAstInfoPass() { return new IslAstInfo(); }
624 
625 INITIALIZE_PASS_BEGIN(IslAstInfo, "polly-ast",
626                       "Polly - Generate an AST of the SCoP (isl)", false,
627                       false);
628 INITIALIZE_PASS_DEPENDENCY(ScopInfo);
629 INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
630 INITIALIZE_PASS_END(IslAstInfo, "polly-ast",
631                     "Polly - Generate an AST from the SCoP (isl)", false, false)
632