annotate graal/com.oracle.truffle.api/src/com/oracle/truffle/api/utilities/ConditionProfile.java @ 16860:fa5e62620593

Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
author Christian Humer <christian.humer@gmail.com>
date Tue, 19 Aug 2014 14:56:19 +0200
parents fc6f12ee71e5
children db090a8d3705
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
16828
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
1 /*
16854
fc6f12ee71e5 Truffle: fixed header dates.
Christian Humer <christian.humer@gmail.com>
parents: 16852
diff changeset
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
16828
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
4 *
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
7 * published by the Free Software Foundation. Oracle designates this
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
8 * particular file as subject to the "Classpath" exception as provided
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
9 * by Oracle in the LICENSE file that accompanied this code.
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
10 *
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
11 * This code is distributed in the hope that it will be useful, but WITHOUT
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
14 * version 2 for more details (a copy is included in the LICENSE file that
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
15 * accompanied this code).
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
16 *
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License version
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
18 * 2 along with this work; if not, write to the Free Software Foundation,
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
20 *
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
22 * or visit www.oracle.com if you need additional information or have any
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
23 * questions.
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
24 */
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
25 package com.oracle.truffle.api.utilities;
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
26
16860
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
27 import com.oracle.truffle.api.*;
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
28
16828
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
29 /**
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
30 * Abstract utility class to speculate on conditions. Condition profiles are intended to be used as
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
31 * part of if conditions.
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
32 *
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
33 * Example usage:
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
34 *
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
35 * <pre>
16860
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
36 * private final ConditionProfile zero = ConditionProfile.createBinaryProfile();
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
37 *
16828
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
38 * int value = ...;
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
39 * if (zero.profile(value == 0)) {
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
40 * return 0;
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
41 * } else {
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
42 * return value;
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
43 * }
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
44 *
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
45 * </pre>
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
46 *
16860
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
47 * @see #createCountingProfile()
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
48 * @see #createBinaryProfile()
16828
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
49 */
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
50 public abstract class ConditionProfile {
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
51
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
52 public abstract boolean profile(boolean value);
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
53
16860
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
54 /**
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
55 * Returns a {@link ConditionProfile} that speculates on conditions to be never
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
56 * <code>true</code> or to be never <code>false</code>. Additionally to a binary profile this
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
57 * method returns a condition profile that also counts the number of times the condition was
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
58 * true and false. This information is reported to the underlying optimization system using
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
59 * {@link CompilerDirectives#injectBranchProbability(double, boolean)}. Condition profiles are
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
60 * intended to be used as part of if conditions.
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
61 *
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
62 * @see ConditionProfile
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
63 * @see #createBinaryProfile()
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
64 */
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
65 public static CountingConditionProfile createCountingProfile() {
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
66 return new CountingConditionProfile();
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
67 }
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
68
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
69 /**
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
70 * REturns a {@link ConditionProfile} that speculates on conditions to be never true or to be
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
71 * never false. Condition profiles are intended to be used as part of if conditions.
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
72 *
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
73 * @see ConditionProfile
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
74 * @see ConditionProfile#createBinaryProfile()
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
75 */
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
76 public static BinaryConditionProfile createBinaryProfile() {
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
77 return new BinaryConditionProfile();
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
78 }
fa5e62620593 Truffle: made constructors of condition profiles package protected to delegate them later to TruffleRuntime.
Christian Humer <christian.humer@gmail.com>
parents: 16854
diff changeset
79
16828
2834af86f398 Truffle: new condition profile primitives.
Christian Humer <christian.humer@gmail.com>
parents:
diff changeset
80 }