James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 1 | //===-- AArch64A57FPLoadBalancing.cpp - Balance FP ops statically on A57---===// |
| 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 | // For best-case performance on Cortex-A57, we should try to use a balanced |
| 10 | // mix of odd and even D-registers when performing a critical sequence of |
| 11 | // independent, non-quadword FP/ASIMD floating-point multiply or |
| 12 | // multiply-accumulate operations. |
| 13 | // |
| 14 | // This pass attempts to detect situations where the register allocation may |
| 15 | // adversely affect this load balancing and to change the registers used so as |
| 16 | // to better utilize the CPU. |
| 17 | // |
| 18 | // Ideally we'd just take each multiply or multiply-accumulate in turn and |
| 19 | // allocate it alternating even or odd registers. However, multiply-accumulates |
| 20 | // are most efficiently performed in the same functional unit as their |
| 21 | // accumulation operand. Therefore this pass tries to find maximal sequences |
| 22 | // ("Chains") of multiply-accumulates linked via their accumulation operand, |
| 23 | // and assign them all the same "color" (oddness/evenness). |
| 24 | // |
| 25 | // This optimization affects S-register and D-register floating point |
| 26 | // multiplies and FMADD/FMAs, as well as vector (floating point only) muls and |
| 27 | // FMADD/FMA. Q register instructions (and 128-bit vector instructions) are |
| 28 | // not affected. |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | #include "AArch64.h" |
| 32 | #include "AArch64InstrInfo.h" |
| 33 | #include "AArch64Subtarget.h" |
| 34 | #include "llvm/ADT/BitVector.h" |
| 35 | #include "llvm/ADT/EquivalenceClasses.h" |
| 36 | #include "llvm/CodeGen/MachineFunction.h" |
| 37 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 38 | #include "llvm/CodeGen/MachineInstr.h" |
| 39 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 40 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 41 | #include "llvm/CodeGen/RegisterClassInfo.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 42 | #include "llvm/CodeGen/RegisterScavenging.h" |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 43 | #include "llvm/Support/CommandLine.h" |
| 44 | #include "llvm/Support/Debug.h" |
| 45 | #include "llvm/Support/raw_ostream.h" |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 46 | using namespace llvm; |
| 47 | |
| 48 | #define DEBUG_TYPE "aarch64-a57-fp-load-balancing" |
| 49 | |
| 50 | // Enforce the algorithm to use the scavenged register even when the original |
| 51 | // destination register is the correct color. Used for testing. |
| 52 | static cl::opt<bool> |
| 53 | TransformAll("aarch64-a57-fp-load-balancing-force-all", |
| 54 | cl::desc("Always modify dest registers regardless of color"), |
| 55 | cl::init(false), cl::Hidden); |
| 56 | |
| 57 | // Never use the balance information obtained from chains - return a specific |
| 58 | // color always. Used for testing. |
| 59 | static cl::opt<unsigned> |
| 60 | OverrideBalance("aarch64-a57-fp-load-balancing-override", |
| 61 | cl::desc("Ignore balance information, always return " |
| 62 | "(1: Even, 2: Odd)."), |
| 63 | cl::init(0), cl::Hidden); |
| 64 | |
| 65 | //===----------------------------------------------------------------------===// |
| 66 | // Helper functions |
| 67 | |
| 68 | // Is the instruction a type of multiply on 64-bit (or 32-bit) FPRs? |
| 69 | static bool isMul(MachineInstr *MI) { |
| 70 | switch (MI->getOpcode()) { |
| 71 | case AArch64::FMULSrr: |
| 72 | case AArch64::FNMULSrr: |
| 73 | case AArch64::FMULDrr: |
| 74 | case AArch64::FNMULDrr: |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 75 | return true; |
| 76 | default: |
| 77 | return false; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // Is the instruction a type of FP multiply-accumulate on 64-bit (or 32-bit) FPRs? |
| 82 | static bool isMla(MachineInstr *MI) { |
| 83 | switch (MI->getOpcode()) { |
| 84 | case AArch64::FMSUBSrrr: |
| 85 | case AArch64::FMADDSrrr: |
| 86 | case AArch64::FNMSUBSrrr: |
| 87 | case AArch64::FNMADDSrrr: |
| 88 | case AArch64::FMSUBDrrr: |
| 89 | case AArch64::FMADDDrrr: |
| 90 | case AArch64::FNMSUBDrrr: |
| 91 | case AArch64::FNMADDDrrr: |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 92 | return true; |
| 93 | default: |
| 94 | return false; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | //===----------------------------------------------------------------------===// |
| 99 | |
| 100 | namespace { |
| 101 | /// A "color", which is either even or odd. Yes, these aren't really colors |
| 102 | /// but the algorithm is conceptually doing two-color graph coloring. |
| 103 | enum class Color { Even, Odd }; |
NAKAMURA Takumi | 08e30fd | 2014-08-08 17:00:59 +0000 | [diff] [blame] | 104 | #ifndef NDEBUG |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 105 | static const char *ColorNames[2] = { "Even", "Odd" }; |
NAKAMURA Takumi | 08e30fd | 2014-08-08 17:00:59 +0000 | [diff] [blame] | 106 | #endif |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 107 | |
| 108 | class Chain; |
| 109 | |
| 110 | class AArch64A57FPLoadBalancing : public MachineFunctionPass { |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 111 | MachineRegisterInfo *MRI; |
| 112 | const TargetRegisterInfo *TRI; |
| 113 | RegisterClassInfo RCI; |
| 114 | |
| 115 | public: |
| 116 | static char ID; |
Chad Rosier | 11d943d | 2015-01-29 22:57:37 +0000 | [diff] [blame] | 117 | explicit AArch64A57FPLoadBalancing() : MachineFunctionPass(ID) { |
| 118 | initializeAArch64A57FPLoadBalancingPass(*PassRegistry::getPassRegistry()); |
| 119 | } |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 120 | |
| 121 | bool runOnMachineFunction(MachineFunction &F) override; |
| 122 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 123 | MachineFunctionProperties getRequiredProperties() const override { |
| 124 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 125 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 128 | StringRef getPassName() const override { |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 129 | return "A57 FP Anti-dependency breaker"; |
| 130 | } |
| 131 | |
| 132 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 133 | AU.setPreservesCFG(); |
| 134 | MachineFunctionPass::getAnalysisUsage(AU); |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | bool runOnBasicBlock(MachineBasicBlock &MBB); |
| 139 | bool colorChainSet(std::vector<Chain*> GV, MachineBasicBlock &MBB, |
| 140 | int &Balance); |
| 141 | bool colorChain(Chain *G, Color C, MachineBasicBlock &MBB); |
| 142 | int scavengeRegister(Chain *G, Color C, MachineBasicBlock &MBB); |
| 143 | void scanInstruction(MachineInstr *MI, unsigned Idx, |
James Molloy | f0de7e5 | 2014-09-12 14:35:17 +0000 | [diff] [blame] | 144 | std::map<unsigned, Chain*> &Active, |
Benjamin Kramer | 76e37aa | 2015-03-13 16:59:29 +0000 | [diff] [blame] | 145 | std::vector<std::unique_ptr<Chain>> &AllChains); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 146 | void maybeKillChain(MachineOperand &MO, unsigned Idx, |
| 147 | std::map<unsigned, Chain*> &RegChains); |
| 148 | Color getColor(unsigned Register); |
| 149 | Chain *getAndEraseNext(Color PreferredColor, std::vector<Chain*> &L); |
| 150 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 151 | } |
Chad Rosier | 11d943d | 2015-01-29 22:57:37 +0000 | [diff] [blame] | 152 | |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 153 | char AArch64A57FPLoadBalancing::ID = 0; |
| 154 | |
Chad Rosier | 11d943d | 2015-01-29 22:57:37 +0000 | [diff] [blame] | 155 | INITIALIZE_PASS_BEGIN(AArch64A57FPLoadBalancing, DEBUG_TYPE, |
| 156 | "AArch64 A57 FP Load-Balancing", false, false) |
| 157 | INITIALIZE_PASS_END(AArch64A57FPLoadBalancing, DEBUG_TYPE, |
| 158 | "AArch64 A57 FP Load-Balancing", false, false) |
| 159 | |
| 160 | namespace { |
Junmo Park | 3ec882f | 2016-01-06 03:41:30 +0000 | [diff] [blame] | 161 | /// A Chain is a sequence of instructions that are linked together by |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 162 | /// an accumulation operand. For example: |
| 163 | /// |
| 164 | /// fmul d0<def>, ? |
| 165 | /// fmla d1<def>, ?, ?, d0<kill> |
| 166 | /// fmla d2<def>, ?, ?, d1<kill> |
| 167 | /// |
| 168 | /// There may be other instructions interleaved in the sequence that |
| 169 | /// do not belong to the chain. These other instructions must not use |
| 170 | /// the "chain" register at any point. |
| 171 | /// |
| 172 | /// We currently only support chains where the "chain" operand is killed |
| 173 | /// at each link in the chain for simplicity. |
| 174 | /// A chain has three important instructions - Start, Last and Kill. |
| 175 | /// * The start instruction is the first instruction in the chain. |
| 176 | /// * Last is the final instruction in the chain. |
| 177 | /// * Kill may or may not be defined. If defined, Kill is the instruction |
| 178 | /// where the outgoing value of the Last instruction is killed. |
| 179 | /// This information is important as if we know the outgoing value is |
| 180 | /// killed with no intervening uses, we can safely change its register. |
| 181 | /// |
| 182 | /// Without a kill instruction, we must assume the outgoing value escapes |
| 183 | /// beyond our model and either must not change its register or must |
| 184 | /// create a fixup FMOV to keep the old register value consistent. |
| 185 | /// |
| 186 | class Chain { |
| 187 | public: |
| 188 | /// The important (marker) instructions. |
| 189 | MachineInstr *StartInst, *LastInst, *KillInst; |
| 190 | /// The index, from the start of the basic block, that each marker |
| 191 | /// appears. These are stored so we can do quick interval tests. |
| 192 | unsigned StartInstIdx, LastInstIdx, KillInstIdx; |
| 193 | /// All instructions in the chain. |
| 194 | std::set<MachineInstr*> Insts; |
| 195 | /// True if KillInst cannot be modified. If this is true, |
| 196 | /// we cannot change LastInst's outgoing register. |
| 197 | /// This will be true for tied values and regmasks. |
| 198 | bool KillIsImmutable; |
| 199 | /// The "color" of LastInst. This will be the preferred chain color, |
| 200 | /// as changing intermediate nodes is easy but changing the last |
| 201 | /// instruction can be more tricky. |
| 202 | Color LastColor; |
| 203 | |
Arnaud A. de Grandmaison | 6afbf2a | 2014-08-29 09:54:11 +0000 | [diff] [blame] | 204 | Chain(MachineInstr *MI, unsigned Idx, Color C) |
| 205 | : StartInst(MI), LastInst(MI), KillInst(nullptr), |
| 206 | StartInstIdx(Idx), LastInstIdx(Idx), KillInstIdx(0), |
| 207 | LastColor(C) { |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 208 | Insts.insert(MI); |
| 209 | } |
| 210 | |
| 211 | /// Add a new instruction into the chain. The instruction's dest operand |
| 212 | /// has the given color. |
| 213 | void add(MachineInstr *MI, unsigned Idx, Color C) { |
| 214 | LastInst = MI; |
| 215 | LastInstIdx = Idx; |
| 216 | LastColor = C; |
Arnaud A. de Grandmaison | 6afbf2a | 2014-08-29 09:54:11 +0000 | [diff] [blame] | 217 | assert((KillInstIdx == 0 || LastInstIdx < KillInstIdx) && |
| 218 | "Chain: broken invariant. A Chain can only be killed after its last " |
| 219 | "def"); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 220 | |
| 221 | Insts.insert(MI); |
| 222 | } |
| 223 | |
| 224 | /// Return true if MI is a member of the chain. |
Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 225 | bool contains(MachineInstr &MI) { return Insts.count(&MI) > 0; } |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 226 | |
| 227 | /// Return the number of instructions in the chain. |
| 228 | unsigned size() const { |
| 229 | return Insts.size(); |
| 230 | } |
| 231 | |
| 232 | /// Inform the chain that its last active register (the dest register of |
| 233 | /// LastInst) is killed by MI with no intervening uses or defs. |
| 234 | void setKill(MachineInstr *MI, unsigned Idx, bool Immutable) { |
| 235 | KillInst = MI; |
| 236 | KillInstIdx = Idx; |
| 237 | KillIsImmutable = Immutable; |
Arnaud A. de Grandmaison | 6afbf2a | 2014-08-29 09:54:11 +0000 | [diff] [blame] | 238 | assert((KillInstIdx == 0 || LastInstIdx < KillInstIdx) && |
| 239 | "Chain: broken invariant. A Chain can only be killed after its last " |
| 240 | "def"); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /// Return the first instruction in the chain. |
| 244 | MachineInstr *getStart() const { return StartInst; } |
| 245 | /// Return the last instruction in the chain. |
| 246 | MachineInstr *getLast() const { return LastInst; } |
| 247 | /// Return the "kill" instruction (as set with setKill()) or NULL. |
| 248 | MachineInstr *getKill() const { return KillInst; } |
| 249 | /// Return an instruction that can be used as an iterator for the end |
| 250 | /// of the chain. This is the maximum of KillInst (if set) and LastInst. |
Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 251 | MachineBasicBlock::iterator end() const { |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 252 | return ++MachineBasicBlock::iterator(KillInst ? KillInst : LastInst); |
| 253 | } |
Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 254 | MachineBasicBlock::iterator begin() const { return getStart(); } |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 255 | |
| 256 | /// Can the Kill instruction (assuming one exists) be modified? |
| 257 | bool isKillImmutable() const { return KillIsImmutable; } |
| 258 | |
| 259 | /// Return the preferred color of this chain. |
| 260 | Color getPreferredColor() { |
| 261 | if (OverrideBalance != 0) |
| 262 | return OverrideBalance == 1 ? Color::Even : Color::Odd; |
| 263 | return LastColor; |
| 264 | } |
| 265 | |
| 266 | /// Return true if this chain (StartInst..KillInst) overlaps with Other. |
James Molloy | f0de7e5 | 2014-09-12 14:35:17 +0000 | [diff] [blame] | 267 | bool rangeOverlapsWith(const Chain &Other) const { |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 268 | unsigned End = KillInst ? KillInstIdx : LastInstIdx; |
James Molloy | f0de7e5 | 2014-09-12 14:35:17 +0000 | [diff] [blame] | 269 | unsigned OtherEnd = Other.KillInst ? |
| 270 | Other.KillInstIdx : Other.LastInstIdx; |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 271 | |
James Molloy | f0de7e5 | 2014-09-12 14:35:17 +0000 | [diff] [blame] | 272 | return StartInstIdx <= OtherEnd && Other.StartInstIdx <= End; |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | /// Return true if this chain starts before Other. |
Chad Rosier | b23c4dd | 2015-01-30 19:55:40 +0000 | [diff] [blame] | 276 | bool startsBefore(const Chain *Other) const { |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 277 | return StartInstIdx < Other->StartInstIdx; |
| 278 | } |
| 279 | |
| 280 | /// Return true if the group will require a fixup MOV at the end. |
| 281 | bool requiresFixup() const { |
| 282 | return (getKill() && isKillImmutable()) || !getKill(); |
| 283 | } |
| 284 | |
| 285 | /// Return a simple string representation of the chain. |
| 286 | std::string str() const { |
| 287 | std::string S; |
| 288 | raw_string_ostream OS(S); |
Junmo Park | 3ec882f | 2016-01-06 03:41:30 +0000 | [diff] [blame] | 289 | |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 290 | OS << "{"; |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 291 | StartInst->print(OS, /* SkipOpers= */true); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 292 | OS << " -> "; |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 293 | LastInst->print(OS, /* SkipOpers= */true); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 294 | if (KillInst) { |
| 295 | OS << " (kill @ "; |
Eric Christopher | 1cdefae | 2015-02-27 00:11:34 +0000 | [diff] [blame] | 296 | KillInst->print(OS, /* SkipOpers= */true); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 297 | OS << ")"; |
| 298 | } |
| 299 | OS << "}"; |
| 300 | |
| 301 | return OS.str(); |
| 302 | } |
| 303 | |
| 304 | }; |
| 305 | |
| 306 | } // end anonymous namespace |
| 307 | |
| 308 | //===----------------------------------------------------------------------===// |
| 309 | |
| 310 | bool AArch64A57FPLoadBalancing::runOnMachineFunction(MachineFunction &F) { |
Andrew Kaylor | 1ac98bb | 2016-04-25 21:58:52 +0000 | [diff] [blame] | 311 | if (skipFunction(*F.getFunction())) |
| 312 | return false; |
| 313 | |
Matthias Braun | 651cff4 | 2016-06-02 18:03:53 +0000 | [diff] [blame] | 314 | if (!F.getSubtarget<AArch64Subtarget>().balanceFPOps()) |
Eric Christopher | 6f1e568 | 2015-03-03 23:22:40 +0000 | [diff] [blame] | 315 | return false; |
| 316 | |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 317 | bool Changed = false; |
| 318 | DEBUG(dbgs() << "***** AArch64A57FPLoadBalancing *****\n"); |
| 319 | |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 320 | MRI = &F.getRegInfo(); |
| 321 | TRI = F.getRegInfo().getTargetRegisterInfo(); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 322 | RCI.runOnMachineFunction(F); |
| 323 | |
| 324 | for (auto &MBB : F) { |
| 325 | Changed |= runOnBasicBlock(MBB); |
| 326 | } |
| 327 | |
| 328 | return Changed; |
| 329 | } |
| 330 | |
| 331 | bool AArch64A57FPLoadBalancing::runOnBasicBlock(MachineBasicBlock &MBB) { |
| 332 | bool Changed = false; |
| 333 | DEBUG(dbgs() << "Running on MBB: " << MBB << " - scanning instructions...\n"); |
| 334 | |
| 335 | // First, scan the basic block producing a set of chains. |
| 336 | |
| 337 | // The currently "active" chains - chains that can be added to and haven't |
| 338 | // been killed yet. This is keyed by register - all chains can only have one |
| 339 | // "link" register between each inst in the chain. |
| 340 | std::map<unsigned, Chain*> ActiveChains; |
Benjamin Kramer | 76e37aa | 2015-03-13 16:59:29 +0000 | [diff] [blame] | 341 | std::vector<std::unique_ptr<Chain>> AllChains; |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 342 | unsigned Idx = 0; |
| 343 | for (auto &MI : MBB) |
| 344 | scanInstruction(&MI, Idx++, ActiveChains, AllChains); |
| 345 | |
| 346 | DEBUG(dbgs() << "Scan complete, "<< AllChains.size() << " chains created.\n"); |
| 347 | |
| 348 | // Group the chains into disjoint sets based on their liveness range. This is |
| 349 | // a poor-man's version of graph coloring. Ideally we'd create an interference |
| 350 | // graph and perform full-on graph coloring on that, but; |
| 351 | // (a) That's rather heavyweight for only two colors. |
| 352 | // (b) We expect multiple disjoint interference regions - in practice the live |
| 353 | // range of chains is quite small and they are clustered between loads |
| 354 | // and stores. |
| 355 | EquivalenceClasses<Chain*> EC; |
James Molloy | f0de7e5 | 2014-09-12 14:35:17 +0000 | [diff] [blame] | 356 | for (auto &I : AllChains) |
| 357 | EC.insert(I.get()); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 358 | |
James Molloy | f0de7e5 | 2014-09-12 14:35:17 +0000 | [diff] [blame] | 359 | for (auto &I : AllChains) |
| 360 | for (auto &J : AllChains) |
| 361 | if (I != J && I->rangeOverlapsWith(*J)) |
| 362 | EC.unionSets(I.get(), J.get()); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 363 | DEBUG(dbgs() << "Created " << EC.getNumClasses() << " disjoint sets.\n"); |
| 364 | |
| 365 | // Now we assume that every member of an equivalence class interferes |
| 366 | // with every other member of that class, and with no members of other classes. |
| 367 | |
| 368 | // Convert the EquivalenceClasses to a simpler set of sets. |
| 369 | std::vector<std::vector<Chain*> > V; |
| 370 | for (auto I = EC.begin(), E = EC.end(); I != E; ++I) { |
| 371 | std::vector<Chain*> Cs(EC.member_begin(I), EC.member_end()); |
| 372 | if (Cs.empty()) continue; |
Benjamin Kramer | e12a6ba | 2014-10-03 18:33:16 +0000 | [diff] [blame] | 373 | V.push_back(std::move(Cs)); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | // Now we have a set of sets, order them by start address so |
| 377 | // we can iterate over them sequentially. |
| 378 | std::sort(V.begin(), V.end(), |
| 379 | [](const std::vector<Chain*> &A, |
| 380 | const std::vector<Chain*> &B) { |
| 381 | return A.front()->startsBefore(B.front()); |
| 382 | }); |
| 383 | |
| 384 | // As we only have two colors, we can track the global (BB-level) balance of |
| 385 | // odds versus evens. We aim to keep this near zero to keep both execution |
| 386 | // units fed. |
| 387 | // Positive means we're even-heavy, negative we're odd-heavy. |
| 388 | // |
| 389 | // FIXME: If chains have interdependencies, for example: |
| 390 | // mul r0, r1, r2 |
| 391 | // mul r3, r0, r1 |
| 392 | // We do not model this and may color each one differently, assuming we'll |
| 393 | // get ILP when we obviously can't. This hasn't been seen to be a problem |
| 394 | // in practice so far, so we simplify the algorithm by ignoring it. |
| 395 | int Parity = 0; |
| 396 | |
| 397 | for (auto &I : V) |
Benjamin Kramer | e12a6ba | 2014-10-03 18:33:16 +0000 | [diff] [blame] | 398 | Changed |= colorChainSet(std::move(I), MBB, Parity); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 399 | |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 400 | return Changed; |
| 401 | } |
| 402 | |
| 403 | Chain *AArch64A57FPLoadBalancing::getAndEraseNext(Color PreferredColor, |
| 404 | std::vector<Chain*> &L) { |
| 405 | if (L.empty()) |
| 406 | return nullptr; |
| 407 | |
| 408 | // We try and get the best candidate from L to color next, given that our |
| 409 | // preferred color is "PreferredColor". L is ordered from larger to smaller |
| 410 | // chains. It is beneficial to color the large chains before the small chains, |
| 411 | // but if we can't find a chain of the maximum length with the preferred color, |
| 412 | // we fuzz the size and look for slightly smaller chains before giving up and |
| 413 | // returning a chain that must be recolored. |
| 414 | |
| 415 | // FIXME: Does this need to be configurable? |
| 416 | const unsigned SizeFuzz = 1; |
| 417 | unsigned MinSize = L.front()->size() - SizeFuzz; |
| 418 | for (auto I = L.begin(), E = L.end(); I != E; ++I) { |
| 419 | if ((*I)->size() <= MinSize) { |
| 420 | // We've gone past the size limit. Return the previous item. |
| 421 | Chain *Ch = *--I; |
| 422 | L.erase(I); |
| 423 | return Ch; |
| 424 | } |
| 425 | |
| 426 | if ((*I)->getPreferredColor() == PreferredColor) { |
| 427 | Chain *Ch = *I; |
| 428 | L.erase(I); |
| 429 | return Ch; |
| 430 | } |
| 431 | } |
Junmo Park | 3ec882f | 2016-01-06 03:41:30 +0000 | [diff] [blame] | 432 | |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 433 | // Bailout case - just return the first item. |
| 434 | Chain *Ch = L.front(); |
| 435 | L.erase(L.begin()); |
| 436 | return Ch; |
| 437 | } |
| 438 | |
| 439 | bool AArch64A57FPLoadBalancing::colorChainSet(std::vector<Chain*> GV, |
| 440 | MachineBasicBlock &MBB, |
| 441 | int &Parity) { |
| 442 | bool Changed = false; |
| 443 | DEBUG(dbgs() << "colorChainSet(): #sets=" << GV.size() << "\n"); |
| 444 | |
| 445 | // Sort by descending size order so that we allocate the most important |
| 446 | // sets first. |
| 447 | // Tie-break equivalent sizes by sorting chains requiring fixups before |
| 448 | // those without fixups. The logic here is that we should look at the |
| 449 | // chains that we cannot change before we look at those we can, |
| 450 | // so the parity counter is updated and we know what color we should |
| 451 | // change them to! |
Chad Rosier | b23c4dd | 2015-01-30 19:55:40 +0000 | [diff] [blame] | 452 | // Final tie-break with instruction order so pass output is stable (i.e. not |
| 453 | // dependent on malloc'd pointer values). |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 454 | std::sort(GV.begin(), GV.end(), [](const Chain *G1, const Chain *G2) { |
| 455 | if (G1->size() != G2->size()) |
| 456 | return G1->size() > G2->size(); |
Chad Rosier | b23c4dd | 2015-01-30 19:55:40 +0000 | [diff] [blame] | 457 | if (G1->requiresFixup() != G2->requiresFixup()) |
| 458 | return G1->requiresFixup() > G2->requiresFixup(); |
| 459 | // Make sure startsBefore() produces a stable final order. |
| 460 | assert((G1 == G2 || (G1->startsBefore(G2) ^ G2->startsBefore(G1))) && |
| 461 | "Starts before not total order!"); |
| 462 | return G1->startsBefore(G2); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 463 | }); |
| 464 | |
| 465 | Color PreferredColor = Parity < 0 ? Color::Even : Color::Odd; |
| 466 | while (Chain *G = getAndEraseNext(PreferredColor, GV)) { |
| 467 | // Start off by assuming we'll color to our own preferred color. |
| 468 | Color C = PreferredColor; |
| 469 | if (Parity == 0) |
| 470 | // But if we really don't care, use the chain's preferred color. |
| 471 | C = G->getPreferredColor(); |
| 472 | |
| 473 | DEBUG(dbgs() << " - Parity=" << Parity << ", Color=" |
| 474 | << ColorNames[(int)C] << "\n"); |
| 475 | |
| 476 | // If we'll need a fixup FMOV, don't bother. Testing has shown that this |
| 477 | // happens infrequently and when it does it has at least a 50% chance of |
| 478 | // slowing code down instead of speeding it up. |
| 479 | if (G->requiresFixup() && C != G->getPreferredColor()) { |
| 480 | C = G->getPreferredColor(); |
| 481 | DEBUG(dbgs() << " - " << G->str() << " - not worthwhile changing; " |
| 482 | "color remains " << ColorNames[(int)C] << "\n"); |
| 483 | } |
| 484 | |
| 485 | Changed |= colorChain(G, C, MBB); |
| 486 | |
| 487 | Parity += (C == Color::Even) ? G->size() : -G->size(); |
| 488 | PreferredColor = Parity < 0 ? Color::Even : Color::Odd; |
| 489 | } |
| 490 | |
| 491 | return Changed; |
| 492 | } |
| 493 | |
| 494 | int AArch64A57FPLoadBalancing::scavengeRegister(Chain *G, Color C, |
| 495 | MachineBasicBlock &MBB) { |
Matthias Braun | d9217c0 | 2017-01-20 03:58:42 +0000 | [diff] [blame^] | 496 | RegScavenger RS; |
| 497 | RS.enterBasicBlock(MBB); |
| 498 | RS.forward(MachineBasicBlock::iterator(G->getStart())); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 499 | |
Matthias Braun | d9217c0 | 2017-01-20 03:58:42 +0000 | [diff] [blame^] | 500 | // Can we find an appropriate register that is available throughout the life |
| 501 | // of the chain? |
| 502 | unsigned RegClassID = G->getStart()->getDesc().OpInfo[0].RegClass; |
| 503 | BitVector AvailableRegs = RS.getRegsAvailable(TRI->getRegClass(RegClassID)); |
| 504 | for (MachineBasicBlock::iterator I = G->begin(), E = G->end(); I != E; ++I) { |
| 505 | RS.forward(I); |
| 506 | AvailableRegs &= RS.getRegsAvailable(TRI->getRegClass(RegClassID)); |
| 507 | |
| 508 | // Remove any registers clobbered by a regmask or any def register that is |
| 509 | // immediately dead. |
| 510 | for (auto J : I->operands()) { |
| 511 | if (J.isRegMask()) |
| 512 | AvailableRegs.clearBitsNotInMask(J.getRegMask()); |
| 513 | |
| 514 | if (J.isReg() && J.isDef()) { |
| 515 | MCRegAliasIterator AI(J.getReg(), TRI, /*IncludeSelf=*/true); |
| 516 | if (J.isDead()) |
| 517 | for (; AI.isValid(); ++AI) |
| 518 | AvailableRegs.reset(*AI); |
| 519 | #ifndef NDEBUG |
| 520 | else |
| 521 | for (; AI.isValid(); ++AI) |
| 522 | assert(!AvailableRegs[*AI] && |
| 523 | "Non-dead def should have been removed by now!"); |
| 524 | #endif |
| 525 | } |
| 526 | } |
| 527 | } |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 528 | |
| 529 | // Make sure we allocate in-order, to get the cheapest registers first. |
| 530 | auto Ord = RCI.getOrder(TRI->getRegClass(RegClassID)); |
| 531 | for (auto Reg : Ord) { |
Matthias Braun | d9217c0 | 2017-01-20 03:58:42 +0000 | [diff] [blame^] | 532 | if (!AvailableRegs[Reg]) |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 533 | continue; |
James Molloy | 4eba015 | 2016-02-26 09:10:53 +0000 | [diff] [blame] | 534 | if (C == getColor(Reg)) |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 535 | return Reg; |
| 536 | } |
| 537 | |
| 538 | return -1; |
| 539 | } |
| 540 | |
| 541 | bool AArch64A57FPLoadBalancing::colorChain(Chain *G, Color C, |
| 542 | MachineBasicBlock &MBB) { |
| 543 | bool Changed = false; |
| 544 | DEBUG(dbgs() << " - colorChain(" << G->str() << ", " |
| 545 | << ColorNames[(int)C] << ")\n"); |
| 546 | |
| 547 | // Try and obtain a free register of the right class. Without a register |
| 548 | // to play with we cannot continue. |
| 549 | int Reg = scavengeRegister(G, C, MBB); |
| 550 | if (Reg == -1) { |
| 551 | DEBUG(dbgs() << "Scavenging (thus coloring) failed!\n"); |
| 552 | return false; |
| 553 | } |
| 554 | DEBUG(dbgs() << " - Scavenged register: " << TRI->getName(Reg) << "\n"); |
| 555 | |
| 556 | std::map<unsigned, unsigned> Substs; |
Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 557 | for (MachineInstr &I : *G) { |
| 558 | if (!G->contains(I) && (&I != G->getKill() || G->isKillImmutable())) |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 559 | continue; |
| 560 | |
| 561 | // I is a member of G, or I is a mutable instruction that kills G. |
| 562 | |
| 563 | std::vector<unsigned> ToErase; |
Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 564 | for (auto &U : I.operands()) { |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 565 | if (U.isReg() && U.isUse() && Substs.find(U.getReg()) != Substs.end()) { |
| 566 | unsigned OrigReg = U.getReg(); |
| 567 | U.setReg(Substs[OrigReg]); |
| 568 | if (U.isKill()) |
| 569 | // Don't erase straight away, because there may be other operands |
| 570 | // that also reference this substitution! |
| 571 | ToErase.push_back(OrigReg); |
| 572 | } else if (U.isRegMask()) { |
| 573 | for (auto J : Substs) { |
| 574 | if (U.clobbersPhysReg(J.first)) |
| 575 | ToErase.push_back(J.first); |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | // Now it's safe to remove the substs identified earlier. |
| 580 | for (auto J : ToErase) |
| 581 | Substs.erase(J); |
| 582 | |
| 583 | // Only change the def if this isn't the last instruction. |
Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 584 | if (&I != G->getKill()) { |
| 585 | MachineOperand &MO = I.getOperand(0); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 586 | |
| 587 | bool Change = TransformAll || getColor(MO.getReg()) != C; |
Duncan P. N. Exon Smith | ab53fd9 | 2016-07-08 20:29:42 +0000 | [diff] [blame] | 588 | if (G->requiresFixup() && &I == G->getLast()) |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 589 | Change = false; |
| 590 | |
| 591 | if (Change) { |
| 592 | Substs[MO.getReg()] = Reg; |
| 593 | MO.setReg(Reg); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 594 | |
| 595 | Changed = true; |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | assert(Substs.size() == 0 && "No substitutions should be left active!"); |
| 600 | |
| 601 | if (G->getKill()) { |
| 602 | DEBUG(dbgs() << " - Kill instruction seen.\n"); |
| 603 | } else { |
| 604 | // We didn't have a kill instruction, but we didn't seem to need to change |
| 605 | // the destination register anyway. |
| 606 | DEBUG(dbgs() << " - Destination register not changed.\n"); |
| 607 | } |
| 608 | return Changed; |
| 609 | } |
| 610 | |
Benjamin Kramer | 76e37aa | 2015-03-13 16:59:29 +0000 | [diff] [blame] | 611 | void AArch64A57FPLoadBalancing::scanInstruction( |
| 612 | MachineInstr *MI, unsigned Idx, std::map<unsigned, Chain *> &ActiveChains, |
| 613 | std::vector<std::unique_ptr<Chain>> &AllChains) { |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 614 | // Inspect "MI", updating ActiveChains and AllChains. |
| 615 | |
| 616 | if (isMul(MI)) { |
| 617 | |
James Molloy | 05ce999 | 2014-09-14 18:24:26 +0000 | [diff] [blame] | 618 | for (auto &I : MI->uses()) |
| 619 | maybeKillChain(I, Idx, ActiveChains); |
| 620 | for (auto &I : MI->defs()) |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 621 | maybeKillChain(I, Idx, ActiveChains); |
| 622 | |
| 623 | // Create a new chain. Multiplies don't require forwarding so can go on any |
| 624 | // unit. |
| 625 | unsigned DestReg = MI->getOperand(0).getReg(); |
| 626 | |
| 627 | DEBUG(dbgs() << "New chain started for register " |
| 628 | << TRI->getName(DestReg) << " at " << *MI); |
| 629 | |
James Molloy | f0de7e5 | 2014-09-12 14:35:17 +0000 | [diff] [blame] | 630 | auto G = llvm::make_unique<Chain>(MI, Idx, getColor(DestReg)); |
| 631 | ActiveChains[DestReg] = G.get(); |
Benjamin Kramer | 76e37aa | 2015-03-13 16:59:29 +0000 | [diff] [blame] | 632 | AllChains.push_back(std::move(G)); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 633 | |
| 634 | } else if (isMla(MI)) { |
| 635 | |
| 636 | // It is beneficial to keep MLAs on the same functional unit as their |
| 637 | // accumulator operand. |
| 638 | unsigned DestReg = MI->getOperand(0).getReg(); |
| 639 | unsigned AccumReg = MI->getOperand(3).getReg(); |
| 640 | |
| 641 | maybeKillChain(MI->getOperand(1), Idx, ActiveChains); |
| 642 | maybeKillChain(MI->getOperand(2), Idx, ActiveChains); |
| 643 | if (DestReg != AccumReg) |
| 644 | maybeKillChain(MI->getOperand(0), Idx, ActiveChains); |
| 645 | |
| 646 | if (ActiveChains.find(AccumReg) != ActiveChains.end()) { |
| 647 | DEBUG(dbgs() << "Chain found for accumulator register " |
| 648 | << TRI->getName(AccumReg) << " in MI " << *MI); |
| 649 | |
| 650 | // For simplicity we only chain together sequences of MULs/MLAs where the |
| 651 | // accumulator register is killed on each instruction. This means we don't |
| 652 | // need to track other uses of the registers we want to rewrite. |
| 653 | // |
| 654 | // FIXME: We could extend to handle the non-kill cases for more coverage. |
| 655 | if (MI->getOperand(3).isKill()) { |
| 656 | // Add to chain. |
| 657 | DEBUG(dbgs() << "Instruction was successfully added to chain.\n"); |
| 658 | ActiveChains[AccumReg]->add(MI, Idx, getColor(DestReg)); |
| 659 | // Handle cases where the destination is not the same as the accumulator. |
Arnaud A. de Grandmaison | 6afbf2a | 2014-08-29 09:54:11 +0000 | [diff] [blame] | 660 | if (DestReg != AccumReg) { |
| 661 | ActiveChains[DestReg] = ActiveChains[AccumReg]; |
| 662 | ActiveChains.erase(AccumReg); |
| 663 | } |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 664 | return; |
| 665 | } |
| 666 | |
| 667 | DEBUG(dbgs() << "Cannot add to chain because accumulator operand wasn't " |
| 668 | << "marked <kill>!\n"); |
| 669 | maybeKillChain(MI->getOperand(3), Idx, ActiveChains); |
| 670 | } |
| 671 | |
| 672 | DEBUG(dbgs() << "Creating new chain for dest register " |
| 673 | << TRI->getName(DestReg) << "\n"); |
James Molloy | f0de7e5 | 2014-09-12 14:35:17 +0000 | [diff] [blame] | 674 | auto G = llvm::make_unique<Chain>(MI, Idx, getColor(DestReg)); |
| 675 | ActiveChains[DestReg] = G.get(); |
Benjamin Kramer | 76e37aa | 2015-03-13 16:59:29 +0000 | [diff] [blame] | 676 | AllChains.push_back(std::move(G)); |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 677 | |
| 678 | } else { |
| 679 | |
| 680 | // Non-MUL or MLA instruction. Invalidate any chain in the uses or defs |
| 681 | // lists. |
James Molloy | 05ce999 | 2014-09-14 18:24:26 +0000 | [diff] [blame] | 682 | for (auto &I : MI->uses()) |
| 683 | maybeKillChain(I, Idx, ActiveChains); |
| 684 | for (auto &I : MI->defs()) |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 685 | maybeKillChain(I, Idx, ActiveChains); |
| 686 | |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | void AArch64A57FPLoadBalancing:: |
| 691 | maybeKillChain(MachineOperand &MO, unsigned Idx, |
| 692 | std::map<unsigned, Chain*> &ActiveChains) { |
| 693 | // Given an operand and the set of active chains (keyed by register), |
| 694 | // determine if a chain should be ended and remove from ActiveChains. |
| 695 | MachineInstr *MI = MO.getParent(); |
| 696 | |
| 697 | if (MO.isReg()) { |
| 698 | |
| 699 | // If this is a KILL of a current chain, record it. |
| 700 | if (MO.isKill() && ActiveChains.find(MO.getReg()) != ActiveChains.end()) { |
| 701 | DEBUG(dbgs() << "Kill seen for chain " << TRI->getName(MO.getReg()) |
| 702 | << "\n"); |
| 703 | ActiveChains[MO.getReg()]->setKill(MI, Idx, /*Immutable=*/MO.isTied()); |
| 704 | } |
| 705 | ActiveChains.erase(MO.getReg()); |
| 706 | |
| 707 | } else if (MO.isRegMask()) { |
| 708 | |
| 709 | for (auto I = ActiveChains.begin(), E = ActiveChains.end(); |
Tim Northover | e42fac5 | 2014-08-08 17:31:52 +0000 | [diff] [blame] | 710 | I != E;) { |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 711 | if (MO.clobbersPhysReg(I->first)) { |
| 712 | DEBUG(dbgs() << "Kill (regmask) seen for chain " |
| 713 | << TRI->getName(I->first) << "\n"); |
| 714 | I->second->setKill(MI, Idx, /*Immutable=*/true); |
Tim Northover | e42fac5 | 2014-08-08 17:31:52 +0000 | [diff] [blame] | 715 | ActiveChains.erase(I++); |
| 716 | } else |
| 717 | ++I; |
James Molloy | 3feea9c | 2014-08-08 12:33:21 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | Color AArch64A57FPLoadBalancing::getColor(unsigned Reg) { |
| 724 | if ((TRI->getEncodingValue(Reg) % 2) == 0) |
| 725 | return Color::Even; |
| 726 | else |
| 727 | return Color::Odd; |
| 728 | } |
| 729 | |
| 730 | // Factory function used by AArch64TargetMachine to add the pass to the passmanager. |
| 731 | FunctionPass *llvm::createAArch64A57FPLoadBalancing() { |
| 732 | return new AArch64A57FPLoadBalancing(); |
| 733 | } |