31 #define DEBUG_TYPE "ppc-branch-select" 33 STATISTIC(NumExpanded,
"Number of branches expanded to long format");
45 std::vector<std::pair<unsigned, unsigned>> BlockSizes;
48 int FirstImpreciseBlock = -1;
65 StringRef getPassName()
const override {
return "PowerPC Branch Selector"; }
90 if (Alignment <= ParentAlign)
95 if (FirstImpreciseBlock < 0)
107 unsigned InitialOffset = 0;
111 return InitialOffset;
127 unsigned AlignExtra = GetAlignmentAdjustment(*MBB, FuncSize);
129 auto &BS = BlockSizes[MBB->
getNumber()-1];
130 BS.first += AlignExtra;
131 BS.second = AlignExtra;
133 FuncSize += AlignExtra;
139 if (
MI.isInlineAsm() && (FirstImpreciseBlock < 0))
140 FirstImpreciseBlock = MBB->getNumber();
143 BlockSizes[MBB->getNumber()].first =
BlockSize;
158 auto &BS = BlockSizes[MBB->
getNumber()-1];
159 BS.first -= BS.second;
162 unsigned AlignExtra = GetAlignmentAdjustment(*MBB, Offset);
164 BS.first += AlignExtra;
165 BS.second = AlignExtra;
167 Offset += AlignExtra;
170 Offset += BlockSizes[MBB->
getNumber()].first;
182 bool NeedExtraAdjustment =
false;
187 BranchSize = BrOffset;
191 BranchSize += BlockSizes[DestBlock].first;
192 for (
unsigned i = DestBlock+1,
e = Src->
getNumber(); i <
e; ++i) {
193 BranchSize += BlockSizes[i].first;
197 NeedExtraAdjustment = (FirstImpreciseBlock >= 0) &&
198 (DestBlock >= FirstImpreciseBlock);
203 BranchSize = BlockSizes[StartBlock].first - BrOffset;
206 for (
unsigned i = StartBlock+1,
e = Dest->
getNumber(); i !=
e; ++i) {
207 BranchSize += BlockSizes[i].first;
211 NeedExtraAdjustment = (FirstImpreciseBlock >= 0) &&
212 (Src->
getNumber() >= FirstImpreciseBlock);
257 if (NeedExtraAdjustment)
258 BranchSize += MaxAlign.
value() - 4;
269 FirstImpreciseBlock = -1;
272 unsigned FuncSize = ComputeBlockSizes(Fn);
277 if (FuncSize < (1 << 15)) {
291 bool MadeChange =
true;
292 bool EverMadeChange =
false;
300 unsigned MBBStartOffset = 0;
304 if (
I->getOpcode() == PPC::BCC && !
I->getOperand(2).isImm())
305 Dest =
I->getOperand(2).getMBB();
306 else if ((
I->getOpcode() == PPC::BC ||
I->getOpcode() == PPC::BCn) &&
307 !
I->getOperand(1).isImm())
308 Dest =
I->getOperand(1).getMBB();
309 else if ((
I->getOpcode() == PPC::BDNZ8 ||
I->getOpcode() ==
PPC::BDNZ ||
310 I->getOpcode() == PPC::BDZ8 ||
I->getOpcode() ==
PPC::BDZ) &&
311 !
I->getOperand(0).isImm())
312 Dest =
I->getOperand(0).getMBB();
321 int BranchSize = computeBranchSize(Fn, &MBB, Dest, MBBStartOffset);
333 if (
I->getOpcode() == PPC::BCC) {
339 Register CRReg =
I->getOperand(1).getReg();
342 BuildMI(MBB,
I, dl, TII->get(PPC::BCC))
344 }
else if (
I->getOpcode() == PPC::BC) {
345 Register CRBit =
I->getOperand(0).getReg();
347 }
else if (
I->getOpcode() == PPC::BCn) {
348 Register CRBit =
I->getOperand(0).getReg();
352 }
else if (
I->getOpcode() == PPC::BDNZ8) {
353 BuildMI(MBB,
I, dl, TII->get(PPC::BDZ8)).addImm(2);
356 }
else if (
I->getOpcode() == PPC::BDZ8) {
357 BuildMI(MBB,
I, dl, TII->get(PPC::BDNZ8)).addImm(2);
380 modifyAdjustment(Fn);
383 EverMadeChange |= MadeChange;
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them...
Align getAlignment() const
getAlignment - Return the alignment of the function.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
constexpr bool isInt< 16 >(int64_t x)
STATISTIC(NumFunctions, "Total number of functions")
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...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
void initializePPCBSelPass(PassRegistry &)
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Align getAlignment() const
Return alignment of the basic block.
CHAIN = BDNZ CHAIN, DESTBB - These are used to create counter-based loops.
int getNumber() const
MachineBasicBlocks are uniquely numbered at the function level, unless they're not in a MachineFuncti...
virtual const TargetInstrInfo * getInstrInfo() const
uint64_t value() const
This is a hole in the type system and should not be abused.
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...
FunctionPass * createPPCBranchSelectionPass()
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
MachineBasicBlock * getBlockNumbered(unsigned N) const
getBlockNumbered - MachineBasicBlocks are automatically numbered when they are inserted into the mach...
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
FunctionPass class - This class is used to implement most global optimizations.
static constexpr const Align None()
Returns a default constructed Align which corresponds to no alignment.
#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.
static const int BlockSize
Iterator for intrusive lists based on ilist_node.
Align max(MaybeAlign Lhs, Align Rhs)
Predicate
Predicate - These are "(BI << 5) | BO" for various predicates.
bool use_empty(unsigned RegNo) const
use_empty - Return true if there are no instructions using the specified register.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Predicate InvertPredicate(Predicate Opcode)
Invert the specified predicate. != -> ==, < -> >=.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
unsigned getInstSizeInBytes(const MachineInstr &MI) const override
GetInstSize - Return the number of bytes of code the specified instruction may be.
INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector", false, false) FunctionPass *llvm
createPPCBranchSelectionPass - returns an instance of the Branch Selection Pass
StringRef - Represent a constant reference to a string, i.e.
Properties which a MachineFunction may have at a given point in time.
Wrapper class representing virtual and physical registers.