1 //===- IslNodeBuilder.cpp - Translate an isl AST into a LLVM-IR AST -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the IslNodeBuilder, a class to translate an isl AST into
10 // a LLVM-IR AST.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "polly/CodeGen/IslNodeBuilder.h"
15 #include "polly/CodeGen/BlockGenerators.h"
16 #include "polly/CodeGen/CodeGeneration.h"
17 #include "polly/CodeGen/IslAst.h"
18 #include "polly/CodeGen/IslExprBuilder.h"
19 #include "polly/CodeGen/LoopGeneratorsGOMP.h"
20 #include "polly/CodeGen/LoopGeneratorsKMP.h"
21 #include "polly/CodeGen/RuntimeDebugBuilder.h"
22 #include "polly/Options.h"
23 #include "polly/ScopInfo.h"
24 #include "polly/Support/ISLTools.h"
25 #include "polly/Support/SCEVValidator.h"
26 #include "polly/Support/ScopHelper.h"
27 #include "llvm/ADT/APInt.h"
28 #include "llvm/ADT/PostOrderIterator.h"
29 #include "llvm/ADT/SetVector.h"
30 #include "llvm/ADT/SmallPtrSet.h"
31 #include "llvm/ADT/Statistic.h"
32 #include "llvm/Analysis/LoopInfo.h"
33 #include "llvm/Analysis/RegionInfo.h"
34 #include "llvm/Analysis/ScalarEvolution.h"
35 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
36 #include "llvm/IR/BasicBlock.h"
37 #include "llvm/IR/Constant.h"
38 #include "llvm/IR/Constants.h"
39 #include "llvm/IR/DataLayout.h"
40 #include "llvm/IR/DerivedTypes.h"
41 #include "llvm/IR/Dominators.h"
42 #include "llvm/IR/Function.h"
43 #include "llvm/IR/InstrTypes.h"
44 #include "llvm/IR/Instruction.h"
45 #include "llvm/IR/Instructions.h"
46 #include "llvm/IR/Type.h"
47 #include "llvm/IR/Value.h"
48 #include "llvm/Support/Casting.h"
49 #include "llvm/Support/CommandLine.h"
50 #include "llvm/Support/ErrorHandling.h"
51 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
52 #include "isl/aff.h"
53 #include "isl/aff_type.h"
54 #include "isl/ast.h"
55 #include "isl/ast_build.h"
56 #include "isl/isl-noexceptions.h"
57 #include "isl/map.h"
58 #include "isl/set.h"
59 #include "isl/union_map.h"
60 #include "isl/union_set.h"
61 #include "isl/val.h"
62 #include <algorithm>
63 #include <cassert>
64 #include <cstdint>
65 #include <cstring>
66 #include <string>
67 #include <utility>
68 #include <vector>
69 
70 using namespace llvm;
71 using namespace polly;
72 
73 #define DEBUG_TYPE "polly-codegen"
74 
75 STATISTIC(VersionedScops, "Number of SCoPs that required versioning.");
76 
77 STATISTIC(SequentialLoops, "Number of generated sequential for-loops");
78 STATISTIC(ParallelLoops, "Number of generated parallel for-loops");
79 STATISTIC(VectorLoops, "Number of generated vector for-loops");
80 STATISTIC(IfConditions, "Number of generated if-conditions");
81 
82 /// OpenMP backend options
83 enum class OpenMPBackend { GNU, LLVM };
84 
85 static cl::opt<bool> PollyGenerateRTCPrint(
86     "polly-codegen-emit-rtc-print",
87     cl::desc("Emit code that prints the runtime check result dynamically."),
88     cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
89 
90 // If this option is set we always use the isl AST generator to regenerate
91 // memory accesses. Without this option set we regenerate expressions using the
92 // original SCEV expressions and only generate new expressions in case the
93 // access relation has been changed and consequently must be regenerated.
94 static cl::opt<bool> PollyGenerateExpressions(
95     "polly-codegen-generate-expressions",
96     cl::desc("Generate AST expressions for unmodified and modified accesses"),
97     cl::Hidden, cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
98 
99 static cl::opt<int> PollyTargetFirstLevelCacheLineSize(
100     "polly-target-first-level-cache-line-size",
101     cl::desc("The size of the first level cache line size specified in bytes."),
102     cl::Hidden, cl::init(64), cl::ZeroOrMore, cl::cat(PollyCategory));
103 
104 static cl::opt<OpenMPBackend> PollyOmpBackend(
105     "polly-omp-backend", cl::desc("Choose the OpenMP library to use:"),
106     cl::values(clEnumValN(OpenMPBackend::GNU, "GNU", "GNU OpenMP"),
107                clEnumValN(OpenMPBackend::LLVM, "LLVM", "LLVM OpenMP")),
108     cl::Hidden, cl::init(OpenMPBackend::GNU), cl::cat(PollyCategory));
109 
110 isl::ast_expr IslNodeBuilder::getUpperBound(isl::ast_node For,
111                                             ICmpInst::Predicate &Predicate) {
112   isl::ast_expr Cond = For.for_get_cond();
113   isl::ast_expr Iterator = For.for_get_iterator();
114   assert(isl_ast_expr_get_type(Cond.get()) == isl_ast_expr_op &&
115          "conditional expression is not an atomic upper bound");
116 
117   isl_ast_op_type OpType = isl_ast_expr_get_op_type(Cond.get());
118 
119   switch (OpType) {
120   case isl_ast_op_le:
121     Predicate = ICmpInst::ICMP_SLE;
122     break;
123   case isl_ast_op_lt:
124     Predicate = ICmpInst::ICMP_SLT;
125     break;
126   default:
127     llvm_unreachable("Unexpected comparison type in loop condition");
128   }
129 
130   isl::ast_expr Arg0 = Cond.get_op_arg(0);
131 
132   assert(isl_ast_expr_get_type(Arg0.get()) == isl_ast_expr_id &&
133          "conditional expression is not an atomic upper bound");
134 
135   isl::id UBID = Arg0.get_id();
136 
137   assert(isl_ast_expr_get_type(Iterator.get()) == isl_ast_expr_id &&
138          "Could not get the iterator");
139 
140   isl::id IteratorID = Iterator.get_id();
141 
142   assert(UBID.get() == IteratorID.get() &&
143          "conditional expression is not an atomic upper bound");
144 
145   return Cond.get_op_arg(1);
146 }
147 
148 /// Return true if a return value of Predicate is true for the value represented
149 /// by passed isl_ast_expr_int.
150 static bool checkIslAstExprInt(__isl_take isl_ast_expr *Expr,
151                                isl_bool (*Predicate)(__isl_keep isl_val *)) {
152   if (isl_ast_expr_get_type(Expr) != isl_ast_expr_int) {
153     isl_ast_expr_free(Expr);
154     return false;
155   }
156   auto ExprVal = isl_ast_expr_get_val(Expr);
157   isl_ast_expr_free(Expr);
158   if (Predicate(ExprVal) != isl_bool_true) {
159     isl_val_free(ExprVal);
160     return false;
161   }
162   isl_val_free(ExprVal);
163   return true;
164 }
165 
166 int IslNodeBuilder::getNumberOfIterations(isl::ast_node For) {
167   assert(isl_ast_node_get_type(For.get()) == isl_ast_node_for);
168   isl::ast_node Body = For.for_get_body();
169 
170   // First, check if we can actually handle this code.
171   switch (isl_ast_node_get_type(Body.get())) {
172   case isl_ast_node_user:
173     break;
174   case isl_ast_node_block: {
175     isl::ast_node_list List = Body.block_get_children();
176     for (isl::ast_node Node : List) {
177       isl_ast_node_type NodeType = isl_ast_node_get_type(Node.get());
178       if (NodeType != isl_ast_node_user)
179         return -1;
180     }
181     break;
182   }
183   default:
184     return -1;
185   }
186 
187   isl::ast_expr Init = For.for_get_init();
188   if (!checkIslAstExprInt(Init.release(), isl_val_is_zero))
189     return -1;
190   isl::ast_expr Inc = For.for_get_inc();
191   if (!checkIslAstExprInt(Inc.release(), isl_val_is_one))
192     return -1;
193   CmpInst::Predicate Predicate;
194   isl::ast_expr UB = getUpperBound(For, Predicate);
195   if (isl_ast_expr_get_type(UB.get()) != isl_ast_expr_int)
196     return -1;
197   isl::val UpVal = UB.get_val();
198   int NumberIterations = UpVal.get_num_si();
199   if (NumberIterations < 0)
200     return -1;
201   if (Predicate == CmpInst::ICMP_SLT)
202     return NumberIterations;
203   else
204     return NumberIterations + 1;
205 }
206 
207 /// Extract the values and SCEVs needed to generate code for a block.
208 static int findReferencesInBlock(struct SubtreeReferences &References,
209                                  const ScopStmt *Stmt, BasicBlock *BB) {
210   for (Instruction &Inst : *BB) {
211     // Include invariant loads
212     if (isa<LoadInst>(Inst))
213       if (Value *InvariantLoad = References.GlobalMap.lookup(&Inst))
214         References.Values.insert(InvariantLoad);
215 
216     for (Value *SrcVal : Inst.operands()) {
217       auto *Scope = References.LI.getLoopFor(BB);
218       if (canSynthesize(SrcVal, References.S, &References.SE, Scope)) {
219         References.SCEVs.insert(References.SE.getSCEVAtScope(SrcVal, Scope));
220         continue;
221       } else if (Value *NewVal = References.GlobalMap.lookup(SrcVal))
222         References.Values.insert(NewVal);
223     }
224   }
225   return 0;
226 }
227 
228 void polly::addReferencesFromStmt(const ScopStmt *Stmt, void *UserPtr,
229                                   bool CreateScalarRefs) {
230   auto &References = *static_cast<struct SubtreeReferences *>(UserPtr);
231 
232   if (Stmt->isBlockStmt())
233     findReferencesInBlock(References, Stmt, Stmt->getBasicBlock());
234   else if (Stmt->isRegionStmt()) {
235     for (BasicBlock *BB : Stmt->getRegion()->blocks())
236       findReferencesInBlock(References, Stmt, BB);
237   } else {
238     assert(Stmt->isCopyStmt());
239     // Copy Stmts have no instructions that we need to consider.
240   }
241 
242   for (auto &Access : *Stmt) {
243     if (References.ParamSpace) {
244       isl::space ParamSpace = Access->getLatestAccessRelation().get_space();
245       (*References.ParamSpace) =
246           References.ParamSpace->align_params(ParamSpace);
247     }
248 
249     if (Access->isLatestArrayKind()) {
250       auto *BasePtr = Access->getLatestScopArrayInfo()->getBasePtr();
251       if (Instruction *OpInst = dyn_cast<Instruction>(BasePtr))
252         if (Stmt->getParent()->contains(OpInst))
253           continue;
254 
255       References.Values.insert(BasePtr);
256       continue;
257     }
258 
259     if (CreateScalarRefs)
260       References.Values.insert(References.BlockGen.getOrCreateAlloca(*Access));
261   }
262 }
263 
264 /// Extract the out-of-scop values and SCEVs referenced from a set describing
265 /// a ScopStmt.
266 ///
267 /// This includes the SCEVUnknowns referenced by the SCEVs used in the
268 /// statement and the base pointers of the memory accesses. For scalar
269 /// statements we force the generation of alloca memory locations and list
270 /// these locations in the set of out-of-scop values as well.
271 ///
272 /// @param Set     A set which references the ScopStmt we are interested in.
273 /// @param UserPtr A void pointer that can be casted to a SubtreeReferences
274 ///                structure.
275 static void addReferencesFromStmtSet(isl::set Set,
276                                      struct SubtreeReferences *UserPtr) {
277   isl::id Id = Set.get_tuple_id();
278   auto *Stmt = static_cast<const ScopStmt *>(Id.get_user());
279   return addReferencesFromStmt(Stmt, UserPtr);
280 }
281 
282 /// Extract the out-of-scop values and SCEVs referenced from a union set
283 /// referencing multiple ScopStmts.
284 ///
285 /// This includes the SCEVUnknowns referenced by the SCEVs used in the
286 /// statement and the base pointers of the memory accesses. For scalar
287 /// statements we force the generation of alloca memory locations and list
288 /// these locations in the set of out-of-scop values as well.
289 ///
290 /// @param USet       A union set referencing the ScopStmts we are interested
291 ///                   in.
292 /// @param References The SubtreeReferences data structure through which
293 ///                   results are returned and further information is
294 ///                   provided.
295 static void
296 addReferencesFromStmtUnionSet(isl::union_set USet,
297                               struct SubtreeReferences &References) {
298 
299   for (isl::set Set : USet.get_set_list())
300     addReferencesFromStmtSet(Set, &References);
301 }
302 
303 isl::union_map
304 IslNodeBuilder::getScheduleForAstNode(const isl::ast_node &Node) {
305   return IslAstInfo::getSchedule(Node);
306 }
307 
308 void IslNodeBuilder::getReferencesInSubtree(const isl::ast_node &For,
309                                             SetVector<Value *> &Values,
310                                             SetVector<const Loop *> &Loops) {
311   SetVector<const SCEV *> SCEVs;
312   struct SubtreeReferences References = {
313       LI, SE, S, ValueMap, Values, SCEVs, getBlockGenerator(), nullptr};
314 
315   for (const auto &I : IDToValue)
316     Values.insert(I.second);
317 
318   // NOTE: this is populated in IslNodeBuilder::addParameters
319   for (const auto &I : OutsideLoopIterations)
320     Values.insert(cast<SCEVUnknown>(I.second)->getValue());
321 
322   isl::union_set Schedule = getScheduleForAstNode(For).domain();
323   addReferencesFromStmtUnionSet(Schedule, References);
324 
325   for (const SCEV *Expr : SCEVs) {
326     findValues(Expr, SE, Values);
327     findLoops(Expr, Loops);
328   }
329 
330   Values.remove_if([](const Value *V) { return isa<GlobalValue>(V); });
331 
332   /// Note: Code generation of induction variables of loops outside Scops
333   ///
334   /// Remove loops that contain the scop or that are part of the scop, as they
335   /// are considered local. This leaves only loops that are before the scop, but
336   /// do not contain the scop itself.
337   /// We ignore loops perfectly contained in the Scop because these are already
338   /// generated at `IslNodeBuilder::addParameters`. These `Loops` are loops
339   /// whose induction variables are referred to by the Scop, but the Scop is not
340   /// fully contained in these Loops. Since there can be many of these,
341   /// we choose to codegen these on-demand.
342   /// @see IslNodeBuilder::materializeNonScopLoopInductionVariable.
343   Loops.remove_if([this](const Loop *L) {
344     return S.contains(L) || L->contains(S.getEntry());
345   });
346 
347   // Contains Values that may need to be replaced with other values
348   // due to replacements from the ValueMap. We should make sure
349   // that we return correctly remapped values.
350   // NOTE: this code path is tested by:
351   //     1.  test/Isl/CodeGen/OpenMP/single_loop_with_loop_invariant_baseptr.ll
352   //     2.  test/Isl/CodeGen/OpenMP/loop-body-references-outer-values-3.ll
353   SetVector<Value *> ReplacedValues;
354   for (Value *V : Values) {
355     ReplacedValues.insert(getLatestValue(V));
356   }
357   Values = ReplacedValues;
358 }
359 
360 void IslNodeBuilder::updateValues(ValueMapT &NewValues) {
361   SmallPtrSet<Value *, 5> Inserted;
362 
363   for (const auto &I : IDToValue) {
364     IDToValue[I.first] = NewValues[I.second];
365     Inserted.insert(I.second);
366   }
367 
368   for (const auto &I : NewValues) {
369     if (Inserted.count(I.first))
370       continue;
371 
372     ValueMap[I.first] = I.second;
373   }
374 }
375 
376 Value *IslNodeBuilder::getLatestValue(Value *Original) const {
377   auto It = ValueMap.find(Original);
378   if (It == ValueMap.end())
379     return Original;
380   return It->second;
381 }
382 
383 void IslNodeBuilder::createUserVector(__isl_take isl_ast_node *User,
384                                       std::vector<Value *> &IVS,
385                                       __isl_take isl_id *IteratorID,
386                                       __isl_take isl_union_map *Schedule) {
387   isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
388   isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
389   isl_id *Id = isl_ast_expr_get_id(StmtExpr);
390   isl_ast_expr_free(StmtExpr);
391   ScopStmt *Stmt = (ScopStmt *)isl_id_get_user(Id);
392   std::vector<LoopToScevMapT> VLTS(IVS.size());
393 
394   isl_union_set *Domain = isl_union_set_from_set(Stmt->getDomain().release());
395   Schedule = isl_union_map_intersect_domain(Schedule, Domain);
396   isl_map *S = isl_map_from_union_map(Schedule);
397 
398   auto *NewAccesses = createNewAccesses(Stmt, User);
399   createSubstitutionsVector(Expr, Stmt, VLTS, IVS, IteratorID);
400   VectorBlockGenerator::generate(BlockGen, *Stmt, VLTS, S, NewAccesses);
401   isl_id_to_ast_expr_free(NewAccesses);
402   isl_map_free(S);
403   isl_id_free(Id);
404   isl_ast_node_free(User);
405 }
406 
407 void IslNodeBuilder::createMark(__isl_take isl_ast_node *Node) {
408   auto *Id = isl_ast_node_mark_get_id(Node);
409   auto Child = isl_ast_node_mark_get_node(Node);
410   isl_ast_node_free(Node);
411   // If a child node of a 'SIMD mark' is a loop that has a single iteration,
412   // it will be optimized away and we should skip it.
413   if (strcmp(isl_id_get_name(Id), "SIMD") == 0 &&
414       isl_ast_node_get_type(Child) == isl_ast_node_for) {
415     bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY;
416     int VectorWidth = getNumberOfIterations(isl::manage_copy(Child));
417     if (Vector && 1 < VectorWidth && VectorWidth <= 16)
418       createForVector(Child, VectorWidth);
419     else
420       createForSequential(isl::manage(Child), true);
421     isl_id_free(Id);
422     return;
423   }
424   if (strcmp(isl_id_get_name(Id), "Inter iteration alias-free") == 0) {
425     auto *BasePtr = static_cast<Value *>(isl_id_get_user(Id));
426     Annotator.addInterIterationAliasFreeBasePtr(BasePtr);
427   }
428 
429   BandAttr *ChildLoopAttr = getLoopAttr(isl::manage_copy(Id));
430   BandAttr *AncestorLoopAttr;
431   if (ChildLoopAttr) {
432     // Save current LoopAttr environment to restore again when leaving this
433     // subtree. This means there was no loop between the ancestor LoopAttr and
434     // this mark, i.e. the ancestor LoopAttr did not directly mark a loop. This
435     // can happen e.g. if the AST build peeled or unrolled the loop.
436     AncestorLoopAttr = Annotator.getStagingAttrEnv();
437 
438     Annotator.getStagingAttrEnv() = ChildLoopAttr;
439   }
440 
441   create(Child);
442 
443   if (ChildLoopAttr) {
444     assert(Annotator.getStagingAttrEnv() == ChildLoopAttr &&
445            "Nest must not overwrite loop attr environment");
446     Annotator.getStagingAttrEnv() = AncestorLoopAttr;
447   }
448 
449   isl_id_free(Id);
450 }
451 
452 void IslNodeBuilder::createForVector(__isl_take isl_ast_node *For,
453                                      int VectorWidth) {
454   isl_ast_node *Body = isl_ast_node_for_get_body(For);
455   isl_ast_expr *Init = isl_ast_node_for_get_init(For);
456   isl_ast_expr *Inc = isl_ast_node_for_get_inc(For);
457   isl_ast_expr *Iterator = isl_ast_node_for_get_iterator(For);
458   isl_id *IteratorID = isl_ast_expr_get_id(Iterator);
459 
460   Value *ValueLB = ExprBuilder.create(Init);
461   Value *ValueInc = ExprBuilder.create(Inc);
462 
463   Type *MaxType = ExprBuilder.getType(Iterator);
464   MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
465   MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
466 
467   if (MaxType != ValueLB->getType())
468     ValueLB = Builder.CreateSExt(ValueLB, MaxType);
469   if (MaxType != ValueInc->getType())
470     ValueInc = Builder.CreateSExt(ValueInc, MaxType);
471 
472   std::vector<Value *> IVS(VectorWidth);
473   IVS[0] = ValueLB;
474 
475   for (int i = 1; i < VectorWidth; i++)
476     IVS[i] = Builder.CreateAdd(IVS[i - 1], ValueInc, "p_vector_iv");
477 
478   isl::union_map Schedule = getScheduleForAstNode(isl::manage_copy(For));
479   assert(!Schedule.is_null() &&
480          "For statement annotation does not contain its schedule");
481 
482   IDToValue[IteratorID] = ValueLB;
483 
484   switch (isl_ast_node_get_type(Body)) {
485   case isl_ast_node_user:
486     createUserVector(Body, IVS, isl_id_copy(IteratorID), Schedule.copy());
487     break;
488   case isl_ast_node_block: {
489     isl_ast_node_list *List = isl_ast_node_block_get_children(Body);
490 
491     for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
492       createUserVector(isl_ast_node_list_get_ast_node(List, i), IVS,
493                        isl_id_copy(IteratorID), Schedule.copy());
494 
495     isl_ast_node_free(Body);
496     isl_ast_node_list_free(List);
497     break;
498   }
499   default:
500     isl_ast_node_dump(Body);
501     llvm_unreachable("Unhandled isl_ast_node in vectorizer");
502   }
503 
504   IDToValue.erase(IDToValue.find(IteratorID));
505   isl_id_free(IteratorID);
506 
507   isl_ast_node_free(For);
508   isl_ast_expr_free(Iterator);
509 
510   VectorLoops++;
511 }
512 
513 /// Restore the initial ordering of dimensions of the band node
514 ///
515 /// In case the band node represents all the dimensions of the iteration
516 /// domain, recreate the band node to restore the initial ordering of the
517 /// dimensions.
518 ///
519 /// @param Node The band node to be modified.
520 /// @return The modified schedule node.
521 static bool IsLoopVectorizerDisabled(isl::ast_node Node) {
522   assert(isl_ast_node_get_type(Node.get()) == isl_ast_node_for);
523   auto Body = Node.for_get_body();
524   if (isl_ast_node_get_type(Body.get()) != isl_ast_node_mark)
525     return false;
526   auto Id = Body.mark_get_id();
527   if (strcmp(Id.get_name().c_str(), "Loop Vectorizer Disabled") == 0)
528     return true;
529   return false;
530 }
531 
532 void IslNodeBuilder::createForSequential(isl::ast_node For, bool MarkParallel) {
533   Value *ValueLB, *ValueUB, *ValueInc;
534   Type *MaxType;
535   BasicBlock *ExitBlock;
536   Value *IV;
537   CmpInst::Predicate Predicate;
538 
539   bool LoopVectorizerDisabled = IsLoopVectorizerDisabled(For);
540 
541   isl::ast_node Body = For.for_get_body();
542 
543   // isl_ast_node_for_is_degenerate(For)
544   //
545   // TODO: For degenerated loops we could generate a plain assignment.
546   //       However, for now we just reuse the logic for normal loops, which will
547   //       create a loop with a single iteration.
548 
549   isl::ast_expr Init = For.for_get_init();
550   isl::ast_expr Inc = For.for_get_inc();
551   isl::ast_expr Iterator = For.for_get_iterator();
552   isl::id IteratorID = Iterator.get_id();
553   isl::ast_expr UB = getUpperBound(For, Predicate);
554 
555   ValueLB = ExprBuilder.create(Init.release());
556   ValueUB = ExprBuilder.create(UB.release());
557   ValueInc = ExprBuilder.create(Inc.release());
558 
559   MaxType = ExprBuilder.getType(Iterator.get());
560   MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
561   MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
562   MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
563 
564   if (MaxType != ValueLB->getType())
565     ValueLB = Builder.CreateSExt(ValueLB, MaxType);
566   if (MaxType != ValueUB->getType())
567     ValueUB = Builder.CreateSExt(ValueUB, MaxType);
568   if (MaxType != ValueInc->getType())
569     ValueInc = Builder.CreateSExt(ValueInc, MaxType);
570 
571   // If we can show that LB <Predicate> UB holds at least once, we can
572   // omit the GuardBB in front of the loop.
573   bool UseGuardBB =
574       !SE.isKnownPredicate(Predicate, SE.getSCEV(ValueLB), SE.getSCEV(ValueUB));
575   IV = createLoop(ValueLB, ValueUB, ValueInc, Builder, LI, DT, ExitBlock,
576                   Predicate, &Annotator, MarkParallel, UseGuardBB,
577                   LoopVectorizerDisabled);
578   IDToValue[IteratorID.get()] = IV;
579 
580   create(Body.release());
581 
582   Annotator.popLoop(MarkParallel);
583 
584   IDToValue.erase(IDToValue.find(IteratorID.get()));
585 
586   Builder.SetInsertPoint(&ExitBlock->front());
587 
588   SequentialLoops++;
589 }
590 
591 /// Remove the BBs contained in a (sub)function from the dominator tree.
592 ///
593 /// This function removes the basic blocks that are part of a subfunction from
594 /// the dominator tree. Specifically, when generating code it may happen that at
595 /// some point the code generation continues in a new sub-function (e.g., when
596 /// generating OpenMP code). The basic blocks that are created in this
597 /// sub-function are then still part of the dominator tree of the original
598 /// function, such that the dominator tree reaches over function boundaries.
599 /// This is not only incorrect, but also causes crashes. This function now
600 /// removes from the dominator tree all basic blocks that are dominated (and
601 /// consequently reachable) from the entry block of this (sub)function.
602 ///
603 /// FIXME: A LLVM (function or region) pass should not touch anything outside of
604 /// the function/region it runs on. Hence, the pure need for this function shows
605 /// that we do not comply to this rule. At the moment, this does not cause any
606 /// issues, but we should be aware that such issues may appear. Unfortunately
607 /// the current LLVM pass infrastructure does not allow to make Polly a module
608 /// or call-graph pass to solve this issue, as such a pass would not have access
609 /// to the per-function analyses passes needed by Polly. A future pass manager
610 /// infrastructure is supposed to enable such kind of access possibly allowing
611 /// us to create a cleaner solution here.
612 ///
613 /// FIXME: Instead of adding the dominance information and then dropping it
614 /// later on, we should try to just not add it in the first place. This requires
615 /// some careful testing to make sure this does not break in interaction with
616 /// the SCEVBuilder and SplitBlock which may rely on the dominator tree or
617 /// which may try to update it.
618 ///
619 /// @param F The function which contains the BBs to removed.
620 /// @param DT The dominator tree from which to remove the BBs.
621 static void removeSubFuncFromDomTree(Function *F, DominatorTree &DT) {
622   DomTreeNode *N = DT.getNode(&F->getEntryBlock());
623   std::vector<BasicBlock *> Nodes;
624 
625   // We can only remove an element from the dominator tree, if all its children
626   // have been removed. To ensure this we obtain the list of nodes to remove
627   // using a post-order tree traversal.
628   for (po_iterator<DomTreeNode *> I = po_begin(N), E = po_end(N); I != E; ++I)
629     Nodes.push_back(I->getBlock());
630 
631   for (BasicBlock *BB : Nodes)
632     DT.eraseNode(BB);
633 }
634 
635 void IslNodeBuilder::createForParallel(__isl_take isl_ast_node *For) {
636   isl_ast_node *Body;
637   isl_ast_expr *Init, *Inc, *Iterator, *UB;
638   isl_id *IteratorID;
639   Value *ValueLB, *ValueUB, *ValueInc;
640   Type *MaxType;
641   Value *IV;
642   CmpInst::Predicate Predicate;
643 
644   // The preamble of parallel code interacts different than normal code with
645   // e.g., scalar initialization. Therefore, we ensure the parallel code is
646   // separated from the last basic block.
647   BasicBlock *ParBB = SplitBlock(Builder.GetInsertBlock(),
648                                  &*Builder.GetInsertPoint(), &DT, &LI);
649   ParBB->setName("polly.parallel.for");
650   Builder.SetInsertPoint(&ParBB->front());
651 
652   Body = isl_ast_node_for_get_body(For);
653   Init = isl_ast_node_for_get_init(For);
654   Inc = isl_ast_node_for_get_inc(For);
655   Iterator = isl_ast_node_for_get_iterator(For);
656   IteratorID = isl_ast_expr_get_id(Iterator);
657   UB = getUpperBound(isl::manage_copy(For), Predicate).release();
658 
659   ValueLB = ExprBuilder.create(Init);
660   ValueUB = ExprBuilder.create(UB);
661   ValueInc = ExprBuilder.create(Inc);
662 
663   // OpenMP always uses SLE. In case the isl generated AST uses a SLT
664   // expression, we need to adjust the loop bound by one.
665   if (Predicate == CmpInst::ICMP_SLT)
666     ValueUB = Builder.CreateAdd(
667         ValueUB, Builder.CreateSExt(Builder.getTrue(), ValueUB->getType()));
668 
669   MaxType = ExprBuilder.getType(Iterator);
670   MaxType = ExprBuilder.getWidestType(MaxType, ValueLB->getType());
671   MaxType = ExprBuilder.getWidestType(MaxType, ValueUB->getType());
672   MaxType = ExprBuilder.getWidestType(MaxType, ValueInc->getType());
673 
674   if (MaxType != ValueLB->getType())
675     ValueLB = Builder.CreateSExt(ValueLB, MaxType);
676   if (MaxType != ValueUB->getType())
677     ValueUB = Builder.CreateSExt(ValueUB, MaxType);
678   if (MaxType != ValueInc->getType())
679     ValueInc = Builder.CreateSExt(ValueInc, MaxType);
680 
681   BasicBlock::iterator LoopBody;
682 
683   SetVector<Value *> SubtreeValues;
684   SetVector<const Loop *> Loops;
685 
686   getReferencesInSubtree(isl::manage_copy(For), SubtreeValues, Loops);
687 
688   // Create for all loops we depend on values that contain the current loop
689   // iteration. These values are necessary to generate code for SCEVs that
690   // depend on such loops. As a result we need to pass them to the subfunction.
691   // See [Code generation of induction variables of loops outside Scops]
692   for (const Loop *L : Loops) {
693     Value *LoopInductionVar = materializeNonScopLoopInductionVariable(L);
694     SubtreeValues.insert(LoopInductionVar);
695   }
696 
697   ValueMapT NewValues;
698 
699   std::unique_ptr<ParallelLoopGenerator> ParallelLoopGenPtr;
700 
701   switch (PollyOmpBackend) {
702   case OpenMPBackend::GNU:
703     ParallelLoopGenPtr.reset(
704         new ParallelLoopGeneratorGOMP(Builder, LI, DT, DL));
705     break;
706   case OpenMPBackend::LLVM:
707     ParallelLoopGenPtr.reset(new ParallelLoopGeneratorKMP(Builder, LI, DT, DL));
708     break;
709   }
710 
711   IV = ParallelLoopGenPtr->createParallelLoop(
712       ValueLB, ValueUB, ValueInc, SubtreeValues, NewValues, &LoopBody);
713   BasicBlock::iterator AfterLoop = Builder.GetInsertPoint();
714   Builder.SetInsertPoint(&*LoopBody);
715 
716   // Remember the parallel subfunction
717   ParallelSubfunctions.push_back(LoopBody->getFunction());
718 
719   // Save the current values.
720   auto ValueMapCopy = ValueMap;
721   IslExprBuilder::IDToValueTy IDToValueCopy = IDToValue;
722 
723   updateValues(NewValues);
724   IDToValue[IteratorID] = IV;
725 
726   ValueMapT NewValuesReverse;
727 
728   for (auto P : NewValues)
729     NewValuesReverse[P.second] = P.first;
730 
731   Annotator.addAlternativeAliasBases(NewValuesReverse);
732 
733   create(Body);
734 
735   Annotator.resetAlternativeAliasBases();
736   // Restore the original values.
737   ValueMap = ValueMapCopy;
738   IDToValue = IDToValueCopy;
739 
740   Builder.SetInsertPoint(&*AfterLoop);
741   removeSubFuncFromDomTree((*LoopBody).getParent()->getParent(), DT);
742 
743   for (const Loop *L : Loops)
744     OutsideLoopIterations.erase(L);
745 
746   isl_ast_node_free(For);
747   isl_ast_expr_free(Iterator);
748   isl_id_free(IteratorID);
749 
750   ParallelLoops++;
751 }
752 
753 /// Return whether any of @p Node's statements contain partial accesses.
754 ///
755 /// Partial accesses are not supported by Polly's vector code generator.
756 static bool hasPartialAccesses(__isl_take isl_ast_node *Node) {
757   return isl_ast_node_foreach_descendant_top_down(
758              Node,
759              [](isl_ast_node *Node, void *User) -> isl_bool {
760                if (isl_ast_node_get_type(Node) != isl_ast_node_user)
761                  return isl_bool_true;
762 
763                isl::ast_expr Expr =
764                    isl::manage(isl_ast_node_user_get_expr(Node));
765                isl::ast_expr StmtExpr = Expr.get_op_arg(0);
766                isl::id Id = StmtExpr.get_id();
767 
768                ScopStmt *Stmt =
769                    static_cast<ScopStmt *>(isl_id_get_user(Id.get()));
770                isl::set StmtDom = Stmt->getDomain();
771                for (auto *MA : *Stmt) {
772                  if (MA->isLatestPartialAccess())
773                    return isl_bool_error;
774                }
775                return isl_bool_true;
776              },
777              nullptr) == isl_stat_error;
778 }
779 
780 void IslNodeBuilder::createFor(__isl_take isl_ast_node *For) {
781   bool Vector = PollyVectorizerChoice == VECTORIZER_POLLY;
782 
783   if (Vector && IslAstInfo::isInnermostParallel(isl::manage_copy(For)) &&
784       !IslAstInfo::isReductionParallel(isl::manage_copy(For))) {
785     int VectorWidth = getNumberOfIterations(isl::manage_copy(For));
786     if (1 < VectorWidth && VectorWidth <= 16 && !hasPartialAccesses(For)) {
787       createForVector(For, VectorWidth);
788       return;
789     }
790   }
791 
792   if (IslAstInfo::isExecutedInParallel(isl::manage_copy(For))) {
793     createForParallel(For);
794     return;
795   }
796   bool Parallel = (IslAstInfo::isParallel(isl::manage_copy(For)) &&
797                    !IslAstInfo::isReductionParallel(isl::manage_copy(For)));
798   createForSequential(isl::manage(For), Parallel);
799 }
800 
801 void IslNodeBuilder::createIf(__isl_take isl_ast_node *If) {
802   isl_ast_expr *Cond = isl_ast_node_if_get_cond(If);
803 
804   Function *F = Builder.GetInsertBlock()->getParent();
805   LLVMContext &Context = F->getContext();
806 
807   BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(),
808                                   &*Builder.GetInsertPoint(), &DT, &LI);
809   CondBB->setName("polly.cond");
810   BasicBlock *MergeBB = SplitBlock(CondBB, &CondBB->front(), &DT, &LI);
811   MergeBB->setName("polly.merge");
812   BasicBlock *ThenBB = BasicBlock::Create(Context, "polly.then", F);
813   BasicBlock *ElseBB = BasicBlock::Create(Context, "polly.else", F);
814 
815   DT.addNewBlock(ThenBB, CondBB);
816   DT.addNewBlock(ElseBB, CondBB);
817   DT.changeImmediateDominator(MergeBB, CondBB);
818 
819   Loop *L = LI.getLoopFor(CondBB);
820   if (L) {
821     L->addBasicBlockToLoop(ThenBB, LI);
822     L->addBasicBlockToLoop(ElseBB, LI);
823   }
824 
825   CondBB->getTerminator()->eraseFromParent();
826 
827   Builder.SetInsertPoint(CondBB);
828   Value *Predicate = ExprBuilder.create(Cond);
829   Builder.CreateCondBr(Predicate, ThenBB, ElseBB);
830   Builder.SetInsertPoint(ThenBB);
831   Builder.CreateBr(MergeBB);
832   Builder.SetInsertPoint(ElseBB);
833   Builder.CreateBr(MergeBB);
834   Builder.SetInsertPoint(&ThenBB->front());
835 
836   create(isl_ast_node_if_get_then(If));
837 
838   Builder.SetInsertPoint(&ElseBB->front());
839 
840   if (isl_ast_node_if_has_else(If))
841     create(isl_ast_node_if_get_else(If));
842 
843   Builder.SetInsertPoint(&MergeBB->front());
844 
845   isl_ast_node_free(If);
846 
847   IfConditions++;
848 }
849 
850 __isl_give isl_id_to_ast_expr *
851 IslNodeBuilder::createNewAccesses(ScopStmt *Stmt,
852                                   __isl_keep isl_ast_node *Node) {
853   isl_id_to_ast_expr *NewAccesses =
854       isl_id_to_ast_expr_alloc(Stmt->getParent()->getIslCtx().get(), 0);
855 
856   auto *Build = IslAstInfo::getBuild(Node);
857   assert(Build && "Could not obtain isl_ast_build from user node");
858   Stmt->setAstBuild(isl::manage_copy(Build));
859 
860   for (auto *MA : *Stmt) {
861     if (!MA->hasNewAccessRelation()) {
862       if (PollyGenerateExpressions) {
863         if (!MA->isAffine())
864           continue;
865         if (MA->getLatestScopArrayInfo()->getBasePtrOriginSAI())
866           continue;
867 
868         auto *BasePtr =
869             dyn_cast<Instruction>(MA->getLatestScopArrayInfo()->getBasePtr());
870         if (BasePtr && Stmt->getParent()->getRegion().contains(BasePtr))
871           continue;
872       } else {
873         continue;
874       }
875     }
876     assert(MA->isAffine() &&
877            "Only affine memory accesses can be code generated");
878 
879     auto Schedule = isl_ast_build_get_schedule(Build);
880 
881 #ifndef NDEBUG
882     if (MA->isRead()) {
883       auto Dom = Stmt->getDomain().release();
884       auto SchedDom = isl_set_from_union_set(
885           isl_union_map_domain(isl_union_map_copy(Schedule)));
886       auto AccDom = isl_map_domain(MA->getAccessRelation().release());
887       Dom = isl_set_intersect_params(Dom,
888                                      Stmt->getParent()->getContext().release());
889       SchedDom = isl_set_intersect_params(
890           SchedDom, Stmt->getParent()->getContext().release());
891       assert(isl_set_is_subset(SchedDom, AccDom) &&
892              "Access relation not defined on full schedule domain");
893       assert(isl_set_is_subset(Dom, AccDom) &&
894              "Access relation not defined on full domain");
895       isl_set_free(AccDom);
896       isl_set_free(SchedDom);
897       isl_set_free(Dom);
898     }
899 #endif
900 
901     auto PWAccRel =
902         MA->applyScheduleToAccessRelation(isl::manage(Schedule)).release();
903 
904     // isl cannot generate an index expression for access-nothing accesses.
905     isl::set AccDomain =
906         isl::manage(isl_pw_multi_aff_domain(isl_pw_multi_aff_copy(PWAccRel)));
907     isl::set Context = S.getContext();
908     AccDomain = AccDomain.intersect_params(Context);
909     if (AccDomain.is_empty()) {
910       isl_pw_multi_aff_free(PWAccRel);
911       continue;
912     }
913 
914     auto AccessExpr = isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
915     NewAccesses =
916         isl_id_to_ast_expr_set(NewAccesses, MA->getId().release(), AccessExpr);
917   }
918 
919   return NewAccesses;
920 }
921 
922 void IslNodeBuilder::createSubstitutions(__isl_take isl_ast_expr *Expr,
923                                          ScopStmt *Stmt, LoopToScevMapT &LTS) {
924   assert(isl_ast_expr_get_type(Expr) == isl_ast_expr_op &&
925          "Expression of type 'op' expected");
926   assert(isl_ast_expr_get_op_type(Expr) == isl_ast_op_call &&
927          "Operation of type 'call' expected");
928   for (int i = 0; i < isl_ast_expr_get_op_n_arg(Expr) - 1; ++i) {
929     isl_ast_expr *SubExpr;
930     Value *V;
931 
932     SubExpr = isl_ast_expr_get_op_arg(Expr, i + 1);
933     V = ExprBuilder.create(SubExpr);
934     ScalarEvolution *SE = Stmt->getParent()->getSE();
935     LTS[Stmt->getLoopForDimension(i)] = SE->getUnknown(V);
936   }
937 
938   isl_ast_expr_free(Expr);
939 }
940 
941 void IslNodeBuilder::createSubstitutionsVector(
942     __isl_take isl_ast_expr *Expr, ScopStmt *Stmt,
943     std::vector<LoopToScevMapT> &VLTS, std::vector<Value *> &IVS,
944     __isl_take isl_id *IteratorID) {
945   int i = 0;
946 
947   Value *OldValue = IDToValue[IteratorID];
948   for (Value *IV : IVS) {
949     IDToValue[IteratorID] = IV;
950     createSubstitutions(isl_ast_expr_copy(Expr), Stmt, VLTS[i]);
951     i++;
952   }
953 
954   IDToValue[IteratorID] = OldValue;
955   isl_id_free(IteratorID);
956   isl_ast_expr_free(Expr);
957 }
958 
959 void IslNodeBuilder::generateCopyStmt(
960     ScopStmt *Stmt, __isl_keep isl_id_to_ast_expr *NewAccesses) {
961   assert(Stmt->size() == 2);
962   auto ReadAccess = Stmt->begin();
963   auto WriteAccess = ReadAccess++;
964   assert((*ReadAccess)->isRead() && (*WriteAccess)->isMustWrite());
965   assert((*ReadAccess)->getElementType() == (*WriteAccess)->getElementType() &&
966          "Accesses use the same data type");
967   assert((*ReadAccess)->isArrayKind() && (*WriteAccess)->isArrayKind());
968   auto *AccessExpr =
969       isl_id_to_ast_expr_get(NewAccesses, (*ReadAccess)->getId().release());
970   auto *LoadValue = ExprBuilder.create(AccessExpr);
971   AccessExpr =
972       isl_id_to_ast_expr_get(NewAccesses, (*WriteAccess)->getId().release());
973   auto *StoreAddr = ExprBuilder.createAccessAddress(AccessExpr).first;
974   Builder.CreateStore(LoadValue, StoreAddr);
975 }
976 
977 Value *IslNodeBuilder::materializeNonScopLoopInductionVariable(const Loop *L) {
978   assert(OutsideLoopIterations.find(L) == OutsideLoopIterations.end() &&
979          "trying to materialize loop induction variable twice");
980   const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
981                                           SE.getUnknown(Builder.getInt64(1)), L,
982                                           SCEV::FlagAnyWrap);
983   Value *V = generateSCEV(OuterLIV);
984   OutsideLoopIterations[L] = SE.getUnknown(V);
985   return V;
986 }
987 
988 void IslNodeBuilder::createUser(__isl_take isl_ast_node *User) {
989   LoopToScevMapT LTS;
990   isl_id *Id;
991   ScopStmt *Stmt;
992 
993   isl_ast_expr *Expr = isl_ast_node_user_get_expr(User);
994   isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
995   Id = isl_ast_expr_get_id(StmtExpr);
996   isl_ast_expr_free(StmtExpr);
997 
998   LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end());
999 
1000   Stmt = (ScopStmt *)isl_id_get_user(Id);
1001   auto *NewAccesses = createNewAccesses(Stmt, User);
1002   if (Stmt->isCopyStmt()) {
1003     generateCopyStmt(Stmt, NewAccesses);
1004     isl_ast_expr_free(Expr);
1005   } else {
1006     createSubstitutions(Expr, Stmt, LTS);
1007 
1008     if (Stmt->isBlockStmt())
1009       BlockGen.copyStmt(*Stmt, LTS, NewAccesses);
1010     else
1011       RegionGen.copyStmt(*Stmt, LTS, NewAccesses);
1012   }
1013 
1014   isl_id_to_ast_expr_free(NewAccesses);
1015   isl_ast_node_free(User);
1016   isl_id_free(Id);
1017 }
1018 
1019 void IslNodeBuilder::createBlock(__isl_take isl_ast_node *Block) {
1020   isl_ast_node_list *List = isl_ast_node_block_get_children(Block);
1021 
1022   for (int i = 0; i < isl_ast_node_list_n_ast_node(List); ++i)
1023     create(isl_ast_node_list_get_ast_node(List, i));
1024 
1025   isl_ast_node_free(Block);
1026   isl_ast_node_list_free(List);
1027 }
1028 
1029 void IslNodeBuilder::create(__isl_take isl_ast_node *Node) {
1030   switch (isl_ast_node_get_type(Node)) {
1031   case isl_ast_node_error:
1032     llvm_unreachable("code generation error");
1033   case isl_ast_node_mark:
1034     createMark(Node);
1035     return;
1036   case isl_ast_node_for:
1037     createFor(Node);
1038     return;
1039   case isl_ast_node_if:
1040     createIf(Node);
1041     return;
1042   case isl_ast_node_user:
1043     createUser(Node);
1044     return;
1045   case isl_ast_node_block:
1046     createBlock(Node);
1047     return;
1048   }
1049 
1050   llvm_unreachable("Unknown isl_ast_node type");
1051 }
1052 
1053 bool IslNodeBuilder::materializeValue(isl_id *Id) {
1054   // If the Id is already mapped, skip it.
1055   if (!IDToValue.count(Id)) {
1056     auto *ParamSCEV = (const SCEV *)isl_id_get_user(Id);
1057     Value *V = nullptr;
1058 
1059     // Parameters could refer to invariant loads that need to be
1060     // preloaded before we can generate code for the parameter. Thus,
1061     // check if any value referred to in ParamSCEV is an invariant load
1062     // and if so make sure its equivalence class is preloaded.
1063     SetVector<Value *> Values;
1064     findValues(ParamSCEV, SE, Values);
1065     for (auto *Val : Values) {
1066       // Check if the value is an instruction in a dead block within the SCoP
1067       // and if so do not code generate it.
1068       if (auto *Inst = dyn_cast<Instruction>(Val)) {
1069         if (S.contains(Inst)) {
1070           bool IsDead = true;
1071 
1072           // Check for "undef" loads first, then if there is a statement for
1073           // the parent of Inst and lastly if the parent of Inst has an empty
1074           // domain. In the first and last case the instruction is dead but if
1075           // there is a statement or the domain is not empty Inst is not dead.
1076           auto MemInst = MemAccInst::dyn_cast(Inst);
1077           auto Address = MemInst ? MemInst.getPointerOperand() : nullptr;
1078           if (Address && SE.getUnknown(UndefValue::get(Address->getType())) ==
1079                              SE.getPointerBase(SE.getSCEV(Address))) {
1080           } else if (S.getStmtFor(Inst)) {
1081             IsDead = false;
1082           } else {
1083             auto *Domain = S.getDomainConditions(Inst->getParent()).release();
1084             IsDead = isl_set_is_empty(Domain);
1085             isl_set_free(Domain);
1086           }
1087 
1088           if (IsDead) {
1089             V = UndefValue::get(ParamSCEV->getType());
1090             break;
1091           }
1092         }
1093       }
1094 
1095       if (auto *IAClass = S.lookupInvariantEquivClass(Val)) {
1096         // Check if this invariant access class is empty, hence if we never
1097         // actually added a loads instruction to it. In that case it has no
1098         // (meaningful) users and we should not try to code generate it.
1099         if (IAClass->InvariantAccesses.empty())
1100           V = UndefValue::get(ParamSCEV->getType());
1101 
1102         if (!preloadInvariantEquivClass(*IAClass)) {
1103           isl_id_free(Id);
1104           return false;
1105         }
1106       }
1107     }
1108 
1109     V = V ? V : generateSCEV(ParamSCEV);
1110     IDToValue[Id] = V;
1111   }
1112 
1113   isl_id_free(Id);
1114   return true;
1115 }
1116 
1117 bool IslNodeBuilder::materializeParameters(isl_set *Set) {
1118   for (unsigned i = 0, e = isl_set_dim(Set, isl_dim_param); i < e; ++i) {
1119     if (!isl_set_involves_dims(Set, isl_dim_param, i, 1))
1120       continue;
1121     isl_id *Id = isl_set_get_dim_id(Set, isl_dim_param, i);
1122     if (!materializeValue(Id))
1123       return false;
1124   }
1125   return true;
1126 }
1127 
1128 bool IslNodeBuilder::materializeParameters() {
1129   for (const SCEV *Param : S.parameters()) {
1130     isl_id *Id = S.getIdForParam(Param).release();
1131     if (!materializeValue(Id))
1132       return false;
1133   }
1134   return true;
1135 }
1136 
1137 /// Generate the computation of the size of the outermost dimension from the
1138 /// Fortran array descriptor (in this case, `@g_arr`). The final `%size`
1139 /// contains the size of the array.
1140 ///
1141 /// %arrty = type { i8*, i64, i64, [3 x %desc.dimensionty] }
1142 /// %desc.dimensionty = type { i64, i64, i64 }
1143 /// @g_arr = global %arrty zeroinitializer, align 32
1144 /// ...
1145 /// %0 = load i64, i64* getelementptr inbounds
1146 ///                       (%arrty, %arrty* @g_arr, i64 0, i32 3, i64 0, i32 2)
1147 /// %1 = load i64, i64* getelementptr inbounds
1148 ///                      (%arrty, %arrty* @g_arr, i64 0, i32 3, i64 0, i32 1)
1149 /// %2 = sub nsw i64 %0, %1
1150 /// %size = add nsw i64 %2, 1
1151 static Value *buildFADOutermostDimensionLoad(Value *GlobalDescriptor,
1152                                              PollyIRBuilder &Builder,
1153                                              std::string ArrayName) {
1154   assert(GlobalDescriptor && "invalid global descriptor given");
1155 
1156   Value *endIdx[4] = {Builder.getInt64(0), Builder.getInt32(3),
1157                       Builder.getInt64(0), Builder.getInt32(2)};
1158   Value *endPtr = Builder.CreateInBoundsGEP(GlobalDescriptor, endIdx,
1159                                             ArrayName + "_end_ptr");
1160   Type *type = cast<GEPOperator>(endPtr)->getResultElementType();
1161   assert(isa<IntegerType>(type) && "expected type of end to be integral");
1162 
1163   Value *end = Builder.CreateLoad(type, endPtr, ArrayName + "_end");
1164 
1165   Value *beginIdx[4] = {Builder.getInt64(0), Builder.getInt32(3),
1166                         Builder.getInt64(0), Builder.getInt32(1)};
1167   Value *beginPtr = Builder.CreateInBoundsGEP(GlobalDescriptor, beginIdx,
1168                                               ArrayName + "_begin_ptr");
1169   Value *begin = Builder.CreateLoad(type, beginPtr, ArrayName + "_begin");
1170 
1171   Value *size =
1172       Builder.CreateNSWSub(end, begin, ArrayName + "_end_begin_delta");
1173 
1174   size = Builder.CreateNSWAdd(
1175       end, ConstantInt::get(type, 1, /* signed = */ true), ArrayName + "_size");
1176 
1177   return size;
1178 }
1179 
1180 bool IslNodeBuilder::materializeFortranArrayOutermostDimension() {
1181   for (ScopArrayInfo *Array : S.arrays()) {
1182     if (Array->getNumberOfDimensions() == 0)
1183       continue;
1184 
1185     Value *FAD = Array->getFortranArrayDescriptor();
1186     if (!FAD)
1187       continue;
1188 
1189     isl_pw_aff *ParametricPwAff = Array->getDimensionSizePw(0).release();
1190     assert(ParametricPwAff && "parametric pw_aff corresponding "
1191                               "to outermost dimension does not "
1192                               "exist");
1193 
1194     isl_id *Id = isl_pw_aff_get_dim_id(ParametricPwAff, isl_dim_param, 0);
1195     isl_pw_aff_free(ParametricPwAff);
1196 
1197     assert(Id && "pw_aff is not parametric");
1198 
1199     if (IDToValue.count(Id)) {
1200       isl_id_free(Id);
1201       continue;
1202     }
1203 
1204     Value *FinalValue =
1205         buildFADOutermostDimensionLoad(FAD, Builder, Array->getName());
1206     assert(FinalValue && "unable to build Fortran array "
1207                          "descriptor load of outermost dimension");
1208     IDToValue[Id] = FinalValue;
1209     isl_id_free(Id);
1210   }
1211   return true;
1212 }
1213 
1214 Value *IslNodeBuilder::preloadUnconditionally(isl_set *AccessRange,
1215                                               isl_ast_build *Build,
1216                                               Instruction *AccInst) {
1217   isl_pw_multi_aff *PWAccRel = isl_pw_multi_aff_from_set(AccessRange);
1218   isl_ast_expr *Access =
1219       isl_ast_build_access_from_pw_multi_aff(Build, PWAccRel);
1220   auto *Address = isl_ast_expr_address_of(Access);
1221   auto *AddressValue = ExprBuilder.create(Address);
1222   Value *PreloadVal;
1223 
1224   // Correct the type as the SAI might have a different type than the user
1225   // expects, especially if the base pointer is a struct.
1226   Type *Ty = AccInst->getType();
1227 
1228   auto *Ptr = AddressValue;
1229   auto Name = Ptr->getName();
1230   auto AS = Ptr->getType()->getPointerAddressSpace();
1231   Ptr = Builder.CreatePointerCast(Ptr, Ty->getPointerTo(AS), Name + ".cast");
1232   PreloadVal = Builder.CreateLoad(Ty, Ptr, Name + ".load");
1233   if (LoadInst *PreloadInst = dyn_cast<LoadInst>(PreloadVal))
1234     PreloadInst->setAlignment(cast<LoadInst>(AccInst)->getAlign());
1235 
1236   // TODO: This is only a hot fix for SCoP sequences that use the same load
1237   //       instruction contained and hoisted by one of the SCoPs.
1238   if (SE.isSCEVable(Ty))
1239     SE.forgetValue(AccInst);
1240 
1241   return PreloadVal;
1242 }
1243 
1244 Value *IslNodeBuilder::preloadInvariantLoad(const MemoryAccess &MA,
1245                                             isl_set *Domain) {
1246   isl_set *AccessRange = isl_map_range(MA.getAddressFunction().release());
1247   AccessRange = isl_set_gist_params(AccessRange, S.getContext().release());
1248 
1249   if (!materializeParameters(AccessRange)) {
1250     isl_set_free(AccessRange);
1251     isl_set_free(Domain);
1252     return nullptr;
1253   }
1254 
1255   auto *Build =
1256       isl_ast_build_from_context(isl_set_universe(S.getParamSpace().release()));
1257   isl_set *Universe = isl_set_universe(isl_set_get_space(Domain));
1258   bool AlwaysExecuted = isl_set_is_equal(Domain, Universe);
1259   isl_set_free(Universe);
1260 
1261   Instruction *AccInst = MA.getAccessInstruction();
1262   Type *AccInstTy = AccInst->getType();
1263 
1264   Value *PreloadVal = nullptr;
1265   if (AlwaysExecuted) {
1266     PreloadVal = preloadUnconditionally(AccessRange, Build, AccInst);
1267     isl_ast_build_free(Build);
1268     isl_set_free(Domain);
1269     return PreloadVal;
1270   }
1271 
1272   if (!materializeParameters(Domain)) {
1273     isl_ast_build_free(Build);
1274     isl_set_free(AccessRange);
1275     isl_set_free(Domain);
1276     return nullptr;
1277   }
1278 
1279   isl_ast_expr *DomainCond = isl_ast_build_expr_from_set(Build, Domain);
1280   Domain = nullptr;
1281 
1282   ExprBuilder.setTrackOverflow(true);
1283   Value *Cond = ExprBuilder.create(DomainCond);
1284   Value *OverflowHappened = Builder.CreateNot(ExprBuilder.getOverflowState(),
1285                                               "polly.preload.cond.overflown");
1286   Cond = Builder.CreateAnd(Cond, OverflowHappened, "polly.preload.cond.result");
1287   ExprBuilder.setTrackOverflow(false);
1288 
1289   if (!Cond->getType()->isIntegerTy(1))
1290     Cond = Builder.CreateIsNotNull(Cond);
1291 
1292   BasicBlock *CondBB = SplitBlock(Builder.GetInsertBlock(),
1293                                   &*Builder.GetInsertPoint(), &DT, &LI);
1294   CondBB->setName("polly.preload.cond");
1295 
1296   BasicBlock *MergeBB = SplitBlock(CondBB, &CondBB->front(), &DT, &LI);
1297   MergeBB->setName("polly.preload.merge");
1298 
1299   Function *F = Builder.GetInsertBlock()->getParent();
1300   LLVMContext &Context = F->getContext();
1301   BasicBlock *ExecBB = BasicBlock::Create(Context, "polly.preload.exec", F);
1302 
1303   DT.addNewBlock(ExecBB, CondBB);
1304   if (Loop *L = LI.getLoopFor(CondBB))
1305     L->addBasicBlockToLoop(ExecBB, LI);
1306 
1307   auto *CondBBTerminator = CondBB->getTerminator();
1308   Builder.SetInsertPoint(CondBBTerminator);
1309   Builder.CreateCondBr(Cond, ExecBB, MergeBB);
1310   CondBBTerminator->eraseFromParent();
1311 
1312   Builder.SetInsertPoint(ExecBB);
1313   Builder.CreateBr(MergeBB);
1314 
1315   Builder.SetInsertPoint(ExecBB->getTerminator());
1316   Value *PreAccInst = preloadUnconditionally(AccessRange, Build, AccInst);
1317   Builder.SetInsertPoint(MergeBB->getTerminator());
1318   auto *MergePHI = Builder.CreatePHI(
1319       AccInstTy, 2, "polly.preload." + AccInst->getName() + ".merge");
1320   PreloadVal = MergePHI;
1321 
1322   if (!PreAccInst) {
1323     PreloadVal = nullptr;
1324     PreAccInst = UndefValue::get(AccInstTy);
1325   }
1326 
1327   MergePHI->addIncoming(PreAccInst, ExecBB);
1328   MergePHI->addIncoming(Constant::getNullValue(AccInstTy), CondBB);
1329 
1330   isl_ast_build_free(Build);
1331   return PreloadVal;
1332 }
1333 
1334 bool IslNodeBuilder::preloadInvariantEquivClass(
1335     InvariantEquivClassTy &IAClass) {
1336   // For an equivalence class of invariant loads we pre-load the representing
1337   // element with the unified execution context. However, we have to map all
1338   // elements of the class to the one preloaded load as they are referenced
1339   // during the code generation and therefor need to be mapped.
1340   const MemoryAccessList &MAs = IAClass.InvariantAccesses;
1341   if (MAs.empty())
1342     return true;
1343 
1344   MemoryAccess *MA = MAs.front();
1345   assert(MA->isArrayKind() && MA->isRead());
1346 
1347   // If the access function was already mapped, the preload of this equivalence
1348   // class was triggered earlier already and doesn't need to be done again.
1349   if (ValueMap.count(MA->getAccessInstruction()))
1350     return true;
1351 
1352   // Check for recursion which can be caused by additional constraints, e.g.,
1353   // non-finite loop constraints. In such a case we have to bail out and insert
1354   // a "false" runtime check that will cause the original code to be executed.
1355   auto PtrId = std::make_pair(IAClass.IdentifyingPointer, IAClass.AccessType);
1356   if (!PreloadedPtrs.insert(PtrId).second)
1357     return false;
1358 
1359   // The execution context of the IAClass.
1360   isl::set &ExecutionCtx = IAClass.ExecutionContext;
1361 
1362   // If the base pointer of this class is dependent on another one we have to
1363   // make sure it was preloaded already.
1364   auto *SAI = MA->getScopArrayInfo();
1365   if (auto *BaseIAClass = S.lookupInvariantEquivClass(SAI->getBasePtr())) {
1366     if (!preloadInvariantEquivClass(*BaseIAClass))
1367       return false;
1368 
1369     // After we preloaded the BaseIAClass we adjusted the BaseExecutionCtx and
1370     // we need to refine the ExecutionCtx.
1371     isl::set BaseExecutionCtx = BaseIAClass->ExecutionContext;
1372     ExecutionCtx = ExecutionCtx.intersect(BaseExecutionCtx);
1373   }
1374 
1375   // If the size of a dimension is dependent on another class, make sure it is
1376   // preloaded.
1377   for (unsigned i = 1, e = SAI->getNumberOfDimensions(); i < e; ++i) {
1378     const SCEV *Dim = SAI->getDimensionSize(i);
1379     SetVector<Value *> Values;
1380     findValues(Dim, SE, Values);
1381     for (auto *Val : Values) {
1382       if (auto *BaseIAClass = S.lookupInvariantEquivClass(Val)) {
1383         if (!preloadInvariantEquivClass(*BaseIAClass))
1384           return false;
1385 
1386         // After we preloaded the BaseIAClass we adjusted the BaseExecutionCtx
1387         // and we need to refine the ExecutionCtx.
1388         isl::set BaseExecutionCtx = BaseIAClass->ExecutionContext;
1389         ExecutionCtx = ExecutionCtx.intersect(BaseExecutionCtx);
1390       }
1391     }
1392   }
1393 
1394   Instruction *AccInst = MA->getAccessInstruction();
1395   Type *AccInstTy = AccInst->getType();
1396 
1397   Value *PreloadVal = preloadInvariantLoad(*MA, ExecutionCtx.copy());
1398   if (!PreloadVal)
1399     return false;
1400 
1401   for (const MemoryAccess *MA : MAs) {
1402     Instruction *MAAccInst = MA->getAccessInstruction();
1403     assert(PreloadVal->getType() == MAAccInst->getType());
1404     ValueMap[MAAccInst] = PreloadVal;
1405   }
1406 
1407   if (SE.isSCEVable(AccInstTy)) {
1408     isl_id *ParamId = S.getIdForParam(SE.getSCEV(AccInst)).release();
1409     if (ParamId)
1410       IDToValue[ParamId] = PreloadVal;
1411     isl_id_free(ParamId);
1412   }
1413 
1414   BasicBlock *EntryBB = &Builder.GetInsertBlock()->getParent()->getEntryBlock();
1415   auto *Alloca = new AllocaInst(AccInstTy, DL.getAllocaAddrSpace(),
1416                                 AccInst->getName() + ".preload.s2a",
1417                                 &*EntryBB->getFirstInsertionPt());
1418   Builder.CreateStore(PreloadVal, Alloca);
1419   ValueMapT PreloadedPointer;
1420   PreloadedPointer[PreloadVal] = AccInst;
1421   Annotator.addAlternativeAliasBases(PreloadedPointer);
1422 
1423   for (auto *DerivedSAI : SAI->getDerivedSAIs()) {
1424     Value *BasePtr = DerivedSAI->getBasePtr();
1425 
1426     for (const MemoryAccess *MA : MAs) {
1427       // As the derived SAI information is quite coarse, any load from the
1428       // current SAI could be the base pointer of the derived SAI, however we
1429       // should only change the base pointer of the derived SAI if we actually
1430       // preloaded it.
1431       if (BasePtr == MA->getOriginalBaseAddr()) {
1432         assert(BasePtr->getType() == PreloadVal->getType());
1433         DerivedSAI->setBasePtr(PreloadVal);
1434       }
1435 
1436       // For scalar derived SAIs we remap the alloca used for the derived value.
1437       if (BasePtr == MA->getAccessInstruction())
1438         ScalarMap[DerivedSAI] = Alloca;
1439     }
1440   }
1441 
1442   for (const MemoryAccess *MA : MAs) {
1443     Instruction *MAAccInst = MA->getAccessInstruction();
1444     // Use the escape system to get the correct value to users outside the SCoP.
1445     BlockGenerator::EscapeUserVectorTy EscapeUsers;
1446     for (auto *U : MAAccInst->users())
1447       if (Instruction *UI = dyn_cast<Instruction>(U))
1448         if (!S.contains(UI))
1449           EscapeUsers.push_back(UI);
1450 
1451     if (EscapeUsers.empty())
1452       continue;
1453 
1454     EscapeMap[MA->getAccessInstruction()] =
1455         std::make_pair(Alloca, std::move(EscapeUsers));
1456   }
1457 
1458   return true;
1459 }
1460 
1461 void IslNodeBuilder::allocateNewArrays(BBPair StartExitBlocks) {
1462   for (auto &SAI : S.arrays()) {
1463     if (SAI->getBasePtr())
1464       continue;
1465 
1466     assert(SAI->getNumberOfDimensions() > 0 && SAI->getDimensionSize(0) &&
1467            "The size of the outermost dimension is used to declare newly "
1468            "created arrays that require memory allocation.");
1469 
1470     Type *NewArrayType = nullptr;
1471 
1472     // Get the size of the array = size(dim_1)*...*size(dim_n)
1473     uint64_t ArraySizeInt = 1;
1474     for (int i = SAI->getNumberOfDimensions() - 1; i >= 0; i--) {
1475       auto *DimSize = SAI->getDimensionSize(i);
1476       unsigned UnsignedDimSize = static_cast<const SCEVConstant *>(DimSize)
1477                                      ->getAPInt()
1478                                      .getLimitedValue();
1479 
1480       if (!NewArrayType)
1481         NewArrayType = SAI->getElementType();
1482 
1483       NewArrayType = ArrayType::get(NewArrayType, UnsignedDimSize);
1484       ArraySizeInt *= UnsignedDimSize;
1485     }
1486 
1487     if (SAI->isOnHeap()) {
1488       LLVMContext &Ctx = NewArrayType->getContext();
1489 
1490       // Get the IntPtrTy from the Datalayout
1491       auto IntPtrTy = DL.getIntPtrType(Ctx);
1492 
1493       // Get the size of the element type in bits
1494       unsigned Size = SAI->getElemSizeInBytes();
1495 
1496       // Insert the malloc call at polly.start
1497       auto InstIt = std::get<0>(StartExitBlocks)->getTerminator();
1498       auto *CreatedArray = CallInst::CreateMalloc(
1499           &*InstIt, IntPtrTy, SAI->getElementType(),
1500           ConstantInt::get(Type::getInt64Ty(Ctx), Size),
1501           ConstantInt::get(Type::getInt64Ty(Ctx), ArraySizeInt), nullptr,
1502           SAI->getName());
1503 
1504       SAI->setBasePtr(CreatedArray);
1505 
1506       // Insert the free call at polly.exiting
1507       CallInst::CreateFree(CreatedArray,
1508                            std::get<1>(StartExitBlocks)->getTerminator());
1509     } else {
1510       auto InstIt = Builder.GetInsertBlock()
1511                         ->getParent()
1512                         ->getEntryBlock()
1513                         .getTerminator();
1514 
1515       auto *CreatedArray = new AllocaInst(NewArrayType, DL.getAllocaAddrSpace(),
1516                                           SAI->getName(), &*InstIt);
1517       if (PollyTargetFirstLevelCacheLineSize)
1518         CreatedArray->setAlignment(Align(PollyTargetFirstLevelCacheLineSize));
1519       SAI->setBasePtr(CreatedArray);
1520     }
1521   }
1522 }
1523 
1524 bool IslNodeBuilder::preloadInvariantLoads() {
1525   auto &InvariantEquivClasses = S.getInvariantAccesses();
1526   if (InvariantEquivClasses.empty())
1527     return true;
1528 
1529   BasicBlock *PreLoadBB = SplitBlock(Builder.GetInsertBlock(),
1530                                      &*Builder.GetInsertPoint(), &DT, &LI);
1531   PreLoadBB->setName("polly.preload.begin");
1532   Builder.SetInsertPoint(&PreLoadBB->front());
1533 
1534   for (auto &IAClass : InvariantEquivClasses)
1535     if (!preloadInvariantEquivClass(IAClass))
1536       return false;
1537 
1538   return true;
1539 }
1540 
1541 void IslNodeBuilder::addParameters(__isl_take isl_set *Context) {
1542   // Materialize values for the parameters of the SCoP.
1543   materializeParameters();
1544 
1545   // materialize the outermost dimension parameters for a Fortran array.
1546   // NOTE: materializeParameters() does not work since it looks through
1547   // the SCEVs. We don't have a corresponding SCEV for the array size
1548   // parameter
1549   materializeFortranArrayOutermostDimension();
1550 
1551   // Generate values for the current loop iteration for all surrounding loops.
1552   //
1553   // We may also reference loops outside of the scop which do not contain the
1554   // scop itself, but as the number of such scops may be arbitrarily large we do
1555   // not generate code for them here, but only at the point of code generation
1556   // where these values are needed.
1557   Loop *L = LI.getLoopFor(S.getEntry());
1558 
1559   while (L != nullptr && S.contains(L))
1560     L = L->getParentLoop();
1561 
1562   while (L != nullptr) {
1563     materializeNonScopLoopInductionVariable(L);
1564     L = L->getParentLoop();
1565   }
1566 
1567   isl_set_free(Context);
1568 }
1569 
1570 Value *IslNodeBuilder::generateSCEV(const SCEV *Expr) {
1571   /// We pass the insert location of our Builder, as Polly ensures during IR
1572   /// generation that there is always a valid CFG into which instructions are
1573   /// inserted. As a result, the insertpoint is known to be always followed by a
1574   /// terminator instruction. This means the insert point may be specified by a
1575   /// terminator instruction, but it can never point to an ->end() iterator
1576   /// which does not have a corresponding instruction. Hence, dereferencing
1577   /// the insertpoint to obtain an instruction is known to be save.
1578   ///
1579   /// We also do not need to update the Builder here, as new instructions are
1580   /// always inserted _before_ the given InsertLocation. As a result, the
1581   /// insert location remains valid.
1582   assert(Builder.GetInsertBlock()->end() != Builder.GetInsertPoint() &&
1583          "Insert location points after last valid instruction");
1584   Instruction *InsertLocation = &*Builder.GetInsertPoint();
1585   return expandCodeFor(S, SE, DL, "polly", Expr, Expr->getType(),
1586                        InsertLocation, &ValueMap,
1587                        StartBlock->getSinglePredecessor());
1588 }
1589 
1590 /// The AST expression we generate to perform the run-time check assumes
1591 /// computations on integer types of infinite size. As we only use 64-bit
1592 /// arithmetic we check for overflows, in case of which we set the result
1593 /// of this run-time check to false to be conservatively correct,
1594 Value *IslNodeBuilder::createRTC(isl_ast_expr *Condition) {
1595   auto ExprBuilder = getExprBuilder();
1596 
1597   // In case the AST expression has integers larger than 64 bit, bail out. The
1598   // resulting LLVM-IR will contain operations on types that use more than 64
1599   // bits. These are -- in case wrapping intrinsics are used -- translated to
1600   // runtime library calls that are not available on all systems (e.g., Android)
1601   // and consequently will result in linker errors.
1602   if (ExprBuilder.hasLargeInts(isl::manage_copy(Condition))) {
1603     isl_ast_expr_free(Condition);
1604     return Builder.getFalse();
1605   }
1606 
1607   ExprBuilder.setTrackOverflow(true);
1608   Value *RTC = ExprBuilder.create(Condition);
1609   if (!RTC->getType()->isIntegerTy(1))
1610     RTC = Builder.CreateIsNotNull(RTC);
1611   Value *OverflowHappened =
1612       Builder.CreateNot(ExprBuilder.getOverflowState(), "polly.rtc.overflown");
1613 
1614   if (PollyGenerateRTCPrint) {
1615     auto *F = Builder.GetInsertBlock()->getParent();
1616     RuntimeDebugBuilder::createCPUPrinter(
1617         Builder,
1618         "F: " + F->getName().str() + " R: " + S.getRegion().getNameStr() +
1619             "RTC: ",
1620         RTC, " Overflow: ", OverflowHappened,
1621         "\n"
1622         "  (0 failed, -1 succeeded)\n"
1623         "  (if one or both are 0 falling back to original code, if both are -1 "
1624         "executing Polly code)\n");
1625   }
1626 
1627   RTC = Builder.CreateAnd(RTC, OverflowHappened, "polly.rtc.result");
1628   ExprBuilder.setTrackOverflow(false);
1629 
1630   if (!isa<ConstantInt>(RTC))
1631     VersionedScops++;
1632 
1633   return RTC;
1634 }
1635