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