Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:19

0001 //===- llvm/Analysis/Trace.h - Represent one trace of LLVM code -*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This class represents a single trace of LLVM basic blocks.  A trace is a
0010 // single entry, multiple exit, region of code that is often hot.  Trace-based
0011 // optimizations treat traces almost like they are a large, strange, basic
0012 // block: because the trace path is assumed to be hot, optimizations for the
0013 // fall-through path are made at the expense of the non-fall-through paths.
0014 //
0015 //===----------------------------------------------------------------------===//
0016 
0017 #ifndef LLVM_ANALYSIS_TRACE_H
0018 #define LLVM_ANALYSIS_TRACE_H
0019 
0020 #include <cassert>
0021 #include <vector>
0022 
0023 namespace llvm {
0024 
0025 class BasicBlock;
0026 class Function;
0027 class Module;
0028 class raw_ostream;
0029 
0030 class Trace {
0031   using BasicBlockListType = std::vector<BasicBlock *>;
0032 
0033   BasicBlockListType BasicBlocks;
0034 
0035 public:
0036   /// Trace ctor - Make a new trace from a vector of basic blocks,
0037   /// residing in the function which is the parent of the first
0038   /// basic block in the vector.
0039   Trace(const std::vector<BasicBlock *> &vBB) : BasicBlocks (vBB) {}
0040 
0041   /// getEntryBasicBlock - Return the entry basic block (first block)
0042   /// of the trace.
0043   BasicBlock *getEntryBasicBlock () const { return BasicBlocks[0]; }
0044 
0045   /// operator[]/getBlock - Return basic block N in the trace.
0046   BasicBlock *operator[](unsigned i) const { return BasicBlocks[i]; }
0047   BasicBlock *getBlock(unsigned i)   const { return BasicBlocks[i]; }
0048 
0049   /// getFunction - Return this trace's parent function.
0050   Function *getFunction () const;
0051 
0052   /// getModule - Return this Module that contains this trace's parent
0053   /// function.
0054   Module *getModule () const;
0055 
0056   /// getBlockIndex - Return the index of the specified basic block in the
0057   /// trace, or -1 if it is not in the trace.
0058   int getBlockIndex(const BasicBlock *X) const {
0059     for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
0060       if (BasicBlocks[i] == X)
0061         return i;
0062     return -1;
0063   }
0064 
0065   /// contains - Returns true if this trace contains the given basic
0066   /// block.
0067   bool contains(const BasicBlock *X) const {
0068     return getBlockIndex(X) != -1;
0069   }
0070 
0071   /// Returns true if B1 occurs before B2 in the trace, or if it is the same
0072   /// block as B2..  Both blocks must be in the trace.
0073   bool dominates(const BasicBlock *B1, const BasicBlock *B2) const {
0074     int B1Idx = getBlockIndex(B1), B2Idx = getBlockIndex(B2);
0075     assert(B1Idx != -1 && B2Idx != -1 && "Block is not in the trace!");
0076     return B1Idx <= B2Idx;
0077   }
0078 
0079   // BasicBlock iterators...
0080   using iterator = BasicBlockListType::iterator;
0081   using const_iterator = BasicBlockListType::const_iterator;
0082   using reverse_iterator = std::reverse_iterator<iterator>;
0083   using const_reverse_iterator = std::reverse_iterator<const_iterator>;
0084 
0085   iterator                begin()       { return BasicBlocks.begin(); }
0086   const_iterator          begin() const { return BasicBlocks.begin(); }
0087   iterator                end  ()       { return BasicBlocks.end();   }
0088   const_iterator          end  () const { return BasicBlocks.end();   }
0089 
0090   reverse_iterator       rbegin()       { return BasicBlocks.rbegin(); }
0091   const_reverse_iterator rbegin() const { return BasicBlocks.rbegin(); }
0092   reverse_iterator       rend  ()       { return BasicBlocks.rend();   }
0093   const_reverse_iterator rend  () const { return BasicBlocks.rend();   }
0094 
0095   unsigned                 size() const { return BasicBlocks.size(); }
0096   bool                    empty() const { return BasicBlocks.empty(); }
0097 
0098   iterator erase(iterator q)               { return BasicBlocks.erase (q); }
0099   iterator erase(iterator q1, iterator q2) { return BasicBlocks.erase (q1, q2); }
0100 
0101   /// print - Write trace to output stream.
0102   void print(raw_ostream &O) const;
0103 
0104   /// dump - Debugger convenience method; writes trace to standard error
0105   /// output stream.
0106   void dump() const;
0107 };
0108 
0109 } // end namespace llvm
0110 
0111 #endif // LLVM_ANALYSIS_TRACE_H