74 #define DEBUG_TYPE "systemz-long-branch" 76 STATISTIC(LongBranches,
"Number of long branches.");
91 unsigned Alignment = 0;
94 unsigned NumTerminators = 0;
100 struct TerminatorInfo {
113 unsigned TargetBlock = 0;
117 unsigned ExtraRelaxSize = 0;
119 TerminatorInfo() =
default;
123 struct BlockPosition {
131 BlockPosition(
unsigned InitialAlignment) : KnownBits(InitialAlignment) {}
141 StringRef getPassName()
const override {
return "SystemZ Long Branch"; }
151 void skipNonTerminators(BlockPosition &
Position, MBBInfo &Block);
155 uint64_t initMBBInfo();
157 bool mustRelaxABranch();
158 void setWorstCaseAddresses();
160 void splitCompareBranch(
MachineInstr *
MI,
unsigned CompareOpcode);
162 void relaxBranches();
172 const uint64_t MaxBackwardRange = 0x10000;
173 const uint64_t MaxForwardRange = 0xfffe;
180 void SystemZLongBranch::skipNonTerminators(BlockPosition &
Position,
182 if (Block.Alignment > Position.KnownBits) {
185 Position.Address += ((uint64_t(1) << Block.Alignment) -
186 (uint64_t(1) << Position.KnownBits));
187 Position.KnownBits = Block.Alignment;
191 uint64_t AlignMask = (uint64_t(1) << Block.Alignment) - 1;
192 Position.Address = (Position.Address + AlignMask) & ~AlignMask;
195 Block.Address = Position.Address;
198 Position.Address += Block.Size;
204 void SystemZLongBranch::skipTerminator(BlockPosition &Position,
206 bool AssumeRelaxed) {
207 Terminator.Address = Position.Address;
208 Position.Address += Terminator.Size;
210 Position.Address += Terminator.ExtraRelaxSize;
214 TerminatorInfo SystemZLongBranch::describeTerminator(
MachineInstr &
MI) {
216 Terminator.Size =
TII->getInstSizeInBytes(MI);
221 Terminator.ExtraRelaxSize = 2;
225 Terminator.ExtraRelaxSize = 2;
230 Terminator.ExtraRelaxSize = 6;
234 Terminator.ExtraRelaxSize = 0;
239 Terminator.ExtraRelaxSize = 2;
244 Terminator.ExtraRelaxSize = 4;
249 Terminator.ExtraRelaxSize = 4;
254 Terminator.ExtraRelaxSize = 6;
259 Terminator.Branch = &
MI;
260 Terminator.TargetBlock =
261 TII->getBranchInfo(MI).Target->getMBB()->getNumber();
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) {
282 MBBInfo &Block = MBBs[
I];
290 while (MI != End && !MI->isTerminator()) {
291 Block.Size +=
TII->getInstSizeInBytes(*MI);
294 skipNonTerminators(Position, Block);
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;
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)
332 bool SystemZLongBranch::mustRelaxABranch() {
333 for (
auto &Terminator : Terminators)
334 if (mustRelaxBranch(Terminator, Terminator.Address))
341 void SystemZLongBranch::setWorstCaseAddresses() {
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);
355 void SystemZLongBranch::splitBranchOnCount(
MachineInstr *MI,
356 unsigned AddOpcode) {
374 void SystemZLongBranch::splitCompareBranch(
MachineInstr *MI,
375 unsigned CompareOpcode) {
378 BuildMI(*MBB, MI, DL,
TII->get(CompareOpcode))
386 BRCL->addRegisterKilled(SystemZ::CC, &
TII->getRegisterInfo());
391 void SystemZLongBranch::relaxBranch(TerminatorInfo &Terminator) {
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);
434 Terminator.Size += Terminator.ExtraRelaxSize;
435 Terminator.ExtraRelaxSize = 0;
436 Terminator.Branch =
nullptr;
442 void SystemZLongBranch::relaxBranches() {
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);
461 uint64_t
Size = initMBBInfo();
462 if (Size <= MaxForwardRange || !mustRelaxABranch())
465 setWorstCaseAddresses();
471 return new SystemZLongBranch(TM);
const MachineInstrBuilder & add(const MachineOperand &MO) const
This class represents lattice values for constants.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
STATISTIC(NumFunctions, "Total number of functions")
const unsigned CCMASK_ICMP
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Position
Position to insert a new instruction relative to an existing instruction.
virtual const TargetInstrInfo * getInstrInfo() const
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
unsigned getAlignment() const
Return alignment of the basic block.
FunctionPass class - This class is used to implement most global optimizations.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
bool isConditionalBranch(QueryType Type=AnyInBundle) const
Return true if this is a branch which may fall through to the next instruction or may transfer contro...
void setDesc(const MCInstrDesc &tid)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
const unsigned CCMASK_CMP_NE
FunctionPass * createSystemZLongBranchPass(SystemZTargetMachine &TM)
Target - Wrapper for Target specific information.
bool isUnconditionalBranch(QueryType Type=AnyInBundle) const
Return true if this is a branch which always transfers control flow to some other block...
const MachineBasicBlock * getParent() const
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool addRegisterKilled(unsigned IncomingReg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI kills a register.
StringRef - Represent a constant reference to a string, i.e.
const MachineOperand & getOperand(unsigned i) const
Properties which a MachineFunction may have at a given point in time.