usage-analyzer.cc 9.99 KB
Newer Older
1
// Copyright 2006-2008 the V8 project authors. All rights reserved.
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
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "v8.h"

#include "ast.h"
#include "scopes.h"
#include "usage-analyzer.h"

34 35
namespace v8 {
namespace internal {
36 37 38 39 40 41 42

// Weight boundaries
static const int MinWeight = 1;
static const int MaxWeight = 1000000;
static const int InitialWeight = 100;


43
class UsageComputer: public AstVisitor {
44
 public:
45
  static bool Traverse(AstNode* node);
46

47 48 49 50 51 52
  // AST node visit functions.
#define DECLARE_VISIT(type) void Visit##type(type* node);
  AST_NODE_LIST(DECLARE_VISIT)
#undef DECLARE_VISIT

  void VisitVariable(Variable* var);
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

 private:
  int weight_;
  bool is_write_;

  UsageComputer(int weight, bool is_write);
  virtual ~UsageComputer();

  // Helper functions
  void RecordUses(UseCount* uses);
  void Read(Expression* x);
  void Write(Expression* x);
  void ReadList(ZoneList<Expression*>* list);
  void ReadList(ZoneList<ObjectLiteral::Property*>* list);

  friend class WeightScaler;
};


class WeightScaler BASE_EMBEDDED {
 public:
  WeightScaler(UsageComputer* uc, float scale);
  ~WeightScaler();

