LLVM API Documentation

SystemZLongBranch.cpp
Go to the documentation of this file.
00001 //===-- SystemZLongBranch.cpp - Branch lengthening for SystemZ ------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This pass makes sure that all branches are in range.  There are several ways
00011 // in which this could be done.  One aggressive approach is to assume that all
00012 // branches are in range and successively replace those that turn out not
00013 // to be in range with a longer form (branch relaxation).  A simple
00014 // implementation is to continually walk through the function relaxing
00015 // branches until no more changes are needed and a fixed point is reached.
00016 // However, in the pathological worst case, this implementation is
00017 // quadratic in the number of blocks; relaxing branch N can make branch N-1
00018 // go out of range, which in turn can make branch N-2 go out of range,
00019 // and so on.
00020 //
00021 // An alternative approach is to assume that all branches must be
00022 // converted to their long forms, then reinstate the short forms of
00023 // branches that, even under this pessimistic assumption, turn out to be
00024 // in range (branch shortening).  This too can be implemented as a function
00025 // walk that is repeated until a fixed point is reached.  In general,
00026 // the result of shortening is not as good as that of relaxation, and
00027 // shortening is also quadratic in the worst case; shortening branch N
00028 // can bring branch N-1 in range of the short form, which in turn can do
00029 // the same for branch N-2, and so on.  The main advantage of shortening
00030 // is that each walk through the function produces valid code, so it is
00031 // possible to stop at any point after the first walk.  The quadraticness
00032 // could therefore be handled with a maximum pass count, although the
00033 // question then becomes: what maximum count should be used?
00034 //
00035 // On SystemZ, long branches are only needed for functions bigger than 64k,
00036 // which are relatively rare to begin with, and the long branch sequences
00037 // are actually relatively cheap.  It therefore doesn't seem worth spending
00038 // much compilation time on the problem.  Instead, the approach we take is:
00039 //
00040 // (1) Work out the address that each block would have if no branches
00041 //     need relaxing.  Exit the pass early if all branches are in range
00042 //     according to this assumption.
00043 //
00044 // (2) Work out the address that each block would have if all branches
00045 //     need relaxing.
00046 //
00047 // (3) Walk through the block calculating the final address of each instruction
00048 //     and relaxing those that need to be relaxed.  For backward branches,
00049 //     this check uses the final address of the target block, as calculated
00050 //     earlier in the walk.  For forward branches, this check uses the
00051 //     address of the target block that was calculated in (2).  Both checks
00052 //     give a conservatively-correct range.
00053 //
00054 //===----------------------------------------------------------------------===//
00055 
00056 #include "SystemZTargetMachine.h"
00057 #include "llvm/ADT/Statistic.h"
00058 #include "llvm/CodeGen/MachineFunctionPass.h"
00059 #include "llvm/CodeGen/MachineInstrBuilder.h"
00060 #include "llvm/IR/Function.h"
00061 #include "llvm/Support/CommandLine.h"
00062 #include "llvm/Support/MathExtras.h"
00063 #include "llvm/Target/TargetInstrInfo.h"
00064 #include "llvm/Target/TargetMachine.h"
00065 #include "llvm/Target/TargetRegisterInfo.h"
00066 
00067 using namespace llvm;
00068 
00069 #define DEBUG_TYPE "systemz-long-branch"
00070 
00071 STATISTIC(LongBranches, "Number of long branches.");
00072 
00073 namespace {
00074 // Represents positional information about a basic block.
00075 struct MBBInfo {
00076   // The address that we currently assume the block has.
00077   uint64_t Address;
00078 
00079   // The size of the block in bytes, excluding terminators.
00080   // This value never changes.
00081   uint64_t Size;
00082 
00083   // The minimum alignment of the block, as a log2 value.
00084   // This value never changes.
00085   unsigned Alignment;
00086 
00087   // The number of terminators in this block.  This value never changes.
00088   unsigned NumTerminators;
00089 
00090   MBBInfo()
00091     : Address(0), Size(0), Alignment(0), NumTerminators(0) {} 
00092 };
00093 
00094 // Represents the state of a block terminator.
00095 struct TerminatorInfo {
00096   // If this terminator is a relaxable branch, this points to the branch
00097   // instruction, otherwise it is null.
00098   MachineInstr *Branch;
00099 
00100   // The address that we currently assume the terminator has.
00101   uint64_t Address;
00102 
00103   // The current size of the terminator in bytes.
00104   uint64_t Size;
00105 
00106   // If Branch is nonnull, this is the number of the target block,
00107   // otherwise it is unused.
00108   unsigned TargetBlock;
00109 
00110   // If Branch is nonnull, this is the length of the longest relaxed form,
00111   // otherwise it is zero.
00112   unsigned ExtraRelaxSize;
00113 
00114   TerminatorInfo() : Branch(nullptr), Size(0), TargetBlock(0),
00115                      ExtraRelaxSize(0) {}
00116 };
00117 
00118 // Used to keep track of the current position while iterating over the blocks.
00119 struct BlockPosition {
00120   // The address that we assume this position has.
00121   uint64_t Address;
00122 
00123   // The number of low bits in Address that are known to be the same
00124   // as the runtime address.
00125   unsigned KnownBits;
00126 
00127   BlockPosition(unsigned InitialAlignment)
00128     : Address(0), KnownBits(InitialAlignment) {}
00129 };
00130 
00131 class SystemZLongBranch : public MachineFunctionPass {
00132 public:
00133   static char ID;
00134   SystemZLongBranch(const SystemZTargetMachine &tm)
00135     : MachineFunctionPass(ID), TII(nullptr) {}
00136 
00137   const char *getPassName() const override {
00138     return "SystemZ Long Branch";
00139   }
00140 
00141   bool runOnMachineFunction(MachineFunction &F) override;
00142 
00143 private:
00144   void skipNonTerminators(BlockPosition &Position, MBBInfo &Block);
00145   void skipTerminator(BlockPosition &Position, TerminatorInfo &Terminator,
00146                       bool AssumeRelaxed);
00147   TerminatorInfo describeTerminator(MachineInstr *MI);
00148   uint64_t initMBBInfo();
00149   bool mustRelaxBranch(const TerminatorInfo &Terminator, uint64_t Address);
00150   bool mustRelaxABranch();
00151   void setWorstCaseAddresses();
00152   void splitBranchOnCount(MachineInstr *MI, unsigned AddOpcode);
00153   void splitCompareBranch(MachineInstr *MI, unsigned CompareOpcode);
00154   void relaxBranch(TerminatorInfo &Terminator);
00155   void relaxBranches();
00156 
00157   const SystemZInstrInfo *TII;
00158   MachineFunction *MF;
00159   SmallVector<MBBInfo, 16> MBBs;
00160   SmallVector<TerminatorInfo, 16> Terminators;
00161 };
00162 
00163 char SystemZLongBranch::ID = 0;
00164 
00165 const uint64_t MaxBackwardRange = 0x10000;
00166 const uint64_t MaxForwardRange = 0xfffe;
00167 } // end anonymous namespace
00168 
00169 FunctionPass *llvm::createSystemZLongBranchPass(SystemZTargetMachine &TM) {
00170   return new SystemZLongBranch(TM);
00171 }
00172 
00173 // Position describes the state immediately before Block.  Update Block
00174 // accordingly and move Position to the end of the block's non-terminator
00175 // instructions.
00176 void SystemZLongBranch::skipNonTerminators(BlockPosition &Position,
00177                                            MBBInfo &Block) {
00178   if (Block.Alignment > Position.KnownBits) {
00179     // When calculating the address of Block, we need to conservatively
00180     // assume that Block had the worst possible misalignment.
00181     Position.Address += ((uint64_t(1) << Block.Alignment) -
00182                          (uint64_t(1) << Position.KnownBits));
00183     Position.KnownBits = Block.Alignment;
00184   }
00185 
00186   // Align the addresses.
00187   uint64_t AlignMask = (uint64_t(1) << Block.Alignment) - 1;
00188   Position.Address = (Position.Address + AlignMask) & ~AlignMask;
00189 
00190   // Record the block's position.
00191   Block.Address = Position.Address;
00192 
00193   // Move past the non-terminators in the block.
00194   Position.Address += Block.Size;
00195 }
00196 
00197 // Position describes the state immediately before Terminator.
00198 // Update Terminator accordingly and move Position past it.
00199 // Assume that Terminator will be relaxed if AssumeRelaxed.
00200 void SystemZLongBranch::skipTerminator(BlockPosition &Position,
00201                                        TerminatorInfo &Terminator,
00202                                        bool AssumeRelaxed) {
00203   Terminator.Address = Position.Address;
00204   Position.Address += Terminator.Size;
00205   if (AssumeRelaxed)
00206     Position.Address += Terminator.ExtraRelaxSize;
00207 }
00208 
00209 // Return a description of terminator instruction MI.
00210 TerminatorInfo SystemZLongBranch::describeTerminator(MachineInstr *MI) {
00211   TerminatorInfo Terminator;
00212   Terminator.Size = TII->getInstSizeInBytes(MI);
00213   if (MI->isConditionalBranch() || MI->isUnconditionalBranch()) {
00214     switch (MI->getOpcode()) {
00215     case SystemZ::J:
00216       // Relaxes to JG, which is 2 bytes longer.
00217       Terminator.ExtraRelaxSize = 2;
00218       break;
00219     case SystemZ::BRC:
00220       // Relaxes to BRCL, which is 2 bytes longer.
00221       Terminator.ExtraRelaxSize = 2;
00222       break;
00223     case SystemZ::BRCT:
00224     case SystemZ::BRCTG:
00225       // Relaxes to A(G)HI and BRCL, which is 6 bytes longer.
00226       Terminator.ExtraRelaxSize = 6;
00227       break;
00228     case SystemZ::CRJ:
00229     case SystemZ::CLRJ:
00230       // Relaxes to a C(L)R/BRCL sequence, which is 2 bytes longer.
00231       Terminator.ExtraRelaxSize = 2;
00232       break;
00233     case SystemZ::CGRJ:
00234     case SystemZ::CLGRJ:
00235       // Relaxes to a C(L)GR/BRCL sequence, which is 4 bytes longer.
00236       Terminator.ExtraRelaxSize = 4;
00237       break;
00238     case SystemZ::CIJ:
00239     case SystemZ::CGIJ:
00240       // Relaxes to a C(G)HI/BRCL sequence, which is 4 bytes longer.
00241       Terminator.ExtraRelaxSize = 4;
00242       break;
00243     case SystemZ::CLIJ:
00244     case SystemZ::CLGIJ:
00245       // Relaxes to a CL(G)FI/BRCL sequence, which is 6 bytes longer.
00246       Terminator.ExtraRelaxSize = 6;
00247       break;
00248     default:
00249       llvm_unreachable("Unrecognized branch instruction");
00250     }
00251     Terminator.Branch = MI;
00252     Terminator.TargetBlock =
00253       TII->getBranchInfo(MI).Target->getMBB()->getNumber();
00254   }
00255   return Terminator;
00256 }
00257 
00258 // Fill MBBs and Terminators, setting the addresses on the assumption
00259 // that no branches need relaxation.  Return the size of the function under
00260 // this assumption.
00261 uint64_t SystemZLongBranch::initMBBInfo() {
00262   MF->RenumberBlocks();
00263   unsigned NumBlocks = MF->size();
00264 
00265   MBBs.clear();
00266   MBBs.resize(NumBlocks);
00267 
00268   Terminators.clear();
00269   Terminators.reserve(NumBlocks);
00270 
00271   BlockPosition Position(MF->getAlignment());
00272   for (unsigned I = 0; I < NumBlocks; ++I) {
00273     MachineBasicBlock *MBB = MF->getBlockNumbered(I);
00274     MBBInfo &Block = MBBs[I];
00275 
00276     // Record the alignment, for quick access.
00277     Block.Alignment = MBB->getAlignment();
00278 
00279     // Calculate the size of the fixed part of the block.
00280     MachineBasicBlock::iterator MI = MBB->begin();
00281     MachineBasicBlock::iterator End = MBB->end();
00282     while (MI != End && !MI->isTerminator()) {
00283       Block.Size += TII->getInstSizeInBytes(MI);
00284       ++MI;
00285     }
00286     skipNonTerminators(Position, Block);
00287 
00288     // Add the terminators.
00289     while (MI != End) {
00290       if (!MI->isDebugValue()) {
00291         assert(MI->isTerminator() && "Terminator followed by non-terminator");
00292         Terminators.push_back(describeTerminator(MI));
00293         skipTerminator(Position, Terminators.back(), false);
00294         ++Block.NumTerminators;
00295       }
00296       ++MI;
00297     }
00298   }
00299 
00300   return Position.Address;
00301 }
00302 
00303 // Return true if, under current assumptions, Terminator would need to be
00304 // relaxed if it were placed at address Address.
00305 bool SystemZLongBranch::mustRelaxBranch(const TerminatorInfo &Terminator,
00306                                         uint64_t Address) {
00307   if (!Terminator.Branch)
00308     return false;
00309 
00310   const MBBInfo &Target = MBBs[Terminator.TargetBlock];
00311   if (Address >= Target.Address) {
00312     if (Address - Target.Address <= MaxBackwardRange)
00313       return false;
00314   } else {
00315     if (Target.Address - Address <= MaxForwardRange)
00316       return false;
00317   }
00318 
00319   return true;
00320 }
00321 
00322 // Return true if, under current assumptions, any terminator needs
00323 // to be relaxed.
00324 bool SystemZLongBranch::mustRelaxABranch() {
00325   for (auto &Terminator : Terminators)
00326     if (mustRelaxBranch(Terminator, Terminator.Address))
00327       return true;
00328   return false;
00329 }
00330 
00331 // Set the address of each block on the assumption that all branches
00332 // must be long.
00333 void SystemZLongBranch::setWorstCaseAddresses() {
00334   SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin();
00335   BlockPosition Position(MF->getAlignment());
00336   for (auto &Block : MBBs) {
00337     skipNonTerminators(Position, Block);
00338     for (unsigned BTI = 0, BTE = Block.NumTerminators; BTI != BTE; ++BTI) {
00339       skipTerminator(Position, *TI, true);
00340       ++TI;
00341     }
00342   }
00343 }
00344 
00345 // Split BRANCH ON COUNT MI into the addition given by AddOpcode followed
00346 // by a BRCL on the result.
00347 void SystemZLongBranch::splitBranchOnCount(MachineInstr *MI,
00348                                            unsigned AddOpcode) {
00349   MachineBasicBlock *MBB = MI->getParent();
00350   DebugLoc DL = MI->getDebugLoc();
00351   BuildMI(*MBB, MI, DL, TII->get(AddOpcode))
00352     .addOperand(MI->getOperand(0))
00353     .addOperand(MI->getOperand(1))
00354     .addImm(-1);
00355   MachineInstr *BRCL = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BRCL))
00356     .addImm(SystemZ::CCMASK_ICMP)
00357     .addImm(SystemZ::CCMASK_CMP_NE)
00358     .addOperand(MI->getOperand(2));
00359   // The implicit use of CC is a killing use.
00360   BRCL->addRegisterKilled(SystemZ::CC, &TII->getRegisterInfo());
00361   MI->eraseFromParent();
00362 }
00363 
00364 // Split MI into the comparison given by CompareOpcode followed
00365 // a BRCL on the result.
00366 void SystemZLongBranch::splitCompareBranch(MachineInstr *MI,
00367                                            unsigned CompareOpcode) {
00368   MachineBasicBlock *MBB = MI->getParent();
00369   DebugLoc DL = MI->getDebugLoc();
00370   BuildMI(*MBB, MI, DL, TII->get(CompareOpcode))
00371     .addOperand(MI->getOperand(0))
00372     .addOperand(MI->getOperand(1));
00373   MachineInstr *BRCL = BuildMI(*MBB, MI, DL, TII->get(SystemZ::BRCL))
00374     .addImm(SystemZ::CCMASK_ICMP)
00375     .addOperand(MI->getOperand(2))
00376     .addOperand(MI->getOperand(3));
00377   // The implicit use of CC is a killing use.
00378   BRCL->addRegisterKilled(SystemZ::CC, &TII->getRegisterInfo());
00379   MI->eraseFromParent();
00380 }
00381 
00382 // Relax the branch described by Terminator.
00383 void SystemZLongBranch::relaxBranch(TerminatorInfo &Terminator) {
00384   MachineInstr *Branch = Terminator.Branch;
00385   switch (Branch->getOpcode()) {
00386   case SystemZ::J:
00387     Branch->setDesc(TII->get(SystemZ::JG));
00388     break;
00389   case SystemZ::BRC:
00390     Branch->setDesc(TII->get(SystemZ::BRCL));
00391     break;
00392   case SystemZ::BRCT:
00393     splitBranchOnCount(Branch, SystemZ::AHI);
00394     break;
00395   case SystemZ::BRCTG:
00396     splitBranchOnCount(Branch, SystemZ::AGHI);
00397     break;
00398   case SystemZ::CRJ:
00399     splitCompareBranch(Branch, SystemZ::CR);
00400     break;
00401   case SystemZ::CGRJ:
00402     splitCompareBranch(Branch, SystemZ::CGR);
00403     break;
00404   case SystemZ::CIJ:
00405     splitCompareBranch(Branch, SystemZ::CHI);
00406     break;
00407   case SystemZ::CGIJ:
00408     splitCompareBranch(Branch, SystemZ::CGHI);
00409     break;
00410   case SystemZ::CLRJ:
00411     splitCompareBranch(Branch, SystemZ::CLR);
00412     break;
00413   case SystemZ::CLGRJ:
00414     splitCompareBranch(Branch, SystemZ::CLGR);
00415     break;
00416   case SystemZ::CLIJ:
00417     splitCompareBranch(Branch, SystemZ::CLFI);
00418     break;
00419   case SystemZ::CLGIJ:
00420     splitCompareBranch(Branch, SystemZ::CLGFI);
00421     break;
00422   default:
00423     llvm_unreachable("Unrecognized branch");
00424   }
00425 
00426   Terminator.Size += Terminator.ExtraRelaxSize;
00427   Terminator.ExtraRelaxSize = 0;
00428   Terminator.Branch = nullptr;
00429 
00430   ++LongBranches;
00431 }
00432 
00433 // Run a shortening pass and relax any branches that need to be relaxed.
00434 void SystemZLongBranch::relaxBranches() {
00435   SmallVector<TerminatorInfo, 16>::iterator TI = Terminators.begin();
00436   BlockPosition Position(MF->getAlignment());
00437   for (auto &Block : MBBs) {
00438     skipNonTerminators(Position, Block);
00439     for (unsigned BTI = 0, BTE = Block.NumTerminators; BTI != BTE; ++BTI) {
00440       assert(Position.Address <= TI->Address &&
00441              "Addresses shouldn't go forwards");
00442       if (mustRelaxBranch(*TI, Position.Address))
00443         relaxBranch(*TI);
00444       skipTerminator(Position, *TI, false);
00445       ++TI;
00446     }
00447   }
00448 }
00449 
00450 bool SystemZLongBranch::runOnMachineFunction(MachineFunction &F) {
00451   TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
00452   MF = &F;
00453   uint64_t Size = initMBBInfo();
00454   if (Size <= MaxForwardRange || !mustRelaxABranch())
00455     return false;
00456 
00457   setWorstCaseAddresses();
00458   relaxBranches();
00459   return true;
00460 }