blob: 09c4423a2f057319d5097d18bdc4c3f62eac48f4 [file] [log] [blame]
Eugene Zelenko94348112017-09-29 21:55:49 +00001//===- llvm/CodeGen/CriticalAntiDepBreaker.h - Anti-Dep Support -*- C++ -*-===//
David Goodwin2e7be612009-10-26 16:59:04 +00002//
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 implements the CriticalAntiDepBreaker class, which
11// implements register anti-dependence breaking along a blocks
12// critical path during post-RA scheduler.
13//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramer00e08fc2014-08-13 16:26:38 +000016#ifndef LLVM_LIB_CODEGEN_CRITICALANTIDEPBREAKER_H
17#define LLVM_LIB_CODEGEN_CRITICALANTIDEPBREAKER_H
David Goodwin2e7be612009-10-26 16:59:04 +000018
David Goodwin82c72482009-10-28 18:29:54 +000019#include "AntiDepBreaker.h"
Chandler Carrutha1514e22012-12-04 07:12:27 +000020#include "llvm/ADT/BitVector.h"
Eugene Zelenko94348112017-09-29 21:55:49 +000021#include "llvm/Support/Compiler.h"
22#include <map>
23#include <vector>
David Goodwin2e7be612009-10-26 16:59:04 +000024
25namespace llvm {
Eugene Zelenko94348112017-09-29 21:55:49 +000026
27class MachineBasicBlock;
28class MachineFunction;
29class MachineInstr;
30class MachineOperand;
31class MachineRegisterInfo;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000032class RegisterClassInfo;
Evan Cheng46df4eb2010-06-16 07:35:02 +000033class TargetInstrInfo;
Eugene Zelenko94348112017-09-29 21:55:49 +000034class TargetRegisterClass;
Evan Cheng46df4eb2010-06-16 07:35:02 +000035class TargetRegisterInfo;
36
Benjamin Kramerb4537752015-07-01 14:47:39 +000037class LLVM_LIBRARY_VISIBILITY CriticalAntiDepBreaker : public AntiDepBreaker {
David Goodwin2e7be612009-10-26 16:59:04 +000038 MachineFunction& MF;
39 MachineRegisterInfo &MRI;
Evan Cheng46df4eb2010-06-16 07:35:02 +000040 const TargetInstrInfo *TII;
David Goodwin2e7be612009-10-26 16:59:04 +000041 const TargetRegisterInfo *TRI;
Jakob Stoklund Olesenfa796dd2011-06-16 21:56:21 +000042 const RegisterClassInfo &RegClassInfo;
David Goodwin2e7be612009-10-26 16:59:04 +000043
Sanjay Patel42dac652014-09-21 14:48:16 +000044 /// The set of allocatable registers.
David Goodwin2e7be612009-10-26 16:59:04 +000045 /// We'll be ignoring anti-dependencies on non-allocatable registers,
46 /// because they may not be safe to break.
47 const BitVector AllocatableSet;
48
Sanjay Patel42dac652014-09-21 14:48:16 +000049 /// For live regs that are only used in one register class in a
David Goodwin2e7be612009-10-26 16:59:04 +000050 /// live range, the register class. If the register is not live, the
51 /// corresponding value is null. If the register is live but used in
52 /// multiple register classes, the corresponding value is -1 casted to a
53 /// pointer.
Eugene Zelenko94348112017-09-29 21:55:49 +000054 std::vector<const TargetRegisterClass *> Classes;
David Goodwin2e7be612009-10-26 16:59:04 +000055
Sanjay Patel42dac652014-09-21 14:48:16 +000056 /// Map registers to all their references within a live range.
David Goodwin2e7be612009-10-26 16:59:04 +000057 std::multimap<unsigned, MachineOperand *> RegRefs;
Eugene Zelenko94348112017-09-29 21:55:49 +000058
59 using RegRefIter =
60 std::multimap<unsigned, MachineOperand *>::const_iterator;
David Goodwin2e7be612009-10-26 16:59:04 +000061
Sanjay Patel42dac652014-09-21 14:48:16 +000062 /// The index of the most recent kill (proceeding bottom-up),
David Goodwin2e7be612009-10-26 16:59:04 +000063 /// or ~0u if the register is not live.
Bill Wendling9c2a0342010-07-15 19:58:14 +000064 std::vector<unsigned> KillIndices;
David Goodwin2e7be612009-10-26 16:59:04 +000065
Sanjay Patel42dac652014-09-21 14:48:16 +000066 /// The index of the most recent complete def (proceeding
Sanjay Patel00295342014-06-24 21:11:51 +000067 /// bottom up), or ~0u if the register is live.
Bill Wendling9c2a0342010-07-15 19:58:14 +000068 std::vector<unsigned> DefIndices;
David Goodwin2e7be612009-10-26 16:59:04 +000069
Sanjay Patel42dac652014-09-21 14:48:16 +000070 /// A set of registers which are live and cannot be changed to
David Goodwin2e7be612009-10-26 16:59:04 +000071 /// break anti-dependencies.
Benjamin Kramercff4ad72012-03-17 20:22:57 +000072 BitVector KeepRegs;
David Goodwin2e7be612009-10-26 16:59:04 +000073
74 public:
Eugene Zelenko94348112017-09-29 21:55:49 +000075 CriticalAntiDepBreaker(MachineFunction& MFi, const RegisterClassInfo &RCI);
Alexander Kornienkoc16fc542015-04-11 02:11:45 +000076 ~CriticalAntiDepBreaker() override;
Jim Grosbach2973b572010-01-06 16:48:02 +000077
Sanjay Patel42dac652014-09-21 14:48:16 +000078 /// Initialize anti-dep breaking for a new basic block.
Craig Topper9f998de2014-03-07 09:26:03 +000079 void StartBlock(MachineBasicBlock *BB) override;
David Goodwin2e7be612009-10-26 16:59:04 +000080
Sanjay Patel42dac652014-09-21 14:48:16 +000081 /// Identifiy anti-dependencies along the critical path
David Goodwin2e7be612009-10-26 16:59:04 +000082 /// of the ScheduleDAG and break them by renaming registers.
Eugene Zelenko94348112017-09-29 21:55:49 +000083 unsigned BreakAntiDependencies(const std::vector<SUnit> &SUnits,
Dan Gohman66db3a02010-04-19 23:11:58 +000084 MachineBasicBlock::iterator Begin,
85 MachineBasicBlock::iterator End,
Devang Patele29e8e12011-06-02 21:26:52 +000086 unsigned InsertPosIndex,
Craig Topper9f998de2014-03-07 09:26:03 +000087 DbgValueVector &DbgValues) override;
David Goodwin2e7be612009-10-26 16:59:04 +000088
Sanjay Patel42dac652014-09-21 14:48:16 +000089 /// Update liveness information to account for the current
David Goodwin2e7be612009-10-26 16:59:04 +000090 /// instruction, which will not be scheduled.
Duncan P. N. Exon Smitha26cd9c2016-02-27 19:33:37 +000091 void Observe(MachineInstr &MI, unsigned Count,
Craig Topper9f998de2014-03-07 09:26:03 +000092 unsigned InsertPosIndex) override;
David Goodwin2e7be612009-10-26 16:59:04 +000093
Sanjay Patel42dac652014-09-21 14:48:16 +000094 /// Finish anti-dep breaking for a basic block.
Craig Topper9f998de2014-03-07 09:26:03 +000095 void FinishBlock() override;
David Goodwin2e7be612009-10-26 16:59:04 +000096
97 private:
Duncan P. N. Exon Smitha26cd9c2016-02-27 19:33:37 +000098 void PrescanInstruction(MachineInstr &MI);
99 void ScanInstruction(MachineInstr &MI, unsigned Count);
Andrew Trickbc4bd922011-02-08 17:39:46 +0000100 bool isNewRegClobberedByRefs(RegRefIter RegRefBegin,
101 RegRefIter RegRefEnd,
102 unsigned NewReg);
Andrew Trick46388522010-11-02 18:16:45 +0000103 unsigned findSuitableFreeRegister(RegRefIter RegRefBegin,
104 RegRefIter RegRefEnd,
Jim Grosbach80c2b0d2010-01-06 22:21:25 +0000105 unsigned AntiDepReg,
David Goodwin2e7be612009-10-26 16:59:04 +0000106 unsigned LastNewReg,
Bill Schmidt5ff776b2013-01-28 18:36:58 +0000107 const TargetRegisterClass *RC,
Craig Topper78477ff2013-07-03 05:16:59 +0000108 SmallVectorImpl<unsigned> &Forbid);
David Goodwin2e7be612009-10-26 16:59:04 +0000109 };
David Goodwin2e7be612009-10-26 16:59:04 +0000110
Eugene Zelenko94348112017-09-29 21:55:49 +0000111} // end namespace llvm
112
113#endif // LLVM_LIB_CODEGEN_CRITICALANTIDEPBREAKER_H