blob: 791f0334e0f1c6cbb60f23865e8beec67c355a05 [file] [log] [blame]
Richard Sandiford312425f2013-05-20 14:23:08 +00001//===-- 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 Sandifordbdbb8af2013-08-05 10:58:53 +000010// 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 Sandiford312425f2013-05-20 14:23:08 +000020//
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 Sandiford03528f32013-05-22 09:57:57 +000040// (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 Sandiford312425f2013-05-20 14:23:08 +000053//
54//===----------------------------------------------------------------------===//
55
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000056#include "SystemZ.h"
57#include "SystemZInstrInfo.h"
Richard Sandiford312425f2013-05-20 14:23:08 +000058#include "SystemZTargetMachine.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000059#include "llvm/ADT/SmallVector.h"
Richard Sandiford312425f2013-05-20 14:23:08 +000060#include "llvm/ADT/Statistic.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000061#include "llvm/ADT/StringRef.h"
62#include "llvm/CodeGen/MachineBasicBlock.h"
63#include "llvm/CodeGen/MachineFunction.h"
Richard Sandiford312425f2013-05-20 14:23:08 +000064#include "llvm/CodeGen/MachineFunctionPass.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000065#include "llvm/CodeGen/MachineInstr.h"
Richard Sandiford312425f2013-05-20 14:23:08 +000066#include "llvm/CodeGen/MachineInstrBuilder.h"
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000067#include "llvm/IR/DebugLoc.h"
68#include "llvm/Support/ErrorHandling.h"
69#include <cassert>
70#include <cstdint>
Richard Sandiford312425f2013-05-20 14:23:08 +000071
72using namespace llvm;
73
Chandler Carruth84e68b22014-04-22 02:41:26 +000074#define DEBUG_TYPE "systemz-long-branch"
75
Richard Sandiford312425f2013-05-20 14:23:08 +000076STATISTIC(LongBranches, "Number of long branches.");
77
78namespace {
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000079
Richard Sandifordc2312692014-03-06 10:38:30 +000080// Represents positional information about a basic block.
81struct MBBInfo {
82 // The address that we currently assume the block has.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000083 uint64_t Address = 0;
Richard Sandiford312425f2013-05-20 14:23:08 +000084
Richard Sandifordc2312692014-03-06 10:38:30 +000085 // The size of the block in bytes, excluding terminators.
86 // This value never changes.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000087 uint64_t Size = 0;
Richard Sandiford312425f2013-05-20 14:23:08 +000088
Richard Sandifordc2312692014-03-06 10:38:30 +000089 // The minimum alignment of the block, as a log2 value.
90 // This value never changes.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000091 unsigned Alignment = 0;
Richard Sandiford312425f2013-05-20 14:23:08 +000092
Richard Sandifordc2312692014-03-06 10:38:30 +000093 // The number of terminators in this block. This value never changes.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000094 unsigned NumTerminators = 0;
Richard Sandiford312425f2013-05-20 14:23:08 +000095
Eugene Zelenko3943d2b2017-01-24 22:10:43 +000096 MBBInfo() = default;
Richard Sandifordc2312692014-03-06 10:38:30 +000097};
Richard Sandiford312425f2013-05-20 14:23:08 +000098
Richard Sandifordc2312692014-03-06 10:38:30 +000099// Represents the state of a block terminator.
100struct TerminatorInfo {
101 // If this terminator is a relaxable branch, this points to the branch
102 // instruction, otherwise it is null.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000103 MachineInstr *Branch = nullptr;
Richard Sandiford312425f2013-05-20 14:23:08 +0000104
Richard Sandifordc2312692014-03-06 10:38:30 +0000105 // The address that we currently assume the terminator has.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000106 uint64_t Address = 0;
Richard Sandiford312425f2013-05-20 14:23:08 +0000107
Richard Sandifordc2312692014-03-06 10:38:30 +0000108 // The current size of the terminator in bytes.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000109 uint64_t Size = 0;
Richard Sandiford312425f2013-05-20 14:23:08 +0000110
Richard Sandifordc2312692014-03-06 10:38:30 +0000111 // If Branch is nonnull, this is the number of the target block,
112 // otherwise it is unused.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000113 unsigned TargetBlock = 0;
Richard Sandiford312425f2013-05-20 14:23:08 +0000114
Richard Sandifordc2312692014-03-06 10:38:30 +0000115 // If Branch is nonnull, this is the length of the longest relaxed form,
116 // otherwise it is zero.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000117 unsigned ExtraRelaxSize = 0;
Richard Sandiford312425f2013-05-20 14:23:08 +0000118
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000119 TerminatorInfo() = default;
Richard Sandifordc2312692014-03-06 10:38:30 +0000120};
Richard Sandiford312425f2013-05-20 14:23:08 +0000121
Richard Sandifordc2312692014-03-06 10:38:30 +0000122// Used to keep track of the current position while iterating over the blocks.
123struct BlockPosition {
124 // The address that we assume this position has.
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000125 uint64_t Address = 0;
Richard Sandiford312425f2013-05-20 14:23:08 +0000126
Richard Sandifordc2312692014-03-06 10:38:30 +0000127 // The number of low bits in Address that are known to be the same
128 // as the runtime address.
129 unsigned KnownBits;
Richard Sandiford312425f2013-05-20 14:23:08 +0000130
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000131 BlockPosition(unsigned InitialAlignment) : KnownBits(InitialAlignment) {}
Richard Sandifordc2312692014-03-06 10:38:30 +0000132};
Richard Sandiford312425f2013-05-20 14:23:08 +0000133
Richard Sandifordc2312692014-03-06 10:38:30 +0000134class SystemZLongBranch : public MachineFunctionPass {
135public:
136 static char ID;
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000137
Richard Sandifordc2312692014-03-06 10:38:30 +0000138 SystemZLongBranch(const SystemZTargetMachine &tm)
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000139 : MachineFunctionPass(ID) {}
Richard Sandiford312425f2013-05-20 14:23:08 +0000140
Mehdi Amini117296c2016-10-01 02:56:57 +0000141 StringRef getPassName() const override { return "SystemZ Long Branch"; }
Richard Sandiford312425f2013-05-20 14:23:08 +0000142
Craig Topper9d74a5a2014-04-29 07:58:41 +0000143 bool runOnMachineFunction(MachineFunction &F) override;
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000144
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000145 MachineFunctionProperties getRequiredProperties() const override {
146 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000147 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000148 }
Richard Sandiford312425f2013-05-20 14:23:08 +0000149
Richard Sandifordc2312692014-03-06 10:38:30 +0000150private:
151 void skipNonTerminators(BlockPosition &Position, MBBInfo &Block);
152 void skipTerminator(BlockPosition &Position, TerminatorInfo &Terminator,
153 bool AssumeRelaxed);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000154 TerminatorInfo describeTerminator(MachineInstr &MI);
Richard Sandifordc2312692014-03-06 10:38:30 +0000155 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 Sandiford312425f2013-05-20 14:23:08 +0000163
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000164 const SystemZInstrInfo *TII = nullptr;
Richard Sandifordc2312692014-03-06 10:38:30 +0000165 MachineFunction *MF;
166 SmallVector<MBBInfo, 16> MBBs;
167 SmallVector<TerminatorInfo, 16> Terminators;
168};
Richard Sandiford312425f2013-05-20 14:23:08 +0000169
Richard Sandifordc2312692014-03-06 10:38:30 +0000170char SystemZLongBranch::ID = 0;
Richard Sandiford312425f2013-05-20 14:23:08 +0000171
Richard Sandifordc2312692014-03-06 10:38:30 +0000172const uint64_t MaxBackwardRange = 0x10000;
173const uint64_t MaxForwardRange = 0xfffe;
Richard Sandiford312425f2013-05-20 14:23:08 +0000174
Eugene Zelenko3943d2b2017-01-24 22:10:43 +0000175} // end anonymous namespace
Richard Sandiford312425f2013-05-20 14:23:08 +0000176
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.
180void 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.
204void 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 Smith9cfc75c2016-06-30 00:01:54 +0000214TerminatorInfo SystemZLongBranch::describeTerminator(MachineInstr &MI) {
Richard Sandiford312425f2013-05-20 14:23:08 +0000215 TerminatorInfo Terminator;
216 Terminator.Size = TII->getInstSizeInBytes(MI);
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000217 if (MI.isConditionalBranch() || MI.isUnconditionalBranch()) {
218 switch (MI.getOpcode()) {
Richard Sandiford312425f2013-05-20 14:23:08 +0000219 case SystemZ::J:
220 // Relaxes to JG, which is 2 bytes longer.
Richard Sandiford312425f2013-05-20 14:23:08 +0000221 Terminator.ExtraRelaxSize = 2;
222 break;
223 case SystemZ::BRC:
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000224 // Relaxes to BRCL, which is 2 bytes longer.
Richard Sandiford312425f2013-05-20 14:23:08 +0000225 Terminator.ExtraRelaxSize = 2;
226 break;
Richard Sandifordc2121252013-08-05 11:23:46 +0000227 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 Weigand75839912016-11-28 13:40:08 +0000232 case SystemZ::BRCTH:
233 // Never needs to be relaxed.
234 Terminator.ExtraRelaxSize = 0;
235 break;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000236 case SystemZ::CRJ:
Richard Sandiford93183ee2013-09-18 09:56:40 +0000237 case SystemZ::CLRJ:
238 // Relaxes to a C(L)R/BRCL sequence, which is 2 bytes longer.
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000239 Terminator.ExtraRelaxSize = 2;
240 break;
241 case SystemZ::CGRJ:
Richard Sandiford93183ee2013-09-18 09:56:40 +0000242 case SystemZ::CLGRJ:
243 // Relaxes to a C(L)GR/BRCL sequence, which is 4 bytes longer.
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000244 Terminator.ExtraRelaxSize = 4;
245 break;
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000246 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 Sandiford93183ee2013-09-18 09:56:40 +0000251 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 Sandiford312425f2013-05-20 14:23:08 +0000256 default:
257 llvm_unreachable("Unrecognized branch instruction");
258 }
Duncan P. N. Exon Smith9cfc75c2016-06-30 00:01:54 +0000259 Terminator.Branch = &MI;
Richard Sandiford53c9efd2013-05-28 10:13:54 +0000260 Terminator.TargetBlock =
261 TII->getBranchInfo(MI).Target->getMBB()->getNumber();
Richard Sandiford312425f2013-05-20 14:23:08 +0000262 }
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.
269uint64_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 Smith9cfc75c2016-06-30 00:01:54 +0000291 Block.Size += TII->getInstSizeInBytes(*MI);
Richard Sandiford312425f2013-05-20 14:23:08 +0000292 ++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 Smith9cfc75c2016-06-30 00:01:54 +0000300 Terminators.push_back(describeTerminator(*MI));
Richard Sandiford312425f2013-05-20 14:23:08 +0000301 skipTerminator(Position, Terminators.back(), false);
302 ++Block.NumTerminators;
303 }
304 ++MI;
305 }
306 }
307
308 return Position.Address;
309}
310
Richard Sandiford03528f32013-05-22 09:57:57 +0000311// Return true if, under current assumptions, Terminator would need to be
312// relaxed if it were placed at address Address.
313bool SystemZLongBranch::mustRelaxBranch(const TerminatorInfo &Terminator,
314 uint64_t Address) {
Richard Sandiford312425f2013-05-20 14:23:08 +0000315 if (!Terminator.Branch)
316 return false;
317
318 const MBBInfo &Target = MBBs[Terminator.TargetBlock];
Richard Sandiford03528f32013-05-22 09:57:57 +0000319 if (Address >= Target.Address) {
320 if (Address - Target.Address <= MaxBackwardRange)
Richard Sandiford312425f2013-05-20 14:23:08 +0000321 return false;
322 } else {
Richard Sandiford03528f32013-05-22 09:57:57 +0000323 if (Target.Address - Address <= MaxForwardRange)
Richard Sandiford312425f2013-05-20 14:23:08 +0000324 return false;
325 }
326
327 return true;
328}
329
330// Return true if, under current assumptions, any terminator needs
331// to be relaxed.
332bool SystemZLongBranch::mustRelaxABranch() {
Richard Sandiford28c111e2014-03-06 11:00:15 +0000333 for (auto &Terminator : Terminators)
334 if (mustRelaxBranch(Terminator, Terminator.Address))
Richard Sandiford312425f2013-05-20 14:23:08 +0000335 return true;
336 return false;
337}
338
339// Set the address of each block on the assumption that all branches
340// must be long.
341void SystemZLongBranch::setWorstCaseAddresses() {
342 SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin();
343 BlockPosition Position(MF->getAlignment());
Richard Sandiford28c111e2014-03-06 11:00:15 +0000344 for (auto &Block : MBBs) {
345 skipNonTerminators(Position, Block);
346 for (unsigned BTI = 0, BTE = Block.NumTerminators; BTI != BTE; ++BTI) {
Richard Sandiford312425f2013-05-20 14:23:08 +0000347 skipTerminator(Position, *TI, true);
348 ++TI;
349 }
350 }
351}
352
Richard Sandifordc2121252013-08-05 11:23:46 +0000353// Split BRANCH ON COUNT MI into the addition given by AddOpcode followed
354// by a BRCL on the result.
355void 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 Picus116bbab2017-01-13 09:58:52 +0000360 .add(MI->getOperand(0))
361 .add(MI->getOperand(1))
362 .addImm(-1);
Richard Sandifordc2121252013-08-05 11:23:46 +0000363 MachineInstr *BRCL = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BRCL))
Diana Picus116bbab2017-01-13 09:58:52 +0000364 .addImm(SystemZ::CCMASK_ICMP)
365 .addImm(SystemZ::CCMASK_CMP_NE)
366 .add(MI->getOperand(2));
Richard Sandifordc2121252013-08-05 11:23:46 +0000367 // The implicit use of CC is a killing use.
368 BRCL->addRegisterKilled(SystemZ::CC, &TII->getRegisterInfo());
369 MI->eraseFromParent();
370}
371
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000372// Split MI into the comparison given by CompareOpcode followed
373// a BRCL on the result.
374void 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 Picus116bbab2017-01-13 09:58:52 +0000379 .add(MI->getOperand(0))
380 .add(MI->getOperand(1));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000381 MachineInstr *BRCL = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BRCL))
Diana Picus116bbab2017-01-13 09:58:52 +0000382 .addImm(SystemZ::CCMASK_ICMP)
383 .add(MI->getOperand(2))
384 .add(MI->getOperand(3));
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000385 // The implicit use of CC is a killing use.
Richard Sandiford3d768e32013-07-31 12:30:20 +0000386 BRCL->addRegisterKilled(SystemZ::CC, &TII->getRegisterInfo());
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000387 MI->eraseFromParent();
388}
389
Richard Sandiford312425f2013-05-20 14:23:08 +0000390// Relax the branch described by Terminator.
391void SystemZLongBranch::relaxBranch(TerminatorInfo &Terminator) {
392 MachineInstr *Branch = Terminator.Branch;
393 switch (Branch->getOpcode()) {
Richard Sandiford3b105a02013-05-21 08:48:24 +0000394 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 Sandifordc2121252013-08-05 11:23:46 +0000400 case SystemZ::BRCT:
401 splitBranchOnCount(Branch, SystemZ::AHI);
402 break;
403 case SystemZ::BRCTG:
404 splitBranchOnCount(Branch, SystemZ::AGHI);
405 break;
Richard Sandiford0fb90ab2013-05-28 10:41:11 +0000406 case SystemZ::CRJ:
407 splitCompareBranch(Branch, SystemZ::CR);
408 break;
409 case SystemZ::CGRJ:
410 splitCompareBranch(Branch, SystemZ::CGR);
411 break;
Richard Sandiforde1d9f002013-05-29 11:58:52 +0000412 case SystemZ::CIJ:
413 splitCompareBranch(Branch, SystemZ::CHI);
414 break;
415 case SystemZ::CGIJ:
416 splitCompareBranch(Branch, SystemZ::CGHI);
417 break;
Richard Sandiford93183ee2013-09-18 09:56:40 +0000418 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 Sandiford3b105a02013-05-21 08:48:24 +0000430 default:
431 llvm_unreachable("Unrecognized branch");
432 }
Richard Sandiford312425f2013-05-20 14:23:08 +0000433
434 Terminator.Size += Terminator.ExtraRelaxSize;
435 Terminator.ExtraRelaxSize = 0;
Craig Topper062a2ba2014-04-25 05:30:21 +0000436 Terminator.Branch = nullptr;
Richard Sandiford312425f2013-05-20 14:23:08 +0000437
438 ++LongBranches;
439}
440
Richard Sandiford03528f32013-05-22 09:57:57 +0000441// Run a shortening pass and relax any branches that need to be relaxed.
Richard Sandiford312425f2013-05-20 14:23:08 +0000442void SystemZLongBranch::relaxBranches() {
Richard Sandiford03528f32013-05-22 09:57:57 +0000443 SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin();
444 BlockPosition Position(MF->getAlignment());
Richard Sandiford28c111e2014-03-06 11:00:15 +0000445 for (auto &Block : MBBs) {
446 skipNonTerminators(Position, Block);
447 for (unsigned BTI = 0, BTE = Block.NumTerminators; BTI != BTE; ++BTI) {
Richard Sandiford03528f32013-05-22 09:57:57 +0000448 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 Sandiford312425f2013-05-20 14:23:08 +0000456}
457
458bool SystemZLongBranch::runOnMachineFunction(MachineFunction &F) {
Eric Christopherfc6de422014-08-05 02:39:49 +0000459 TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
Richard Sandiford312425f2013-05-20 14:23:08 +0000460 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 Zelenko3943d2b2017-01-24 22:10:43 +0000469
470FunctionPass *llvm::createSystemZLongBranchPass(SystemZTargetMachine &TM) {
471 return new SystemZLongBranch(TM);
472}