1 //===-- llvm/CodeGen/TailDuplicator.h ---------------------------*- 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 file defines the TailDuplicator class. Used by the 11 // TailDuplication pass, and MachineBlockPlacement. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CODEGEN_TAILDUPLICATOR_H 16 #define LLVM_CODEGEN_TAILDUPLICATOR_H 17 18 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h" 19 #include "llvm/CodeGen/MachineModuleInfo.h" 20 #include "llvm/CodeGen/MachineRegisterInfo.h" 21 #include "llvm/CodeGen/MachineSSAUpdater.h" 22 #include "llvm/CodeGen/RegisterScavenging.h" 23 #include "llvm/Target/TargetInstrInfo.h" 24 #include "llvm/Target/TargetRegisterInfo.h" 25 #include "llvm/Target/TargetSubtargetInfo.h" 26 27 namespace llvm { 28 29 /// Utility class to perform tail duplication. 30 class TailDuplicator { 31 const TargetInstrInfo *TII; 32 const TargetRegisterInfo *TRI; 33 const MachineBranchProbabilityInfo *MBPI; 34 const MachineModuleInfo *MMI; 35 MachineRegisterInfo *MRI; 36 bool PreRegAlloc; 37 38 // A list of virtual registers for which to update SSA form. 39 SmallVector<unsigned, 16> SSAUpdateVRs; 40 41 // For each virtual register in SSAUpdateVals keep a list of source virtual 42 // registers. 43 typedef std::vector<std::pair<MachineBasicBlock *, unsigned>> AvailableValsTy; 44 45 DenseMap<unsigned, AvailableValsTy> SSAUpdateVals; 46 47 public: 48 void initMF(MachineFunction &MF, const MachineModuleInfo *MMI, 49 const MachineBranchProbabilityInfo *MBPI); 50 bool tailDuplicateBlocks(MachineFunction &MF); 51 static bool isSimpleBB(MachineBasicBlock *TailBB); 52 bool shouldTailDuplicate(const MachineFunction &MF, bool IsSimple, 53 MachineBasicBlock &TailBB); 54 bool tailDuplicateAndUpdate(MachineFunction &MF, bool IsSimple, 55 MachineBasicBlock *MBB); 56 57 private: 58 typedef TargetInstrInfo::RegSubRegPair RegSubRegPair; 59 60 void addSSAUpdateEntry(unsigned OrigReg, unsigned NewReg, 61 MachineBasicBlock *BB); 62 void processPHI(MachineInstr *MI, MachineBasicBlock *TailBB, 63 MachineBasicBlock *PredBB, 64 DenseMap<unsigned, RegSubRegPair> &LocalVRMap, 65 SmallVectorImpl<std::pair<unsigned, RegSubRegPair>> &Copies, 66 const DenseSet<unsigned> &UsedByPhi, bool Remove); 67 void duplicateInstruction(MachineInstr *MI, MachineBasicBlock *TailBB, 68 MachineBasicBlock *PredBB, MachineFunction &MF, 69 DenseMap<unsigned, RegSubRegPair> &LocalVRMap, 70 const DenseSet<unsigned> &UsedByPhi); 71 void updateSuccessorsPHIs(MachineBasicBlock *FromBB, bool isDead, 72 SmallVectorImpl<MachineBasicBlock *> &TDBBs, 73 SmallSetVector<MachineBasicBlock *, 8> &Succs); 74 bool canCompletelyDuplicateBB(MachineBasicBlock &BB); 75 bool duplicateSimpleBB(MachineBasicBlock *TailBB, 76 SmallVectorImpl<MachineBasicBlock *> &TDBBs, 77 const DenseSet<unsigned> &RegsUsedByPhi, 78 SmallVectorImpl<MachineInstr *> &Copies); 79 bool tailDuplicate(MachineFunction &MF, bool IsSimple, 80 MachineBasicBlock *TailBB, 81 SmallVectorImpl<MachineBasicBlock *> &TDBBs, 82 SmallVectorImpl<MachineInstr *> &Copies); 83 void appendCopies(MachineBasicBlock *MBB, 84 SmallVectorImpl<std::pair<unsigned,RegSubRegPair>> &CopyInfos, 85 SmallVectorImpl<MachineInstr *> &Copies); 86 87 void removeDeadBlock(MachineBasicBlock *MBB); 88 }; 89 90 } // End llvm namespace 91 92 #endif 93