Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 1 | //===- MipsConstantIslandPass.cpp - Emit Pc Relative loads ----------------===// |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 9 | // This pass is used to make Pc relative loads of constants. |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 10 | // For now, only Mips16 will use this. |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 11 | // |
| 12 | // Loading constants inline is expensive on Mips16 and it's in general better |
| 13 | // to place the constant nearby in code space and then it can be loaded with a |
| 14 | // simple 16 bit load instruction. |
| 15 | // |
| 16 | // The constants can be not just numbers but addresses of functions and labels. |
| 17 | // This can be particularly helpful in static relocation mode for embedded |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 18 | // non-linux targets. |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 19 | // |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 21 | |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 22 | #include "Mips.h" |
Reed Kotler | 5c8ae09 | 2013-11-13 04:37:52 +0000 | [diff] [blame] | 23 | #include "Mips16InstrInfo.h" |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 24 | #include "MipsMachineFunction.h" |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 25 | #include "MipsSubtarget.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/STLExtras.h" |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallSet.h" |
| 28 | #include "llvm/ADT/SmallVector.h" |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/StringRef.h" |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 31 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Eric Christopher | 79cc1e3 | 2014-09-02 22:28:02 +0000 | [diff] [blame] | 32 | #include "llvm/CodeGen/MachineConstantPool.h" |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 33 | #include "llvm/CodeGen/MachineFunction.h" |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 34 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 35 | #include "llvm/CodeGen/MachineInstr.h" |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 36 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 37 | #include "llvm/CodeGen/MachineOperand.h" |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 38 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Nico Weber | 432a388 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 39 | #include "llvm/Config/llvm-config.h" |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 40 | #include "llvm/IR/Constants.h" |
| 41 | #include "llvm/IR/DataLayout.h" |
| 42 | #include "llvm/IR/DebugLoc.h" |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 43 | #include "llvm/IR/Function.h" |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 44 | #include "llvm/IR/Type.h" |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 45 | #include "llvm/Support/CommandLine.h" |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Compiler.h" |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 47 | #include "llvm/Support/Debug.h" |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 48 | #include "llvm/Support/ErrorHandling.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Format.h" |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 50 | #include "llvm/Support/MathExtras.h" |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 51 | #include "llvm/Support/raw_ostream.h" |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 52 | #include <algorithm> |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 53 | #include <cassert> |
| 54 | #include <cstdint> |
| 55 | #include <iterator> |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 56 | #include <vector> |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 57 | |
| 58 | using namespace llvm; |
| 59 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 60 | #define DEBUG_TYPE "mips-constant-islands" |
| 61 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 62 | STATISTIC(NumCPEs, "Number of constpool entries"); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 63 | STATISTIC(NumSplit, "Number of uncond branches inserted"); |
| 64 | STATISTIC(NumCBrFixed, "Number of cond branches fixed"); |
| 65 | STATISTIC(NumUBrFixed, "Number of uncond branches fixed"); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 66 | |
| 67 | // FIXME: This option should be removed once it has received sufficient testing. |
| 68 | static cl::opt<bool> |
| 69 | AlignConstantIslands("mips-align-constant-islands", cl::Hidden, cl::init(true), |
| 70 | cl::desc("Align constant islands in code")); |
| 71 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 72 | // Rather than do make check tests with huge amounts of code, we force |
| 73 | // the test to use this amount. |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 74 | static cl::opt<int> ConstantIslandsSmallOffset( |
| 75 | "mips-constant-islands-small-offset", |
| 76 | cl::init(0), |
| 77 | cl::desc("Make small offsets be this amount for testing purposes"), |
| 78 | cl::Hidden); |
| 79 | |
Reed Kotler | 45c5927 | 2013-11-10 00:09:26 +0000 | [diff] [blame] | 80 | // For testing purposes we tell it to not use relaxed load forms so that it |
| 81 | // will split blocks. |
Reed Kotler | 45c5927 | 2013-11-10 00:09:26 +0000 | [diff] [blame] | 82 | static cl::opt<bool> NoLoadRelaxation( |
| 83 | "mips-constant-islands-no-load-relaxation", |
| 84 | cl::init(false), |
| 85 | cl::desc("Don't relax loads to long loads - for testing purposes"), |
| 86 | cl::Hidden); |
| 87 | |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 88 | static unsigned int branchTargetOperand(MachineInstr *MI) { |
| 89 | switch (MI->getOpcode()) { |
| 90 | case Mips::Bimm16: |
| 91 | case Mips::BimmX16: |
| 92 | case Mips::Bteqz16: |
| 93 | case Mips::BteqzX16: |
| 94 | case Mips::Btnez16: |
| 95 | case Mips::BtnezX16: |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 96 | case Mips::JalB16: |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 97 | return 0; |
| 98 | case Mips::BeqzRxImm16: |
| 99 | case Mips::BeqzRxImmX16: |
| 100 | case Mips::BnezRxImm16: |
| 101 | case Mips::BnezRxImmX16: |
| 102 | return 1; |
| 103 | } |
| 104 | llvm_unreachable("Unknown branch type"); |
| 105 | } |
| 106 | |
| 107 | static unsigned int longformBranchOpcode(unsigned int Opcode) { |
| 108 | switch (Opcode) { |
| 109 | case Mips::Bimm16: |
| 110 | case Mips::BimmX16: |
| 111 | return Mips::BimmX16; |
| 112 | case Mips::Bteqz16: |
| 113 | case Mips::BteqzX16: |
| 114 | return Mips::BteqzX16; |
| 115 | case Mips::Btnez16: |
| 116 | case Mips::BtnezX16: |
| 117 | return Mips::BtnezX16; |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 118 | case Mips::JalB16: |
| 119 | return Mips::JalB16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 120 | case Mips::BeqzRxImm16: |
| 121 | case Mips::BeqzRxImmX16: |
| 122 | return Mips::BeqzRxImmX16; |
| 123 | case Mips::BnezRxImm16: |
| 124 | case Mips::BnezRxImmX16: |
| 125 | return Mips::BnezRxImmX16; |
| 126 | } |
| 127 | llvm_unreachable("Unknown branch type"); |
| 128 | } |
| 129 | |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 130 | // FIXME: need to go through this whole constant islands port and check the math |
| 131 | // for branch ranges and clean this up and make some functions to calculate things |
| 132 | // that are done many times identically. |
| 133 | // Need to refactor some of the code to call this routine. |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 134 | static unsigned int branchMaxOffsets(unsigned int Opcode) { |
| 135 | unsigned Bits, Scale; |
| 136 | switch (Opcode) { |
| 137 | case Mips::Bimm16: |
| 138 | Bits = 11; |
| 139 | Scale = 2; |
| 140 | break; |
| 141 | case Mips::BimmX16: |
| 142 | Bits = 16; |
| 143 | Scale = 2; |
| 144 | break; |
| 145 | case Mips::BeqzRxImm16: |
| 146 | Bits = 8; |
| 147 | Scale = 2; |
| 148 | break; |
| 149 | case Mips::BeqzRxImmX16: |
| 150 | Bits = 16; |
| 151 | Scale = 2; |
| 152 | break; |
| 153 | case Mips::BnezRxImm16: |
| 154 | Bits = 8; |
| 155 | Scale = 2; |
| 156 | break; |
| 157 | case Mips::BnezRxImmX16: |
| 158 | Bits = 16; |
| 159 | Scale = 2; |
| 160 | break; |
| 161 | case Mips::Bteqz16: |
| 162 | Bits = 8; |
| 163 | Scale = 2; |
| 164 | break; |
| 165 | case Mips::BteqzX16: |
| 166 | Bits = 16; |
| 167 | Scale = 2; |
| 168 | break; |
| 169 | case Mips::Btnez16: |
| 170 | Bits = 8; |
| 171 | Scale = 2; |
| 172 | break; |
| 173 | case Mips::BtnezX16: |
| 174 | Bits = 16; |
| 175 | Scale = 2; |
| 176 | break; |
| 177 | default: |
| 178 | llvm_unreachable("Unknown branch type"); |
| 179 | } |
| 180 | unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; |
| 181 | return MaxOffs; |
| 182 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 183 | |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 184 | namespace { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 185 | |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 186 | using Iter = MachineBasicBlock::iterator; |
| 187 | using ReverseIter = MachineBasicBlock::reverse_iterator; |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 188 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 189 | /// MipsConstantIslands - Due to limited PC-relative displacements, Mips |
| 190 | /// requires constant pool entries to be scattered among the instructions |
| 191 | /// inside a function. To do this, it completely ignores the normal LLVM |
| 192 | /// constant pool; instead, it places constants wherever it feels like with |
| 193 | /// special instructions. |
| 194 | /// |
| 195 | /// The terminology used in this pass includes: |
| 196 | /// Islands - Clumps of constants placed in the function. |
| 197 | /// Water - Potential places where an island could be formed. |
| 198 | /// CPE - A constant pool entry that has been placed somewhere, which |
| 199 | /// tracks a list of users. |
| 200 | |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 201 | class MipsConstantIslands : public MachineFunctionPass { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 202 | /// BasicBlockInfo - Information about the offset and size of a single |
| 203 | /// basic block. |
| 204 | struct BasicBlockInfo { |
| 205 | /// Offset - Distance from the beginning of the function to the beginning |
| 206 | /// of this basic block. |
| 207 | /// |
| 208 | /// Offsets are computed assuming worst case padding before an aligned |
| 209 | /// block. This means that subtracting basic block offsets always gives a |
| 210 | /// conservative estimate of the real distance which may be smaller. |
| 211 | /// |
| 212 | /// Because worst case padding is used, the computed offset of an aligned |
| 213 | /// block may not actually be aligned. |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 214 | unsigned Offset = 0; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 215 | |
| 216 | /// Size - Size of the basic block in bytes. If the block contains |
| 217 | /// inline assembly, this is a worst case estimate. |
| 218 | /// |
| 219 | /// The size does not include any alignment padding whether from the |
| 220 | /// beginning of the block, or from an aligned jump table at the end. |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 221 | unsigned Size = 0; |
| 222 | |
| 223 | BasicBlockInfo() = default; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 224 | |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame] | 225 | unsigned postOffset() const { return Offset + Size; } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | std::vector<BasicBlockInfo> BBInfo; |
| 229 | |
| 230 | /// WaterList - A sorted list of basic blocks where islands could be placed |
| 231 | /// (i.e. blocks that don't fall through to the following block, due |
| 232 | /// to a return, unreachable, or unconditional branch). |
| 233 | std::vector<MachineBasicBlock*> WaterList; |
| 234 | |
| 235 | /// NewWaterList - The subset of WaterList that was created since the |
| 236 | /// previous iteration by inserting unconditional branches. |
| 237 | SmallSet<MachineBasicBlock*, 4> NewWaterList; |
| 238 | |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 239 | using water_iterator = std::vector<MachineBasicBlock *>::iterator; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 240 | |
| 241 | /// CPUser - One user of a constant pool, keeping the machine instruction |
| 242 | /// pointer, the constant pool being referenced, and the max displacement |
| 243 | /// allowed from the instruction to the CP. The HighWaterMark records the |
| 244 | /// highest basic block where a new CPEntry can be placed. To ensure this |
| 245 | /// pass terminates, the CP entries are initially placed at the end of the |
| 246 | /// function and then move monotonically to lower addresses. The |
| 247 | /// exception to this rule is when the current CP entry for a particular |
| 248 | /// CPUser is out of range, but there is another CP entry for the same |
| 249 | /// constant value in range. We want to use the existing in-range CP |
| 250 | /// entry, but if it later moves out of range, the search for new water |
| 251 | /// should resume where it left off. The HighWaterMark is used to record |
| 252 | /// that point. |
| 253 | struct CPUser { |
| 254 | MachineInstr *MI; |
| 255 | MachineInstr *CPEMI; |
| 256 | MachineBasicBlock *HighWaterMark; |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 257 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 258 | private: |
| 259 | unsigned MaxDisp; |
| 260 | unsigned LongFormMaxDisp; // mips16 has 16/32 bit instructions |
| 261 | // with different displacements |
| 262 | unsigned LongFormOpcode; |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 263 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 264 | public: |
| 265 | bool NegOk; |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 266 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 267 | CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp, |
Reed Kotler | b09ebe9 | 2013-11-05 22:34:29 +0000 | [diff] [blame] | 268 | bool neg, |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 269 | unsigned longformmaxdisp, unsigned longformopcode) |
| 270 | : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), |
| 271 | LongFormMaxDisp(longformmaxdisp), LongFormOpcode(longformopcode), |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 272 | NegOk(neg){ |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 273 | HighWaterMark = CPEMI->getParent(); |
| 274 | } |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 275 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 276 | /// getMaxDisp - Returns the maximum displacement supported by MI. |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 277 | unsigned getMaxDisp() const { |
| 278 | unsigned xMaxDisp = ConstantIslandsSmallOffset? |
| 279 | ConstantIslandsSmallOffset: MaxDisp; |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 280 | return xMaxDisp; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 281 | } |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 282 | |
Reed Kotler | 45c5927 | 2013-11-10 00:09:26 +0000 | [diff] [blame] | 283 | void setMaxDisp(unsigned val) { |
| 284 | MaxDisp = val; |
| 285 | } |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 286 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 287 | unsigned getLongFormMaxDisp() const { |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 288 | return LongFormMaxDisp; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 289 | } |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 290 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 291 | unsigned getLongFormOpcode() const { |
| 292 | return LongFormOpcode; |
| 293 | } |
| 294 | }; |
| 295 | |
| 296 | /// CPUsers - Keep track of all of the machine instructions that use various |
| 297 | /// constant pools and their max displacement. |
| 298 | std::vector<CPUser> CPUsers; |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 299 | |
| 300 | /// CPEntry - One per constant pool entry, keeping the machine instruction |
| 301 | /// pointer, the constpool index, and the number of CPUser's which |
| 302 | /// reference this entry. |
| 303 | struct CPEntry { |
| 304 | MachineInstr *CPEMI; |
| 305 | unsigned CPI; |
| 306 | unsigned RefCount; |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 307 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 308 | CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0) |
| 309 | : CPEMI(cpemi), CPI(cpi), RefCount(rc) {} |
| 310 | }; |
| 311 | |
| 312 | /// CPEntries - Keep track of all of the constant pool entry machine |
| 313 | /// instructions. For each original constpool index (i.e. those that |
| 314 | /// existed upon entry to this pass), it keeps a vector of entries. |
| 315 | /// Original elements are cloned as we go along; the clones are |
| 316 | /// put in the vector of the original element, but have distinct CPIs. |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 317 | std::vector<std::vector<CPEntry>> CPEntries; |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 318 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 319 | /// ImmBranch - One per immediate branch, keeping the machine instruction |
| 320 | /// pointer, conditional or unconditional, the max displacement, |
| 321 | /// and (if isCond is true) the corresponding unconditional branch |
| 322 | /// opcode. |
| 323 | struct ImmBranch { |
| 324 | MachineInstr *MI; |
| 325 | unsigned MaxDisp : 31; |
| 326 | bool isCond : 1; |
| 327 | int UncondBr; |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 328 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 329 | ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr) |
| 330 | : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {} |
| 331 | }; |
| 332 | |
| 333 | /// ImmBranches - Keep track of all the immediate branch instructions. |
| 334 | /// |
| 335 | std::vector<ImmBranch> ImmBranches; |
| 336 | |
| 337 | /// HasFarJump - True if any far jump instruction has been emitted during |
| 338 | /// the branch fix up pass. |
| 339 | bool HasFarJump; |
| 340 | |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 341 | const MipsSubtarget *STI = nullptr; |
Reed Kotler | 5c8ae09 | 2013-11-13 04:37:52 +0000 | [diff] [blame] | 342 | const Mips16InstrInfo *TII; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 343 | MipsFunctionInfo *MFI; |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 344 | MachineFunction *MF = nullptr; |
| 345 | MachineConstantPool *MCP = nullptr; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 346 | |
| 347 | unsigned PICLabelUId; |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 348 | bool PrescannedForConstants = false; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 349 | |
| 350 | void initPICLabelUId(unsigned UId) { |
| 351 | PICLabelUId = UId; |
| 352 | } |
| 353 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 354 | unsigned createPICLabelUId() { |
| 355 | return PICLabelUId++; |
| 356 | } |
| 357 | |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 358 | public: |
| 359 | static char ID; |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 360 | |
| 361 | MipsConstantIslands() : MachineFunctionPass(ID) {} |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 362 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 363 | StringRef getPassName() const override { return "Mips Constant Islands"; } |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 364 | |
Craig Topper | 56c590a | 2014-04-29 07:58:02 +0000 | [diff] [blame] | 365 | bool runOnMachineFunction(MachineFunction &F) override; |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 366 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 367 | MachineFunctionProperties getRequiredProperties() const override { |
| 368 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 369 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 372 | void doInitialPlacement(std::vector<MachineInstr*> &CPEMIs); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 373 | CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI); |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 374 | Align getCPEAlign(const MachineInstr &CPEMI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 375 | void initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs); |
| 376 | unsigned getOffsetOf(MachineInstr *MI) const; |
| 377 | unsigned getUserOffset(CPUser&) const; |
| 378 | void dumpBBs(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 379 | |
| 380 | bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset, |
Reed Kotler | b09ebe9 | 2013-11-05 22:34:29 +0000 | [diff] [blame] | 381 | unsigned Disp, bool NegativeOK); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 382 | bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset, |
| 383 | const CPUser &U); |
| 384 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 385 | void computeBlockSize(MachineBasicBlock *MBB); |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 386 | MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 387 | void updateForInsertedWaterBlock(MachineBasicBlock *NewBB); |
| 388 | void adjustBBOffsetsAfter(MachineBasicBlock *BB); |
| 389 | bool decrementCPEReferenceCount(unsigned CPI, MachineInstr* CPEMI); |
| 390 | int findInRangeCPEntry(CPUser& U, unsigned UserOffset); |
| 391 | int findLongFormInRangeCPEntry(CPUser& U, unsigned UserOffset); |
| 392 | bool findAvailableWater(CPUser&U, unsigned UserOffset, |
| 393 | water_iterator &WaterIter); |
| 394 | void createNewWater(unsigned CPUserIndex, unsigned UserOffset, |
| 395 | MachineBasicBlock *&NewMBB); |
| 396 | bool handleConstantPoolUser(unsigned CPUserIndex); |
| 397 | void removeDeadCPEMI(MachineInstr *CPEMI); |
| 398 | bool removeUnusedCPEntries(); |
| 399 | bool isCPEntryInRange(MachineInstr *MI, unsigned UserOffset, |
| 400 | MachineInstr *CPEMI, unsigned Disp, bool NegOk, |
| 401 | bool DoDump = false); |
| 402 | bool isWaterInRange(unsigned UserOffset, MachineBasicBlock *Water, |
| 403 | CPUser &U, unsigned &Growth); |
| 404 | bool isBBInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp); |
| 405 | bool fixupImmediateBr(ImmBranch &Br); |
| 406 | bool fixupConditionalBr(ImmBranch &Br); |
| 407 | bool fixupUnconditionalBr(ImmBranch &Br); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 408 | |
| 409 | void prescanForConstants(); |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 410 | }; |
| 411 | |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 412 | } // end anonymous namespace |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 413 | |
Eugene Zelenko | 79220eae | 2017-08-03 22:12:30 +0000 | [diff] [blame] | 414 | char MipsConstantIslands::ID = 0; |
| 415 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 416 | bool MipsConstantIslands::isOffsetInRange |
| 417 | (unsigned UserOffset, unsigned TrialOffset, |
| 418 | const CPUser &U) { |
| 419 | return isOffsetInRange(UserOffset, TrialOffset, |
Reed Kotler | b09ebe9 | 2013-11-05 22:34:29 +0000 | [diff] [blame] | 420 | U.getMaxDisp(), U.NegOk); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 421 | } |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 422 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 423 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 424 | /// print block size and offset information - debugging |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 425 | LLVM_DUMP_METHOD void MipsConstantIslands::dumpBBs() { |
| 426 | for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) { |
| 427 | const BasicBlockInfo &BBI = BBInfo[J]; |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 428 | dbgs() << format("%08x %bb.%u\t", BBI.Offset, J) |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 429 | << format(" size=%#x\n", BBInfo[J].Size); |
| 430 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 431 | } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 432 | #endif |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 433 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 434 | bool MipsConstantIslands::runOnMachineFunction(MachineFunction &mf) { |
Reed Kotler | 1595f36 | 2013-04-09 19:46:01 +0000 | [diff] [blame] | 435 | // The intention is for this to be a mips16 only pass for now |
| 436 | // FIXME: |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 437 | MF = &mf; |
| 438 | MCP = mf.getConstantPool(); |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 439 | STI = &static_cast<const MipsSubtarget &>(mf.getSubtarget()); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 440 | LLVM_DEBUG(dbgs() << "constant island machine function " |
| 441 | << "\n"); |
Eric Christopher | 4e7d1e7 | 2014-07-18 23:41:32 +0000 | [diff] [blame] | 442 | if (!STI->inMips16Mode() || !MipsSubtarget::useConstantIslands()) { |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 443 | return false; |
| 444 | } |
Eric Christopher | 96e72c6 | 2015-01-29 23:27:36 +0000 | [diff] [blame] | 445 | TII = (const Mips16InstrInfo *)STI->getInstrInfo(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 446 | MFI = MF->getInfo<MipsFunctionInfo>(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 447 | LLVM_DEBUG(dbgs() << "constant island processing " |
| 448 | << "\n"); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 449 | // |
| 450 | // will need to make predermination if there is any constants we need to |
| 451 | // put in constant islands. TBD. |
| 452 | // |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 453 | if (!PrescannedForConstants) prescanForConstants(); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 454 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 455 | HasFarJump = false; |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 456 | // This pass invalidates liveness information when it splits basic blocks. |
| 457 | MF->getRegInfo().invalidateLiveness(); |
| 458 | |
| 459 | // Renumber all of the machine basic blocks in the function, guaranteeing that |
| 460 | // the numbers agree with the position of the block in the function. |
| 461 | MF->RenumberBlocks(); |
| 462 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 463 | bool MadeChange = false; |
| 464 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 465 | // Perform the initial placement of the constant pool entries. To start with, |
| 466 | // we put them all at the end of the function. |
| 467 | std::vector<MachineInstr*> CPEMIs; |
| 468 | if (!MCP->isEmpty()) |
| 469 | doInitialPlacement(CPEMIs); |
| 470 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 471 | /// The next UID to take is the first unused one. |
| 472 | initPICLabelUId(CPEMIs.size()); |
| 473 | |
| 474 | // Do the initial scan of the function, building up information about the |
| 475 | // sizes of each block, the location of all the water, and finding all of the |
| 476 | // constant pool users. |
| 477 | initializeFunctionInfo(CPEMIs); |
| 478 | CPEMIs.clear(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 479 | LLVM_DEBUG(dumpBBs()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 480 | |
| 481 | /// Remove dead constant pool entries. |
| 482 | MadeChange |= removeUnusedCPEntries(); |
| 483 | |
| 484 | // Iteratively place constant pool entries and fix up branches until there |
| 485 | // is no change. |
| 486 | unsigned NoCPIters = 0, NoBRIters = 0; |
| 487 | (void)NoBRIters; |
| 488 | while (true) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 489 | LLVM_DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n'); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 490 | bool CPChange = false; |
| 491 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) |
| 492 | CPChange |= handleConstantPoolUser(i); |
| 493 | if (CPChange && ++NoCPIters > 30) |
| 494 | report_fatal_error("Constant Island pass failed to converge!"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 495 | LLVM_DEBUG(dumpBBs()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 496 | |
| 497 | // Clear NewWaterList now. If we split a block for branches, it should |
| 498 | // appear as "new water" for the next iteration of constant pool placement. |
| 499 | NewWaterList.clear(); |
| 500 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 501 | LLVM_DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n'); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 502 | bool BRChange = false; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 503 | for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) |
| 504 | BRChange |= fixupImmediateBr(ImmBranches[i]); |
| 505 | if (BRChange && ++NoBRIters > 30) |
| 506 | report_fatal_error("Branch Fix Up pass failed to converge!"); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 507 | LLVM_DEBUG(dumpBBs()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 508 | if (!CPChange && !BRChange) |
| 509 | break; |
| 510 | MadeChange = true; |
| 511 | } |
| 512 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 513 | LLVM_DEBUG(dbgs() << '\n'; dumpBBs()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 514 | |
| 515 | BBInfo.clear(); |
| 516 | WaterList.clear(); |
| 517 | CPUsers.clear(); |
| 518 | CPEntries.clear(); |
| 519 | ImmBranches.clear(); |
| 520 | return MadeChange; |
Reed Kotler | bb3094a | 2013-02-27 03:33:58 +0000 | [diff] [blame] | 521 | } |
| 522 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 523 | /// doInitialPlacement - Perform the initial placement of the constant pool |
| 524 | /// entries. To start with, we put them all at the end of the function. |
| 525 | void |
| 526 | MipsConstantIslands::doInitialPlacement(std::vector<MachineInstr*> &CPEMIs) { |
| 527 | // Create the basic block to hold the CPE's. |
| 528 | MachineBasicBlock *BB = MF->CreateMachineBasicBlock(); |
| 529 | MF->push_back(BB); |
| 530 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 531 | // MachineConstantPool measures alignment in bytes. We measure in log2(bytes). |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 532 | const Align MaxAlign(MCP->getConstantPoolAlignment()); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 533 | |
| 534 | // Mark the basic block as required by the const-pool. |
| 535 | // If AlignConstantIslands isn't set, use 4-byte alignment for everything. |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 536 | BB->setAlignment(AlignConstantIslands ? MaxAlign : Align(4)); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 537 | |
| 538 | // The function needs to be as aligned as the basic blocks. The linker may |
| 539 | // move functions around based on their alignment. |
Guillaume Chatelet | 48904e9 | 2019-09-11 11:16:48 +0000 | [diff] [blame] | 540 | MF->ensureAlignment(BB->getAlignment()); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 541 | |
| 542 | // Order the entries in BB by descending alignment. That ensures correct |
| 543 | // alignment of all entries as long as BB is sufficiently aligned. Keep |
| 544 | // track of the insertion point for each alignment. We are going to bucket |
| 545 | // sort the entries as they are created. |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame] | 546 | SmallVector<MachineBasicBlock::iterator, 8> InsPoint(Log2(MaxAlign) + 1, |
Guillaume Chatelet | aff45e4 | 2019-09-05 10:00:22 +0000 | [diff] [blame] | 547 | BB->end()); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 548 | |
| 549 | // Add all of the constants from the constant pool to the end block, use an |
| 550 | // identity mapping of CPI's to CPE's. |
| 551 | const std::vector<MachineConstantPoolEntry> &CPs = MCP->getConstants(); |
| 552 | |
Mehdi Amini | bd7287e | 2015-07-16 06:11:10 +0000 | [diff] [blame] | 553 | const DataLayout &TD = MF->getDataLayout(); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 554 | for (unsigned i = 0, e = CPs.size(); i != e; ++i) { |
| 555 | unsigned Size = TD.getTypeAllocSize(CPs[i].getType()); |
| 556 | assert(Size >= 4 && "Too small constant pool entry"); |
| 557 | unsigned Align = CPs[i].getAlignment(); |
| 558 | assert(isPowerOf2_32(Align) && "Invalid alignment"); |
| 559 | // Verify that all constant pool entries are a multiple of their alignment. |
| 560 | // If not, we would have to pad them out so that instructions stay aligned. |
| 561 | assert((Size % Align) == 0 && "CP Entry not multiple of 4 bytes!"); |
| 562 | |
| 563 | // Insert CONSTPOOL_ENTRY before entries with a smaller alignment. |
| 564 | unsigned LogAlign = Log2_32(Align); |
| 565 | MachineBasicBlock::iterator InsAt = InsPoint[LogAlign]; |
| 566 | |
| 567 | MachineInstr *CPEMI = |
| 568 | BuildMI(*BB, InsAt, DebugLoc(), TII->get(Mips::CONSTPOOL_ENTRY)) |
| 569 | .addImm(i).addConstantPoolIndex(i).addImm(Size); |
| 570 | |
| 571 | CPEMIs.push_back(CPEMI); |
| 572 | |
| 573 | // Ensure that future entries with higher alignment get inserted before |
| 574 | // CPEMI. This is bucket sort with iterators. |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame] | 575 | for (unsigned a = LogAlign + 1; a <= Log2(MaxAlign); ++a) |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 576 | if (InsPoint[a] == InsAt) |
| 577 | InsPoint[a] = CPEMI; |
| 578 | // Add a new CPEntry, but no corresponding CPUser yet. |
Benjamin Kramer | e12a6ba | 2014-10-03 18:33:16 +0000 | [diff] [blame] | 579 | CPEntries.emplace_back(1, CPEntry(CPEMI, i)); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 580 | ++NumCPEs; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 581 | LLVM_DEBUG(dbgs() << "Moved CPI#" << i << " to end of function, size = " |
| 582 | << Size << ", align = " << Align << '\n'); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 583 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 584 | LLVM_DEBUG(BB->dump()); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 587 | /// BBHasFallthrough - Return true if the specified basic block can fallthrough |
| 588 | /// into the block immediately after it. |
| 589 | static bool BBHasFallthrough(MachineBasicBlock *MBB) { |
| 590 | // Get the next machine basic block in the function. |
Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 591 | MachineFunction::iterator MBBI = MBB->getIterator(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 592 | // Can't fall off end of function. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 593 | if (std::next(MBBI) == MBB->getParent()->end()) |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 594 | return false; |
| 595 | |
Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 596 | MachineBasicBlock *NextBB = &*std::next(MBBI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 597 | for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), |
| 598 | E = MBB->succ_end(); I != E; ++I) |
| 599 | if (*I == NextBB) |
| 600 | return true; |
| 601 | |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI, |
| 606 | /// look up the corresponding CPEntry. |
| 607 | MipsConstantIslands::CPEntry |
| 608 | *MipsConstantIslands::findConstPoolEntry(unsigned CPI, |
| 609 | const MachineInstr *CPEMI) { |
| 610 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 611 | // Number of entries per constpool index should be small, just do a |
| 612 | // linear search. |
| 613 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 614 | if (CPEs[i].CPEMI == CPEMI) |
| 615 | return &CPEs[i]; |
| 616 | } |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 617 | return nullptr; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame] | 620 | /// getCPEAlign - Returns the required alignment of the constant pool entry |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 621 | /// represented by CPEMI. Alignment is measured in log2(bytes) units. |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 622 | Align MipsConstantIslands::getCPEAlign(const MachineInstr &CPEMI) { |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 623 | assert(CPEMI.getOpcode() == Mips::CONSTPOOL_ENTRY); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 624 | |
| 625 | // Everything is 4-byte aligned unless AlignConstantIslands is set. |
| 626 | if (!AlignConstantIslands) |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 627 | return Align(4); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 628 | |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 629 | unsigned CPI = CPEMI.getOperand(1).getIndex(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 630 | assert(CPI < MCP->getConstants().size() && "Invalid constant pool index."); |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 631 | return Align(MCP->getConstants()[CPI].getAlignment()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /// initializeFunctionInfo - Do the initial scan of the function, building up |
| 635 | /// information about the sizes of each block, the location of all the water, |
| 636 | /// and finding all of the constant pool users. |
| 637 | void MipsConstantIslands:: |
| 638 | initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs) { |
| 639 | BBInfo.clear(); |
| 640 | BBInfo.resize(MF->getNumBlockIDs()); |
| 641 | |
| 642 | // First thing, compute the size of all basic blocks, and see if the function |
| 643 | // has any inline assembly in it. If so, we have to be conservative about |
| 644 | // alignment assumptions, as we don't know for sure the size of any |
| 645 | // instructions in the inline assembly. |
| 646 | for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I) |
Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 647 | computeBlockSize(&*I); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 648 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 649 | // Compute block offsets. |
Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 650 | adjustBBOffsetsAfter(&MF->front()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 651 | |
| 652 | // Now go back through the instructions and build up our data structures. |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 653 | for (MachineBasicBlock &MBB : *MF) { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 654 | // If this block doesn't fall through into the next MBB, then this is |
| 655 | // 'water' that a constant pool island could be placed. |
| 656 | if (!BBHasFallthrough(&MBB)) |
| 657 | WaterList.push_back(&MBB); |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 658 | for (MachineInstr &MI : MBB) { |
Shiva Chen | 801bf7e | 2018-05-09 02:42:00 +0000 | [diff] [blame] | 659 | if (MI.isDebugInstr()) |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 660 | continue; |
| 661 | |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 662 | int Opc = MI.getOpcode(); |
| 663 | if (MI.isBranch()) { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 664 | bool isCond = false; |
| 665 | unsigned Bits = 0; |
| 666 | unsigned Scale = 1; |
| 667 | int UOpc = Opc; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 668 | switch (Opc) { |
| 669 | default: |
Reed Kotler | 4b7afe5 | 2013-11-13 23:52:18 +0000 | [diff] [blame] | 670 | continue; // Ignore other branches for now |
| 671 | case Mips::Bimm16: |
| 672 | Bits = 11; |
| 673 | Scale = 2; |
| 674 | isCond = false; |
| 675 | break; |
| 676 | case Mips::BimmX16: |
| 677 | Bits = 16; |
| 678 | Scale = 2; |
| 679 | isCond = false; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 680 | break; |
| 681 | case Mips::BeqzRxImm16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 682 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 683 | Bits = 8; |
| 684 | Scale = 2; |
| 685 | isCond = true; |
| 686 | break; |
| 687 | case Mips::BeqzRxImmX16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 688 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 689 | Bits = 16; |
| 690 | Scale = 2; |
| 691 | isCond = true; |
| 692 | break; |
| 693 | case Mips::BnezRxImm16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 694 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 695 | Bits = 8; |
| 696 | Scale = 2; |
| 697 | isCond = true; |
| 698 | break; |
| 699 | case Mips::BnezRxImmX16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 700 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 701 | Bits = 16; |
| 702 | Scale = 2; |
| 703 | isCond = true; |
| 704 | break; |
| 705 | case Mips::Bteqz16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 706 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 707 | Bits = 8; |
| 708 | Scale = 2; |
| 709 | isCond = true; |
| 710 | break; |
| 711 | case Mips::BteqzX16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 712 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 713 | Bits = 16; |
| 714 | Scale = 2; |
| 715 | isCond = true; |
| 716 | break; |
| 717 | case Mips::Btnez16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 718 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 719 | Bits = 8; |
| 720 | Scale = 2; |
| 721 | isCond = true; |
| 722 | break; |
| 723 | case Mips::BtnezX16: |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 724 | UOpc=Mips::Bimm16; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 725 | Bits = 16; |
| 726 | Scale = 2; |
| 727 | isCond = true; |
| 728 | break; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 729 | } |
| 730 | // Record this immediate branch. |
| 731 | unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 732 | ImmBranches.push_back(ImmBranch(&MI, MaxOffs, isCond, UOpc)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 733 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 734 | |
| 735 | if (Opc == Mips::CONSTPOOL_ENTRY) |
| 736 | continue; |
| 737 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 738 | // Scan the instructions for constant pool operands. |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 739 | for (unsigned op = 0, e = MI.getNumOperands(); op != e; ++op) |
| 740 | if (MI.getOperand(op).isCPI()) { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 741 | // We found one. The addressing mode tells us the max displacement |
| 742 | // from the PC that this instruction permits. |
| 743 | |
| 744 | // Basic size info comes from the TSFlags field. |
| 745 | unsigned Bits = 0; |
| 746 | unsigned Scale = 1; |
| 747 | bool NegOk = false; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 748 | unsigned LongFormBits = 0; |
| 749 | unsigned LongFormScale = 0; |
| 750 | unsigned LongFormOpcode = 0; |
| 751 | switch (Opc) { |
| 752 | default: |
| 753 | llvm_unreachable("Unknown addressing mode for CP reference!"); |
| 754 | case Mips::LwRxPcTcp16: |
| 755 | Bits = 8; |
Reed Kotler | 3d7b33f | 2013-11-06 04:29:52 +0000 | [diff] [blame] | 756 | Scale = 4; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 757 | LongFormOpcode = Mips::LwRxPcTcpX16; |
Reed Kotler | 43788a2 | 2014-01-16 00:47:46 +0000 | [diff] [blame] | 758 | LongFormBits = 14; |
Reed Kotler | 45c5927 | 2013-11-10 00:09:26 +0000 | [diff] [blame] | 759 | LongFormScale = 1; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 760 | break; |
| 761 | case Mips::LwRxPcTcpX16: |
Reed Kotler | 43788a2 | 2014-01-16 00:47:46 +0000 | [diff] [blame] | 762 | Bits = 14; |
Reed Kotler | 3d7b33f | 2013-11-06 04:29:52 +0000 | [diff] [blame] | 763 | Scale = 1; |
| 764 | NegOk = true; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 765 | break; |
| 766 | } |
| 767 | // Remember that this is a user of a CP entry. |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 768 | unsigned CPI = MI.getOperand(op).getIndex(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 769 | MachineInstr *CPEMI = CPEMIs[CPI]; |
| 770 | unsigned MaxOffs = ((1 << Bits)-1) * Scale; |
| 771 | unsigned LongFormMaxOffs = ((1 << LongFormBits)-1) * LongFormScale; |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 772 | CPUsers.push_back(CPUser(&MI, CPEMI, MaxOffs, NegOk, LongFormMaxOffs, |
| 773 | LongFormOpcode)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 774 | |
| 775 | // Increment corresponding CPEntry reference count. |
| 776 | CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); |
| 777 | assert(CPE && "Cannot find a corresponding CPEntry!"); |
| 778 | CPE->RefCount++; |
| 779 | |
| 780 | // Instructions can only use one CP entry, don't bother scanning the |
| 781 | // rest of the operands. |
| 782 | break; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 783 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 784 | } |
| 785 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /// computeBlockSize - Compute the size and some alignment information for MBB. |
| 789 | /// This function updates BBInfo directly. |
| 790 | void MipsConstantIslands::computeBlockSize(MachineBasicBlock *MBB) { |
| 791 | BasicBlockInfo &BBI = BBInfo[MBB->getNumber()]; |
| 792 | BBI.Size = 0; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 793 | |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 794 | for (const MachineInstr &MI : *MBB) |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 795 | BBI.Size += TII->getInstSizeInBytes(MI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /// getOffsetOf - Return the current offset of the specified machine instruction |
| 799 | /// from the start of the function. This offset changes as stuff is moved |
| 800 | /// around inside the function. |
| 801 | unsigned MipsConstantIslands::getOffsetOf(MachineInstr *MI) const { |
| 802 | MachineBasicBlock *MBB = MI->getParent(); |
| 803 | |
| 804 | // The offset is composed of two things: the sum of the sizes of all MBB's |
| 805 | // before this instruction's block, and the offset from the start of the block |
| 806 | // it is in. |
| 807 | unsigned Offset = BBInfo[MBB->getNumber()].Offset; |
| 808 | |
| 809 | // Sum instructions before MI in MBB. |
| 810 | for (MachineBasicBlock::iterator I = MBB->begin(); &*I != MI; ++I) { |
| 811 | assert(I != MBB->end() && "Didn't find MI in its own basic block?"); |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 812 | Offset += TII->getInstSizeInBytes(*I); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 813 | } |
| 814 | return Offset; |
| 815 | } |
| 816 | |
| 817 | /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB |
| 818 | /// ID. |
| 819 | static bool CompareMBBNumbers(const MachineBasicBlock *LHS, |
| 820 | const MachineBasicBlock *RHS) { |
| 821 | return LHS->getNumber() < RHS->getNumber(); |
| 822 | } |
| 823 | |
| 824 | /// updateForInsertedWaterBlock - When a block is newly inserted into the |
| 825 | /// machine function, it upsets all of the block numbers. Renumber the blocks |
| 826 | /// and update the arrays that parallel this numbering. |
| 827 | void MipsConstantIslands::updateForInsertedWaterBlock |
| 828 | (MachineBasicBlock *NewBB) { |
| 829 | // Renumber the MBB's to keep them consecutive. |
| 830 | NewBB->getParent()->RenumberBlocks(NewBB); |
| 831 | |
| 832 | // Insert an entry into BBInfo to align it properly with the (newly |
| 833 | // renumbered) block numbers. |
| 834 | BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); |
| 835 | |
| 836 | // Next, update WaterList. Specifically, we need to add NewMBB as having |
| 837 | // available water after it. |
Fangrui Song | dc8de60 | 2019-06-21 05:40:31 +0000 | [diff] [blame] | 838 | water_iterator IP = llvm::lower_bound(WaterList, NewBB, CompareMBBNumbers); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 839 | WaterList.insert(IP, NewBB); |
| 840 | } |
| 841 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 842 | unsigned MipsConstantIslands::getUserOffset(CPUser &U) const { |
Reed Kotler | 0eb8739 | 2013-11-05 21:39:57 +0000 | [diff] [blame] | 843 | return getOffsetOf(U.MI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /// Split the basic block containing MI into two blocks, which are joined by |
| 847 | /// an unconditional branch. Update data structures and renumber blocks to |
| 848 | /// account for this change and returns the newly created block. |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 849 | MachineBasicBlock * |
| 850 | MipsConstantIslands::splitBlockBeforeInstr(MachineInstr &MI) { |
| 851 | MachineBasicBlock *OrigBB = MI.getParent(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 852 | |
| 853 | // Create a new MBB for the code after the OrigBB. |
| 854 | MachineBasicBlock *NewBB = |
| 855 | MF->CreateMachineBasicBlock(OrigBB->getBasicBlock()); |
Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 856 | MachineFunction::iterator MBBI = ++OrigBB->getIterator(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 857 | MF->insert(MBBI, NewBB); |
| 858 | |
| 859 | // Splice the instructions starting with MI over to NewBB. |
| 860 | NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end()); |
| 861 | |
| 862 | // Add an unconditional branch from OrigBB to NewBB. |
| 863 | // Note the new unconditional branch is not being recorded. |
| 864 | // There doesn't seem to be meaningful DebugInfo available; this doesn't |
| 865 | // correspond to anything in the source. |
Reed Kotler | f0e6968 | 2013-11-12 02:27:12 +0000 | [diff] [blame] | 866 | BuildMI(OrigBB, DebugLoc(), TII->get(Mips::Bimm16)).addMBB(NewBB); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 867 | ++NumSplit; |
| 868 | |
| 869 | // Update the CFG. All succs of OrigBB are now succs of NewBB. |
| 870 | NewBB->transferSuccessors(OrigBB); |
| 871 | |
| 872 | // OrigBB branches to NewBB. |
| 873 | OrigBB->addSuccessor(NewBB); |
| 874 | |
| 875 | // Update internal data structures to account for the newly inserted MBB. |
| 876 | // This is almost the same as updateForInsertedWaterBlock, except that |
| 877 | // the Water goes after OrigBB, not NewBB. |
| 878 | MF->RenumberBlocks(NewBB); |
| 879 | |
| 880 | // Insert an entry into BBInfo to align it properly with the (newly |
| 881 | // renumbered) block numbers. |
| 882 | BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); |
| 883 | |
| 884 | // Next, update WaterList. Specifically, we need to add OrigMBB as having |
| 885 | // available water after it (but not if it's already there, which happens |
| 886 | // when splitting before a conditional branch that is followed by an |
| 887 | // unconditional branch - in that case we want to insert NewBB). |
Fangrui Song | dc8de60 | 2019-06-21 05:40:31 +0000 | [diff] [blame] | 888 | water_iterator IP = llvm::lower_bound(WaterList, OrigBB, CompareMBBNumbers); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 889 | MachineBasicBlock* WaterBB = *IP; |
| 890 | if (WaterBB == OrigBB) |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 891 | WaterList.insert(std::next(IP), NewBB); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 892 | else |
| 893 | WaterList.insert(IP, OrigBB); |
| 894 | NewWaterList.insert(OrigBB); |
| 895 | |
| 896 | // Figure out how large the OrigBB is. As the first half of the original |
| 897 | // block, it cannot contain a tablejump. The size includes |
| 898 | // the new jump we added. (It should be possible to do this without |
| 899 | // recounting everything, but it's very confusing, and this is rarely |
| 900 | // executed.) |
| 901 | computeBlockSize(OrigBB); |
| 902 | |
| 903 | // Figure out how large the NewMBB is. As the second half of the original |
| 904 | // block, it may contain a tablejump. |
| 905 | computeBlockSize(NewBB); |
| 906 | |
| 907 | // All BBOffsets following these blocks must be modified. |
| 908 | adjustBBOffsetsAfter(OrigBB); |
| 909 | |
| 910 | return NewBB; |
| 911 | } |
| 912 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 913 | /// isOffsetInRange - Checks whether UserOffset (the location of a constant pool |
| 914 | /// reference) is within MaxDisp of TrialOffset (a proposed location of a |
| 915 | /// constant pool entry). |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 916 | bool MipsConstantIslands::isOffsetInRange(unsigned UserOffset, |
| 917 | unsigned TrialOffset, unsigned MaxDisp, |
Reed Kotler | b09ebe9 | 2013-11-05 22:34:29 +0000 | [diff] [blame] | 918 | bool NegativeOK) { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 919 | if (UserOffset <= TrialOffset) { |
| 920 | // User before the Trial. |
| 921 | if (TrialOffset - UserOffset <= MaxDisp) |
| 922 | return true; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 923 | } else if (NegativeOK) { |
| 924 | if (UserOffset - TrialOffset <= MaxDisp) |
| 925 | return true; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 926 | } |
| 927 | return false; |
| 928 | } |
| 929 | |
| 930 | /// isWaterInRange - Returns true if a CPE placed after the specified |
| 931 | /// Water (a basic block) will be in range for the specific MI. |
| 932 | /// |
| 933 | /// Compute how much the function will grow by inserting a CPE after Water. |
| 934 | bool MipsConstantIslands::isWaterInRange(unsigned UserOffset, |
| 935 | MachineBasicBlock* Water, CPUser &U, |
| 936 | unsigned &Growth) { |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame] | 937 | unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(); |
Guillaume Chatelet | af11cc7 | 2019-09-12 15:20:36 +0000 | [diff] [blame] | 938 | unsigned NextBlockOffset; |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 939 | Align NextBlockAlignment; |
Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 940 | MachineFunction::const_iterator NextBlock = ++Water->getIterator(); |
| 941 | if (NextBlock == MF->end()) { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 942 | NextBlockOffset = BBInfo[Water->getNumber()].postOffset(); |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 943 | NextBlockAlignment = Align::None(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 944 | } else { |
| 945 | NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset; |
Guillaume Chatelet | af11cc7 | 2019-09-12 15:20:36 +0000 | [diff] [blame] | 946 | NextBlockAlignment = NextBlock->getAlignment(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 947 | } |
| 948 | unsigned Size = U.CPEMI->getOperand(2).getImm(); |
| 949 | unsigned CPEEnd = CPEOffset + Size; |
| 950 | |
| 951 | // The CPE may be able to hide in the alignment padding before the next |
| 952 | // block. It may also cause more padding to be required if it is more aligned |
| 953 | // that the next block. |
| 954 | if (CPEEnd > NextBlockOffset) { |
| 955 | Growth = CPEEnd - NextBlockOffset; |
| 956 | // Compute the padding that would go at the end of the CPE to align the next |
| 957 | // block. |
Guillaume Chatelet | af11cc7 | 2019-09-12 15:20:36 +0000 | [diff] [blame] | 958 | Growth += offsetToAlignment(CPEEnd, NextBlockAlignment); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 959 | |
| 960 | // If the CPE is to be inserted before the instruction, that will raise |
| 961 | // the offset of the instruction. Also account for unknown alignment padding |
| 962 | // in blocks between CPE and the user. |
| 963 | if (CPEOffset < UserOffset) |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 964 | UserOffset += Growth; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 965 | } else |
| 966 | // CPE fits in existing padding. |
| 967 | Growth = 0; |
| 968 | |
| 969 | return isOffsetInRange(UserOffset, CPEOffset, U); |
| 970 | } |
| 971 | |
| 972 | /// isCPEntryInRange - Returns true if the distance between specific MI and |
| 973 | /// specific ConstPool entry instruction can fit in MI's displacement field. |
| 974 | bool MipsConstantIslands::isCPEntryInRange |
| 975 | (MachineInstr *MI, unsigned UserOffset, |
| 976 | MachineInstr *CPEMI, unsigned MaxDisp, |
| 977 | bool NegOk, bool DoDump) { |
| 978 | unsigned CPEOffset = getOffsetOf(CPEMI); |
| 979 | |
| 980 | if (DoDump) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 981 | LLVM_DEBUG({ |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 982 | unsigned Block = MI->getParent()->getNumber(); |
| 983 | const BasicBlockInfo &BBI = BBInfo[Block]; |
| 984 | dbgs() << "User of CPE#" << CPEMI->getOperand(0).getImm() |
| 985 | << " max delta=" << MaxDisp |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 986 | << format(" insn address=%#x", UserOffset) << " in " |
| 987 | << printMBBReference(*MI->getParent()) << ": " |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 988 | << format("%#x-%x\t", BBI.Offset, BBI.postOffset()) << *MI |
| 989 | << format("CPE address=%#x offset=%+d: ", CPEOffset, |
Francis Visoiu Mistrih | 25528d6 | 2017-12-04 17:18:51 +0000 | [diff] [blame] | 990 | int(CPEOffset - UserOffset)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 991 | }); |
| 992 | } |
| 993 | |
| 994 | return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk); |
| 995 | } |
| 996 | |
| 997 | #ifndef NDEBUG |
| 998 | /// BBIsJumpedOver - Return true of the specified basic block's only predecessor |
| 999 | /// unconditionally branches to its only successor. |
| 1000 | static bool BBIsJumpedOver(MachineBasicBlock *MBB) { |
| 1001 | if (MBB->pred_size() != 1 || MBB->succ_size() != 1) |
| 1002 | return false; |
| 1003 | MachineBasicBlock *Succ = *MBB->succ_begin(); |
| 1004 | MachineBasicBlock *Pred = *MBB->pred_begin(); |
| 1005 | MachineInstr *PredMI = &Pred->back(); |
Reed Kotler | f0e6968 | 2013-11-12 02:27:12 +0000 | [diff] [blame] | 1006 | if (PredMI->getOpcode() == Mips::Bimm16) |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1007 | return PredMI->getOperand(0).getMBB() == Succ; |
| 1008 | return false; |
| 1009 | } |
| 1010 | #endif |
| 1011 | |
| 1012 | void MipsConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock *BB) { |
| 1013 | unsigned BBNum = BB->getNumber(); |
| 1014 | for(unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) { |
| 1015 | // Get the offset and known bits at the end of the layout predecessor. |
| 1016 | // Include the alignment of the current block. |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 1017 | unsigned Offset = BBInfo[i - 1].Offset + BBInfo[i - 1].Size; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1018 | BBInfo[i].Offset = Offset; |
| 1019 | } |
| 1020 | } |
| 1021 | |
| 1022 | /// decrementCPEReferenceCount - find the constant pool entry with index CPI |
| 1023 | /// and instruction CPEMI, and decrement its refcount. If the refcount |
| 1024 | /// becomes 0 remove the entry and instruction. Returns true if we removed |
| 1025 | /// the entry, false if we didn't. |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1026 | bool MipsConstantIslands::decrementCPEReferenceCount(unsigned CPI, |
| 1027 | MachineInstr *CPEMI) { |
| 1028 | // Find the old entry. Eliminate it if it is no longer used. |
| 1029 | CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); |
| 1030 | assert(CPE && "Unexpected!"); |
| 1031 | if (--CPE->RefCount == 0) { |
| 1032 | removeDeadCPEMI(CPEMI); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1033 | CPE->CPEMI = nullptr; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1034 | --NumCPEs; |
| 1035 | return true; |
| 1036 | } |
| 1037 | return false; |
| 1038 | } |
| 1039 | |
| 1040 | /// LookForCPEntryInRange - see if the currently referenced CPE is in range; |
| 1041 | /// if not, see if an in-range clone of the CPE is in range, and if so, |
| 1042 | /// change the data structures so the user references the clone. Returns: |
| 1043 | /// 0 = no existing entry found |
| 1044 | /// 1 = entry found, and there were no code insertions or deletions |
| 1045 | /// 2 = entry found, and there were code insertions or deletions |
| 1046 | int MipsConstantIslands::findInRangeCPEntry(CPUser& U, unsigned UserOffset) |
| 1047 | { |
| 1048 | MachineInstr *UserMI = U.MI; |
| 1049 | MachineInstr *CPEMI = U.CPEMI; |
| 1050 | |
| 1051 | // Check to see if the CPE is already in-range. |
| 1052 | if (isCPEntryInRange(UserMI, UserOffset, CPEMI, U.getMaxDisp(), U.NegOk, |
| 1053 | true)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1054 | LLVM_DEBUG(dbgs() << "In range\n"); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1055 | return 1; |
| 1056 | } |
| 1057 | |
| 1058 | // No. Look for previously created clones of the CPE that are in range. |
| 1059 | unsigned CPI = CPEMI->getOperand(1).getIndex(); |
| 1060 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 1061 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 1062 | // We already tried this one |
| 1063 | if (CPEs[i].CPEMI == CPEMI) |
| 1064 | continue; |
| 1065 | // Removing CPEs can leave empty entries, skip |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1066 | if (CPEs[i].CPEMI == nullptr) |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1067 | continue; |
| 1068 | if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getMaxDisp(), |
| 1069 | U.NegOk)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1070 | LLVM_DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#" |
| 1071 | << CPEs[i].CPI << "\n"); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1072 | // Point the CPUser node to the replacement |
| 1073 | U.CPEMI = CPEs[i].CPEMI; |
| 1074 | // Change the CPI in the instruction operand to refer to the clone. |
| 1075 | for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j) |
| 1076 | if (UserMI->getOperand(j).isCPI()) { |
| 1077 | UserMI->getOperand(j).setIndex(CPEs[i].CPI); |
| 1078 | break; |
| 1079 | } |
| 1080 | // Adjust the refcount of the clone... |
| 1081 | CPEs[i].RefCount++; |
| 1082 | // ...and the original. If we didn't remove the old entry, none of the |
| 1083 | // addresses changed, so we don't need another pass. |
| 1084 | return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1; |
| 1085 | } |
| 1086 | } |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
| 1090 | /// LookForCPEntryInRange - see if the currently referenced CPE is in range; |
| 1091 | /// This version checks if the longer form of the instruction can be used to |
| 1092 | /// to satisfy things. |
| 1093 | /// if not, see if an in-range clone of the CPE is in range, and if so, |
| 1094 | /// change the data structures so the user references the clone. Returns: |
| 1095 | /// 0 = no existing entry found |
| 1096 | /// 1 = entry found, and there were no code insertions or deletions |
| 1097 | /// 2 = entry found, and there were code insertions or deletions |
| 1098 | int MipsConstantIslands::findLongFormInRangeCPEntry |
| 1099 | (CPUser& U, unsigned UserOffset) |
| 1100 | { |
| 1101 | MachineInstr *UserMI = U.MI; |
| 1102 | MachineInstr *CPEMI = U.CPEMI; |
| 1103 | |
| 1104 | // Check to see if the CPE is already in-range. |
| 1105 | if (isCPEntryInRange(UserMI, UserOffset, CPEMI, |
| 1106 | U.getLongFormMaxDisp(), U.NegOk, |
| 1107 | true)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1108 | LLVM_DEBUG(dbgs() << "In range\n"); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1109 | UserMI->setDesc(TII->get(U.getLongFormOpcode())); |
Reed Kotler | 45c5927 | 2013-11-10 00:09:26 +0000 | [diff] [blame] | 1110 | U.setMaxDisp(U.getLongFormMaxDisp()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1111 | return 2; // instruction is longer length now |
| 1112 | } |
| 1113 | |
| 1114 | // No. Look for previously created clones of the CPE that are in range. |
| 1115 | unsigned CPI = CPEMI->getOperand(1).getIndex(); |
| 1116 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 1117 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 1118 | // We already tried this one |
| 1119 | if (CPEs[i].CPEMI == CPEMI) |
| 1120 | continue; |
| 1121 | // Removing CPEs can leave empty entries, skip |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1122 | if (CPEs[i].CPEMI == nullptr) |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1123 | continue; |
| 1124 | if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, |
| 1125 | U.getLongFormMaxDisp(), U.NegOk)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1126 | LLVM_DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#" |
| 1127 | << CPEs[i].CPI << "\n"); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1128 | // Point the CPUser node to the replacement |
| 1129 | U.CPEMI = CPEs[i].CPEMI; |
| 1130 | // Change the CPI in the instruction operand to refer to the clone. |
| 1131 | for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j) |
| 1132 | if (UserMI->getOperand(j).isCPI()) { |
| 1133 | UserMI->getOperand(j).setIndex(CPEs[i].CPI); |
| 1134 | break; |
| 1135 | } |
| 1136 | // Adjust the refcount of the clone... |
| 1137 | CPEs[i].RefCount++; |
| 1138 | // ...and the original. If we didn't remove the old entry, none of the |
| 1139 | // addresses changed, so we don't need another pass. |
| 1140 | return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1; |
| 1141 | } |
| 1142 | } |
| 1143 | return 0; |
| 1144 | } |
| 1145 | |
| 1146 | /// getUnconditionalBrDisp - Returns the maximum displacement that can fit in |
| 1147 | /// the specific unconditional branch instruction. |
| 1148 | static inline unsigned getUnconditionalBrDisp(int Opc) { |
| 1149 | switch (Opc) { |
Reed Kotler | f0e6968 | 2013-11-12 02:27:12 +0000 | [diff] [blame] | 1150 | case Mips::Bimm16: |
| 1151 | return ((1<<10)-1)*2; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1152 | case Mips::BimmX16: |
| 1153 | return ((1<<16)-1)*2; |
| 1154 | default: |
| 1155 | break; |
| 1156 | } |
| 1157 | return ((1<<16)-1)*2; |
| 1158 | } |
| 1159 | |
| 1160 | /// findAvailableWater - Look for an existing entry in the WaterList in which |
| 1161 | /// we can place the CPE referenced from U so it's within range of U's MI. |
| 1162 | /// Returns true if found, false if not. If it returns true, WaterIter |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1163 | /// is set to the WaterList entry. |
Reed Kotler | 4d0313d | 2013-11-05 12:04:37 +0000 | [diff] [blame] | 1164 | /// To ensure that this pass |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1165 | /// terminates, the CPE location for a particular CPUser is only allowed to |
| 1166 | /// move to a lower address, so search backward from the end of the list and |
| 1167 | /// prefer the first water that is in range. |
| 1168 | bool MipsConstantIslands::findAvailableWater(CPUser &U, unsigned UserOffset, |
| 1169 | water_iterator &WaterIter) { |
| 1170 | if (WaterList.empty()) |
| 1171 | return false; |
| 1172 | |
| 1173 | unsigned BestGrowth = ~0u; |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1174 | for (water_iterator IP = std::prev(WaterList.end()), B = WaterList.begin();; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1175 | --IP) { |
| 1176 | MachineBasicBlock* WaterBB = *IP; |
| 1177 | // Check if water is in range and is either at a lower address than the |
| 1178 | // current "high water mark" or a new water block that was created since |
| 1179 | // the previous iteration by inserting an unconditional branch. In the |
| 1180 | // latter case, we want to allow resetting the high water mark back to |
| 1181 | // this new water since we haven't seen it before. Inserting branches |
| 1182 | // should be relatively uncommon and when it does happen, we want to be |
| 1183 | // sure to take advantage of it for all the CPEs near that block, so that |
| 1184 | // we don't insert more branches than necessary. |
| 1185 | unsigned Growth; |
| 1186 | if (isWaterInRange(UserOffset, WaterBB, U, Growth) && |
| 1187 | (WaterBB->getNumber() < U.HighWaterMark->getNumber() || |
| 1188 | NewWaterList.count(WaterBB)) && Growth < BestGrowth) { |
| 1189 | // This is the least amount of required padding seen so far. |
| 1190 | BestGrowth = Growth; |
| 1191 | WaterIter = IP; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1192 | LLVM_DEBUG(dbgs() << "Found water after " << printMBBReference(*WaterBB) |
| 1193 | << " Growth=" << Growth << '\n'); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1194 | |
| 1195 | // Keep looking unless it is perfect. |
| 1196 | if (BestGrowth == 0) |
| 1197 | return true; |
| 1198 | } |
| 1199 | if (IP == B) |
| 1200 | break; |
| 1201 | } |
| 1202 | return BestGrowth != ~0u; |
| 1203 | } |
| 1204 | |
| 1205 | /// createNewWater - No existing WaterList entry will work for |
| 1206 | /// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the |
| 1207 | /// block is used if in range, and the conditional branch munged so control |
| 1208 | /// flow is correct. Otherwise the block is split to create a hole with an |
| 1209 | /// unconditional branch around it. In either case NewMBB is set to a |
| 1210 | /// block following which the new island can be inserted (the WaterList |
| 1211 | /// is not adjusted). |
| 1212 | void MipsConstantIslands::createNewWater(unsigned CPUserIndex, |
| 1213 | unsigned UserOffset, |
| 1214 | MachineBasicBlock *&NewMBB) { |
| 1215 | CPUser &U = CPUsers[CPUserIndex]; |
| 1216 | MachineInstr *UserMI = U.MI; |
| 1217 | MachineInstr *CPEMI = U.CPEMI; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1218 | MachineBasicBlock *UserMBB = UserMI->getParent(); |
| 1219 | const BasicBlockInfo &UserBBI = BBInfo[UserMBB->getNumber()]; |
| 1220 | |
| 1221 | // If the block does not end in an unconditional branch already, and if the |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1222 | // end of the block is within range, make new water there. |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1223 | if (BBHasFallthrough(UserMBB)) { |
| 1224 | // Size of branch to insert. |
| 1225 | unsigned Delta = 2; |
| 1226 | // Compute the offset where the CPE will begin. |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame] | 1227 | unsigned CPEOffset = UserBBI.postOffset() + Delta; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1228 | |
| 1229 | if (isOffsetInRange(UserOffset, CPEOffset, U)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1230 | LLVM_DEBUG(dbgs() << "Split at end of " << printMBBReference(*UserMBB) |
| 1231 | << format(", expected CPE offset %#x\n", CPEOffset)); |
Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 1232 | NewMBB = &*++UserMBB->getIterator(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1233 | // Add an unconditional branch from UserMBB to fallthrough block. Record |
| 1234 | // it for branch lengthening; this new branch will not get out of range, |
| 1235 | // but if the preceding conditional branch is out of range, the targets |
| 1236 | // will be exchanged, and the altered branch may be out of range, so the |
| 1237 | // machinery has to know about it. |
Reed Kotler | f0e6968 | 2013-11-12 02:27:12 +0000 | [diff] [blame] | 1238 | int UncondBr = Mips::Bimm16; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1239 | BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB); |
| 1240 | unsigned MaxDisp = getUnconditionalBrDisp(UncondBr); |
| 1241 | ImmBranches.push_back(ImmBranch(&UserMBB->back(), |
| 1242 | MaxDisp, false, UncondBr)); |
| 1243 | BBInfo[UserMBB->getNumber()].Size += Delta; |
| 1244 | adjustBBOffsetsAfter(UserMBB); |
| 1245 | return; |
| 1246 | } |
| 1247 | } |
| 1248 | |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1249 | // What a big block. Find a place within the block to split it. |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1250 | |
| 1251 | // Try to split the block so it's fully aligned. Compute the latest split |
| 1252 | // point where we can add a 4-byte branch instruction, and then align to |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame] | 1253 | // Align which is the largest possible alignment in the function. |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 1254 | const Align Align = MF->getAlignment(); |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 1255 | unsigned BaseInsertOffset = UserOffset + U.getMaxDisp(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1256 | LLVM_DEBUG(dbgs() << format("Split in middle of big block before %#x", |
| 1257 | BaseInsertOffset)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1258 | |
| 1259 | // The 4 in the following is for the unconditional branch we'll be inserting |
Reed Kotler | 4d0313d | 2013-11-05 12:04:37 +0000 | [diff] [blame] | 1260 | // Alignment of the island is handled |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1261 | // inside isOffsetInRange. |
| 1262 | BaseInsertOffset -= 4; |
| 1263 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1264 | LLVM_DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset) |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame] | 1265 | << " la=" << Log2(Align) << '\n'); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1266 | |
| 1267 | // This could point off the end of the block if we've already got constant |
| 1268 | // pool entries following this block; only the last one is in the water list. |
| 1269 | // Back past any possible branches (allow for a conditional and a maximally |
| 1270 | // long unconditional). |
| 1271 | if (BaseInsertOffset + 8 >= UserBBI.postOffset()) { |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 1272 | BaseInsertOffset = UserBBI.postOffset() - 8; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1273 | LLVM_DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1274 | } |
Reed Kotler | 7ded5b6 | 2013-11-05 23:36:58 +0000 | [diff] [blame] | 1275 | unsigned EndInsertOffset = BaseInsertOffset + 4 + |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1276 | CPEMI->getOperand(2).getImm(); |
| 1277 | MachineBasicBlock::iterator MI = UserMI; |
| 1278 | ++MI; |
| 1279 | unsigned CPUIndex = CPUserIndex+1; |
| 1280 | unsigned NumCPUsers = CPUsers.size(); |
| 1281 | //MachineInstr *LastIT = 0; |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1282 | for (unsigned Offset = UserOffset + TII->getInstSizeInBytes(*UserMI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1283 | Offset < BaseInsertOffset; |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1284 | Offset += TII->getInstSizeInBytes(*MI), MI = std::next(MI)) { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1285 | assert(MI != UserMBB->end() && "Fell off end of block"); |
Duncan P. N. Exon Smith | f197b1f | 2016-08-12 05:05:36 +0000 | [diff] [blame] | 1286 | if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1287 | CPUser &U = CPUsers[CPUIndex]; |
| 1288 | if (!isOffsetInRange(Offset, EndInsertOffset, U)) { |
| 1289 | // Shift intertion point by one unit of alignment so it is within reach. |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame] | 1290 | BaseInsertOffset -= Align.value(); |
| 1291 | EndInsertOffset -= Align.value(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1292 | } |
| 1293 | // This is overly conservative, as we don't account for CPEMIs being |
| 1294 | // reused within the block, but it doesn't matter much. Also assume CPEs |
| 1295 | // are added in order with alignment padding. We may eventually be able |
| 1296 | // to pack the aligned CPEs better. |
| 1297 | EndInsertOffset += U.CPEMI->getOperand(2).getImm(); |
| 1298 | CPUIndex++; |
| 1299 | } |
| 1300 | } |
| 1301 | |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 1302 | NewMBB = splitBlockBeforeInstr(*--MI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | /// handleConstantPoolUser - Analyze the specified user, checking to see if it |
| 1306 | /// is out-of-range. If so, pick up the constant pool value and move it some |
| 1307 | /// place in-range. Return true if we changed any addresses (thus must run |
| 1308 | /// another pass of branch lengthening), false otherwise. |
| 1309 | bool MipsConstantIslands::handleConstantPoolUser(unsigned CPUserIndex) { |
| 1310 | CPUser &U = CPUsers[CPUserIndex]; |
| 1311 | MachineInstr *UserMI = U.MI; |
| 1312 | MachineInstr *CPEMI = U.CPEMI; |
| 1313 | unsigned CPI = CPEMI->getOperand(1).getIndex(); |
| 1314 | unsigned Size = CPEMI->getOperand(2).getImm(); |
| 1315 | // Compute this only once, it's expensive. |
| 1316 | unsigned UserOffset = getUserOffset(U); |
| 1317 | |
| 1318 | // See if the current entry is within range, or there is a clone of it |
| 1319 | // in range. |
| 1320 | int result = findInRangeCPEntry(U, UserOffset); |
| 1321 | if (result==1) return false; |
| 1322 | else if (result==2) return true; |
| 1323 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1324 | // Look for water where we can place this CPE. |
| 1325 | MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock(); |
| 1326 | MachineBasicBlock *NewMBB; |
| 1327 | water_iterator IP; |
| 1328 | if (findAvailableWater(U, UserOffset, IP)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1329 | LLVM_DEBUG(dbgs() << "Found water in range\n"); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1330 | MachineBasicBlock *WaterBB = *IP; |
| 1331 | |
| 1332 | // If the original WaterList entry was "new water" on this iteration, |
| 1333 | // propagate that to the new island. This is just keeping NewWaterList |
| 1334 | // updated to match the WaterList, which will be updated below. |
| 1335 | if (NewWaterList.erase(WaterBB)) |
| 1336 | NewWaterList.insert(NewIsland); |
| 1337 | |
| 1338 | // The new CPE goes before the following block (NewMBB). |
Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 1339 | NewMBB = &*++WaterBB->getIterator(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1340 | } else { |
| 1341 | // No water found. |
| 1342 | // we first see if a longer form of the instrucion could have reached |
| 1343 | // the constant. in that case we won't bother to split |
Reed Kotler | 45c5927 | 2013-11-10 00:09:26 +0000 | [diff] [blame] | 1344 | if (!NoLoadRelaxation) { |
| 1345 | result = findLongFormInRangeCPEntry(U, UserOffset); |
| 1346 | if (result != 0) return true; |
| 1347 | } |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1348 | LLVM_DEBUG(dbgs() << "No water found\n"); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1349 | createNewWater(CPUserIndex, UserOffset, NewMBB); |
| 1350 | |
| 1351 | // splitBlockBeforeInstr adds to WaterList, which is important when it is |
| 1352 | // called while handling branches so that the water will be seen on the |
| 1353 | // next iteration for constant pools, but in this context, we don't want |
| 1354 | // it. Check for this so it will be removed from the WaterList. |
| 1355 | // Also remove any entry from NewWaterList. |
Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 1356 | MachineBasicBlock *WaterBB = &*--NewMBB->getIterator(); |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 1357 | IP = llvm::find(WaterList, WaterBB); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1358 | if (IP != WaterList.end()) |
| 1359 | NewWaterList.erase(WaterBB); |
| 1360 | |
| 1361 | // We are adding new water. Update NewWaterList. |
| 1362 | NewWaterList.insert(NewIsland); |
| 1363 | } |
| 1364 | |
| 1365 | // Remove the original WaterList entry; we want subsequent insertions in |
| 1366 | // this vicinity to go after the one we're about to insert. This |
| 1367 | // considerably reduces the number of times we have to move the same CPE |
| 1368 | // more than once and is also important to ensure the algorithm terminates. |
| 1369 | if (IP != WaterList.end()) |
| 1370 | WaterList.erase(IP); |
| 1371 | |
| 1372 | // Okay, we know we can put an island before NewMBB now, do it! |
Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 1373 | MF->insert(NewMBB->getIterator(), NewIsland); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1374 | |
| 1375 | // Update internal data structures to account for the newly inserted MBB. |
| 1376 | updateForInsertedWaterBlock(NewIsland); |
| 1377 | |
| 1378 | // Decrement the old entry, and remove it if refcount becomes 0. |
| 1379 | decrementCPEReferenceCount(CPI, CPEMI); |
| 1380 | |
Reed Kotler | d3b28eb | 2013-11-24 02:53:09 +0000 | [diff] [blame] | 1381 | // No existing clone of this CPE is within range. |
| 1382 | // We will be generating a new clone. Get a UID for it. |
| 1383 | unsigned ID = createPICLabelUId(); |
| 1384 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1385 | // Now that we have an island to add the CPE to, clone the original CPE and |
| 1386 | // add it to the island. |
| 1387 | U.HighWaterMark = NewIsland; |
| 1388 | U.CPEMI = BuildMI(NewIsland, DebugLoc(), TII->get(Mips::CONSTPOOL_ENTRY)) |
| 1389 | .addImm(ID).addConstantPoolIndex(CPI).addImm(Size); |
| 1390 | CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1)); |
| 1391 | ++NumCPEs; |
| 1392 | |
| 1393 | // Mark the basic block as aligned as required by the const-pool entry. |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame] | 1394 | NewIsland->setAlignment(getCPEAlign(*U.CPEMI)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1395 | |
| 1396 | // Increase the size of the island block to account for the new entry. |
| 1397 | BBInfo[NewIsland->getNumber()].Size += Size; |
Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 1398 | adjustBBOffsetsAfter(&*--NewIsland->getIterator()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1399 | |
| 1400 | // Finally, change the CPI in the instruction operand to be ID. |
| 1401 | for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i) |
| 1402 | if (UserMI->getOperand(i).isCPI()) { |
| 1403 | UserMI->getOperand(i).setIndex(ID); |
| 1404 | break; |
| 1405 | } |
| 1406 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1407 | LLVM_DEBUG( |
| 1408 | dbgs() << " Moved CPE to #" << ID << " CPI=" << CPI |
| 1409 | << format(" offset=%#x\n", BBInfo[NewIsland->getNumber()].Offset)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1410 | |
| 1411 | return true; |
| 1412 | } |
| 1413 | |
| 1414 | /// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update |
| 1415 | /// sizes and offsets of impacted basic blocks. |
| 1416 | void MipsConstantIslands::removeDeadCPEMI(MachineInstr *CPEMI) { |
| 1417 | MachineBasicBlock *CPEBB = CPEMI->getParent(); |
| 1418 | unsigned Size = CPEMI->getOperand(2).getImm(); |
| 1419 | CPEMI->eraseFromParent(); |
| 1420 | BBInfo[CPEBB->getNumber()].Size -= Size; |
| 1421 | // All succeeding offsets have the current size value added in, fix this. |
| 1422 | if (CPEBB->empty()) { |
| 1423 | BBInfo[CPEBB->getNumber()].Size = 0; |
| 1424 | |
| 1425 | // This block no longer needs to be aligned. |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 1426 | CPEBB->setAlignment(Align(1)); |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame] | 1427 | } else { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1428 | // Entries are sorted by descending alignment, so realign from the front. |
Guillaume Chatelet | d4c4671 | 2019-09-18 15:49:49 +0000 | [diff] [blame] | 1429 | CPEBB->setAlignment(getCPEAlign(*CPEBB->begin())); |
| 1430 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1431 | |
| 1432 | adjustBBOffsetsAfter(CPEBB); |
| 1433 | // An island has only one predecessor BB and one successor BB. Check if |
| 1434 | // this BB's predecessor jumps directly to this BB's successor. This |
| 1435 | // shouldn't happen currently. |
| 1436 | assert(!BBIsJumpedOver(CPEBB) && "How did this happen?"); |
| 1437 | // FIXME: remove the empty blocks after all the work is done? |
| 1438 | } |
| 1439 | |
| 1440 | /// removeUnusedCPEntries - Remove constant pool entries whose refcounts |
| 1441 | /// are zero. |
| 1442 | bool MipsConstantIslands::removeUnusedCPEntries() { |
| 1443 | unsigned MadeChange = false; |
| 1444 | for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) { |
| 1445 | std::vector<CPEntry> &CPEs = CPEntries[i]; |
| 1446 | for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) { |
| 1447 | if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) { |
| 1448 | removeDeadCPEMI(CPEs[j].CPEMI); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 1449 | CPEs[j].CPEMI = nullptr; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1450 | MadeChange = true; |
| 1451 | } |
| 1452 | } |
| 1453 | } |
| 1454 | return MadeChange; |
| 1455 | } |
| 1456 | |
| 1457 | /// isBBInRange - Returns true if the distance between specific MI and |
| 1458 | /// specific BB can fit in MI's displacement field. |
| 1459 | bool MipsConstantIslands::isBBInRange |
| 1460 | (MachineInstr *MI,MachineBasicBlock *DestBB, unsigned MaxDisp) { |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 1461 | unsigned PCAdj = 4; |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1462 | unsigned BrOffset = getOffsetOf(MI) + PCAdj; |
| 1463 | unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset; |
| 1464 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1465 | LLVM_DEBUG(dbgs() << "Branch of destination " << printMBBReference(*DestBB) |
| 1466 | << " from " << printMBBReference(*MI->getParent()) |
| 1467 | << " max delta=" << MaxDisp << " from " << getOffsetOf(MI) |
| 1468 | << " to " << DestOffset << " offset " |
| 1469 | << int(DestOffset - BrOffset) << "\t" << *MI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1470 | |
| 1471 | if (BrOffset <= DestOffset) { |
| 1472 | // Branch before the Dest. |
| 1473 | if (DestOffset-BrOffset <= MaxDisp) |
| 1474 | return true; |
| 1475 | } else { |
| 1476 | if (BrOffset-DestOffset <= MaxDisp) |
| 1477 | return true; |
| 1478 | } |
| 1479 | return false; |
| 1480 | } |
| 1481 | |
| 1482 | /// fixupImmediateBr - Fix up an immediate branch whose destination is too far |
| 1483 | /// away to fit in its displacement field. |
| 1484 | bool MipsConstantIslands::fixupImmediateBr(ImmBranch &Br) { |
| 1485 | MachineInstr *MI = Br.MI; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 1486 | unsigned TargetOperand = branchTargetOperand(MI); |
| 1487 | MachineBasicBlock *DestBB = MI->getOperand(TargetOperand).getMBB(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1488 | |
| 1489 | // Check to see if the DestBB is already in-range. |
| 1490 | if (isBBInRange(MI, DestBB, Br.MaxDisp)) |
| 1491 | return false; |
| 1492 | |
| 1493 | if (!Br.isCond) |
| 1494 | return fixupUnconditionalBr(Br); |
| 1495 | return fixupConditionalBr(Br); |
| 1496 | } |
| 1497 | |
| 1498 | /// fixupUnconditionalBr - Fix up an unconditional branch whose destination is |
| 1499 | /// too far away to fit in its displacement field. If the LR register has been |
| 1500 | /// spilled in the epilogue, then we can use BL to implement a far jump. |
| 1501 | /// Otherwise, add an intermediate branch instruction to a branch. |
| 1502 | bool |
| 1503 | MipsConstantIslands::fixupUnconditionalBr(ImmBranch &Br) { |
| 1504 | MachineInstr *MI = Br.MI; |
| 1505 | MachineBasicBlock *MBB = MI->getParent(); |
Reed Kotler | 2fc05be | 2013-11-21 05:13:23 +0000 | [diff] [blame] | 1506 | MachineBasicBlock *DestBB = MI->getOperand(0).getMBB(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1507 | // Use BL to implement far jump. |
Reed Kotler | 2fc05be | 2013-11-21 05:13:23 +0000 | [diff] [blame] | 1508 | unsigned BimmX16MaxDisp = ((1 << 16)-1) * 2; |
| 1509 | if (isBBInRange(MI, DestBB, BimmX16MaxDisp)) { |
| 1510 | Br.MaxDisp = BimmX16MaxDisp; |
| 1511 | MI->setDesc(TII->get(Mips::BimmX16)); |
| 1512 | } |
| 1513 | else { |
| 1514 | // need to give the math a more careful look here |
| 1515 | // this is really a segment address and not |
| 1516 | // a PC relative address. FIXME. But I think that |
| 1517 | // just reducing the bits by 1 as I've done is correct. |
| 1518 | // The basic block we are branching too much be longword aligned. |
| 1519 | // we know that RA is saved because we always save it right now. |
| 1520 | // this requirement will be relaxed later but we also have an alternate |
| 1521 | // way to implement this that I will implement that does not need jal. |
| 1522 | // We should have a way to back out this alignment restriction if we "can" later. |
| 1523 | // but it is not harmful. |
| 1524 | // |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 1525 | DestBB->setAlignment(Align(4)); |
Reed Kotler | 2fc05be | 2013-11-21 05:13:23 +0000 | [diff] [blame] | 1526 | Br.MaxDisp = ((1<<24)-1) * 2; |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1527 | MI->setDesc(TII->get(Mips::JalB16)); |
Reed Kotler | 2fc05be | 2013-11-21 05:13:23 +0000 | [diff] [blame] | 1528 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1529 | BBInfo[MBB->getNumber()].Size += 2; |
| 1530 | adjustBBOffsetsAfter(MBB); |
| 1531 | HasFarJump = true; |
| 1532 | ++NumUBrFixed; |
| 1533 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1534 | LLVM_DEBUG(dbgs() << " Changed B to long jump " << *MI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1535 | |
| 1536 | return true; |
| 1537 | } |
| 1538 | |
| 1539 | /// fixupConditionalBr - Fix up a conditional branch whose destination is too |
| 1540 | /// far away to fit in its displacement field. It is converted to an inverse |
| 1541 | /// conditional branch + an unconditional branch to the destination. |
| 1542 | bool |
| 1543 | MipsConstantIslands::fixupConditionalBr(ImmBranch &Br) { |
| 1544 | MachineInstr *MI = Br.MI; |
Reed Kotler | 0d409e2 | 2013-11-28 00:56:37 +0000 | [diff] [blame] | 1545 | unsigned TargetOperand = branchTargetOperand(MI); |
| 1546 | MachineBasicBlock *DestBB = MI->getOperand(TargetOperand).getMBB(); |
| 1547 | unsigned Opcode = MI->getOpcode(); |
| 1548 | unsigned LongFormOpcode = longformBranchOpcode(Opcode); |
| 1549 | unsigned LongFormMaxOff = branchMaxOffsets(LongFormOpcode); |
| 1550 | |
| 1551 | // Check to see if the DestBB is already in-range. |
| 1552 | if (isBBInRange(MI, DestBB, LongFormMaxOff)) { |
| 1553 | Br.MaxDisp = LongFormMaxOff; |
| 1554 | MI->setDesc(TII->get(LongFormOpcode)); |
| 1555 | return true; |
| 1556 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1557 | |
| 1558 | // Add an unconditional branch to the destination and invert the branch |
| 1559 | // condition to jump over it: |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1560 | // bteqz L1 |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1561 | // => |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1562 | // bnez L2 |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1563 | // b L1 |
| 1564 | // L2: |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1565 | |
| 1566 | // If the branch is at the end of its MBB and that has a fall-through block, |
| 1567 | // direct the updated conditional branch to the fall-through block. Otherwise, |
| 1568 | // split the MBB before the next instruction. |
| 1569 | MachineBasicBlock *MBB = MI->getParent(); |
| 1570 | MachineInstr *BMI = &MBB->back(); |
| 1571 | bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB); |
Reed Kotler | 47f3c64a | 2013-12-19 00:43:08 +0000 | [diff] [blame] | 1572 | unsigned OppositeBranchOpcode = TII->getOppositeBranchOpc(Opcode); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1573 | |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1574 | ++NumCBrFixed; |
| 1575 | if (BMI != MI) { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1576 | if (std::next(MachineBasicBlock::iterator(MI)) == std::prev(MBB->end()) && |
Daniel Sanders | f9d8b8c | 2016-05-06 13:23:51 +0000 | [diff] [blame] | 1577 | BMI->isUnconditionalBranch()) { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1578 | // Last MI in the BB is an unconditional branch. Can we simply invert the |
| 1579 | // condition and swap destinations: |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1580 | // beqz L1 |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1581 | // b L2 |
| 1582 | // => |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1583 | // bnez L2 |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1584 | // b L1 |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1585 | unsigned BMITargetOperand = branchTargetOperand(BMI); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 1586 | MachineBasicBlock *NewDest = |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1587 | BMI->getOperand(BMITargetOperand).getMBB(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1588 | if (isBBInRange(MI, NewDest, Br.MaxDisp)) { |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1589 | LLVM_DEBUG( |
| 1590 | dbgs() << " Invert Bcc condition and swap its destination with " |
| 1591 | << *BMI); |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 1592 | MI->setDesc(TII->get(OppositeBranchOpcode)); |
Reed Kotler | ad450f2 | 2013-11-29 22:32:56 +0000 | [diff] [blame] | 1593 | BMI->getOperand(BMITargetOperand).setMBB(DestBB); |
| 1594 | MI->getOperand(TargetOperand).setMBB(NewDest); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1595 | return true; |
| 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | if (NeedSplit) { |
Duncan P. N. Exon Smith | 670900b | 2016-07-15 23:09:47 +0000 | [diff] [blame] | 1601 | splitBlockBeforeInstr(*MI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1602 | // No need for the branch to the next block. We're adding an unconditional |
| 1603 | // branch to the destination. |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1604 | int delta = TII->getInstSizeInBytes(MBB->back()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1605 | BBInfo[MBB->getNumber()].Size -= delta; |
| 1606 | MBB->back().eraseFromParent(); |
| 1607 | // BBInfo[SplitBB].Offset is wrong temporarily, fixed below |
| 1608 | } |
Duncan P. N. Exon Smith | 7869148 | 2015-10-20 00:15:20 +0000 | [diff] [blame] | 1609 | MachineBasicBlock *NextBB = &*++MBB->getIterator(); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1610 | |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1611 | LLVM_DEBUG(dbgs() << " Insert B to " << printMBBReference(*DestBB) |
| 1612 | << " also invert condition and change dest. to " |
| 1613 | << printMBBReference(*NextBB) << "\n"); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1614 | |
| 1615 | // Insert a new conditional branch and a new unconditional branch. |
| 1616 | // Also update the ImmBranch as well as adding a new entry for the new branch. |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 1617 | if (MI->getNumExplicitOperands() == 2) { |
| 1618 | BuildMI(MBB, DebugLoc(), TII->get(OppositeBranchOpcode)) |
| 1619 | .addReg(MI->getOperand(0).getReg()) |
| 1620 | .addMBB(NextBB); |
Reed Kotler | 47f3c64a | 2013-12-19 00:43:08 +0000 | [diff] [blame] | 1621 | } else { |
| 1622 | BuildMI(MBB, DebugLoc(), TII->get(OppositeBranchOpcode)) |
| 1623 | .addMBB(NextBB); |
Reed Kotler | 59975c2 | 2013-12-03 23:42:51 +0000 | [diff] [blame] | 1624 | } |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1625 | Br.MI = &MBB->back(); |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1626 | BBInfo[MBB->getNumber()].Size += TII->getInstSizeInBytes(MBB->back()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1627 | BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB); |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1628 | BBInfo[MBB->getNumber()].Size += TII->getInstSizeInBytes(MBB->back()); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1629 | unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr); |
| 1630 | ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr)); |
| 1631 | |
| 1632 | // Remove the old conditional branch. It may or may not still be in MBB. |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 1633 | BBInfo[MI->getParent()->getNumber()].Size -= TII->getInstSizeInBytes(*MI); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1634 | MI->eraseFromParent(); |
| 1635 | adjustBBOffsetsAfter(MBB); |
| 1636 | return true; |
| 1637 | } |
| 1638 | |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1639 | void MipsConstantIslands::prescanForConstants() { |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1640 | unsigned J = 0; |
| 1641 | (void)J; |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1642 | for (MachineFunction::iterator B = |
| 1643 | MF->begin(), E = MF->end(); B != E; ++B) { |
| 1644 | for (MachineBasicBlock::instr_iterator I = |
| 1645 | B->instr_begin(), EB = B->instr_end(); I != EB; ++I) { |
| 1646 | switch(I->getDesc().getOpcode()) { |
| 1647 | case Mips::LwConstant32: { |
Reed Kotler | a787aa2 | 2013-11-24 06:18:50 +0000 | [diff] [blame] | 1648 | PrescannedForConstants = true; |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1649 | LLVM_DEBUG(dbgs() << "constant island constant " << *I << "\n"); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1650 | J = I->getNumOperands(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1651 | LLVM_DEBUG(dbgs() << "num operands " << J << "\n"); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1652 | MachineOperand& Literal = I->getOperand(1); |
| 1653 | if (Literal.isImm()) { |
| 1654 | int64_t V = Literal.getImm(); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1655 | LLVM_DEBUG(dbgs() << "literal " << V << "\n"); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1656 | Type *Int32Ty = |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 1657 | Type::getInt32Ty(MF->getFunction().getContext()); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1658 | const Constant *C = ConstantInt::get(Int32Ty, V); |
| 1659 | unsigned index = MCP->getConstantPoolIndex(C, 4); |
| 1660 | I->getOperand(2).ChangeToImmediate(index); |
Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 1661 | LLVM_DEBUG(dbgs() << "constant island constant " << *I << "\n"); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1662 | I->setDesc(TII->get(Mips::LwRxPcTcp16)); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1663 | I->RemoveOperand(1); |
| 1664 | I->RemoveOperand(1); |
| 1665 | I->addOperand(MachineOperand::CreateCPI(index, 0)); |
Reed Kotler | 0f007fc | 2013-11-05 08:14:14 +0000 | [diff] [blame] | 1666 | I->addOperand(MachineOperand::CreateImm(4)); |
Reed Kotler | 91ae982 | 2013-10-27 21:57:36 +0000 | [diff] [blame] | 1667 | } |
| 1668 | break; |
| 1669 | } |
| 1670 | default: |
| 1671 | break; |
| 1672 | } |
| 1673 | } |
| 1674 | } |
| 1675 | } |
Eugene Zelenko | dde94e4 | 2017-01-30 23:21:32 +0000 | [diff] [blame] | 1676 | |
| 1677 | /// Returns a pass that converts branches to long branches. |
| 1678 | FunctionPass *llvm::createMipsConstantIslandPass() { |
| 1679 | return new MipsConstantIslands(); |
| 1680 | } |