00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00027
#include "ShLinearAllocator.hpp"
00028
#include <set>
00029
#include <algorithm>
00030
#include "ShDebug.hpp"
00031
00032
namespace {
00033
00034
struct LifeToken {
00035 LifeToken(
const SH::ShVariableNodePtr& var,
int index,
bool end)
00036 : var(var), index(index), end(end)
00037 {
00038 }
00039
00040
SH::ShVariableNodePtr var;
00041
int index;
00042
bool end;
00043
00044
00045
bool operator<(
const LifeToken& other)
const
00046
{
00047
if (index == other.index) {
00048
return !end;
00049 }
00050
return index < other.index;
00051 }
00052 };
00053
00054 }
00055
00056
namespace SH {
00057
00058 ShLinearAllocator::ShLinearAllocator(ShBackendCodePtr backendCode)
00059 : m_backendCode(backendCode)
00060 {
00061 }
00062
00063
void ShLinearAllocator::mark(
const ShVariableNodePtr& var,
int index)
00064 {
00065
if (!var)
return;
00066 LifetimeMap::iterator I = m_lifetimes.find(var);
00067
00068
if (I == m_lifetimes.end()) {
00069 m_lifetimes[var] = ShLifeTime(var, index);
00070 }
else {
00071 I->second.mark(index);
00072 }
00073 }
00074
00075
void ShLinearAllocator::debugDump()
00076 {
00077
#ifdef SH_DEBUG
00078
for (LifetimeMap::const_iterator I = m_lifetimes.begin(); I != m_lifetimes.end(); ++I) {
00079 SH_DEBUG_PRINT(I->first->name() <<
" = {" << I->second.first <<
", " << I->second.last <<
"}");
00080 }
00081
#endif
00082
}
00083
00084
void ShLinearAllocator::allocate()
00085 {
00086 std::multiset<LifeToken> temps;
00087
00088
for (LifetimeMap::const_iterator I = m_lifetimes.begin(); I != m_lifetimes.end(); ++I) {
00089 temps.insert(LifeToken(I->first, I->second.first,
false));
00090 temps.insert(LifeToken(I->first, I->second.last,
true));
00091 }
00092
00093
for (std::multiset<LifeToken>::const_iterator I = temps.begin(); I != temps.end(); ++I) {
00094
if (!I->end) {
00095
if (!m_backendCode->allocateRegister(I->var)) {
00096
00097 SH_DEBUG_WARN(
"Error allocating a register for " << I->var->name());
00098 }
00099 }
else {
00100 m_backendCode->freeRegister(I->var);
00101 }
00102 }
00103 }
00104
00105 }
00106