loop_dependence.cpp 64 KB

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