30#define DEBUG_TYPE "ppc-branch-select"
32STATISTIC(NumExpanded,
"Number of branches expanded to long format");
33STATISTIC(NumPrefixed,
"Number of prefixed instructions");
35 "Number of prefixed instructions that have been aligned");
45 std::vector<std::pair<unsigned, unsigned>> BlockSizes;
48 int FirstImpreciseBlock = -1;
50 unsigned GetAlignmentAdjustment(MachineBasicBlock &
MBB,
unsigned Offset);
51 unsigned ComputeBlockSizes(MachineFunction &Fn);
52 void modifyAdjustment(MachineFunction &Fn);
53 int computeBranchSize(MachineFunction &Fn,
54 const MachineBasicBlock *Src,
55 const MachineBasicBlock *Dest,
58 bool runOnMachineFunction(MachineFunction &Fn)
override;
60 MachineFunctionProperties getRequiredProperties()
const override {
61 return MachineFunctionProperties().setNoVRegs();
64 StringRef getPassName()
const override {
return "PowerPC Branch Selector"; }
84 if (Alignment ==
Align(1))
89 if (Alignment <= ParentAlign)
94 if (FirstImpreciseBlock < 0)
106 unsigned InitialOffset = 0;
110 return InitialOffset;
114unsigned PPCBSel::ComputeBlockSizes(MachineFunction &Fn) {
115 const PPCInstrInfo *
TII =
116 static_cast<const PPCInstrInfo *
>(Fn.
getSubtarget().getInstrInfo());
119 for (MachineBasicBlock &
MBB : Fn) {
123 unsigned AlignExtra = GetAlignmentAdjustment(
MBB, FuncSize);
126 BS.first += AlignExtra;
127 BS.second = AlignExtra;
129 FuncSize += AlignExtra;
133 unsigned UnalignedBytesRemaining = 0;
134 for (MachineInstr &
MI :
MBB) {
135 unsigned MINumBytes =
TII->getInstSizeInBytes(
MI);
136 if (
MI.isInlineAsm() && (FirstImpreciseBlock < 0))
138 if (
TII->isPrefixed(
MI.getOpcode())) {
157 if (!UnalignedBytesRemaining) {
159 UnalignedBytesRemaining = 60;
160 NumPrefixedAligned++;
163 UnalignedBytesRemaining -= std::min(UnalignedBytesRemaining, MINumBytes);
175void PPCBSel::modifyAdjustment(MachineFunction &Fn) {
177 for (MachineBasicBlock &
MBB : Fn) {
180 BS.first -= BS.second;
183 unsigned AlignExtra = GetAlignmentAdjustment(
MBB,
Offset);
185 BS.first += AlignExtra;
186 BS.second = AlignExtra;
197int PPCBSel::computeBranchSize(MachineFunction &Fn,
198 const MachineBasicBlock *Src,
199 const MachineBasicBlock *Dest,
203 bool NeedExtraAdjustment =
false;
204 if (Dest->
getNumber() <= Src->getNumber()) {
208 BranchSize = BrOffset;
209 MaxAlign = std::max(MaxAlign, Src->getAlignment());
212 BranchSize += BlockSizes[DestBlock].first;
213 for (
unsigned i = DestBlock+1, e = Src->getNumber(); i < e; ++i) {
214 BranchSize += BlockSizes[i].first;
218 NeedExtraAdjustment = (FirstImpreciseBlock >= 0) &&
219 (DestBlock >= FirstImpreciseBlock);
223 unsigned StartBlock = Src->getNumber();
224 BranchSize = BlockSizes[StartBlock].first - BrOffset;
227 for (
unsigned i = StartBlock+1, e = Dest->
getNumber(); i != e; ++i) {
228 BranchSize += BlockSizes[i].first;
232 NeedExtraAdjustment = (FirstImpreciseBlock >= 0) &&
233 (Src->getNumber() >= FirstImpreciseBlock);
278 if (NeedExtraAdjustment)
279 BranchSize += MaxAlign.
value() - 4;
284bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
285 const PPCInstrInfo *
TII =
286 static_cast<const PPCInstrInfo *
>(Fn.
getSubtarget().getInstrInfo());
290 FirstImpreciseBlock = -1;
293 unsigned FuncSize = ComputeBlockSizes(Fn);
298 if (FuncSize < (1 << 15)) {
312 bool MadeChange =
true;
313 bool EverMadeChange =
false;
320 MachineBasicBlock &
MBB = *MFI;
321 unsigned MBBStartOffset = 0;
324 MachineBasicBlock *Dest =
nullptr;
325 if (
I->getOpcode() == PPC::BCC && !
I->getOperand(2).isImm())
326 Dest =
I->getOperand(2).getMBB();
327 else if ((
I->getOpcode() == PPC::BC ||
I->getOpcode() == PPC::BCn) &&
328 !
I->getOperand(1).isImm())
329 Dest =
I->getOperand(1).getMBB();
330 else if ((
I->getOpcode() == PPC::BDNZ8 ||
I->getOpcode() == PPC::BDNZ ||
331 I->getOpcode() == PPC::BDZ8 ||
I->getOpcode() == PPC::BDZ) &&
332 !
I->getOperand(0).isImm())
333 Dest =
I->getOperand(0).getMBB();
336 MBBStartOffset +=
TII->getInstSizeInBytes(*
I);
342 int BranchSize = computeBranchSize(Fn, &
MBB, Dest, MBBStartOffset);
351 MachineInstr &OldBranch = *
I;
354 if (
I->getOpcode() == PPC::BCC) {
360 Register CRReg =
I->getOperand(1).getReg();
365 }
else if (
I->getOpcode() == PPC::BC) {
366 Register CRBit =
I->getOperand(0).getReg();
368 }
else if (
I->getOpcode() == PPC::BCn) {
369 Register CRBit =
I->getOperand(0).getReg();
371 }
else if (
I->getOpcode() == PPC::BDNZ) {
373 }
else if (
I->getOpcode() == PPC::BDNZ8) {
375 }
else if (
I->getOpcode() == PPC::BDZ) {
377 }
else if (
I->getOpcode() == PPC::BDZ8) {
401 modifyAdjustment(Fn);
404 EverMadeChange |= MadeChange;
408 return EverMadeChange;
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
Promote Memory to Register
static unsigned GetInitialOffset(MachineFunction &Fn)
We need to be careful about the offset of the first block in the function because it might not have t...
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
static const int BlockSize
FunctionPass class - This class is used to implement most global optimizations.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Align getAlignment() const
Return alignment of the basic block.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
MachineBasicBlock * getBlockNumbered(unsigned N) const
getBlockNumbered - MachineBasicBlocks are automatically numbered when they are inserted into the mach...
Align getAlignment() const
getAlignment - Return the alignment of the function.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
BasicBlockListType::iterator iterator
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addMBB(MachineBasicBlock *MBB, unsigned TargetFlags=0) const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
bool use_empty(Register RegNo) const
use_empty - Return true if there are no instructions using the specified register.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
Predicate InvertPredicate(Predicate Opcode)
Invert the specified predicate. != -> ==, < -> >=.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
constexpr bool isInt(int64_t x)
Checks if an integer fits into the given bit width.
uint64_t offsetToAlignment(uint64_t Value, Align Alignment)
Returns the offset to the next integer (mod 2**64) that is greater than or equal to Value and is a mu...
FunctionPass * createPPCBranchSelectionPass()
uint64_t value() const
This is a hole in the type system and should not be abused.