Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 1 | //===- llvm/CodeGen/AntiDepBreaker.h - Anti-Dependence Breaking -*- C++ -*-===// |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 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 implements the AntiDepBreaker class, which implements |
| 11 | // anti-dependence breaking heuristics for post-register-allocation scheduling. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Benjamin Kramer | 00e08fc | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 15 | #ifndef LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H |
| 16 | #define LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 17 | |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/iterator_range.h" |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineInstr.h" |
| 21 | #include "llvm/CodeGen/MachineOperand.h" |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/ScheduleDAG.h" |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Compiler.h" |
| 24 | #include <cassert> |
| 25 | #include <utility> |
David Goodwin | 557bbe6 | 2009-11-20 19:32:48 +0000 | [diff] [blame] | 26 | #include <vector> |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 27 | |
| 28 | namespace llvm { |
| 29 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 30 | /// This class works in conjunction with the post-RA scheduler to rename |
| 31 | /// registers to break register anti-dependencies (WAR hazards). |
Benjamin Kramer | b453775 | 2015-07-01 14:47:39 +0000 | [diff] [blame] | 32 | class LLVM_LIBRARY_VISIBILITY AntiDepBreaker { |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 33 | public: |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 34 | using DbgValueVector = |
| 35 | std::vector<std::pair<MachineInstr *, MachineInstr *>>; |
Devang Patel | e29e8e1 | 2011-06-02 21:26:52 +0000 | [diff] [blame] | 36 | |
David Goodwin | ada0ef8 | 2009-10-26 19:41:00 +0000 | [diff] [blame] | 37 | virtual ~AntiDepBreaker(); |
David Goodwin | c932213 | 2009-10-26 19:00:47 +0000 | [diff] [blame] | 38 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 39 | /// Initialize anti-dep breaking for a new basic block. |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 40 | virtual void StartBlock(MachineBasicBlock *BB) = 0; |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 41 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 42 | /// Identifiy anti-dependencies within a basic-block region and break them by |
| 43 | /// renaming registers. Return the number of anti-dependencies broken. |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 44 | virtual unsigned BreakAntiDependencies(const std::vector<SUnit> &SUnits, |
Devang Patel | e29e8e1 | 2011-06-02 21:26:52 +0000 | [diff] [blame] | 45 | MachineBasicBlock::iterator Begin, |
| 46 | MachineBasicBlock::iterator End, |
| 47 | unsigned InsertPosIndex, |
| 48 | DbgValueVector &DbgValues) = 0; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 49 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 50 | /// Update liveness information to account for the current |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 51 | /// instruction, which will not be scheduled. |
Duncan P. N. Exon Smith | a26cd9c | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 52 | virtual void Observe(MachineInstr &MI, unsigned Count, |
| 53 | unsigned InsertPosIndex) = 0; |
| 54 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 55 | /// Finish anti-dep breaking for a basic block. |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 56 | virtual void FinishBlock() = 0; |
Devang Patel | e29e8e1 | 2011-06-02 21:26:52 +0000 | [diff] [blame] | 57 | |
Sanjay Patel | 42dac65 | 2014-09-21 14:48:16 +0000 | [diff] [blame] | 58 | /// Update DBG_VALUE if dependency breaker is updating |
Devang Patel | e29e8e1 | 2011-06-02 21:26:52 +0000 | [diff] [blame] | 59 | /// other machine instruction to use NewReg. |
Duncan P. N. Exon Smith | a26cd9c | 2016-02-27 19:33:37 +0000 | [diff] [blame] | 60 | void UpdateDbgValue(MachineInstr &MI, unsigned OldReg, unsigned NewReg) { |
| 61 | assert(MI.isDebugValue() && "MI is not DBG_VALUE!"); |
| 62 | if (MI.getOperand(0).isReg() && MI.getOperand(0).getReg() == OldReg) |
| 63 | MI.getOperand(0).setReg(NewReg); |
Devang Patel | e29e8e1 | 2011-06-02 21:26:52 +0000 | [diff] [blame] | 64 | } |
Andrew Ng | d8be4bf | 2017-04-25 15:39:57 +0000 | [diff] [blame] | 65 | |
| 66 | /// Update all DBG_VALUE instructions that may be affected by the dependency |
| 67 | /// breaker's update of ParentMI to use NewReg. |
| 68 | void UpdateDbgValues(const DbgValueVector &DbgValues, MachineInstr *ParentMI, |
| 69 | unsigned OldReg, unsigned NewReg) { |
| 70 | // The following code is dependent on the order in which the DbgValues are |
| 71 | // constructed in ScheduleDAGInstrs::buildSchedGraph. |
| 72 | MachineInstr *PrevDbgMI = nullptr; |
| 73 | for (const auto &DV : make_range(DbgValues.crbegin(), DbgValues.crend())) { |
| 74 | MachineInstr *PrevMI = DV.second; |
| 75 | if ((PrevMI == ParentMI) || (PrevMI == PrevDbgMI)) { |
| 76 | MachineInstr *DbgMI = DV.first; |
| 77 | UpdateDbgValue(*DbgMI, OldReg, NewReg); |
| 78 | PrevDbgMI = DbgMI; |
| 79 | } else if (PrevDbgMI) { |
| 80 | break; // If no match and already found a DBG_VALUE, we're done. |
| 81 | } |
| 82 | } |
| 83 | } |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 84 | }; |
| 85 | |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 86 | } // end namespace llvm |
David Goodwin | 2e7be61 | 2009-10-26 16:59:04 +0000 | [diff] [blame] | 87 | |
Eugene Zelenko | 9434811 | 2017-09-29 21:55:49 +0000 | [diff] [blame] | 88 | #endif // LLVM_LIB_CODEGEN_ANTIDEPBREAKER_H |