1 //===-- SystemZLongBranch.cpp - Branch lengthening for SystemZ ------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This pass makes sure that all branches are in range. There are several ways
10 // in which this could be done. One aggressive approach is to assume that all
11 // branches are in range and successively replace those that turn out not
12 // to be in range with a longer form (branch relaxation). A simple
13 // implementation is to continually walk through the function relaxing
14 // branches until no more changes are needed and a fixed point is reached.
15 // However, in the pathological worst case, this implementation is
16 // quadratic in the number of blocks; relaxing branch N can make branch N-1
17 // go out of range, which in turn can make branch N-2 go out of range,
20 // An alternative approach is to assume that all branches must be
21 // converted to their long forms, then reinstate the short forms of
22 // branches that, even under this pessimistic assumption, turn out to be
23 // in range (branch shortening). This too can be implemented as a function
24 // walk that is repeated until a fixed point is reached. In general,
25 // the result of shortening is not as good as that of relaxation, and
26 // shortening is also quadratic in the worst case; shortening branch N
27 // can bring branch N-1 in range of the short form, which in turn can do
28 // the same for branch N-2, and so on. The main advantage of shortening
29 // is that each walk through the function produces valid code, so it is
30 // possible to stop at any point after the first walk. The quadraticness
31 // could therefore be handled with a maximum pass count, although the
32 // question then becomes: what maximum count should be used?
34 // On SystemZ, long branches are only needed for functions bigger than 64k,
35 // which are relatively rare to begin with, and the long branch sequences
36 // are actually relatively cheap. It therefore doesn't seem worth spending
37 // much compilation time on the problem. Instead, the approach we take is:
39 // (1) Work out the address that each block would have if no branches
40 // need relaxing. Exit the pass early if all branches are in range
41 // according to this assumption.
43 // (2) Work out the address that each block would have if all branches
46 // (3) Walk through the block calculating the final address of each instruction
47 // and relaxing those that need to be relaxed. For backward branches,
48 // this check uses the final address of the target block, as calculated
49 // earlier in the walk. For forward branches, this check uses the
50 // address of the target block that was calculated in (2). Both checks
51 // give a conservatively-correct range.
53 //===----------------------------------------------------------------------===//
56 #include "SystemZInstrInfo.h"
57 #include "SystemZTargetMachine.h"
58 #include "llvm/ADT/SmallVector.h"
59 #include "llvm/ADT/Statistic.h"
60 #include "llvm/ADT/StringRef.h"
61 #include "llvm/CodeGen/MachineBasicBlock.h"
62 #include "llvm/CodeGen/MachineFunction.h"
63 #include "llvm/CodeGen/MachineFunctionPass.h"
64 #include "llvm/CodeGen/MachineInstr.h"
65 #include "llvm/CodeGen/MachineInstrBuilder.h"
66 #include "llvm/IR/DebugLoc.h"
67 #include "llvm/Support/ErrorHandling.h"
73 #define DEBUG_TYPE "systemz-long-branch"
75 STATISTIC(LongBranches
, "Number of long branches.");
79 // Represents positional information about a basic block.
81 // The address that we currently assume the block has.
84 // The size of the block in bytes, excluding terminators.
85 // This value never changes.
88 // The minimum alignment of the block.
89 // This value never changes.
92 // The number of terminators in this block. This value never changes.
93 unsigned NumTerminators
= 0;
98 // Represents the state of a block terminator.
99 struct TerminatorInfo
{
100 // If this terminator is a relaxable branch, this points to the branch
101 // instruction, otherwise it is null.
102 MachineInstr
*Branch
= nullptr;
104 // The address that we currently assume the terminator has.
105 uint64_t Address
= 0;
107 // The current size of the terminator in bytes.
110 // If Branch is nonnull, this is the number of the target block,
111 // otherwise it is unused.
112 unsigned TargetBlock
= 0;
114 // If Branch is nonnull, this is the length of the longest relaxed form,
115 // otherwise it is zero.
116 unsigned ExtraRelaxSize
= 0;
118 TerminatorInfo() = default;
121 // Used to keep track of the current position while iterating over the blocks.
122 struct BlockPosition
{
123 // The address that we assume this position has.
124 uint64_t Address
= 0;
126 // The number of low bits in Address that are known to be the same
127 // as the runtime address.
130 BlockPosition(unsigned InitialLogAlignment
)
131 : KnownBits(InitialLogAlignment
) {}
134 class SystemZLongBranch
: public MachineFunctionPass
{
138 SystemZLongBranch() : MachineFunctionPass(ID
) {
139 initializeSystemZLongBranchPass(*PassRegistry::getPassRegistry());
142 bool runOnMachineFunction(MachineFunction
&F
) override
;
144 MachineFunctionProperties
getRequiredProperties() const override
{
145 return MachineFunctionProperties().set(
146 MachineFunctionProperties::Property::NoVRegs
);
150 void skipNonTerminators(BlockPosition
&Position
, MBBInfo
&Block
);
151 void skipTerminator(BlockPosition
&Position
, TerminatorInfo
&Terminator
,
153 TerminatorInfo
describeTerminator(MachineInstr
&MI
);
154 uint64_t initMBBInfo();
155 bool mustRelaxBranch(const TerminatorInfo
&Terminator
, uint64_t Address
);
156 bool mustRelaxABranch();
157 void setWorstCaseAddresses();
158 void splitBranchOnCount(MachineInstr
*MI
, unsigned AddOpcode
);
159 void splitCompareBranch(MachineInstr
*MI
, unsigned CompareOpcode
);
160 void relaxBranch(TerminatorInfo
&Terminator
);
161 void relaxBranches();
163 const SystemZInstrInfo
*TII
= nullptr;
164 MachineFunction
*MF
= nullptr;
165 SmallVector
<MBBInfo
, 16> MBBs
;
166 SmallVector
<TerminatorInfo
, 16> Terminators
;
169 char SystemZLongBranch::ID
= 0;
171 const uint64_t MaxBackwardRange
= 0x10000;
172 const uint64_t MaxForwardRange
= 0xfffe;
174 } // end anonymous namespace
176 INITIALIZE_PASS(SystemZLongBranch
, DEBUG_TYPE
, "SystemZ Long Branch", false,
179 // Position describes the state immediately before Block. Update Block
180 // accordingly and move Position to the end of the block's non-terminator
182 void SystemZLongBranch::skipNonTerminators(BlockPosition
&Position
,
184 if (Log2(Block
.Alignment
) > Position
.KnownBits
) {
185 // When calculating the address of Block, we need to conservatively
186 // assume that Block had the worst possible misalignment.
188 (Block
.Alignment
.value() - (uint64_t(1) << Position
.KnownBits
));
189 Position
.KnownBits
= Log2(Block
.Alignment
);
192 // Align the addresses.
193 Position
.Address
= alignTo(Position
.Address
, Block
.Alignment
);
195 // Record the block's position.
196 Block
.Address
= Position
.Address
;
198 // Move past the non-terminators in the block.
199 Position
.Address
+= Block
.Size
;
202 // Position describes the state immediately before Terminator.
203 // Update Terminator accordingly and move Position past it.
204 // Assume that Terminator will be relaxed if AssumeRelaxed.
205 void SystemZLongBranch::skipTerminator(BlockPosition
&Position
,
206 TerminatorInfo
&Terminator
,
207 bool AssumeRelaxed
) {
208 Terminator
.Address
= Position
.Address
;
209 Position
.Address
+= Terminator
.Size
;
211 Position
.Address
+= Terminator
.ExtraRelaxSize
;
214 static unsigned getInstSizeInBytes(const MachineInstr
&MI
,
215 const SystemZInstrInfo
*TII
) {
216 unsigned Size
= TII
->getInstSizeInBytes(MI
);
218 // These do not have a size:
219 MI
.isDebugOrPseudoInstr() || MI
.isPosition() || MI
.isKill() ||
220 MI
.isImplicitDef() || MI
.getOpcode() == TargetOpcode::MEMBARRIER
||
221 // These have a size that may be zero:
222 MI
.isInlineAsm() || MI
.getOpcode() == SystemZ::STACKMAP
||
223 MI
.getOpcode() == SystemZ::PATCHPOINT
||
224 // EH_SjLj_Setup is a dummy terminator instruction of size 0,
225 // It is used to handle the clobber register for builtin setjmp.
226 MI
.getOpcode() == SystemZ::EH_SjLj_Setup
) &&
227 "Missing size value for instruction.");
231 // Return a description of terminator instruction MI.
232 TerminatorInfo
SystemZLongBranch::describeTerminator(MachineInstr
&MI
) {
233 TerminatorInfo Terminator
;
234 Terminator
.Size
= getInstSizeInBytes(MI
, TII
);
235 if (MI
.isConditionalBranch() || MI
.isUnconditionalBranch()) {
236 switch (MI
.getOpcode()) {
238 // Relaxes to JG, which is 2 bytes longer.
239 Terminator
.ExtraRelaxSize
= 2;
242 // Relaxes to BRCL, which is 2 bytes longer.
243 Terminator
.ExtraRelaxSize
= 2;
247 // Relaxes to A(G)HI and BRCL, which is 6 bytes longer.
248 Terminator
.ExtraRelaxSize
= 6;
251 // Never needs to be relaxed.
252 Terminator
.ExtraRelaxSize
= 0;
256 // Relaxes to a C(L)R/BRCL sequence, which is 2 bytes longer.
257 Terminator
.ExtraRelaxSize
= 2;
261 // Relaxes to a C(L)GR/BRCL sequence, which is 4 bytes longer.
262 Terminator
.ExtraRelaxSize
= 4;
266 // Relaxes to a C(G)HI/BRCL sequence, which is 4 bytes longer.
267 Terminator
.ExtraRelaxSize
= 4;
271 // Relaxes to a CL(G)FI/BRCL sequence, which is 6 bytes longer.
272 Terminator
.ExtraRelaxSize
= 6;
275 llvm_unreachable("Unrecognized branch instruction");
277 Terminator
.Branch
= &MI
;
278 Terminator
.TargetBlock
=
279 TII
->getBranchInfo(MI
).getMBBTarget()->getNumber();
284 // Fill MBBs and Terminators, setting the addresses on the assumption
285 // that no branches need relaxation. Return the size of the function under
287 uint64_t SystemZLongBranch::initMBBInfo() {
288 MF
->RenumberBlocks();
289 unsigned NumBlocks
= MF
->size();
292 MBBs
.resize(NumBlocks
);
295 Terminators
.reserve(NumBlocks
);
297 BlockPosition
Position(Log2(MF
->getAlignment()));
298 for (unsigned I
= 0; I
< NumBlocks
; ++I
) {
299 MachineBasicBlock
*MBB
= MF
->getBlockNumbered(I
);
300 MBBInfo
&Block
= MBBs
[I
];
302 // Record the alignment, for quick access.
303 Block
.Alignment
= MBB
->getAlignment();
305 // Calculate the size of the fixed part of the block.
306 MachineBasicBlock::iterator MI
= MBB
->begin();
307 MachineBasicBlock::iterator End
= MBB
->end();
308 while (MI
!= End
&& !MI
->isTerminator()) {
309 Block
.Size
+= getInstSizeInBytes(*MI
, TII
);
312 skipNonTerminators(Position
, Block
);
314 // Add the terminators.
316 if (!MI
->isDebugInstr()) {
317 assert(MI
->isTerminator() && "Terminator followed by non-terminator");
318 Terminators
.push_back(describeTerminator(*MI
));
319 skipTerminator(Position
, Terminators
.back(), false);
320 ++Block
.NumTerminators
;
326 return Position
.Address
;
329 // Return true if, under current assumptions, Terminator would need to be
330 // relaxed if it were placed at address Address.
331 bool SystemZLongBranch::mustRelaxBranch(const TerminatorInfo
&Terminator
,
333 if (!Terminator
.Branch
|| Terminator
.ExtraRelaxSize
== 0)
336 const MBBInfo
&Target
= MBBs
[Terminator
.TargetBlock
];
337 if (Address
>= Target
.Address
) {
338 if (Address
- Target
.Address
<= MaxBackwardRange
)
341 if (Target
.Address
- Address
<= MaxForwardRange
)
348 // Return true if, under current assumptions, any terminator needs
350 bool SystemZLongBranch::mustRelaxABranch() {
351 for (auto &Terminator
: Terminators
)
352 if (mustRelaxBranch(Terminator
, Terminator
.Address
))
357 // Set the address of each block on the assumption that all branches
359 void SystemZLongBranch::setWorstCaseAddresses() {
360 SmallVector
<TerminatorInfo
, 16>::iterator TI
= Terminators
.begin();
361 BlockPosition
Position(Log2(MF
->getAlignment()));
362 for (auto &Block
: MBBs
) {
363 skipNonTerminators(Position
, Block
);
364 for (unsigned BTI
= 0, BTE
= Block
.NumTerminators
; BTI
!= BTE
; ++BTI
) {
365 skipTerminator(Position
, *TI
, true);
371 // Split BRANCH ON COUNT MI into the addition given by AddOpcode followed
372 // by a BRCL on the result.
373 void SystemZLongBranch::splitBranchOnCount(MachineInstr
*MI
,
374 unsigned AddOpcode
) {
375 MachineBasicBlock
*MBB
= MI
->getParent();
376 DebugLoc DL
= MI
->getDebugLoc();
377 BuildMI(*MBB
, MI
, DL
, TII
->get(AddOpcode
))
378 .add(MI
->getOperand(0))
379 .add(MI
->getOperand(1))
381 MachineInstr
*BRCL
= BuildMI(*MBB
, MI
, DL
, TII
->get(SystemZ::BRCL
))
382 .addImm(SystemZ::CCMASK_ICMP
)
383 .addImm(SystemZ::CCMASK_CMP_NE
)
384 .add(MI
->getOperand(2));
385 // The implicit use of CC is a killing use.
386 BRCL
->addRegisterKilled(SystemZ::CC
, &TII
->getRegisterInfo());
387 MI
->eraseFromParent();
390 // Split MI into the comparison given by CompareOpcode followed
391 // a BRCL on the result.
392 void SystemZLongBranch::splitCompareBranch(MachineInstr
*MI
,
393 unsigned CompareOpcode
) {
394 MachineBasicBlock
*MBB
= MI
->getParent();
395 DebugLoc DL
= MI
->getDebugLoc();
396 BuildMI(*MBB
, MI
, DL
, TII
->get(CompareOpcode
))
397 .add(MI
->getOperand(0))
398 .add(MI
->getOperand(1));
399 MachineInstr
*BRCL
= BuildMI(*MBB
, MI
, DL
, TII
->get(SystemZ::BRCL
))
400 .addImm(SystemZ::CCMASK_ICMP
)
401 .add(MI
->getOperand(2))
402 .add(MI
->getOperand(3));
403 // The implicit use of CC is a killing use.
404 BRCL
->addRegisterKilled(SystemZ::CC
, &TII
->getRegisterInfo());
405 MI
->eraseFromParent();
408 // Relax the branch described by Terminator.
409 void SystemZLongBranch::relaxBranch(TerminatorInfo
&Terminator
) {
410 MachineInstr
*Branch
= Terminator
.Branch
;
411 switch (Branch
->getOpcode()) {
413 Branch
->setDesc(TII
->get(SystemZ::JG
));
416 Branch
->setDesc(TII
->get(SystemZ::BRCL
));
419 splitBranchOnCount(Branch
, SystemZ::AHI
);
422 splitBranchOnCount(Branch
, SystemZ::AGHI
);
425 splitCompareBranch(Branch
, SystemZ::CR
);
428 splitCompareBranch(Branch
, SystemZ::CGR
);
431 splitCompareBranch(Branch
, SystemZ::CHI
);
434 splitCompareBranch(Branch
, SystemZ::CGHI
);
437 splitCompareBranch(Branch
, SystemZ::CLR
);
440 splitCompareBranch(Branch
, SystemZ::CLGR
);
443 splitCompareBranch(Branch
, SystemZ::CLFI
);
446 splitCompareBranch(Branch
, SystemZ::CLGFI
);
449 llvm_unreachable("Unrecognized branch");
452 Terminator
.Size
+= Terminator
.ExtraRelaxSize
;
453 Terminator
.ExtraRelaxSize
= 0;
454 Terminator
.Branch
= nullptr;
459 // Run a shortening pass and relax any branches that need to be relaxed.
460 void SystemZLongBranch::relaxBranches() {
461 SmallVector
<TerminatorInfo
, 16>::iterator TI
= Terminators
.begin();
462 BlockPosition
Position(Log2(MF
->getAlignment()));
463 for (auto &Block
: MBBs
) {
464 skipNonTerminators(Position
, Block
);
465 for (unsigned BTI
= 0, BTE
= Block
.NumTerminators
; BTI
!= BTE
; ++BTI
) {
466 assert(Position
.Address
<= TI
->Address
&&
467 "Addresses shouldn't go forwards");
468 if (mustRelaxBranch(*TI
, Position
.Address
))
470 skipTerminator(Position
, *TI
, false);
476 bool SystemZLongBranch::runOnMachineFunction(MachineFunction
&F
) {
477 TII
= static_cast<const SystemZInstrInfo
*>(F
.getSubtarget().getInstrInfo());
479 uint64_t Size
= initMBBInfo();
480 if (Size
<= MaxForwardRange
|| !mustRelaxABranch())
483 setWorstCaseAddresses();
488 FunctionPass
*llvm::createSystemZLongBranchPass(SystemZTargetMachine
&TM
) {
489 return new SystemZLongBranch();