 private:
  UsageComputer* uc_;
  int old_weight_;
};


// ----------------------------------------------------------------------------
// Implementation of UsageComputer

86
bool UsageComputer::Traverse(AstNode* node) {
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 152 153 154 155 156 157 158 159 160 161
  UsageComputer uc(InitialWeight, false);
  uc.Visit(node);
  return !uc.HasStackOverflow();
}


void UsageComputer::VisitBlock(Block* node) {
  VisitStatements(node->statements());
}


void UsageComputer::VisitDeclaration(Declaration* node) {
  Write(node->proxy());
  if (node->fun() != NULL)
    VisitFunctionLiteral(node->fun());
}


void UsageComputer::VisitExpressionStatement(ExpressionStatement* node) {
  Visit(node->expression());
}


void UsageComputer::VisitEmptyStatement(EmptyStatement* node) {
  // nothing to do
}


void UsageComputer::VisitIfStatement(IfStatement* node) {
  Read(node->condition());
  { WeightScaler ws(this, 0.5);  // executed 50% of the time
    Visit(node->then_statement());
    Visit(node->else_statement());
  }
}


void UsageComputer::VisitContinueStatement(ContinueStatement* node) {
  // nothing to do
}


void UsageComputer::VisitBreakStatement(BreakStatement* node) {
  // nothing to do
}


void UsageComputer::VisitReturnStatement(ReturnStatement* node) {
  Read(node->expression());
}


void UsageComputer::VisitWithEnterStatement(WithEnterStatement* node) {
  Read(node->expression());
}


void UsageComputer::VisitWithExitStatement(WithExitStatement* node) {
  // nothing to do
}


void UsageComputer::VisitSwitchStatement(SwitchStatement* node) {
  Read(node->tag());
  ZoneList<CaseClause*>* cases = node->cases();
  for (int i = cases->length(); i-- > 0;) {
    WeightScaler ws(this, static_cast<float>(1.0 / cases->length()));
    CaseClause* clause = cases->at(i);
    if (!clause->is_default())
      Read(clause->label());
    VisitStatements(clause->statements());
  }
}


162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
void UsageComputer::VisitDoWhileStatement(DoWhileStatement* node) {
  WeightScaler ws(this, 10.0);
  Read(node->cond());
  Visit(node->body());
}


void UsageComputer::VisitWhileStatement(WhileStatement* node) {
  WeightScaler ws(this, 10.0);
  Read(node->cond());
  Visit(node->body());
}


void UsageComputer::VisitForStatement(ForStatement* node) {
  if (node->init() != NULL) Visit(node->init());
178
  { WeightScaler ws(this, 10.0);  // executed in each iteration
179 180
    if (node->cond() != NULL) Read(node->cond());
    if (node->next() != NULL) Visit(node->next());
181 182 183 184 185 186 187 188 189 190 191 192 193
    Visit(node->body());
  }
}


void UsageComputer::VisitForInStatement(ForInStatement* node) {
  WeightScaler ws(this, 10.0);
  Write(node->each());
  Read(node->enumerable());
  Visit(node->body());
}


194
void UsageComputer::VisitTryCatchStatement(TryCatchStatement* node) {
195 196 197 198 199 200 201 202
  Visit(node->try_block());
  { WeightScaler ws(this, 0.25);
    Write(node->catch_var());
    Visit(node->catch_block());
  }
}


203
void UsageComputer::VisitTryFinallyStatement(TryFinallyStatement* node) {
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
  Visit(node->try_block());
  Visit(node->finally_block());
}


void UsageComputer::VisitDebuggerStatement(DebuggerStatement* node) {
}


void UsageComputer::VisitFunctionLiteral(FunctionLiteral* node) {
  ZoneList<Declaration*>* decls = node->scope()->declarations();
  for (int i = 0; i < decls->length(); i++) VisitDeclaration(decls->at(i));
  VisitStatements(node->body());
}


void UsageComputer::VisitFunctionBoilerplateLiteral(
    FunctionBoilerplateLiteral* node) {
  // Do nothing.
}


void UsageComputer::VisitConditional(Conditional* node) {
  Read(node->condition());
  { WeightScaler ws(this, 0.5);
    Read(node->then_expression());
    Read(node->else_expression());
  }
}


void UsageComputer::VisitSlot(Slot* node) {
  UNREACHABLE();
}


void UsageComputer::VisitVariable(Variable* node) {
  RecordUses(node->var_uses());
}


void UsageComputer::VisitVariableProxy(VariableProxy* node) {
  // The proxy may refer to a variable in which case it was bound via
  // VariableProxy::BindTo.
  RecordUses(node->var_uses());
}


void UsageComputer::VisitLiteral(Literal* node) {
  // nothing to do
}

void UsageComputer::VisitRegExpLiteral(RegExpLiteral* node) {
  // nothing to do
}


void UsageComputer::VisitObjectLiteral(ObjectLiteral* node) {
  ReadList(node->properties());
}


void UsageComputer::VisitArrayLiteral(ArrayLiteral* node) {
  ReadList(node->values());
}


271 272 273 274 275
void UsageComputer::VisitCatchExtensionObject(CatchExtensionObject* node) {
  Read(node->value());
}


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
void UsageComputer::VisitAssignment(Assignment* node) {
  if (node->op() != Token::ASSIGN)
    Read(node->target());
  Write(node->target());
  Read(node->value());
}


void UsageComputer::VisitThrow(Throw* node) {
  Read(node->exception());
}


void UsageComputer::VisitProperty(Property* node) {
  // In any case (read or write) we read both the
  // node's object and the key.
  Read(node->obj());
  Read(node->key());
  // If the node's object is a variable proxy,
  // we have a 'simple' object property access. We count
  // the access via the variable or proxy's object uses.
  VariableProxy* proxy = node->obj()->AsVariableProxy();
  if (proxy != NULL) {
    RecordUses(proxy->obj_uses());
  }
}


void UsageComputer::VisitCall(Call* node) {
  Read(node->expression());
  ReadList(node->arguments());
}


void UsageComputer::VisitCallNew(CallNew* node) {
311 312
  Read(node->expression());
  ReadList(node->arguments());
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 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
}


void UsageComputer::VisitCallRuntime(CallRuntime* node) {
  ReadList(node->arguments());
}


void UsageComputer::VisitUnaryOperation(UnaryOperation* node) {
  Read(node->expression());
}


void UsageComputer::VisitCountOperation(CountOperation* node) {
  Read(node->expression());
  Write(node->expression());
}


void UsageComputer::VisitBinaryOperation(BinaryOperation* node) {
  Read(node->left());
  Read(node->right());
}


void UsageComputer::VisitCompareOperation(CompareOperation* node) {
  Read(node->left());
  Read(node->right());
}


void UsageComputer::VisitThisFunction(ThisFunction* node) {
}


UsageComputer::UsageComputer(int weight, bool is_write) {
  weight_ = weight;
  is_write_ = is_write;
}


UsageComputer::~UsageComputer() {
  // nothing to do
}


void UsageComputer::RecordUses(UseCount* uses) {
  if (is_write_)
    uses->RecordWrite(weight_);
  else
    uses->RecordRead(weight_);
}


void UsageComputer::Read(Expression* x) {
  if (is_write_) {
    UsageComputer uc(weight_, false);
    uc.Visit(x);
  } else {
    Visit(x);
  }
}


void UsageComputer::Write(Expression* x) {
  if (!is_write_) {
    UsageComputer uc(weight_, true);
    uc.Visit(x);
  } else {
    Visit(x);
  }
}


void UsageComputer::ReadList(ZoneList<Expression*>* list) {
  for (int i = list->length(); i-- > 0; )
    Read(list->at(i));
}


void UsageComputer::ReadList(ZoneList<ObjectLiteral::Property*>* list) {
  for (int i = list->length(); i-- > 0; )
    Read(list->at(i)->value());
}


// ----------------------------------------------------------------------------
// Implementation of WeightScaler

WeightScaler::WeightScaler(UsageComputer* uc, float scale) {
  uc_ = uc;
  old_weight_ = uc->weight_;
  int new_weight = static_cast<int>(uc->weight_ * scale);
  if (new_weight <= 0) new_weight = MinWeight;
  else if (new_weight > MaxWeight) new_weight = MaxWeight;
  uc->weight_ = new_weight;
}


WeightScaler::~WeightScaler() {
  uc_->weight_ = old_weight_;
}


// ----------------------------------------------------------------------------
// Interface to variable usage analysis

bool AnalyzeVariableUsage(FunctionLiteral* lit) {
  if (!FLAG_usage_computation) return true;
422
  HistogramTimerScope timer(&Counters::usage_analysis);
423 424 425 426
  return UsageComputer::Traverse(lit);
}

} }  // namespace v8::internal