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 | // |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 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. |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 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 { |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 74 | // Represents positional information about a basic block. |
| 75 | struct MBBInfo { |
| 76 | // The address that we currently assume the block has. |
| 77 | uint64_t Address; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 78 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 79 | // The size of the block in bytes, excluding terminators. |
| 80 | // This value never changes. |
| 81 | uint64_t Size; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 82 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 83 | // The minimum alignment of the block, as a log2 value. |
| 84 | // This value never changes. |
| 85 | unsigned Alignment; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 86 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 87 | // The number of terminators in this block. This value never changes. |
| 88 | unsigned NumTerminators; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 89 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 90 | MBBInfo() |
| 91 | : Address(0), Size(0), Alignment(0), NumTerminators(0) {} |
| 92 | }; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 93 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 94 | // Represents the state of a block terminator. |
| 95 | struct TerminatorInfo { |
| 96 | // If this terminator is a relaxable branch, this points to the branch |
| 97 | // instruction, otherwise it is null. |
| 98 | MachineInstr *Branch; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 99 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 100 | // The address that we currently assume the terminator has. |
| 101 | uint64_t Address; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 102 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 103 | // The current size of the terminator in bytes. |
| 104 | uint64_t Size; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 105 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 106 | // If Branch is nonnull, this is the number of the target block, |
| 107 | // otherwise it is unused. |
| 108 | unsigned TargetBlock; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 109 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 110 | // If Branch is nonnull, this is the length of the longest relaxed form, |
| 111 | // otherwise it is zero. |
| 112 | unsigned ExtraRelaxSize; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 113 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 114 | TerminatorInfo() : Branch(0), Size(0), TargetBlock(0), ExtraRelaxSize(0) {} |
| 115 | }; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 116 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 117 | // Used to keep track of the current position while iterating over the blocks. |
| 118 | struct BlockPosition { |
| 119 | // The address that we assume this position has. |
| 120 | uint64_t Address; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 121 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 122 | // The number of low bits in Address that are known to be the same |
| 123 | // as the runtime address. |
| 124 | unsigned KnownBits; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 125 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 126 | BlockPosition(unsigned InitialAlignment) |
| 127 | : Address(0), KnownBits(InitialAlignment) {} |
| 128 | }; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 129 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 130 | class SystemZLongBranch : public MachineFunctionPass { |
| 131 | public: |
| 132 | static char ID; |
| 133 | SystemZLongBranch(const SystemZTargetMachine &tm) |
| 134 | : MachineFunctionPass(ID), TII(0) {} |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 135 | |
Richard Sandiford | b4d67b5 | 2014-03-06 12:03:36 +0000 | [diff] [blame^] | 136 | const char *getPassName() const override { |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 137 | return "SystemZ Long Branch"; |
| 138 | } |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 139 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 140 | bool runOnMachineFunction(MachineFunction &F); |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 141 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 142 | private: |
| 143 | void skipNonTerminators(BlockPosition &Position, MBBInfo &Block); |
| 144 | void skipTerminator(BlockPosition &Position, TerminatorInfo &Terminator, |
| 145 | bool AssumeRelaxed); |
| 146 | TerminatorInfo describeTerminator(MachineInstr *MI); |
| 147 | uint64_t initMBBInfo(); |
| 148 | bool mustRelaxBranch(const TerminatorInfo &Terminator, uint64_t Address); |
| 149 | bool mustRelaxABranch(); |
| 150 | void setWorstCaseAddresses(); |
| 151 | void splitBranchOnCount(MachineInstr *MI, unsigned AddOpcode); |
| 152 | void splitCompareBranch(MachineInstr *MI, unsigned CompareOpcode); |
| 153 | void relaxBranch(TerminatorInfo &Terminator); |
| 154 | void relaxBranches(); |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 155 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 156 | const SystemZInstrInfo *TII; |
| 157 | MachineFunction *MF; |
| 158 | SmallVector<MBBInfo, 16> MBBs; |
| 159 | SmallVector<TerminatorInfo, 16> Terminators; |
| 160 | }; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 161 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 162 | char SystemZLongBranch::ID = 0; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 163 | |
Richard Sandiford | c231269 | 2014-03-06 10:38:30 +0000 | [diff] [blame] | 164 | const uint64_t MaxBackwardRange = 0x10000; |
| 165 | const uint64_t MaxForwardRange = 0xfffe; |
| 166 | } // end anonymous namespace |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 167 | |
| 168 | FunctionPass *llvm::createSystemZLongBranchPass(SystemZTargetMachine &TM) { |
| 169 | return new SystemZLongBranch(TM); |
| 170 | } |
| 171 | |
| 172 | // Position describes the state immediately before Block. Update Block |
| 173 | // accordingly and move Position to the end of the block's non-terminator |
| 174 | // instructions. |
| 175 | void SystemZLongBranch::skipNonTerminators(BlockPosition &Position, |
| 176 | MBBInfo &Block) { |
| 177 | if (Block.Alignment > Position.KnownBits) { |
| 178 | // When calculating the address of Block, we need to conservatively |
| 179 | // assume that Block had the worst possible misalignment. |
| 180 | Position.Address += ((uint64_t(1) << Block.Alignment) - |
| 181 | (uint64_t(1) << Position.KnownBits)); |
| 182 | Position.KnownBits = Block.Alignment; |
| 183 | } |
| 184 | |
| 185 | // Align the addresses. |
| 186 | uint64_t AlignMask = (uint64_t(1) << Block.Alignment) - 1; |
| 187 | Position.Address = (Position.Address + AlignMask) & ~AlignMask; |
| 188 | |
| 189 | // Record the block's position. |
| 190 | Block.Address = Position.Address; |
| 191 | |
| 192 | // Move past the non-terminators in the block. |
| 193 | Position.Address += Block.Size; |
| 194 | } |
| 195 | |
| 196 | // Position describes the state immediately before Terminator. |
| 197 | // Update Terminator accordingly and move Position past it. |
| 198 | // Assume that Terminator will be relaxed if AssumeRelaxed. |
| 199 | void SystemZLongBranch::skipTerminator(BlockPosition &Position, |
| 200 | TerminatorInfo &Terminator, |
| 201 | bool AssumeRelaxed) { |
| 202 | Terminator.Address = Position.Address; |
| 203 | Position.Address += Terminator.Size; |
| 204 | if (AssumeRelaxed) |
| 205 | Position.Address += Terminator.ExtraRelaxSize; |
| 206 | } |
| 207 | |
| 208 | // Return a description of terminator instruction MI. |
| 209 | TerminatorInfo SystemZLongBranch::describeTerminator(MachineInstr *MI) { |
| 210 | TerminatorInfo Terminator; |
| 211 | Terminator.Size = TII->getInstSizeInBytes(MI); |
| 212 | if (MI->isConditionalBranch() || MI->isUnconditionalBranch()) { |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 213 | switch (MI->getOpcode()) { |
| 214 | case SystemZ::J: |
| 215 | // Relaxes to JG, which is 2 bytes longer. |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 216 | Terminator.ExtraRelaxSize = 2; |
| 217 | break; |
| 218 | case SystemZ::BRC: |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 219 | // Relaxes to BRCL, which is 2 bytes longer. |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 220 | Terminator.ExtraRelaxSize = 2; |
| 221 | break; |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 222 | case SystemZ::BRCT: |
| 223 | case SystemZ::BRCTG: |
| 224 | // Relaxes to A(G)HI and BRCL, which is 6 bytes longer. |
| 225 | Terminator.ExtraRelaxSize = 6; |
| 226 | break; |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 227 | case SystemZ::CRJ: |
Richard Sandiford | 93183ee | 2013-09-18 09:56:40 +0000 | [diff] [blame] | 228 | case SystemZ::CLRJ: |
| 229 | // Relaxes to a C(L)R/BRCL sequence, which is 2 bytes longer. |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 230 | Terminator.ExtraRelaxSize = 2; |
| 231 | break; |
| 232 | case SystemZ::CGRJ: |
Richard Sandiford | 93183ee | 2013-09-18 09:56:40 +0000 | [diff] [blame] | 233 | case SystemZ::CLGRJ: |
| 234 | // Relaxes to a C(L)GR/BRCL sequence, which is 4 bytes longer. |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 235 | Terminator.ExtraRelaxSize = 4; |
| 236 | break; |
Richard Sandiford | e1d9f00 | 2013-05-29 11:58:52 +0000 | [diff] [blame] | 237 | case SystemZ::CIJ: |
| 238 | case SystemZ::CGIJ: |
| 239 | // Relaxes to a C(G)HI/BRCL sequence, which is 4 bytes longer. |
| 240 | Terminator.ExtraRelaxSize = 4; |
| 241 | break; |
Richard Sandiford | 93183ee | 2013-09-18 09:56:40 +0000 | [diff] [blame] | 242 | case SystemZ::CLIJ: |
| 243 | case SystemZ::CLGIJ: |
| 244 | // Relaxes to a CL(G)FI/BRCL sequence, which is 6 bytes longer. |
| 245 | Terminator.ExtraRelaxSize = 6; |
| 246 | break; |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 247 | default: |
| 248 | llvm_unreachable("Unrecognized branch instruction"); |
| 249 | } |
Richard Sandiford | 53c9efd | 2013-05-28 10:13:54 +0000 | [diff] [blame] | 250 | Terminator.Branch = MI; |
| 251 | Terminator.TargetBlock = |
| 252 | TII->getBranchInfo(MI).Target->getMBB()->getNumber(); |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 253 | } |
| 254 | return Terminator; |
| 255 | } |
| 256 | |
| 257 | // Fill MBBs and Terminators, setting the addresses on the assumption |
| 258 | // that no branches need relaxation. Return the size of the function under |
| 259 | // this assumption. |
| 260 | uint64_t SystemZLongBranch::initMBBInfo() { |
| 261 | MF->RenumberBlocks(); |
| 262 | unsigned NumBlocks = MF->size(); |
| 263 | |
| 264 | MBBs.clear(); |
| 265 | MBBs.resize(NumBlocks); |
| 266 | |
| 267 | Terminators.clear(); |
| 268 | Terminators.reserve(NumBlocks); |
| 269 | |
| 270 | BlockPosition Position(MF->getAlignment()); |
| 271 | for (unsigned I = 0; I < NumBlocks; ++I) { |
| 272 | MachineBasicBlock *MBB = MF->getBlockNumbered(I); |
| 273 | MBBInfo &Block = MBBs[I]; |
| 274 | |
| 275 | // Record the alignment, for quick access. |
| 276 | Block.Alignment = MBB->getAlignment(); |
| 277 | |
| 278 | // Calculate the size of the fixed part of the block. |
| 279 | MachineBasicBlock::iterator MI = MBB->begin(); |
| 280 | MachineBasicBlock::iterator End = MBB->end(); |
| 281 | while (MI != End && !MI->isTerminator()) { |
Richard Sandiford | bdbb8af | 2013-08-05 10:58:53 +0000 | [diff] [blame] | 282 | Block.Size += TII->getInstSizeInBytes(MI); |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 283 | ++MI; |
| 284 | } |
| 285 | skipNonTerminators(Position, Block); |
| 286 | |
| 287 | // Add the terminators. |
| 288 | while (MI != End) { |
| 289 | if (!MI->isDebugValue()) { |
| 290 | assert(MI->isTerminator() && "Terminator followed by non-terminator"); |
| 291 | Terminators.push_back(describeTerminator(MI)); |
| 292 | skipTerminator(Position, Terminators.back(), false); |
| 293 | ++Block.NumTerminators; |
| 294 | } |
| 295 | ++MI; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | return Position.Address; |
| 300 | } |
| 301 | |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 302 | // Return true if, under current assumptions, Terminator would need to be |
| 303 | // relaxed if it were placed at address Address. |
| 304 | bool SystemZLongBranch::mustRelaxBranch(const TerminatorInfo &Terminator, |
| 305 | uint64_t Address) { |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 306 | if (!Terminator.Branch) |
| 307 | return false; |
| 308 | |
| 309 | const MBBInfo &Target = MBBs[Terminator.TargetBlock]; |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 310 | if (Address >= Target.Address) { |
| 311 | if (Address - Target.Address <= MaxBackwardRange) |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 312 | return false; |
| 313 | } else { |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 314 | if (Target.Address - Address <= MaxForwardRange) |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 315 | return false; |
| 316 | } |
| 317 | |
| 318 | return true; |
| 319 | } |
| 320 | |
| 321 | // Return true if, under current assumptions, any terminator needs |
| 322 | // to be relaxed. |
| 323 | bool SystemZLongBranch::mustRelaxABranch() { |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 324 | for (auto &Terminator : Terminators) |
| 325 | if (mustRelaxBranch(Terminator, Terminator.Address)) |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 326 | return true; |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | // Set the address of each block on the assumption that all branches |
| 331 | // must be long. |
| 332 | void SystemZLongBranch::setWorstCaseAddresses() { |
| 333 | SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin(); |
| 334 | BlockPosition Position(MF->getAlignment()); |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 335 | for (auto &Block : MBBs) { |
| 336 | skipNonTerminators(Position, Block); |
| 337 | for (unsigned BTI = 0, BTE = Block.NumTerminators; BTI != BTE; ++BTI) { |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 338 | skipTerminator(Position, *TI, true); |
| 339 | ++TI; |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 344 | // Split BRANCH ON COUNT MI into the addition given by AddOpcode followed |
| 345 | // by a BRCL on the result. |
| 346 | void SystemZLongBranch::splitBranchOnCount(MachineInstr *MI, |
| 347 | unsigned AddOpcode) { |
| 348 | MachineBasicBlock *MBB = MI->getParent(); |
| 349 | DebugLoc DL = MI->getDebugLoc(); |
| 350 | BuildMI(*MBB, MI, DL, TII->get(AddOpcode)) |
| 351 | .addOperand(MI->getOperand(0)) |
| 352 | .addOperand(MI->getOperand(1)) |
| 353 | .addImm(-1); |
| 354 | MachineInstr *BRCL = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BRCL)) |
| 355 | .addImm(SystemZ::CCMASK_ICMP) |
| 356 | .addImm(SystemZ::CCMASK_CMP_NE) |
| 357 | .addOperand(MI->getOperand(2)); |
| 358 | // The implicit use of CC is a killing use. |
| 359 | BRCL->addRegisterKilled(SystemZ::CC, &TII->getRegisterInfo()); |
| 360 | MI->eraseFromParent(); |
| 361 | } |
| 362 | |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 363 | // Split MI into the comparison given by CompareOpcode followed |
| 364 | // a BRCL on the result. |
| 365 | void SystemZLongBranch::splitCompareBranch(MachineInstr *MI, |
| 366 | unsigned CompareOpcode) { |
| 367 | MachineBasicBlock *MBB = MI->getParent(); |
| 368 | DebugLoc DL = MI->getDebugLoc(); |
| 369 | BuildMI(*MBB, MI, DL, TII->get(CompareOpcode)) |
| 370 | .addOperand(MI->getOperand(0)) |
| 371 | .addOperand(MI->getOperand(1)); |
| 372 | MachineInstr *BRCL = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BRCL)) |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 373 | .addImm(SystemZ::CCMASK_ICMP) |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 374 | .addOperand(MI->getOperand(2)) |
| 375 | .addOperand(MI->getOperand(3)); |
| 376 | // The implicit use of CC is a killing use. |
Richard Sandiford | 3d768e3 | 2013-07-31 12:30:20 +0000 | [diff] [blame] | 377 | BRCL->addRegisterKilled(SystemZ::CC, &TII->getRegisterInfo()); |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 378 | MI->eraseFromParent(); |
| 379 | } |
| 380 | |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 381 | // Relax the branch described by Terminator. |
| 382 | void SystemZLongBranch::relaxBranch(TerminatorInfo &Terminator) { |
| 383 | MachineInstr *Branch = Terminator.Branch; |
| 384 | switch (Branch->getOpcode()) { |
Richard Sandiford | 3b105a0 | 2013-05-21 08:48:24 +0000 | [diff] [blame] | 385 | case SystemZ::J: |
| 386 | Branch->setDesc(TII->get(SystemZ::JG)); |
| 387 | break; |
| 388 | case SystemZ::BRC: |
| 389 | Branch->setDesc(TII->get(SystemZ::BRCL)); |
| 390 | break; |
Richard Sandiford | c212125 | 2013-08-05 11:23:46 +0000 | [diff] [blame] | 391 | case SystemZ::BRCT: |
| 392 | splitBranchOnCount(Branch, SystemZ::AHI); |
| 393 | break; |
| 394 | case SystemZ::BRCTG: |
| 395 | splitBranchOnCount(Branch, SystemZ::AGHI); |
| 396 | break; |
Richard Sandiford | 0fb90ab | 2013-05-28 10:41:11 +0000 | [diff] [blame] | 397 | case SystemZ::CRJ: |
| 398 | splitCompareBranch(Branch, SystemZ::CR); |
| 399 | break; |
| 400 | case SystemZ::CGRJ: |
| 401 | splitCompareBranch(Branch, SystemZ::CGR); |
| 402 | break; |
Richard Sandiford | e1d9f00 | 2013-05-29 11:58:52 +0000 | [diff] [blame] | 403 | case SystemZ::CIJ: |
| 404 | splitCompareBranch(Branch, SystemZ::CHI); |
| 405 | break; |
| 406 | case SystemZ::CGIJ: |
| 407 | splitCompareBranch(Branch, SystemZ::CGHI); |
| 408 | break; |
Richard Sandiford | 93183ee | 2013-09-18 09:56:40 +0000 | [diff] [blame] | 409 | case SystemZ::CLRJ: |
| 410 | splitCompareBranch(Branch, SystemZ::CLR); |
| 411 | break; |
| 412 | case SystemZ::CLGRJ: |
| 413 | splitCompareBranch(Branch, SystemZ::CLGR); |
| 414 | break; |
| 415 | case SystemZ::CLIJ: |
| 416 | splitCompareBranch(Branch, SystemZ::CLFI); |
| 417 | break; |
| 418 | case SystemZ::CLGIJ: |
| 419 | splitCompareBranch(Branch, SystemZ::CLGFI); |
| 420 | break; |
Richard Sandiford | 3b105a0 | 2013-05-21 08:48:24 +0000 | [diff] [blame] | 421 | default: |
| 422 | llvm_unreachable("Unrecognized branch"); |
| 423 | } |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 424 | |
| 425 | Terminator.Size += Terminator.ExtraRelaxSize; |
| 426 | Terminator.ExtraRelaxSize = 0; |
| 427 | Terminator.Branch = 0; |
| 428 | |
| 429 | ++LongBranches; |
| 430 | } |
| 431 | |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 432 | // 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] | 433 | void SystemZLongBranch::relaxBranches() { |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 434 | SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin(); |
| 435 | BlockPosition Position(MF->getAlignment()); |
Richard Sandiford | 28c111e | 2014-03-06 11:00:15 +0000 | [diff] [blame] | 436 | for (auto &Block : MBBs) { |
| 437 | skipNonTerminators(Position, Block); |
| 438 | for (unsigned BTI = 0, BTE = Block.NumTerminators; BTI != BTE; ++BTI) { |
Richard Sandiford | 03528f3 | 2013-05-22 09:57:57 +0000 | [diff] [blame] | 439 | assert(Position.Address <= TI->Address && |
| 440 | "Addresses shouldn't go forwards"); |
| 441 | if (mustRelaxBranch(*TI, Position.Address)) |
| 442 | relaxBranch(*TI); |
| 443 | skipTerminator(Position, *TI, false); |
| 444 | ++TI; |
| 445 | } |
| 446 | } |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | bool SystemZLongBranch::runOnMachineFunction(MachineFunction &F) { |
Bill Wendling | 637d97d | 2013-06-07 20:42:15 +0000 | [diff] [blame] | 450 | TII = static_cast<const SystemZInstrInfo *>(F.getTarget().getInstrInfo()); |
Richard Sandiford | 312425f | 2013-05-20 14:23:08 +0000 | [diff] [blame] | 451 | MF = &F; |
| 452 | uint64_t Size = initMBBInfo(); |
| 453 | if (Size <= MaxForwardRange || !mustRelaxABranch()) |
| 454 | return false; |
| 455 | |
| 456 | setWorstCaseAddresses(); |
| 457 | relaxBranches(); |
| 458 | return true; |
| 459 | } |