1198892Srdivacky//=- llvm/CodeGen/CriticalAntiDepBreaker.h - Anti-Dep Support -*- C++ -*-=//
2198892Srdivacky//
3198892Srdivacky//                     The LLVM Compiler Infrastructure
4198892Srdivacky//
5198892Srdivacky// This file is distributed under the University of Illinois Open Source
6198892Srdivacky// License. See LICENSE.TXT for details.
7198892Srdivacky//
8198892Srdivacky//===----------------------------------------------------------------------===//
9198892Srdivacky//
10198892Srdivacky// This file implements the CriticalAntiDepBreaker class, which
11198892Srdivacky// implements register anti-dependence breaking along a blocks
12198892Srdivacky// critical path during post-RA scheduler.
13198892Srdivacky//
14198892Srdivacky//===----------------------------------------------------------------------===//
15198892Srdivacky
16198892Srdivacky#ifndef LLVM_CODEGEN_CRITICALANTIDEPBREAKER_H
17198892Srdivacky#define LLVM_CODEGEN_CRITICALANTIDEPBREAKER_H
18198892Srdivacky
19198892Srdivacky#include "AntiDepBreaker.h"
20249423Sdim#include "llvm/ADT/BitVector.h"
21198892Srdivacky#include "llvm/CodeGen/MachineBasicBlock.h"
22198892Srdivacky#include "llvm/CodeGen/MachineFrameInfo.h"
23198892Srdivacky#include "llvm/CodeGen/MachineFunction.h"
24198892Srdivacky#include "llvm/CodeGen/MachineRegisterInfo.h"
25239462Sdim#include "llvm/CodeGen/RegisterClassInfo.h"
26198892Srdivacky#include "llvm/CodeGen/ScheduleDAG.h"
27199989Srdivacky#include <map>
28198892Srdivacky
29198892Srdivackynamespace llvm {
30224145Sdimclass RegisterClassInfo;
31210299Sedclass TargetInstrInfo;
32210299Sedclass TargetRegisterInfo;
33210299Sed
34198892Srdivacky  class CriticalAntiDepBreaker : public AntiDepBreaker {
35198892Srdivacky    MachineFunction& MF;
36198892Srdivacky    MachineRegisterInfo &MRI;
37210299Sed    const TargetInstrInfo *TII;
38198892Srdivacky    const TargetRegisterInfo *TRI;
39224145Sdim    const RegisterClassInfo &RegClassInfo;
40198892Srdivacky
41198892Srdivacky    /// AllocatableSet - The set of allocatable registers.
42198892Srdivacky    /// We'll be ignoring anti-dependencies on non-allocatable registers,
43198892Srdivacky    /// because they may not be safe to break.
44198892Srdivacky    const BitVector AllocatableSet;
45198892Srdivacky
46198892Srdivacky    /// Classes - For live regs that are only used in one register class in a
47198892Srdivacky    /// live range, the register class. If the register is not live, the
48198892Srdivacky    /// corresponding value is null. If the register is live but used in
49198892Srdivacky    /// multiple register classes, the corresponding value is -1 casted to a
50198892Srdivacky    /// pointer.
51212904Sdim    std::vector<const TargetRegisterClass*> Classes;
52198892Srdivacky
53218893Sdim    /// RegRefs - Map registers to all their references within a live range.
54198892Srdivacky    std::multimap<unsigned, MachineOperand *> RegRefs;
55218893Sdim    typedef std::multimap<unsigned, MachineOperand *>::const_iterator
56218893Sdim      RegRefIter;
57198892Srdivacky
58198892Srdivacky    /// KillIndices - The index of the most recent kill (proceding bottom-up),
59198892Srdivacky    /// or ~0u if the register is not live.
60212904Sdim    std::vector<unsigned> KillIndices;
61198892Srdivacky
62198892Srdivacky    /// DefIndices - The index of the most recent complete def (proceding bottom
63198892Srdivacky    /// up), or ~0u if the register is live.
64212904Sdim    std::vector<unsigned> DefIndices;
65198892Srdivacky
66198892Srdivacky    /// KeepRegs - A set of registers which are live and cannot be changed to
67198892Srdivacky    /// break anti-dependencies.
68234353Sdim    BitVector KeepRegs;
69198892Srdivacky
70198892Srdivacky  public:
71224145Sdim    CriticalAntiDepBreaker(MachineFunction& MFi, const RegisterClassInfo&);
72198892Srdivacky    ~CriticalAntiDepBreaker();
73202375Srdivacky
74198892Srdivacky    /// Start - Initialize anti-dep breaking for a new basic block.
75198892Srdivacky    void StartBlock(MachineBasicBlock *BB);
76198892Srdivacky
77202375Srdivacky    /// BreakAntiDependencies - Identifiy anti-dependencies along the critical
78202375Srdivacky    /// path
79198892Srdivacky    /// of the ScheduleDAG and break them by renaming registers.
80198892Srdivacky    ///
81207618Srdivacky    unsigned BreakAntiDependencies(const std::vector<SUnit>& SUnits,
82207618Srdivacky                                   MachineBasicBlock::iterator Begin,
83207618Srdivacky                                   MachineBasicBlock::iterator End,
84223017Sdim                                   unsigned InsertPosIndex,
85223017Sdim                                   DbgValueVector &DbgValues);
86198892Srdivacky
87198892Srdivacky    /// Observe - Update liveness information to account for the current
88198892Srdivacky    /// instruction, which will not be scheduled.
89198892Srdivacky    ///
90198892Srdivacky    void Observe(MachineInstr *MI, unsigned Count, unsigned InsertPosIndex);
91198892Srdivacky
92198892Srdivacky    /// Finish - Finish anti-dep breaking for a basic block.
93198892Srdivacky    void FinishBlock();
94198892Srdivacky
95198892Srdivacky  private:
96198892Srdivacky    void PrescanInstruction(MachineInstr *MI);
97198892Srdivacky    void ScanInstruction(MachineInstr *MI, unsigned Count);
98218893Sdim    bool isNewRegClobberedByRefs(RegRefIter RegRefBegin,
99218893Sdim                                 RegRefIter RegRefEnd,
100218893Sdim                                 unsigned NewReg);
101218893Sdim    unsigned findSuitableFreeRegister(RegRefIter RegRefBegin,
102218893Sdim                                      RegRefIter RegRefEnd,
103202375Srdivacky                                      unsigned AntiDepReg,
104198892Srdivacky                                      unsigned LastNewReg,
105249423Sdim                                      const TargetRegisterClass *RC,
106263508Sdim                                      SmallVectorImpl<unsigned> &Forbid);
107198892Srdivacky  };
108198892Srdivacky}
109198892Srdivacky
110198892Srdivacky#endif
111