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