initial import
[vuplus_webkit] / Source / JavaScriptCore / bytecode / ValueProfile.cpp
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "ValueProfile.h"
31
32 namespace JSC {
33
34 #if ENABLE(VALUE_PROFILER)
35 void ValueProfile::computeStatistics(const ClassInfo* classInfo, Statistics& statistics)
36 {
37     statistics.cells++;
38     
39     if (classInfo == &JSFinalObject::s_info) {
40         statistics.finalObjects++;
41         statistics.objects++;
42         return;
43     }
44     
45     if (classInfo == &JSArray::s_info) {
46         statistics.arrays++;
47         statistics.objects++;
48         return;
49     }
50     
51     if (classInfo == &JSString::s_info) {
52         statistics.strings++;
53         return;
54     }
55     
56     if (classInfo->isSubClassOf(&JSObject::s_info))
57         statistics.objects++;
58 }
59
60 void ValueProfile::computeStatistics(Statistics& statistics) const
61 {
62     for (unsigned i = 0; i < numberOfBuckets; ++i) {
63         if (!m_buckets[i]) {
64             WeakBucket weakBucket = m_weakBuckets[i];
65             if (!!weakBucket) {
66                 statistics.samples++;
67                 computeStatistics(weakBucket.getClassInfo(), statistics);
68             }
69             
70             continue;
71         }
72         
73         statistics.samples++;
74         
75         JSValue value = JSValue::decode(m_buckets[i]);
76         if (value.isInt32())
77             statistics.int32s++;
78         else if (value.isDouble())
79             statistics.doubles++;
80         else if (value.isCell())
81             computeStatistics(value.asCell()->structure()->classInfo(), statistics);
82         else if (value.isBoolean())
83             statistics.booleans++;
84     }
85 }
86
87 PredictedType ValueProfile::computeUpdatedPrediction()
88 {
89     ValueProfile::Statistics statistics;
90     computeStatistics(statistics);
91     
92     PredictedType prediction;
93     
94     if (!statistics.samples)
95         prediction = PredictNone;
96     else if (statistics.int32s == statistics.samples)
97         prediction = StrongPredictionTag | PredictInt32;
98     else if (statistics.doubles == statistics.samples)
99         prediction = StrongPredictionTag | PredictDouble;
100     else if (statistics.int32s + statistics.doubles == statistics.samples)
101         prediction = StrongPredictionTag | PredictNumber;
102     else if (statistics.arrays == statistics.samples)
103         prediction = StrongPredictionTag | PredictArray;
104     else if (statistics.finalObjects == statistics.samples)
105         prediction = StrongPredictionTag | PredictFinalObject;
106     else if (statistics.strings == statistics.samples)
107         prediction = StrongPredictionTag | PredictString;
108     else if (statistics.objects == statistics.samples)
109         prediction = StrongPredictionTag | PredictObjectOther;
110     else if (statistics.cells == statistics.samples)
111         prediction = StrongPredictionTag | PredictCellOther;
112     else if (statistics.booleans == statistics.samples)
113         prediction = StrongPredictionTag | PredictBoolean;
114     else
115         prediction = StrongPredictionTag | PredictOther;
116
117     m_numberOfSamplesInPrediction += statistics.samples;
118     mergePrediction(m_prediction, prediction);
119     for (unsigned i = 0; i < numberOfBuckets; ++i) {
120         m_buckets[i] = JSValue::encode(JSValue());
121         m_weakBuckets[i] = WeakBucket();
122     }
123     return m_prediction;
124 }
125 #endif // ENABLE(VALUE_PROFILER)
126
127 } // namespace JSC