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