IslNodeBuilder.cpp 56 KB

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