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