Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 1 | //===-- SystemZLongBranch.cpp - Branch lengthening for SystemZ ------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass makes sure that all branches are in range. There are several ways |
| 11 | // in which this could be done. One aggressive approach is to assume that all |
| 12 | // branches are in range and successively replace those that turn out not |
| 13 | // to be in range with a longer form (branch relaxation). A simple |
| 14 | // implementation is to continually walk through the function relaxing |
| 15 | // branches until no more changes are needed and a fixed point is reached. |
| 16 | // However, in the pathological worst case, this implementation is |
| 17 | // quadratic in the number of blocks; relaxing branch N can make branch N-1 |
| 18 | // go out of range, which in turn can make branch N-2 go out of range, |
| 19 | // and so on. |
| 20 | // |
| 21 | // An alternative approach is to assume that all branches must be |
| 22 | // converted to their long forms, then reinstate the short forms of |
| 23 | // branches that, even under this pessimistic assumption, turn out to be |
| 24 | // in range (branch shortening). This too can be implemented as a function |
| 25 | // walk that is repeated until a fixed point is reached. In general, |
| 26 | // the result of shortening is not as good as that of relaxation, and |
| 27 | // shortening is also quadratic in the worst case; shortening branch N |
| 28 | // can bring branch N-1 in range of the short form, which in turn can do |
| 29 | // the same for branch N-2, and so on. The main advantage of shortening |
| 30 | // is that each walk through the function produces valid code, so it is |
| 31 | // possible to stop at any point after the first walk. The quadraticness |
| 32 | // could therefore be handled with a maximum pass count, although the |
| 33 | // question then becomes: what maximum count should be used? |
| 34 | // |
| 35 | // On SystemZ, long branches are only needed for functions bigger than 64k, |
| 36 | // which are relatively rare to begin with, and the long branch sequences |
| 37 | // are actually relatively cheap. It therefore doesn't seem worth spending |
| 38 | // much compilation time on the problem. Instead, the approach we take is: |
| 39 | // |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 40 | // (1) Work out the address that each block would have if no branches |
| 41 | // need relaxing. Exit the pass early if all branches are in range |
| 42 | // according to this assumption. |
| 43 | // |
| 44 | // (2) Work out the address that each block would have if all branches |
| 45 | // need relaxing. |
| 46 | // |
| 47 | // (3) Walk through the block calculating the final address of each instruction |
| 48 | // and relaxing those that need to be relaxed. For backward branches, |
| 49 | // this check uses the final address of the target block, as calculated |
| 50 | // earlier in the walk. For forward branches, this check uses the |
| 51 | // address of the target block that was calculated in (2). Both checks |
| 52 | // give a conservatively-correct range. |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 53 | // |
| 54 | //===----------------------------------------------------------------------===// |
| 55 | |
| 56 | #define DEBUG_TYPE "systemz-long-branch" |
| 57 | |
| 58 | #include "SystemZTargetMachine.h" |
| 59 | #include "llvm/ADT/Statistic.h" |
| 60 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 61 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 62 | #include "llvm/IR/Function.h" |
| 63 | #include "llvm/Support/CommandLine.h" |
| 64 | #include "llvm/Support/MathExtras.h" |
| 65 | #include "llvm/Target/TargetInstrInfo.h" |
| 66 | #include "llvm/Target/TargetMachine.h" |
| 67 | #include "llvm/Target/TargetRegisterInfo.h" |
| 68 | |
| 69 | using namespace llvm; |
| 70 | |
| 71 | STATISTIC(LongBranches, "Number of long branches."); |
| 72 | |
| 73 | namespace { |
| 74 | typedef MachineBasicBlock::iterator Iter; |
| 75 | |
| 76 | // Represents positional information about a basic block. |
| 77 | struct MBBInfo { |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 78 | // The address that we currently assume the block has. |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 79 | uint64_t Address; |
| 80 | |
| 81 | // The size of the block in bytes, excluding terminators. |
| 82 | // This value never changes. |
| 83 | uint64_t Size; |
| 84 | |
| 85 | // The minimum alignment of the block, as a log2 value. |
| 86 | // This value never changes. |
| 87 | unsigned Alignment; |
| 88 | |
| 89 | // The number of terminators in this block. This value never changes. |
| 90 | unsigned NumTerminators; |
| 91 | |
| 92 | MBBInfo() |
| 93 | : Address(0), Size(0), Alignment(0), NumTerminators(0) {} |
| 94 | }; |
| 95 | |
| 96 | // Represents the state of a block terminator. |
| 97 | struct TerminatorInfo { |
| 98 | // If this terminator is a relaxable branch, this points to the branch |
| 99 | // instruction, otherwise it is null. |
| 100 | MachineInstr *Branch; |
| 101 | |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 102 | // The address that we currently assume the terminator has. |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 103 | uint64_t Address; |
| 104 | |
| 105 | // The current size of the terminator in bytes. |
| 106 | uint64_t Size; |
| 107 | |
| 108 | // If Branch is nonnull, this is the number of the target block, |
| 109 | // otherwise it is unused. |
| 110 | unsigned TargetBlock; |
| 111 | |
| 112 | // If Branch is nonnull, this is the length of the longest relaxed form, |
| 113 | // otherwise it is zero. |
| 114 | unsigned ExtraRelaxSize; |
| 115 | |
| 116 | TerminatorInfo() : Branch(0), Size(0), TargetBlock(0), ExtraRelaxSize(0) {} |
| 117 | }; |
| 118 | |
| 119 | // Used to keep track of the current position while iterating over the blocks. |
| 120 | struct BlockPosition { |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 121 | // The address that we assume this position has. |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 122 | uint64_t Address; |
| 123 | |
| 124 | // The number of low bits in Address that are known to be the same |
| 125 | // as the runtime address. |
| 126 | unsigned KnownBits; |
| 127 | |
| 128 | BlockPosition(unsigned InitialAlignment) |
| 129 | : Address(0), KnownBits(InitialAlignment) {} |
| 130 | }; |
| 131 | |
| 132 | class SystemZLongBranch : public MachineFunctionPass { |
| 133 | public: |
| 134 | static char ID; |
| 135 | SystemZLongBranch(const SystemZTargetMachine &tm) |
| 136 | : MachineFunctionPass(ID), |
| 137 | TII(static_cast<const SystemZInstrInfo *>(tm.getInstrInfo())) {} |
| 138 | |
| 139 | virtual const char *getPassName() const { |
| 140 | return "SystemZ Long Branch"; |
| 141 | } |
| 142 | |
| 143 | bool runOnMachineFunction(MachineFunction &F); |
| 144 | |
| 145 | private: |
| 146 | void skipNonTerminators(BlockPosition &Position, MBBInfo &Block); |
| 147 | void skipTerminator(BlockPosition &Position, TerminatorInfo &Terminator, |
| 148 | bool AssumeRelaxed); |
| 149 | TerminatorInfo describeTerminator(MachineInstr *MI); |
| 150 | uint64_t initMBBInfo(); |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 151 | bool mustRelaxBranch(const TerminatorInfo &Terminator, uint64_t Address); |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 152 | bool mustRelaxABranch(); |
| 153 | void setWorstCaseAddresses(); |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame^] | 154 | void splitCompareBranch(MachineInstr *MI, unsigned CompareOpcode); |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 155 | void relaxBranch(TerminatorInfo &Terminator); |
| 156 | void relaxBranches(); |
| 157 | |
| 158 | const SystemZInstrInfo *TII; |
| 159 | MachineFunction *MF; |
| 160 | SmallVector<MBBInfo, 16> MBBs; |
| 161 | SmallVector<TerminatorInfo, 16> Terminators; |
| 162 | }; |
| 163 | |
| 164 | char SystemZLongBranch::ID = 0; |
| 165 | |
| 166 | const uint64_t MaxBackwardRange = 0x10000; |
| 167 | const uint64_t MaxForwardRange = 0xfffe; |
| 168 | } // end of anonymous namespace |
| 169 | |
| 170 | FunctionPass *llvm::createSystemZLongBranchPass(SystemZTargetMachine &TM) { |
| 171 | return new SystemZLongBranch(TM); |
| 172 | } |
| 173 | |
| 174 | // Position describes the state immediately before Block. Update Block |
| 175 | // accordingly and move Position to the end of the block's non-terminator |
| 176 | // instructions. |
| 177 | void SystemZLongBranch::skipNonTerminators(BlockPosition &Position, |
| 178 | MBBInfo &Block) { |
| 179 | if (Block.Alignment > Position.KnownBits) { |
| 180 | // When calculating the address of Block, we need to conservatively |
| 181 | // assume that Block had the worst possible misalignment. |
| 182 | Position.Address += ((uint64_t(1) << Block.Alignment) - |
| 183 | (uint64_t(1) << Position.KnownBits)); |
| 184 | Position.KnownBits = Block.Alignment; |
| 185 | } |
| 186 | |
| 187 | // Align the addresses. |
| 188 | uint64_t AlignMask = (uint64_t(1) << Block.Alignment) - 1; |
| 189 | Position.Address = (Position.Address + AlignMask) & ~AlignMask; |
| 190 | |
| 191 | // Record the block's position. |
| 192 | Block.Address = Position.Address; |
| 193 | |
| 194 | // Move past the non-terminators in the block. |
| 195 | Position.Address += Block.Size; |
| 196 | } |
| 197 | |
| 198 | // Position describes the state immediately before Terminator. |
| 199 | // Update Terminator accordingly and move Position past it. |
| 200 | // Assume that Terminator will be relaxed if AssumeRelaxed. |
| 201 | void SystemZLongBranch::skipTerminator(BlockPosition &Position, |
| 202 | TerminatorInfo &Terminator, |
| 203 | bool AssumeRelaxed) { |
| 204 | Terminator.Address = Position.Address; |
| 205 | Position.Address += Terminator.Size; |
| 206 | if (AssumeRelaxed) |
| 207 | Position.Address += Terminator.ExtraRelaxSize; |
| 208 | } |
| 209 | |
| 210 | // Return a description of terminator instruction MI. |
| 211 | TerminatorInfo SystemZLongBranch::describeTerminator(MachineInstr *MI) { |
| 212 | TerminatorInfo Terminator; |
| 213 | Terminator.Size = TII->getInstSizeInBytes(MI); |
| 214 | if (MI->isConditionalBranch() || MI->isUnconditionalBranch()) { |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 215 | switch (MI->getOpcode()) { |
| 216 | case SystemZ::J: |
| 217 | // Relaxes to JG, which is 2 bytes longer. |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 218 | Terminator.ExtraRelaxSize = 2; |
| 219 | break; |
| 220 | case SystemZ::BRC: |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 221 | // Relaxes to BRCL, which is 2 bytes longer. |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 222 | Terminator.ExtraRelaxSize = 2; |
| 223 | break; |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame^] | 224 | case SystemZ::CRJ: |
| 225 | // Relaxes to a CR/BRCL sequence, which is 2 bytes longer. |
| 226 | Terminator.ExtraRelaxSize = 2; |
| 227 | break; |
| 228 | case SystemZ::CGRJ: |
| 229 | // Relaxes to a CGR/BRCL sequence, which is 4 bytes longer. |
| 230 | Terminator.ExtraRelaxSize = 4; |
| 231 | break; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 232 | default: |
| 233 | llvm_unreachable("Unrecognized branch instruction"); |
| 234 | } |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 235 | Terminator.Branch = MI; |
| 236 | Terminator.TargetBlock = |
| 237 | TII->getBranchInfo(MI).Target->getMBB()->getNumber(); |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 238 | } |
| 239 | return Terminator; |
| 240 | } |
| 241 | |
| 242 | // Fill MBBs and Terminators, setting the addresses on the assumption |
| 243 | // that no branches need relaxation. Return the size of the function under |
| 244 | // this assumption. |
| 245 | uint64_t SystemZLongBranch::initMBBInfo() { |
| 246 | MF->RenumberBlocks(); |
| 247 | unsigned NumBlocks = MF->size(); |
| 248 | |
| 249 | MBBs.clear(); |
| 250 | MBBs.resize(NumBlocks); |
| 251 | |
| 252 | Terminators.clear(); |
| 253 | Terminators.reserve(NumBlocks); |
| 254 | |
| 255 | BlockPosition Position(MF->getAlignment()); |
| 256 | for (unsigned I = 0; I < NumBlocks; ++I) { |
| 257 | MachineBasicBlock *MBB = MF->getBlockNumbered(I); |
| 258 | MBBInfo &Block = MBBs[I]; |
| 259 | |
| 260 | // Record the alignment, for quick access. |
| 261 | Block.Alignment = MBB->getAlignment(); |
| 262 | |
| 263 | // Calculate the size of the fixed part of the block. |
| 264 | MachineBasicBlock::iterator MI = MBB->begin(); |
| 265 | MachineBasicBlock::iterator End = MBB->end(); |
| 266 | while (MI != End && !MI->isTerminator()) { |
| 267 | Block.Size += TII->getInstSizeInBytes(MI); |
| 268 | ++MI; |
| 269 | } |
| 270 | skipNonTerminators(Position, Block); |
| 271 | |
| 272 | // Add the terminators. |
| 273 | while (MI != End) { |
| 274 | if (!MI->isDebugValue()) { |
| 275 | assert(MI->isTerminator() && "Terminator followed by non-terminator"); |
| 276 | Terminators.push_back(describeTerminator(MI)); |
| 277 | skipTerminator(Position, Terminators.back(), false); |
| 278 | ++Block.NumTerminators; |
| 279 | } |
| 280 | ++MI; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return Position.Address; |
| 285 | } |
| 286 | |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 287 | // Return true if, under current assumptions, Terminator would need to be |
| 288 | // relaxed if it were placed at address Address. |
| 289 | bool SystemZLongBranch::mustRelaxBranch(const TerminatorInfo &Terminator, |
| 290 | uint64_t Address) { |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 291 | if (!Terminator.Branch) |
| 292 | return false; |
| 293 | |
| 294 | const MBBInfo &Target = MBBs[Terminator.TargetBlock]; |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 295 | if (Address >= Target.Address) { |
| 296 | if (Address - Target.Address <= MaxBackwardRange) |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 297 | return false; |
| 298 | } else { |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 299 | if (Target.Address - Address <= MaxForwardRange) |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 300 | return false; |
| 301 | } |
| 302 | |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | // Return true if, under current assumptions, any terminator needs |
| 307 | // to be relaxed. |
| 308 | bool SystemZLongBranch::mustRelaxABranch() { |
| 309 | for (SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin(), |
| 310 | TE = Terminators.end(); TI != TE; ++TI) |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 311 | if (mustRelaxBranch(*TI, TI->Address)) |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 312 | return true; |
| 313 | return false; |
| 314 | } |
| 315 | |
| 316 | // Set the address of each block on the assumption that all branches |
| 317 | // must be long. |
| 318 | void SystemZLongBranch::setWorstCaseAddresses() { |
| 319 | SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin(); |
| 320 | BlockPosition Position(MF->getAlignment()); |
| 321 | for (SmallVector<MBBInfo, 16>::iterator BI = MBBs.begin(), BE = MBBs.end(); |
| 322 | BI != BE; ++BI) { |
| 323 | skipNonTerminators(Position, *BI); |
| 324 | for (unsigned BTI = 0, BTE = BI->NumTerminators; BTI != BTE; ++BTI) { |
| 325 | skipTerminator(Position, *TI, true); |
| 326 | ++TI; |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame^] | 331 | // Split MI into the comparison given by CompareOpcode followed |
| 332 | // a BRCL on the result. |
| 333 | void SystemZLongBranch::splitCompareBranch(MachineInstr *MI, |
| 334 | unsigned CompareOpcode) { |
| 335 | MachineBasicBlock *MBB = MI->getParent(); |
| 336 | DebugLoc DL = MI->getDebugLoc(); |
| 337 | BuildMI(*MBB, MI, DL, TII->get(CompareOpcode)) |
| 338 | .addOperand(MI->getOperand(0)) |
| 339 | .addOperand(MI->getOperand(1)); |
| 340 | MachineInstr *BRCL = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BRCL)) |
| 341 | .addOperand(MI->getOperand(2)) |
| 342 | .addOperand(MI->getOperand(3)); |
| 343 | // The implicit use of CC is a killing use. |
| 344 | BRCL->getOperand(2).setIsKill(); |
| 345 | MI->eraseFromParent(); |
| 346 | } |
| 347 | |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 348 | // Relax the branch described by Terminator. |
| 349 | void SystemZLongBranch::relaxBranch(TerminatorInfo &Terminator) { |
| 350 | MachineInstr *Branch = Terminator.Branch; |
| 351 | switch (Branch->getOpcode()) { |
Richard Sandiford | 3b105a0 | 2013-05-21 08:48:24 +0000 | [diff] [blame] | 352 | case SystemZ::J: |
| 353 | Branch->setDesc(TII->get(SystemZ::JG)); |
| 354 | break; |
| 355 | case SystemZ::BRC: |
| 356 | Branch->setDesc(TII->get(SystemZ::BRCL)); |
| 357 | break; |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame^] | 358 | case SystemZ::CRJ: |
| 359 | splitCompareBranch(Branch, SystemZ::CR); |
| 360 | break; |
| 361 | case SystemZ::CGRJ: |
| 362 | splitCompareBranch(Branch, SystemZ::CGR); |
| 363 | break; |
Richard Sandiford | 3b105a0 | 2013-05-21 08:48:24 +0000 | [diff] [blame] | 364 | default: |
| 365 | llvm_unreachable("Unrecognized branch"); |
| 366 | } |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 367 | |
| 368 | Terminator.Size += Terminator.ExtraRelaxSize; |
| 369 | Terminator.ExtraRelaxSize = 0; |
| 370 | Terminator.Branch = 0; |
| 371 | |
| 372 | ++LongBranches; |
| 373 | } |
| 374 | |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 375 | // Run a shortening pass and relax any branches that need to be relaxed. |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 376 | void SystemZLongBranch::relaxBranches() { |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 377 | SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin(); |
| 378 | BlockPosition Position(MF->getAlignment()); |
| 379 | for (SmallVector<MBBInfo, 16>::iterator BI = MBBs.begin(), BE = MBBs.end(); |
| 380 | BI != BE; ++BI) { |
| 381 | skipNonTerminators(Position, *BI); |
| 382 | for (unsigned BTI = 0, BTE = BI->NumTerminators; BTI != BTE; ++BTI) { |
| 383 | assert(Position.Address <= TI->Address && |
| 384 | "Addresses shouldn't go forwards"); |
| 385 | if (mustRelaxBranch(*TI, Position.Address)) |
| 386 | relaxBranch(*TI); |
| 387 | skipTerminator(Position, *TI, false); |
| 388 | ++TI; |
| 389 | } |
| 390 | } |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | bool SystemZLongBranch::runOnMachineFunction(MachineFunction &F) { |
| 394 | MF = &F; |
| 395 | uint64_t Size = initMBBInfo(); |
| 396 | if (Size <= MaxForwardRange || !mustRelaxABranch()) |
| 397 | return false; |
| 398 | |
| 399 | setWorstCaseAddresses(); |
| 400 | relaxBranches(); |
| 401 | return true; |
| 402 | } |