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