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->isExplicit()) {
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   // The preamble of parallel code interacts different than normal code with
538   // e.g., scalar initialization. Therefore, we ensure the parallel code is
539   // separated from the last basic block.
540   BasicBlock *ParBB =
541       SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
542   ParBB->setName("polly.parallel.for");
543   Builder.SetInsertPoint(ParBB->begin());
544 
545   Body = isl_ast_node_for_get_body(For);
546   Init = isl_ast_node_for_get_init(For);
547   Inc = isl_ast_node_for_get_inc(For);
548   Iterator = isl_ast_node_for_get_iterator(For);
549   IteratorID = isl_ast_expr_get_id(Iterator);
550   UB = getUpperBound(For, Predicate);
551 
552   ValueLB = ExprBuilder.create(Init);
553   ValueUB = ExprBuilder.create(UB);
554   ValueInc = ExprBuilder.create(Inc);
555 
556   // OpenMP always uses SLE. In case the isl generated AST uses a SLT
557   // expression, we need to adjust the loop blound by one.
558   if (Predicate == CmpInst::ICMP_SLT)
559     ValueUB = Builder.CreateAdd(
560         ValueUB, Builder.CreateSExt(Builder.getTrue(), ValueUB->getType()));
561 
562   MaxType = ExprBuilder.getType(Iterator);
563   MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
564   MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
565   MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
566 
567   if (MaxType != ValueLB->getType())
568     ValueLB = Builder.CreateSExt(ValueLB, MaxType);
569   if (MaxType != ValueUB->getType())
570     ValueUB = Builder.CreateSExt(ValueUB, MaxType);
571   if (MaxType != ValueInc->getType())
572     ValueInc = Builder.CreateSExt(ValueInc, MaxType);
573 
574   BasicBlock::iterator LoopBody;
575 
576   SetVector<Value *> SubtreeValues;
577   SetVector<const Loop *> Loops;
578 
579   getReferencesInSubtree(For, SubtreeValues, Loops);
580 
581   // Create for all loops we depend on values that contain the current loop
582   // iteration. These values are necessary to generate code for SCEVs that
583   // depend on such loops. As a result we need to pass them to the subfunction.
584   for (const Loop *L : Loops) {
585     const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
586                                             SE.getUnknown(Builder.getInt64(1)),
587                                             L, SCEV::FlagAnyWrap);
588     Value *V = generateSCEV(OuterLIV);
589     OutsideLoopIterations[L] = SE.getUnknown(V);
590     SubtreeValues.insert(V);
591   }
592 
593   ParallelLoopGenerator::ValueToValueMapTy NewValues;
594   ParallelLoopGenerator ParallelLoopGen(Builder, P, LI, DT, DL);
595 
596   IV = ParallelLoopGen.createParallelLoop(ValueLB, ValueUB, ValueInc,
597                                           SubtreeValues, NewValues, &LoopBody);
598   BasicBlock::iterator AfterLoop = Builder.GetInsertPoint();
599   Builder.SetInsertPoint(LoopBody);
600 
601   // Save the current values.
602   ValueMapT ValueMapCopy = ValueMap;
603   IslExprBuilder::IDToValueTy IDToValueCopy = IDToValue;
604 
605   updateValues(NewValues);
606   IDToValue[IteratorID] = IV;
607 
608   ParallelLoopGenerator::ValueToValueMapTy NewValuesReverse;
609 
610   for (auto P : NewValues)
611     NewValuesReverse[P.second] = P.first;
612 
613   Annotator.addAlternativeAliasBases(NewValuesReverse);
614 
615   create(Body);
616 
617   Annotator.resetAlternativeAliasBases();
618   // Restore the original values.
619   ValueMap = ValueMapCopy;
620   IDToValue = IDToValueCopy;
621 
622   Builder.SetInsertPoint(AfterLoop);
623   removeSubFuncFromDomTree((*LoopBody).getParent()->getParent(), DT);
624 
625   for (const Loop *L : Loops)
626     OutsideLoopIterations.erase(L);
627 
628   isl_ast_node_free(For);
629   isl_ast_expr_free(Iterator);
630   isl_id_free(IteratorID);
631 }
632 
633 void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) {
634   bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY;
635 
636   if (Vector && IslAstInfo::isInnermostParallel(For) &&
637       !IslAstInfo::isReductionParallel(For)) {
638     int VectorWidth = getNumberOfIterations(For);
639     if (1 < VectorWidth && VectorWidth <= 16) {
640       createForVector(For, VectorWidth);
641       return;
642     }
643   }
644 
645   if (IslAstInfo::isExecutedInParallel(For)) {
646     createForParallel(For);
647     return;
648   }
649   createForSequential(For);
650 }
651 
652 void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
653   isl_ast_expr *Cond = isl_ast_node_if_get_cond(If);
654 
655   Function *F = Builder.GetInsertBlock()->getParent();
656   LLVMContext &Context = F->getContext();
657 
658   BasicBlock *CondBB =
659       SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
660   CondBB->setName("polly.cond");
661   BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), &DT, &LI);
662   MergeBB->setName("polly.merge");
663   BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
664   BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F);
665 
666   DT.addNewBlock(ThenBB, CondBB);
667   DT.addNewBlock(ElseBB, CondBB);
668   DT.changeImmediateDominator(MergeBB, CondBB);
669 
670   Loop *L = LI.getLoopFor(CondBB);
671   if (L) {
672     L->addBasicBlockToLoop(ThenBB, LI);
673     L->addBasicBlockToLoop(ElseBB, LI);
674   }
675 
676   CondBB->getTerminator()->eraseFromParent();
677 
678   Builder.SetInsertPoint(CondBB);
679   Value *Predicate = ExprBuilder.create(Cond);
680   Builder.CreateCondBr(Predicate, ThenBB, ElseBB);
681   Builder.SetInsertPoint(ThenBB);
682   Builder.CreateBr(MergeBB);
683   Builder.SetInsertPoint(ElseBB);
684   Builder.CreateBr(MergeBB);
685   Builder.SetInsertPoint(ThenBB->begin());
686 
687   create(isl_ast_node_if_get_then(If));
688 
689   Builder.SetInsertPoint(ElseBB->begin());
690 
691   if (isl_ast_node_if_has_else(If))
692     create(isl_ast_node_if_get_else(If));
693 
694   Builder.SetInsertPoint(MergeBB->begin());
695 
696   isl_ast_node_free(If);
697 }
698 
699 __isl_give isl_id_to_ast_expr *
700 IslNodeBuilder::createNewAccesses(ScopStmt *Stmt,
701                                   __isl_keep isl_ast_node *Node) {
702   isl_id_to_ast_expr *NewAccesses =
703       isl_id_to_ast_expr_alloc(Stmt->getParent()->getIslCtx(), 0);
704   for (auto *MA : *Stmt) {
705     if (!MA->hasNewAccessRelation())
706       continue;
707 
708     auto Build = IslAstInfo::getBuild(Node);
709     assert(Build && "Could not obtain isl_ast_build from user node");
710     auto Schedule = isl_ast_build_get_schedule(Build);
711     auto PWAccRel = MA->applyScheduleToAccessRelation(Schedule);
712 
713     auto AccessExpr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
714     NewAccesses = isl_id_to_ast_expr_set(NewAccesses, MA->getId(), AccessExpr);
715   }
716 
717   return NewAccesses;
718 }
719 
720 void IslNodeBuilder::createSubstitutions(isl_ast_expr *Expr, ScopStmt *Stmt,
721                                          LoopToScevMapT &LTS) {
722   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
723          "Expression of type 'op' expected");
724   assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call &&
725          "Opertation of type 'call' expected");
726   for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) {
727     isl_ast_expr *SubExpr;
728     Value *V;
729 
730     SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1);
731     V = ExprBuilder.create(SubExpr);
732     ScalarEvolution *SE = Stmt->getParent()->getSE();
733     LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V);
734   }
735 
736   isl_ast_expr_free(Expr);
737 }
738 
739 void IslNodeBuilder::createSubstitutionsVector(
740     __isl_take isl_ast_expr *Expr, ScopStmt *Stmt,
741     std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS,
742     __isl_take isl_id *IteratorID) {
743   int i = 0;
744 
745   Value *OldValue = IDToValue[IteratorID];
746   for (Value *IV : IVS) {
747     IDToValue[IteratorID] = IV;
748     createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VLTS[i]);
749     i++;
750   }
751 
752   IDToValue[IteratorID] = OldValue;
753   isl_id_free(IteratorID);
754   isl_ast_expr_free(Expr);
755 }
756 
757 void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) {
758   LoopToScevMapT LTS;
759   isl_id *Id;
760   ScopStmt *Stmt;
761 
762   isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
763   isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
764   Id = isl_ast_expr_get_id(StmtExpr);
765   isl_ast_expr_free(StmtExpr);
766 
767   LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end());
768 
769   Stmt = (ScopStmt *)isl_id_get_user(Id);
770   auto *NewAccesses = createNewAccesses(Stmt, User);
771   createSubstitutions(Expr, Stmt, LTS);
772 
773   if (Stmt->isBlockStmt())
774     BlockGen.copyStmt(*Stmt, LTS, NewAccesses);
775   else
776     RegionGen.copyStmt(*Stmt, LTS, NewAccesses);
777 
778   isl_id_to_ast_expr_free(NewAccesses);
779   isl_ast_node_free(User);
780   isl_id_free(Id);
781 }
782 
783 void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) {
784   isl_ast_node_list *List = isl_ast_node_block_get_children(Block);
785 
786   for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
787     create(isl_ast_node_list_get_ast_node(List, i));
788 
789   isl_ast_node_free(Block);
790   isl_ast_node_list_free(List);
791 }
792 
793 void IslNodeBuilder::create(__isl_take isl_ast_node *Node) {
794   switch (isl_ast_node_get_type(Node)) {
795   case isl_ast_node_error:
796     llvm_unreachable("code generation error");
797   case isl_ast_node_mark:
798     createMark(Node);
799     return;
800   case isl_ast_node_for:
801     createFor(Node);
802     return;
803   case isl_ast_node_if:
804     createIf(Node);
805     return;
806   case isl_ast_node_user:
807     createUser(Node);
808     return;
809   case isl_ast_node_block:
810     createBlock(Node);
811     return;
812   }
813 
814   llvm_unreachable("Unknown isl_ast_node type");
815 }
816 
817 void IslNodeBuilder::materializeValue(isl_id *Id) {
818   Value *&V = IDToValue[Id];
819 
820   // If the Id is already mapped, skip it.
821   if (!V)
822     V = generateSCEV((const SCEV *)isl_id_get_user(Id));
823 
824   isl_id_free(Id);
825 }
826 
827 void IslNodeBuilder::materializeParameters(isl_set *Set, bool All) {
828   for (unsigned i = 0, e = isl_set_dim(Set, isl_dim_param); i < e; ++i) {
829     if (!All && !isl_set_involves_dims(Set, isl_dim_param, i, 1))
830       continue;
831     isl_id *Id = isl_set_get_dim_id(Set, isl_dim_param, i);
832     materializeValue(Id);
833   }
834 }
835 
836 /// @brief Create the actual preload memory access for @p MA.
837 static inline Value *createPreloadLoad(Scop &S, const MemoryAccess &MA,
838                                        isl_ast_build *Build,
839                                        IslExprBuilder &ExprBuilder) {
840   isl_set *AccessRange = isl_map_range(MA.getAccessRelation());
841   isl_pw_multi_aff *PWAccRel = isl_pw_multi_aff_from_set(AccessRange);
842   PWAccRel = isl_pw_multi_aff_gist_params(PWAccRel, S.getContext());
843   isl_ast_expr *Access =
844       isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
845   return ExprBuilder.create(Access);
846 }
847 
848 Value *IslNodeBuilder::preloadInvariantLoad(const MemoryAccess &MA,
849                                             isl_set *Domain,
850                                             isl_ast_build *Build) {
851 
852   isl_set *Universe = isl_set_universe(isl_set_get_space(Domain));
853   bool AlwaysExecuted = isl_set_is_equal(Domain, Universe);
854   isl_set_free(Universe);
855 
856   if (AlwaysExecuted) {
857     isl_set_free(Domain);
858     return createPreloadLoad(S, MA, Build, ExprBuilder);
859   } else {
860 
861     isl_ast_expr *DomainCond = isl_ast_build_expr_from_set(Build, Domain);
862 
863     Value *Cond = ExprBuilder.create(DomainCond);
864     if (!Cond->getType()->isIntegerTy(1))
865       Cond = Builder.CreateIsNotNull(Cond);
866 
867     BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(),
868                                     Builder.GetInsertPoint(), &DT, &LI);
869     CondBB->setName("polly.preload.cond");
870 
871     BasicBlock *MergeBB = SplitBlock(CondBB, CondBB->begin(), &DT, &LI);
872     MergeBB->setName("polly.preload.merge");
873 
874     Function *F = Builder.GetInsertBlock()->getParent();
875     LLVMContext &Context = F->getContext();
876     BasicBlock *ExecBB = BasicBlock::Create(Context, "polly.preload.exec", F);
877 
878     DT.addNewBlock(ExecBB, CondBB);
879     if (Loop *L = LI.getLoopFor(CondBB))
880       L->addBasicBlockToLoop(ExecBB, LI);
881 
882     auto *CondBBTerminator = CondBB->getTerminator();
883     Builder.SetInsertPoint(CondBBTerminator);
884     Builder.CreateCondBr(Cond, ExecBB, MergeBB);
885     CondBBTerminator->eraseFromParent();
886 
887     Builder.SetInsertPoint(ExecBB);
888     Builder.CreateBr(MergeBB);
889 
890     Builder.SetInsertPoint(ExecBB->getTerminator());
891     Instruction *AccInst = MA.getAccessInstruction();
892     Type *AccInstTy = AccInst->getType();
893     Value *PreAccInst = createPreloadLoad(S, MA, Build, ExprBuilder);
894 
895     Builder.SetInsertPoint(MergeBB->getTerminator());
896     auto *MergePHI = Builder.CreatePHI(
897         AccInstTy, 2, "polly.preload." + AccInst->getName() + ".merge");
898     MergePHI->addIncoming(PreAccInst, ExecBB);
899     MergePHI->addIncoming(Constant::getNullValue(AccInstTy), CondBB);
900 
901     return MergePHI;
902   }
903 }
904 
905 void IslNodeBuilder::preloadInvariantLoads() {
906 
907   const auto &InvAccList = S.getInvariantAccesses();
908   if (InvAccList.empty())
909     return;
910 
911   const Region &R = S.getRegion();
912   BasicBlock *EntryBB = &Builder.GetInsertBlock()->getParent()->getEntryBlock();
913 
914   BasicBlock *PreLoadBB =
915       SplitBlock(Builder.GetInsertBlock(), Builder.GetInsertPoint(), &DT, &LI);
916   PreLoadBB->setName("polly.preload.begin");
917   Builder.SetInsertPoint(PreLoadBB->begin());
918 
919   isl_ast_build *Build =
920       isl_ast_build_from_context(isl_set_universe(S.getParamSpace()));
921 
922   for (const auto &IA : InvAccList) {
923     MemoryAccess *MA = IA.first;
924     assert(!MA->isImplicit());
925 
926     isl_set *Domain = isl_set_copy(IA.second);
927     Instruction *AccInst = MA->getAccessInstruction();
928     Value *PreloadVal = preloadInvariantLoad(*MA, Domain, Build);
929     ValueMap[AccInst] = PreloadVal;
930 
931     if (SE.isSCEVable(AccInst->getType())) {
932       isl_id *ParamId = S.getIdForParam(SE.getSCEV(AccInst));
933       if (ParamId)
934         IDToValue[ParamId] = PreloadVal;
935       isl_id_free(ParamId);
936     }
937 
938     auto *SAI = S.getScopArrayInfo(MA->getBaseAddr());
939     for (auto *DerivedSAI : SAI->getDerivedSAIs())
940       DerivedSAI->setBasePtr(PreloadVal);
941 
942     // Use the escape system to get the correct value to users outside
943     // the SCoP.
944     BlockGenerator::EscapeUserVectorTy EscapeUsers;
945     for (auto *U : AccInst->users())
946       if (Instruction *UI = dyn_cast<Instruction>(U))
947         if (!R.contains(UI))
948           EscapeUsers.push_back(UI);
949 
950     if (EscapeUsers.empty())
951       continue;
952 
953     auto *Ty = AccInst->getType();
954     auto *Alloca = new AllocaInst(Ty, AccInst->getName() + ".preload.s2a");
955     Alloca->insertBefore(EntryBB->getFirstInsertionPt());
956     Builder.CreateStore(PreloadVal, Alloca);
957 
958     EscapeMap[AccInst] = std::make_pair(Alloca, std::move(EscapeUsers));
959   }
960 
961   isl_ast_build_free(Build);
962 }
963 
964 void IslNodeBuilder::addParameters(__isl_take isl_set *Context) {
965 
966   // Materialize values for the parameters of the SCoP.
967   materializeParameters(Context, /* all */ true);
968 
969   // Generate values for the current loop iteration for all surrounding loops.
970   //
971   // We may also reference loops outside of the scop which do not contain the
972   // scop itself, but as the number of such scops may be arbitrarily large we do
973   // not generate code for them here, but only at the point of code generation
974   // where these values are needed.
975   Region &R = S.getRegion();
976   Loop *L = LI.getLoopFor(R.getEntry());
977 
978   while (L != nullptr && R.contains(L))
979     L = L->getParentLoop();
980 
981   while (L != nullptr) {
982     const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
983                                             SE.getUnknown(Builder.getInt64(1)),
984                                             L, SCEV::FlagAnyWrap);
985     Value *V = generateSCEV(OuterLIV);
986     OutsideLoopIterations[L] = SE.getUnknown(V);
987     L = L->getParentLoop();
988   }
989 
990   isl_set_free(Context);
991 }
992 
993 Value *IslNodeBuilder::generateSCEV(const SCEV *Expr) {
994   Instruction *InsertLocation = --(Builder.GetInsertBlock()->end());
995   return expandCodeFor(S, SE, DL, "polly", Expr, Expr->getType(),
996                        InsertLocation);
997 }
998