liveness-analyzer-unittest.cc 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/js-graph.h"
#include "src/compiler/linkage.h"
#include "src/compiler/liveness-analyzer.h"
#include "src/compiler/node-matchers.h"
#include "src/compiler/state-values-utils.h"
#include "test/unittests/compiler/graph-unittest.h"
#include "test/unittests/compiler/node-test-utils.h"

using testing::MakeMatcher;
using testing::MatcherInterface;
using testing::MatchResultListener;
using testing::StringMatchResultListener;

namespace v8 {
namespace internal {
namespace compiler {

class LivenessAnalysisTest : public GraphTest {
 public:
  explicit LivenessAnalysisTest(int locals_count = 4)
      : locals_count_(locals_count),
26
        machine_(zone(), MachineRepresentation::kWord32),
27
        javascript_(zone()),
28 29
        jsgraph_(isolate(), graph(), common(), &javascript_, nullptr,
                 &machine_),
30
        analyzer_(locals_count, false, zone()),
31 32
        empty_values_(graph()->NewNode(
            common()->StateValues(0, SparseInputMask::Dense()), 0, nullptr)),
33 34 35 36 37 38 39 40 41
        next_checkpoint_id_(0),
        current_block_(nullptr) {}

 protected:
  JSGraph* jsgraph() { return &jsgraph_; }

  LivenessAnalyzer* analyzer() { return &analyzer_; }
  void Run() {
    StateValuesCache cache(jsgraph());
42 43 44
    NonLiveFrameStateSlotReplacer replacer(
        &cache, jsgraph()->UndefinedConstant(), analyzer()->local_count(),
        false, zone());
45 46 47 48 49 50 51
    analyzer()->Run(&replacer);
  }

  Node* Checkpoint() {
    int ast_num = next_checkpoint_id_++;
    int first_const = intconst_from_bailout_id(ast_num, locals_count_);

52 53
    const Operator* locals_op =
        common()->StateValues(locals_count_, SparseInputMask::Dense());
54 55 56 57 58 59 60 61

    ZoneVector<Node*> local_inputs(locals_count_, nullptr, zone());
    for (int i = 0; i < locals_count_; i++) {
      local_inputs[i] = jsgraph()->Int32Constant(i + first_const);
    }
    Node* locals =
        graph()->NewNode(locals_op, locals_count_, &local_inputs.front());

62 63 64
    const FrameStateFunctionInfo* state_info =
        common()->CreateFrameStateFunctionInfo(
            FrameStateType::kJavaScriptFunction, 0, locals_count_,
65
            Handle<SharedFunctionInfo>());
66

67
    const Operator* op = common()->FrameState(
68
        BailoutId(ast_num), OutputFrameStateCombine::Ignore(), state_info);
69 70 71 72
    Node* result =
        graph()->NewNode(op, empty_values_, locals, empty_values_,
                         jsgraph()->UndefinedConstant(),
                         jsgraph()->UndefinedConstant(), graph()->start());
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

    current_block_->Checkpoint(result);
    return result;
  }

  void Bind(int var) { current_block()->Bind(var); }
  void Lookup(int var) { current_block()->Lookup(var); }

  class CheckpointMatcher : public MatcherInterface<Node*> {
   public:
    explicit CheckpointMatcher(const char* liveness, Node* empty_values,
                               int locals_count, Node* replacement)
        : liveness_(liveness),
          empty_values_(empty_values),
          locals_count_(locals_count),
          replacement_(replacement) {}

90
    void DescribeTo(std::ostream* os) const override {
91 92 93 94 95 96
      *os << "is a frame state with '" << liveness_
          << "' liveness, empty "
             "parameters and empty expression stack";
    }

