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 void IslAst::buildRunCondition(__isl_keep isl_ast_build *Build) {
333   // The conditions that need to be checked at run-time for this scop are
334   // available as an isl_set in the runtime check context from which we can
335   // directly derive a run-time condition.
336   RunCondition =
337       isl_ast_build_expr_from_set(Build, S->getRuntimeCheckContext());
338 
339   // Create the alias checks from the minimal/maximal accesses in each alias
340   // group which consists of read only and non read only (read write) accesses.
341   // This operation is by construction quadratic in the read-write pointers and
342   // linear int the read only pointers in each alias group.
343   for (const Scop::MinMaxVectorPairTy &MinMaxAccessPair : S->getAliasGroups()) {
344     auto &MinMaxReadWrite = MinMaxAccessPair.first;
345     auto &MinMaxReadOnly = MinMaxAccessPair.second;
346     auto RWAccEnd = MinMaxReadWrite.end();
347 
348     for (auto RWAccIt0 = MinMaxReadWrite.begin(); RWAccIt0 != RWAccEnd;
349          ++RWAccIt0) {
350       for (auto RWAccIt1 = RWAccIt0 + 1; RWAccIt1 != RWAccEnd; ++RWAccIt1)
351         RunCondition = isl_ast_expr_and(
352             RunCondition, buildCondition(Build, RWAccIt0, RWAccIt1));
353       for (const Scop::MinMaxAccessTy &ROAccIt : MinMaxReadOnly)
354         RunCondition = isl_ast_expr_and(
355             RunCondition, buildCondition(Build, RWAccIt0, &ROAccIt));
356     }
357   }
358 }
359 
360 /// @brief Simple cost analysis for a given SCoP
361 ///
362 /// TODO: Improve this analysis and extract it to make it usable in other
363 ///       places too.
364 ///       In order to improve the cost model we could either keep track of
365 ///       performed optimizations (e.g., tiling) or compute properties on the
366 ///       original as well as optimized SCoP (e.g., #stride-one-accesses).
367 static bool benefitsFromPolly(Scop *Scop, bool PerformParallelTest) {
368 
369   if (PollyProcessUnprofitable)
370     return true;
371 
372   // Check if nothing interesting happened.
373   if (!PerformParallelTest && !Scop->isOptimized() &&
374       Scop->getAliasGroups().empty())
375     return false;
376 
377   // The default assumption is that Polly improves the code.
378   return true;
379 }
380 
381 IslAst::IslAst(Scop *Scop)
382     : S(Scop), Root(nullptr), RunCondition(nullptr),
383       Ctx(Scop->getSharedIslCtx()) {}
384 
385 void IslAst::init(const Dependences &D) {
386   bool PerformParallelTest = PollyParallel || DetectParallel ||
387                              PollyVectorizerChoice != VECTORIZER_NONE;
388 
389   // Skip AST and code generation if there was no benefit achieved.
390   if (!benefitsFromPolly(S, PerformParallelTest))
391     return;
392 
393   isl_ctx *Ctx = S->getIslCtx();
394   isl_options_set_ast_build_atomic_upper_bound(Ctx, true);
395   isl_ast_build *Build;
396   AstBuildUserInfo BuildInfo;
397 
398   if (UseContext)
399     Build = isl_ast_build_from_context(S->getContext());
400   else
401     Build = isl_ast_build_from_context(isl_set_universe(S->getParamSpace()));
402 
403   Build = isl_ast_build_set_at_each_domain(Build, AtEachDomain, nullptr);
404 
405   if (PerformParallelTest) {
406     BuildInfo.Deps = &D;
407     BuildInfo.InParallelFor = 0;
408 
409     Build = isl_ast_build_set_before_each_for(Build, &astBuildBeforeFor,
410                                               &BuildInfo);
411     Build =
412         isl_ast_build_set_after_each_for(Build, &astBuildAfterFor, &BuildInfo);
413 
414     Build = isl_ast_build_set_before_each_mark(Build, &astBuildBeforeMark,
415                                                &BuildInfo);
416 
417     Build = isl_ast_build_set_after_each_mark(Build, &astBuildAfterMark,
418                                               &BuildInfo);
419   }
420 
421   buildRunCondition(Build);
422 
423   Root = isl_ast_build_node_from_schedule(Build, S->getScheduleTree());
424 
425   isl_ast_build_free(Build);
426 }
427 
428 IslAst *IslAst::create(Scop *Scop, const Dependences &D) {
429   auto Ast = new IslAst(Scop);
430   Ast->init(D);
431   return Ast;
432 }
433 
434 IslAst::~IslAst() {
435   isl_ast_node_free(Root);
436   isl_ast_expr_free(RunCondition);
437 }
438 
439 __isl_give isl_ast_node *IslAst::getAst() { return isl_ast_node_copy(Root); }
440 __isl_give isl_ast_expr *IslAst::getRunCondition() {
441   return isl_ast_expr_copy(RunCondition);
442 }
443 
444 void IslAstInfo::releaseMemory() {
445   if (Ast) {
446     delete Ast;
447     Ast = nullptr;
448   }
449 }
450 
451 bool IslAstInfo::runOnScop(Scop &Scop) {
452   if (Ast)
453     delete Ast;
454 
455   S = &Scop;
456 
457   const Dependences &D = getAnalysis<DependenceInfo>().getDependences();
458 
459   Ast = IslAst::create(&Scop, D);
460 
461   DEBUG(printScop(dbgs(), Scop));
462   return false;
463 }
464 
465 __isl_give isl_ast_node *IslAstInfo::getAst() const { return Ast->getAst(); }
466 __isl_give isl_ast_expr *IslAstInfo::getRunCondition() const {
467   return Ast->getRunCondition();
468 }
469 
470 IslAstUserPayload *IslAstInfo::getNodePayload(__isl_keep isl_ast_node *Node) {
471   isl_id *Id = isl_ast_node_get_annotation(Node);
472   if (!Id)
473     return nullptr;
474   IslAstUserPayload *Payload = (IslAstUserPayload *)isl_id_get_user(Id);
475   isl_id_free(Id);
476   return Payload;
477 }
478 
479 bool IslAstInfo::isInnermost(__isl_keep isl_ast_node *Node) {
480   IslAstUserPayload *Payload = getNodePayload(Node);
481   return Payload && Payload->IsInnermost;
482 }
483 
484 bool IslAstInfo::isParallel(__isl_keep isl_ast_node *Node) {
485   return IslAstInfo::isInnermostParallel(Node) ||
486          IslAstInfo::isOutermostParallel(Node);
487 }
488 
489 bool IslAstInfo::isInnermostParallel(__isl_keep isl_ast_node *Node) {
490   IslAstUserPayload *Payload = getNodePayload(Node);
491   return Payload && Payload->IsInnermostParallel;
492 }
493 
494 bool IslAstInfo::isOutermostParallel(__isl_keep isl_ast_node *Node) {
495   IslAstUserPayload *Payload = getNodePayload(Node);
496   return Payload && Payload->IsOutermostParallel;
497 }
498 
499 bool IslAstInfo::isReductionParallel(__isl_keep isl_ast_node *Node) {
500   IslAstUserPayload *Payload = getNodePayload(Node);
501   return Payload && Payload->IsReductionParallel;
502 }
503 
504 bool IslAstInfo::isExecutedInParallel(__isl_keep isl_ast_node *Node) {
505 
506   if (!PollyParallel)
507     return false;
508 
509   // Do not parallelize innermost loops.
510   //
511   // Parallelizing innermost loops is often not profitable, especially if
512   // they have a low number of iterations.
513   //
514   // TODO: Decide this based on the number of loop iterations that will be
515   //       executed. This can possibly require run-time checks, which again
516   //       raises the question of both run-time check overhead and code size
517   //       costs.
518   if (!PollyParallelForce && isInnermost(Node))
519     return false;
520 
521   return isOutermostParallel(Node) && !isReductionParallel(Node);
522 }
523 
524 isl_union_map *IslAstInfo::getSchedule(__isl_keep isl_ast_node *Node) {
525   IslAstUserPayload *Payload = getNodePayload(Node);
526   return Payload ? isl_ast_build_get_schedule(Payload->Build) : nullptr;
527 }
528 
529 isl_pw_aff *
530 IslAstInfo::getMinimalDependenceDistance(__isl_keep isl_ast_node *Node) {
531   IslAstUserPayload *Payload = getNodePayload(Node);
532   return Payload ? isl_pw_aff_copy(Payload->MinimalDependenceDistance)
533                  : nullptr;
534 }
535 
536 IslAstInfo::MemoryAccessSet *
537 IslAstInfo::getBrokenReductions(__isl_keep isl_ast_node *Node) {
538   IslAstUserPayload *Payload = getNodePayload(Node);
539   return Payload ? &Payload->BrokenReductions : nullptr;
540 }
541 
542 isl_ast_build *IslAstInfo::getBuild(__isl_keep isl_ast_node *Node) {
543   IslAstUserPayload *Payload = getNodePayload(Node);
544   return Payload ? Payload->Build : nullptr;
545 }
546 
547 void IslAstInfo::printScop(raw_ostream &OS, Scop &S) const {
548   isl_ast_print_options *Options;
549   isl_ast_node *RootNode = getAst();
550   Function *F = S.getRegion().getEntry()->getParent();
551 
552   OS << ":: isl ast :: " << F->getName() << " :: " << S.getRegion().getNameStr()
553      << "\n";
554 
555   if (!RootNode) {
556     OS << ":: isl ast generation and code generation was skipped!\n\n";
557     OS << ":: This is either because no useful optimizations could be applied "
558           "(use -polly-process-unprofitable to enforce code generation) or "
559           "because earlier passes such as dependence analysis timed out (use "
560           "-polly-dependences-computeout=0 to set dependence analysis timeout "
561           "to infinity)\n\n";
562     return;
563   }
564 
565   isl_ast_expr *RunCondition = getRunCondition();
566   char *RtCStr, *AstStr;
567 
568   Options = isl_ast_print_options_alloc(S.getIslCtx());
569   Options = isl_ast_print_options_set_print_for(Options, cbPrintFor, nullptr);
570 
571   isl_printer *P = isl_printer_to_str(S.getIslCtx());
572   P = isl_printer_print_ast_expr(P, RunCondition);
573   RtCStr = isl_printer_get_str(P);
574   P = isl_printer_flush(P);
575   P = isl_printer_indent(P, 4);
576   P = isl_printer_set_output_format(P, ISL_FORMAT_C);
577   P = isl_ast_node_print(RootNode, P, Options);
578   AstStr = isl_printer_get_str(P);
579 
580   isl_union_map *Schedule =
581       isl_union_map_intersect_domain(S.getSchedule(), S.getDomains());
582 
583   DEBUG({
584     dbgs() << S.getContextStr() << "\n";
585     dbgs() << stringFromIslObj(Schedule);
586   });
587   OS << "\nif (" << RtCStr << ")\n\n";
588   OS << AstStr << "\n";
589   OS << "else\n";
590   OS << "    {  /* original code */ }\n\n";
591 
592   free(RtCStr);
593   free(AstStr);
594 
595   isl_ast_expr_free(RunCondition);
596   isl_union_map_free(Schedule);
597   isl_ast_node_free(RootNode);
598   isl_printer_free(P);
599 }
600 
601 void IslAstInfo::getAnalysisUsage(AnalysisUsage &AU) const {
602   // Get the Common analysis usage of ScopPasses.
603   ScopPass::getAnalysisUsage(AU);
604   AU.addRequired<ScopInfo>();
605   AU.addRequired<DependenceInfo>();
606 }
607 
608 char IslAstInfo::ID = 0;
609 
610 Pass *polly::createIslAstInfoPass() { return new IslAstInfo(); }
611 
612 INITIALIZE_PASS_BEGIN(IslAstInfo, "polly-ast",
613                       "Polly - Generate an AST of the SCoP (isl)", false,
614                       false);
615 INITIALIZE_PASS_DEPENDENCY(ScopInfo);
616 INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
617 INITIALIZE_PASS_END(IslAstInfo, "polly-ast",
618                     "Polly - Generate an AST from the SCoP (isl)", false, false)
619