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

ShOptimizations.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #ifndef SHOPTIMIZATIONS_HPP
00025 #define SHOPTIMIZATIONS_HPP
00026 
00027 #include <iosfwd>
00028 #include <vector>
00029 #include <set>
00030 #include <map>
00031 #include "ShProgram.hpp"
00032 #include "ShInfo.hpp"
00033 #include "ShStatement.hpp"
00034 
00035 // Uncomment this to turn on optimizer debugging using dot.
00036 // Warning: This is very verbose!
00037 //#define SH_DEBUG_OPTIMIZER
00038 
00039 namespace SH {
00040 
00042 SH_DLLEXPORT
00043 void optimize(ShProgram& p, int level);
00044 SH_DLLEXPORT
00045 void optimize(const ShProgramNodePtr& p, int level);
00046 
00049 SH_DLLEXPORT
00050 void optimize(ShProgram& p);
00051 SH_DLLEXPORT
00052 void optimize(const ShProgramNodePtr& p);
00053 
00054 // Internal stuff.
00055 
00056 // Add value tracking information to the given program's CFG
00057 // statements.
00058 // If it already exists, overwrite it.
00059 SH_DLLEXPORT
00060 void add_value_tracking(ShProgram& prg);
00061 
00063 SH_DLLEXPORT
00064 void insert_branch_instructions(ShProgram& prg);
00065 
00067 SH_DLLEXPORT
00068 void remove_branch_instructions(ShProgram& prg);
00069 
00071 SH_DLLEXPORT
00072 void straighten(ShProgram& p, bool& changed);
00073 
00075 SH_DLLEXPORT
00076 void remove_dead_code(ShProgram& p, bool& changed);
00077 
00079 SH_DLLEXPORT
00080 void propagate_constants(ShProgram& p);
00081 
00082 /* per statement uddu chains */
00083 struct 
00084 SH_DLLEXPORT
00085 ValueTracking : public ShInfo {
00086   ValueTracking(ShStatement* stmt);
00087 
00088   ShInfo* clone() const;
00089 
00090   
00091   struct Def {
00092     enum Kind {
00093       INPUT,
00094       STMT
00095     };
00096 
00097     Def(ShStatement* stmt, int index)
00098       : kind(STMT), node(0), stmt(stmt), index(index)
00099     {
00100     }
00101 
00102     Def(ShVariableNodePtr node, int index)
00103       : kind(INPUT), node(node), stmt(0), index(index) 
00104     {}
00105     
00106     Kind kind;
00107     ShVariableNodePtr node;
00108     ShStatement* stmt;
00109     int index; 
00110 
00111     bool operator<(const Def& other) const
00112     {
00113       return kind < other.kind ||
00114              (kind == other.kind && 
00115              (node < other.node ||
00116              (node == other.node &&
00117              (stmt < other.stmt || 
00118              (stmt == other.stmt && index < other.index)))));
00119     }
00120 
00121     int absIndex() const
00122     {
00123       if(kind == INPUT) return index;
00124       return stmt->dest.swizzle()[index];
00125     }
00126 
00127     friend std::ostream& operator<<(std::ostream& out, const Def& def);
00128   };
00129 
00130   struct Use {
00131     enum Kind {
00132       OUTPUT,
00133       STMT
00134     };
00135 
00136     Use(ShStatement* stmt, int source, int index)
00137       : kind(STMT), node(0), stmt(stmt), source(source), index(index)
00138     {
00139     }
00140 
00141     Use(ShVariableNodePtr node, int index)
00142       : kind(OUTPUT), node(node), stmt(0), source(0), index(index)
00143     {
00144     }
00145 
00146     bool operator<(const Use& other) const
00147     {
00148       return kind < other.kind ||
00149              (kind == other.kind && 
00150              (node < other.node ||
00151              (node == other.node &&
00152              (stmt < other.stmt || 
00153              (stmt == other.stmt && 
00154              (source < other.source ||
00155              (source == other.source && index < other.index)))))));
00156     }
00157 
00158     int absIndex() const
00159     {
00160       if(kind == OUTPUT) return index;
00161       return stmt->src[source].swizzle()[index];
00162     }
00163 
00164     friend std::ostream& operator<<(std::ostream& out, const Use& use);
00165 
00166     Kind kind;
00167     ShVariableNodePtr node;
00168     ShStatement* stmt;
00169     int source; // source variable
00170     int index; // tuple index (swizzled)
00171   };
00172 
00173   // For each tuple element, track all of the uses of our definition.
00174   typedef std::set<Use> DefUseChain;
00175   typedef std::vector<DefUseChain> TupleDefUseChain;
00176   TupleDefUseChain uses;
00177 
00178   friend std::ostream& operator<<(std::ostream& out, const TupleDefUseChain& tdu);
00179   
00180   // For each tuple element in each of our sources, track all the
00181   // definition points.
00182   typedef std::set<Def> UseDefChain;
00183   typedef std::vector<UseDefChain> TupleUseDefChain;
00184   typedef std::vector<TupleUseDefChain> TupleUseDefChainVec;
00185   TupleUseDefChainVec defs;
00186 
00187   friend std::ostream& operator<<(std::ostream& out, const TupleUseDefChain& tud);
00188 };
00189 
00190 /* du chains for all inputs in a program */
00191 struct InputValueTracking: public ShInfo 
00192 {
00193   ShInfo* clone() const;
00194   
00195   friend std::ostream& operator<<(std::ostream& out, const InputValueTracking& ivt);
00196 
00197   typedef std::map<ShVariableNodePtr, ValueTracking::TupleDefUseChain> InputTupleDefUseChain; 
00198   InputTupleDefUseChain inputUses;
00199 };
00200 
00201 /* ud chains for all outputs in a program */
00202 struct OutputValueTracking: public ShInfo 
00203 {
00204   ShInfo* clone() const;
00205 
00206   friend std::ostream& operator<<(std::ostream& out, const OutputValueTracking& ovt);
00207 
00208   typedef std::map<ShVariableNodePtr, ValueTracking::TupleUseDefChain> OutputTupleUseDefChain; 
00209   OutputTupleUseDefChain outputDefs;
00210 };
00211 
00212 }
00213 
00214 #endif

Generated on Thu Apr 21 17:32:48 2005 for Sh by  doxygen 1.4.2