52#define DEBUG_TYPE "ppc-ctrloops"
54STATISTIC(NumCTRLoops,
"Number of CTR loops generated");
55STATISTIC(NumNormalLoops,
"Number of normal compare + branch loops generated");
85char PPCCTRLoops::ID = 0;
98 auto &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
102 for (
auto *
ML : MLI) {
103 if (
ML->isOutermost())
104 Changed |= processLoop(
ML);
110 assert((
I.getOpcode() != PPC::DecreaseCTRloop &&
111 I.getOpcode() != PPC::DecreaseCTR8loop) &&
112 "CTR loop pseudo is not expanded!");
119bool PPCCTRLoops::isCTRClobber(
MachineInstr *
MI,
bool CheckReads)
const {
126 return MI->definesRegister(PPC::CTR,
nullptr) ||
127 MI->definesRegister(PPC::CTR8,
nullptr);
130 if (
MI->modifiesRegister(PPC::CTR,
nullptr) ||
131 MI->modifiesRegister(PPC::CTR8,
nullptr))
134 if (
MI->getDesc().isCall())
139 if (
MI->readsRegister(PPC::CTR,
nullptr) ||
140 MI->readsRegister(PPC::CTR8,
nullptr))
147 bool Changed =
false;
151 Changed |= processLoop(
I);
159 return MI.getOpcode() == PPC::MTCTRloop ||
160 MI.getOpcode() == PPC::MTCTR8loop;
163 auto SearchForStart =
165 for (
auto &
MI : *
MBB) {
174 bool InvalidCTRLoop =
false;
182 Start = SearchForStart(Preheader);
189 InvalidCTRLoop =
true;
194 std::next(Start->getReverseIterator());
198 if (isCTRClobber(&*
I,
false)) {
199 InvalidCTRLoop =
true;
207 if (isCTRClobber(&*
I,
true)) {
208 InvalidCTRLoop =
true;
215 for (
auto &
MI : *
MBB) {
216 if (
MI.getOpcode() == PPC::DecreaseCTRloop ||
217 MI.getOpcode() == PPC::DecreaseCTR8loop)
219 else if (!InvalidCTRLoop)
221 InvalidCTRLoop |= isCTRClobber(&
MI,
true);
223 if (Dec && InvalidCTRLoop)
227 assert(Dec &&
"CTR loop is not complete!");
229 if (InvalidCTRLoop) {
230 expandNormalLoops(
ML, Start, Dec);
234 expandCTRLoops(
ML, Start, Dec);
243 Start->getParent()->getParent()->getSubtarget<
PPCSubtarget>().isPPC64();
247 assert((Preheader && Exiting) &&
248 "Preheader and exiting should exist for CTR loop!");
251 "Loop decrement stride must be 1");
253 unsigned ADDIOpcode = Is64Bit ? PPC::ADDI8 : PPC::ADDI;
254 unsigned CMPOpcode = Is64Bit ? PPC::CMPLDI : PPC::CMPLWI;
257 MRI->createVirtualRegister(Is64Bit ? &PPC::G8RC_and_G8RC_NOX0RegClass
258 : &PPC::GPRC_and_GPRC_NOR0RegClass);
260 Start->getParent()->getParent()->getProperties().reset(
261 MachineFunctionProperties::Property::NoPHIs);
264 auto PHIMIB =
BuildMI(*
ML->getHeader(),
ML->getHeader()->getFirstNonPHI(),
266 PHIMIB.addReg(Start->getOperand(0).getReg()).addMBB(Preheader);
269 MRI->createVirtualRegister(Is64Bit ? &PPC::G8RC_and_G8RC_NOX0RegClass
270 : &PPC::GPRC_and_GPRC_NOR0RegClass);
277 if (
ML->isLoopLatch(Exiting)) {
282 assert(
ML->getHeader()->pred_size() == 2 &&
283 "Loop header predecessor is not right!");
284 PHIMIB.addReg(ADDIDef).addMBB(Exiting);
291 if (
ML->contains(
P)) {
293 "Loop's header in-loop predecessor is not loop latch!");
294 PHIMIB.addReg(ADDIDef).addMBB(
P);
297 "CTR loop should not be generated for irreducible loop!");
302 Register CMPDef =
MRI->createVirtualRegister(&PPC::CRRCRegClass);
310 .
addReg(CMPMIB->getOperand(0).getReg(), 0, PPC::sub_gt);
313 Start->eraseFromParent();
320 Start->getParent()->getParent()->getSubtarget<
PPCSubtarget>().isPPC64();
326 assert((Preheader && Exiting) &&
327 "Preheader and exiting should exist for CTR loop!");
331 unsigned BDNZOpcode = Is64Bit ? PPC::BDNZ8 : PPC::BDNZ;
332 unsigned BDZOpcode = Is64Bit ? PPC::BDZ8 : PPC::BDZ;
335 "There should be only one user for loop decrement pseudo!");
338 switch (BrInstr->getOpcode()) {
342 assert(
ML->contains(BrInstr->getOperand(1).getMBB()) &&
343 "Invalid ctr loop!");
347 assert(!
ML->contains(BrInstr->getOperand(1).getMBB()) &&
348 "Invalid ctr loop!");
355 BuildMI(*Exiting, &*BrInstr, BrInstr->getDebugLoc(),
TII->get(Opcode))
356 .
addMBB(BrInstr->getOperand(1).getMBB());
unsigned const MachineRegisterInfo * MRI
const HexagonInstrInfo * TII
PowerPC CTR loops generation
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
FunctionPass class - This class is used to implement most global optimizations.
bool isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
reverse_instr_iterator instr_rend()
Instructions::iterator instr_iterator
instr_iterator instr_end()
Instructions::reverse_iterator reverse_instr_iterator
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...
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.
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
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
const MachineOperand & getOperand(unsigned i) const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Wrapper class representing virtual and physical registers.
virtual const TargetInstrInfo * getInstrInfo() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
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.
FunctionPass * createPPCCTRLoopsPass()
auto reverse(ContainerTy &&C)
void initializePPCCTRLoopsPass(PassRegistry &)