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