Home | History | Annotate | Download | only in CodeGen
      1 //===-- PrologEpilogInserter.h - Prolog/Epilog code insertion -*- C++ -* --===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This pass is responsible for finalizing the functions frame layout, saving
     11 // callee saved registers, and for emitting prolog & epilog code for the
     12 // function.
     13 //
     14 // This pass must be run after register allocation.  After this pass is
     15 // executed, it is illegal to construct MO_FrameIndex operands.
     16 //
     17 // This pass also implements a shrink wrapping variant of prolog/epilog
     18 // insertion.
     19 //
     20 //===----------------------------------------------------------------------===//
     21 
     22 #ifndef LLVM_CODEGEN_PEI_H
     23 #define LLVM_CODEGEN_PEI_H
     24 
     25 #include "llvm/CodeGen/Passes.h"
     26 #include "llvm/CodeGen/MachineFunctionPass.h"
     27 #include "llvm/CodeGen/MachineLoopInfo.h"
     28 #include "llvm/ADT/SparseBitVector.h"
     29 #include "llvm/ADT/DenseMap.h"
     30 #include "llvm/Target/TargetRegisterInfo.h"
     31 
     32 namespace llvm {
     33   class RegScavenger;
     34   class MachineBasicBlock;
     35 
     36   class PEI : public MachineFunctionPass {
     37   public:
     38     static char ID;
     39     PEI() : MachineFunctionPass(ID) {
     40       initializePEIPass(*PassRegistry::getPassRegistry());
     41     }
     42 
     43     const char *getPassName() const {
     44       return "Prolog/Epilog Insertion & Frame Finalization";
     45     }
     46 
     47     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
     48 
     49     /// runOnMachineFunction - Insert prolog/epilog code and replace abstract
     50     /// frame indexes with appropriate references.
     51     ///
     52     bool runOnMachineFunction(MachineFunction &Fn);
     53 
     54   private:
     55     RegScavenger *RS;
     56 
     57     // MinCSFrameIndex, MaxCSFrameIndex - Keeps the range of callee saved
     58     // stack frame indexes.
     59     unsigned MinCSFrameIndex, MaxCSFrameIndex;
     60 
     61     // Analysis info for spill/restore placement.
     62     // "CSR": "callee saved register".
     63 
     64     // CSRegSet contains indices into the Callee Saved Register Info
     65     // vector built by calculateCalleeSavedRegisters() and accessed
     66     // via MF.getFrameInfo()->getCalleeSavedInfo().
     67     typedef SparseBitVector<> CSRegSet;
     68 
     69     // CSRegBlockMap maps MachineBasicBlocks to sets of callee
     70     // saved register indices.
     71     typedef DenseMap<MachineBasicBlock*, CSRegSet> CSRegBlockMap;
     72 
     73     // Set and maps for computing CSR spill/restore placement:
     74     //  used in function (UsedCSRegs)
     75     //  used in a basic block (CSRUsed)
     76     //  anticipatable in a basic block (Antic{In,Out})
     77     //  available in a basic block (Avail{In,Out})
     78     //  to be spilled at the entry to a basic block (CSRSave)
     79     //  to be restored at the end of a basic block (CSRRestore)
     80     CSRegSet UsedCSRegs;
     81     CSRegBlockMap CSRUsed;
     82     CSRegBlockMap AnticIn, AnticOut;
     83     CSRegBlockMap AvailIn, AvailOut;
     84     CSRegBlockMap CSRSave;
     85     CSRegBlockMap CSRRestore;
     86 
     87     // Entry and return blocks of the current function.
     88     MachineBasicBlock* EntryBlock;
     89     SmallVector<MachineBasicBlock*, 4> ReturnBlocks;
     90 
     91     // Map of MBBs to top level MachineLoops.
     92     DenseMap<MachineBasicBlock*, MachineLoop*> TLLoops;
     93 
     94     // Flag to control shrink wrapping per-function:
     95     // may choose to skip shrink wrapping for certain
     96     // functions.
     97     bool ShrinkWrapThisFunction;
     98 
     99     // Flag to control whether to use the register scavenger to resolve
    100     // frame index materialization registers. Set according to
    101     // TRI->requiresFrameIndexScavenging() for the curren function.
    102     bool FrameIndexVirtualScavenging;
    103 
    104 #ifndef NDEBUG
    105     // Machine function handle.
    106     MachineFunction* MF;
    107 
    108     // Flag indicating that the current function
    109     // has at least one "short" path in the machine
    110     // CFG from the entry block to an exit block.
    111     bool HasFastExitPath;
    112 #endif
    113 
    114     bool calculateSets(MachineFunction &Fn);
    115     bool calcAnticInOut(MachineBasicBlock* MBB);
    116     bool calcAvailInOut(MachineBasicBlock* MBB);
    117     void calculateAnticAvail(MachineFunction &Fn);
    118     bool addUsesForMEMERegion(MachineBasicBlock* MBB,
    119                               SmallVector<MachineBasicBlock*, 4>& blks);
    120     bool addUsesForTopLevelLoops(SmallVector<MachineBasicBlock*, 4>& blks);
    121     bool calcSpillPlacements(MachineBasicBlock* MBB,
    122                              SmallVector<MachineBasicBlock*, 4> &blks,
    123                              CSRegBlockMap &prevSpills);
    124     bool calcRestorePlacements(MachineBasicBlock* MBB,
    125                                SmallVector<MachineBasicBlock*, 4> &blks,
    126                                CSRegBlockMap &prevRestores);
    127     void placeSpillsAndRestores(MachineFunction &Fn);
    128     void placeCSRSpillsAndRestores(MachineFunction &Fn);
    129     void calculateCallsInformation(MachineFunction &Fn);
    130     void calculateCalleeSavedRegisters(MachineFunction &Fn);
    131     void insertCSRSpillsAndRestores(MachineFunction &Fn);
    132     void calculateFrameObjectOffsets(MachineFunction &Fn);
    133     void replaceFrameIndices(MachineFunction &Fn);
    134     void scavengeFrameVirtualRegs(MachineFunction &Fn);
    135     void insertPrologEpilogCode(MachineFunction &Fn);
    136 
    137     // Initialize DFA sets, called before iterations.
    138     void clearAnticAvailSets();
    139     // Clear all sets constructed by shrink wrapping.
    140     void clearAllSets();
    141 
    142     // Initialize all shrink wrapping data.
    143     void initShrinkWrappingInfo();
    144 
    145     // Convienences for dealing with machine loops.
    146     MachineBasicBlock* getTopLevelLoopPreheader(MachineLoop* LP);
    147     MachineLoop* getTopLevelLoopParent(MachineLoop *LP);
    148 
    149     // Propgate CSRs used in MBB to all MBBs of loop LP.
    150     void propagateUsesAroundLoop(MachineBasicBlock* MBB, MachineLoop* LP);
    151 
    152     // Convenience for recognizing return blocks.
    153     bool isReturnBlock(MachineBasicBlock* MBB);
    154 
    155 #ifndef NDEBUG
    156     // Debugging methods.
    157 
    158     // Mark this function as having fast exit paths.
    159     void findFastExitPath();
    160 
    161     // Verify placement of spills/restores.
    162     void verifySpillRestorePlacement();
    163 
    164     std::string getBasicBlockName(const MachineBasicBlock* MBB);
    165     std::string stringifyCSRegSet(const CSRegSet& s);
    166     void dumpSet(const CSRegSet& s);
    167     void dumpUsed(MachineBasicBlock* MBB);
    168     void dumpAllUsed();
    169     void dumpSets(MachineBasicBlock* MBB);
    170     void dumpSets1(MachineBasicBlock* MBB);
    171     void dumpAllSets();
    172     void dumpSRSets();
    173 #endif
    174 
    175   };
    176 } // End llvm namespace
    177 #endif
    178