scalar_analysis.cpp 33 KB

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