LLVM 20.0.0git
PPCEarlyReturn.cpp
Go to the documentation of this file.
1//===------------- PPCEarlyReturn.cpp - Form Early Returns ----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// A pass that form early (predicated) returns. If-conversion handles some of
10// this, but this pass picks up some remaining cases.
11//
12//===----------------------------------------------------------------------===//
13
14#include "PPC.h"
15#include "PPCInstrInfo.h"
16#include "llvm/ADT/STLExtras.h"
17#include "llvm/ADT/Statistic.h"
23
24using namespace llvm;
25
26#define DEBUG_TYPE "ppc-early-ret"
27STATISTIC(NumBCLR, "Number of early conditional returns");
28STATISTIC(NumBLR, "Number of early returns");
29
30namespace {
31 // PPCEarlyReturn pass - For simple functions without epilogue code, move
32 // returns up, and create conditional returns, to avoid unnecessary
33 // branch-to-blr sequences.
34 struct PPCEarlyReturn : public MachineFunctionPass {
35 static char ID;
36 PPCEarlyReturn() : MachineFunctionPass(ID) {
38 }
39
40 const TargetInstrInfo *TII;
41
42protected:
43 bool processBlock(MachineBasicBlock &ReturnMBB) {
44 bool Changed = false;
45
47 I = ReturnMBB.SkipPHIsLabelsAndDebug(I);
48
49 // The block must be essentially empty except for the blr.
50 if (I == ReturnMBB.end() ||
51 (I->getOpcode() != PPC::BLR && I->getOpcode() != PPC::BLR8) ||
52 I != ReturnMBB.getLastNonDebugInstr())
53 return Changed;
54
56 for (MachineBasicBlock *Pred : ReturnMBB.predecessors()) {
57 bool OtherReference = false, BlockChanged = false;
58
59 if (Pred->empty())
60 continue;
61
62 for (MachineBasicBlock::iterator J = Pred->getLastNonDebugInstr();;) {
63 if (J == Pred->end())
64 break;
65
66 if (J->getOpcode() == PPC::B) {
67 if (J->getOperand(0).getMBB() == &ReturnMBB) {
68 // This is an unconditional branch to the return. Replace the
69 // branch with a blr.
70 MachineInstr *MI = ReturnMBB.getParent()->CloneMachineInstr(&*I);
71 Pred->insert(J, MI);
72
74 K->eraseFromParent();
75 BlockChanged = true;
76 ++NumBLR;
77 continue;
78 }
79 } else if (J->getOpcode() == PPC::BCC) {
80 if (J->getOperand(2).getMBB() == &ReturnMBB) {
81 // This is a conditional branch to the return. Replace the branch
82 // with a bclr.
83 MachineInstr *MI = ReturnMBB.getParent()->CloneMachineInstr(&*I);
84 MI->setDesc(TII->get(PPC::BCCLR));
85 MachineInstrBuilder(*ReturnMBB.getParent(), MI)
86 .add(J->getOperand(0))
87 .add(J->getOperand(1));
88 Pred->insert(J, MI);
89
91 K->eraseFromParent();
92 BlockChanged = true;
93 ++NumBCLR;
94 continue;
95 }
96 } else if (J->getOpcode() == PPC::BC || J->getOpcode() == PPC::BCn) {
97 if (J->getOperand(1).getMBB() == &ReturnMBB) {
98 // This is a conditional branch to the return. Replace the branch
99 // with a bclr.
100 MachineInstr *MI = ReturnMBB.getParent()->CloneMachineInstr(&*I);
101 MI->setDesc(
102 TII->get(J->getOpcode() == PPC::BC ? PPC::BCLR : PPC::BCLRn));
103 MachineInstrBuilder(*ReturnMBB.getParent(), MI)
104 .add(J->getOperand(0));
105 Pred->insert(J, MI);
106
108 K->eraseFromParent();
109 BlockChanged = true;
110 ++NumBCLR;
111 continue;
112 }
113 } else if (J->isBranch()) {
114 if (J->isIndirectBranch()) {
115 if (ReturnMBB.hasAddressTaken())
116 OtherReference = true;
117 } else
118 for (unsigned i = 0; i < J->getNumOperands(); ++i)
119 if (J->getOperand(i).isMBB() &&
120 J->getOperand(i).getMBB() == &ReturnMBB)
121 OtherReference = true;
122 } else if (!J->isTerminator() && !J->isDebugInstr())
123 break;
124
125 if (J == Pred->begin())
126 break;
127
128 --J;
129 }
130
131 if (Pred->canFallThrough() && Pred->isLayoutSuccessor(&ReturnMBB))
132 OtherReference = true;
133
134 // Predecessors are stored in a vector and can't be removed here.
135 if (!OtherReference && BlockChanged) {
136 PredToRemove.push_back(Pred);
137 }
138
139 if (BlockChanged)
140 Changed = true;
141 }
142
143 for (MachineBasicBlock *MBB : PredToRemove)
144 MBB->removeSuccessor(&ReturnMBB, true);
145
146 if (Changed && !ReturnMBB.hasAddressTaken()) {
147 // We now might be able to merge this blr-only block into its
148 // by-layout predecessor.
149 if (ReturnMBB.pred_size() == 1) {
150 MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
151 if (PrevMBB.isLayoutSuccessor(&ReturnMBB) && PrevMBB.canFallThrough()) {
152 // Move the blr into the preceding block.
153 PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
154 PrevMBB.removeSuccessor(&ReturnMBB, true);
155 }
156 }
157
158 if (ReturnMBB.pred_empty())
159 ReturnMBB.eraseFromParent();
160 }
161
162 return Changed;
163 }
164
165public:
166 bool runOnMachineFunction(MachineFunction &MF) override {
167 if (skipFunction(MF.getFunction()))
168 return false;
169
171
172 bool Changed = false;
173
174 // If the function does not have at least two blocks, then there is
175 // nothing to do.
176 if (MF.size() < 2)
177 return Changed;
178
180 Changed |= processBlock(B);
181
182 return Changed;
183 }
184
187 MachineFunctionProperties::Property::NoVRegs);
188 }
189
190 void getAnalysisUsage(AnalysisUsage &AU) const override {
192 }
193 };
194}
195
196INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
197 "PowerPC Early-Return Creation", false, false)
198
199char PPCEarlyReturn::ID = 0;
201llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }
MachineBasicBlock & MBB
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
#define DEBUG_TYPE
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
This file contains some templates that are useful if you are working with the STL at all.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
Represent the analysis usage information of a pass.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:178
unsigned pred_size() const
iterator SkipPHIsLabelsAndDebug(iterator I, Register Reg=Register(), bool SkipPseudoOp=true)
Return the first instruction in MBB after I that is not a PHI, label or debug.
bool canFallThrough()
Return true if the block can implicitly transfer control to the block after it by falling off the end...
bool hasAddressTaken() const
Test whether this block is used as something other than the target of a terminator,...
void removeSuccessor(MachineBasicBlock *Succ, bool NormalizeSuccProbs=false)
Remove successor from the successors list of this MachineBasicBlock.
iterator getLastNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the last non-debug instruction in the basic block, or end().
bool isLayoutSuccessor(const MachineBasicBlock *MBB) const
Return true if the specified MBB will be emitted immediately after this block, such that if this bloc...
void eraseFromParent()
This method unlinks 'this' from the containing function and deletes it.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
iterator_range< pred_iterator > predecessors()
void splice(iterator Where, MachineBasicBlock *Other, iterator From)
Take an instruction from MBB 'Other' at the position From, and insert it into this MBB right before '...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
unsigned size() const
Function & getFunction()
Return the LLVM function that this machine code represents.
MachineInstr * CloneMachineInstr(const MachineInstr *Orig)
Create a new MachineInstr which is a copy of Orig, identical in all ways except the instruction has n...
const MachineInstrBuilder & add(const MachineOperand &MO) const
Representation of each machine instruction.
Definition: MachineInstr.h:69
void insert(mop_iterator InsertBefore, ArrayRef< MachineOperand > Ops)
Inserts Ops BEFORE It. Can untie/retie tied operands.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:657
FunctionPass * createPPCEarlyReturnPass()
void initializePPCEarlyReturnPass(PassRegistry &)