summaryrefslogtreecommitdiffstats
path: root/js/src/jit/JitHints.cpp
blob: 6b3783ea8a03ff2ae6d59de6d41927dc40bc7f2b (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: set ts=8 sts=2 et sw=2 tw=80:
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "jit/JitHints-inl.h"
#include "vm/BytecodeLocation-inl.h"
#include "vm/JSScript-inl.h"

using namespace js;
using namespace js::jit;

JitHintsMap::~JitHintsMap() {
  while (!ionHintQueue_.isEmpty()) {
    IonHint* e = ionHintQueue_.popFirst();
    js_delete(e);
  }
  ionHintMap_.clear();
}

JitHintsMap::IonHint* JitHintsMap::addIonHint(ScriptKey key,
                                              ScriptToHintMap::AddPtr& p) {
  UniquePtr<IonHint> hint = MakeUnique<IonHint>(key);
  if (!hint) {
    return nullptr;
  }

  if (!ionHintMap_.add(p, key, hint.get())) {
    return nullptr;
  }

  ionHintQueue_.insertBack(hint.get());

  if (ionHintMap_.count() > IonHintMaxEntries) {
    IonHint* h = ionHintQueue_.popFirst();
    ionHintMap_.remove(h->key());
    js_delete(h);
  }

  return hint.release();
}

void JitHintsMap::updateAsRecentlyUsed(IonHint* hint) {
  hint->remove();
  ionHintQueue_.insertBack(hint);
}

bool JitHintsMap::recordIonCompilation(JSScript* script) {
  ScriptKey key = getScriptKey(script);
  if (!key) {
    return true;
  }

  // Only add hints for scripts that will be eager baseline compiled.
  if (!baselineHintMap_.mightContain(key)) {
    return true;
  }

  auto p = ionHintMap_.lookupForAdd(key);
  IonHint* hint = nullptr;
  if (p) {
    // Don't modify existing threshold values.
    hint = p->value();
    updateAsRecentlyUsed(hint);
  } else {
    hint = addIonHint(key, p);
    if (!hint) {
      return false;
    }
  }

  hint->initThreshold(script->warmUpCountAtLastICStub());
  return true;
}

bool JitHintsMap::getIonThresholdHint(JSScript* script,
                                      uint32_t& thresholdOut) {
  ScriptKey key = getScriptKey(script);
  if (key) {
    auto p = ionHintMap_.lookup(key);
    if (p) {
      IonHint* hint = p->value();
      // If the threshold is 0, the hint only contains
      // monomorphic inlining location information and
      // may not have entered Ion before.
      if (hint->threshold() != 0) {
        updateAsRecentlyUsed(hint);
        thresholdOut = hint->threshold();
        return true;
      }
    }
  }
  return false;
}

void JitHintsMap::recordInvalidation(JSScript* script) {
  ScriptKey key = getScriptKey(script);
  if (key) {
    auto p = ionHintMap_.lookup(key);
    if (p) {
      p->value()->incThreshold(InvalidationThresholdIncrement);
    }
  }
}

bool JitHintsMap::addMonomorphicInlineLocation(JSScript* script,
                                               BytecodeLocation loc) {
  ScriptKey key = getScriptKey(script);
  if (!key) {
    return true;
  }

  // Only add inline hints for scripts that will be eager baseline compiled.
  if (!baselineHintMap_.mightContain(key)) {
    return true;
  }

  auto p = ionHintMap_.lookupForAdd(key);
  IonHint* hint = nullptr;
  if (p) {
    hint = p->value();
  } else {
    hint = addIonHint(key, p);
    if (!hint) {
      return false;
    }
  }

  if (!hint->hasSpaceForMonomorphicInlineEntry()) {
    return true;
  }

  uint32_t offset = loc.bytecodeToOffset(script);
  return hint->addMonomorphicInlineOffset(offset);
}

bool JitHintsMap::hasMonomorphicInlineHintAtOffset(JSScript* script,
                                                   uint32_t offset) {
  ScriptKey key = getScriptKey(script);
  if (!key) {
    return false;
  }

  auto p = ionHintMap_.lookup(key);
  if (p) {
    return p->value()->hasMonomorphicInlineOffset(offset);
  }

  return false;
}