73 #define DEBUG_TYPE "systemz-long-branch" 75 STATISTIC(LongBranches,
"Number of long branches.");
93 unsigned NumTerminators = 0;
99 struct TerminatorInfo {
112 unsigned TargetBlock = 0;
116 unsigned ExtraRelaxSize = 0;
118 TerminatorInfo() =
default;
122 struct BlockPosition {
130 BlockPosition(
unsigned InitialLogAlignment)
131 : KnownBits(InitialLogAlignment) {}
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 (
Log2(Block.Alignment) > Position.KnownBits) {
186 (Block.Alignment.value() - (uint64_t(1) << Position.KnownBits));
187 Position.KnownBits =
Log2(Block.Alignment);
191 Position.Address =
alignTo(Position.Address, Block.Alignment);
194 Block.Address = Position.Address;
197 Position.Address += Block.Size;
203 void SystemZLongBranch::skipTerminator(BlockPosition &Position,
205 bool AssumeRelaxed) {
206 Terminator.Address = Position.Address;
207 Position.Address += Terminator.Size;
209 Position.Address += Terminator.ExtraRelaxSize;
213 TerminatorInfo SystemZLongBranch::describeTerminator(
MachineInstr &
MI) {
215 Terminator.Size =
TII->getInstSizeInBytes(MI);
220 Terminator.ExtraRelaxSize = 2;
224 Terminator.ExtraRelaxSize = 2;
229 Terminator.ExtraRelaxSize = 6;
233 Terminator.ExtraRelaxSize = 0;
238 Terminator.ExtraRelaxSize = 2;
243 Terminator.ExtraRelaxSize = 4;
248 Terminator.ExtraRelaxSize = 4;
253 Terminator.ExtraRelaxSize = 6;
258 Terminator.Branch = &
MI;
259 Terminator.TargetBlock =
260 TII->getBranchInfo(MI).getMBBTarget()->getNumber();
268 uint64_t SystemZLongBranch::initMBBInfo() {
269 MF->RenumberBlocks();
270 unsigned NumBlocks = MF->size();
273 MBBs.resize(NumBlocks);
276 Terminators.reserve(NumBlocks);
279 for (
unsigned I = 0;
I < NumBlocks; ++
I) {
281 MBBInfo &Block = MBBs[
I];
289 while (MI != End && !MI->isTerminator()) {
290 Block.Size +=
TII->getInstSizeInBytes(*MI);
293 skipNonTerminators(Position, Block);
297 if (!MI->isDebugInstr()) {
298 assert(MI->isTerminator() &&
"Terminator followed by non-terminator");
299 Terminators.push_back(describeTerminator(*MI));
300 skipTerminator(Position, Terminators.back(),
false);
301 ++Block.NumTerminators;
307 return Position.Address;
312 bool SystemZLongBranch::mustRelaxBranch(
const TerminatorInfo &Terminator,
314 if (!Terminator.Branch || Terminator.ExtraRelaxSize == 0)
317 const MBBInfo &
Target = MBBs[Terminator.TargetBlock];
318 if (Address >= Target.Address) {
319 if (Address - Target.Address <= MaxBackwardRange)
322 if (Target.Address - Address <= MaxForwardRange)
331 bool SystemZLongBranch::mustRelaxABranch() {
332 for (
auto &Terminator : Terminators)
333 if (mustRelaxBranch(Terminator, Terminator.Address))
340 void SystemZLongBranch::setWorstCaseAddresses() {
343 for (
auto &Block : MBBs) {
344 skipNonTerminators(Position, Block);
345 for (
unsigned BTI = 0, BTE = Block.NumTerminators; BTI != BTE; ++BTI) {
346 skipTerminator(Position, *TI,
true);
354 void SystemZLongBranch::splitBranchOnCount(
MachineInstr *MI,
355 unsigned AddOpcode) {
373 void SystemZLongBranch::splitCompareBranch(
MachineInstr *MI,
374 unsigned CompareOpcode) {
377 BuildMI(*MBB, MI, DL,
TII->get(CompareOpcode))
385 BRCL->addRegisterKilled(SystemZ::CC, &
TII->getRegisterInfo());
390 void SystemZLongBranch::relaxBranch(TerminatorInfo &Terminator) {
400 splitBranchOnCount(Branch, SystemZ::AHI);
403 splitBranchOnCount(Branch, SystemZ::AGHI);
406 splitCompareBranch(Branch, SystemZ::CR);
409 splitCompareBranch(Branch, SystemZ::CGR);
412 splitCompareBranch(Branch, SystemZ::CHI);
415 splitCompareBranch(Branch, SystemZ::CGHI);
418 splitCompareBranch(Branch, SystemZ::CLR);
421 splitCompareBranch(Branch, SystemZ::CLGR);
424 splitCompareBranch(Branch, SystemZ::CLFI);
427 splitCompareBranch(Branch, SystemZ::CLGFI);
433 Terminator.Size += Terminator.ExtraRelaxSize;
434 Terminator.ExtraRelaxSize = 0;
435 Terminator.Branch =
nullptr;
441 void SystemZLongBranch::relaxBranches() {
444 for (
auto &Block : MBBs) {
445 skipNonTerminators(Position, Block);
446 for (
unsigned BTI = 0, BTE = Block.NumTerminators; BTI != BTE; ++BTI) {
447 assert(Position.Address <= TI->Address &&
448 "Addresses shouldn't go forwards");
449 if (mustRelaxBranch(*TI, Position.Address))
451 skipTerminator(Position, *TI,
false);
460 uint64_t
Size = initMBBInfo();
461 if (Size <= MaxForwardRange || !mustRelaxABranch())
464 setWorstCaseAddresses();
470 return new SystemZLongBranch(TM);
unsigned Log2(Align A)
Returns the log2 of the alignment.
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.
Align getAlignment() const
Return alignment of the basic block.
bool addRegisterKilled(Register IncomingReg, const TargetRegisterInfo *RegInfo, bool AddIfNotFound=false)
We have determined MI kills a register.
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.
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.
This struct is a compact representation of a valid (non-zero power of two) alignment.
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...
static uint64_t add(uint64_t LeftOp, uint64_t RightOp)
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.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.