1 //===------ IslNodeBuilder.cpp - Translate an isl AST into a LLVM-IR AST---===//
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 // This file contains the IslNodeBuilder, a class to translate an isl AST into
11 // a LLVM-IR AST.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "polly/CodeGen/IslNodeBuilder.h"
16 #include "polly/CodeGen/BlockGenerators.h"
17 #include "polly/CodeGen/CodeGeneration.h"
18 #include "polly/CodeGen/IslAst.h"
19 #include "polly/CodeGen/IslExprBuilder.h"
20 #include "polly/CodeGen/LoopGenerators.h"
21 #include "polly/CodeGen/Utils.h"
22 #include "polly/Config/config.h"
23 #include "polly/DependenceInfo.h"
24 #include "polly/LinkAllPasses.h"
25 #include "polly/ScopInfo.h"
26 #include "polly/Support/GICHelper.h"
27 #include "polly/Support/SCEVValidator.h"
28 #include "polly/Support/ScopHelper.h"
29 #include "polly/TempScopInfo.h"
30 #include "llvm/ADT/PostOrderIterator.h"
31 #include "llvm/ADT/SmallPtrSet.h"
32 #include "llvm/Analysis/LoopInfo.h"
33 #include "llvm/Analysis/PostDominators.h"
34 #include "llvm/Analysis/ScalarEvolutionExpander.h"
35 #include "llvm/IR/DataLayout.h"
36 #include "llvm/IR/Module.h"
37 #include "llvm/IR/Verifier.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/Debug.h"
40 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
41 #include "isl/aff.h"
42 #include "isl/ast.h"
43 #include "isl/ast_build.h"
44 #include "isl/list.h"
45 #include "isl/map.h"
46 #include "isl/set.h"
47 #include "isl/union_map.h"
48 #include "isl/union_set.h"
49 
50 using namespace polly;
51 using namespace llvm;
52 
53 __isl_give isl_ast_expr *
54 IslNodeBuilder::getUpperBound(__isl_keep isl_ast_node *For,
55                               ICmpInst::Predicate &Predicate) {
56   isl_id *UBID, *IteratorID;
57   isl_ast_expr *Cond, *Iterator, *UB, *Arg0;
58   isl_ast_op_type Type;
59 
60   Cond = isl_ast_node_for_get_cond(For);
61   Iterator = isl_ast_node_for_get_iterator(For);
62   isl_ast_expr_get_type(Cond);
63   assert(isl_ast_expr_get_type(Cond) == isl_ast_expr_op &&
64          "conditional expression is not an atomic upper bound");
65 
66   Type = isl_ast_expr_get_op_type(Cond);
67 
68   switch (Type) {
69   case isl_ast_op_le:
70     Predicate = ICmpInst::ICMP_SLE;
71     break;
72   case isl_ast_op_lt:
73     Predicate = ICmpInst::ICMP_SLT;
74     break;
75   default:
76     llvm_unreachable("Unexpected comparision type in loop conditon");
77   }
78 
79   Arg0 = isl_ast_expr_get_op_arg(Cond, 0);
80 
81   assert(isl_ast_expr_get_type(Arg0) == isl_ast_expr_id &&
82          "conditional expression is not an atomic upper bound");
83 
84   UBID = isl_ast_expr_get_id(Arg0);
85 
86   assert(isl_ast_expr_get_type(Iterator) == isl_ast_expr_id &&
87          "Could not get the iterator");
88 
89   IteratorID = isl_ast_expr_get_id(Iterator);
90 
91   assert(UBID == IteratorID &&
92          "conditional expression is not an atomic upper bound");
93 
94   UB = isl_ast_expr_get_op_arg(Cond, 1);
95 
96   isl_ast_expr_free(Cond);
97   isl_ast_expr_free(Iterator);
98   isl_ast_expr_free(Arg0);
99   isl_id_free(IteratorID);
100   isl_id_free(UBID);
101 
102   return UB;
103 }
104 
105 unsigned IslNodeBuilder::getNumberOfIterations(__isl_keep isl_ast_node *For) {
106   isl_union_map *Schedule = IslAstInfo::getSchedule(For);
107   isl_set *LoopDomain = isl_set_from_union_set(isl_union_map_range(Schedule));
108   if (isl_set_is_wrapping(LoopDomain))
109     LoopDomain = isl_map_range(isl_set_unwrap(LoopDomain));
110   int Dim = isl_set_dim(LoopDomain, isl_dim_set);
111 
112   // Calculate a map similar to the identity map, but with the last input
113   // and output dimension not related.
114   //  [i0, i1, i2, i3] -> [i0, i1, i2, o0]
115   isl_space *Space = isl_set_get_space(LoopDomain);
116   Space = isl_space_drop_dims(Space, isl_dim_out, Dim - 1, 1);
117   Space = isl_space_map_from_set(Space);
118   isl_map *Identity = isl_map_identity(Space);
119   Identity = isl_map_add_dims(Identity, isl_dim_in, 1);
120   Identity = isl_map_add_dims(Identity, isl_dim_out, 1);
121 
122   LoopDomain = isl_set_reset_tuple_id(LoopDomain);
123 
124   isl_map *Map = isl_map_from_domain_and_range(isl_set_copy(LoopDomain),
125                                                isl_set_copy(LoopDomain));
126   isl_set_free(LoopDomain);
127   Map = isl_map_intersect(Map, Identity);
128 
129   isl_map *LexMax = isl_map_lexmax(isl_map_copy(Map));
130   isl_map *LexMin = isl_map_lexmin(Map);
131   isl_map *Sub = isl_map_sum(LexMax, isl_map_neg(LexMin));
132 
133   isl_set *Elements = isl_map_range(Sub);
134 
135   if (!isl_set_is_singleton(Elements)) {
136     isl_set_free(Elements);
137     return -1;
138   }
139 
140   isl_point *P = isl_set_sample_point(Elements);
141 
142   isl_val *V;
143   V = isl_point_get_coordinate_val(P, isl_dim_set, Dim - 1);
144   int NumberIterations = isl_val_get_num_si(V);
145   isl_val_free(V);
146   isl_point_free(P);
147   if (NumberIterations == -1)
148     return -1;
149   return NumberIterations + 1;
150 }
151 
152 struct FindValuesUser {
153   LoopInfo &LI;
154   ScalarEvolution &SE;
155   Region &R;
156   SetVector<Value *> &Values;
157   SetVector<const SCEV *> &SCEVs;
158 };
159 
160 /// @brief Extract the values and SCEVs needed to generate code for a block.
161 static int findValuesInBlock(struct FindValuesUser &User, const ScopStmt *Stmt,
162                              const BasicBlock *BB) {
163   // Check all the operands of instructions in the basic block.
164   for (const Instruction &Inst : *BB) {
165     for (Value *SrcVal : Inst.operands()) {
166       if (Instruction *OpInst = dyn_cast<Instruction>(SrcVal))
167         if (canSynthesize(OpInst, &User.LI, &User.SE, &User.R)) {
168           User.SCEVs.insert(
169               User.SE.getSCEVAtScope(OpInst, User.LI.getLoopFor(BB)));
170           continue;
171         }
172       if (Instruction *OpInst = dyn_cast<Instruction>(SrcVal))
173         if (Stmt->getParent()->getRegion().contains(OpInst))
174           continue;
175 
176       if (isa<Instruction>(SrcVal) || isa<Argument>(SrcVal))
177         User.Values.insert(SrcVal);
178     }
179   }
180   return 0;
181 }
182 
183 /// Extract the values and SCEVs needed to generate code for a ScopStmt.
184 ///
185 /// This function extracts a ScopStmt from a given isl_set and computes the
186 /// Values this statement depends on as well as a set of SCEV expressions that
187 /// need to be synthesized when generating code for this statment.
188 static isl_stat findValuesInStmt(isl_set *Set, void *UserPtr) {
189   isl_id *Id = isl_set_get_tuple_id(Set);
190   struct FindValuesUser &User = *static_cast<struct FindValuesUser *>(UserPtr);
191   const ScopStmt *Stmt = static_cast<const ScopStmt *>(isl_id_get_user(Id));
192 
193   if (Stmt->isBlockStmt())
194     findValuesInBlock(User, Stmt, Stmt->getBasicBlock());
195   else {
196     assert(Stmt->isRegionStmt() &&
197            "Stmt was neither block nor region statement");
198     for (const BasicBlock *BB : Stmt->getRegion()->blocks())
199       findValuesInBlock(User, Stmt, BB);
200   }
201 
202   isl_id_free(Id);
203   isl_set_free(Set);
204   return isl_stat_ok;
205 }
206 
207 void IslNodeBuilder::getReferencesInSubtree(__isl_keep isl_ast_node *For,
208                                             SetVector<Value *> &Values,
209                                             SetVector<const Loop *> &Loops) {
210 
211   SetVector<const SCEV *> SCEVs;
212   struct FindValuesUser FindValues = {LI, SE, S.getRegion(), Values, SCEVs};
213 
214   for (const auto &I : IDToValue)
215     Values.insert(I.second);
216 
217   for (const auto &I : OutsideLoopIterations)
218     Values.insert(cast<SCEVUnknown>(I.second)->getValue());
219 
220   isl_union_set *Schedule = isl_union_map_domain(IslAstInfo::getSchedule(For));
221 
222   isl_union_set_foreach_set(Schedule, findValuesInStmt, &FindValues);
223   isl_union_set_free(Schedule);
224 
225   for (const SCEV *Expr : SCEVs) {
226     findValues(Expr, Values);
227     findLoops(Expr, Loops);
228   }
229 
230   Values.remove_if([](const Value *V) { return isa<GlobalValue>(V); });
231 
232   /// Remove loops that contain the scop or that are part of the scop, as they
233   /// are considered local. This leaves only loops that are before the scop, but
234   /// do not contain the scop itself.
235   Loops.remove_if([this](const Loop *L) {
236     return S.getRegion().contains(L) || L->contains(S.getRegion().getEntry());
237   });
238 }
239 
240 void IslNodeBuilder::updateValues(
241     ParallelLoopGenerator::ValueToValueMapTy &NewValues) {
242   SmallPtrSet<Value *, 5> Inserted;
243 
244   for (const auto &I : IDToValue) {
245     IDToValue[I.first] = NewValues[I.second];
246     Inserted.insert(I.second);
247   }
248 
249   for (const auto &I : NewValues) {
250     if (Inserted.count(I.first))
251       continue;
252 
253     ValueMap[I.first] = I.second;
254   }
255 }
256 
257 void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User,
258                                       std::vector<Value *> &IVS,
259                                       __isl_take isl_id *IteratorID,
260                                       __isl_take isl_union_map *Schedule) {
261   isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
262   isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
263   isl_id *Id = isl_ast_expr_get_id(StmtExpr);
264   isl_ast_expr_free(StmtExpr);
265   ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id);
266   Stmt->setAstBuild(IslAstInfo::getBuild(User));
267   VectorValueMapT VectorMap(IVS.size());
268   std::vector<LoopToScevMapT> VLTS(IVS.size());
269 
270   isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain());
271   Schedule = isl_union_map_intersect_domain(Schedule, Domain);
272   isl_map *S = isl_map_from_union_map(Schedule);
273 
274   createSubstitutionsVector(Expr, Stmt, VectorMap, VLTS, IVS, IteratorID);
275   VectorBlockGenerator::generate(BlockGen, *Stmt, VectorMap, VLTS, S);
276 
277   isl_map_free(S);
278   isl_id_free(Id);
279   isl_ast_node_free(User);
280 }
281 
282 void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For,
283                                      int VectorWidth) {
284   isl_ast_node *Body = isl_ast_node_for_get_body(For);
285   isl_ast_expr *Init = isl_ast_node_for_get_init(For);
286   isl_ast_expr *Inc = isl_ast_node_for_get_inc(For);
287   isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For);
288   isl_id *IteratorID = isl_ast_expr_get_id(Iterator);
289 
290   Value *ValueLB = ExprBuilder.create(Init);
291   Value *ValueInc = ExprBuilder.create(Inc);
292 
293   Type *MaxType = ExprBuilder.getType(Iterator);
294   MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
295   MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
296 
297   if (MaxType != ValueLB->getType())
298     ValueLB = Builder.CreateSExt(ValueLB, MaxType);
299   if (MaxType != ValueInc->getType())
300     ValueInc = Builder.CreateSExt(ValueInc, MaxType);
301 
302   std::vector<Value *> IVS(VectorWidth);
303   IVS[0] = ValueLB;
304 
305   for (int i = 1; i < VectorWidth; i++)
306     IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv");
307 
308   isl_union_map *Schedule = IslAstInfo::getSchedule(For);
309   assert(Schedule && "For statement annotation does not contain its schedule");
310 
311   IDToValue[IteratorID] = ValueLB;
312 
313   switch (isl_ast_node_get_type(Body)) {
314   case isl_ast_node_user:
315     createUserVector(Body, IVS, isl_id_copy(IteratorID),
316                      isl_union_map_copy(Schedule));
317     break;
318   case isl_ast_node_block: {
319     isl_ast_node_list *List = isl_ast_node_block_get_children(Body);
320 
321     for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
322       createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS,
323                        isl_id_copy(IteratorID), isl_union_map_copy(Schedule));
324 
325     isl_ast_node_free(Body);
326     isl_ast_node_list_free(List);
327     break;
328   }
329   default:
330     isl_ast_node_dump(Body);
331     llvm_unreachable("Unhandled isl_ast_node in vectorizer");
332   }
333 
334   IDToValue.erase(IDToValue.find(IteratorID));
335   isl_id_free(IteratorID);
336   isl_union_map_free(Schedule);
337 
338   isl_ast_node_free(For);
339   isl_ast_expr_free(Iterator);
340 }
341 
342 void IslNodeBuilder::createForSequential(__isl_take isl_ast_node *For) {
343   isl_ast_node *Body;
344   isl_ast_expr *Init, *Inc, *Iterator, *UB;
345   isl_id *IteratorID;
346   Value *ValueLB, *ValueUB, *ValueInc;
347   Type *MaxType;
348   BasicBlock *ExitBlock;
349   Value *IV;
350   CmpInst::Predicate Predicate;
351   bool Parallel;
352 
353   Parallel =
354       IslAstInfo::isParallel(For) && !IslAstInfo::isReductionParallel(For);
355 
356   Body = isl_ast_node_for_get_body(For);
357 
358   // isl_ast_node_for_is_degenerate(For)
359   //
360   // TODO: For degenerated loops we could generate a plain assignment.
361   //       However, for now we just reuse the logic for normal loops, which will
362   //       create a loop with a single iteration.
363 
364   Init = isl_ast_node_for_get_init(For);
365   Inc = isl_ast_node_for_get_inc(For);
366   Iterator = isl_ast_node_for_get_iterator(For);
367   IteratorID = isl_ast_expr_get_id(Iterator);
368   UB = getUpperBound(For, Predicate);
369 
370   ValueLB = ExprBuilder.create(Init);
371   ValueUB = ExprBuilder.create(UB);
372   ValueInc = ExprBuilder.create(Inc);
373 
374   MaxType = ExprBuilder.getType(Iterator);
375   MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
376   MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
377   MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
378 
379   if (MaxType != ValueLB->getType())
380     ValueLB = Builder.CreateSExt(ValueLB, MaxType);
381   if (MaxType != ValueUB->getType())
382     ValueUB = Builder.CreateSExt(ValueUB, MaxType);
383   if (MaxType != ValueInc->getType())
384     ValueInc = Builder.CreateSExt(ValueInc, MaxType);
385 
386   // If we can show that LB <Predicate> UB holds at least once, we can
387   // omit the GuardBB in front of the loop.
388   bool UseGuardBB =
389       !SE.isKnownPredicate(Predicate, SE.getSCEV(ValueLB), SE.getSCEV(ValueUB));
390   IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, P, LI, DT, ExitBlock,
391                   Predicate, &Annotator, Parallel, UseGuardBB);
392   IDToValue[IteratorID] = IV;
393 
394   create(Body);
395 
396   Annotator.popLoop(Parallel);
397 
398   IDToValue.erase(IDToValue.find(IteratorID));
399 
400   Builder.SetInsertPoint(ExitBlock->begin());
401 
402   isl_ast_node_free(For);
403   isl_ast_expr_free(Iterator);
404   isl_id_free(IteratorID);
405 }
406 
407 /// @brief Remove the BBs contained in a (sub)function from the dominator tree.
408 ///
409 /// This function removes the basic blocks that are part of a subfunction from
410 /// the dominator tree. Specifically, when generating code it may happen that at
411 /// some point the code generation continues in a new sub-function (e.g., when
412 /// generating OpenMP code). The basic blocks that are created in this
413 /// sub-function are then still part of the dominator tree of the original
414 /// function, such that the dominator tree reaches over function boundaries.
415 /// This is not only incorrect, but also causes crashes. This function now
416 /// removes from the dominator tree all basic blocks that are dominated (and
417 /// consequently reachable) from the entry block of this (sub)function.
418 ///
419 /// FIXME: A LLVM (function or region) pass should not touch anything outside of
420 /// the function/region it runs on. Hence, the pure need for this function shows
421 /// that we do not comply to this rule. At the moment, this does not cause any
422 /// issues, but we should be aware that such issues may appear. Unfortunately
423 /// the current LLVM pass infrastructure does not allow to make Polly a module
424 /// or call-graph pass to solve this issue, as such a pass would not have access
425 /// to the per-function analyses passes needed by Polly. A future pass manager
426 /// infrastructure is supposed to enable such kind of access possibly allowing
427 /// us to create a cleaner solution here.
428 ///
429 /// FIXME: Instead of adding the dominance information and then dropping it
430 /// later on, we should try to just not add it in the first place. This requires
431 /// some careful testing to make sure this does not break in interaction with
432 /// the SCEVBuilder and SplitBlock which may rely on the dominator tree or
433 /// which may try to update it.
434 ///
435 /// @param F The function which contains the BBs to removed.
436 /// @param DT The dominator tree from which to remove the BBs.
437 static void removeSubFuncFromDomTree(Function *F, DominatorTree &DT) {
438   DomTreeNode *N = DT.getNode(&F->getEntryBlock());
439   std::vector<BasicBlock *> Nodes;
440 
441   // We can only remove an element from the dominator tree, if all its children
442   // have been removed. To ensure this we obtain the list of nodes to remove
443   // using a post-order tree traversal.
444   for (po_iterator<DomTreeNode *> I = po_begin(N), E = po_end(N); I != E; ++I)
445     Nodes.push_back(I->getBlock());
446 
447   for (BasicBlock *BB : Nodes)
448     DT.eraseNode(BB);
449 }
450 
451 void IslNodeBuilder::createForParallel(__isl_take isl_ast_node *For) {
452   isl_ast_node *Body;
453   isl_ast_expr *Init, *Inc, *Iterator, *UB;
454   isl_id *IteratorID;
455   Value *ValueLB, *ValueUB, *ValueInc;
456   Type *MaxType;
457   Value *IV;
458   CmpInst::Predicate Predicate;
459 
460   Body = isl_ast_node_for_get_body(For);
461   Init = isl_ast_node_for_get_init(For);
462   Inc = isl_ast_node_for_get_inc(For);
463   Iterator = isl_ast_node_for_get_iterator(For);
464   IteratorID = isl_ast_expr_get_id(Iterator);
465   UB = getUpperBound(For, Predicate);
466 
467   ValueLB = ExprBuilder.create(Init);
468   ValueUB = ExprBuilder.create(UB);
469   ValueInc = ExprBuilder.create(Inc);
470 
471   // OpenMP always uses SLE. In case the isl generated AST uses a SLT
472   // expression, we need to adjust the loop blound by one.
473   if (Predicate == CmpInst::ICMP_SLT)
474     ValueUB = Builder.CreateAdd(
475         ValueUB, Builder.CreateSExt(Builder.getTrue(), ValueUB->getType()));
476 
477   MaxType = ExprBuilder.getType(Iterator);
478   MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
479   MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
480   MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
481 
482   if (MaxType != ValueLB->getType())
483     ValueLB = Builder.CreateSExt(ValueLB, MaxType);
484   if (MaxType != ValueUB->getType())
485     ValueUB = Builder.CreateSExt(ValueUB, MaxType);
486   if (MaxType != ValueInc->getType())
487     ValueInc = Builder.CreateSExt(ValueInc, MaxType);
488 
489   BasicBlock::iterator LoopBody;
490 
491   SetVector<Value *> SubtreeValues;
492   SetVector<const Loop *> Loops;
493 
494   getReferencesInSubtree(For, SubtreeValues, Loops);
495 
496   // Create for all loops we depend on values that contain the current loop
497   // iteration. These values are necessary to generate code for SCEVs that
498   // depend on such loops. As a result we need to pass them to the subfunction.
499   for (const Loop *L : Loops) {
500     const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
501                                             SE.getUnknown(Builder.getInt64(1)),
502                                             L, SCEV::FlagAnyWrap);
503     Value *V = generateSCEV(OuterLIV);
504     OutsideLoopIterations[L] = SE.getUnknown(V);
505     SubtreeValues.insert(V);
506   }
507 
508   ParallelLoopGenerator::ValueToValueMapTy NewValues;
509   ParallelLoopGenerator ParallelLoopGen(Builder, P, LI, DT, DL);
510 
511   IV = ParallelLoopGen.createParallelLoop(ValueLB, ValueUB, ValueInc,
512                                           SubtreeValues, NewValues, &LoopBody);
513   BasicBlock::iterator AfterLoop = Builder.GetInsertPoint();
514   Builder.SetInsertPoint(LoopBody);
515 
516   // Save the current values.
517   ValueMapT ValueMapCopy = ValueMap;
518   IslExprBuilder::IDToValueTy IDToValueCopy = IDToValue;
519 
520   updateValues(NewValues);
521   IDToValue[IteratorID] = IV;
522 
523   create(Body);
524 
525   // Restore the original values.
526   ValueMap = ValueMapCopy;
527   IDToValue = IDToValueCopy;
528 
529   Builder.SetInsertPoint(AfterLoop);
530   removeSubFuncFromDomTree((*LoopBody).getParent()->getParent(), DT);
531 
532   for (const Loop *L : Loops)
533     OutsideLoopIterations.erase(L);
534 
535   isl_ast_node_free(For);
536   isl_ast_expr_free(Iterator);
537   isl_id_free(IteratorID);
538 }
539 
540 void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) {
541   bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY;
542 
543   if (Vector && IslAstInfo::isInnermostParallel(For) &&
544       !IslAstInfo::isReductionParallel(For)) {
545     int VectorWidth = getNumberOfIterations(For);
546     if (1 < VectorWidth && VectorWidth <= 16) {
547       createForVector(For, VectorWidth);
548       return;
549     }
550   }
551 
552   if (IslAstInfo::isExecutedInParallel(For)) {
553     createForParallel(For);
554     return;
555   }
556   createForSequential(For);
557 }
558 
559 void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
560   isl_ast_expr *Cond = isl_ast_node_if_get_cond(If);
561 
562   Function *F = Builder.GetInsertBlock()->getParent();
563   LLVMContext &Context = F->getContext();
564 
565   BasicBlock *CondBB =
566       SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
567   CondBB->setName("polly.cond");
568   BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), &DT, &LI);
569   MergeBB->setName("polly.merge");
570   BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
571   BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F);
572 
573   DT.addNewBlock(ThenBB, CondBB);
574   DT.addNewBlock(ElseBB, CondBB);
575   DT.changeImmediateDominator(MergeBB, CondBB);
576 
577   Loop *L = LI.getLoopFor(CondBB);
578   if (L) {
579     L->addBasicBlockToLoop(ThenBB, LI);
580     L->addBasicBlockToLoop(ElseBB, LI);
581   }
582 
583   CondBB->getTerminator()->eraseFromParent();
584 
585   Builder.SetInsertPoint(CondBB);
586   Value *Predicate = ExprBuilder.create(Cond);
587   Builder.CreateCondBr(Predicate, ThenBB, ElseBB);
588   Builder.SetInsertPoint(ThenBB);
589   Builder.CreateBr(MergeBB);
590   Builder.SetInsertPoint(ElseBB);
591   Builder.CreateBr(MergeBB);
592   Builder.SetInsertPoint(ThenBB->begin());
593 
594   create(isl_ast_node_if_get_then(If));
595 
596   Builder.SetInsertPoint(ElseBB->begin());
597 
598   if (isl_ast_node_if_has_else(If))
599     create(isl_ast_node_if_get_else(If));
600 
601   Builder.SetInsertPoint(MergeBB->begin());
602 
603   isl_ast_node_free(If);
604 }
605 
606 void IslNodeBuilder::createSubstitutions(isl_ast_expr *Expr, ScopStmt *Stmt,
607                                          ValueMapT &VMap, LoopToScevMapT &LTS) {
608   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
609          "Expression of type 'op' expected");
610   assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call &&
611          "Opertation of type 'call' expected");
612   for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) {
613     isl_ast_expr *SubExpr;
614     Value *V;
615 
616     SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1);
617     V = ExprBuilder.create(SubExpr);
618     ScalarEvolution *SE = Stmt->getParent()->getSE();
619     LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V);
620   }
621 
622   // Add the current ValueMap to our per-statement value map.
623   //
624   // This is needed e.g. to rewrite array base addresses when moving code
625   // into a parallely executed subfunction.
626   VMap.insert(ValueMap.begin(), ValueMap.end());
627 
628   isl_ast_expr_free(Expr);
629 }
630 
631 void IslNodeBuilder::createSubstitutionsVector(
632     __isl_take isl_ast_expr *Expr, ScopStmt *Stmt, VectorValueMapT &VMap,
633     std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS,
634     __isl_take isl_id *IteratorID) {
635   int i = 0;
636 
637   Value *OldValue = IDToValue[IteratorID];
638   for (Value *IV : IVS) {
639     IDToValue[IteratorID] = IV;
640     createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VMap[i], VLTS[i]);
641     i++;
642   }
643 
644   IDToValue[IteratorID] = OldValue;
645   isl_id_free(IteratorID);
646   isl_ast_expr_free(Expr);
647 }
648 
649 void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) {
650   ValueMapT VMap;
651   LoopToScevMapT LTS;
652   isl_id *Id;
653   ScopStmt *Stmt;
654 
655   isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
656   isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
657   Id = isl_ast_expr_get_id(StmtExpr);
658   isl_ast_expr_free(StmtExpr);
659 
660   LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end());
661 
662   Stmt = (ScopStmt *)isl_id_get_user(Id);
663   Stmt->setAstBuild(IslAstInfo::getBuild(User));
664 
665   createSubstitutions(Expr, Stmt, VMap, LTS);
666   if (Stmt->isBlockStmt())
667     BlockGen.copyStmt(*Stmt, VMap, LTS);
668   else
669     RegionGen.copyStmt(*Stmt, VMap, LTS);
670 
671   isl_ast_node_free(User);
672   isl_id_free(Id);
673 }
674 
675 void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) {
676   isl_ast_node_list *List = isl_ast_node_block_get_children(Block);
677 
678   for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
679     create(isl_ast_node_list_get_ast_node(List, i));
680 
681   isl_ast_node_free(Block);
682   isl_ast_node_list_free(List);
683 }
684 
685 void IslNodeBuilder::create(__isl_take isl_ast_node *Node) {
686   switch (isl_ast_node_get_type(Node)) {
687   case isl_ast_node_error:
688     llvm_unreachable("code generation error");
689   case isl_ast_node_mark:
690     llvm_unreachable("Mark node unexpected");
691   case isl_ast_node_for:
692     createFor(Node);
693     return;
694   case isl_ast_node_if:
695     createIf(Node);
696     return;
697   case isl_ast_node_user:
698     createUser(Node);
699     return;
700   case isl_ast_node_block:
701     createBlock(Node);
702     return;
703   }
704 
705   llvm_unreachable("Unknown isl_ast_node type");
706 }
707 
708 void IslNodeBuilder::addParameters(__isl_take isl_set *Context) {
709 
710   for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
711     isl_id *Id;
712 
713     Id = isl_set_get_dim_id(Context, isl_dim_param, i);
714     IDToValue[Id] = generateSCEV((const SCEV *)isl_id_get_user(Id));
715 
716     isl_id_free(Id);
717   }
718 
719   // Generate values for the current loop iteration for all surrounding loops.
720   //
721   // We may also reference loops outside of the scop which do not contain the
722   // scop itself, but as the number of such scops may be arbitrarily large we do
723   // not generate code for them here, but only at the point of code generation
724   // where these values are needed.
725   Region &R = S.getRegion();
726   Loop *L = LI.getLoopFor(R.getEntry());
727 
728   while (L != nullptr && R.contains(L))
729     L = L->getParentLoop();
730 
731   while (L != nullptr) {
732     const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
733                                             SE.getUnknown(Builder.getInt64(1)),
734                                             L, SCEV::FlagAnyWrap);
735     Value *V = generateSCEV(OuterLIV);
736     OutsideLoopIterations[L] = SE.getUnknown(V);
737     L = L->getParentLoop();
738   }
739 
740   isl_set_free(Context);
741 }
742 
743 Value *IslNodeBuilder::generateSCEV(const SCEV *Expr) {
744   Instruction *InsertLocation = --(Builder.GetInsertBlock()->end());
745   return Rewriter.expandCodeFor(Expr, Expr->getType(), InsertLocation);
746 }
747