Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShLinearAllocator.cpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright (c) 2003 University of Waterloo Computer Graphics Laboratory
00004 // Project administrator: Michael D. McCool
00005 // Authors: Zheng Qin, Stefanus Du Toit, Kevin Moule, Tiberiu S. Popa,
00006 //          Michael D. McCool
00007 // 
00008 // This software is provided 'as-is', without any express or implied
00009 // warranty. In no event will the authors be held liable for any damages
00010 // arising from the use of this software.
00011 // 
00012 // Permission is granted to anyone to use this software for any purpose,
00013 // including commercial applications, and to alter it and redistribute it
00014 // freely, subject to the following restrictions:
00015 // 
00016 // 1. The origin of this software must not be misrepresented; you must
00017 // not claim that you wrote the original software. If you use this
00018 // software in a product, an acknowledgment in the product documentation
00019 // would be appreciated but is not required.
00020 // 
00021 // 2. Altered source versions must be plainly marked as such, and must
00022 // not be misrepresented as being the original software.
00023 // 
00024 // 3. This notice may not be removed or altered from any source
00025 // distribution.
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   // order by index, then end
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         // TODO: Error
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 

Generated on Mon Jan 24 18:36:33 2005 for Sh by  doxygen 1.4.1