Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
PPCCTRLoopsVerify.cpp
Go to the documentation of this file.
1//===-- PPCCTRLoops.cpp - Verify CTR loops -----------------===//
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// This pass verifies that all bdnz/bdz instructions are dominated by a loop
10// mtctr before any other instructions that might clobber the ctr register.
11//
12//===----------------------------------------------------------------------===//
13
14// CTR loops are produced by the HardwareLoops pass and this pass is simply a
15// verification that no invalid CTR loops are produced. As such, it isn't
16// something that needs to be run (or even defined) for Release builds so the
17// entire file is guarded by NDEBUG.
18#ifndef NDEBUG
19#include "MCTargetDesc/PPCMCTargetDesc.h"
20#include "PPC.h"
21#include "llvm/ADT/SmallSet.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/ilist_iterator.h"
25#include "llvm/CodeGen/MachineBasicBlock.h"
26#include "llvm/CodeGen/MachineDominators.h"
27#include "llvm/CodeGen/MachineFunction.h"
28#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineInstr.h"
30#include "llvm/CodeGen/MachineInstrBundleIterator.h"
31#include "llvm/CodeGen/MachineOperand.h"
32#include "llvm/CodeGen/Register.h"
33#include "llvm/InitializePasses.h"
34#include "llvm/Pass.h"
35#include "llvm/PassRegistry.h"
36#include "llvm/Support/Debug.h"
37#include "llvm/Support/ErrorHandling.h"
38#include "llvm/Support/Printable.h"
39#include "llvm/Support/raw_ostream.h"
40
41using namespacellvm;
42
43#define DEBUG_TYPE "ppc-ctrloops-verify"
44
45namespace{
46
47structPPCCTRLoopsVerify :publicMachineFunctionPass {
48public:
49staticcharID;
50
51 PPCCTRLoopsVerify() :MachineFunctionPass(ID) {
52initializePPCCTRLoopsVerifyPass(*PassRegistry::getPassRegistry());
53 }
54
55voidgetAnalysisUsage(AnalysisUsage &AU) const override{
56 AU.addRequired<MachineDominatorTreeWrapperPass>();
57MachineFunctionPass::getAnalysisUsage(AU);
58 }
59
60boolrunOnMachineFunction(MachineFunction &MF)override;
61
62private:
63MachineDominatorTree *MDT;
64 };
65
66char PPCCTRLoopsVerify::ID = 0;
67}// end anonymous namespace
68
69INITIALIZE_PASS_BEGIN(PPCCTRLoopsVerify,"ppc-ctr-loops-verify",
70"PowerPC CTR Loops Verify",false,false)
71INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)
72INITIALIZE_PASS_END(PPCCTRLoopsVerify, "ppc-ctr-loops-verify",
73 "PowerPC CTRLoopsVerify",false,false)
74
75FunctionPass *llvm::createPPCCTRLoopsVerify() {
76returnnew PPCCTRLoopsVerify();
77}
78
79staticboolclobbersCTR(constMachineInstr &MI) {
80for (constMachineOperand &MO :MI.operands()) {
81if (MO.isReg()) {
82if (MO.isDef() && (MO.getReg() == PPC::CTR || MO.getReg() == PPC::CTR8))
83returntrue;
84 }elseif (MO.isRegMask()) {
85if (MO.clobbersPhysReg(PPC::CTR) || MO.clobbersPhysReg(PPC::CTR8))
86returntrue;
87 }
88 }
89
90returnfalse;
91}
92
93staticboolverifyCTRBranch(MachineBasicBlock *MBB,
94MachineBasicBlock::iteratorI) {
95MachineBasicBlock::iterator BI =I;
96SmallSet<MachineBasicBlock *, 16> Visited;
97SmallVector<MachineBasicBlock *, 8> Preds;
98bool CheckPreds;
99
100if (I ==MBB->begin()) {
101 Visited.insert(MBB);
102goto queue_preds;
103 }else
104 --I;
105
106check_block:
107 Visited.insert(MBB);
108if (I ==MBB->end())
109goto queue_preds;
110
111 CheckPreds =true;
112for (MachineBasicBlock::iterator IE =MBB->begin();; --I) {
113unsigned Opc =I->getOpcode();
114if (Opc == PPC::MTCTRloop || Opc == PPC::MTCTR8loop) {
115 CheckPreds =false;
116break;
117 }
118
119if (I != BI &&clobbersCTR(*I)) {
120LLVM_DEBUG(dbgs() <<printMBBReference(*MBB) <<" (" <<MBB->getFullName()
121 <<") instruction " << *I
122 <<" clobbers CTR, invalidating "
123 <<printMBBReference(*BI->getParent()) <<" ("
124 << BI->getParent()->getFullName() <<") instruction "
125 << *BI <<"\n");
126returnfalse;
127 }
128
129if (I == IE)
130break;
131 }
132
133if (!CheckPreds && Preds.empty())
134returntrue;
135
136if (CheckPreds) {
137queue_preds:
138if (MachineFunction::iterator(MBB) ==MBB->getParent()->begin()) {
139LLVM_DEBUG(dbgs() <<"Unable to find a MTCTR instruction for "
140 <<printMBBReference(*BI->getParent()) <<" ("
141 << BI->getParent()->getFullName() <<") instruction "
142 << *BI <<"\n");
143returnfalse;
144 }
145
146append_range(Preds,MBB->predecessors());
147 }
148
149do {
150MBB = Preds.pop_back_val();
151if (!Visited.count(MBB)) {
152I =MBB->getLastNonDebugInstr();
153goto check_block;
154 }
155 }while (!Preds.empty());
156
157returntrue;
158}
159
160bool PPCCTRLoopsVerify::runOnMachineFunction(MachineFunction &MF) {
161 MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
162
163// Verify that all bdnz/bdz instructions are dominated by a loop mtctr before
164// any other instructions that might clobber the ctr register.
165for (MachineBasicBlock &MBB : MF) {
166if (!MDT->isReachableFromEntry(&MBB))
167continue;
168
169for (MachineBasicBlock::iterator MII =MBB.getFirstTerminator(),
170 MIIE =MBB.end(); MII != MIIE; ++MII) {
171unsigned Opc = MII->getOpcode();
172if (Opc == PPC::BDNZ8 || Opc == PPC::BDNZ ||
173 Opc == PPC::BDZ8 || Opc == PPC::BDZ)
174if (!verifyCTRBranch(&MBB, MII))
175llvm_unreachable("Invalid PPC CTR loop!");
176 }
177 }
178
179returnfalse;
180}
181#endif// NDEBUG
MBB
MachineBasicBlock & MBB
Definition:ARMSLSHardening.cpp:71
Debug.h
LLVM_DEBUG
#define LLVM_DEBUG(...)
Definition:Debug.h:106
Loops
Hexagon Hardware Loops
Definition:HexagonHardwareLoops.cpp:373
MI
IRTranslator LLVM IR MI
Definition:IRTranslator.cpp:112
InitializePasses.h
loops
loops
Definition:LoopInfo.cpp:1221
I
#define I(x, y, z)
Definition:MD5.cpp:58
MachineBasicBlock.h
MachineDominators.h
MachineFunctionPass.h
MachineFunction.h
MachineInstrBundleIterator.h
MachineInstr.h
MachineOperand.h
verifyCTRBranch
static bool verifyCTRBranch(MachineBasicBlock *MBB, MachineBasicBlock::iterator I)
Definition:PPCCTRLoopsVerify.cpp:93
Verify
ppc ctr loops PowerPC CTR Loops Verify
Definition:PPCCTRLoopsVerify.cpp:73
verify
ppc ctr loops verify
Definition:PPCCTRLoopsVerify.cpp:72
clobbersCTR
static bool clobbersCTR(const MachineInstr &MI)
Definition:PPCCTRLoopsVerify.cpp:79
PPCMCTargetDesc.h
PPC.h
PassRegistry.h
INITIALIZE_PASS_DEPENDENCY
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition:PassSupport.h:55
INITIALIZE_PASS_END
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition:PassSupport.h:57
INITIALIZE_PASS_BEGIN
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition:PassSupport.h:52
Pass.h
Printable.h
Register.h
SmallSet.h
This file defines the SmallSet class.
SmallVector.h
This file defines the SmallVector class.
StringRef.h
T
llvm::AnalysisUsage
Represent the analysis usage information of a pass.
Definition:PassAnalysisSupport.h:47
llvm::AnalysisUsage::addRequired
AnalysisUsage & addRequired()
Definition:PassAnalysisSupport.h:75
llvm::FunctionPass
FunctionPass class - This class is used to implement most global optimizations.
Definition:Pass.h:310
llvm::MachineBasicBlock
Definition:MachineBasicBlock.h:125
llvm::MachineBasicBlock::getFirstTerminator
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
Definition:MachineBasicBlock.cpp:244
llvm::MachineBasicBlock::begin
iterator begin()
Definition:MachineBasicBlock.h:355
llvm::MachineBasicBlock::getLastNonDebugInstr
iterator getLastNonDebugInstr(bool SkipPseudoOp=true)
Returns an iterator to the last non-debug instruction in the basic block, or end().
Definition:MachineBasicBlock.cpp:273
llvm::MachineBasicBlock::end
iterator end()
Definition:MachineBasicBlock.h:357
llvm::MachineBasicBlock::getParent
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
Definition:MachineBasicBlock.h:311
llvm::MachineBasicBlock::getFullName
std::string getFullName() const
Return a formatted string to identify this block and its parent function.
Definition:MachineBasicBlock.cpp:334
llvm::MachineBasicBlock::predecessors
iterator_range< pred_iterator > predecessors()
Definition:MachineBasicBlock.h:438
llvm::MachineDominatorTreeWrapperPass
Analysis pass which computes a MachineDominatorTree.
Definition:MachineDominators.h:131
llvm::MachineDominatorTree
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
Definition:MachineDominators.h:75
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition:MachineFunctionPass.h:30
llvm::MachineFunctionPass::getAnalysisUsage
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Definition:MachineFunctionPass.cpp:169
llvm::MachineFunctionPass::runOnMachineFunction
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
llvm::MachineFunction
Definition:MachineFunction.h:267
llvm::MachineFunction::begin
iterator begin()
Definition:MachineFunction.h:947
llvm::MachineInstrBundleIterator< MachineInstr >
llvm::MachineInstr
Representation of each machine instruction.
Definition:MachineInstr.h:71
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition:MachineOperand.h:48
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition:PassRegistry.cpp:24
llvm::SmallSet
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition:SmallSet.h:132
llvm::SmallSet::count
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition:SmallSet.h:175
llvm::SmallSet::insert
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition:SmallSet.h:181
llvm::SmallVectorBase::empty
bool empty() const
Definition:SmallVector.h:81
llvm::SmallVectorImpl::pop_back_val
T pop_back_val()
Definition:SmallVector.h:673
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
unsigned
ilist_iterator.h
ErrorHandling.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
false
Definition:StackSlotColoring.cpp:193
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition:CallingConv.h:24
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::append_range
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition:STLExtras.h:2115
llvm::initializePPCCTRLoopsVerifyPass
void initializePPCCTRLoopsVerifyPass(PassRegistry &)
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition:Debug.cpp:163
llvm::createPPCCTRLoopsVerify
FunctionPass * createPPCCTRLoopsVerify()
Definition:PPCCTRLoopsVerify.cpp:75
llvm::printMBBReference
Printable printMBBReference(const MachineBasicBlock &MBB)
Prints a machine basic block reference.
Definition:MachineBasicBlock.cpp:122
raw_ostream.h

Generated on Thu Jul 17 2025 15:18:06 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp