xref: /aosp_15_r20/external/llvm/lib/Target/SystemZ/SystemZLongBranch.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===-- SystemZLongBranch.cpp - Branch lengthening for SystemZ ------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This pass makes sure that all branches are in range.  There are several ways
11*9880d681SAndroid Build Coastguard Worker // in which this could be done.  One aggressive approach is to assume that all
12*9880d681SAndroid Build Coastguard Worker // branches are in range and successively replace those that turn out not
13*9880d681SAndroid Build Coastguard Worker // to be in range with a longer form (branch relaxation).  A simple
14*9880d681SAndroid Build Coastguard Worker // implementation is to continually walk through the function relaxing
15*9880d681SAndroid Build Coastguard Worker // branches until no more changes are needed and a fixed point is reached.
16*9880d681SAndroid Build Coastguard Worker // However, in the pathological worst case, this implementation is
17*9880d681SAndroid Build Coastguard Worker // quadratic in the number of blocks; relaxing branch N can make branch N-1
18*9880d681SAndroid Build Coastguard Worker // go out of range, which in turn can make branch N-2 go out of range,
19*9880d681SAndroid Build Coastguard Worker // and so on.
20*9880d681SAndroid Build Coastguard Worker //
21*9880d681SAndroid Build Coastguard Worker // An alternative approach is to assume that all branches must be
22*9880d681SAndroid Build Coastguard Worker // converted to their long forms, then reinstate the short forms of
23*9880d681SAndroid Build Coastguard Worker // branches that, even under this pessimistic assumption, turn out to be
24*9880d681SAndroid Build Coastguard Worker // in range (branch shortening).  This too can be implemented as a function
25*9880d681SAndroid Build Coastguard Worker // walk that is repeated until a fixed point is reached.  In general,
26*9880d681SAndroid Build Coastguard Worker // the result of shortening is not as good as that of relaxation, and
27*9880d681SAndroid Build Coastguard Worker // shortening is also quadratic in the worst case; shortening branch N
28*9880d681SAndroid Build Coastguard Worker // can bring branch N-1 in range of the short form, which in turn can do
29*9880d681SAndroid Build Coastguard Worker // the same for branch N-2, and so on.  The main advantage of shortening
30*9880d681SAndroid Build Coastguard Worker // is that each walk through the function produces valid code, so it is
31*9880d681SAndroid Build Coastguard Worker // possible to stop at any point after the first walk.  The quadraticness
32*9880d681SAndroid Build Coastguard Worker // could therefore be handled with a maximum pass count, although the
33*9880d681SAndroid Build Coastguard Worker // question then becomes: what maximum count should be used?
34*9880d681SAndroid Build Coastguard Worker //
35*9880d681SAndroid Build Coastguard Worker // On SystemZ, long branches are only needed for functions bigger than 64k,
36*9880d681SAndroid Build Coastguard Worker // which are relatively rare to begin with, and the long branch sequences
37*9880d681SAndroid Build Coastguard Worker // are actually relatively cheap.  It therefore doesn't seem worth spending
38*9880d681SAndroid Build Coastguard Worker // much compilation time on the problem.  Instead, the approach we take is:
39*9880d681SAndroid Build Coastguard Worker //
40*9880d681SAndroid Build Coastguard Worker // (1) Work out the address that each block would have if no branches
41*9880d681SAndroid Build Coastguard Worker //     need relaxing.  Exit the pass early if all branches are in range
42*9880d681SAndroid Build Coastguard Worker //     according to this assumption.
43*9880d681SAndroid Build Coastguard Worker //
44*9880d681SAndroid Build Coastguard Worker // (2) Work out the address that each block would have if all branches
45*9880d681SAndroid Build Coastguard Worker //     need relaxing.
46*9880d681SAndroid Build Coastguard Worker //
47*9880d681SAndroid Build Coastguard Worker // (3) Walk through the block calculating the final address of each instruction
48*9880d681SAndroid Build Coastguard Worker //     and relaxing those that need to be relaxed.  For backward branches,
49*9880d681SAndroid Build Coastguard Worker //     this check uses the final address of the target block, as calculated
50*9880d681SAndroid Build Coastguard Worker //     earlier in the walk.  For forward branches, this check uses the
51*9880d681SAndroid Build Coastguard Worker //     address of the target block that was calculated in (2).  Both checks
52*9880d681SAndroid Build Coastguard Worker //     give a conservatively-correct range.
53*9880d681SAndroid Build Coastguard Worker //
54*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
55*9880d681SAndroid Build Coastguard Worker 
56*9880d681SAndroid Build Coastguard Worker #include "SystemZTargetMachine.h"
57*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
58*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
59*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
60*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
61*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/MathExtras.h"
62*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
63*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
64*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
65*9880d681SAndroid Build Coastguard Worker 
66*9880d681SAndroid Build Coastguard Worker using namespace llvm;
67*9880d681SAndroid Build Coastguard Worker 
68*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "systemz-long-branch"
69*9880d681SAndroid Build Coastguard Worker 
70*9880d681SAndroid Build Coastguard Worker STATISTIC(LongBranches, "Number of long branches.");
71*9880d681SAndroid Build Coastguard Worker 
72*9880d681SAndroid Build Coastguard Worker namespace {
73*9880d681SAndroid Build Coastguard Worker // Represents positional information about a basic block.
74*9880d681SAndroid Build Coastguard Worker struct MBBInfo {
75*9880d681SAndroid Build Coastguard Worker   // The address that we currently assume the block has.
76*9880d681SAndroid Build Coastguard Worker   uint64_t Address;
77*9880d681SAndroid Build Coastguard Worker 
78*9880d681SAndroid Build Coastguard Worker   // The size of the block in bytes, excluding terminators.
79*9880d681SAndroid Build Coastguard Worker   // This value never changes.
80*9880d681SAndroid Build Coastguard Worker   uint64_t Size;
81*9880d681SAndroid Build Coastguard Worker 
82*9880d681SAndroid Build Coastguard Worker   // The minimum alignment of the block, as a log2 value.
83*9880d681SAndroid Build Coastguard Worker   // This value never changes.
84*9880d681SAndroid Build Coastguard Worker   unsigned Alignment;
85*9880d681SAndroid Build Coastguard Worker 
86*9880d681SAndroid Build Coastguard Worker   // The number of terminators in this block.  This value never changes.
87*9880d681SAndroid Build Coastguard Worker   unsigned NumTerminators;
88*9880d681SAndroid Build Coastguard Worker 
MBBInfo__anon4767eb890111::MBBInfo89*9880d681SAndroid Build Coastguard Worker   MBBInfo()
90*9880d681SAndroid Build Coastguard Worker     : Address(0), Size(0), Alignment(0), NumTerminators(0) {}
91*9880d681SAndroid Build Coastguard Worker };
92*9880d681SAndroid Build Coastguard Worker 
93*9880d681SAndroid Build Coastguard Worker // Represents the state of a block terminator.
94*9880d681SAndroid Build Coastguard Worker struct TerminatorInfo {
95*9880d681SAndroid Build Coastguard Worker   // If this terminator is a relaxable branch, this points to the branch
96*9880d681SAndroid Build Coastguard Worker   // instruction, otherwise it is null.
97*9880d681SAndroid Build Coastguard Worker   MachineInstr *Branch;
98*9880d681SAndroid Build Coastguard Worker 
99*9880d681SAndroid Build Coastguard Worker   // The address that we currently assume the terminator has.
100*9880d681SAndroid Build Coastguard Worker   uint64_t Address;
101*9880d681SAndroid Build Coastguard Worker 
102*9880d681SAndroid Build Coastguard Worker   // The current size of the terminator in bytes.
103*9880d681SAndroid Build Coastguard Worker   uint64_t Size;
104*9880d681SAndroid Build Coastguard Worker 
105*9880d681SAndroid Build Coastguard Worker   // If Branch is nonnull, this is the number of the target block,
106*9880d681SAndroid Build Coastguard Worker   // otherwise it is unused.
107*9880d681SAndroid Build Coastguard Worker   unsigned TargetBlock;
108*9880d681SAndroid Build Coastguard Worker 
109*9880d681SAndroid Build Coastguard Worker   // If Branch is nonnull, this is the length of the longest relaxed form,
110*9880d681SAndroid Build Coastguard Worker   // otherwise it is zero.
111*9880d681SAndroid Build Coastguard Worker   unsigned ExtraRelaxSize;
112*9880d681SAndroid Build Coastguard Worker 
TerminatorInfo__anon4767eb890111::TerminatorInfo113*9880d681SAndroid Build Coastguard Worker   TerminatorInfo() : Branch(nullptr), Size(0), TargetBlock(0),
114*9880d681SAndroid Build Coastguard Worker                      ExtraRelaxSize(0) {}
115*9880d681SAndroid Build Coastguard Worker };
116*9880d681SAndroid Build Coastguard Worker 
117*9880d681SAndroid Build Coastguard Worker // Used to keep track of the current position while iterating over the blocks.
118*9880d681SAndroid Build Coastguard Worker struct BlockPosition {
119*9880d681SAndroid Build Coastguard Worker   // The address that we assume this position has.
120*9880d681SAndroid Build Coastguard Worker   uint64_t Address;
121*9880d681SAndroid Build Coastguard Worker 
122*9880d681SAndroid Build Coastguard Worker   // The number of low bits in Address that are known to be the same
123*9880d681SAndroid Build Coastguard Worker   // as the runtime address.
124*9880d681SAndroid Build Coastguard Worker   unsigned KnownBits;
125*9880d681SAndroid Build Coastguard Worker 
BlockPosition__anon4767eb890111::BlockPosition126*9880d681SAndroid Build Coastguard Worker   BlockPosition(unsigned InitialAlignment)
127*9880d681SAndroid Build Coastguard Worker     : Address(0), KnownBits(InitialAlignment) {}
128*9880d681SAndroid Build Coastguard Worker };
129*9880d681SAndroid Build Coastguard Worker 
130*9880d681SAndroid Build Coastguard Worker class SystemZLongBranch : public MachineFunctionPass {
131*9880d681SAndroid Build Coastguard Worker public:
132*9880d681SAndroid Build Coastguard Worker   static char ID;
SystemZLongBranch(const SystemZTargetMachine & tm)133*9880d681SAndroid Build Coastguard Worker   SystemZLongBranch(const SystemZTargetMachine &tm)
134*9880d681SAndroid Build Coastguard Worker     : MachineFunctionPass(ID), TII(nullptr) {}
135*9880d681SAndroid Build Coastguard Worker 
getPassName() const136*9880d681SAndroid Build Coastguard Worker   const char *getPassName() const override {
137*9880d681SAndroid Build Coastguard Worker     return "SystemZ Long Branch";
138*9880d681SAndroid Build Coastguard Worker   }
139*9880d681SAndroid Build Coastguard Worker 
140*9880d681SAndroid Build Coastguard Worker   bool runOnMachineFunction(MachineFunction &F) override;
getRequiredProperties() const141*9880d681SAndroid Build Coastguard Worker   MachineFunctionProperties getRequiredProperties() const override {
142*9880d681SAndroid Build Coastguard Worker     return MachineFunctionProperties().set(
143*9880d681SAndroid Build Coastguard Worker         MachineFunctionProperties::Property::AllVRegsAllocated);
144*9880d681SAndroid Build Coastguard Worker   }
145*9880d681SAndroid Build Coastguard Worker 
146*9880d681SAndroid Build Coastguard Worker private:
147*9880d681SAndroid Build Coastguard Worker   void skipNonTerminators(BlockPosition &Position, MBBInfo &Block);
148*9880d681SAndroid Build Coastguard Worker   void skipTerminator(BlockPosition &Position, TerminatorInfo &Terminator,
149*9880d681SAndroid Build Coastguard Worker                       bool AssumeRelaxed);
150*9880d681SAndroid Build Coastguard Worker   TerminatorInfo describeTerminator(MachineInstr &MI);
151*9880d681SAndroid Build Coastguard Worker   uint64_t initMBBInfo();
152*9880d681SAndroid Build Coastguard Worker   bool mustRelaxBranch(const TerminatorInfo &Terminator, uint64_t Address);
153*9880d681SAndroid Build Coastguard Worker   bool mustRelaxABranch();
154*9880d681SAndroid Build Coastguard Worker   void setWorstCaseAddresses();
155*9880d681SAndroid Build Coastguard Worker   void splitBranchOnCount(MachineInstr *MI, unsigned AddOpcode);
156*9880d681SAndroid Build Coastguard Worker   void splitCompareBranch(MachineInstr *MI, unsigned CompareOpcode);
157*9880d681SAndroid Build Coastguard Worker   void relaxBranch(TerminatorInfo &Terminator);
158*9880d681SAndroid Build Coastguard Worker   void relaxBranches();
159*9880d681SAndroid Build Coastguard Worker 
160*9880d681SAndroid Build Coastguard Worker   const SystemZInstrInfo *TII;
161*9880d681SAndroid Build Coastguard Worker   MachineFunction *MF;
162*9880d681SAndroid Build Coastguard Worker   SmallVector<MBBInfo, 16> MBBs;
163*9880d681SAndroid Build Coastguard Worker   SmallVector<TerminatorInfo, 16> Terminators;
164*9880d681SAndroid Build Coastguard Worker };
165*9880d681SAndroid Build Coastguard Worker 
166*9880d681SAndroid Build Coastguard Worker char SystemZLongBranch::ID = 0;
167*9880d681SAndroid Build Coastguard Worker 
168*9880d681SAndroid Build Coastguard Worker const uint64_t MaxBackwardRange = 0x10000;
169*9880d681SAndroid Build Coastguard Worker const uint64_t MaxForwardRange = 0xfffe;
170*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
171*9880d681SAndroid Build Coastguard Worker 
createSystemZLongBranchPass(SystemZTargetMachine & TM)172*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createSystemZLongBranchPass(SystemZTargetMachine &TM) {
173*9880d681SAndroid Build Coastguard Worker   return new SystemZLongBranch(TM);
174*9880d681SAndroid Build Coastguard Worker }
175*9880d681SAndroid Build Coastguard Worker 
176*9880d681SAndroid Build Coastguard Worker // Position describes the state immediately before Block.  Update Block
177*9880d681SAndroid Build Coastguard Worker // accordingly and move Position to the end of the block's non-terminator
178*9880d681SAndroid Build Coastguard Worker // instructions.
skipNonTerminators(BlockPosition & Position,MBBInfo & Block)179*9880d681SAndroid Build Coastguard Worker void SystemZLongBranch::skipNonTerminators(BlockPosition &Position,
180*9880d681SAndroid Build Coastguard Worker                                            MBBInfo &Block) {
181*9880d681SAndroid Build Coastguard Worker   if (Block.Alignment > Position.KnownBits) {
182*9880d681SAndroid Build Coastguard Worker     // When calculating the address of Block, we need to conservatively
183*9880d681SAndroid Build Coastguard Worker     // assume that Block had the worst possible misalignment.
184*9880d681SAndroid Build Coastguard Worker     Position.Address += ((uint64_t(1) << Block.Alignment) -
185*9880d681SAndroid Build Coastguard Worker                          (uint64_t(1) << Position.KnownBits));
186*9880d681SAndroid Build Coastguard Worker     Position.KnownBits = Block.Alignment;
187*9880d681SAndroid Build Coastguard Worker   }
188*9880d681SAndroid Build Coastguard Worker 
189*9880d681SAndroid Build Coastguard Worker   // Align the addresses.
190*9880d681SAndroid Build Coastguard Worker   uint64_t AlignMask = (uint64_t(1) << Block.Alignment) - 1;
191*9880d681SAndroid Build Coastguard Worker   Position.Address = (Position.Address + AlignMask) & ~AlignMask;
192*9880d681SAndroid Build Coastguard Worker 
193*9880d681SAndroid Build Coastguard Worker   // Record the block's position.
194*9880d681SAndroid Build Coastguard Worker   Block.Address = Position.Address;
195*9880d681SAndroid Build Coastguard Worker 
196*9880d681SAndroid Build Coastguard Worker   // Move past the non-terminators in the block.
197*9880d681SAndroid Build Coastguard Worker   Position.Address += Block.Size;
198*9880d681SAndroid Build Coastguard Worker }
199*9880d681SAndroid Build Coastguard Worker 
200*9880d681SAndroid Build Coastguard Worker // Position describes the state immediately before Terminator.
201*9880d681SAndroid Build Coastguard Worker // Update Terminator accordingly and move Position past it.
202*9880d681SAndroid Build Coastguard Worker // Assume that Terminator will be relaxed if AssumeRelaxed.
skipTerminator(BlockPosition & Position,TerminatorInfo & Terminator,bool AssumeRelaxed)203*9880d681SAndroid Build Coastguard Worker void SystemZLongBranch::skipTerminator(BlockPosition &Position,
204*9880d681SAndroid Build Coastguard Worker                                        TerminatorInfo &Terminator,
205*9880d681SAndroid Build Coastguard Worker                                        bool AssumeRelaxed) {
206*9880d681SAndroid Build Coastguard Worker   Terminator.Address = Position.Address;
207*9880d681SAndroid Build Coastguard Worker   Position.Address += Terminator.Size;
208*9880d681SAndroid Build Coastguard Worker   if (AssumeRelaxed)
209*9880d681SAndroid Build Coastguard Worker     Position.Address += Terminator.ExtraRelaxSize;
210*9880d681SAndroid Build Coastguard Worker }
211*9880d681SAndroid Build Coastguard Worker 
212*9880d681SAndroid Build Coastguard Worker // Return a description of terminator instruction MI.
describeTerminator(MachineInstr & MI)213*9880d681SAndroid Build Coastguard Worker TerminatorInfo SystemZLongBranch::describeTerminator(MachineInstr &MI) {
214*9880d681SAndroid Build Coastguard Worker   TerminatorInfo Terminator;
215*9880d681SAndroid Build Coastguard Worker   Terminator.Size = TII->getInstSizeInBytes(MI);
216*9880d681SAndroid Build Coastguard Worker   if (MI.isConditionalBranch() || MI.isUnconditionalBranch()) {
217*9880d681SAndroid Build Coastguard Worker     switch (MI.getOpcode()) {
218*9880d681SAndroid Build Coastguard Worker     case SystemZ::J:
219*9880d681SAndroid Build Coastguard Worker       // Relaxes to JG, which is 2 bytes longer.
220*9880d681SAndroid Build Coastguard Worker       Terminator.ExtraRelaxSize = 2;
221*9880d681SAndroid Build Coastguard Worker       break;
222*9880d681SAndroid Build Coastguard Worker     case SystemZ::BRC:
223*9880d681SAndroid Build Coastguard Worker       // Relaxes to BRCL, which is 2 bytes longer.
224*9880d681SAndroid Build Coastguard Worker       Terminator.ExtraRelaxSize = 2;
225*9880d681SAndroid Build Coastguard Worker       break;
226*9880d681SAndroid Build Coastguard Worker     case SystemZ::BRCT:
227*9880d681SAndroid Build Coastguard Worker     case SystemZ::BRCTG:
228*9880d681SAndroid Build Coastguard Worker       // Relaxes to A(G)HI and BRCL, which is 6 bytes longer.
229*9880d681SAndroid Build Coastguard Worker       Terminator.ExtraRelaxSize = 6;
230*9880d681SAndroid Build Coastguard Worker       break;
231*9880d681SAndroid Build Coastguard Worker     case SystemZ::CRJ:
232*9880d681SAndroid Build Coastguard Worker     case SystemZ::CLRJ:
233*9880d681SAndroid Build Coastguard Worker       // Relaxes to a C(L)R/BRCL sequence, which is 2 bytes longer.
234*9880d681SAndroid Build Coastguard Worker       Terminator.ExtraRelaxSize = 2;
235*9880d681SAndroid Build Coastguard Worker       break;
236*9880d681SAndroid Build Coastguard Worker     case SystemZ::CGRJ:
237*9880d681SAndroid Build Coastguard Worker     case SystemZ::CLGRJ:
238*9880d681SAndroid Build Coastguard Worker       // Relaxes to a C(L)GR/BRCL sequence, which is 4 bytes longer.
239*9880d681SAndroid Build Coastguard Worker       Terminator.ExtraRelaxSize = 4;
240*9880d681SAndroid Build Coastguard Worker       break;
241*9880d681SAndroid Build Coastguard Worker     case SystemZ::CIJ:
242*9880d681SAndroid Build Coastguard Worker     case SystemZ::CGIJ:
243*9880d681SAndroid Build Coastguard Worker       // Relaxes to a C(G)HI/BRCL sequence, which is 4 bytes longer.
244*9880d681SAndroid Build Coastguard Worker       Terminator.ExtraRelaxSize = 4;
245*9880d681SAndroid Build Coastguard Worker       break;
246*9880d681SAndroid Build Coastguard Worker     case SystemZ::CLIJ:
247*9880d681SAndroid Build Coastguard Worker     case SystemZ::CLGIJ:
248*9880d681SAndroid Build Coastguard Worker       // Relaxes to a CL(G)FI/BRCL sequence, which is 6 bytes longer.
249*9880d681SAndroid Build Coastguard Worker       Terminator.ExtraRelaxSize = 6;
250*9880d681SAndroid Build Coastguard Worker       break;
251*9880d681SAndroid Build Coastguard Worker     default:
252*9880d681SAndroid Build Coastguard Worker       llvm_unreachable("Unrecognized branch instruction");
253*9880d681SAndroid Build Coastguard Worker     }
254*9880d681SAndroid Build Coastguard Worker     Terminator.Branch = &MI;
255*9880d681SAndroid Build Coastguard Worker     Terminator.TargetBlock =
256*9880d681SAndroid Build Coastguard Worker       TII->getBranchInfo(MI).Target->getMBB()->getNumber();
257*9880d681SAndroid Build Coastguard Worker   }
258*9880d681SAndroid Build Coastguard Worker   return Terminator;
259*9880d681SAndroid Build Coastguard Worker }
260*9880d681SAndroid Build Coastguard Worker 
261*9880d681SAndroid Build Coastguard Worker // Fill MBBs and Terminators, setting the addresses on the assumption
262*9880d681SAndroid Build Coastguard Worker // that no branches need relaxation.  Return the size of the function under
263*9880d681SAndroid Build Coastguard Worker // this assumption.
initMBBInfo()264*9880d681SAndroid Build Coastguard Worker uint64_t SystemZLongBranch::initMBBInfo() {
265*9880d681SAndroid Build Coastguard Worker   MF->RenumberBlocks();
266*9880d681SAndroid Build Coastguard Worker   unsigned NumBlocks = MF->size();
267*9880d681SAndroid Build Coastguard Worker 
268*9880d681SAndroid Build Coastguard Worker   MBBs.clear();
269*9880d681SAndroid Build Coastguard Worker   MBBs.resize(NumBlocks);
270*9880d681SAndroid Build Coastguard Worker 
271*9880d681SAndroid Build Coastguard Worker   Terminators.clear();
272*9880d681SAndroid Build Coastguard Worker   Terminators.reserve(NumBlocks);
273*9880d681SAndroid Build Coastguard Worker 
274*9880d681SAndroid Build Coastguard Worker   BlockPosition Position(MF->getAlignment());
275*9880d681SAndroid Build Coastguard Worker   for (unsigned I = 0; I < NumBlocks; ++I) {
276*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock *MBB = MF->getBlockNumbered(I);
277*9880d681SAndroid Build Coastguard Worker     MBBInfo &Block = MBBs[I];
278*9880d681SAndroid Build Coastguard Worker 
279*9880d681SAndroid Build Coastguard Worker     // Record the alignment, for quick access.
280*9880d681SAndroid Build Coastguard Worker     Block.Alignment = MBB->getAlignment();
281*9880d681SAndroid Build Coastguard Worker 
282*9880d681SAndroid Build Coastguard Worker     // Calculate the size of the fixed part of the block.
283*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock::iterator MI = MBB->begin();
284*9880d681SAndroid Build Coastguard Worker     MachineBasicBlock::iterator End = MBB->end();
285*9880d681SAndroid Build Coastguard Worker     while (MI != End && !MI->isTerminator()) {
286*9880d681SAndroid Build Coastguard Worker       Block.Size += TII->getInstSizeInBytes(*MI);
287*9880d681SAndroid Build Coastguard Worker       ++MI;
288*9880d681SAndroid Build Coastguard Worker     }
289*9880d681SAndroid Build Coastguard Worker     skipNonTerminators(Position, Block);
290*9880d681SAndroid Build Coastguard Worker 
291*9880d681SAndroid Build Coastguard Worker     // Add the terminators.
292*9880d681SAndroid Build Coastguard Worker     while (MI != End) {
293*9880d681SAndroid Build Coastguard Worker       if (!MI->isDebugValue()) {
294*9880d681SAndroid Build Coastguard Worker         assert(MI->isTerminator() && "Terminator followed by non-terminator");
295*9880d681SAndroid Build Coastguard Worker         Terminators.push_back(describeTerminator(*MI));
296*9880d681SAndroid Build Coastguard Worker         skipTerminator(Position, Terminators.back(), false);
297*9880d681SAndroid Build Coastguard Worker         ++Block.NumTerminators;
298*9880d681SAndroid Build Coastguard Worker       }
299*9880d681SAndroid Build Coastguard Worker       ++MI;
300*9880d681SAndroid Build Coastguard Worker     }
301*9880d681SAndroid Build Coastguard Worker   }
302*9880d681SAndroid Build Coastguard Worker 
303*9880d681SAndroid Build Coastguard Worker   return Position.Address;
304*9880d681SAndroid Build Coastguard Worker }
305*9880d681SAndroid Build Coastguard Worker 
306*9880d681SAndroid Build Coastguard Worker // Return true if, under current assumptions, Terminator would need to be
307*9880d681SAndroid Build Coastguard Worker // relaxed if it were placed at address Address.
mustRelaxBranch(const TerminatorInfo & Terminator,uint64_t Address)308*9880d681SAndroid Build Coastguard Worker bool SystemZLongBranch::mustRelaxBranch(const TerminatorInfo &Terminator,
309*9880d681SAndroid Build Coastguard Worker                                         uint64_t Address) {
310*9880d681SAndroid Build Coastguard Worker   if (!Terminator.Branch)
311*9880d681SAndroid Build Coastguard Worker     return false;
312*9880d681SAndroid Build Coastguard Worker 
313*9880d681SAndroid Build Coastguard Worker   const MBBInfo &Target = MBBs[Terminator.TargetBlock];
314*9880d681SAndroid Build Coastguard Worker   if (Address >= Target.Address) {
315*9880d681SAndroid Build Coastguard Worker     if (Address - Target.Address <= MaxBackwardRange)
316*9880d681SAndroid Build Coastguard Worker       return false;
317*9880d681SAndroid Build Coastguard Worker   } else {
318*9880d681SAndroid Build Coastguard Worker     if (Target.Address - Address <= MaxForwardRange)
319*9880d681SAndroid Build Coastguard Worker       return false;
320*9880d681SAndroid Build Coastguard Worker   }
321*9880d681SAndroid Build Coastguard Worker 
322*9880d681SAndroid Build Coastguard Worker   return true;
323*9880d681SAndroid Build Coastguard Worker }
324*9880d681SAndroid Build Coastguard Worker 
325*9880d681SAndroid Build Coastguard Worker // Return true if, under current assumptions, any terminator needs
326*9880d681SAndroid Build Coastguard Worker // to be relaxed.
mustRelaxABranch()327*9880d681SAndroid Build Coastguard Worker bool SystemZLongBranch::mustRelaxABranch() {
328*9880d681SAndroid Build Coastguard Worker   for (auto &Terminator : Terminators)
329*9880d681SAndroid Build Coastguard Worker     if (mustRelaxBranch(Terminator, Terminator.Address))
330*9880d681SAndroid Build Coastguard Worker       return true;
331*9880d681SAndroid Build Coastguard Worker   return false;
332*9880d681SAndroid Build Coastguard Worker }
333*9880d681SAndroid Build Coastguard Worker 
334*9880d681SAndroid Build Coastguard Worker // Set the address of each block on the assumption that all branches
335*9880d681SAndroid Build Coastguard Worker // must be long.
setWorstCaseAddresses()336*9880d681SAndroid Build Coastguard Worker void SystemZLongBranch::setWorstCaseAddresses() {
337*9880d681SAndroid Build Coastguard Worker   SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin();
338*9880d681SAndroid Build Coastguard Worker   BlockPosition Position(MF->getAlignment());
339*9880d681SAndroid Build Coastguard Worker   for (auto &Block : MBBs) {
340*9880d681SAndroid Build Coastguard Worker     skipNonTerminators(Position, Block);
341*9880d681SAndroid Build Coastguard Worker     for (unsigned BTI = 0, BTE = Block.NumTerminators; BTI != BTE; ++BTI) {
342*9880d681SAndroid Build Coastguard Worker       skipTerminator(Position, *TI, true);
343*9880d681SAndroid Build Coastguard Worker       ++TI;
344*9880d681SAndroid Build Coastguard Worker     }
345*9880d681SAndroid Build Coastguard Worker   }
346*9880d681SAndroid Build Coastguard Worker }
347*9880d681SAndroid Build Coastguard Worker 
348*9880d681SAndroid Build Coastguard Worker // Split BRANCH ON COUNT MI into the addition given by AddOpcode followed
349*9880d681SAndroid Build Coastguard Worker // by a BRCL on the result.
splitBranchOnCount(MachineInstr * MI,unsigned AddOpcode)350*9880d681SAndroid Build Coastguard Worker void SystemZLongBranch::splitBranchOnCount(MachineInstr *MI,
351*9880d681SAndroid Build Coastguard Worker                                            unsigned AddOpcode) {
352*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *MBB = MI->getParent();
353*9880d681SAndroid Build Coastguard Worker   DebugLoc DL = MI->getDebugLoc();
354*9880d681SAndroid Build Coastguard Worker   BuildMI(*MBB, MI, DL, TII->get(AddOpcode))
355*9880d681SAndroid Build Coastguard Worker     .addOperand(MI->getOperand(0))
356*9880d681SAndroid Build Coastguard Worker     .addOperand(MI->getOperand(1))
357*9880d681SAndroid Build Coastguard Worker     .addImm(-1);
358*9880d681SAndroid Build Coastguard Worker   MachineInstr *BRCL = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BRCL))
359*9880d681SAndroid Build Coastguard Worker     .addImm(SystemZ::CCMASK_ICMP)
360*9880d681SAndroid Build Coastguard Worker     .addImm(SystemZ::CCMASK_CMP_NE)
361*9880d681SAndroid Build Coastguard Worker     .addOperand(MI->getOperand(2));
362*9880d681SAndroid Build Coastguard Worker   // The implicit use of CC is a killing use.
363*9880d681SAndroid Build Coastguard Worker   BRCL->addRegisterKilled(SystemZ::CC, &TII->getRegisterInfo());
364*9880d681SAndroid Build Coastguard Worker   MI->eraseFromParent();
365*9880d681SAndroid Build Coastguard Worker }
366*9880d681SAndroid Build Coastguard Worker 
367*9880d681SAndroid Build Coastguard Worker // Split MI into the comparison given by CompareOpcode followed
368*9880d681SAndroid Build Coastguard Worker // a BRCL on the result.
splitCompareBranch(MachineInstr * MI,unsigned CompareOpcode)369*9880d681SAndroid Build Coastguard Worker void SystemZLongBranch::splitCompareBranch(MachineInstr *MI,
370*9880d681SAndroid Build Coastguard Worker                                            unsigned CompareOpcode) {
371*9880d681SAndroid Build Coastguard Worker   MachineBasicBlock *MBB = MI->getParent();
372*9880d681SAndroid Build Coastguard Worker   DebugLoc DL = MI->getDebugLoc();
373*9880d681SAndroid Build Coastguard Worker   BuildMI(*MBB, MI, DL, TII->get(CompareOpcode))
374*9880d681SAndroid Build Coastguard Worker     .addOperand(MI->getOperand(0))
375*9880d681SAndroid Build Coastguard Worker     .addOperand(MI->getOperand(1));
376*9880d681SAndroid Build Coastguard Worker   MachineInstr *BRCL = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BRCL))
377*9880d681SAndroid Build Coastguard Worker     .addImm(SystemZ::CCMASK_ICMP)
378*9880d681SAndroid Build Coastguard Worker     .addOperand(MI->getOperand(2))
379*9880d681SAndroid Build Coastguard Worker     .addOperand(MI->getOperand(3));
380*9880d681SAndroid Build Coastguard Worker   // The implicit use of CC is a killing use.
381*9880d681SAndroid Build Coastguard Worker   BRCL->addRegisterKilled(SystemZ::CC, &TII->getRegisterInfo());
382*9880d681SAndroid Build Coastguard Worker   MI->eraseFromParent();
383*9880d681SAndroid Build Coastguard Worker }
384*9880d681SAndroid Build Coastguard Worker 
385*9880d681SAndroid Build Coastguard Worker // Relax the branch described by Terminator.
relaxBranch(TerminatorInfo & Terminator)386*9880d681SAndroid Build Coastguard Worker void SystemZLongBranch::relaxBranch(TerminatorInfo &Terminator) {
387*9880d681SAndroid Build Coastguard Worker   MachineInstr *Branch = Terminator.Branch;
388*9880d681SAndroid Build Coastguard Worker   switch (Branch->getOpcode()) {
389*9880d681SAndroid Build Coastguard Worker   case SystemZ::J:
390*9880d681SAndroid Build Coastguard Worker     Branch->setDesc(TII->get(SystemZ::JG));
391*9880d681SAndroid Build Coastguard Worker     break;
392*9880d681SAndroid Build Coastguard Worker   case SystemZ::BRC:
393*9880d681SAndroid Build Coastguard Worker     Branch->setDesc(TII->get(SystemZ::BRCL));
394*9880d681SAndroid Build Coastguard Worker     break;
395*9880d681SAndroid Build Coastguard Worker   case SystemZ::BRCT:
396*9880d681SAndroid Build Coastguard Worker     splitBranchOnCount(Branch, SystemZ::AHI);
397*9880d681SAndroid Build Coastguard Worker     break;
398*9880d681SAndroid Build Coastguard Worker   case SystemZ::BRCTG:
399*9880d681SAndroid Build Coastguard Worker     splitBranchOnCount(Branch, SystemZ::AGHI);
400*9880d681SAndroid Build Coastguard Worker     break;
401*9880d681SAndroid Build Coastguard Worker   case SystemZ::CRJ:
402*9880d681SAndroid Build Coastguard Worker     splitCompareBranch(Branch, SystemZ::CR);
403*9880d681SAndroid Build Coastguard Worker     break;
404*9880d681SAndroid Build Coastguard Worker   case SystemZ::CGRJ:
405*9880d681SAndroid Build Coastguard Worker     splitCompareBranch(Branch, SystemZ::CGR);
406*9880d681SAndroid Build Coastguard Worker     break;
407*9880d681SAndroid Build Coastguard Worker   case SystemZ::CIJ:
408*9880d681SAndroid Build Coastguard Worker     splitCompareBranch(Branch, SystemZ::CHI);
409*9880d681SAndroid Build Coastguard Worker     break;
410*9880d681SAndroid Build Coastguard Worker   case SystemZ::CGIJ:
411*9880d681SAndroid Build Coastguard Worker     splitCompareBranch(Branch, SystemZ::CGHI);
412*9880d681SAndroid Build Coastguard Worker     break;
413*9880d681SAndroid Build Coastguard Worker   case SystemZ::CLRJ:
414*9880d681SAndroid Build Coastguard Worker     splitCompareBranch(Branch, SystemZ::CLR);
415*9880d681SAndroid Build Coastguard Worker     break;
416*9880d681SAndroid Build Coastguard Worker   case SystemZ::CLGRJ:
417*9880d681SAndroid Build Coastguard Worker     splitCompareBranch(Branch, SystemZ::CLGR);
418*9880d681SAndroid Build Coastguard Worker     break;
419*9880d681SAndroid Build Coastguard Worker   case SystemZ::CLIJ:
420*9880d681SAndroid Build Coastguard Worker     splitCompareBranch(Branch, SystemZ::CLFI);
421*9880d681SAndroid Build Coastguard Worker     break;
422*9880d681SAndroid Build Coastguard Worker   case SystemZ::CLGIJ:
423*9880d681SAndroid Build Coastguard Worker     splitCompareBranch(Branch, SystemZ::CLGFI);
424*9880d681SAndroid Build Coastguard Worker     break;
425*9880d681SAndroid Build Coastguard Worker   default:
426*9880d681SAndroid Build Coastguard Worker     llvm_unreachable("Unrecognized branch");
427*9880d681SAndroid Build Coastguard Worker   }
428*9880d681SAndroid Build Coastguard Worker 
429*9880d681SAndroid Build Coastguard Worker   Terminator.Size += Terminator.ExtraRelaxSize;
430*9880d681SAndroid Build Coastguard Worker   Terminator.ExtraRelaxSize = 0;
431*9880d681SAndroid Build Coastguard Worker   Terminator.Branch = nullptr;
432*9880d681SAndroid Build Coastguard Worker 
433*9880d681SAndroid Build Coastguard Worker   ++LongBranches;
434*9880d681SAndroid Build Coastguard Worker }
435*9880d681SAndroid Build Coastguard Worker 
436*9880d681SAndroid Build Coastguard Worker // Run a shortening pass and relax any branches that need to be relaxed.
relaxBranches()437*9880d681SAndroid Build Coastguard Worker void SystemZLongBranch::relaxBranches() {
438*9880d681SAndroid Build Coastguard Worker   SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin();
439*9880d681SAndroid Build Coastguard Worker   BlockPosition Position(MF->getAlignment());
440*9880d681SAndroid Build Coastguard Worker   for (auto &Block : MBBs) {
441*9880d681SAndroid Build Coastguard Worker     skipNonTerminators(Position, Block);
442*9880d681SAndroid Build Coastguard Worker     for (unsigned BTI = 0, BTE = Block.NumTerminators; BTI != BTE; ++BTI) {
443*9880d681SAndroid Build Coastguard Worker       assert(Position.Address <= TI->Address &&
444*9880d681SAndroid Build Coastguard Worker              "Addresses shouldn't go forwards");
445*9880d681SAndroid Build Coastguard Worker       if (mustRelaxBranch(*TI, Position.Address))
446*9880d681SAndroid Build Coastguard Worker         relaxBranch(*TI);
447*9880d681SAndroid Build Coastguard Worker       skipTerminator(Position, *TI, false);
448*9880d681SAndroid Build Coastguard Worker       ++TI;
449*9880d681SAndroid Build Coastguard Worker     }
450*9880d681SAndroid Build Coastguard Worker   }
451*9880d681SAndroid Build Coastguard Worker }
452*9880d681SAndroid Build Coastguard Worker 
runOnMachineFunction(MachineFunction & F)453*9880d681SAndroid Build Coastguard Worker bool SystemZLongBranch::runOnMachineFunction(MachineFunction &F) {
454*9880d681SAndroid Build Coastguard Worker   TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
455*9880d681SAndroid Build Coastguard Worker   MF = &F;
456*9880d681SAndroid Build Coastguard Worker   uint64_t Size = initMBBInfo();
457*9880d681SAndroid Build Coastguard Worker   if (Size <= MaxForwardRange || !mustRelaxABranch())
458*9880d681SAndroid Build Coastguard Worker     return false;
459*9880d681SAndroid Build Coastguard Worker 
460*9880d681SAndroid Build Coastguard Worker   setWorstCaseAddresses();
461*9880d681SAndroid Build Coastguard Worker   relaxBranches();
462*9880d681SAndroid Build Coastguard Worker   return true;
463*9880d681SAndroid Build Coastguard Worker }
464