    bool MatchAndExplain(Node* frame_state,
97
                         MatchResultListener* listener) const override {
98 99 100 101 102 103
      if (frame_state == NULL) {
        *listener << "which is NULL";
        return false;
      }
      DCHECK(frame_state->opcode() == IrOpcode::kFrameState);

104
      FrameStateInfo state_info = OpParameter<FrameStateInfo>(frame_state);
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
      int ast_num = state_info.bailout_id().ToInt();
      int first_const = intconst_from_bailout_id(ast_num, locals_count_);

      if (empty_values_ != frame_state->InputAt(0)) {
        *listener << "whose parameters are " << frame_state->InputAt(0)
                  << " but should have been " << empty_values_ << " (empty)";
        return false;
      }
      if (empty_values_ != frame_state->InputAt(2)) {
        *listener << "whose expression stack is " << frame_state->InputAt(2)
                  << " but should have been " << empty_values_ << " (empty)";
        return false;
      }
      StateValuesAccess locals(frame_state->InputAt(1));
      if (locals_count_ != static_cast<int>(locals.size())) {
        *listener << "whose number of locals is " << locals.size()
                  << " but should have been " << locals_count_;
        return false;
      }
      int i = 0;
125
      for (StateValuesAccess::TypedNode value : locals) {
126 127
        if (liveness_[i] == 'L') {
          StringMatchResultListener value_listener;
128 129
          if (value.node == replacement_) {
            *listener << "whose local #" << i << " was " << value.node->opcode()
130 131 132
                      << " but should have been 'undefined'";
            return false;
          } else if (!IsInt32Constant(first_const + i)
133
                          .MatchAndExplain(value.node, &value_listener)) {
134 135 136 137 138 139 140
            *listener << "whose local #" << i << " does not match";
            if (value_listener.str() != "") {
              *listener << ", " << value_listener.str();
            }
            return false;
          }
        } else if (liveness_[i] == '.') {
141 142
          if (value.node != replacement_) {
            *listener << "whose local #" << i << " is " << value.node
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
                      << " but should have been " << replacement_
                      << " (undefined)";
            return false;
          }
        } else {
          UNREACHABLE();
        }
        i++;
      }
      return true;
    }

   private:
    const char* liveness_;
    Node* empty_values_;
    int locals_count_;
    Node* replacement_;
  };

  Matcher<Node*> IsCheckpointModuloLiveness(const char* liveness) {
    return MakeMatcher(new CheckpointMatcher(liveness, empty_values_,
                                             locals_count_,
                                             jsgraph()->UndefinedConstant()));
  }

  LivenessAnalyzerBlock* current_block() { return current_block_; }
  void set_current_block(LivenessAnalyzerBlock* block) {
    current_block_ = block;
  }

 private:
  static int intconst_from_bailout_id(int ast_num, int locals_count) {
    return (locals_count + 1) * ast_num + 1;
  }

