basicBlock.hpp

Go to the documentation of this file.
00001 /* Copyright 1994 - 1996 LongView Technologies L.L.C. $Revision: 1.39 $ */
00002 /* Copyright (c) 2006, Sun Microsystems, Inc.
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the 
00006 following conditions are met:
00007 
00008     * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following 
00010           disclaimer in the documentation and/or other materials provided with the distribution.
00011     * Neither the name of Sun Microsystems nor the names of its contributors may be used to endorse or promote products derived 
00012           from this software without specific prior written permission.
00013 
00014 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT 
00015 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
00016 THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
00017 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
00018 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
00019 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00020 
00021 
00022 */
00023 
00024 // BBs (Basic Blocks) are used by the Compiler to perform local optimizations 
00025 // and code generation
00026 
00027 # ifdef DELTA_COMPILER
00028 
00029 class NodeVisitor;
00030 
00031 class BB: public PrintableResourceObj {
00032  protected:
00033   bool _visited;
00034 
00035  public:                        // was protected: originally
00036   Node  *first, *last;
00037   int16 nnodes;                 // number of nodes in this BB
00038  protected:
00039   int16 _id;                    // unique BB id
00040   int16 _loopDepth;             // the loop nesting level
00041   int16 _genCount;              // code already generated?
00042 
00043  public:        
00044   BBDUTable  duInfo;            // defs/uses of PRegs
00045   static int genCounter;        // to enumerate BBs in code-generation order
00046 
00047  public:
00048   BB(Node* f, Node* l, int n) { init(f, l, n); _visited = false; }
00049 
00050   BB*  after_visit()                    { _visited = true;  return this; }
00051   BB*  before_visit()                   { _visited = false; return this; }
00052   bool visited() const                  { return _visited; }
00053 
00054   // successor/predecessor functionality
00055   bool hasSingleSuccessor() const;
00056   bool hasSinglePredecessor() const;
00057   int nPredecessors() const;
00058   int nSuccessors() const;
00059   bool isPredecessor(const BB* n) const;
00060   bool isSuccessor(const BB* n) const;
00061   BB* next() const;
00062   BB* next1() const;
00063   BB* next(int i) const;
00064   BB* firstPrev() const;
00065   BB* prev(int i) const;
00066    
00067   int  id() const               { return this == NULL ? -1 : _id; }
00068   int  loopDepth() const        { return _loopDepth; }
00069   void setGenCount()            { _genCount = genCounter++; }
00070   int  genCount() const         { return _genCount; }
00071 
00072   void localCopyPropagate();
00073   void bruteForceCopyPropagate();
00074 
00075   void makeUses();
00076   Use* addUse(NonTrivialNode* n, PReg* r, bool soft = false);
00077   Def* addDef(NonTrivialNode* n, PReg* r);
00078   void remove(Node* n);                         // remove node
00079   void addAfter(Node* prev, Node* newNode);     // add node after prev
00080   void localAlloc(GrowableArray<BitVector*>* hardwired, 
00081                   GrowableArray<PReg*>* localRegs,
00082                   GrowableArray<BitVector*>* lives);
00083  protected:
00084   void init(Node* f, Node* l, int n);
00085   void slowLocalAlloc(GrowableArray<BitVector*>* hardwired, 
00086                       GrowableArray<PReg*>* localRegs,
00087                       GrowableArray<BitVector*>* lives);
00088   void doAlloc(PReg* r, Location l);
00089  public:
00090   void computeEscapingBlocks(GrowableArray<BlockPReg*>* l);
00091 
00092   void apply(NodeVisitor* v);
00093   bool verifyLabels();
00094 
00095   bool contains(const Node* n) const;
00096   void verify();
00097   void dfs(GrowableArray<BB*>* list, int depth);
00098   void print_short();
00099   void print_code(bool suppressTrivial);
00100   void print();
00101 
00102  protected:
00103   int addUDHelper(PReg* r);
00104   void renumber();
00105   friend class BBIterator;
00106 };
00107 
00108 
00109 typedef void (*BBDoFn)(BB* b);
00110 
00111   
00112 class BBIterator: public PrintableResourceObj {
00113   Node* _first;                                         // first node 
00114  public:                                                // was protected: originally
00115   GrowableArray<BB*>* bbTable;                          // BBs sorted in topological order
00116   int bbCount;                                          // number of BBs
00117   GrowableArray<PReg*>* pregTable;                      // holds all PRegs; indexed by their id
00118   GrowableArray<PReg*>* globals;                        // holds globally allocated PRegs; indexed by
00119                                                         // their num()
00120   bool usesBuilt;                                       // true after uses have been built
00121   bool blocksBuilt;                                     // true after basic blocks have been built
00122   GrowableArray<BlockPReg*>* exposedBlks;               // list of escaping blocks
00123 
00124  public:
00125   BBIterator() {
00126     bbTable = NULL; bbCount = 0; usesBuilt = blocksBuilt = false;
00127   }
00128 
00129   void build(Node* first);                              // build bbTable
00130 
00131  protected:
00132   void buildBBs();
00133   void buildTable();
00134 
00135   static BB* bb_for(Node* n);
00136   void add_BBs_to_list(GrowableArray<BB*>& list, GrowableArray<BB*>& work);
00137 
00138  public:
00139   bool isSequential(int curr, int next) const;          // are the two BB indices sequential in bbTable order?
00140   bool isSequentialCode(BB* curr, BB* next) const;      // are the two BBs sequential in codeGen order?
00141   GrowableArray<BB*>* code_generation_order();          // list of BBs in code generation order
00142 
00143   void clear();                                         // clear bbTable
00144   void apply(BBDoFn f);
00145     
00146   void makeUses();
00147   void eliminateUnneededResults();
00148   void computeEscapingBlocks();
00149   void computeUplevelAccesses();
00150   void localAlloc();
00151   void localCopyPropagate();
00152   void globalCopyPropagate();
00153   void bruteForceCopyPropagate();
00154   void gen(Node* first);
00155   void apply(NodeVisitor* v);
00156   bool verifyLabels();
00157 
00158   void print();
00159   void print_code(bool suppressTrivial);
00160   void verify();
00161 };
00162 
00163 extern BBIterator* bbIterator;
00164 
00165 
00166 // NodeVisitor
00167 
00168 class Node;
00169 class PrologueNode;
00170 class LoadIntNode;
00171 class LoadOffsetNode;
00172 class LoadUplevelNode;
00173 class AssignNode;
00174 class StoreOffsetNode;
00175 class StoreUplevelNode;
00176 class ArithRRNode;
00177 class ArithRCNode;
00178 class FloatArithRRNode;
00179 class FloatUnaryArithNode;
00180 class TArithRRNode;
00181 class ContextCreateNode;
00182 class ContextInitNode;
00183 class ContextZapNode;
00184 class BlockCreateNode;
00185 class BlockMaterializeNode;
00186 class SendNode;
00187 class PrimNode;
00188 class DLLNode;
00189 class LoopHeaderNode;
00190 class ReturnNode;
00191 class NLRSetupNode;
00192 class InlinedReturnNode;
00193 class NLRContinuationNode;
00194 class BranchNode;
00195 class TypeTestNode;
00196 class NLRTestNode;
00197 class MergeNode;
00198 class ArrayAtNode;
00199 class ArrayAtPutNode;
00200 class InlinedPrimitiveNode;
00201 class UncommonNode;
00202 class FixedCodeNode;
00203 class NopNode;
00204 class CommentNode;
00205 
00206 
00207 class NodeVisitor: public ResourceObj {
00208  public:
00209   // Basic blocks
00210   virtual void beginOfBasicBlock(Node* node) = 0;       // called for the first node in a BB, before the node's method is called
00211   virtual void endOfBasicBlock(Node* node) = 0;         // called for the last node in a BB, after the node's method was called
00212   
00213  public:
00214   // For all nodes
00215   virtual void beginOfNode(Node* node) = 0;             // called for each node, before the node's method is called
00216   virtual void endOfNode(Node* node) = 0;               // called for each node, after the node's method was called
00217 
00218  public:
00219   // Individual nodes
00220   virtual void aPrologueNode(PrologueNode* node) = 0;
00221 
00222   virtual void aLoadIntNode(LoadIntNode* node) = 0;
00223   virtual void aLoadOffsetNode(LoadOffsetNode* node) = 0;
00224   virtual void aLoadUplevelNode(LoadUplevelNode* node) = 0;
00225 
00226   virtual void anAssignNode(AssignNode* node) = 0;
00227   virtual void aStoreOffsetNode(StoreOffsetNode* node) = 0;
00228   virtual void aStoreUplevelNode(StoreUplevelNode* node) = 0;
00229   
00230   virtual void anArithRRNode(ArithRRNode* node) = 0;
00231   virtual void aFloatArithRRNode(FloatArithRRNode* node) = 0;
00232   virtual void aFloatUnaryArithNode(FloatUnaryArithNode* node) = 0;
00233   virtual void anArithRCNode(ArithRCNode* node) = 0;
00234   virtual void aTArithRRNode(TArithRRNode* node) = 0;
00235   
00236   virtual void aContextCreateNode(ContextCreateNode* node) = 0;
00237   virtual void aContextInitNode(ContextInitNode* node) = 0;
00238   virtual void aContextZapNode(ContextZapNode* node) = 0;
00239 
00240   virtual void aBlockCreateNode(BlockCreateNode* node) = 0;
00241   virtual void aBlockMaterializeNode(BlockMaterializeNode* node) = 0;
00242   
00243   virtual void aSendNode(SendNode* node) = 0;
00244   virtual void aPrimNode(PrimNode* node) = 0;
00245   virtual void aDLLNode(DLLNode* node) = 0;
00246 
00247   virtual void aLoopHeaderNode(LoopHeaderNode* node) = 0;
00248 
00249   virtual void aReturnNode(ReturnNode* node) = 0;
00250   virtual void aNLRSetupNode(NLRSetupNode* node) = 0;
00251   virtual void anInlinedReturnNode(InlinedReturnNode* node) = 0;
00252   virtual void aNLRContinuationNode(NLRContinuationNode* node) = 0;
00253   
00254   virtual void aBranchNode(BranchNode* node) = 0;
00255   virtual void aTypeTestNode(TypeTestNode* node) = 0;
00256   virtual void aNLRTestNode(NLRTestNode* node) = 0;
00257   virtual void aMergeNode(MergeNode* node) = 0;
00258   
00259   virtual void anArrayAtNode(ArrayAtNode* node) = 0;
00260   virtual void anArrayAtPutNode(ArrayAtPutNode* node) = 0;
00261 
00262   virtual void anInlinedPrimitiveNode(InlinedPrimitiveNode* node) = 0;
00263   virtual void anUncommonNode(UncommonNode* node) = 0;
00264   virtual void aFixedCodeNode(FixedCodeNode* node) = 0;
00265 
00266   virtual void aNopNode(NopNode* node) = 0;
00267   virtual void aCommentNode(CommentNode* node) = 0;
00268 };
00269 
00270 
00271 # endif

Generated on Mon Oct 9 13:37:02 2006 for Strongtalk VM by  doxygen 1.4.7