Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 1 | //===- ExecutionDepsFix.cpp - Fix execution dependecy issues ----*- C++ -*-===// |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +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 | // |
Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 10 | // This file contains the execution dependency fix pass. |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 11 | // |
Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 12 | // Some X86 SSE instructions like mov, and, or, xor are available in different |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 13 | // variants for different operand types. These variant instructions are |
| 14 | // equivalent, but on Nehalem and newer cpus there is extra latency |
Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 15 | // transferring data between integer and floating point domains. ARM cores |
| 16 | // have similar issues when they are configured with both VFP and NEON |
| 17 | // pipelines. |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 18 | // |
| 19 | // This pass changes the variant instructions to minimize domain crossings. |
| 20 | // |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/Passes.h" |
| 24 | #include "llvm/ADT/PostOrderIterator.h" |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/iterator_range.h" |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/LivePhysRegs.h" |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 28 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Marina Yatsina | 53ce3f9 | 2016-08-17 19:07:40 +0000 | [diff] [blame] | 29 | #include "llvm/CodeGen/RegisterClassInfo.h" |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Allocator.h" |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetInstrInfo.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 35 | |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 38 | #define DEBUG_TYPE "execution-fix" |
| 39 | |
Chris Lattner | 503a0ef | 2010-03-31 20:32:51 +0000 | [diff] [blame] | 40 | /// A DomainValue is a bit like LiveIntervals' ValNo, but it also keeps track |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 41 | /// of execution domains. |
| 42 | /// |
| 43 | /// An open DomainValue represents a set of instructions that can still switch |
| 44 | /// execution domain. Multiple registers may refer to the same open |
| 45 | /// DomainValue - they will eventually be collapsed to the same execution |
| 46 | /// domain. |
| 47 | /// |
| 48 | /// A collapsed DomainValue represents a single register that has been forced |
| 49 | /// into one of more execution domains. There is a separate collapsed |
| 50 | /// DomainValue for each register, but it may contain multiple execution |
| 51 | /// domains. A register value is initially created in a single execution |
| 52 | /// domain, but if we were forced to pay the penalty of a domain crossing, we |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 53 | /// keep track of the fact that the register is now available in multiple |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 54 | /// domains. |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 55 | namespace { |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 56 | struct DomainValue { |
| 57 | // Basic reference counting. |
| 58 | unsigned Refs; |
| 59 | |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 60 | // Bitmask of available domains. For an open DomainValue, it is the still |
| 61 | // possible domains for collapsing. For a collapsed DomainValue it is the |
| 62 | // domains where the register is available for free. |
| 63 | unsigned AvailableDomains; |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 64 | |
Jakob Stoklund Olesen | 53ec977 | 2011-11-09 00:06:18 +0000 | [diff] [blame] | 65 | // Pointer to the next DomainValue in a chain. When two DomainValues are |
| 66 | // merged, Victim.Next is set to point to Victor, so old DomainValue |
Benjamin Kramer | bde9176 | 2012-06-02 10:20:22 +0000 | [diff] [blame] | 67 | // references can be updated by following the chain. |
Jakob Stoklund Olesen | 53ec977 | 2011-11-09 00:06:18 +0000 | [diff] [blame] | 68 | DomainValue *Next; |
| 69 | |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 70 | // Twiddleable instructions using or defining these registers. |
| 71 | SmallVector<MachineInstr*, 8> Instrs; |
| 72 | |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 73 | // A collapsed DomainValue has no instructions to twiddle - it simply keeps |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 74 | // track of the domains where the registers are already available. |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 75 | bool isCollapsed() const { return Instrs.empty(); } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 76 | |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 77 | // Is domain available? |
| 78 | bool hasDomain(unsigned domain) const { |
Aaron Ballman | 0d6a010 | 2014-12-16 14:04:11 +0000 | [diff] [blame] | 79 | assert(domain < |
| 80 | static_cast<unsigned>(std::numeric_limits<unsigned>::digits) && |
Michael Ilseman | addddc4 | 2014-12-15 18:48:43 +0000 | [diff] [blame] | 81 | "undefined behavior"); |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 82 | return AvailableDomains & (1u << domain); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 85 | // Mark domain as available. |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 86 | void addDomain(unsigned domain) { |
| 87 | AvailableDomains |= 1u << domain; |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 90 | // Restrict to a single domain available. |
| 91 | void setSingleDomain(unsigned domain) { |
| 92 | AvailableDomains = 1u << domain; |
| 93 | } |
| 94 | |
| 95 | // Return bitmask of domains that are available and in mask. |
| 96 | unsigned getCommonDomains(unsigned mask) const { |
| 97 | return AvailableDomains & mask; |
| 98 | } |
| 99 | |
| 100 | // First domain available. |
| 101 | unsigned getFirstDomain() const { |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 102 | return countTrailingZeros(AvailableDomains); |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Jakob Stoklund Olesen | b7e44a3 | 2011-11-08 23:26:00 +0000 | [diff] [blame] | 105 | DomainValue() : Refs(0) { clear(); } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 106 | |
Jakob Stoklund Olesen | 53ec977 | 2011-11-09 00:06:18 +0000 | [diff] [blame] | 107 | // Clear this DomainValue and point to next which has all its data. |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 108 | void clear() { |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 109 | AvailableDomains = 0; |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 110 | Next = nullptr; |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 111 | Instrs.clear(); |
| 112 | } |
| 113 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 114 | } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 115 | |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 116 | namespace { |
Sanjay Patel | 4297c3f | 2015-03-15 18:16:04 +0000 | [diff] [blame] | 117 | /// Information about a live register. |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 118 | struct LiveReg { |
| 119 | /// Value currently in this register, or NULL when no value is being tracked. |
| 120 | /// This counts as a DomainValue reference. |
| 121 | DomainValue *Value; |
| 122 | |
| 123 | /// Instruction that defined this register, relative to the beginning of the |
| 124 | /// current basic block. When a LiveReg is used to represent a live-out |
| 125 | /// register, this value is relative to the end of the basic block, so it |
| 126 | /// will be a negative number. |
| 127 | int Def; |
| 128 | }; |
Sanjay Patel | 12fa37f | 2015-03-15 18:11:35 +0000 | [diff] [blame] | 129 | } // anonymous namespace |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 130 | |
| 131 | namespace { |
Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 132 | class ExeDepsFix : public MachineFunctionPass { |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 133 | static char ID; |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 134 | SpecificBumpPtrAllocator<DomainValue> Allocator; |
| 135 | SmallVector<DomainValue*,16> Avail; |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 136 | |
Jakob Stoklund Olesen | 30c8112 | 2011-09-27 23:50:46 +0000 | [diff] [blame] | 137 | const TargetRegisterClass *const RC; |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 138 | MachineFunction *MF; |
Jakob Stoklund Olesen | 30c8112 | 2011-09-27 23:50:46 +0000 | [diff] [blame] | 139 | const TargetInstrInfo *TII; |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 140 | const TargetRegisterInfo *TRI; |
Marina Yatsina | 53ce3f9 | 2016-08-17 19:07:40 +0000 | [diff] [blame] | 141 | RegisterClassInfo RegClassInfo; |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 142 | std::vector<SmallVector<int, 1>> AliasMap; |
Jakob Stoklund Olesen | 30c8112 | 2011-09-27 23:50:46 +0000 | [diff] [blame] | 143 | const unsigned NumRegs; |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 144 | LiveReg *LiveRegs; |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 145 | struct MBBInfo { |
| 146 | // Keeps clearance and domain information for all registers. Note that this |
| 147 | // is different from the usual definition notion of liveness. The CPU |
| 148 | // doesn't care whether or not we consider a register killed. |
| 149 | LiveReg *OutRegs; |
| 150 | |
| 151 | // Whether we have gotten to this block in primary processing yet. |
| 152 | bool PrimaryCompleted; |
| 153 | |
| 154 | // The number of predecessors for which primary processing has completed |
| 155 | unsigned IncomingProcessed; |
| 156 | |
| 157 | // The value of `IncomingProcessed` at the start of primary processing |
| 158 | unsigned PrimaryIncoming; |
| 159 | |
| 160 | // The number of predecessors for which all processing steps are done. |
| 161 | unsigned IncomingCompleted; |
| 162 | |
| 163 | MBBInfo() |
| 164 | : OutRegs(nullptr), PrimaryCompleted(false), IncomingProcessed(0), |
| 165 | PrimaryIncoming(0), IncomingCompleted(0) {} |
| 166 | }; |
| 167 | typedef DenseMap<MachineBasicBlock *, MBBInfo> MBBInfoMap; |
| 168 | MBBInfoMap MBBInfos; |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 169 | |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 170 | /// List of undefined register reads in this block in forward order. |
| 171 | std::vector<std::pair<MachineInstr*, unsigned> > UndefReads; |
| 172 | |
| 173 | /// Storage for register unit liveness. |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 174 | LivePhysRegs LiveRegSet; |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 175 | |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 176 | /// Current instruction number. |
| 177 | /// The first instruction in each basic block is 0. |
| 178 | int CurInstr; |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 179 | public: |
Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 180 | ExeDepsFix(const TargetRegisterClass *rc) |
Jakob Stoklund Olesen | 30c8112 | 2011-09-27 23:50:46 +0000 | [diff] [blame] | 181 | : MachineFunctionPass(ID), RC(rc), NumRegs(RC->getNumRegs()) {} |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 182 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 183 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 184 | AU.setPreservesAll(); |
| 185 | MachineFunctionPass::getAnalysisUsage(AU); |
| 186 | } |
| 187 | |
Craig Topper | 4584cd5 | 2014-03-07 09:26:03 +0000 | [diff] [blame] | 188 | bool runOnMachineFunction(MachineFunction &MF) override; |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 189 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 190 | MachineFunctionProperties getRequiredProperties() const override { |
| 191 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 192 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 195 | StringRef getPassName() const override { return "Execution dependency fix"; } |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 196 | |
| 197 | private: |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 198 | iterator_range<SmallVectorImpl<int>::const_iterator> |
Matthias Braun | 046318b | 2015-03-06 18:56:20 +0000 | [diff] [blame] | 199 | regIndices(unsigned Reg) const; |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 200 | // DomainValue allocation. |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 201 | DomainValue *alloc(int domain = -1); |
Jakob Stoklund Olesen | 53ec977 | 2011-11-09 00:06:18 +0000 | [diff] [blame] | 202 | DomainValue *retain(DomainValue *DV) { |
| 203 | if (DV) ++DV->Refs; |
| 204 | return DV; |
| 205 | } |
Jakob Stoklund Olesen | 1438e19 | 2011-11-08 21:57:44 +0000 | [diff] [blame] | 206 | void release(DomainValue*); |
Jakob Stoklund Olesen | 53ec977 | 2011-11-09 00:06:18 +0000 | [diff] [blame] | 207 | DomainValue *resolve(DomainValue*&); |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 208 | |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 209 | // LiveRegs manipulations. |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 210 | void setLiveReg(int rx, DomainValue *DV); |
| 211 | void kill(int rx); |
| 212 | void force(int rx, unsigned domain); |
| 213 | void collapse(DomainValue *dv, unsigned domain); |
| 214 | bool merge(DomainValue *A, DomainValue *B); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 215 | |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 216 | void enterBasicBlock(MachineBasicBlock*); |
Jakob Stoklund Olesen | 736cf46 | 2011-11-07 21:40:27 +0000 | [diff] [blame] | 217 | void leaveBasicBlock(MachineBasicBlock*); |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 218 | bool isBlockDone(MachineBasicBlock *); |
| 219 | void processBasicBlock(MachineBasicBlock *MBB, bool PrimaryPass); |
| 220 | void updateSuccessors(MachineBasicBlock *MBB, bool PrimaryPass); |
| 221 | bool visitInstr(MachineInstr *); |
| 222 | void processDefs(MachineInstr *, bool breakDependency, bool Kill); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 223 | void visitSoftInstr(MachineInstr*, unsigned mask); |
| 224 | void visitHardInstr(MachineInstr*, unsigned domain); |
Marina Yatsina | 88f0c31 | 2016-08-11 07:32:08 +0000 | [diff] [blame] | 225 | void pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx, |
| 226 | unsigned Pref); |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 227 | bool shouldBreakDependence(MachineInstr*, unsigned OpIdx, unsigned Pref); |
| 228 | void processUndefReads(MachineBasicBlock*); |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 229 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 230 | } |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 231 | |
Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 232 | char ExeDepsFix::ID = 0; |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 233 | |
Matthias Braun | 046318b | 2015-03-06 18:56:20 +0000 | [diff] [blame] | 234 | /// Translate TRI register number to a list of indices into our smaller tables |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 235 | /// of interesting registers. |
| 236 | iterator_range<SmallVectorImpl<int>::const_iterator> |
Matthias Braun | 046318b | 2015-03-06 18:56:20 +0000 | [diff] [blame] | 237 | ExeDepsFix::regIndices(unsigned Reg) const { |
Jakob Stoklund Olesen | 30c8112 | 2011-09-27 23:50:46 +0000 | [diff] [blame] | 238 | assert(Reg < AliasMap.size() && "Invalid register"); |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 239 | const auto &Entry = AliasMap[Reg]; |
| 240 | return make_range(Entry.begin(), Entry.end()); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 243 | DomainValue *ExeDepsFix::alloc(int domain) { |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 244 | DomainValue *dv = Avail.empty() ? |
| 245 | new(Allocator.Allocate()) DomainValue : |
| 246 | Avail.pop_back_val(); |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 247 | if (domain >= 0) |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 248 | dv->addDomain(domain); |
Jakob Stoklund Olesen | b7e44a3 | 2011-11-08 23:26:00 +0000 | [diff] [blame] | 249 | assert(dv->Refs == 0 && "Reference count wasn't cleared"); |
Jakob Stoklund Olesen | 53ec977 | 2011-11-09 00:06:18 +0000 | [diff] [blame] | 250 | assert(!dv->Next && "Chained DomainValue shouldn't have been recycled"); |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 251 | return dv; |
| 252 | } |
| 253 | |
Sanjay Patel | 4297c3f | 2015-03-15 18:16:04 +0000 | [diff] [blame] | 254 | /// Release a reference to DV. When the last reference is released, |
Jakob Stoklund Olesen | 1438e19 | 2011-11-08 21:57:44 +0000 | [diff] [blame] | 255 | /// collapse if needed. |
| 256 | void ExeDepsFix::release(DomainValue *DV) { |
Jakob Stoklund Olesen | 53ec977 | 2011-11-09 00:06:18 +0000 | [diff] [blame] | 257 | while (DV) { |
| 258 | assert(DV->Refs && "Bad DomainValue"); |
| 259 | if (--DV->Refs) |
| 260 | return; |
Jakob Stoklund Olesen | 1438e19 | 2011-11-08 21:57:44 +0000 | [diff] [blame] | 261 | |
Jakob Stoklund Olesen | 53ec977 | 2011-11-09 00:06:18 +0000 | [diff] [blame] | 262 | // There are no more DV references. Collapse any contained instructions. |
| 263 | if (DV->AvailableDomains && !DV->isCollapsed()) |
| 264 | collapse(DV, DV->getFirstDomain()); |
Jakob Stoklund Olesen | 1438e19 | 2011-11-08 21:57:44 +0000 | [diff] [blame] | 265 | |
Jakob Stoklund Olesen | 53ec977 | 2011-11-09 00:06:18 +0000 | [diff] [blame] | 266 | DomainValue *Next = DV->Next; |
| 267 | DV->clear(); |
| 268 | Avail.push_back(DV); |
| 269 | // Also release the next DomainValue in the chain. |
| 270 | DV = Next; |
| 271 | } |
| 272 | } |
| 273 | |
Sanjay Patel | 4297c3f | 2015-03-15 18:16:04 +0000 | [diff] [blame] | 274 | /// Follow the chain of dead DomainValues until a live DomainValue is reached. |
| 275 | /// Update the referenced pointer when necessary. |
Jakob Stoklund Olesen | 53ec977 | 2011-11-09 00:06:18 +0000 | [diff] [blame] | 276 | DomainValue *ExeDepsFix::resolve(DomainValue *&DVRef) { |
| 277 | DomainValue *DV = DVRef; |
| 278 | if (!DV || !DV->Next) |
| 279 | return DV; |
| 280 | |
| 281 | // DV has a chain. Find the end. |
| 282 | do DV = DV->Next; |
| 283 | while (DV->Next); |
| 284 | |
| 285 | // Update DVRef to point to DV. |
| 286 | retain(DV); |
| 287 | release(DVRef); |
| 288 | DVRef = DV; |
| 289 | return DV; |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 292 | /// Set LiveRegs[rx] = dv, updating reference counts. |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 293 | void ExeDepsFix::setLiveReg(int rx, DomainValue *dv) { |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 294 | assert(unsigned(rx) < NumRegs && "Invalid index"); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 295 | assert(LiveRegs && "Must enter basic block first."); |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 296 | |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 297 | if (LiveRegs[rx].Value == dv) |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 298 | return; |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 299 | if (LiveRegs[rx].Value) |
| 300 | release(LiveRegs[rx].Value); |
| 301 | LiveRegs[rx].Value = retain(dv); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | // Kill register rx, recycle or collapse any DomainValue. |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 305 | void ExeDepsFix::kill(int rx) { |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 306 | assert(unsigned(rx) < NumRegs && "Invalid index"); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 307 | assert(LiveRegs && "Must enter basic block first."); |
| 308 | if (!LiveRegs[rx].Value) |
| 309 | return; |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 310 | |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 311 | release(LiveRegs[rx].Value); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 312 | LiveRegs[rx].Value = nullptr; |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /// Force register rx into domain. |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 316 | void ExeDepsFix::force(int rx, unsigned domain) { |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 317 | assert(unsigned(rx) < NumRegs && "Invalid index"); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 318 | assert(LiveRegs && "Must enter basic block first."); |
| 319 | if (DomainValue *dv = LiveRegs[rx].Value) { |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 320 | if (dv->isCollapsed()) |
| 321 | dv->addDomain(domain); |
Jakob Stoklund Olesen | 41051a0 | 2010-04-06 19:48:56 +0000 | [diff] [blame] | 322 | else if (dv->hasDomain(domain)) |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 323 | collapse(dv, domain); |
Jakob Stoklund Olesen | 41051a0 | 2010-04-06 19:48:56 +0000 | [diff] [blame] | 324 | else { |
Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 325 | // This is an incompatible open DomainValue. Collapse it to whatever and |
| 326 | // force the new value into domain. This costs a domain crossing. |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 327 | collapse(dv, dv->getFirstDomain()); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 328 | assert(LiveRegs[rx].Value && "Not live after collapse?"); |
| 329 | LiveRegs[rx].Value->addDomain(domain); |
Jakob Stoklund Olesen | 41051a0 | 2010-04-06 19:48:56 +0000 | [diff] [blame] | 330 | } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 331 | } else { |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 332 | // Set up basic collapsed DomainValue. |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 333 | setLiveReg(rx, alloc(domain)); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | /// Collapse open DomainValue into given domain. If there are multiple |
| 338 | /// registers using dv, they each get a unique collapsed DomainValue. |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 339 | void ExeDepsFix::collapse(DomainValue *dv, unsigned domain) { |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 340 | assert(dv->hasDomain(domain) && "Cannot collapse"); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 341 | |
| 342 | // Collapse all the instructions. |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 343 | while (!dv->Instrs.empty()) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 344 | TII->setExecutionDomain(*dv->Instrs.pop_back_val(), domain); |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 345 | dv->setSingleDomain(domain); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 346 | |
| 347 | // If there are multiple users, give them new, unique DomainValues. |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 348 | if (LiveRegs && dv->Refs > 1) |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 349 | for (unsigned rx = 0; rx != NumRegs; ++rx) |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 350 | if (LiveRegs[rx].Value == dv) |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 351 | setLiveReg(rx, alloc(domain)); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Sanjay Patel | 4297c3f | 2015-03-15 18:16:04 +0000 | [diff] [blame] | 354 | /// All instructions and registers in B are moved to A, and B is released. |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 355 | bool ExeDepsFix::merge(DomainValue *A, DomainValue *B) { |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 356 | assert(!A->isCollapsed() && "Cannot merge into collapsed"); |
| 357 | assert(!B->isCollapsed() && "Cannot merge from collapsed"); |
Jakob Stoklund Olesen | 58ca0a6 | 2010-03-31 20:05:12 +0000 | [diff] [blame] | 358 | if (A == B) |
Jakob Stoklund Olesen | 4cd5866 | 2010-03-31 17:13:16 +0000 | [diff] [blame] | 359 | return true; |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 360 | // Restrict to the domains that A and B have in common. |
| 361 | unsigned common = A->getCommonDomains(B->AvailableDomains); |
| 362 | if (!common) |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 363 | return false; |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 364 | A->AvailableDomains = common; |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 365 | A->Instrs.append(B->Instrs.begin(), B->Instrs.end()); |
Jakob Stoklund Olesen | 1205881 | 2011-11-08 20:57:04 +0000 | [diff] [blame] | 366 | |
| 367 | // Clear the old DomainValue so we won't try to swizzle instructions twice. |
Jakob Stoklund Olesen | b7e44a3 | 2011-11-08 23:26:00 +0000 | [diff] [blame] | 368 | B->clear(); |
Jakob Stoklund Olesen | 53ec977 | 2011-11-09 00:06:18 +0000 | [diff] [blame] | 369 | // All uses of B are referred to A. |
| 370 | B->Next = retain(A); |
Jakob Stoklund Olesen | 1205881 | 2011-11-08 20:57:04 +0000 | [diff] [blame] | 371 | |
Michael Ilseman | addddc4 | 2014-12-15 18:48:43 +0000 | [diff] [blame] | 372 | for (unsigned rx = 0; rx != NumRegs; ++rx) { |
| 373 | assert(LiveRegs && "no space allocated for live registers"); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 374 | if (LiveRegs[rx].Value == B) |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 375 | setLiveReg(rx, A); |
Michael Ilseman | addddc4 | 2014-12-15 18:48:43 +0000 | [diff] [blame] | 376 | } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 377 | return true; |
| 378 | } |
| 379 | |
Sanjay Patel | 4297c3f | 2015-03-15 18:16:04 +0000 | [diff] [blame] | 380 | /// Set up LiveRegs by merging predecessor live-out values. |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 381 | void ExeDepsFix::enterBasicBlock(MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 382 | // Reset instruction counter in each basic block. |
| 383 | CurInstr = 0; |
| 384 | |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 385 | // Set up UndefReads to track undefined register reads. |
| 386 | UndefReads.clear(); |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 387 | LiveRegSet.clear(); |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 388 | |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 389 | // Set up LiveRegs to represent registers entering MBB. |
| 390 | if (!LiveRegs) |
| 391 | LiveRegs = new LiveReg[NumRegs]; |
| 392 | |
| 393 | // Default values are 'nothing happened a long time ago'. |
| 394 | for (unsigned rx = 0; rx != NumRegs; ++rx) { |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 395 | LiveRegs[rx].Value = nullptr; |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 396 | LiveRegs[rx].Def = -(1 << 20); |
| 397 | } |
| 398 | |
| 399 | // This is the entry block. |
| 400 | if (MBB->pred_empty()) { |
Matthias Braun | d9da162 | 2015-09-09 18:08:03 +0000 | [diff] [blame] | 401 | for (const auto &LI : MBB->liveins()) { |
| 402 | for (int rx : regIndices(LI.PhysReg)) { |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 403 | // Treat function live-ins as if they were defined just before the first |
| 404 | // instruction. Usually, function arguments are set up immediately |
| 405 | // before the call. |
| 406 | LiveRegs[rx].Def = -1; |
| 407 | } |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 408 | } |
| 409 | DEBUG(dbgs() << "BB#" << MBB->getNumber() << ": entry\n"); |
| 410 | return; |
| 411 | } |
Jakob Stoklund Olesen | 3dc89c9 | 2011-11-09 01:06:56 +0000 | [diff] [blame] | 412 | |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 413 | // Try to coalesce live-out registers from predecessors. |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 414 | for (MachineBasicBlock::const_pred_iterator pi = MBB->pred_begin(), |
| 415 | pe = MBB->pred_end(); pi != pe; ++pi) { |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 416 | auto fi = MBBInfos.find(*pi); |
| 417 | assert(fi != MBBInfos.end() && |
| 418 | "Should have pre-allocated MBBInfos for all MBBs"); |
| 419 | LiveReg *Incoming = fi->second.OutRegs; |
| 420 | // Incoming is null if this is a backedge from a BB |
| 421 | // we haven't processed yet |
| 422 | if (Incoming == nullptr) { |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 423 | continue; |
| 424 | } |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 425 | |
| 426 | for (unsigned rx = 0; rx != NumRegs; ++rx) { |
| 427 | // Use the most recent predecessor def for each register. |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 428 | LiveRegs[rx].Def = std::max(LiveRegs[rx].Def, Incoming[rx].Def); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 429 | |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 430 | DomainValue *pdv = resolve(Incoming[rx].Value); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 431 | if (!pdv) |
Jakob Stoklund Olesen | 3dc89c9 | 2011-11-09 01:06:56 +0000 | [diff] [blame] | 432 | continue; |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 433 | if (!LiveRegs[rx].Value) { |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 434 | setLiveReg(rx, pdv); |
Chris Lattner | 503a0ef | 2010-03-31 20:32:51 +0000 | [diff] [blame] | 435 | continue; |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 436 | } |
Chris Lattner | 503a0ef | 2010-03-31 20:32:51 +0000 | [diff] [blame] | 437 | |
| 438 | // We have a live DomainValue from more than one predecessor. |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 439 | if (LiveRegs[rx].Value->isCollapsed()) { |
Eric Christopher | 650c8f2 | 2014-05-20 17:11:11 +0000 | [diff] [blame] | 440 | // We are already collapsed, but predecessor is not. Force it. |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 441 | unsigned Domain = LiveRegs[rx].Value->getFirstDomain(); |
| 442 | if (!pdv->isCollapsed() && pdv->hasDomain(Domain)) |
| 443 | collapse(pdv, Domain); |
Chris Lattner | 503a0ef | 2010-03-31 20:32:51 +0000 | [diff] [blame] | 444 | continue; |
| 445 | } |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 446 | |
Chris Lattner | 503a0ef | 2010-03-31 20:32:51 +0000 | [diff] [blame] | 447 | // Currently open, merge in predecessor. |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 448 | if (!pdv->isCollapsed()) |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 449 | merge(LiveRegs[rx].Value, pdv); |
Chris Lattner | 503a0ef | 2010-03-31 20:32:51 +0000 | [diff] [blame] | 450 | else |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 451 | force(rx, pdv->getFirstDomain()); |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 454 | DEBUG( |
| 455 | dbgs() << "BB#" << MBB->getNumber() |
| 456 | << (!isBlockDone(MBB) ? ": incomplete\n" : ": all preds known\n")); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Jakob Stoklund Olesen | 736cf46 | 2011-11-07 21:40:27 +0000 | [diff] [blame] | 459 | void ExeDepsFix::leaveBasicBlock(MachineBasicBlock *MBB) { |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 460 | assert(LiveRegs && "Must enter basic block first."); |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 461 | LiveReg *OldOutRegs = MBBInfos[MBB].OutRegs; |
| 462 | // Save register clearances at end of MBB - used by enterBasicBlock(). |
| 463 | MBBInfos[MBB].OutRegs = LiveRegs; |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 464 | |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 465 | // While processing the basic block, we kept `Def` relative to the start |
| 466 | // of the basic block for convenience. However, future use of this information |
| 467 | // only cares about the clearance from the end of the block, so adjust |
| 468 | // everything to be relative to the end of the basic block. |
| 469 | for (unsigned i = 0, e = NumRegs; i != e; ++i) |
| 470 | LiveRegs[i].Def -= CurInstr; |
| 471 | if (OldOutRegs) { |
| 472 | // This must be the second pass. |
Jakob Stoklund Olesen | 3dc89c9 | 2011-11-09 01:06:56 +0000 | [diff] [blame] | 473 | // Release all the DomainValues instead of keeping them. |
| 474 | for (unsigned i = 0, e = NumRegs; i != e; ++i) |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 475 | release(OldOutRegs[i].Value); |
| 476 | delete[] OldOutRegs; |
Jakob Stoklund Olesen | 3dc89c9 | 2011-11-09 01:06:56 +0000 | [diff] [blame] | 477 | } |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 478 | LiveRegs = nullptr; |
Jakob Stoklund Olesen | 736cf46 | 2011-11-07 21:40:27 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 481 | bool ExeDepsFix::visitInstr(MachineInstr *MI) { |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 482 | // Update instructions with explicit execution domains. |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 483 | std::pair<uint16_t, uint16_t> DomP = TII->getExecutionDomain(*MI); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 484 | if (DomP.first) { |
| 485 | if (DomP.second) |
| 486 | visitSoftInstr(MI, DomP.second); |
Jakob Stoklund Olesen | 736cf46 | 2011-11-07 21:40:27 +0000 | [diff] [blame] | 487 | else |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 488 | visitHardInstr(MI, DomP.first); |
| 489 | } |
| 490 | |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 491 | return !DomP.first; |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Marina Yatsina | 88f0c31 | 2016-08-11 07:32:08 +0000 | [diff] [blame] | 494 | /// \brief Helps avoid false dependencies on undef registers by updating the |
| 495 | /// machine instructions' undef operand to use a register that the instruction |
| 496 | /// is truly dependent on, or use a register with clearance higher than Pref. |
| 497 | void ExeDepsFix::pickBestRegisterForUndef(MachineInstr *MI, unsigned OpIdx, |
| 498 | unsigned Pref) { |
| 499 | MachineOperand &MO = MI->getOperand(OpIdx); |
| 500 | assert(MO.isUndef() && "Expected undef machine operand"); |
| 501 | |
| 502 | unsigned OriginalReg = MO.getReg(); |
| 503 | |
| 504 | // Update only undef operands that are mapped to one register. |
| 505 | if (AliasMap[OriginalReg].size() != 1) |
| 506 | return; |
| 507 | |
| 508 | // Get the undef operand's register class |
| 509 | const TargetRegisterClass *OpRC = |
| 510 | TII->getRegClass(MI->getDesc(), OpIdx, TRI, *MF); |
| 511 | |
| 512 | // If the instruction has a true dependency, we can hide the false depdency |
| 513 | // behind it. |
| 514 | for (MachineOperand &CurrMO : MI->operands()) { |
| 515 | if (!CurrMO.isReg() || CurrMO.isDef() || CurrMO.isUndef() || |
| 516 | !OpRC->contains(CurrMO.getReg())) |
| 517 | continue; |
| 518 | // We found a true dependency - replace the undef register with the true |
| 519 | // dependency. |
| 520 | MO.setReg(CurrMO.getReg()); |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | // Go over all registers in the register class and find the register with |
| 525 | // max clearance or clearance higher than Pref. |
| 526 | unsigned MaxClearance = 0; |
| 527 | unsigned MaxClearanceReg = OriginalReg; |
Marina Yatsina | 53ce3f9 | 2016-08-17 19:07:40 +0000 | [diff] [blame] | 528 | ArrayRef<MCPhysReg> Order = RegClassInfo.getOrder(OpRC); |
| 529 | for (auto Reg : Order) { |
Marina Yatsina | 4b22642 | 2016-08-17 11:40:21 +0000 | [diff] [blame] | 530 | assert(AliasMap[Reg].size() == 1 && |
| 531 | "Reg is expected to be mapped to a single index"); |
| 532 | int RCrx = *regIndices(Reg).begin(); |
| 533 | unsigned Clearance = CurInstr - LiveRegs[RCrx].Def; |
Marina Yatsina | 88f0c31 | 2016-08-11 07:32:08 +0000 | [diff] [blame] | 534 | if (Clearance <= MaxClearance) |
| 535 | continue; |
| 536 | MaxClearance = Clearance; |
Marina Yatsina | 4b22642 | 2016-08-17 11:40:21 +0000 | [diff] [blame] | 537 | MaxClearanceReg = Reg; |
Marina Yatsina | 88f0c31 | 2016-08-11 07:32:08 +0000 | [diff] [blame] | 538 | |
| 539 | if (MaxClearance > Pref) |
| 540 | break; |
| 541 | } |
| 542 | |
| 543 | // Update the operand if we found a register with better clearance. |
| 544 | if (MaxClearanceReg != OriginalReg) |
| 545 | MO.setReg(MaxClearanceReg); |
| 546 | } |
| 547 | |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 548 | /// \brief Return true to if it makes sense to break dependence on a partial def |
| 549 | /// or undef use. |
| 550 | bool ExeDepsFix::shouldBreakDependence(MachineInstr *MI, unsigned OpIdx, |
| 551 | unsigned Pref) { |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 552 | unsigned reg = MI->getOperand(OpIdx).getReg(); |
Matthias Braun | 046318b | 2015-03-06 18:56:20 +0000 | [diff] [blame] | 553 | for (int rx : regIndices(reg)) { |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 554 | unsigned Clearance = CurInstr - LiveRegs[rx].Def; |
| 555 | DEBUG(dbgs() << "Clearance: " << Clearance << ", want " << Pref); |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 556 | |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 557 | if (Pref > Clearance) { |
| 558 | DEBUG(dbgs() << ": Break dependency.\n"); |
| 559 | continue; |
| 560 | } |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 561 | DEBUG(dbgs() << ": OK .\n"); |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 562 | return false; |
| 563 | } |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 564 | return true; |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 567 | // Update def-ages for registers defined by MI. |
| 568 | // If Kill is set, also kill off DomainValues clobbered by the defs. |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 569 | // |
| 570 | // Also break dependencies on partial defs and undef uses. |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 571 | void ExeDepsFix::processDefs(MachineInstr *MI, bool breakDependency, |
| 572 | bool Kill) { |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 573 | assert(!MI->isDebugValue() && "Won't process debug values"); |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 574 | |
| 575 | // Break dependence on undef uses. Do this before updating LiveRegs below. |
| 576 | unsigned OpNum; |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 577 | if (breakDependency) { |
| 578 | unsigned Pref = TII->getUndefRegClearance(*MI, OpNum, TRI); |
| 579 | if (Pref) { |
| 580 | pickBestRegisterForUndef(MI, OpNum, Pref); |
| 581 | if (shouldBreakDependence(MI, OpNum, Pref)) |
| 582 | UndefReads.push_back(std::make_pair(MI, OpNum)); |
| 583 | } |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 584 | } |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 585 | const MCInstrDesc &MCID = MI->getDesc(); |
| 586 | for (unsigned i = 0, |
Evan Cheng | 7f8e563 | 2011-12-07 07:15:52 +0000 | [diff] [blame] | 587 | e = MI->isVariadic() ? MI->getNumOperands() : MCID.getNumDefs(); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 588 | i != e; ++i) { |
| 589 | MachineOperand &MO = MI->getOperand(i); |
| 590 | if (!MO.isReg()) |
| 591 | continue; |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 592 | if (MO.isUse()) |
| 593 | continue; |
Matthias Braun | 046318b | 2015-03-06 18:56:20 +0000 | [diff] [blame] | 594 | for (int rx : regIndices(MO.getReg())) { |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 595 | // This instruction explicitly defines rx. |
| 596 | DEBUG(dbgs() << TRI->getName(RC->getRegister(rx)) << ":\t" << CurInstr |
| 597 | << '\t' << *MI); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 598 | |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 599 | if (breakDependency) { |
| 600 | // Check clearance before partial register updates. |
| 601 | // Call breakDependence before setting LiveRegs[rx].Def. |
| 602 | unsigned Pref = TII->getPartialRegUpdateClearance(*MI, i, TRI); |
| 603 | if (Pref && shouldBreakDependence(MI, i, Pref)) |
| 604 | TII->breakPartialRegDependency(*MI, i, TRI); |
| 605 | } |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 606 | |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 607 | // How many instructions since rx was last written? |
| 608 | LiveRegs[rx].Def = CurInstr; |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 609 | |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 610 | // Kill off domains redefined by generic instructions. |
| 611 | if (Kill) |
| 612 | kill(rx); |
| 613 | } |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 614 | } |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 615 | ++CurInstr; |
Jakob Stoklund Olesen | 736cf46 | 2011-11-07 21:40:27 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 618 | /// \break Break false dependencies on undefined register reads. |
| 619 | /// |
| 620 | /// Walk the block backward computing precise liveness. This is expensive, so we |
| 621 | /// only do it on demand. Note that the occurrence of undefined register reads |
| 622 | /// that should be broken is very rare, but when they occur we may have many in |
| 623 | /// a single block. |
| 624 | void ExeDepsFix::processUndefReads(MachineBasicBlock *MBB) { |
| 625 | if (UndefReads.empty()) |
| 626 | return; |
| 627 | |
| 628 | // Collect this block's live out register units. |
Matthias Braun | 0c989a8 | 2016-12-08 00:15:51 +0000 | [diff] [blame] | 629 | LiveRegSet.init(*TRI); |
Matthias Braun | 24f26e6 | 2016-05-03 00:08:46 +0000 | [diff] [blame] | 630 | // We do not need to care about pristine registers as they are just preserved |
| 631 | // but not actually used in the function. |
Matthias Braun | d1aabb2 | 2016-05-03 00:24:32 +0000 | [diff] [blame] | 632 | LiveRegSet.addLiveOutsNoPristines(*MBB); |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 633 | |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 634 | MachineInstr *UndefMI = UndefReads.back().first; |
| 635 | unsigned OpIdx = UndefReads.back().second; |
| 636 | |
Pete Cooper | 7679afd | 2015-07-24 21:13:43 +0000 | [diff] [blame] | 637 | for (MachineInstr &I : make_range(MBB->rbegin(), MBB->rend())) { |
Andrew Trick | 60cf0ad | 2013-12-13 22:23:54 +0000 | [diff] [blame] | 638 | // Update liveness, including the current instruction's defs. |
Pete Cooper | 7679afd | 2015-07-24 21:13:43 +0000 | [diff] [blame] | 639 | LiveRegSet.stepBackward(I); |
Andrew Trick | 3a99693 | 2013-10-15 03:39:43 +0000 | [diff] [blame] | 640 | |
Pete Cooper | 7679afd | 2015-07-24 21:13:43 +0000 | [diff] [blame] | 641 | if (UndefMI == &I) { |
Juergen Ributzka | 310034e | 2013-12-14 06:52:56 +0000 | [diff] [blame] | 642 | if (!LiveRegSet.contains(UndefMI->getOperand(OpIdx).getReg())) |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 643 | TII->breakPartialRegDependency(*UndefMI, OpIdx, TRI); |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 644 | |
| 645 | UndefReads.pop_back(); |
| 646 | if (UndefReads.empty()) |
| 647 | return; |
| 648 | |
| 649 | UndefMI = UndefReads.back().first; |
| 650 | OpIdx = UndefReads.back().second; |
| 651 | } |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 652 | } |
| 653 | } |
| 654 | |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 655 | // A hard instruction only works in one domain. All input registers will be |
| 656 | // forced into that domain. |
Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 657 | void ExeDepsFix::visitHardInstr(MachineInstr *mi, unsigned domain) { |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 658 | // Collapse all uses. |
| 659 | for (unsigned i = mi->getDesc().getNumDefs(), |
| 660 | e = mi->getDesc().getNumOperands(); i != e; ++i) { |
| 661 | MachineOperand &mo = mi->getOperand(i); |
| 662 | if (!mo.isReg()) continue; |
Matthias Braun | 046318b | 2015-03-06 18:56:20 +0000 | [diff] [blame] | 663 | for (int rx : regIndices(mo.getReg())) { |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 664 | force(rx, domain); |
| 665 | } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | // Kill all defs and force them. |
| 669 | for (unsigned i = 0, e = mi->getDesc().getNumDefs(); i != e; ++i) { |
| 670 | MachineOperand &mo = mi->getOperand(i); |
| 671 | if (!mo.isReg()) continue; |
Matthias Braun | 046318b | 2015-03-06 18:56:20 +0000 | [diff] [blame] | 672 | for (int rx : regIndices(mo.getReg())) { |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 673 | kill(rx); |
| 674 | force(rx, domain); |
| 675 | } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
| 679 | // A soft instruction can be changed to work in other domains given by mask. |
Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 680 | void ExeDepsFix::visitSoftInstr(MachineInstr *mi, unsigned mask) { |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 681 | // Bitmask of available domains for this instruction after taking collapsed |
| 682 | // operands into account. |
| 683 | unsigned available = mask; |
| 684 | |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 685 | // Scan the explicit use operands for incoming domains. |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 686 | SmallVector<int, 4> used; |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 687 | if (LiveRegs) |
| 688 | for (unsigned i = mi->getDesc().getNumDefs(), |
| 689 | e = mi->getDesc().getNumOperands(); i != e; ++i) { |
Chris Lattner | 503a0ef | 2010-03-31 20:32:51 +0000 | [diff] [blame] | 690 | MachineOperand &mo = mi->getOperand(i); |
| 691 | if (!mo.isReg()) continue; |
Matthias Braun | 046318b | 2015-03-06 18:56:20 +0000 | [diff] [blame] | 692 | for (int rx : regIndices(mo.getReg())) { |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 693 | DomainValue *dv = LiveRegs[rx].Value; |
| 694 | if (dv == nullptr) |
| 695 | continue; |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 696 | // Bitmask of domains that dv and available have in common. |
| 697 | unsigned common = dv->getCommonDomains(available); |
Chris Lattner | 503a0ef | 2010-03-31 20:32:51 +0000 | [diff] [blame] | 698 | // Is it possible to use this collapsed register for free? |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 699 | if (dv->isCollapsed()) { |
| 700 | // Restrict available domains to the ones in common with the operand. |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 701 | // If there are no common domains, we must pay the cross-domain |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 702 | // penalty for this operand. |
| 703 | if (common) available = common; |
| 704 | } else if (common) |
| 705 | // Open DomainValue is compatible, save it for merging. |
Chris Lattner | 503a0ef | 2010-03-31 20:32:51 +0000 | [diff] [blame] | 706 | used.push_back(rx); |
| 707 | else |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 708 | // Open DomainValue is not compatible with instruction. It is useless |
| 709 | // now. |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 710 | kill(rx); |
Chris Lattner | 503a0ef | 2010-03-31 20:32:51 +0000 | [diff] [blame] | 711 | } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 712 | } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 713 | |
| 714 | // If the collapsed operands force a single domain, propagate the collapse. |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 715 | if (isPowerOf2_32(available)) { |
Michael J. Spencer | df1ecbd7 | 2013-05-24 22:23:49 +0000 | [diff] [blame] | 716 | unsigned domain = countTrailingZeros(available); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 717 | TII->setExecutionDomain(*mi, domain); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 718 | visitHardInstr(mi, domain); |
| 719 | return; |
| 720 | } |
| 721 | |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 722 | // Kill off any remaining uses that don't match available, and build a list of |
| 723 | // incoming DomainValues that we want to merge. |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 724 | SmallVector<LiveReg, 4> Regs; |
Craig Topper | c446b1f | 2017-02-24 06:38:24 +0000 | [diff] [blame^] | 725 | for (int rx : used) { |
Michael Ilseman | addddc4 | 2014-12-15 18:48:43 +0000 | [diff] [blame] | 726 | assert(LiveRegs && "no space allocated for live registers"); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 727 | const LiveReg &LR = LiveRegs[rx]; |
Jakob Stoklund Olesen | 3b9af40 | 2010-03-30 20:04:01 +0000 | [diff] [blame] | 728 | // This useless DomainValue could have been missed above. |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 729 | if (!LR.Value->getCommonDomains(available)) { |
| 730 | kill(rx); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 731 | continue; |
| 732 | } |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 733 | // Sorted insertion. |
| 734 | bool Inserted = false; |
Craig Topper | e1c1d36 | 2013-07-03 05:11:49 +0000 | [diff] [blame] | 735 | for (SmallVectorImpl<LiveReg>::iterator i = Regs.begin(), e = Regs.end(); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 736 | i != e && !Inserted; ++i) { |
| 737 | if (LR.Def < i->Def) { |
| 738 | Inserted = true; |
| 739 | Regs.insert(i, LR); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 740 | } |
| 741 | } |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 742 | if (!Inserted) |
| 743 | Regs.push_back(LR); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 746 | // doms are now sorted in order of appearance. Try to merge them all, giving |
| 747 | // priority to the latest ones. |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 748 | DomainValue *dv = nullptr; |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 749 | while (!Regs.empty()) { |
Chris Lattner | 503a0ef | 2010-03-31 20:32:51 +0000 | [diff] [blame] | 750 | if (!dv) { |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 751 | dv = Regs.pop_back_val().Value; |
Jakob Stoklund Olesen | 0284541 | 2011-11-23 04:03:08 +0000 | [diff] [blame] | 752 | // Force the first dv to match the current instruction. |
| 753 | dv->AvailableDomains = dv->getCommonDomains(available); |
| 754 | assert(dv->AvailableDomains && "Domain should have been filtered"); |
Chris Lattner | 503a0ef | 2010-03-31 20:32:51 +0000 | [diff] [blame] | 755 | continue; |
| 756 | } |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 757 | |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 758 | DomainValue *Latest = Regs.pop_back_val().Value; |
| 759 | // Skip already merged values. |
| 760 | if (Latest == dv || Latest->Next) |
| 761 | continue; |
| 762 | if (merge(dv, Latest)) |
| 763 | continue; |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 764 | |
Jakob Stoklund Olesen | d03ac95 | 2010-04-04 21:27:26 +0000 | [diff] [blame] | 765 | // If latest didn't merge, it is useless now. Kill all registers using it. |
Michael Ilseman | addddc4 | 2014-12-15 18:48:43 +0000 | [diff] [blame] | 766 | for (int i : used) { |
| 767 | assert(LiveRegs && "no space allocated for live registers"); |
| 768 | if (LiveRegs[i].Value == Latest) |
| 769 | kill(i); |
| 770 | } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | // dv is the DomainValue we are going to use for this instruction. |
Jakob Stoklund Olesen | 0284541 | 2011-11-23 04:03:08 +0000 | [diff] [blame] | 774 | if (!dv) { |
Jakob Stoklund Olesen | 9e338bb | 2011-11-08 21:57:47 +0000 | [diff] [blame] | 775 | dv = alloc(); |
Jakob Stoklund Olesen | 0284541 | 2011-11-23 04:03:08 +0000 | [diff] [blame] | 776 | dv->AvailableDomains = available; |
| 777 | } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 778 | dv->Instrs.push_back(mi); |
| 779 | |
Silviu Baranga | 3c31499 | 2012-10-03 08:29:36 +0000 | [diff] [blame] | 780 | // Finally set all defs and non-collapsed uses to dv. We must iterate through |
| 781 | // all the operators, including imp-def ones. |
| 782 | for (MachineInstr::mop_iterator ii = mi->operands_begin(), |
| 783 | ee = mi->operands_end(); |
| 784 | ii != ee; ++ii) { |
| 785 | MachineOperand &mo = *ii; |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 786 | if (!mo.isReg()) continue; |
Matthias Braun | 046318b | 2015-03-06 18:56:20 +0000 | [diff] [blame] | 787 | for (int rx : regIndices(mo.getReg())) { |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 788 | if (!LiveRegs[rx].Value || (mo.isDef() && LiveRegs[rx].Value != dv)) { |
| 789 | kill(rx); |
| 790 | setLiveReg(rx, dv); |
| 791 | } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | } |
| 795 | |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 796 | void ExeDepsFix::processBasicBlock(MachineBasicBlock *MBB, bool PrimaryPass) { |
| 797 | enterBasicBlock(MBB); |
| 798 | // If this block is not done, it makes little sense to make any decisions |
| 799 | // based on clearance information. We need to make a second pass anyway, |
| 800 | // and by then we'll have better information, so we can avoid doing the work |
| 801 | // to try and break dependencies now. |
| 802 | bool breakDependency = isBlockDone(MBB); |
| 803 | for (MachineInstr &MI : *MBB) { |
| 804 | if (!MI.isDebugValue()) { |
| 805 | bool Kill = false; |
| 806 | if (PrimaryPass) |
| 807 | Kill = visitInstr(&MI); |
| 808 | processDefs(&MI, breakDependency, Kill); |
| 809 | } |
| 810 | } |
| 811 | if (breakDependency) |
| 812 | processUndefReads(MBB); |
| 813 | leaveBasicBlock(MBB); |
| 814 | } |
| 815 | |
| 816 | bool ExeDepsFix::isBlockDone(MachineBasicBlock *MBB) { |
| 817 | return MBBInfos[MBB].PrimaryCompleted && |
| 818 | MBBInfos[MBB].IncomingCompleted == MBBInfos[MBB].PrimaryIncoming && |
| 819 | MBBInfos[MBB].IncomingProcessed == MBB->pred_size(); |
| 820 | } |
| 821 | |
| 822 | void ExeDepsFix::updateSuccessors(MachineBasicBlock *MBB, bool Primary) { |
| 823 | bool Done = isBlockDone(MBB); |
| 824 | for (auto *Succ : MBB->successors()) { |
| 825 | if (!isBlockDone(Succ)) { |
| 826 | if (Primary) { |
| 827 | MBBInfos[Succ].IncomingProcessed++; |
| 828 | } |
| 829 | if (Done) { |
| 830 | MBBInfos[Succ].IncomingCompleted++; |
| 831 | } |
| 832 | if (isBlockDone(Succ)) { |
| 833 | // Perform secondary processing for this successor. See the big comment |
| 834 | // in runOnMachineFunction, for an explanation of the iteration order. |
| 835 | processBasicBlock(Succ, false); |
| 836 | updateSuccessors(Succ, false); |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | |
Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 842 | bool ExeDepsFix::runOnMachineFunction(MachineFunction &mf) { |
Andrew Kaylor | 50271f7 | 2016-05-03 22:32:30 +0000 | [diff] [blame] | 843 | if (skipFunction(*mf.getFunction())) |
| 844 | return false; |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 845 | MF = &mf; |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 846 | TII = MF->getSubtarget().getInstrInfo(); |
| 847 | TRI = MF->getSubtarget().getRegisterInfo(); |
Marina Yatsina | 53ce3f9 | 2016-08-17 19:07:40 +0000 | [diff] [blame] | 848 | RegClassInfo.runOnMachineFunction(mf); |
Craig Topper | c0196b1 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 849 | LiveRegs = nullptr; |
Jakob Stoklund Olesen | 30c8112 | 2011-09-27 23:50:46 +0000 | [diff] [blame] | 850 | assert(NumRegs == RC->getNumRegs() && "Bad regclass"); |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 851 | |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 852 | DEBUG(dbgs() << "********** FIX EXECUTION DEPENDENCIES: " |
Craig Topper | cf0444b | 2014-11-17 05:50:14 +0000 | [diff] [blame] | 853 | << TRI->getRegClassName(RC) << " **********\n"); |
Jakob Stoklund Olesen | 543bef6 | 2011-11-15 01:15:25 +0000 | [diff] [blame] | 854 | |
Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 855 | // If no relevant registers are used in the function, we can skip it |
| 856 | // completely. |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 857 | bool anyregs = false; |
Matthias Braun | 9912bb8 | 2015-07-14 17:52:07 +0000 | [diff] [blame] | 858 | const MachineRegisterInfo &MRI = mf.getRegInfo(); |
Matthias Braun | d55bcf2 | 2015-08-18 18:54:27 +0000 | [diff] [blame] | 859 | for (unsigned Reg : *RC) { |
| 860 | if (MRI.isPhysRegUsed(Reg)) { |
| 861 | anyregs = true; |
| 862 | break; |
| 863 | } |
| 864 | } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 865 | if (!anyregs) return false; |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 866 | |
Jakob Stoklund Olesen | 30c8112 | 2011-09-27 23:50:46 +0000 | [diff] [blame] | 867 | // Initialize the AliasMap on the first use. |
| 868 | if (AliasMap.empty()) { |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 869 | // Given a PhysReg, AliasMap[PhysReg] returns a list of indices into RC and |
| 870 | // therefore the LiveRegs array. |
| 871 | AliasMap.resize(TRI->getNumRegs()); |
Jakob Stoklund Olesen | 30c8112 | 2011-09-27 23:50:46 +0000 | [diff] [blame] | 872 | for (unsigned i = 0, e = RC->getNumRegs(); i != e; ++i) |
Jakob Stoklund Olesen | 54038d7 | 2012-06-01 23:28:30 +0000 | [diff] [blame] | 873 | for (MCRegAliasIterator AI(RC->getRegister(i), TRI, true); |
| 874 | AI.isValid(); ++AI) |
Matthias Braun | 8142efa | 2014-12-17 19:13:47 +0000 | [diff] [blame] | 875 | AliasMap[*AI].push_back(i); |
Jakob Stoklund Olesen | 30c8112 | 2011-09-27 23:50:46 +0000 | [diff] [blame] | 876 | } |
| 877 | |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 878 | // Initialize the MMBInfos |
| 879 | for (auto &MBB : mf) { |
| 880 | MBBInfo InitialInfo; |
| 881 | MBBInfos.insert(std::make_pair(&MBB, InitialInfo)); |
| 882 | } |
| 883 | |
| 884 | /* |
| 885 | * We want to visit every instruction in every basic block in order to update |
| 886 | * it's execution domain or break any false dependencies. However, for the |
| 887 | * dependency breaking, we need to know clearances from all predecessors |
| 888 | * (including any backedges). One way to do so would be to do two complete |
| 889 | * passes over all basic blocks/instructions, the first for recording |
| 890 | * clearances, the second to break the dependencies. However, for functions |
| 891 | * without backedges, or functions with a lot of straight-line code, and |
| 892 | * a small loop, that would be a lot of unnecessary work (since only the |
| 893 | * BBs that are part of the loop require two passes). As an example, |
| 894 | * consider the following loop. |
| 895 | * |
| 896 | * |
| 897 | * PH -> A -> B (xmm<Undef> -> xmm<Def>) -> C -> D -> EXIT |
| 898 | * ^ | |
| 899 | * +----------------------------------+ |
| 900 | * |
| 901 | * The iteration order is as follows: |
| 902 | * Naive: PH A B C D A' B' C' D' |
| 903 | * Optimized: PH A B C A' B' C' D |
| 904 | * |
| 905 | * Note that we avoid processing D twice, because we can entirely process |
| 906 | * the predecessors before getting to D. We call a block that is ready |
| 907 | * for its second round of processing `done` (isBlockDone). Once we finish |
| 908 | * processing some block, we update the counters in MBBInfos and re-process |
| 909 | * any successors that are now done. |
| 910 | */ |
| 911 | |
Duncan P. N. Exon Smith | 8f11e1a | 2015-10-09 16:54:49 +0000 | [diff] [blame] | 912 | MachineBasicBlock *Entry = &*MF->begin(); |
Jakob Stoklund Olesen | 68e197e | 2011-11-07 21:59:29 +0000 | [diff] [blame] | 913 | ReversePostOrderTraversal<MachineBasicBlock*> RPOT(Entry); |
| 914 | for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator |
| 915 | MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) { |
| 916 | MachineBasicBlock *MBB = *MBBI; |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 917 | // N.B: IncomingProcessed and IncomingCompleted were already updated while |
| 918 | // processing this block's predecessors. |
| 919 | MBBInfos[MBB].PrimaryCompleted = true; |
| 920 | MBBInfos[MBB].PrimaryIncoming = MBBInfos[MBB].IncomingProcessed; |
| 921 | processBasicBlock(MBB, true); |
| 922 | updateSuccessors(MBB, true); |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 923 | } |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 924 | |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 925 | // We need to go through again and finalize any blocks that are not done yet. |
| 926 | // This is possible if blocks have dead predecessors, so we didn't visit them |
| 927 | // above. |
| 928 | for (ReversePostOrderTraversal<MachineBasicBlock *>::rpo_iterator |
| 929 | MBBI = RPOT.begin(), |
| 930 | MBBE = RPOT.end(); |
| 931 | MBBI != MBBE; ++MBBI) { |
| 932 | MachineBasicBlock *MBB = *MBBI; |
| 933 | if (!isBlockDone(MBB)) { |
| 934 | processBasicBlock(MBB, false); |
| 935 | // Don't update successors here. We'll get to them anyway through this |
| 936 | // loop. |
| 937 | } |
Jakob Stoklund Olesen | 3dc89c9 | 2011-11-09 01:06:56 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Jakob Stoklund Olesen | a70e941 | 2011-11-07 23:08:21 +0000 | [diff] [blame] | 940 | // Clear the LiveOuts vectors and collapse any remaining DomainValues. |
| 941 | for (ReversePostOrderTraversal<MachineBasicBlock*>::rpo_iterator |
| 942 | MBBI = RPOT.begin(), MBBE = RPOT.end(); MBBI != MBBE; ++MBBI) { |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 943 | auto FI = MBBInfos.find(*MBBI); |
| 944 | if (FI == MBBInfos.end() || !FI->second.OutRegs) |
Jakob Stoklund Olesen | a70e941 | 2011-11-07 23:08:21 +0000 | [diff] [blame] | 945 | continue; |
Jakob Stoklund Olesen | a70e941 | 2011-11-07 23:08:21 +0000 | [diff] [blame] | 946 | for (unsigned i = 0, e = NumRegs; i != e; ++i) |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 947 | if (FI->second.OutRegs[i].Value) |
| 948 | release(FI->second.OutRegs[i].Value); |
| 949 | delete[] FI->second.OutRegs; |
Jakob Stoklund Olesen | a70e941 | 2011-11-07 23:08:21 +0000 | [diff] [blame] | 950 | } |
Keno Fischer | 578cf7a | 2017-01-30 23:37:03 +0000 | [diff] [blame] | 951 | MBBInfos.clear(); |
Andrew Trick | b6d56be | 2013-10-14 22:19:03 +0000 | [diff] [blame] | 952 | UndefReads.clear(); |
Jakob Stoklund Olesen | 42caaa4 | 2010-04-04 18:00:21 +0000 | [diff] [blame] | 953 | Avail.clear(); |
| 954 | Allocator.DestroyAll(); |
Jakob Stoklund Olesen | b551aa4 | 2010-03-29 23:24:21 +0000 | [diff] [blame] | 955 | |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 956 | return false; |
| 957 | } |
| 958 | |
Jakob Stoklund Olesen | 30c8112 | 2011-09-27 23:50:46 +0000 | [diff] [blame] | 959 | FunctionPass * |
| 960 | llvm::createExecutionDependencyFixPass(const TargetRegisterClass *RC) { |
Jakob Stoklund Olesen | bd5109f | 2011-09-28 00:01:56 +0000 | [diff] [blame] | 961 | return new ExeDepsFix(RC); |
Jakob Stoklund Olesen | 49e121d | 2010-03-25 17:25:00 +0000 | [diff] [blame] | 962 | } |