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 unsigned IslNodeBuilder::getNumberOfIterations(__isl_keep isl_ast_node *For) {
105   isl_union_map *Schedule = IslAstInfo::getSchedule(For);
106   isl_set *LoopDomain = isl_set_from_union_set(isl_union_map_range(Schedule));
107   if (isl_set_is_wrapping(LoopDomain))
108     LoopDomain = isl_map_range(isl_set_unwrap(LoopDomain));
109   int Dim = isl_set_dim(LoopDomain, isl_dim_set);
110 
111   // Calculate a map similar to the identity map, but with the last input
112   // and output dimension not related.
113   //  [i0, i1, i2, i3] -> [i0, i1, i2, o0]
114   isl_space *Space = isl_set_get_space(LoopDomain);
115   Space = isl_space_drop_dims(Space, isl_dim_out, Dim - 1, 1);
116   Space = isl_space_map_from_set(Space);
117   isl_map *Identity = isl_map_identity(Space);
118   Identity = isl_map_add_dims(Identity, isl_dim_in, 1);
119   Identity = isl_map_add_dims(Identity, isl_dim_out, 1);
120 
121   LoopDomain = isl_set_reset_tuple_id(LoopDomain);
122 
123   isl_map *Map = isl_map_from_domain_and_range(isl_set_copy(LoopDomain),
124                                                isl_set_copy(LoopDomain));
125   isl_set_free(LoopDomain);
126   Map = isl_map_intersect(Map, Identity);
127 
128   isl_map *LexMax = isl_map_lexmax(isl_map_copy(Map));
129   isl_map *LexMin = isl_map_lexmin(Map);
130   isl_map *Sub = isl_map_sum(LexMax, isl_map_neg(LexMin));
131 
132   isl_set *Elements = isl_map_range(Sub);
133 
134   if (!isl_set_is_singleton(Elements)) {
135     isl_set_free(Elements);
136     return -1;
137   }
138 
139   isl_point *P = isl_set_sample_point(Elements);
140 
141   isl_val *V;
142   V = isl_point_get_coordinate_val(P, isl_dim_set, Dim - 1);
143   int NumberIterations = isl_val_get_num_si(V);
144   isl_val_free(V);
145   isl_point_free(P);
146   if (NumberIterations == -1)
147     return -1;
148   return NumberIterations + 1;
149 }
150 
151 struct FindValuesUser {
152   LoopInfo &LI;
153   ScalarEvolution &SE;
154   Region &R;
155   SetVector<Value *> &Values;
156   SetVector<const SCEV *> &SCEVs;
157 };
158 
159 /// @brief Extract the values and SCEVs needed to generate code for a block.
160 static int findValuesInBlock(struct FindValuesUser &User, const ScopStmt *Stmt,
161                              const BasicBlock *BB) {
162   // Check all the operands of instructions in the basic block.
163   for (const Instruction &Inst : *BB) {
164     for (Value *SrcVal : Inst.operands()) {
165       if (Instruction *OpInst = dyn_cast<Instruction>(SrcVal))
166         if (canSynthesize(OpInst, &User.LI, &User.SE, &User.R)) {
167           User.SCEVs.insert(
168               User.SE.getSCEVAtScope(OpInst, User.LI.getLoopFor(BB)));
169           continue;
170         }
171       if (Instruction *OpInst = dyn_cast<Instruction>(SrcVal))
172         if (Stmt->getParent()->getRegion().contains(OpInst))
173           continue;
174 
175       if (isa<Instruction>(SrcVal) || isa<Argument>(SrcVal))
176         User.Values.insert(SrcVal);
177     }
178   }
179   return 0;
180 }
181 
182 /// Extract the values and SCEVs needed to generate code for a ScopStmt.
183 ///
184 /// This function extracts a ScopStmt from a given isl_set and computes the
185 /// Values this statement depends on as well as a set of SCEV expressions that
186 /// need to be synthesized when generating code for this statment.
187 static isl_stat findValuesInStmt(isl_set *Set, void *UserPtr) {
188   isl_id *Id = isl_set_get_tuple_id(Set);
189   struct FindValuesUser &User = *static_cast<struct FindValuesUser *>(UserPtr);
190   const ScopStmt *Stmt = static_cast<const ScopStmt *>(isl_id_get_user(Id));
191 
192   if (Stmt->isBlockStmt())
193     findValuesInBlock(User, Stmt, Stmt->getBasicBlock());
194   else {
195     assert(Stmt->isRegionStmt() &&
196            "Stmt was neither block nor region statement");
197     for (const BasicBlock *BB : Stmt->getRegion()->blocks())
198       findValuesInBlock(User, Stmt, BB);
199   }
200 
201   isl_id_free(Id);
202   isl_set_free(Set);
203   return isl_stat_ok;
204 }
205 
206 void IslNodeBuilder::getReferencesInSubtree(__isl_keep isl_ast_node *For,
207                                             SetVector<Value *> &Values,
208                                             SetVector<const Loop *> &Loops) {
209 
210   SetVector<const SCEV *> SCEVs;
211   struct FindValuesUser FindValues = {LI, SE, S.getRegion(), Values, SCEVs};
212 
213   for (const auto &I : IDToValue)
214     Values.insert(I.second);
215 
216   for (const auto &I : OutsideLoopIterations)
217     Values.insert(cast<SCEVUnknown>(I.second)->getValue());
218 
219   isl_union_set *Schedule = isl_union_map_domain(IslAstInfo::getSchedule(For));
220 
221   isl_union_set_foreach_set(Schedule, findValuesInStmt, &FindValues);
222   isl_union_set_free(Schedule);
223 
224   for (const SCEV *Expr : SCEVs) {
225     findValues(Expr, Values);
226     findLoops(Expr, Loops);
227   }
228 
229   Values.remove_if([](const Value *V) { return isa<GlobalValue>(V); });
230 
231   /// Remove loops that contain the scop or that are part of the scop, as they
232   /// are considered local. This leaves only loops that are before the scop, but
233   /// do not contain the scop itself.
234   Loops.remove_if([this](const Loop *L) {
235     return S.getRegion().contains(L) || 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   ParallelLoopGenerator::ValueToValueMapTy NewValuesReverse;
523 
524   for (auto P : NewValues)
525     NewValuesReverse[P.second] = P.first;
526 
527   Annotator.addAlternativeAliasBases(NewValuesReverse);
528 
529   create(Body);
530 
531   Annotator.resetAlternativeAliasBases();
532   // Restore the original values.
533   ValueMap = ValueMapCopy;
534   IDToValue = IDToValueCopy;
535 
536   Builder.SetInsertPoint(AfterLoop);
537   removeSubFuncFromDomTree((*LoopBody).getParent()->getParent(), DT);
538 
539   for (const Loop *L : Loops)
540     OutsideLoopIterations.erase(L);
541 
542   isl_ast_node_free(For);
543   isl_ast_expr_free(Iterator);
544   isl_id_free(IteratorID);
545 }
546 
547 void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) {
548   bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY;
549 
550   if (Vector && IslAstInfo::isInnermostParallel(For) &&
551       !IslAstInfo::isReductionParallel(For)) {
552     int VectorWidth = getNumberOfIterations(For);
553     if (1 < VectorWidth && VectorWidth <= 16) {
554       createForVector(For, VectorWidth);
555       return;
556     }
557   }
558 
559   if (IslAstInfo::isExecutedInParallel(For)) {
560     createForParallel(For);
561     return;
562   }
563   createForSequential(For);
564 }
565 
566 void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
567   isl_ast_expr *Cond = isl_ast_node_if_get_cond(If);
568 
569   Function *F = Builder.GetInsertBlock()->getParent();
570   LLVMContext &Context = F->getContext();
571 
572   BasicBlock *CondBB =
573       SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
574   CondBB->setName("polly.cond");
575   BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), &DT, &LI);
576   MergeBB->setName("polly.merge");
577   BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
578   BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F);
579 
580   DT.addNewBlock(ThenBB, CondBB);
581   DT.addNewBlock(ElseBB, CondBB);
582   DT.changeImmediateDominator(MergeBB, CondBB);
583 
584   Loop *L = LI.getLoopFor(CondBB);
585   if (L) {
586     L->addBasicBlockToLoop(ThenBB, LI);
587     L->addBasicBlockToLoop(ElseBB, LI);
588   }
589 
590   CondBB->getTerminator()->eraseFromParent();
591 
592   Builder.SetInsertPoint(CondBB);
593   Value *Predicate = ExprBuilder.create(Cond);
594   Builder.CreateCondBr(Predicate, ThenBB, ElseBB);
595   Builder.SetInsertPoint(ThenBB);
596   Builder.CreateBr(MergeBB);
597   Builder.SetInsertPoint(ElseBB);
598   Builder.CreateBr(MergeBB);
599   Builder.SetInsertPoint(ThenBB->begin());
600 
601   create(isl_ast_node_if_get_then(If));
602 
603   Builder.SetInsertPoint(ElseBB->begin());
604 
605   if (isl_ast_node_if_has_else(If))
606     create(isl_ast_node_if_get_else(If));
607 
608   Builder.SetInsertPoint(MergeBB->begin());
609 
610   isl_ast_node_free(If);
611 }
612 
613 void IslNodeBuilder::createSubstitutions(isl_ast_expr *Expr, ScopStmt *Stmt,
614                                          ValueMapT &VMap, LoopToScevMapT &LTS) {
615   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
616          "Expression of type 'op' expected");
617   assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call &&
618          "Opertation of type 'call' expected");
619   for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) {
620     isl_ast_expr *SubExpr;
621     Value *V;
622 
623     SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1);
624     V = ExprBuilder.create(SubExpr);
625     ScalarEvolution *SE = Stmt->getParent()->getSE();
626     LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V);
627   }
628 
629   // Add the current ValueMap to our per-statement value map.
630   //
631   // This is needed e.g. to rewrite array base addresses when moving code
632   // into a parallely executed subfunction.
633   VMap.insert(ValueMap.begin(), ValueMap.end());
634 
635   isl_ast_expr_free(Expr);
636 }
637 
638 void IslNodeBuilder::createSubstitutionsVector(
639     __isl_take isl_ast_expr *Expr, ScopStmt *Stmt, VectorValueMapT &VMap,
640     std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS,
641     __isl_take isl_id *IteratorID) {
642   int i = 0;
643 
644   Value *OldValue = IDToValue[IteratorID];
645   for (Value *IV : IVS) {
646     IDToValue[IteratorID] = IV;
647     createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VMap[i], VLTS[i]);
648     i++;
649   }
650 
651   IDToValue[IteratorID] = OldValue;
652   isl_id_free(IteratorID);
653   isl_ast_expr_free(Expr);
654 }
655 
656 void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) {
657   ValueMapT VMap;
658   LoopToScevMapT LTS;
659   isl_id *Id;
660   ScopStmt *Stmt;
661 
662   isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
663   isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
664   Id = isl_ast_expr_get_id(StmtExpr);
665   isl_ast_expr_free(StmtExpr);
666 
667   LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end());
668 
669   Stmt = (ScopStmt *)isl_id_get_user(Id);
670   Stmt->setAstBuild(IslAstInfo::getBuild(User));
671 
672   createSubstitutions(Expr, Stmt, VMap, LTS);
673   if (Stmt->isBlockStmt())
674     BlockGen.copyStmt(*Stmt, VMap, LTS);
675   else
676     RegionGen.copyStmt(*Stmt, VMap, LTS);
677 
678   isl_ast_node_free(User);
679   isl_id_free(Id);
680 }
681 
682 void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) {
683   isl_ast_node_list *List = isl_ast_node_block_get_children(Block);
684 
685   for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
686     create(isl_ast_node_list_get_ast_node(List, i));
687 
688   isl_ast_node_free(Block);
689   isl_ast_node_list_free(List);
690 }
691 
692 void IslNodeBuilder::create(__isl_take isl_ast_node *Node) {
693   switch (isl_ast_node_get_type(Node)) {
694   case isl_ast_node_error:
695     llvm_unreachable("code generation error");
696   case isl_ast_node_mark:
697     llvm_unreachable("Mark node unexpected");
698   case isl_ast_node_for:
699     createFor(Node);
700     return;
701   case isl_ast_node_if:
702     createIf(Node);
703     return;
704   case isl_ast_node_user:
705     createUser(Node);
706     return;
707   case isl_ast_node_block:
708     createBlock(Node);
709     return;
710   }
711 
712   llvm_unreachable("Unknown isl_ast_node type");
713 }
714 
715 void IslNodeBuilder::addParameters(__isl_take isl_set *Context) {
716 
717   for (unsigned i = 0; i < isl_set_dim(Context, isl_dim_param); ++i) {
718     isl_id *Id;
719 
720     Id = isl_set_get_dim_id(Context, isl_dim_param, i);
721     IDToValue[Id] = generateSCEV((const SCEV *)isl_id_get_user(Id));
722 
723     isl_id_free(Id);
724   }
725 
726   // Generate values for the current loop iteration for all surrounding loops.
727   //
728   // We may also reference loops outside of the scop which do not contain the
729   // scop itself, but as the number of such scops may be arbitrarily large we do
730   // not generate code for them here, but only at the point of code generation
731   // where these values are needed.
732   Region &R = S.getRegion();
733   Loop *L = LI.getLoopFor(R.getEntry());
734 
735   while (L != nullptr && R.contains(L))
736     L = L->getParentLoop();
737 
738   while (L != nullptr) {
739     const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
740                                             SE.getUnknown(Builder.getInt64(1)),
741                                             L, SCEV::FlagAnyWrap);
742     Value *V = generateSCEV(OuterLIV);
743     OutsideLoopIterations[L] = SE.getUnknown(V);
744     L = L->getParentLoop();
745   }
746 
747   isl_set_free(Context);
748 }
749 
750 Value *IslNodeBuilder::generateSCEV(const SCEV *Expr) {
751   Instruction *InsertLocation = --(Builder.GetInsertBlock()->end());
752   return expandCodeFor(S, SE, DL, "polly", Expr, Expr->getType(),
753                        InsertLocation);
754 }
755