50#define DEBUG_TYPE "ppc-ctrloops"
52STATISTIC(NumCTRLoops,
"Number of CTR loops generated");
53STATISTIC(NumNormalLoops,
"Number of normal compare + branch loops generated");
83char PPCCTRLoops::ID = 0;
96 auto &MLI = getAnalysis<MachineLoopInfoWrapperPass>().getLI();
100 for (
auto *
ML : MLI) {
101 if (
ML->isOutermost())
102 Changed |= processLoop(
ML);
108 assert((
I.getOpcode() != PPC::DecreaseCTRloop &&
109 I.getOpcode() != PPC::DecreaseCTR8loop) &&
110 "CTR loop pseudo is not expanded!");
117bool PPCCTRLoops::isCTRClobber(
MachineInstr *
MI,
bool CheckReads)
const {
124 return MI->definesRegister(PPC::CTR,
nullptr) ||
125 MI->definesRegister(PPC::CTR8,
nullptr);
128 if (
MI->modifiesRegister(PPC::CTR,
nullptr) ||
129 MI->modifiesRegister(PPC::CTR8,
nullptr))
132 if (
MI->getDesc().isCall())
137 if (
MI->readsRegister(PPC::CTR,
nullptr) ||
138 MI->readsRegister(PPC::CTR8,
nullptr))
145 bool Changed =
false;
149 Changed |= processLoop(
I);
157 return MI.getOpcode() == PPC::MTCTRloop ||
158 MI.getOpcode() == PPC::MTCTR8loop;
161 auto SearchForStart =
163 for (
auto &
MI : *
MBB) {
172 bool InvalidCTRLoop =
false;
180 Start = SearchForStart(Preheader);
187 InvalidCTRLoop =
true;
192 std::next(Start->getReverseIterator());
196 if (isCTRClobber(&*
I,
false)) {
197 InvalidCTRLoop =
true;
205 if (isCTRClobber(&*
I,
true)) {
206 InvalidCTRLoop =
true;
213 for (
auto &
MI : *
MBB) {
214 if (
MI.getOpcode() == PPC::DecreaseCTRloop ||
215 MI.getOpcode() == PPC::DecreaseCTR8loop)
217 else if (!InvalidCTRLoop)
219 InvalidCTRLoop |= isCTRClobber(&
MI,
true);
221 if (Dec && InvalidCTRLoop)
225 assert(Dec &&
"CTR loop is not complete!");
227 if (InvalidCTRLoop) {
228 expandNormalLoops(
ML, Start, Dec);
232 expandCTRLoops(
ML, Start, Dec);
241 Start->getParent()->getParent()->getSubtarget<
PPCSubtarget>().isPPC64();
245 assert((Preheader && Exiting) &&
246 "Preheader and exiting should exist for CTR loop!");
249 "Loop decrement stride must be 1");
251 unsigned ADDIOpcode = Is64Bit ? PPC::ADDI8 : PPC::ADDI;
252 unsigned CMPOpcode = Is64Bit ? PPC::CMPLDI : PPC::CMPLWI;
255 MRI->createVirtualRegister(Is64Bit ? &PPC::G8RC_and_G8RC_NOX0RegClass
256 : &PPC::GPRC_and_GPRC_NOR0RegClass);
258 Start->getParent()->getParent()->getProperties().reset(
259 MachineFunctionProperties::Property::NoPHIs);
262 auto PHIMIB =
BuildMI(*
ML->getHeader(),
ML->getHeader()->getFirstNonPHI(),
264 PHIMIB.addReg(Start->getOperand(0).getReg()).addMBB(Preheader);
267 MRI->createVirtualRegister(Is64Bit ? &PPC::G8RC_and_G8RC_NOX0RegClass
268 : &PPC::GPRC_and_GPRC_NOR0RegClass);
275 if (
ML->isLoopLatch(Exiting)) {
280 assert(
ML->getHeader()->pred_size() == 2 &&
281 "Loop header predecessor is not right!");
282 PHIMIB.addReg(ADDIDef).addMBB(Exiting);
289 if (
ML->contains(
P)) {
291 "Loop's header in-loop predecessor is not loop latch!");
292 PHIMIB.addReg(ADDIDef).addMBB(
P);
295 "CTR loop should not be generated for irreducible loop!");
300 Register CMPDef =
MRI->createVirtualRegister(&PPC::CRRCRegClass);
308 .
addReg(CMPMIB->getOperand(0).getReg(), 0, PPC::sub_gt);
311 Start->eraseFromParent();
318 Start->getParent()->getParent()->getSubtarget<
PPCSubtarget>().isPPC64();
324 assert((Preheader && Exiting) &&
325 "Preheader and exiting should exist for CTR loop!");
329 unsigned BDNZOpcode = Is64Bit ? PPC::BDNZ8 : PPC::BDNZ;
330 unsigned BDZOpcode = Is64Bit ? PPC::BDZ8 : PPC::BDZ;
333 "There should be only one user for loop decrement pseudo!");
336 switch (BrInstr->getOpcode()) {
340 assert(
ML->contains(BrInstr->getOperand(1).getMBB()) &&
341 "Invalid ctr loop!");
345 assert(!
ML->contains(BrInstr->getOperand(1).getMBB()) &&
346 "Invalid ctr loop!");
353 BuildMI(*Exiting, &*BrInstr, BrInstr->getDebugLoc(),
TII->get(Opcode))
354 .
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.
reverse_instr_iterator instr_rend()
Instructions::iterator instr_iterator
instr_iterator instr_end()
bool isLiveIn(MCRegister Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
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 &)