loop_dependence.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/loop_dependence.h"
  15. #include <functional>
  16. #include <memory>
  17. #include <numeric>
  18. #include <string>
  19. #include <utility>
  20. #include <vector>
  21. #include "source/opt/instruction.h"
  22. #include "source/opt/scalar_analysis.h"
  23. #include "source/opt/scalar_analysis_nodes.h"
  24. namespace spvtools {
  25. namespace opt {
  26. using SubscriptPair = std::pair<SENode*, SENode*>;
  27. namespace {
  28. // Calculate the greatest common divisor of a & b using Stein's algorithm.
  29. // https://en.wikipedia.org/wiki/Binary_GCD_algorithm
  30. int64_t GreatestCommonDivisor(int64_t a, int64_t b) {
  31. // Simple cases
  32. if (a == b) {
  33. return a;
  34. } else if (a == 0) {
  35. return b;
  36. } else if (b == 0) {
  37. return a;
  38. }
  39. // Both even
  40. if (a % 2 == 0 && b % 2 == 0) {
  41. return 2 * GreatestCommonDivisor(a / 2, b / 2);
  42. }
  43. // Even a, odd b
  44. if (a % 2 == 0 && b % 2 == 1) {
  45. return GreatestCommonDivisor(a / 2, b);
  46. }
  47. // Odd a, even b
  48. if (a % 2 == 1 && b % 2 == 0) {
  49. return GreatestCommonDivisor(a, b / 2);
  50. }
  51. // Both odd, reduce the larger argument
  52. if (a > b) {
  53. return GreatestCommonDivisor((a - b) / 2, b);
  54. } else {
  55. return GreatestCommonDivisor((b - a) / 2, a);
  56. }
  57. }
  58. // Check if node is affine, ie in the form: a0*i0 + a1*i1 + ... an*in + c
  59. // and contains only the following types of nodes: SERecurrentNode, SEAddNode
  60. // and SEConstantNode
  61. bool IsInCorrectFormForGCDTest(SENode* node) {
  62. bool children_ok = true;
  63. if (auto add_node = node->AsSEAddNode()) {
  64. for (auto child : add_node->GetChildren()) {
  65. children_ok &= IsInCorrectFormForGCDTest(child);
  66. }
  67. }
  68. bool this_ok = node->AsSERecurrentNode() || node->AsSEAddNode() ||
  69. node->AsSEConstantNode();
  70. return children_ok && this_ok;
  71. }
  72. // If |node| is an SERecurrentNode then returns |node| or if |node| is an
  73. // SEAddNode returns a vector of SERecurrentNode that are its children.
  74. std::vector<SERecurrentNode*> GetAllTopLevelRecurrences(SENode* node) {
  75. auto nodes = std::vector<SERecurrentNode*>{};
  76. if (auto recurrent_node = node->AsSERecurrentNode()) {
  77. nodes.push_back(recurrent_node);
  78. }
  79. if (auto add_node = node->AsSEAddNode()) {
  80. for (auto child : add_node->GetChildren()) {
  81. auto child_nodes = GetAllTopLevelRecurrences(child);
  82. nodes.insert(nodes.end(), child_nodes.begin(), child_nodes.end());
  83. }
  84. }
  85. return nodes;
  86. }
  87. // If |node| is an SEConstantNode then returns |node| or if |node| is an
  88. // SEAddNode returns a vector of SEConstantNode that are its children.
  89. std::vector<SEConstantNode*> GetAllTopLevelConstants(SENode* node) {
  90. auto nodes = std::vector<SEConstantNode*>{};
  91. if (auto recurrent_node = node->AsSEConstantNode()) {
  92. nodes.push_back(recurrent_node);
  93. }
  94. if (auto add_node = node->AsSEAddNode()) {
  95. for (auto child : add_node->GetChildren()) {
  96. auto child_nodes = GetAllTopLevelConstants(child);
  97. nodes.insert(nodes.end(), child_nodes.begin(), child_nodes.end());
  98. }
  99. }
  100. return nodes;
  101. }
  102. bool AreOffsetsAndCoefficientsConstant(
  103. const std::vector<SERecurrentNode*>& nodes) {
  104. for (auto node : nodes) {
  105. if (!node->GetOffset()->AsSEConstantNode() ||
  106. !node->GetOffset()->AsSEConstantNode()) {
  107. return false;
  108. }
  109. }
  110. return true;
  111. }
  112. // Fold all SEConstantNode that appear in |recurrences| and |constants| into a
  113. // single integer value.
  114. int64_t CalculateConstantTerm(const std::vector<SERecurrentNode*>& recurrences,
  115. const std::vector<SEConstantNode*>& constants) {
  116. int64_t constant_term = 0;
  117. for (auto recurrence : recurrences) {
  118. constant_term +=
  119. recurrence->GetOffset()->AsSEConstantNode()->FoldToSingleValue();
  120. }
  121. for (auto constant : constants) {
  122. constant_term += constant->FoldToSingleValue();
  123. }
  124. return constant_term;
  125. }
  126. int64_t CalculateGCDFromCoefficients(
  127. const std::vector<SERecurrentNode*>& recurrences, int64_t running_gcd) {
  128. for (SERecurrentNode* recurrence : recurrences) {
  129. auto coefficient = recurrence->GetCoefficient()->AsSEConstantNode();
  130. running_gcd = GreatestCommonDivisor(
  131. running_gcd, std::abs(coefficient->FoldToSingleValue()));
  132. }
  133. return running_gcd;
  134. }
  135. // Compare 2 fractions while first normalizing them, e.g. 2/4 and 4/8 will both
  136. // be simplified to 1/2 and then determined to be equal.
  137. bool NormalizeAndCompareFractions(int64_t numerator_0, int64_t denominator_0,
  138. int64_t numerator_1, int64_t denominator_1) {
  139. auto gcd_0 =
  140. GreatestCommonDivisor(std::abs(numerator_0), std::abs(denominator_0));
  141. auto gcd_1 =
  142. GreatestCommonDivisor(std::abs(numerator_1), std::abs(denominator_1));
  143. auto normalized_numerator_0 = numerator_0 / gcd_0;
  144. auto normalized_denominator_0 = denominator_0 / gcd_0;
  145. auto normalized_numerator_1 = numerator_1 / gcd_1;
  146. auto normalized_denominator_1 = denominator_1 / gcd_1;
  147. return normalized_numerator_0 == normalized_numerator_1 &&
  148. normalized_denominator_0 == normalized_denominator_1;
  149. }
  150. } // namespace
  151. bool LoopDependenceAnalysis::GetDependence(const Instruction* source,
  152. const Instruction* destination,
  153. DistanceVector* distance_vector) {
  154. // Start off by finding and marking all the loops in |loops_| that are
  155. // irrelevant to the dependence analysis.
  156. MarkUnsusedDistanceEntriesAsIrrelevant(source, destination, distance_vector);
  157. Instruction* source_access_chain = GetOperandDefinition(source, 0);
  158. Instruction* destination_access_chain = GetOperandDefinition(destination, 0);
  159. auto num_access_chains =
  160. (source_access_chain->opcode() == SpvOpAccessChain) +
  161. (destination_access_chain->opcode() == SpvOpAccessChain);
  162. // If neither is an access chain, then they are load/store to a variable.
  163. if (num_access_chains == 0) {
  164. if (source_access_chain != destination_access_chain) {
  165. // Not the same location, report independence
  166. return true;
  167. } else {
  168. // Accessing the same variable
  169. for (auto& entry : distance_vector->GetEntries()) {
  170. entry = DistanceEntry();
  171. }
  172. return false;
  173. }
  174. }
  175. // If only one is an access chain, it could be accessing a part of a struct
  176. if (num_access_chains == 1) {
  177. auto source_is_chain = source_access_chain->opcode() == SpvOpAccessChain;
  178. auto access_chain =
  179. source_is_chain ? source_access_chain : destination_access_chain;
  180. auto variable =
  181. source_is_chain ? destination_access_chain : source_access_chain;
  182. auto location_in_chain = GetOperandDefinition(access_chain, 0);
  183. if (variable != location_in_chain) {
  184. // Not the same location, report independence
  185. return true;
  186. } else {
  187. // Accessing the same variable
  188. for (auto& entry : distance_vector->GetEntries()) {
  189. entry = DistanceEntry();
  190. }
  191. return false;
  192. }
  193. }
  194. // If the access chains aren't collecting from the same structure there is no
  195. // dependence.
  196. Instruction* source_array = GetOperandDefinition(source_access_chain, 0);
  197. Instruction* destination_array =
  198. GetOperandDefinition(destination_access_chain, 0);
  199. // Nested access chains are not supported yet, bail out.
  200. if (source_array->opcode() == SpvOpAccessChain ||
  201. destination_array->opcode() == SpvOpAccessChain) {
  202. for (auto& entry : distance_vector->GetEntries()) {
  203. entry = DistanceEntry();
  204. }
  205. return false;
  206. }
  207. if (source_array != destination_array) {
  208. PrintDebug("Proved independence through different arrays.");
  209. return true;
  210. }
  211. // To handle multiple subscripts we must get every operand in the access
  212. // chains past the first.
  213. std::vector<Instruction*> source_subscripts = GetSubscripts(source);
  214. std::vector<Instruction*> destination_subscripts = GetSubscripts(destination);
  215. auto sets_of_subscripts =
  216. PartitionSubscripts(source_subscripts, destination_subscripts);
  217. auto first_coupled = std::partition(
  218. std::begin(sets_of_subscripts), std::end(sets_of_subscripts),
  219. [](const std::set<std::pair<Instruction*, Instruction*>>& set) {
  220. return set.size() == 1;
  221. });
  222. // Go through each subscript testing for independence.
  223. // If any subscript results in independence, we prove independence between the
  224. // load and store.
  225. // If we can't prove independence we store what information we can gather in
  226. // a DistanceVector.
  227. for (auto it = std::begin(sets_of_subscripts); it < first_coupled; ++it) {
  228. auto source_subscript = std::get<0>(*(*it).begin());
  229. auto destination_subscript = std::get<1>(*(*it).begin());
  230. SENode* source_node = scalar_evolution_.SimplifyExpression(
  231. scalar_evolution_.AnalyzeInstruction(source_subscript));
  232. SENode* destination_node = scalar_evolution_.SimplifyExpression(
  233. scalar_evolution_.AnalyzeInstruction(destination_subscript));
  234. // Check the loops are in a form we support.
  235. auto subscript_pair = std::make_pair(source_node, destination_node);
  236. const Loop* loop = GetLoopForSubscriptPair(subscript_pair);
  237. if (loop) {
  238. if (!IsSupportedLoop(loop)) {
  239. PrintDebug(
  240. "GetDependence found an unsupported loop form. Assuming <=> for "
  241. "loop.");
  242. DistanceEntry* distance_entry =
  243. GetDistanceEntryForSubscriptPair(subscript_pair, distance_vector);
  244. if (distance_entry) {
  245. distance_entry->direction = DistanceEntry::Directions::ALL;
  246. }
  247. continue;
  248. }
  249. }
  250. // If either node is simplified to a CanNotCompute we can't perform any
  251. // analysis so must assume <=> dependence and return.
  252. if (source_node->GetType() == SENode::CanNotCompute ||
  253. destination_node->GetType() == SENode::CanNotCompute) {
  254. // Record the <=> dependence if we can get a DistanceEntry
  255. PrintDebug(
  256. "GetDependence found source_node || destination_node as "
  257. "CanNotCompute. Abandoning evaluation for this subscript.");
  258. DistanceEntry* distance_entry =
  259. GetDistanceEntryForSubscriptPair(subscript_pair, distance_vector);
  260. if (distance_entry) {
  261. distance_entry->direction = DistanceEntry::Directions::ALL;
  262. }
  263. continue;
  264. }
  265. // We have no induction variables so can apply a ZIV test.
  266. if (IsZIV(subscript_pair)) {
  267. PrintDebug("Found a ZIV subscript pair");
  268. if (ZIVTest(subscript_pair)) {
  269. PrintDebug("Proved independence with ZIVTest.");
  270. return true;
  271. }
  272. }
  273. // We have only one induction variable so should attempt an SIV test.
  274. if (IsSIV(subscript_pair)) {
  275. PrintDebug("Found a SIV subscript pair.");
  276. if (SIVTest(subscript_pair, distance_vector)) {
  277. PrintDebug("Proved independence with SIVTest.");
  278. return true;
  279. }
  280. }
  281. // We have multiple induction variables so should attempt an MIV test.
  282. if (IsMIV(subscript_pair)) {
  283. PrintDebug("Found a MIV subscript pair.");
  284. if (GCDMIVTest(subscript_pair)) {
  285. PrintDebug("Proved independence with the GCD test.");
  286. auto current_loops = CollectLoops(source_node, destination_node);
  287. for (auto current_loop : current_loops) {
  288. auto distance_entry =
  289. GetDistanceEntryForLoop(current_loop, distance_vector);
  290. distance_entry->direction = DistanceEntry::Directions::NONE;
  291. }
  292. return true;
  293. }
  294. }
  295. }
  296. for (auto it = first_coupled; it < std::end(sets_of_subscripts); ++it) {
  297. auto coupled_instructions = *it;
  298. std::vector<SubscriptPair> coupled_subscripts{};
  299. for (const auto& elem : coupled_instructions) {
  300. auto source_subscript = std::get<0>(elem);
  301. auto destination_subscript = std::get<1>(elem);
  302. SENode* source_node = scalar_evolution_.SimplifyExpression(
  303. scalar_evolution_.AnalyzeInstruction(source_subscript));
  304. SENode* destination_node = scalar_evolution_.SimplifyExpression(
  305. scalar_evolution_.AnalyzeInstruction(destination_subscript));
  306. coupled_subscripts.push_back({source_node, destination_node});
  307. }
  308. auto supported = true;
  309. for (const auto& subscript : coupled_subscripts) {
  310. auto loops = CollectLoops(std::get<0>(subscript), std::get<1>(subscript));
  311. auto is_subscript_supported =
  312. std::all_of(std::begin(loops), std::end(loops),
  313. [this](const Loop* l) { return IsSupportedLoop(l); });
  314. supported = supported && is_subscript_supported;
  315. }
  316. if (DeltaTest(coupled_subscripts, distance_vector)) {
  317. return true;
  318. }
  319. }
  320. // We were unable to prove independence so must gather all of the direction
  321. // information we found.
  322. PrintDebug(
  323. "Couldn't prove independence.\n"
  324. "All possible direction information has been collected in the input "
  325. "DistanceVector.");
  326. return false;
  327. }
  328. bool LoopDependenceAnalysis::ZIVTest(
  329. const std::pair<SENode*, SENode*>& subscript_pair) {
  330. auto source = std::get<0>(subscript_pair);
  331. auto destination = std::get<1>(subscript_pair);
  332. PrintDebug("Performing ZIVTest");
  333. // If source == destination, dependence with direction = and distance 0.
  334. if (source == destination) {
  335. PrintDebug("ZIVTest found EQ dependence.");
  336. return false;
  337. } else {
  338. PrintDebug("ZIVTest found independence.");
  339. // Otherwise we prove independence.
  340. return true;
  341. }
  342. }
  343. bool LoopDependenceAnalysis::SIVTest(
  344. const std::pair<SENode*, SENode*>& subscript_pair,
  345. DistanceVector* distance_vector) {
  346. DistanceEntry* distance_entry =
  347. GetDistanceEntryForSubscriptPair(subscript_pair, distance_vector);
  348. if (!distance_entry) {
  349. PrintDebug(
  350. "SIVTest could not find a DistanceEntry for subscript_pair. Exiting");
  351. }
  352. SENode* source_node = std::get<0>(subscript_pair);
  353. SENode* destination_node = std::get<1>(subscript_pair);
  354. int64_t source_induction_count = CountInductionVariables(source_node);
  355. int64_t destination_induction_count =
  356. CountInductionVariables(destination_node);
  357. // If the source node has no induction variables we can apply a
  358. // WeakZeroSrcTest.
  359. if (source_induction_count == 0) {
  360. PrintDebug("Found source has no induction variable.");
  361. if (WeakZeroSourceSIVTest(
  362. source_node, destination_node->AsSERecurrentNode(),
  363. destination_node->AsSERecurrentNode()->GetCoefficient(),
  364. distance_entry)) {
  365. PrintDebug("Proved independence with WeakZeroSourceSIVTest.");
  366. distance_entry->dependence_information =
  367. DistanceEntry::DependenceInformation::DIRECTION;
  368. distance_entry->direction = DistanceEntry::Directions::NONE;
  369. return true;
  370. }
  371. }
  372. // If the destination has no induction variables we can apply a
  373. // WeakZeroDestTest.
  374. if (destination_induction_count == 0) {
  375. PrintDebug("Found destination has no induction variable.");
  376. if (WeakZeroDestinationSIVTest(
  377. source_node->AsSERecurrentNode(), destination_node,
  378. source_node->AsSERecurrentNode()->GetCoefficient(),
  379. distance_entry)) {
  380. PrintDebug("Proved independence with WeakZeroDestinationSIVTest.");
  381. distance_entry->dependence_information =
  382. DistanceEntry::DependenceInformation::DIRECTION;
  383. distance_entry->direction = DistanceEntry::Directions::NONE;
  384. return true;
  385. }
  386. }
  387. // We now need to collect the SERecurrentExpr nodes from source and
  388. // destination. We do not handle cases where source or destination have
  389. // multiple SERecurrentExpr nodes.
  390. std::vector<SERecurrentNode*> source_recurrent_nodes =
  391. source_node->CollectRecurrentNodes();
  392. std::vector<SERecurrentNode*> destination_recurrent_nodes =
  393. destination_node->CollectRecurrentNodes();
  394. if (source_recurrent_nodes.size() == 1 &&
  395. destination_recurrent_nodes.size() == 1) {
  396. PrintDebug("Found source and destination have 1 induction variable.");
  397. SERecurrentNode* source_recurrent_expr = *source_recurrent_nodes.begin();
  398. SERecurrentNode* destination_recurrent_expr =
  399. *destination_recurrent_nodes.begin();
  400. // If the coefficients are identical we can apply a StrongSIVTest.
  401. if (source_recurrent_expr->GetCoefficient() ==
  402. destination_recurrent_expr->GetCoefficient()) {
  403. PrintDebug("Found source and destination share coefficient.");
  404. if (StrongSIVTest(source_node, destination_node,
  405. source_recurrent_expr->GetCoefficient(),
  406. distance_entry)) {
  407. PrintDebug("Proved independence with StrongSIVTest");
  408. distance_entry->dependence_information =
  409. DistanceEntry::DependenceInformation::DIRECTION;
  410. distance_entry->direction = DistanceEntry::Directions::NONE;
  411. return true;
  412. }
  413. }
  414. // If the coefficients are of equal magnitude and opposite sign we can
  415. // apply a WeakCrossingSIVTest.
  416. if (source_recurrent_expr->GetCoefficient() ==
  417. scalar_evolution_.CreateNegation(
  418. destination_recurrent_expr->GetCoefficient())) {
  419. PrintDebug("Found source coefficient = -destination coefficient.");
  420. if (WeakCrossingSIVTest(source_node, destination_node,
  421. source_recurrent_expr->GetCoefficient(),
  422. distance_entry)) {
  423. PrintDebug("Proved independence with WeakCrossingSIVTest");
  424. distance_entry->dependence_information =
  425. DistanceEntry::DependenceInformation::DIRECTION;
  426. distance_entry->direction = DistanceEntry::Directions::NONE;
  427. return true;
  428. }
  429. }
  430. }
  431. return false;
  432. }
  433. bool LoopDependenceAnalysis::StrongSIVTest(SENode* source, SENode* destination,
  434. SENode* coefficient,
  435. DistanceEntry* distance_entry) {
  436. PrintDebug("Performing StrongSIVTest.");
  437. // If both source and destination are SERecurrentNodes we can perform tests
  438. // based on distance.
  439. // If either source or destination contain value unknown nodes or if one or
  440. // both are not SERecurrentNodes we must attempt a symbolic test.
  441. std::vector<SEValueUnknown*> source_value_unknown_nodes =
  442. source->CollectValueUnknownNodes();
  443. std::vector<SEValueUnknown*> destination_value_unknown_nodes =
  444. destination->CollectValueUnknownNodes();
  445. if (source_value_unknown_nodes.size() > 0 ||
  446. destination_value_unknown_nodes.size() > 0) {
  447. PrintDebug(
  448. "StrongSIVTest found symbolics. Will attempt SymbolicStrongSIVTest.");
  449. return SymbolicStrongSIVTest(source, destination, coefficient,
  450. distance_entry);
  451. }
  452. if (!source->AsSERecurrentNode() || !destination->AsSERecurrentNode()) {
  453. PrintDebug(
  454. "StrongSIVTest could not simplify source and destination to "
  455. "SERecurrentNodes so will exit.");
  456. distance_entry->direction = DistanceEntry::Directions::ALL;
  457. return false;
  458. }
  459. // Build an SENode for distance.
  460. std::pair<SENode*, SENode*> subscript_pair =
  461. std::make_pair(source, destination);
  462. const Loop* subscript_loop = GetLoopForSubscriptPair(subscript_pair);
  463. SENode* source_constant_term =
  464. GetConstantTerm(subscript_loop, source->AsSERecurrentNode());
  465. SENode* destination_constant_term =
  466. GetConstantTerm(subscript_loop, destination->AsSERecurrentNode());
  467. if (!source_constant_term || !destination_constant_term) {
  468. PrintDebug(
  469. "StrongSIVTest could not collect the constant terms of either source "
  470. "or destination so will exit.");
  471. return false;
  472. }
  473. SENode* constant_term_delta =
  474. scalar_evolution_.SimplifyExpression(scalar_evolution_.CreateSubtraction(
  475. destination_constant_term, source_constant_term));
  476. // Scalar evolution doesn't perform division, so we must fold to constants and
  477. // do it manually.
  478. // We must check the offset delta and coefficient are constants.
  479. int64_t distance = 0;
  480. SEConstantNode* delta_constant = constant_term_delta->AsSEConstantNode();
  481. SEConstantNode* coefficient_constant = coefficient->AsSEConstantNode();
  482. if (delta_constant && coefficient_constant) {
  483. int64_t delta_value = delta_constant->FoldToSingleValue();
  484. int64_t coefficient_value = coefficient_constant->FoldToSingleValue();
  485. PrintDebug(
  486. "StrongSIVTest found delta value and coefficient value as constants "
  487. "with values:\n"
  488. "\tdelta value: " +
  489. ToString(delta_value) +
  490. "\n\tcoefficient value: " + ToString(coefficient_value) + "\n");
  491. // Check if the distance is not integral to try to prove independence.
  492. if (delta_value % coefficient_value != 0) {
  493. PrintDebug(
  494. "StrongSIVTest proved independence through distance not being an "
  495. "integer.");
  496. distance_entry->dependence_information =
  497. DistanceEntry::DependenceInformation::DIRECTION;
  498. distance_entry->direction = DistanceEntry::Directions::NONE;
  499. return true;
  500. } else {
  501. distance = delta_value / coefficient_value;
  502. PrintDebug("StrongSIV test found distance as " + ToString(distance));
  503. }
  504. } else {
  505. // If we can't fold delta and coefficient to single values we can't produce
  506. // distance.
  507. // As a result we can't perform the rest of the pass and must assume
  508. // dependence in all directions.
  509. PrintDebug("StrongSIVTest could not produce a distance. Must exit.");
  510. distance_entry->distance = DistanceEntry::Directions::ALL;
  511. return false;
  512. }
  513. // Next we gather the upper and lower bounds as constants if possible. If
  514. // distance > upper_bound - lower_bound we prove independence.
  515. SENode* lower_bound = GetLowerBound(subscript_loop);
  516. SENode* upper_bound = GetUpperBound(subscript_loop);
  517. if (lower_bound && upper_bound) {
  518. PrintDebug("StrongSIVTest found bounds.");
  519. SENode* bounds = scalar_evolution_.SimplifyExpression(
  520. scalar_evolution_.CreateSubtraction(upper_bound, lower_bound));
  521. if (bounds->GetType() == SENode::SENodeType::Constant) {
  522. int64_t bounds_value = bounds->AsSEConstantNode()->FoldToSingleValue();
  523. PrintDebug(
  524. "StrongSIVTest found upper_bound - lower_bound as a constant with "
  525. "value " +
  526. ToString(bounds_value));
  527. // If the absolute value of the distance is > upper bound - lower bound
  528. // then we prove independence.
  529. if (llabs(distance) > llabs(bounds_value)) {
  530. PrintDebug(
  531. "StrongSIVTest proved independence through distance escaping the "
  532. "loop bounds.");
  533. distance_entry->dependence_information =
  534. DistanceEntry::DependenceInformation::DISTANCE;
  535. distance_entry->direction = DistanceEntry::Directions::NONE;
  536. distance_entry->distance = distance;
  537. return true;
  538. }
  539. }
  540. } else {
  541. PrintDebug("StrongSIVTest was unable to gather lower and upper bounds.");
  542. }
  543. // Otherwise we can get a direction as follows
  544. // { < if distance > 0
  545. // direction = { = if distance == 0
  546. // { > if distance < 0
  547. PrintDebug(
  548. "StrongSIVTest could not prove independence. Gathering direction "
  549. "information.");
  550. if (distance > 0) {
  551. distance_entry->dependence_information =
  552. DistanceEntry::DependenceInformation::DISTANCE;
  553. distance_entry->direction = DistanceEntry::Directions::LT;
  554. distance_entry->distance = distance;
  555. return false;
  556. }
  557. if (distance == 0) {
  558. distance_entry->dependence_information =
  559. DistanceEntry::DependenceInformation::DISTANCE;
  560. distance_entry->direction = DistanceEntry::Directions::EQ;
  561. distance_entry->distance = 0;
  562. return false;
  563. }
  564. if (distance < 0) {
  565. distance_entry->dependence_information =
  566. DistanceEntry::DependenceInformation::DISTANCE;
  567. distance_entry->direction = DistanceEntry::Directions::GT;
  568. distance_entry->distance = distance;
  569. return false;
  570. }
  571. // We were unable to prove independence or discern any additional information
  572. // Must assume <=> direction.
  573. PrintDebug(
  574. "StrongSIVTest was unable to determine any dependence information.");
  575. distance_entry->direction = DistanceEntry::Directions::ALL;
  576. return false;
  577. }
  578. bool LoopDependenceAnalysis::SymbolicStrongSIVTest(
  579. SENode* source, SENode* destination, SENode* coefficient,
  580. DistanceEntry* distance_entry) {
  581. PrintDebug("Performing SymbolicStrongSIVTest.");
  582. SENode* source_destination_delta = scalar_evolution_.SimplifyExpression(
  583. scalar_evolution_.CreateSubtraction(source, destination));
  584. // By cancelling out the induction variables by subtracting the source and
  585. // destination we can produce an expression of symbolics and constants. This
  586. // expression can be compared to the loop bounds to find if the offset is
  587. // outwith the bounds.
  588. std::pair<SENode*, SENode*> subscript_pair =
  589. std::make_pair(source, destination);
  590. const Loop* subscript_loop = GetLoopForSubscriptPair(subscript_pair);
  591. if (IsProvablyOutsideOfLoopBounds(subscript_loop, source_destination_delta,
  592. coefficient)) {
  593. PrintDebug(
  594. "SymbolicStrongSIVTest proved independence through loop bounds.");
  595. distance_entry->dependence_information =
  596. DistanceEntry::DependenceInformation::DIRECTION;
  597. distance_entry->direction = DistanceEntry::Directions::NONE;
  598. return true;
  599. }
  600. // We were unable to prove independence or discern any additional information.
  601. // Must assume <=> direction.
  602. PrintDebug(
  603. "SymbolicStrongSIVTest was unable to determine any dependence "
  604. "information.");
  605. distance_entry->direction = DistanceEntry::Directions::ALL;
  606. return false;
  607. }
  608. bool LoopDependenceAnalysis::WeakZeroSourceSIVTest(
  609. SENode* source, SERecurrentNode* destination, SENode* coefficient,
  610. DistanceEntry* distance_entry) {
  611. PrintDebug("Performing WeakZeroSourceSIVTest.");
  612. std::pair<SENode*, SENode*> subscript_pair =
  613. std::make_pair(source, destination);
  614. const Loop* subscript_loop = GetLoopForSubscriptPair(subscript_pair);
  615. // Build an SENode for distance.
  616. SENode* destination_constant_term =
  617. GetConstantTerm(subscript_loop, destination);
  618. SENode* delta = scalar_evolution_.SimplifyExpression(
  619. scalar_evolution_.CreateSubtraction(source, destination_constant_term));
  620. // Scalar evolution doesn't perform division, so we must fold to constants and
  621. // do it manually.
  622. int64_t distance = 0;
  623. SEConstantNode* delta_constant = delta->AsSEConstantNode();
  624. SEConstantNode* coefficient_constant = coefficient->AsSEConstantNode();
  625. if (delta_constant && coefficient_constant) {
  626. PrintDebug(
  627. "WeakZeroSourceSIVTest folding delta and coefficient to constants.");
  628. int64_t delta_value = delta_constant->FoldToSingleValue();
  629. int64_t coefficient_value = coefficient_constant->FoldToSingleValue();
  630. // Check if the distance is not integral.
  631. if (delta_value % coefficient_value != 0) {
  632. PrintDebug(
  633. "WeakZeroSourceSIVTest proved independence through distance not "
  634. "being an integer.");
  635. distance_entry->dependence_information =
  636. DistanceEntry::DependenceInformation::DIRECTION;
  637. distance_entry->direction = DistanceEntry::Directions::NONE;
  638. return true;
  639. } else {
  640. distance = delta_value / coefficient_value;
  641. PrintDebug(
  642. "WeakZeroSourceSIVTest calculated distance with the following "
  643. "values\n"
  644. "\tdelta value: " +
  645. ToString(delta_value) +
  646. "\n\tcoefficient value: " + ToString(coefficient_value) +
  647. "\n\tdistance: " + ToString(distance) + "\n");
  648. }
  649. } else {
  650. PrintDebug(
  651. "WeakZeroSourceSIVTest was unable to fold delta and coefficient to "
  652. "constants.");
  653. }
  654. // If we can prove the distance is outside the bounds we prove independence.
  655. SEConstantNode* lower_bound =
  656. GetLowerBound(subscript_loop)->AsSEConstantNode();
  657. SEConstantNode* upper_bound =
  658. GetUpperBound(subscript_loop)->AsSEConstantNode();
  659. if (lower_bound && upper_bound) {
  660. PrintDebug("WeakZeroSourceSIVTest found bounds as SEConstantNodes.");
  661. int64_t lower_bound_value = lower_bound->FoldToSingleValue();
  662. int64_t upper_bound_value = upper_bound->FoldToSingleValue();
  663. if (!IsWithinBounds(llabs(distance), lower_bound_value,
  664. upper_bound_value)) {
  665. PrintDebug(
  666. "WeakZeroSourceSIVTest proved independence through distance escaping "
  667. "the loop bounds.");
  668. PrintDebug(
  669. "Bound values were as follow\n"
  670. "\tlower bound value: " +
  671. ToString(lower_bound_value) +
  672. "\n\tupper bound value: " + ToString(upper_bound_value) +
  673. "\n\tdistance value: " + ToString(distance) + "\n");
  674. distance_entry->dependence_information =
  675. DistanceEntry::DependenceInformation::DISTANCE;
  676. distance_entry->direction = DistanceEntry::Directions::NONE;
  677. distance_entry->distance = distance;
  678. return true;
  679. }
  680. } else {
  681. PrintDebug(
  682. "WeakZeroSourceSIVTest was unable to find lower and upper bound as "
  683. "SEConstantNodes.");
  684. }
  685. // Now we want to see if we can detect to peel the first or last iterations.
  686. // We get the FirstTripValue as GetFirstTripInductionNode() +
  687. // GetConstantTerm(destination)
  688. SENode* first_trip_SENode =
  689. scalar_evolution_.SimplifyExpression(scalar_evolution_.CreateAddNode(
  690. GetFirstTripInductionNode(subscript_loop),
  691. GetConstantTerm(subscript_loop, destination)));
  692. // If source == FirstTripValue, peel_first.
  693. if (first_trip_SENode) {
  694. PrintDebug("WeakZeroSourceSIVTest built first_trip_SENode.");
  695. if (first_trip_SENode->AsSEConstantNode()) {
  696. PrintDebug(
  697. "WeakZeroSourceSIVTest has found first_trip_SENode as an "
  698. "SEConstantNode with value: " +
  699. ToString(first_trip_SENode->AsSEConstantNode()->FoldToSingleValue()) +
  700. "\n");
  701. }
  702. if (source == first_trip_SENode) {
  703. // We have found that peeling the first iteration will break dependency.
  704. PrintDebug(
  705. "WeakZeroSourceSIVTest has found peeling first iteration will break "
  706. "dependency");
  707. distance_entry->dependence_information =
  708. DistanceEntry::DependenceInformation::PEEL;
  709. distance_entry->peel_first = true;
  710. return false;
  711. }
  712. } else {
  713. PrintDebug("WeakZeroSourceSIVTest was unable to build first_trip_SENode");
  714. }
  715. // We get the LastTripValue as GetFinalTripInductionNode(coefficient) +
  716. // GetConstantTerm(destination)
  717. SENode* final_trip_SENode =
  718. scalar_evolution_.SimplifyExpression(scalar_evolution_.CreateAddNode(
  719. GetFinalTripInductionNode(subscript_loop, coefficient),
  720. GetConstantTerm(subscript_loop, destination)));
  721. // If source == LastTripValue, peel_last.
  722. if (final_trip_SENode) {
  723. PrintDebug("WeakZeroSourceSIVTest built final_trip_SENode.");
  724. if (first_trip_SENode->AsSEConstantNode()) {
  725. PrintDebug(
  726. "WeakZeroSourceSIVTest has found final_trip_SENode as an "
  727. "SEConstantNode with value: " +
  728. ToString(final_trip_SENode->AsSEConstantNode()->FoldToSingleValue()) +
  729. "\n");
  730. }
  731. if (source == final_trip_SENode) {
  732. // We have found that peeling the last iteration will break dependency.
  733. PrintDebug(
  734. "WeakZeroSourceSIVTest has found peeling final iteration will break "
  735. "dependency");
  736. distance_entry->dependence_information =
  737. DistanceEntry::DependenceInformation::PEEL;
  738. distance_entry->peel_last = true;
  739. return false;
  740. }
  741. } else {
  742. PrintDebug("WeakZeroSourceSIVTest was unable to build final_trip_SENode");
  743. }
  744. // We were unable to prove independence or discern any additional information.
  745. // Must assume <=> direction.
  746. PrintDebug(
  747. "WeakZeroSourceSIVTest was unable to determine any dependence "
  748. "information.");
  749. distance_entry->direction = DistanceEntry::Directions::ALL;
  750. return false;
  751. }
  752. bool LoopDependenceAnalysis::WeakZeroDestinationSIVTest(
  753. SERecurrentNode* source, SENode* destination, SENode* coefficient,
  754. DistanceEntry* distance_entry) {
  755. PrintDebug("Performing WeakZeroDestinationSIVTest.");
  756. // Build an SENode for distance.
  757. std::pair<SENode*, SENode*> subscript_pair =
  758. std::make_pair(source, destination);
  759. const Loop* subscript_loop = GetLoopForSubscriptPair(subscript_pair);
  760. SENode* source_constant_term = GetConstantTerm(subscript_loop, source);
  761. SENode* delta = scalar_evolution_.SimplifyExpression(
  762. scalar_evolution_.CreateSubtraction(destination, source_constant_term));
  763. // Scalar evolution doesn't perform division, so we must fold to constants and
  764. // do it manually.
  765. int64_t distance = 0;
  766. SEConstantNode* delta_constant = delta->AsSEConstantNode();
  767. SEConstantNode* coefficient_constant = coefficient->AsSEConstantNode();
  768. if (delta_constant && coefficient_constant) {
  769. PrintDebug(
  770. "WeakZeroDestinationSIVTest folding delta and coefficient to "
  771. "constants.");
  772. int64_t delta_value = delta_constant->FoldToSingleValue();
  773. int64_t coefficient_value = coefficient_constant->FoldToSingleValue();
  774. // Check if the distance is not integral.
  775. if (delta_value % coefficient_value != 0) {
  776. PrintDebug(
  777. "WeakZeroDestinationSIVTest proved independence through distance not "
  778. "being an integer.");
  779. distance_entry->dependence_information =
  780. DistanceEntry::DependenceInformation::DIRECTION;
  781. distance_entry->direction = DistanceEntry::Directions::NONE;
  782. return true;
  783. } else {
  784. distance = delta_value / coefficient_value;
  785. PrintDebug(
  786. "WeakZeroDestinationSIVTest calculated distance with the following "
  787. "values\n"
  788. "\tdelta value: " +
  789. ToString(delta_value) +
  790. "\n\tcoefficient value: " + ToString(coefficient_value) +
  791. "\n\tdistance: " + ToString(distance) + "\n");
  792. }
  793. } else {
  794. PrintDebug(
  795. "WeakZeroDestinationSIVTest was unable to fold delta and coefficient "
  796. "to constants.");
  797. }
  798. // If we can prove the distance is outside the bounds we prove independence.
  799. SEConstantNode* lower_bound =
  800. GetLowerBound(subscript_loop)->AsSEConstantNode();
  801. SEConstantNode* upper_bound =
  802. GetUpperBound(subscript_loop)->AsSEConstantNode();
  803. if (lower_bound && upper_bound) {
  804. PrintDebug("WeakZeroDestinationSIVTest found bounds as SEConstantNodes.");
  805. int64_t lower_bound_value = lower_bound->FoldToSingleValue();
  806. int64_t upper_bound_value = upper_bound->FoldToSingleValue();
  807. if (!IsWithinBounds(llabs(distance), lower_bound_value,
  808. upper_bound_value)) {
  809. PrintDebug(
  810. "WeakZeroDestinationSIVTest proved independence through distance "
  811. "escaping the loop bounds.");
  812. PrintDebug(
  813. "Bound values were as follows\n"
  814. "\tlower bound value: " +
  815. ToString(lower_bound_value) +
  816. "\n\tupper bound value: " + ToString(upper_bound_value) +
  817. "\n\tdistance value: " + ToString(distance));
  818. distance_entry->dependence_information =
  819. DistanceEntry::DependenceInformation::DISTANCE;
  820. distance_entry->direction = DistanceEntry::Directions::NONE;
  821. distance_entry->distance = distance;
  822. return true;
  823. }
  824. } else {
  825. PrintDebug(
  826. "WeakZeroDestinationSIVTest was unable to find lower and upper bound "
  827. "as SEConstantNodes.");
  828. }
  829. // Now we want to see if we can detect to peel the first or last iterations.
  830. // We get the FirstTripValue as GetFirstTripInductionNode() +
  831. // GetConstantTerm(source)
  832. SENode* first_trip_SENode = scalar_evolution_.SimplifyExpression(
  833. scalar_evolution_.CreateAddNode(GetFirstTripInductionNode(subscript_loop),
  834. GetConstantTerm(subscript_loop, source)));
  835. // If destination == FirstTripValue, peel_first.
  836. if (first_trip_SENode) {
  837. PrintDebug("WeakZeroDestinationSIVTest built first_trip_SENode.");
  838. if (first_trip_SENode->AsSEConstantNode()) {
  839. PrintDebug(
  840. "WeakZeroDestinationSIVTest has found first_trip_SENode as an "
  841. "SEConstantNode with value: " +
  842. ToString(first_trip_SENode->AsSEConstantNode()->FoldToSingleValue()) +
  843. "\n");
  844. }
  845. if (destination == first_trip_SENode) {
  846. // We have found that peeling the first iteration will break dependency.
  847. PrintDebug(
  848. "WeakZeroDestinationSIVTest has found peeling first iteration will "
  849. "break dependency");
  850. distance_entry->dependence_information =
  851. DistanceEntry::DependenceInformation::PEEL;
  852. distance_entry->peel_first = true;
  853. return false;
  854. }
  855. } else {
  856. PrintDebug(
  857. "WeakZeroDestinationSIVTest was unable to build first_trip_SENode");
  858. }
  859. // We get the LastTripValue as GetFinalTripInductionNode(coefficient) +
  860. // GetConstantTerm(source)
  861. SENode* final_trip_SENode =
  862. scalar_evolution_.SimplifyExpression(scalar_evolution_.CreateAddNode(
  863. GetFinalTripInductionNode(subscript_loop, coefficient),
  864. GetConstantTerm(subscript_loop, source)));
  865. // If destination == LastTripValue, peel_last.
  866. if (final_trip_SENode) {
  867. PrintDebug("WeakZeroDestinationSIVTest built final_trip_SENode.");
  868. if (final_trip_SENode->AsSEConstantNode()) {
  869. PrintDebug(
  870. "WeakZeroDestinationSIVTest has found final_trip_SENode as an "
  871. "SEConstantNode with value: " +
  872. ToString(final_trip_SENode->AsSEConstantNode()->FoldToSingleValue()) +
  873. "\n");
  874. }
  875. if (destination == final_trip_SENode) {
  876. // We have found that peeling the last iteration will break dependency.
  877. PrintDebug(
  878. "WeakZeroDestinationSIVTest has found peeling final iteration will "
  879. "break dependency");
  880. distance_entry->dependence_information =
  881. DistanceEntry::DependenceInformation::PEEL;
  882. distance_entry->peel_last = true;
  883. return false;
  884. }
  885. } else {
  886. PrintDebug(
  887. "WeakZeroDestinationSIVTest was unable to build final_trip_SENode");
  888. }
  889. // We were unable to prove independence or discern any additional information.
  890. // Must assume <=> direction.
  891. PrintDebug(
  892. "WeakZeroDestinationSIVTest was unable to determine any dependence "
  893. "information.");
  894. distance_entry->direction = DistanceEntry::Directions::ALL;
  895. return false;
  896. }
  897. bool LoopDependenceAnalysis::WeakCrossingSIVTest(
  898. SENode* source, SENode* destination, SENode* coefficient,
  899. DistanceEntry* distance_entry) {
  900. PrintDebug("Performing WeakCrossingSIVTest.");
  901. // We currently can't handle symbolic WeakCrossingSIVTests. If either source
  902. // or destination are not SERecurrentNodes we must exit.
  903. if (!source->AsSERecurrentNode() || !destination->AsSERecurrentNode()) {
  904. PrintDebug(
  905. "WeakCrossingSIVTest found source or destination != SERecurrentNode. "
  906. "Exiting");
  907. distance_entry->direction = DistanceEntry::Directions::ALL;
  908. return false;
  909. }
  910. // Build an SENode for distance.
  911. SENode* offset_delta =
  912. scalar_evolution_.SimplifyExpression(scalar_evolution_.CreateSubtraction(
  913. destination->AsSERecurrentNode()->GetOffset(),
  914. source->AsSERecurrentNode()->GetOffset()));
  915. // Scalar evolution doesn't perform division, so we must fold to constants and
  916. // do it manually.
  917. int64_t distance = 0;
  918. SEConstantNode* delta_constant = offset_delta->AsSEConstantNode();
  919. SEConstantNode* coefficient_constant = coefficient->AsSEConstantNode();
  920. if (delta_constant && coefficient_constant) {
  921. PrintDebug(
  922. "WeakCrossingSIVTest folding offset_delta and coefficient to "
  923. "constants.");
  924. int64_t delta_value = delta_constant->FoldToSingleValue();
  925. int64_t coefficient_value = coefficient_constant->FoldToSingleValue();
  926. // Check if the distance is not integral or if it has a non-integral part
  927. // equal to 1/2.
  928. if (delta_value % (2 * coefficient_value) != 0 &&
  929. static_cast<float>(delta_value % (2 * coefficient_value)) /
  930. static_cast<float>(2 * coefficient_value) !=
  931. 0.5) {
  932. PrintDebug(
  933. "WeakCrossingSIVTest proved independence through distance escaping "
  934. "the loop bounds.");
  935. distance_entry->dependence_information =
  936. DistanceEntry::DependenceInformation::DIRECTION;
  937. distance_entry->direction = DistanceEntry::Directions::NONE;
  938. return true;
  939. } else {
  940. distance = delta_value / (2 * coefficient_value);
  941. }
  942. if (distance == 0) {
  943. PrintDebug("WeakCrossingSIVTest found EQ dependence.");
  944. distance_entry->dependence_information =
  945. DistanceEntry::DependenceInformation::DISTANCE;
  946. distance_entry->direction = DistanceEntry::Directions::EQ;
  947. distance_entry->distance = 0;
  948. return false;
  949. }
  950. } else {
  951. PrintDebug(
  952. "WeakCrossingSIVTest was unable to fold offset_delta and coefficient "
  953. "to constants.");
  954. }
  955. // We were unable to prove independence or discern any additional information.
  956. // Must assume <=> direction.
  957. PrintDebug(
  958. "WeakCrossingSIVTest was unable to determine any dependence "
  959. "information.");
  960. distance_entry->direction = DistanceEntry::Directions::ALL;
  961. return false;
  962. }
  963. // Perform the GCD test if both, the source and the destination nodes, are in
  964. // the form a0*i0 + a1*i1 + ... an*in + c.
  965. bool LoopDependenceAnalysis::GCDMIVTest(
  966. const std::pair<SENode*, SENode*>& subscript_pair) {
  967. auto source = std::get<0>(subscript_pair);
  968. auto destination = std::get<1>(subscript_pair);
  969. // Bail out if source/destination is in an unexpected form.
  970. if (!IsInCorrectFormForGCDTest(source) ||
  971. !IsInCorrectFormForGCDTest(destination)) {
  972. return false;
  973. }
  974. auto source_recurrences = GetAllTopLevelRecurrences(source);
  975. auto dest_recurrences = GetAllTopLevelRecurrences(destination);
  976. // Bail out if all offsets and coefficients aren't constant.
  977. if (!AreOffsetsAndCoefficientsConstant(source_recurrences) ||
  978. !AreOffsetsAndCoefficientsConstant(dest_recurrences)) {
  979. return false;
  980. }
  981. // Calculate the GCD of all coefficients.
  982. auto source_constants = GetAllTopLevelConstants(source);
  983. int64_t source_constant =
  984. CalculateConstantTerm(source_recurrences, source_constants);
  985. auto dest_constants = GetAllTopLevelConstants(destination);
  986. int64_t destination_constant =
  987. CalculateConstantTerm(dest_recurrences, dest_constants);
  988. int64_t delta = std::abs(source_constant - destination_constant);
  989. int64_t running_gcd = 0;
  990. running_gcd = CalculateGCDFromCoefficients(source_recurrences, running_gcd);
  991. running_gcd = CalculateGCDFromCoefficients(dest_recurrences, running_gcd);
  992. return delta % running_gcd != 0;
  993. }
  994. using PartitionedSubscripts =
  995. std::vector<std::set<std::pair<Instruction*, Instruction*>>>;
  996. PartitionedSubscripts LoopDependenceAnalysis::PartitionSubscripts(
  997. const std::vector<Instruction*>& source_subscripts,
  998. const std::vector<Instruction*>& destination_subscripts) {
  999. PartitionedSubscripts partitions{};
  1000. auto num_subscripts = source_subscripts.size();
  1001. // Create initial partitions with one subscript pair per partition.
  1002. for (size_t i = 0; i < num_subscripts; ++i) {
  1003. partitions.push_back({{source_subscripts[i], destination_subscripts[i]}});
  1004. }
  1005. // Iterate over the loops to create all partitions
  1006. for (auto loop : loops_) {
  1007. int64_t k = -1;
  1008. for (size_t j = 0; j < partitions.size(); ++j) {
  1009. auto& current_partition = partitions[j];
  1010. // Does |loop| appear in |current_partition|
  1011. auto it = std::find_if(
  1012. current_partition.begin(), current_partition.end(),
  1013. [loop,
  1014. this](const std::pair<Instruction*, Instruction*>& elem) -> bool {
  1015. auto source_recurrences =
  1016. scalar_evolution_.AnalyzeInstruction(std::get<0>(elem))
  1017. ->CollectRecurrentNodes();
  1018. auto destination_recurrences =
  1019. scalar_evolution_.AnalyzeInstruction(std::get<1>(elem))
  1020. ->CollectRecurrentNodes();
  1021. source_recurrences.insert(source_recurrences.end(),
  1022. destination_recurrences.begin(),
  1023. destination_recurrences.end());
  1024. auto loops_in_pair = CollectLoops(source_recurrences);
  1025. auto end_it = loops_in_pair.end();
  1026. return std::find(loops_in_pair.begin(), end_it, loop) != end_it;
  1027. });
  1028. auto has_loop = it != current_partition.end();
  1029. if (has_loop) {
  1030. if (k == -1) {
  1031. k = j;
  1032. } else {
  1033. // Add |partitions[j]| to |partitions[k]| and discard |partitions[j]|
  1034. partitions[static_cast<size_t>(k)].insert(current_partition.begin(),
  1035. current_partition.end());
  1036. current_partition.clear();
  1037. }
  1038. }
  1039. }
  1040. }
  1041. // Remove empty (discarded) partitions
  1042. partitions.erase(
  1043. std::remove_if(
  1044. partitions.begin(), partitions.end(),
  1045. [](const std::set<std::pair<Instruction*, Instruction*>>& partition) {
  1046. return partition.empty();
  1047. }),
  1048. partitions.end());
  1049. return partitions;
  1050. }
  1051. Constraint* LoopDependenceAnalysis::IntersectConstraints(
  1052. Constraint* constraint_0, Constraint* constraint_1,
  1053. const SENode* lower_bound, const SENode* upper_bound) {
  1054. if (constraint_0->AsDependenceNone()) {
  1055. return constraint_1;
  1056. } else if (constraint_1->AsDependenceNone()) {
  1057. return constraint_0;
  1058. }
  1059. // Both constraints are distances. Either the same distance or independent.
  1060. if (constraint_0->AsDependenceDistance() &&
  1061. constraint_1->AsDependenceDistance()) {
  1062. auto dist_0 = constraint_0->AsDependenceDistance();
  1063. auto dist_1 = constraint_1->AsDependenceDistance();
  1064. if (*dist_0->GetDistance() == *dist_1->GetDistance()) {
  1065. return constraint_0;
  1066. } else {
  1067. return make_constraint<DependenceEmpty>();
  1068. }
  1069. }
  1070. // Both constraints are points. Either the same point or independent.
  1071. if (constraint_0->AsDependencePoint() && constraint_1->AsDependencePoint()) {
  1072. auto point_0 = constraint_0->AsDependencePoint();
  1073. auto point_1 = constraint_1->AsDependencePoint();
  1074. if (*point_0->GetSource() == *point_1->GetSource() &&
  1075. *point_0->GetDestination() == *point_1->GetDestination()) {
  1076. return constraint_0;
  1077. } else {
  1078. return make_constraint<DependenceEmpty>();
  1079. }
  1080. }
  1081. // Both constraints are lines/distances.
  1082. if ((constraint_0->AsDependenceDistance() ||
  1083. constraint_0->AsDependenceLine()) &&
  1084. (constraint_1->AsDependenceDistance() ||
  1085. constraint_1->AsDependenceLine())) {
  1086. auto is_distance_0 = constraint_0->AsDependenceDistance() != nullptr;
  1087. auto is_distance_1 = constraint_1->AsDependenceDistance() != nullptr;
  1088. auto a0 = is_distance_0 ? scalar_evolution_.CreateConstant(1)
  1089. : constraint_0->AsDependenceLine()->GetA();
  1090. auto b0 = is_distance_0 ? scalar_evolution_.CreateConstant(-1)
  1091. : constraint_0->AsDependenceLine()->GetB();
  1092. auto c0 =
  1093. is_distance_0
  1094. ? scalar_evolution_.SimplifyExpression(
  1095. scalar_evolution_.CreateNegation(
  1096. constraint_0->AsDependenceDistance()->GetDistance()))
  1097. : constraint_0->AsDependenceLine()->GetC();
  1098. auto a1 = is_distance_1 ? scalar_evolution_.CreateConstant(1)
  1099. : constraint_1->AsDependenceLine()->GetA();
  1100. auto b1 = is_distance_1 ? scalar_evolution_.CreateConstant(-1)
  1101. : constraint_1->AsDependenceLine()->GetB();
  1102. auto c1 =
  1103. is_distance_1
  1104. ? scalar_evolution_.SimplifyExpression(
  1105. scalar_evolution_.CreateNegation(
  1106. constraint_1->AsDependenceDistance()->GetDistance()))
  1107. : constraint_1->AsDependenceLine()->GetC();
  1108. if (a0->AsSEConstantNode() && b0->AsSEConstantNode() &&
  1109. c0->AsSEConstantNode() && a1->AsSEConstantNode() &&
  1110. b1->AsSEConstantNode() && c1->AsSEConstantNode()) {
  1111. auto constant_a0 = a0->AsSEConstantNode()->FoldToSingleValue();
  1112. auto constant_b0 = b0->AsSEConstantNode()->FoldToSingleValue();
  1113. auto constant_c0 = c0->AsSEConstantNode()->FoldToSingleValue();
  1114. auto constant_a1 = a1->AsSEConstantNode()->FoldToSingleValue();
  1115. auto constant_b1 = b1->AsSEConstantNode()->FoldToSingleValue();
  1116. auto constant_c1 = c1->AsSEConstantNode()->FoldToSingleValue();
  1117. // a & b can't both be zero, otherwise it wouldn't be line.
  1118. if (NormalizeAndCompareFractions(constant_a0, constant_b0, constant_a1,
  1119. constant_b1)) {
  1120. // Slopes are equal, either parallel lines or the same line.
  1121. if (constant_b0 == 0 && constant_b1 == 0) {
  1122. if (NormalizeAndCompareFractions(constant_c0, constant_a0,
  1123. constant_c1, constant_a1)) {
  1124. return constraint_0;
  1125. }
  1126. return make_constraint<DependenceEmpty>();
  1127. } else if (NormalizeAndCompareFractions(constant_c0, constant_b0,
  1128. constant_c1, constant_b1)) {
  1129. // Same line.
  1130. return constraint_0;
  1131. } else {
  1132. // Parallel lines can't intersect, report independence.
  1133. return make_constraint<DependenceEmpty>();
  1134. }
  1135. } else {
  1136. // Lines are not parallel, therefore, they must intersect.
  1137. // Calculate intersection.
  1138. if (upper_bound->AsSEConstantNode() &&
  1139. lower_bound->AsSEConstantNode()) {
  1140. auto constant_lower_bound =
  1141. lower_bound->AsSEConstantNode()->FoldToSingleValue();
  1142. auto constant_upper_bound =
  1143. upper_bound->AsSEConstantNode()->FoldToSingleValue();
  1144. auto up = constant_b1 * constant_c0 - constant_b0 * constant_c1;
  1145. // Both b or both a can't be 0, so down is never 0
  1146. // otherwise would have entered the parallel line section.
  1147. auto down = constant_b1 * constant_a0 - constant_b0 * constant_a1;
  1148. auto x_coord = up / down;
  1149. int64_t y_coord = 0;
  1150. int64_t arg1 = 0;
  1151. int64_t const_b_to_use = 0;
  1152. if (constant_b1 != 0) {
  1153. arg1 = constant_c1 - constant_a1 * x_coord;
  1154. y_coord = arg1 / constant_b1;
  1155. const_b_to_use = constant_b1;
  1156. } else if (constant_b0 != 0) {
  1157. arg1 = constant_c0 - constant_a0 * x_coord;
  1158. y_coord = arg1 / constant_b0;
  1159. const_b_to_use = constant_b0;
  1160. }
  1161. if (up % down == 0 &&
  1162. arg1 % const_b_to_use == 0 && // Coordinates are integers.
  1163. constant_lower_bound <=
  1164. x_coord && // x_coord is within loop bounds.
  1165. x_coord <= constant_upper_bound &&
  1166. constant_lower_bound <=
  1167. y_coord && // y_coord is within loop bounds.
  1168. y_coord <= constant_upper_bound) {
  1169. // Lines intersect at integer coordinates.
  1170. return make_constraint<DependencePoint>(
  1171. scalar_evolution_.CreateConstant(x_coord),
  1172. scalar_evolution_.CreateConstant(y_coord),
  1173. constraint_0->GetLoop());
  1174. } else {
  1175. return make_constraint<DependenceEmpty>();
  1176. }
  1177. } else {
  1178. // Not constants, bail out.
  1179. return make_constraint<DependenceNone>();
  1180. }
  1181. }
  1182. } else {
  1183. // Not constants, bail out.
  1184. return make_constraint<DependenceNone>();
  1185. }
  1186. }
  1187. // One constraint is a line/distance and the other is a point.
  1188. if ((constraint_0->AsDependencePoint() &&
  1189. (constraint_1->AsDependenceLine() ||
  1190. constraint_1->AsDependenceDistance())) ||
  1191. (constraint_1->AsDependencePoint() &&
  1192. (constraint_0->AsDependenceLine() ||
  1193. constraint_0->AsDependenceDistance()))) {
  1194. auto point_0 = constraint_0->AsDependencePoint() != nullptr;
  1195. auto point = point_0 ? constraint_0->AsDependencePoint()
  1196. : constraint_1->AsDependencePoint();
  1197. auto line_or_distance = point_0 ? constraint_1 : constraint_0;
  1198. auto is_distance = line_or_distance->AsDependenceDistance() != nullptr;
  1199. auto a = is_distance ? scalar_evolution_.CreateConstant(1)
  1200. : line_or_distance->AsDependenceLine()->GetA();
  1201. auto b = is_distance ? scalar_evolution_.CreateConstant(-1)
  1202. : line_or_distance->AsDependenceLine()->GetB();
  1203. auto c =
  1204. is_distance
  1205. ? scalar_evolution_.SimplifyExpression(
  1206. scalar_evolution_.CreateNegation(
  1207. line_or_distance->AsDependenceDistance()->GetDistance()))
  1208. : line_or_distance->AsDependenceLine()->GetC();
  1209. auto x = point->GetSource();
  1210. auto y = point->GetDestination();
  1211. if (a->AsSEConstantNode() && b->AsSEConstantNode() &&
  1212. c->AsSEConstantNode() && x->AsSEConstantNode() &&
  1213. y->AsSEConstantNode()) {
  1214. auto constant_a = a->AsSEConstantNode()->FoldToSingleValue();
  1215. auto constant_b = b->AsSEConstantNode()->FoldToSingleValue();
  1216. auto constant_c = c->AsSEConstantNode()->FoldToSingleValue();
  1217. auto constant_x = x->AsSEConstantNode()->FoldToSingleValue();
  1218. auto constant_y = y->AsSEConstantNode()->FoldToSingleValue();
  1219. auto left_hand_side = constant_a * constant_x + constant_b * constant_y;
  1220. if (left_hand_side == constant_c) {
  1221. // Point is on line, return point
  1222. return point_0 ? constraint_0 : constraint_1;
  1223. } else {
  1224. // Point not on line, report independence (empty constraint).
  1225. return make_constraint<DependenceEmpty>();
  1226. }
  1227. } else {
  1228. // Not constants, bail out.
  1229. return make_constraint<DependenceNone>();
  1230. }
  1231. }
  1232. return nullptr;
  1233. }
  1234. // Propagate constraints function as described in section 5 of Practical
  1235. // Dependence Testing, Goff, Kennedy, Tseng, 1991.
  1236. SubscriptPair LoopDependenceAnalysis::PropagateConstraints(
  1237. const SubscriptPair& subscript_pair,
  1238. const std::vector<Constraint*>& constraints) {
  1239. SENode* new_first = subscript_pair.first;
  1240. SENode* new_second = subscript_pair.second;
  1241. for (auto& constraint : constraints) {
  1242. // In the paper this is a[k]. We're extracting the coefficient ('a') of a
  1243. // recurrent expression with respect to the loop 'k'.
  1244. SENode* coefficient_of_recurrent =
  1245. scalar_evolution_.GetCoefficientFromRecurrentTerm(
  1246. new_first, constraint->GetLoop());
  1247. // In the paper this is a'[k].
  1248. SENode* coefficient_of_recurrent_prime =
  1249. scalar_evolution_.GetCoefficientFromRecurrentTerm(
  1250. new_second, constraint->GetLoop());
  1251. if (constraint->GetType() == Constraint::Distance) {
  1252. DependenceDistance* as_distance = constraint->AsDependenceDistance();
  1253. // In the paper this is a[k]*d
  1254. SENode* rhs = scalar_evolution_.CreateMultiplyNode(
  1255. coefficient_of_recurrent, as_distance->GetDistance());
  1256. // In the paper this is a[k] <- 0
  1257. SENode* zeroed_coefficient =
  1258. scalar_evolution_.BuildGraphWithoutRecurrentTerm(
  1259. new_first, constraint->GetLoop());
  1260. // In the paper this is e <- e - a[k]*d.
  1261. new_first = scalar_evolution_.CreateSubtraction(zeroed_coefficient, rhs);
  1262. new_first = scalar_evolution_.SimplifyExpression(new_first);
  1263. // In the paper this is a'[k] - a[k].
  1264. SENode* new_child = scalar_evolution_.SimplifyExpression(
  1265. scalar_evolution_.CreateSubtraction(coefficient_of_recurrent_prime,
  1266. coefficient_of_recurrent));
  1267. // In the paper this is a'[k]'i[k].
  1268. SERecurrentNode* prime_recurrent =
  1269. scalar_evolution_.GetRecurrentTerm(new_second, constraint->GetLoop());
  1270. if (!prime_recurrent) continue;
  1271. // As we hash the nodes we need to create a new node when we update a
  1272. // child.
  1273. SENode* new_recurrent = scalar_evolution_.CreateRecurrentExpression(
  1274. constraint->GetLoop(), prime_recurrent->GetOffset(), new_child);
  1275. // In the paper this is a'[k] <- a'[k] - a[k].
  1276. new_second = scalar_evolution_.UpdateChildNode(
  1277. new_second, prime_recurrent, new_recurrent);
  1278. }
  1279. }
  1280. new_second = scalar_evolution_.SimplifyExpression(new_second);
  1281. return std::make_pair(new_first, new_second);
  1282. }
  1283. bool LoopDependenceAnalysis::DeltaTest(
  1284. const std::vector<SubscriptPair>& coupled_subscripts,
  1285. DistanceVector* dv_entry) {
  1286. std::vector<Constraint*> constraints(loops_.size());
  1287. std::vector<bool> loop_appeared(loops_.size());
  1288. std::generate(std::begin(constraints), std::end(constraints),
  1289. [this]() { return make_constraint<DependenceNone>(); });
  1290. // Separate SIV and MIV subscripts
  1291. std::vector<SubscriptPair> siv_subscripts{};
  1292. std::vector<SubscriptPair> miv_subscripts{};
  1293. for (const auto& subscript_pair : coupled_subscripts) {
  1294. if (IsSIV(subscript_pair)) {
  1295. siv_subscripts.push_back(subscript_pair);
  1296. } else {
  1297. miv_subscripts.push_back(subscript_pair);
  1298. }
  1299. }
  1300. // Delta Test
  1301. while (!siv_subscripts.empty()) {
  1302. std::vector<bool> results(siv_subscripts.size());
  1303. std::vector<DistanceVector> current_distances(
  1304. siv_subscripts.size(), DistanceVector(loops_.size()));
  1305. // Apply SIV test to all SIV subscripts, report independence if any of them
  1306. // is independent
  1307. std::transform(
  1308. std::begin(siv_subscripts), std::end(siv_subscripts),
  1309. std::begin(current_distances), std::begin(results),
  1310. [this](SubscriptPair& p, DistanceVector& d) { return SIVTest(p, &d); });
  1311. if (std::accumulate(std::begin(results), std::end(results), false,
  1312. std::logical_or<bool>{})) {
  1313. return true;
  1314. }
  1315. // Derive new constraint vector.
  1316. std::vector<std::pair<Constraint*, size_t>> all_new_constrants{};
  1317. for (size_t i = 0; i < siv_subscripts.size(); ++i) {
  1318. auto loop = GetLoopForSubscriptPair(siv_subscripts[i]);
  1319. auto loop_id =
  1320. std::distance(std::begin(loops_),
  1321. std::find(std::begin(loops_), std::end(loops_), loop));
  1322. loop_appeared[loop_id] = true;
  1323. auto distance_entry = current_distances[i].GetEntries()[loop_id];
  1324. if (distance_entry.dependence_information ==
  1325. DistanceEntry::DependenceInformation::DISTANCE) {
  1326. // Construct a DependenceDistance.
  1327. auto node = scalar_evolution_.CreateConstant(distance_entry.distance);
  1328. all_new_constrants.push_back(
  1329. {make_constraint<DependenceDistance>(node, loop), loop_id});
  1330. } else {
  1331. // Construct a DependenceLine.
  1332. const auto& subscript_pair = siv_subscripts[i];
  1333. SENode* source_node = std::get<0>(subscript_pair);
  1334. SENode* destination_node = std::get<1>(subscript_pair);
  1335. int64_t source_induction_count = CountInductionVariables(source_node);
  1336. int64_t destination_induction_count =
  1337. CountInductionVariables(destination_node);
  1338. SENode* a = nullptr;
  1339. SENode* b = nullptr;
  1340. SENode* c = nullptr;
  1341. if (destination_induction_count != 0) {
  1342. a = destination_node->AsSERecurrentNode()->GetCoefficient();
  1343. c = scalar_evolution_.CreateNegation(
  1344. destination_node->AsSERecurrentNode()->GetOffset());
  1345. } else {
  1346. a = scalar_evolution_.CreateConstant(0);
  1347. c = scalar_evolution_.CreateNegation(destination_node);
  1348. }
  1349. if (source_induction_count != 0) {
  1350. b = scalar_evolution_.CreateNegation(
  1351. source_node->AsSERecurrentNode()->GetCoefficient());
  1352. c = scalar_evolution_.CreateAddNode(
  1353. c, source_node->AsSERecurrentNode()->GetOffset());
  1354. } else {
  1355. b = scalar_evolution_.CreateConstant(0);
  1356. c = scalar_evolution_.CreateAddNode(c, source_node);
  1357. }
  1358. a = scalar_evolution_.SimplifyExpression(a);
  1359. b = scalar_evolution_.SimplifyExpression(b);
  1360. c = scalar_evolution_.SimplifyExpression(c);
  1361. all_new_constrants.push_back(
  1362. {make_constraint<DependenceLine>(a, b, c, loop), loop_id});
  1363. }
  1364. }
  1365. // Calculate the intersection between the new and existing constraints.
  1366. std::vector<Constraint*> intersection = constraints;
  1367. for (const auto& constraint_to_intersect : all_new_constrants) {
  1368. auto loop_id = std::get<1>(constraint_to_intersect);
  1369. auto loop = loops_[loop_id];
  1370. intersection[loop_id] = IntersectConstraints(
  1371. intersection[loop_id], std::get<0>(constraint_to_intersect),
  1372. GetLowerBound(loop), GetUpperBound(loop));
  1373. }
  1374. // Report independence if an empty constraint (DependenceEmpty) is found.
  1375. auto first_empty =
  1376. std::find_if(std::begin(intersection), std::end(intersection),
  1377. [](Constraint* constraint) {
  1378. return constraint->AsDependenceEmpty() != nullptr;
  1379. });
  1380. if (first_empty != std::end(intersection)) {
  1381. return true;
  1382. }
  1383. std::vector<SubscriptPair> new_siv_subscripts{};
  1384. std::vector<SubscriptPair> new_miv_subscripts{};
  1385. auto equal =
  1386. std::equal(std::begin(constraints), std::end(constraints),
  1387. std::begin(intersection),
  1388. [](Constraint* a, Constraint* b) { return *a == *b; });
  1389. // If any constraints have changed, propagate them into the rest of the
  1390. // subscripts possibly creating new ZIV/SIV subscripts.
  1391. if (!equal) {
  1392. std::vector<SubscriptPair> new_subscripts(miv_subscripts.size());
  1393. // Propagate constraints into MIV subscripts
  1394. std::transform(std::begin(miv_subscripts), std::end(miv_subscripts),
  1395. std::begin(new_subscripts),
  1396. [this, &intersection](SubscriptPair& subscript_pair) {
  1397. return PropagateConstraints(subscript_pair,
  1398. intersection);
  1399. });
  1400. // If a ZIV subscript is returned, apply test, otherwise, update untested
  1401. // subscripts.
  1402. for (auto& subscript : new_subscripts) {
  1403. if (IsZIV(subscript) && ZIVTest(subscript)) {
  1404. return true;
  1405. } else if (IsSIV(subscript)) {
  1406. new_siv_subscripts.push_back(subscript);
  1407. } else {
  1408. new_miv_subscripts.push_back(subscript);
  1409. }
  1410. }
  1411. }
  1412. // Set new constraints and subscripts to test.
  1413. std::swap(siv_subscripts, new_siv_subscripts);
  1414. std::swap(miv_subscripts, new_miv_subscripts);
  1415. std::swap(constraints, intersection);
  1416. }
  1417. // Create the dependence vector from the constraints.
  1418. for (size_t i = 0; i < loops_.size(); ++i) {
  1419. // Don't touch entries for loops that weren't tested.
  1420. if (loop_appeared[i]) {
  1421. auto current_constraint = constraints[i];
  1422. auto& current_distance_entry = (*dv_entry).GetEntries()[i];
  1423. if (auto dependence_distance =
  1424. current_constraint->AsDependenceDistance()) {
  1425. if (auto constant_node =
  1426. dependence_distance->GetDistance()->AsSEConstantNode()) {
  1427. current_distance_entry.dependence_information =
  1428. DistanceEntry::DependenceInformation::DISTANCE;
  1429. current_distance_entry.distance = constant_node->FoldToSingleValue();
  1430. if (current_distance_entry.distance == 0) {
  1431. current_distance_entry.direction = DistanceEntry::Directions::EQ;
  1432. } else if (current_distance_entry.distance < 0) {
  1433. current_distance_entry.direction = DistanceEntry::Directions::GT;
  1434. } else {
  1435. current_distance_entry.direction = DistanceEntry::Directions::LT;
  1436. }
  1437. }
  1438. } else if (auto dependence_point =
  1439. current_constraint->AsDependencePoint()) {
  1440. auto source = dependence_point->GetSource();
  1441. auto destination = dependence_point->GetDestination();
  1442. if (source->AsSEConstantNode() && destination->AsSEConstantNode()) {
  1443. current_distance_entry = DistanceEntry(
  1444. source->AsSEConstantNode()->FoldToSingleValue(),
  1445. destination->AsSEConstantNode()->FoldToSingleValue());
  1446. }
  1447. }
  1448. }
  1449. }
  1450. // Test any remaining MIV subscripts and report independence if found.
  1451. std::vector<bool> results(miv_subscripts.size());
  1452. std::transform(std::begin(miv_subscripts), std::end(miv_subscripts),
  1453. std::begin(results),
  1454. [this](const SubscriptPair& p) { return GCDMIVTest(p); });
  1455. return std::accumulate(std::begin(results), std::end(results), false,
  1456. std::logical_or<bool>{});
  1457. }
  1458. } // namespace opt
  1459. } // namespace spvtools