1 //===-- SystemZLongBranch.cpp - Branch lengthening for SystemZ ------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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,
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?
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:
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.
44 // (2) Work out the address that each block would have if all branches
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.
54 //===----------------------------------------------------------------------===//
57 #include "SystemZInstrInfo.h"
58 #include "SystemZTargetMachine.h"
59 #include "llvm/ADT/SmallVector.h"
60 #include "llvm/ADT/Statistic.h"
61 #include "llvm/ADT/StringRef.h"
62 #include "llvm/CodeGen/MachineBasicBlock.h"
63 #include "llvm/CodeGen/MachineFunction.h"
64 #include "llvm/CodeGen/MachineFunctionPass.h"
65 #include "llvm/CodeGen/MachineInstr.h"
66 #include "llvm/CodeGen/MachineInstrBuilder.h"
67 #include "llvm/IR/DebugLoc.h"
68 #include "llvm/Support/ErrorHandling.h"
74 #define DEBUG_TYPE "systemz-long-branch"
76 STATISTIC(LongBranches
, "Number of long branches.");
80 // Represents positional information about a basic block.
82 // The address that we currently assume the block has.
85 // The size of the block in bytes, excluding terminators.
86 // This value never changes.
89 // The minimum alignment of the block, as a log2 value.
90 // This value never changes.
91 unsigned Alignment
= 0;
93 // The number of terminators in this block. This value never changes.
94 unsigned NumTerminators
= 0;
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.
103 MachineInstr
*Branch
= nullptr;
105 // The address that we currently assume the terminator has.
106 uint64_t Address
= 0;
108 // The current size of the terminator in bytes.
111 // If Branch is nonnull, this is the number of the target block,
112 // otherwise it is unused.
113 unsigned TargetBlock
= 0;
115 // If Branch is nonnull, this is the length of the longest relaxed form,
116 // otherwise it is zero.
117 unsigned ExtraRelaxSize
= 0;
119 TerminatorInfo() = default;
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.
125 uint64_t Address
= 0;
127 // The number of low bits in Address that are known to be the same
128 // as the runtime address.
131 BlockPosition(unsigned InitialAlignment
) : KnownBits(InitialAlignment
) {}
134 class SystemZLongBranch
: public MachineFunctionPass
{
138 SystemZLongBranch(const SystemZTargetMachine
&tm
)
139 : MachineFunctionPass(ID
) {}
141 StringRef
getPassName() const override
{ return "SystemZ Long Branch"; }
143 bool runOnMachineFunction(MachineFunction
&F
) override
;
145 MachineFunctionProperties
getRequiredProperties() const override
{
146 return MachineFunctionProperties().set(
147 MachineFunctionProperties::Property::NoVRegs
);
151 void skipNonTerminators(BlockPosition
&Position
, MBBInfo
&Block
);
152 void skipTerminator(BlockPosition
&Position
, TerminatorInfo
&Terminator
,
154 TerminatorInfo
describeTerminator(MachineInstr
&MI
);
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();
164 const SystemZInstrInfo
*TII
= nullptr;
166 SmallVector
<MBBInfo
, 16> MBBs
;
167 SmallVector
<TerminatorInfo
, 16> Terminators
;
170 char SystemZLongBranch::ID
= 0;
172 const uint64_t MaxBackwardRange
= 0x10000;
173 const uint64_t MaxForwardRange
= 0xfffe;
175 } // end anonymous namespace
177 // Position describes the state immediately before Block. Update Block
178 // accordingly and move Position to the end of the block's non-terminator
180 void SystemZLongBranch::skipNonTerminators(BlockPosition
&Position
,
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
;
190 // Align the addresses.
191 uint64_t AlignMask
= (uint64_t(1) << Block
.Alignment
) - 1;
192 Position
.Address
= (Position
.Address
+ AlignMask
) & ~AlignMask
;
194 // Record the block's position.
195 Block
.Address
= Position
.Address
;
197 // Move past the non-terminators in the block.
198 Position
.Address
+= Block
.Size
;
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
;
210 Position
.Address
+= Terminator
.ExtraRelaxSize
;
213 // Return a description of terminator instruction MI.
214 TerminatorInfo
SystemZLongBranch::describeTerminator(MachineInstr
&MI
) {
215 TerminatorInfo Terminator
;
216 Terminator
.Size
= TII
->getInstSizeInBytes(MI
);
217 if (MI
.isConditionalBranch() || MI
.isUnconditionalBranch()) {
218 switch (MI
.getOpcode()) {
220 // Relaxes to JG, which is 2 bytes longer.
221 Terminator
.ExtraRelaxSize
= 2;
224 // Relaxes to BRCL, which is 2 bytes longer.
225 Terminator
.ExtraRelaxSize
= 2;
229 // Relaxes to A(G)HI and BRCL, which is 6 bytes longer.
230 Terminator
.ExtraRelaxSize
= 6;
233 // Never needs to be relaxed.
234 Terminator
.ExtraRelaxSize
= 0;
238 // Relaxes to a C(L)R/BRCL sequence, which is 2 bytes longer.
239 Terminator
.ExtraRelaxSize
= 2;
243 // Relaxes to a C(L)GR/BRCL sequence, which is 4 bytes longer.
244 Terminator
.ExtraRelaxSize
= 4;
248 // Relaxes to a C(G)HI/BRCL sequence, which is 4 bytes longer.
249 Terminator
.ExtraRelaxSize
= 4;
253 // Relaxes to a CL(G)FI/BRCL sequence, which is 6 bytes longer.
254 Terminator
.ExtraRelaxSize
= 6;
257 llvm_unreachable("Unrecognized branch instruction");
259 Terminator
.Branch
= &MI
;
260 Terminator
.TargetBlock
=
261 TII
->getBranchInfo(MI
).Target
->getMBB()->getNumber();
266 // Fill MBBs and Terminators, setting the addresses on the assumption
267 // that no branches need relaxation. Return the size of the function under
269 uint64_t SystemZLongBranch::initMBBInfo() {
270 MF
->RenumberBlocks();
271 unsigned NumBlocks
= MF
->size();
274 MBBs
.resize(NumBlocks
);
277 Terminators
.reserve(NumBlocks
);
279 BlockPosition
Position(MF
->getAlignment());
280 for (unsigned I
= 0; I
< NumBlocks
; ++I
) {
281 MachineBasicBlock
*MBB
= MF
->getBlockNumbered(I
);
282 MBBInfo
&Block
= MBBs
[I
];
284 // Record the alignment, for quick access.
285 Block
.Alignment
= MBB
->getAlignment();
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()) {
291 Block
.Size
+= TII
->getInstSizeInBytes(*MI
);
294 skipNonTerminators(Position
, Block
);
296 // Add the terminators.
298 if (!MI
->isDebugInstr()) {
299 assert(MI
->isTerminator() && "Terminator followed by non-terminator");
300 Terminators
.push_back(describeTerminator(*MI
));
301 skipTerminator(Position
, Terminators
.back(), false);
302 ++Block
.NumTerminators
;
308 return Position
.Address
;
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
,
315 if (!Terminator
.Branch
|| Terminator
.ExtraRelaxSize
== 0)
318 const MBBInfo
&Target
= MBBs
[Terminator
.TargetBlock
];
319 if (Address
>= Target
.Address
) {
320 if (Address
- Target
.Address
<= MaxBackwardRange
)
323 if (Target
.Address
- Address
<= MaxForwardRange
)
330 // Return true if, under current assumptions, any terminator needs
332 bool SystemZLongBranch::mustRelaxABranch() {
333 for (auto &Terminator
: Terminators
)
334 if (mustRelaxBranch(Terminator
, Terminator
.Address
))
339 // Set the address of each block on the assumption that all branches
341 void SystemZLongBranch::setWorstCaseAddresses() {
342 SmallVector
<TerminatorInfo
, 16>::iterator TI
= Terminators
.begin();
343 BlockPosition
Position(MF
->getAlignment());
344 for (auto &Block
: MBBs
) {
345 skipNonTerminators(Position
, Block
);
346 for (unsigned BTI
= 0, BTE
= Block
.NumTerminators
; BTI
!= BTE
; ++BTI
) {
347 skipTerminator(Position
, *TI
, true);
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
))
360 .add(MI
->getOperand(0))
361 .add(MI
->getOperand(1))
363 MachineInstr
*BRCL
= BuildMI(*MBB
, MI
, DL
, TII
->get(SystemZ::BRCL
))
364 .addImm(SystemZ::CCMASK_ICMP
)
365 .addImm(SystemZ::CCMASK_CMP_NE
)
366 .add(MI
->getOperand(2));
367 // The implicit use of CC is a killing use.
368 BRCL
->addRegisterKilled(SystemZ::CC
, &TII
->getRegisterInfo());
369 MI
->eraseFromParent();
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
))
379 .add(MI
->getOperand(0))
380 .add(MI
->getOperand(1));
381 MachineInstr
*BRCL
= BuildMI(*MBB
, MI
, DL
, TII
->get(SystemZ::BRCL
))
382 .addImm(SystemZ::CCMASK_ICMP
)
383 .add(MI
->getOperand(2))
384 .add(MI
->getOperand(3));
385 // The implicit use of CC is a killing use.
386 BRCL
->addRegisterKilled(SystemZ::CC
, &TII
->getRegisterInfo());
387 MI
->eraseFromParent();
390 // Relax the branch described by Terminator.
391 void SystemZLongBranch::relaxBranch(TerminatorInfo
&Terminator
) {
392 MachineInstr
*Branch
= Terminator
.Branch
;
393 switch (Branch
->getOpcode()) {
395 Branch
->setDesc(TII
->get(SystemZ::JG
));
398 Branch
->setDesc(TII
->get(SystemZ::BRCL
));
401 splitBranchOnCount(Branch
, SystemZ::AHI
);
404 splitBranchOnCount(Branch
, SystemZ::AGHI
);
407 splitCompareBranch(Branch
, SystemZ::CR
);
410 splitCompareBranch(Branch
, SystemZ::CGR
);
413 splitCompareBranch(Branch
, SystemZ::CHI
);
416 splitCompareBranch(Branch
, SystemZ::CGHI
);
419 splitCompareBranch(Branch
, SystemZ::CLR
);
422 splitCompareBranch(Branch
, SystemZ::CLGR
);
425 splitCompareBranch(Branch
, SystemZ::CLFI
);
428 splitCompareBranch(Branch
, SystemZ::CLGFI
);
431 llvm_unreachable("Unrecognized branch");
434 Terminator
.Size
+= Terminator
.ExtraRelaxSize
;
435 Terminator
.ExtraRelaxSize
= 0;
436 Terminator
.Branch
= nullptr;
441 // Run a shortening pass and relax any branches that need to be relaxed.
442 void SystemZLongBranch::relaxBranches() {
443 SmallVector
<TerminatorInfo
, 16>::iterator TI
= Terminators
.begin();
444 BlockPosition
Position(MF
->getAlignment());
445 for (auto &Block
: MBBs
) {
446 skipNonTerminators(Position
, Block
);
447 for (unsigned BTI
= 0, BTE
= Block
.NumTerminators
; BTI
!= BTE
; ++BTI
) {
448 assert(Position
.Address
<= TI
->Address
&&
449 "Addresses shouldn't go forwards");
450 if (mustRelaxBranch(*TI
, Position
.Address
))
452 skipTerminator(Position
, *TI
, false);
458 bool SystemZLongBranch::runOnMachineFunction(MachineFunction
&F
) {
459 TII
= static_cast<const SystemZInstrInfo
*>(F
.getSubtarget().getInstrInfo());
461 uint64_t Size
= initMBBInfo();
462 if (Size
<= MaxForwardRange
|| !mustRelaxABranch())
465 setWorstCaseAddresses();
470 FunctionPass
*llvm::createSystemZLongBranchPass(SystemZTargetMachine
&TM
) {
471 return new SystemZLongBranch(TM
);