IslNodeBuilder.cpp 56 KB

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