// Copyright (c) 2018, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// VMOptions=--deterministic --optimization-counter-threshold=102 --enable-inlining-annotations --optimization-filter=Box_

import 'package:expect/expect.dart';

const String NeverInline = 'NeverInline';
const String AlwaysInline = 'AlwaysInline';
const int kLimit = 100;

main() {
  // Get all 3 functions optimized with _Smi's.
  optimizeMilliseconds(1);
  optimizeMicroseconds(1);
  optimizeConstructor(1);

  // Now we trigger a store of a _Mint into the Box._value
  testMints(2048);
}

@NeverInline
optimizeConstructor(int value) {
  for (int i = 0; i < kLimit; ++i) {
    new Box(milliseconds: value);
  }
}

@NeverInline
optimizeMicroseconds(int value) {
  final d = new Box(milliseconds: value);
  for (int i = 0; i < kLimit; ++i) {
    Expect.equals(value * 1000, d.inMicroseconds);
  }
}

@NeverInline
optimizeMilliseconds(int value) {
  final d = new Box(seconds: value);
  for (int i = 0; i < kLimit; ++i) {
    Expect.equals(value * 1000, d.inMilliseconds);
  }
}

@NeverInline
testMints(int value) {
  final d = new Box(seconds: value);
  for (int i = 0; i < kLimit; ++i) {
    Expect.equals(value * 1000 * 1000, d.inMicroseconds);
    Expect.equals(value * 1000, d.inMilliseconds);
  }
}

int c = 0;

class Box {
  final int _value;

  @NeverInline
  Box(
      {int days: 0,
      int hours: 0,
      int minutes: 0,
      int seconds: 0,
      int milliseconds: 0,
      int microseconds: 0})
      : this._microseconds(1000 * 1000 * 60 * 60 * 24 * days +
            1000 * 1000 * 60 * 60 * hours +
            1000 * 1000 * 60 * minutes +
            1000 * 1000 * seconds +
            1000 * milliseconds +
            microseconds);

  Box._microseconds(this._value);

  @NeverInline
  int get inMilliseconds => _value ~/ 1000;

  @NeverInline
  int get inMicroseconds => _value;
}

Add regression test for buildbot flakiness due to smi-widening optimization in VM
[VM] Add regression test for buildbot flakiness due to smi-widening optimization in VM

The added test in this CL causes code to be optimized with _Smi's and
eliminated field guards.  Field guards were eliminated due to usage of Smi ops,
which get removed due to the WidenSmiToInt32 optimization.
A use of the code with _Mint's will then trigger a test failure.

The WidenSmiToInt32 optimization has been disabled for the time being.

Change-Id: I6f7dd003a875b0abd708b3b948cda44378c42198
Reviewed-on: https://dart-review.googlesource.com/c/79540
Reviewed-by: Vyacheslav Egorov <72c81f38df725d8f1832acb9b5bb99563fc2f1b9@google.com>
Commit-Queue: Martin Kustermann <a35dd5b79fac84b2bb3b026a16b8becc55af5961@google.com>