  int locals_count_;
  MachineOperatorBuilder machine_;
  JSOperatorBuilder javascript_;
  JSGraph jsgraph_;
  LivenessAnalyzer analyzer_;
  Node* empty_values_;
  int next_checkpoint_id_;
  LivenessAnalyzerBlock* current_block_;
};


TEST_F(LivenessAnalysisTest, EmptyBlock) {
  set_current_block(analyzer()->NewBlock());

  Node* c1 = Checkpoint();

  Run();

  // Nothing is live.
  EXPECT_THAT(c1, IsCheckpointModuloLiveness("...."));
}


TEST_F(LivenessAnalysisTest, SimpleLookup) {
  set_current_block(analyzer()->NewBlock());

  Node* c1 = Checkpoint();
  Lookup(1);
  Node* c2 = Checkpoint();

  Run();

  EXPECT_THAT(c1, IsCheckpointModuloLiveness(".L.."));
  EXPECT_THAT(c2, IsCheckpointModuloLiveness("...."));
}


TEST_F(LivenessAnalysisTest, DiamondLookups) {
  // Start block.
  LivenessAnalyzerBlock* start = analyzer()->NewBlock();
  set_current_block(start);
  Node* c1_start = Checkpoint();

  // First branch.
  LivenessAnalyzerBlock* b1 = analyzer()->NewBlock(start);
  set_current_block(b1);

  Node* c1_b1 = Checkpoint();
  Lookup(1);
  Node* c2_b1 = Checkpoint();
  Lookup(3);
  Node* c3_b1 = Checkpoint();

  // Second branch.
  LivenessAnalyzerBlock* b2 = analyzer()->NewBlock(start);
  set_current_block(b2);

  Node* c1_b2 = Checkpoint();
  Lookup(3);
  Node* c2_b2 = Checkpoint();
  Lookup(2);
  Node* c3_b2 = Checkpoint();

  // Merge block.
  LivenessAnalyzerBlock* m = analyzer()->NewBlock(b1);
  m->AddPredecessor(b2);
  set_current_block(m);
  Node* c1_m = Checkpoint();
  Lookup(0);
  Node* c2_m = Checkpoint();

  Run();

  EXPECT_THAT(c1_start, IsCheckpointModuloLiveness("LLLL"));

  EXPECT_THAT(c1_b1, IsCheckpointModuloLiveness("LL.L"));
  EXPECT_THAT(c2_b1, IsCheckpointModuloLiveness("L..L"));
  EXPECT_THAT(c3_b1, IsCheckpointModuloLiveness("L..."));

  EXPECT_THAT(c1_b2, IsCheckpointModuloLiveness("L.LL"));
  EXPECT_THAT(c2_b2, IsCheckpointModuloLiveness("L.L."));
  EXPECT_THAT(c3_b2, IsCheckpointModuloLiveness("L..."));

  EXPECT_THAT(c1_m, IsCheckpointModuloLiveness("L..."));
  EXPECT_THAT(c2_m, IsCheckpointModuloLiveness("...."));
}


TEST_F(LivenessAnalysisTest, DiamondLookupsAndBinds) {
  // Start block.
  LivenessAnalyzerBlock* start = analyzer()->NewBlock();
  set_current_block(start);
  Node* c1_start = Checkpoint();
  Bind(0);
  Node* c2_start = Checkpoint();

  // First branch.
  LivenessAnalyzerBlock* b1 = analyzer()->NewBlock(start);
  set_current_block(b1);

  Node* c1_b1 = Checkpoint();
  Bind(2);
  Bind(1);
  Node* c2_b1 = Checkpoint();
  Bind(3);
  Node* c3_b1 = Checkpoint();

  // Second branch.
  LivenessAnalyzerBlock* b2 = analyzer()->NewBlock(start);
  set_current_block(b2);

  Node* c1_b2 = Checkpoint();
  Lookup(2);
  Node* c2_b2 = Checkpoint();
  Bind(2);
  Bind(3);
  Node* c3_b2 = Checkpoint();

  // Merge block.
  LivenessAnalyzerBlock* m = analyzer()->NewBlock(b1);
  m->AddPredecessor(b2);
  set_current_block(m);
  Node* c1_m = Checkpoint();
  Lookup(0);
  Lookup(1);
  Lookup(2);
  Lookup(3);
  Node* c2_m = Checkpoint();

  Run();

  EXPECT_THAT(c1_start, IsCheckpointModuloLiveness(".LL."));
  EXPECT_THAT(c2_start, IsCheckpointModuloLiveness("LLL."));

  EXPECT_THAT(c1_b1, IsCheckpointModuloLiveness("L..."));
  EXPECT_THAT(c2_b1, IsCheckpointModuloLiveness("LLL."));
  EXPECT_THAT(c3_b1, IsCheckpointModuloLiveness("LLLL"));

  EXPECT_THAT(c1_b2, IsCheckpointModuloLiveness("LLL."));
  EXPECT_THAT(c2_b2, IsCheckpointModuloLiveness("LL.."));
  EXPECT_THAT(c3_b2, IsCheckpointModuloLiveness("LLLL"));

  EXPECT_THAT(c1_m, IsCheckpointModuloLiveness("LLLL"));
  EXPECT_THAT(c2_m, IsCheckpointModuloLiveness("...."));
}


TEST_F(LivenessAnalysisTest, SimpleLoop) {
  // Start block.
  LivenessAnalyzerBlock* start = analyzer()->NewBlock();
  set_current_block(start);
  Node* c1_start = Checkpoint();
  Bind(0);
  Bind(1);
  Bind(2);
  Bind(3);
  Node* c2_start = Checkpoint();

  // Loop header block.
  LivenessAnalyzerBlock* header = analyzer()->NewBlock(start);
  set_current_block(header);
  Node* c1_header = Checkpoint();
  Lookup(0);
  Bind(2);
  Node* c2_header = Checkpoint();

  // Inside-loop block.
  LivenessAnalyzerBlock* in_loop = analyzer()->NewBlock(header);
  set_current_block(in_loop);
  Node* c1_in_loop = Checkpoint();
  Bind(0);
  Lookup(3);
  Node* c2_in_loop = Checkpoint();

  // Add back edge.
  header->AddPredecessor(in_loop);

  // After-loop block.
  LivenessAnalyzerBlock* end = analyzer()->NewBlock(header);
  set_current_block(end);
  Node* c1_end = Checkpoint();
  Lookup(1);
  Lookup(2);
  Node* c2_end = Checkpoint();

  Run();

  EXPECT_THAT(c1_start, IsCheckpointModuloLiveness("...."));
  EXPECT_THAT(c2_start, IsCheckpointModuloLiveness("LL.L"));

  EXPECT_THAT(c1_header, IsCheckpointModuloLiveness("LL.L"));
  EXPECT_THAT(c2_header, IsCheckpointModuloLiveness(".LLL"));

  EXPECT_THAT(c1_in_loop, IsCheckpointModuloLiveness(".L.L"));
  EXPECT_THAT(c2_in_loop, IsCheckpointModuloLiveness("LL.L"));

  EXPECT_THAT(c1_end, IsCheckpointModuloLiveness(".LL."));
  EXPECT_THAT(c2_end, IsCheckpointModuloLiveness("...."));
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8