scalar_analysis.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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/scalar_analysis.h"
  15. #include <algorithm>
  16. #include <functional>
  17. #include <string>
  18. #include <utility>
  19. #include "source/opt/ir_context.h"
  20. // Transforms a given scalar operation instruction into a DAG representation.
  21. //
  22. // 1. Take an instruction and traverse its operands until we reach a
  23. // constant node or an instruction which we do not know how to compute the
  24. // value, such as a load.
  25. //
  26. // 2. Create a new node for each instruction traversed and build the nodes for
  27. // the in operands of that instruction as well.
  28. //
  29. // 3. Add the operand nodes as children of the first and hash the node. Use the
  30. // hash to see if the node is already in the cache. We ensure the children are
  31. // always in sorted order so that two nodes with the same children but inserted
  32. // in a different order have the same hash and so that the overloaded operator==
  33. // will return true. If the node is already in the cache return the cached
  34. // version instead.
  35. //
  36. // 4. The created DAG can then be simplified by
  37. // ScalarAnalysis::SimplifyExpression, implemented in
  38. // scalar_analysis_simplification.cpp. See that file for further information on
  39. // the simplification process.
  40. //
  41. namespace spvtools {
  42. namespace opt {
  43. uint32_t SENode::NumberOfNodes = 0;
  44. ScalarEvolutionAnalysis::ScalarEvolutionAnalysis(IRContext* context)
  45. : context_(context), pretend_equal_{} {
  46. // Create and cached the CantComputeNode.
  47. cached_cant_compute_ =
  48. GetCachedOrAdd(std::unique_ptr<SECantCompute>(new SECantCompute(this)));
  49. }
  50. SENode* ScalarEvolutionAnalysis::CreateNegation(SENode* operand) {
  51. // If operand is can't compute then the whole graph is can't compute.
  52. if (operand->IsCantCompute()) return CreateCantComputeNode();
  53. if (operand->GetType() == SENode::Constant) {
  54. return CreateConstant(-operand->AsSEConstantNode()->FoldToSingleValue());
  55. }
  56. std::unique_ptr<SENode> negation_node{new SENegative(this)};
  57. negation_node->AddChild(operand);
  58. return GetCachedOrAdd(std::move(negation_node));
  59. }
  60. SENode* ScalarEvolutionAnalysis::CreateConstant(int64_t integer) {
  61. return GetCachedOrAdd(
  62. std::unique_ptr<SENode>(new SEConstantNode(this, integer)));
  63. }
  64. SENode* ScalarEvolutionAnalysis::CreateRecurrentExpression(
  65. const Loop* loop, SENode* offset, SENode* coefficient) {
  66. assert(loop && "Recurrent add expressions must have a valid loop.");
  67. // If operands are can't compute then the whole graph is can't compute.
  68. if (offset->IsCantCompute() || coefficient->IsCantCompute())
  69. return CreateCantComputeNode();
  70. const Loop* loop_to_use = nullptr;
  71. if (pretend_equal_[loop]) {
  72. loop_to_use = pretend_equal_[loop];
  73. } else {
  74. loop_to_use = loop;
  75. }
  76. std::unique_ptr<SERecurrentNode> phi_node{
  77. new SERecurrentNode(this, loop_to_use)};
  78. phi_node->AddOffset(offset);
  79. phi_node->AddCoefficient(coefficient);
  80. return GetCachedOrAdd(std::move(phi_node));
  81. }
  82. SENode* ScalarEvolutionAnalysis::AnalyzeMultiplyOp(
  83. const Instruction* multiply) {
  84. assert(multiply->opcode() == SpvOp::SpvOpIMul &&
  85. "Multiply node did not come from a multiply instruction");
  86. analysis::DefUseManager* def_use = context_->get_def_use_mgr();
  87. SENode* op1 =
  88. AnalyzeInstruction(def_use->GetDef(multiply->GetSingleWordInOperand(0)));
  89. SENode* op2 =
  90. AnalyzeInstruction(def_use->GetDef(multiply->GetSingleWordInOperand(1)));
  91. return CreateMultiplyNode(op1, op2);
  92. }
  93. SENode* ScalarEvolutionAnalysis::CreateMultiplyNode(SENode* operand_1,
  94. SENode* operand_2) {
  95. // If operands are can't compute then the whole graph is can't compute.
  96. if (operand_1->IsCantCompute() || operand_2->IsCantCompute())
  97. return CreateCantComputeNode();
  98. if (operand_1->GetType() == SENode::Constant &&
  99. operand_2->GetType() == SENode::Constant) {
  100. return CreateConstant(operand_1->AsSEConstantNode()->FoldToSingleValue() *
  101. operand_2->AsSEConstantNode()->FoldToSingleValue());
  102. }
  103. std::unique_ptr<SENode> multiply_node{new SEMultiplyNode(this)};
  104. multiply_node->AddChild(operand_1);
  105. multiply_node->AddChild(operand_2);
  106. return GetCachedOrAdd(std::move(multiply_node));
  107. }
  108. SENode* ScalarEvolutionAnalysis::CreateSubtraction(SENode* operand_1,
  109. SENode* operand_2) {
  110. // Fold if both operands are constant.
  111. if (operand_1->GetType() == SENode::Constant &&
  112. operand_2->GetType() == SENode::Constant) {
  113. return CreateConstant(operand_1->AsSEConstantNode()->FoldToSingleValue() -
  114. operand_2->AsSEConstantNode()->FoldToSingleValue());
  115. }
  116. return CreateAddNode(operand_1, CreateNegation(operand_2));
  117. }
  118. SENode* ScalarEvolutionAnalysis::CreateAddNode(SENode* operand_1,
  119. SENode* operand_2) {
  120. // Fold if both operands are constant and the |simplify| flag is true.
  121. if (operand_1->GetType() == SENode::Constant &&
  122. operand_2->GetType() == SENode::Constant) {
  123. return CreateConstant(operand_1->AsSEConstantNode()->FoldToSingleValue() +
  124. operand_2->AsSEConstantNode()->FoldToSingleValue());
  125. }
  126. // If operands are can't compute then the whole graph is can't compute.
  127. if (operand_1->IsCantCompute() || operand_2->IsCantCompute())
  128. return CreateCantComputeNode();
  129. std::unique_ptr<SENode> add_node{new SEAddNode(this)};
  130. add_node->AddChild(operand_1);
  131. add_node->AddChild(operand_2);
  132. return GetCachedOrAdd(std::move(add_node));
  133. }
  134. SENode* ScalarEvolutionAnalysis::AnalyzeInstruction(const Instruction* inst) {
  135. auto itr = recurrent_node_map_.find(inst);
  136. if (itr != recurrent_node_map_.end()) return itr->second;
  137. SENode* output = nullptr;
  138. switch (inst->opcode()) {
  139. case SpvOp::SpvOpPhi: {
  140. output = AnalyzePhiInstruction(inst);
  141. break;
  142. }
  143. case SpvOp::SpvOpConstant:
  144. case SpvOp::SpvOpConstantNull: {
  145. output = AnalyzeConstant(inst);
  146. break;
  147. }
  148. case SpvOp::SpvOpISub:
  149. case SpvOp::SpvOpIAdd: {
  150. output = AnalyzeAddOp(inst);
  151. break;
  152. }
  153. case SpvOp::SpvOpIMul: {
  154. output = AnalyzeMultiplyOp(inst);
  155. break;
  156. }
  157. default: {
  158. output = CreateValueUnknownNode(inst);
  159. break;
  160. }
  161. }
  162. return output;
  163. }
  164. SENode* ScalarEvolutionAnalysis::AnalyzeConstant(const Instruction* inst) {
  165. if (inst->opcode() == SpvOp::SpvOpConstantNull) return CreateConstant(0);
  166. assert(inst->opcode() == SpvOp::SpvOpConstant);
  167. assert(inst->NumInOperands() == 1);
  168. int64_t value = 0;
  169. // Look up the instruction in the constant manager.
  170. const analysis::Constant* constant =
  171. context_->get_constant_mgr()->FindDeclaredConstant(inst->result_id());
  172. if (!constant) return CreateCantComputeNode();
  173. const analysis::IntConstant* int_constant = constant->AsIntConstant();
  174. // Exit out if it is a 64 bit integer.
  175. if (!int_constant || int_constant->words().size() != 1)
  176. return CreateCantComputeNode();
  177. if (int_constant->type()->AsInteger()->IsSigned()) {
  178. value = int_constant->GetS32BitValue();
  179. } else {
  180. value = int_constant->GetU32BitValue();
  181. }
  182. return CreateConstant(value);
  183. }
  184. // Handles both addition and subtraction. If the |sub| flag is set then the
  185. // addition will be op1+(-op2) otherwise op1+op2.
  186. SENode* ScalarEvolutionAnalysis::AnalyzeAddOp(const Instruction* inst) {
  187. assert((inst->opcode() == SpvOp::SpvOpIAdd ||
  188. inst->opcode() == SpvOp::SpvOpISub) &&
  189. "Add node must be created from a OpIAdd or OpISub instruction");
  190. analysis::DefUseManager* def_use = context_->get_def_use_mgr();
  191. SENode* op1 =
  192. AnalyzeInstruction(def_use->GetDef(inst->GetSingleWordInOperand(0)));
  193. SENode* op2 =
  194. AnalyzeInstruction(def_use->GetDef(inst->GetSingleWordInOperand(1)));
  195. // To handle subtraction we wrap the second operand in a unary negation node.
  196. if (inst->opcode() == SpvOp::SpvOpISub) {
  197. op2 = CreateNegation(op2);
  198. }
  199. return CreateAddNode(op1, op2);
  200. }
  201. SENode* ScalarEvolutionAnalysis::AnalyzePhiInstruction(const Instruction* phi) {
  202. // The phi should only have two incoming value pairs.
  203. if (phi->NumInOperands() != 4) {
  204. return CreateCantComputeNode();
  205. }
  206. analysis::DefUseManager* def_use = context_->get_def_use_mgr();
  207. // Get the basic block this instruction belongs to.
  208. BasicBlock* basic_block =
  209. context_->get_instr_block(const_cast<Instruction*>(phi));
  210. // And then the function that the basic blocks belongs to.
  211. Function* function = basic_block->GetParent();
  212. // Use the function to get the loop descriptor.
  213. LoopDescriptor* loop_descriptor = context_->GetLoopDescriptor(function);
  214. // We only handle phis in loops at the moment.
  215. if (!loop_descriptor) return CreateCantComputeNode();
  216. // Get the innermost loop which this block belongs to.
  217. Loop* loop = (*loop_descriptor)[basic_block->id()];
  218. // If the loop doesn't exist or doesn't have a preheader or latch block, exit
  219. // out.
  220. if (!loop || !loop->GetLatchBlock() || !loop->GetPreHeaderBlock() ||
  221. loop->GetHeaderBlock() != basic_block)
  222. return recurrent_node_map_[phi] = CreateCantComputeNode();
  223. const Loop* loop_to_use = nullptr;
  224. if (pretend_equal_[loop]) {
  225. loop_to_use = pretend_equal_[loop];
  226. } else {
  227. loop_to_use = loop;
  228. }
  229. std::unique_ptr<SERecurrentNode> phi_node{
  230. new SERecurrentNode(this, loop_to_use)};
  231. // We add the node to this map to allow it to be returned before the node is
  232. // fully built. This is needed as the subsequent call to AnalyzeInstruction
  233. // could lead back to this |phi| instruction so we return the pointer
  234. // immediately in AnalyzeInstruction to break the recursion.
  235. recurrent_node_map_[phi] = phi_node.get();
  236. // Traverse the operands of the instruction an create new nodes for each one.
  237. for (uint32_t i = 0; i < phi->NumInOperands(); i += 2) {
  238. uint32_t value_id = phi->GetSingleWordInOperand(i);
  239. uint32_t incoming_label_id = phi->GetSingleWordInOperand(i + 1);
  240. Instruction* value_inst = def_use->GetDef(value_id);
  241. SENode* value_node = AnalyzeInstruction(value_inst);
  242. // If any operand is CantCompute then the whole graph is CantCompute.
  243. if (value_node->IsCantCompute())
  244. return recurrent_node_map_[phi] = CreateCantComputeNode();
  245. // If the value is coming from the preheader block then the value is the
  246. // initial value of the phi.
  247. if (incoming_label_id == loop->GetPreHeaderBlock()->id()) {
  248. phi_node->AddOffset(value_node);
  249. } else if (incoming_label_id == loop->GetLatchBlock()->id()) {
  250. // Assumed to be in the form of step + phi.
  251. if (value_node->GetType() != SENode::Add)
  252. return recurrent_node_map_[phi] = CreateCantComputeNode();
  253. SENode* step_node = nullptr;
  254. SENode* phi_operand = nullptr;
  255. SENode* operand_1 = value_node->GetChild(0);
  256. SENode* operand_2 = value_node->GetChild(1);
  257. // Find which node is the step term.
  258. if (!operand_1->AsSERecurrentNode())
  259. step_node = operand_1;
  260. else if (!operand_2->AsSERecurrentNode())
  261. step_node = operand_2;
  262. // Find which node is the recurrent expression.
  263. if (operand_1->AsSERecurrentNode())
  264. phi_operand = operand_1;
  265. else if (operand_2->AsSERecurrentNode())
  266. phi_operand = operand_2;
  267. // If it is not in the form step + phi exit out.
  268. if (!(step_node && phi_operand))
  269. return recurrent_node_map_[phi] = CreateCantComputeNode();
  270. // If the phi operand is not the same phi node exit out.
  271. if (phi_operand != phi_node.get())
  272. return recurrent_node_map_[phi] = CreateCantComputeNode();
  273. if (!IsLoopInvariant(loop, step_node))
  274. return recurrent_node_map_[phi] = CreateCantComputeNode();
  275. phi_node->AddCoefficient(step_node);
  276. }
  277. }
  278. // Once the node is fully built we update the map with the version from the
  279. // cache (if it has already been added to the cache).
  280. return recurrent_node_map_[phi] = GetCachedOrAdd(std::move(phi_node));
  281. }
  282. SENode* ScalarEvolutionAnalysis::CreateValueUnknownNode(
  283. const Instruction* inst) {
  284. std::unique_ptr<SEValueUnknown> load_node{
  285. new SEValueUnknown(this, inst->result_id())};
  286. return GetCachedOrAdd(std::move(load_node));
  287. }
  288. SENode* ScalarEvolutionAnalysis::CreateCantComputeNode() {
  289. return cached_cant_compute_;
  290. }
  291. // Add the created node into the cache of nodes. If it already exists return it.
  292. SENode* ScalarEvolutionAnalysis::GetCachedOrAdd(
  293. std::unique_ptr<SENode> prospective_node) {
  294. auto itr = node_cache_.find(prospective_node);
  295. if (itr != node_cache_.end()) {
  296. return (*itr).get();
  297. }
  298. SENode* raw_ptr_to_node = prospective_node.get();
  299. node_cache_.insert(std::move(prospective_node));
  300. return raw_ptr_to_node;
  301. }
  302. bool ScalarEvolutionAnalysis::IsLoopInvariant(const Loop* loop,
  303. const SENode* node) const {
  304. for (auto itr = node->graph_cbegin(); itr != node->graph_cend(); ++itr) {
  305. if (const SERecurrentNode* rec = itr->AsSERecurrentNode()) {
  306. const BasicBlock* header = rec->GetLoop()->GetHeaderBlock();
  307. // If the loop which the recurrent expression belongs to is either |loop
  308. // or a nested loop inside |loop| then we assume it is variant.
  309. if (loop->IsInsideLoop(header)) {
  310. return false;
  311. }
  312. } else if (const SEValueUnknown* unknown = itr->AsSEValueUnknown()) {
  313. // If the instruction is inside the loop we conservatively assume it is
  314. // loop variant.
  315. if (loop->IsInsideLoop(unknown->ResultId())) return false;
  316. }
  317. }
  318. return true;
  319. }
  320. SENode* ScalarEvolutionAnalysis::GetCoefficientFromRecurrentTerm(
  321. SENode* node, const Loop* loop) {
  322. // Traverse the DAG to find the recurrent expression belonging to |loop|.
  323. for (auto itr = node->graph_begin(); itr != node->graph_end(); ++itr) {
  324. SERecurrentNode* rec = itr->AsSERecurrentNode();
  325. if (rec && rec->GetLoop() == loop) {
  326. return rec->GetCoefficient();
  327. }
  328. }
  329. return CreateConstant(0);
  330. }
  331. SENode* ScalarEvolutionAnalysis::UpdateChildNode(SENode* parent,
  332. SENode* old_child,
  333. SENode* new_child) {
  334. // Only handles add.
  335. if (parent->GetType() != SENode::Add) return parent;
  336. std::vector<SENode*> new_children;
  337. for (SENode* child : *parent) {
  338. if (child == old_child) {
  339. new_children.push_back(new_child);
  340. } else {
  341. new_children.push_back(child);
  342. }
  343. }
  344. std::unique_ptr<SENode> add_node{new SEAddNode(this)};
  345. for (SENode* child : new_children) {
  346. add_node->AddChild(child);
  347. }
  348. return SimplifyExpression(GetCachedOrAdd(std::move(add_node)));
  349. }
  350. // Rebuild the |node| eliminating, if it exists, the recurrent term which
  351. // belongs to the |loop|.
  352. SENode* ScalarEvolutionAnalysis::BuildGraphWithoutRecurrentTerm(
  353. SENode* node, const Loop* loop) {
  354. // If the node is already a recurrent expression belonging to loop then just
  355. // return the offset.
  356. SERecurrentNode* recurrent = node->AsSERecurrentNode();
  357. if (recurrent) {
  358. if (recurrent->GetLoop() == loop) {
  359. return recurrent->GetOffset();
  360. } else {
  361. return node;
  362. }
  363. }
  364. std::vector<SENode*> new_children;
  365. // Otherwise find the recurrent node in the children of this node.
  366. for (auto itr : *node) {
  367. recurrent = itr->AsSERecurrentNode();
  368. if (recurrent && recurrent->GetLoop() == loop) {
  369. new_children.push_back(recurrent->GetOffset());
  370. } else {
  371. new_children.push_back(itr);
  372. }
  373. }
  374. std::unique_ptr<SENode> add_node{new SEAddNode(this)};
  375. for (SENode* child : new_children) {
  376. add_node->AddChild(child);
  377. }
  378. return SimplifyExpression(GetCachedOrAdd(std::move(add_node)));
  379. }
  380. // Return the recurrent term belonging to |loop| if it appears in the graph
  381. // starting at |node| or null if it doesn't.
  382. SERecurrentNode* ScalarEvolutionAnalysis::GetRecurrentTerm(SENode* node,
  383. const Loop* loop) {
  384. for (auto itr = node->graph_begin(); itr != node->graph_end(); ++itr) {
  385. SERecurrentNode* rec = itr->AsSERecurrentNode();
  386. if (rec && rec->GetLoop() == loop) {
  387. return rec;
  388. }
  389. }
  390. return nullptr;
  391. }
  392. std::string SENode::AsString() const {
  393. switch (GetType()) {
  394. case Constant:
  395. return "Constant";
  396. case RecurrentAddExpr:
  397. return "RecurrentAddExpr";
  398. case Add:
  399. return "Add";
  400. case Negative:
  401. return "Negative";
  402. case Multiply:
  403. return "Multiply";
  404. case ValueUnknown:
  405. return "Value Unknown";
  406. case CanNotCompute:
  407. return "Can not compute";
  408. }
  409. return "NULL";
  410. }
  411. bool SENode::operator==(const SENode& other) const {
  412. if (GetType() != other.GetType()) return false;
  413. if (other.GetChildren().size() != children_.size()) return false;
  414. const SERecurrentNode* this_as_recurrent = AsSERecurrentNode();
  415. // Check the children are the same, for SERecurrentNodes we need to check the
  416. // offset and coefficient manually as the child vector is sorted by ids so the
  417. // offset/coefficient information is lost.
  418. if (!this_as_recurrent) {
  419. for (size_t index = 0; index < children_.size(); ++index) {
  420. if (other.GetChildren()[index] != children_[index]) return false;
  421. }
  422. } else {
  423. const SERecurrentNode* other_as_recurrent = other.AsSERecurrentNode();
  424. // We've already checked the types are the same, this should not fail if
  425. // this->AsSERecurrentNode() succeeded.
  426. assert(other_as_recurrent);
  427. if (this_as_recurrent->GetCoefficient() !=
  428. other_as_recurrent->GetCoefficient())
  429. return false;
  430. if (this_as_recurrent->GetOffset() != other_as_recurrent->GetOffset())
  431. return false;
  432. if (this_as_recurrent->GetLoop() != other_as_recurrent->GetLoop())
  433. return false;
  434. }
  435. // If we're dealing with a value unknown node check both nodes were created by
  436. // the same instruction.
  437. if (GetType() == SENode::ValueUnknown) {
  438. if (AsSEValueUnknown()->ResultId() !=
  439. other.AsSEValueUnknown()->ResultId()) {
  440. return false;
  441. }
  442. }
  443. if (AsSEConstantNode()) {
  444. if (AsSEConstantNode()->FoldToSingleValue() !=
  445. other.AsSEConstantNode()->FoldToSingleValue())
  446. return false;
  447. }
  448. return true;
  449. }
  450. bool SENode::operator!=(const SENode& other) const { return !(*this == other); }
  451. namespace {
  452. // Helper functions to insert 32/64 bit values into the 32 bit hash string. This
  453. // allows us to add pointers to the string by reinterpreting the pointers as
  454. // uintptr_t. PushToString will deduce the type, call sizeof on it and use
  455. // that size to call into the correct PushToStringImpl functor depending on
  456. // whether it is 32 or 64 bit.
  457. template <typename T, size_t size_of_t>
  458. struct PushToStringImpl;
  459. template <typename T>
  460. struct PushToStringImpl<T, 8> {
  461. void operator()(T id, std::u32string* str) {
  462. str->push_back(static_cast<uint32_t>(id >> 32));
  463. str->push_back(static_cast<uint32_t>(id));
  464. }
  465. };
  466. template <typename T>
  467. struct PushToStringImpl<T, 4> {
  468. void operator()(T id, std::u32string* str) {
  469. str->push_back(static_cast<uint32_t>(id));
  470. }
  471. };
  472. template <typename T>
  473. static void PushToString(T id, std::u32string* str) {
  474. PushToStringImpl<T, sizeof(T)>{}(id, str);
  475. }
  476. } // namespace
  477. // Implements the hashing of SENodes.
  478. size_t SENodeHash::operator()(const SENode* node) const {
  479. // Concatinate the terms into a string which we can hash.
  480. std::u32string hash_string{};
  481. // Hashing the type as a string is safer than hashing the enum as the enum is
  482. // very likely to collide with constants.
  483. for (char ch : node->AsString()) {
  484. hash_string.push_back(static_cast<char32_t>(ch));
  485. }
  486. // We just ignore the literal value unless it is a constant.
  487. if (node->GetType() == SENode::Constant)
  488. PushToString(node->AsSEConstantNode()->FoldToSingleValue(), &hash_string);
  489. const SERecurrentNode* recurrent = node->AsSERecurrentNode();
  490. // If we're dealing with a recurrent expression hash the loop as well so that
  491. // nested inductions like i=0,i++ and j=0,j++ correspond to different nodes.
  492. if (recurrent) {
  493. PushToString(reinterpret_cast<uintptr_t>(recurrent->GetLoop()),
  494. &hash_string);
  495. // Recurrent expressions can't be hashed using the normal method as the
  496. // order of coefficient and offset matters to the hash.
  497. PushToString(reinterpret_cast<uintptr_t>(recurrent->GetCoefficient()),
  498. &hash_string);
  499. PushToString(reinterpret_cast<uintptr_t>(recurrent->GetOffset()),
  500. &hash_string);
  501. return std::hash<std::u32string>{}(hash_string);
  502. }
  503. // Hash the result id of the original instruction which created this node if
  504. // it is a value unknown node.
  505. if (node->GetType() == SENode::ValueUnknown) {
  506. PushToString(node->AsSEValueUnknown()->ResultId(), &hash_string);
  507. }
  508. // Hash the pointers of the child nodes, each SENode has a unique pointer
  509. // associated with it.
  510. const std::vector<SENode*>& children = node->GetChildren();
  511. for (const SENode* child : children) {
  512. PushToString(reinterpret_cast<uintptr_t>(child), &hash_string);
  513. }
  514. return std::hash<std::u32string>{}(hash_string);
  515. }
  516. // This overload is the actual overload used by the node_cache_ set.
  517. size_t SENodeHash::operator()(const std::unique_ptr<SENode>& node) const {
  518. return this->operator()(node.get());
  519. }
  520. void SENode::DumpDot(std::ostream& out, bool recurse) const {
  521. size_t unique_id = std::hash<const SENode*>{}(this);
  522. out << unique_id << " [label=\"" << AsString() << " ";
  523. if (GetType() == SENode::Constant) {
  524. out << "\nwith value: " << this->AsSEConstantNode()->FoldToSingleValue();
  525. }
  526. out << "\"]\n";
  527. for (const SENode* child : children_) {
  528. size_t child_unique_id = std::hash<const SENode*>{}(child);
  529. out << unique_id << " -> " << child_unique_id << " \n";
  530. if (recurse) child->DumpDot(out, true);
  531. }
  532. }
  533. namespace {
  534. class IsGreaterThanZero {
  535. public:
  536. explicit IsGreaterThanZero(IRContext* context) : context_(context) {}
  537. // Determine if the value of |node| is always strictly greater than zero if
  538. // |or_equal_zero| is false or greater or equal to zero if |or_equal_zero| is
  539. // true. It returns true is the evaluation was able to conclude something, in
  540. // which case the result is stored in |result|.
  541. // The algorithm work by going through all the nodes and determine the
  542. // sign of each of them.
  543. bool Eval(const SENode* node, bool or_equal_zero, bool* result) {
  544. *result = false;
  545. switch (Visit(node)) {
  546. case Signedness::kPositiveOrNegative: {
  547. return false;
  548. }
  549. case Signedness::kStrictlyNegative: {
  550. *result = false;
  551. break;
  552. }
  553. case Signedness::kNegative: {
  554. if (!or_equal_zero) {
  555. return false;
  556. }
  557. *result = false;
  558. break;
  559. }
  560. case Signedness::kStrictlyPositive: {
  561. *result = true;
  562. break;
  563. }
  564. case Signedness::kPositive: {
  565. if (!or_equal_zero) {
  566. return false;
  567. }
  568. *result = true;
  569. break;
  570. }
  571. }
  572. return true;
  573. }
  574. private:
  575. enum class Signedness {
  576. kPositiveOrNegative, // Yield a value positive or negative.
  577. kStrictlyNegative, // Yield a value strictly less than 0.
  578. kNegative, // Yield a value less or equal to 0.
  579. kStrictlyPositive, // Yield a value strictly greater than 0.
  580. kPositive // Yield a value greater or equal to 0.
  581. };
  582. // Combine the signedness according to arithmetic rules of a given operator.
  583. using Combiner = std::function<Signedness(Signedness, Signedness)>;
  584. // Returns a functor to interpret the signedness of 2 expressions as if they
  585. // were added.
  586. Combiner GetAddCombiner() const {
  587. return [](Signedness lhs, Signedness rhs) {
  588. switch (lhs) {
  589. case Signedness::kPositiveOrNegative:
  590. break;
  591. case Signedness::kStrictlyNegative:
  592. if (rhs == Signedness::kStrictlyNegative ||
  593. rhs == Signedness::kNegative)
  594. return lhs;
  595. break;
  596. case Signedness::kNegative: {
  597. if (rhs == Signedness::kStrictlyNegative)
  598. return Signedness::kStrictlyNegative;
  599. if (rhs == Signedness::kNegative) return Signedness::kNegative;
  600. break;
  601. }
  602. case Signedness::kStrictlyPositive: {
  603. if (rhs == Signedness::kStrictlyPositive ||
  604. rhs == Signedness::kPositive) {
  605. return Signedness::kStrictlyPositive;
  606. }
  607. break;
  608. }
  609. case Signedness::kPositive: {
  610. if (rhs == Signedness::kStrictlyPositive)
  611. return Signedness::kStrictlyPositive;
  612. if (rhs == Signedness::kPositive) return Signedness::kPositive;
  613. break;
  614. }
  615. }
  616. return Signedness::kPositiveOrNegative;
  617. };
  618. }
  619. // Returns a functor to interpret the signedness of 2 expressions as if they
  620. // were multiplied.
  621. Combiner GetMulCombiner() const {
  622. return [](Signedness lhs, Signedness rhs) {
  623. switch (lhs) {
  624. case Signedness::kPositiveOrNegative:
  625. break;
  626. case Signedness::kStrictlyNegative: {
  627. switch (rhs) {
  628. case Signedness::kPositiveOrNegative: {
  629. break;
  630. }
  631. case Signedness::kStrictlyNegative: {
  632. return Signedness::kStrictlyPositive;
  633. }
  634. case Signedness::kNegative: {
  635. return Signedness::kPositive;
  636. }
  637. case Signedness::kStrictlyPositive: {
  638. return Signedness::kStrictlyNegative;
  639. }
  640. case Signedness::kPositive: {
  641. return Signedness::kNegative;
  642. }
  643. }
  644. break;
  645. }
  646. case Signedness::kNegative: {
  647. switch (rhs) {
  648. case Signedness::kPositiveOrNegative: {
  649. break;
  650. }
  651. case Signedness::kStrictlyNegative:
  652. case Signedness::kNegative: {
  653. return Signedness::kPositive;
  654. }
  655. case Signedness::kStrictlyPositive:
  656. case Signedness::kPositive: {
  657. return Signedness::kNegative;
  658. }
  659. }
  660. break;
  661. }
  662. case Signedness::kStrictlyPositive: {
  663. return rhs;
  664. }
  665. case Signedness::kPositive: {
  666. switch (rhs) {
  667. case Signedness::kPositiveOrNegative: {
  668. break;
  669. }
  670. case Signedness::kStrictlyNegative:
  671. case Signedness::kNegative: {
  672. return Signedness::kNegative;
  673. }
  674. case Signedness::kStrictlyPositive:
  675. case Signedness::kPositive: {
  676. return Signedness::kPositive;
  677. }
  678. }
  679. break;
  680. }
  681. }
  682. return Signedness::kPositiveOrNegative;
  683. };
  684. }
  685. Signedness Visit(const SENode* node) {
  686. switch (node->GetType()) {
  687. case SENode::Constant:
  688. return Visit(node->AsSEConstantNode());
  689. break;
  690. case SENode::RecurrentAddExpr:
  691. return Visit(node->AsSERecurrentNode());
  692. break;
  693. case SENode::Negative:
  694. return Visit(node->AsSENegative());
  695. break;
  696. case SENode::CanNotCompute:
  697. return Visit(node->AsSECantCompute());
  698. break;
  699. case SENode::ValueUnknown:
  700. return Visit(node->AsSEValueUnknown());
  701. break;
  702. case SENode::Add:
  703. return VisitExpr(node, GetAddCombiner());
  704. break;
  705. case SENode::Multiply:
  706. return VisitExpr(node, GetMulCombiner());
  707. break;
  708. }
  709. return Signedness::kPositiveOrNegative;
  710. }
  711. // Returns the signedness of a constant |node|.
  712. Signedness Visit(const SEConstantNode* node) {
  713. if (0 == node->FoldToSingleValue()) return Signedness::kPositive;
  714. if (0 < node->FoldToSingleValue()) return Signedness::kStrictlyPositive;
  715. if (0 > node->FoldToSingleValue()) return Signedness::kStrictlyNegative;
  716. return Signedness::kPositiveOrNegative;
  717. }
  718. // Returns the signedness of an unknown |node| based on its type.
  719. Signedness Visit(const SEValueUnknown* node) {
  720. Instruction* insn = context_->get_def_use_mgr()->GetDef(node->ResultId());
  721. analysis::Type* type = context_->get_type_mgr()->GetType(insn->type_id());
  722. assert(type && "Can't retrieve a type for the instruction");
  723. analysis::Integer* int_type = type->AsInteger();
  724. assert(type && "Can't retrieve an integer type for the instruction");
  725. return int_type->IsSigned() ? Signedness::kPositiveOrNegative
  726. : Signedness::kPositive;
  727. }
  728. // Returns the signedness of a recurring expression.
  729. Signedness Visit(const SERecurrentNode* node) {
  730. Signedness coeff_sign = Visit(node->GetCoefficient());
  731. // SERecurrentNode represent an affine expression in the range [0,
  732. // loop_bound], so the result cannot be strictly positive or negative.
  733. switch (coeff_sign) {
  734. default:
  735. break;
  736. case Signedness::kStrictlyNegative:
  737. coeff_sign = Signedness::kNegative;
  738. break;
  739. case Signedness::kStrictlyPositive:
  740. coeff_sign = Signedness::kPositive;
  741. break;
  742. }
  743. return GetAddCombiner()(coeff_sign, Visit(node->GetOffset()));
  744. }
  745. // Returns the signedness of a negation |node|.
  746. Signedness Visit(const SENegative* node) {
  747. switch (Visit(*node->begin())) {
  748. case Signedness::kPositiveOrNegative: {
  749. return Signedness::kPositiveOrNegative;
  750. }
  751. case Signedness::kStrictlyNegative: {
  752. return Signedness::kStrictlyPositive;
  753. }
  754. case Signedness::kNegative: {
  755. return Signedness::kPositive;
  756. }
  757. case Signedness::kStrictlyPositive: {
  758. return Signedness::kStrictlyNegative;
  759. }
  760. case Signedness::kPositive: {
  761. return Signedness::kNegative;
  762. }
  763. }
  764. return Signedness::kPositiveOrNegative;
  765. }
  766. Signedness Visit(const SECantCompute*) {
  767. return Signedness::kPositiveOrNegative;
  768. }
  769. // Returns the signedness of a binary expression by using the combiner
  770. // |reduce|.
  771. Signedness VisitExpr(
  772. const SENode* node,
  773. std::function<Signedness(Signedness, Signedness)> reduce) {
  774. Signedness result = Visit(*node->begin());
  775. for (const SENode* operand : make_range(++node->begin(), node->end())) {
  776. if (result == Signedness::kPositiveOrNegative) {
  777. return Signedness::kPositiveOrNegative;
  778. }
  779. result = reduce(result, Visit(operand));
  780. }
  781. return result;
  782. }
  783. IRContext* context_;
  784. };
  785. } // namespace
  786. bool ScalarEvolutionAnalysis::IsAlwaysGreaterThanZero(SENode* node,
  787. bool* is_gt_zero) const {
  788. return IsGreaterThanZero(context_).Eval(node, false, is_gt_zero);
  789. }
  790. bool ScalarEvolutionAnalysis::IsAlwaysGreaterOrEqualToZero(
  791. SENode* node, bool* is_ge_zero) const {
  792. return IsGreaterThanZero(context_).Eval(node, true, is_ge_zero);
  793. }
  794. namespace {
  795. // Remove |node| from the |mul| chain (of the form A * ... * |node| * ... * Z),
  796. // if |node| is not in the chain, returns the original chain.
  797. static SENode* RemoveOneNodeFromMultiplyChain(SEMultiplyNode* mul,
  798. const SENode* node) {
  799. SENode* lhs = mul->GetChildren()[0];
  800. SENode* rhs = mul->GetChildren()[1];
  801. if (lhs == node) {
  802. return rhs;
  803. }
  804. if (rhs == node) {
  805. return lhs;
  806. }
  807. if (lhs->AsSEMultiplyNode()) {
  808. SENode* res = RemoveOneNodeFromMultiplyChain(lhs->AsSEMultiplyNode(), node);
  809. if (res != lhs)
  810. return mul->GetParentAnalysis()->CreateMultiplyNode(res, rhs);
  811. }
  812. if (rhs->AsSEMultiplyNode()) {
  813. SENode* res = RemoveOneNodeFromMultiplyChain(rhs->AsSEMultiplyNode(), node);
  814. if (res != rhs)
  815. return mul->GetParentAnalysis()->CreateMultiplyNode(res, rhs);
  816. }
  817. return mul;
  818. }
  819. } // namespace
  820. std::pair<SExpression, int64_t> SExpression::operator/(
  821. SExpression rhs_wrapper) const {
  822. SENode* lhs = node_;
  823. SENode* rhs = rhs_wrapper.node_;
  824. // Check for division by 0.
  825. if (rhs->AsSEConstantNode() &&
  826. !rhs->AsSEConstantNode()->FoldToSingleValue()) {
  827. return {scev_->CreateCantComputeNode(), 0};
  828. }
  829. // Trivial case.
  830. if (lhs->AsSEConstantNode() && rhs->AsSEConstantNode()) {
  831. int64_t lhs_value = lhs->AsSEConstantNode()->FoldToSingleValue();
  832. int64_t rhs_value = rhs->AsSEConstantNode()->FoldToSingleValue();
  833. return {scev_->CreateConstant(lhs_value / rhs_value),
  834. lhs_value % rhs_value};
  835. }
  836. // look for a "c U / U" pattern.
  837. if (lhs->AsSEMultiplyNode()) {
  838. assert(lhs->GetChildren().size() == 2 &&
  839. "More than 2 operand for a multiply node.");
  840. SENode* res = RemoveOneNodeFromMultiplyChain(lhs->AsSEMultiplyNode(), rhs);
  841. if (res != lhs) {
  842. return {res, 0};
  843. }
  844. }
  845. return {scev_->CreateCantComputeNode(), 0};
  846. }
  847. } // namespace opt
  848. } // namespace spvtools