2 * Copyright (C) 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef PredictionTracker_h
27 #define PredictionTracker_h
29 #include "PredictedType.h"
30 #include <wtf/HashMap.h>
31 #include <wtf/Vector.h>
35 // helper function to distinguish vars & temporaries from arguments.
36 inline bool operandIsArgument(int operand) { return operand < 0; }
38 struct PredictionSlot {
41 : m_value(PredictNone)
44 PredictedType m_value;
47 class PredictionTracker {
53 PredictionTracker(unsigned numArguments, unsigned numVariables)
54 : m_arguments(numArguments)
55 , m_variables(numVariables)
59 void initializeSimilarTo(const PredictionTracker& other)
61 m_arguments.resize(other.numberOfArguments());
62 m_variables.resize(other.numberOfVariables());
65 void copyLocalsFrom(const PredictionTracker& other)
67 m_arguments = other.m_arguments;
68 m_variables = other.m_variables;
71 size_t numberOfArguments() const { return m_arguments.size(); }
72 size_t numberOfVariables() const { return m_variables.size(); }
74 unsigned argumentIndexForOperand(int operand) const { return operand + m_arguments.size() + RegisterFile::CallFrameHeaderSize; }
76 bool predictArgument(unsigned argument, PredictedType prediction, PredictionSource source)
78 return mergePrediction(m_arguments[argument].m_value, makePrediction(prediction, source));
81 bool predict(int operand, PredictedType prediction, PredictionSource source)
83 if (operandIsArgument(operand))
84 return predictArgument(argumentIndexForOperand(operand), prediction, source);
85 if ((unsigned)operand >= m_variables.size()) {
87 m_variables.resize(operand + 1);
90 return mergePrediction(m_variables[operand].m_value, makePrediction(prediction, source));
93 bool predictGlobalVar(unsigned varNumber, PredictedType prediction, PredictionSource source)
95 HashMap<unsigned, PredictionSlot>::iterator iter = m_globalVars.find(varNumber + 1);
96 if (iter == m_globalVars.end()) {
97 PredictionSlot predictionSlot;
98 bool result = mergePrediction(predictionSlot.m_value, makePrediction(prediction, source));
99 m_globalVars.add(varNumber + 1, predictionSlot);
102 return mergePrediction(iter->second.m_value, makePrediction(prediction, source));
105 PredictedType getArgumentPrediction(unsigned argument)
107 return m_arguments[argument].m_value;
110 PredictedType getPrediction(int operand)
112 if (operandIsArgument(operand))
113 return getArgumentPrediction(argumentIndexForOperand(operand));
114 if ((unsigned)operand < m_variables.size())
115 return m_variables[operand].m_value;
119 PredictedType getGlobalVarPrediction(unsigned varNumber)
121 HashMap<unsigned, PredictionSlot>::iterator iter = m_globalVars.find(varNumber + 1);
122 if (iter == m_globalVars.end())
124 return iter->second.m_value;
128 Vector<PredictionSlot, 16> m_arguments;
129 Vector<PredictionSlot, 16> m_variables;
130 HashMap<unsigned, PredictionSlot> m_globalVars;
135 #endif // PredictionTracker_h