DFGDominators.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2011 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
  17. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  20. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  21. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "DFGDominators.h"
  27. #if ENABLE(DFG_JIT)
  28. #include "DFGGraph.h"
  29. namespace JSC { namespace DFG {
  30. Dominators::Dominators()
  31. : m_valid(false)
  32. {
  33. }
  34. Dominators::~Dominators()
  35. {
  36. }
  37. void Dominators::compute(Graph& graph)
  38. {
  39. // This implements a naive dominator solver.
  40. ASSERT(graph.m_blocks[0]->m_predecessors.isEmpty());
  41. unsigned numBlocks = graph.m_blocks.size();
  42. if (numBlocks > m_results.size()) {
  43. m_results.grow(numBlocks);
  44. for (unsigned i = numBlocks; i--;)
  45. m_results[i].resize(numBlocks);
  46. m_scratch.resize(numBlocks);
  47. }
  48. m_results[0].clearAll();
  49. m_results[0].set(0);
  50. m_scratch.clearAll();
  51. for (unsigned i = numBlocks; i--;) {
  52. if (!graph.m_blocks[i])
  53. continue;
  54. m_scratch.set(i);
  55. }
  56. for (unsigned i = numBlocks; i-- > 1;) {
  57. if (!graph.m_blocks[i] || graph.m_blocks[i]->m_predecessors.isEmpty())
  58. m_results[i].clearAll();
  59. else
  60. m_results[i].set(m_scratch);
  61. }
  62. bool changed;
  63. do {
  64. changed = false;
  65. for (unsigned i = 1; i < numBlocks; ++i)
  66. changed |= iterateForBlock(graph, i);
  67. if (!changed)
  68. break;
  69. changed = false;
  70. for (unsigned i = numBlocks; i-- > 1;)
  71. changed |= iterateForBlock(graph, i);
  72. } while (changed);
  73. m_valid = true;
  74. }
  75. bool Dominators::iterateForBlock(Graph& graph, BlockIndex i)
  76. {
  77. BasicBlock* block = graph.m_blocks[i].get();
  78. if (!block)
  79. return false;
  80. if (block->m_predecessors.isEmpty())
  81. return false;
  82. m_scratch.set(m_results[block->m_predecessors[0]]);
  83. for (unsigned j = block->m_predecessors.size(); j-- > 1;)
  84. m_scratch.filter(m_results[block->m_predecessors[j]]);
  85. m_scratch.set(i);
  86. return m_results[i].setAndCheck(m_scratch);
  87. }
  88. } } // namespace JSC::DFG
  89. #endif // ENABLE(DFG_JIT)