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