Home | History | Annotate | Download | only in opt
      1 // Copyright (c) 2017 The Khronos Group Inc.
      2 // Copyright (c) 2017 Valve Corporation
      3 // Copyright (c) 2017 LunarG Inc.
      4 //
      5 // Licensed under the Apache License, Version 2.0 (the "License");
      6 // you may not use this file except in compliance with the License.
      7 // You may obtain a copy of the License at
      8 //
      9 //     http://www.apache.org/licenses/LICENSE-2.0
     10 //
     11 // Unless required by applicable law or agreed to in writing, software
     12 // distributed under the License is distributed on an "AS IS" BASIS,
     13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 // See the License for the specific language governing permissions and
     15 // limitations under the License.
     16 
     17 #ifndef LIBSPIRV_OPT_LOCAL_SSA_ELIM_PASS_H_
     18 #define LIBSPIRV_OPT_LOCAL_SSA_ELIM_PASS_H_
     19 
     20 
     21 #include <algorithm>
     22 #include <map>
     23 #include <queue>
     24 #include <utility>
     25 #include <unordered_map>
     26 #include <unordered_set>
     27 
     28 #include "basic_block.h"
     29 #include "def_use_manager.h"
     30 #include "module.h"
     31 #include "mem_pass.h"
     32 
     33 namespace spvtools {
     34 namespace opt {
     35 
     36 // See optimizer.hpp for documentation.
     37 class LocalMultiStoreElimPass : public MemPass {
     38   using cbb_ptr = const ir::BasicBlock*;
     39 
     40  public:
     41    using GetBlocksFunction =
     42      std::function<std::vector<ir::BasicBlock*>*(const ir::BasicBlock*)>;
     43 
     44   LocalMultiStoreElimPass();
     45   const char* name() const override { return "eliminate-local-multi-store"; }
     46   Status Process(ir::Module*) override;
     47 
     48  private:
     49   // Return type id for |ptrInst|'s pointee
     50   uint32_t GetPointeeTypeId(const ir::Instruction* ptrInst) const;
     51 
     52   // Return true if all uses of |varId| are only through supported reference
     53   // operations ie. loads and store. Also cache in supported_ref_vars_;
     54   bool HasOnlySupportedRefs(uint32_t varId);
     55 
     56   // Initialize data structures used by EliminateLocalMultiStore for
     57   // function |func|, specifically block predecessors and target variables.
     58   void InitSSARewrite(ir::Function& func);
     59 
     60   // Returns the id of the merge block declared by a merge instruction in
     61   // this block, if any.  If none, returns zero.
     62   uint32_t MergeBlockIdIfAny(const ir::BasicBlock& blk, uint32_t* cbid);
     63 
     64   // Compute structured successors for function |func|.
     65   // A block's structured successors are the blocks it branches to
     66   // together with its declared merge block if it has one.
     67   // When order matters, the merge block always appears first.
     68   // This assures correct depth first search in the presence of early
     69   // returns and kills. If the successor vector contain duplicates
     70   // if the merge block, they are safely ignored by DFS.
     71   void ComputeStructuredSuccessors(ir::Function* func);
     72 
     73   // Compute structured block order for |func| into |structuredOrder|. This
     74   // order has the property that dominators come before all blocks they
     75   // dominate and merge blocks come after all blocks that are in the control
     76   // constructs of their header.
     77   void ComputeStructuredOrder(ir::Function* func,
     78       std::list<ir::BasicBlock*>* order);
     79 
     80   // Return true if loop header block
     81   bool IsLoopHeader(ir::BasicBlock* block_ptr) const;
     82 
     83   // Initialize label2ssa_map_ entry for block |block_ptr| with single
     84   // predecessor.
     85   void SSABlockInitSinglePred(ir::BasicBlock* block_ptr);
     86 
     87   // Return true if variable is loaded in block with |label| or in
     88   // any succeeding block in structured order.
     89   bool IsLiveAfter(uint32_t var_id, uint32_t label) const;
     90 
     91   // Initialize label2ssa_map_ entry for loop header block pointed to
     92   // |block_itr| by merging entries from all predecessors. If any value
     93   // ids differ for any variable across predecessors, create a phi function
     94   // in the block and use that value id for the variable in the new map.
     95   // Assumes all predecessors have been visited by EliminateLocalMultiStore
     96   // except the back edge. Use a dummy value in the phi for the back edge
     97   // until the back edge block is visited and patch the phi value then.
     98   void SSABlockInitLoopHeader(std::list<ir::BasicBlock*>::iterator block_itr);
     99 
    100   // Initialize label2ssa_map_ entry for multiple predecessor block
    101   // |block_ptr| by merging label2ssa_map_ entries for all predecessors.
    102   // If any value ids differ for any variable across predecessors, create
    103   // a phi function in the block and use that value id for the variable in
    104   // the new map. Assumes all predecessors have been visited by
    105   // EliminateLocalMultiStore.
    106   void SSABlockInitMultiPred(ir::BasicBlock* block_ptr);
    107 
    108   // Initialize the label2ssa_map entry for a block pointed to by |block_itr|.
    109   // Insert phi instructions into block when necessary. All predecessor
    110   // blocks must have been visited by EliminateLocalMultiStore except for
    111   // backedges.
    112   void SSABlockInit(std::list<ir::BasicBlock*>::iterator block_itr);
    113 
    114   // Return undef in function for type. Create and insert an undef after the
    115   // first non-variable in the function if it doesn't already exist. Add
    116   // undef to function undef map.
    117   uint32_t Type2Undef(uint32_t type_id);
    118 
    119   // Patch phis in loop header block now that the map is complete for the
    120   // backedge predecessor. Specifically, for each phi, find the value
    121   // corresponding to the backedge predecessor. That contains the variable id
    122   // that this phi corresponds to. Change this phi operand to the the value
    123   // which corresponds to that variable in the predecessor map.
    124   void PatchPhis(uint32_t header_id, uint32_t back_id);
    125 
    126   // Initialize extensions whitelist
    127   void InitExtensions();
    128 
    129   // Return true if all extensions in this module are allowed by this pass.
    130   bool AllExtensionsSupported() const;
    131 
    132   // Remove remaining loads and stores of function scope variables only
    133   // referenced with non-access-chain loads and stores from function |func|.
    134   // Insert Phi functions where necessary. Running LocalAccessChainRemoval,
    135   // SingleBlockLocalElim and SingleStoreLocalElim beforehand will improve
    136   // the runtime and effectiveness of this function.
    137   bool EliminateMultiStoreLocal(ir::Function* func);
    138 
    139   // Save next available id into |module|.
    140   inline void FinalizeNextId(ir::Module* module) {
    141     module->SetIdBound(next_id_);
    142   }
    143 
    144   // Return next available id and calculate next.
    145   inline uint32_t TakeNextId() {
    146     return next_id_++;
    147   }
    148 
    149   void Initialize(ir::Module* module);
    150   Pass::Status ProcessImpl();
    151 
    152   // Map from block's label id to block.
    153   std::unordered_map<uint32_t, ir::BasicBlock*> id2block_;
    154 
    155   // Set of label ids of visited blocks
    156   std::unordered_set<uint32_t> visitedBlocks_;
    157 
    158   // Map from type to undef
    159   std::unordered_map<uint32_t, uint32_t> type2undefs_;
    160 
    161   // Variables that are only referenced by supported operations for this
    162   // pass ie. loads and stores.
    163   std::unordered_set<uint32_t> supported_ref_vars_;
    164 
    165   // Map from block to its structured successor blocks. See
    166   // ComputeStructuredSuccessors() for definition.
    167   std::unordered_map<const ir::BasicBlock*, std::vector<ir::BasicBlock*>>
    168       block2structured_succs_;
    169 
    170   // Map from block's label id to its predecessor blocks ids
    171   std::unordered_map<uint32_t, std::vector<uint32_t>> label2preds_;
    172 
    173   // Map from block's label id to a map of a variable to its value at the
    174   // end of the block.
    175   std::unordered_map<uint32_t, std::unordered_map<uint32_t, uint32_t>>
    176       label2ssa_map_;
    177 
    178   // Extra block whose successors are all blocks with no predecessors
    179   // in function.
    180   ir::BasicBlock pseudo_entry_block_;
    181 
    182   // Extensions supported by this pass.
    183   std::unordered_set<std::string> extensions_whitelist_;
    184 
    185   // Next unused ID
    186   uint32_t next_id_;
    187 };
    188 
    189 }  // namespace opt
    190 }  // namespace spvtools
    191 
    192 #endif  // LIBSPIRV_OPT_LOCAL_SSA_ELIM_PASS_H_
    193 
    194