39#define DEBUG_TYPE "mir-canonicalizer"
49 return "Rename register operands in a canonical ordering.";
62char MIRCanonicalizer::ID;
67 "Rename Register Operands Canonically",
false,
false)
76 std::vector<MachineBasicBlock *> RPOList;
88 using StringInstrPair = std::pair<std::string, MachineInstr *>;
89 std::vector<StringInstrPair> StringInstrMap;
97 const size_t i = S.find(
'=');
98 StringInstrMap.push_back({(i == std::string::npos) ? S : S.substr(i),
II});
103 for (
auto &
II : StringInstrMap) {
106 dbgs() <<
"Splicing ";
108 dbgs() <<
" right before: ";
113 MBB->splice(getPos(),
MBB,
II.second);
127 for (
const auto &CurMI : *
MI.getParent()) {
137 std::vector<MachineInstr *> Instructions;
138 for (
auto &
MI : *
MBB) {
139 Instructions.push_back(&
MI);
142 std::map<MachineInstr *, std::vector<MachineInstr *>> MultiUsers;
143 std::map<unsigned, MachineInstr *> MultiUserLookup;
144 unsigned UseToBringDefCloserToCount = 0;
145 std::vector<MachineInstr *> PseudoIdempotentInstructions;
146 std::vector<MCRegister> PhysRegDefs;
147 for (
auto *
II : Instructions) {
148 for (
unsigned i = 1; i <
II->getNumOperands(); i++) {
159 PhysRegDefs.push_back(MO.
getReg());
163 for (
auto *
II : Instructions) {
164 if (
II->getNumOperands() == 0)
166 if (
II->mayLoadOrStore())
175 bool IsPseudoIdempotent =
true;
176 for (
unsigned i = 1; i <
II->getNumOperands(); i++) {
178 if (
II->getOperand(i).isImm()) {
182 if (
II->getOperand(i).isReg()) {
183 if (!
II->getOperand(i).getReg().isVirtual())
185 II->getOperand(i).getReg().asMCReg())) {
190 IsPseudoIdempotent =
false;
194 if (IsPseudoIdempotent) {
195 PseudoIdempotentInstructions.push_back(
II);
202 unsigned Distance = ~0U;
205 for (
auto &UO :
MRI->use_nodbg_operands(MO.
getReg())) {
208 const unsigned DefLoc = getInstrIdx(*Def);
209 const unsigned UseLoc = getInstrIdx(*UseInst);
210 const unsigned Delta = (UseLoc - DefLoc);
212 if (UseInst->
getParent() != Def->getParent())
214 if (DefLoc >= UseLoc)
217 if (Delta < Distance) {
219 UseToBringDefCloserTo = UseInst;
220 MultiUserLookup[UseToBringDefCloserToCount++] = UseToBringDefCloserTo;
224 const auto BBE =
MBB->instr_end();
228 for (
auto BBI =
MBB->instr_begin(); BBI != BBE; ++BBI) {
230 if (DefI != BBE && UseI != BBE)
238 if (&*BBI == UseToBringDefCloserTo) {
244 if (DefI == BBE || UseI == BBE)
248 dbgs() <<
"Splicing ";
250 dbgs() <<
" right before: ";
254 MultiUsers[UseToBringDefCloserTo].push_back(Def);
256 MBB->splice(UseI,
MBB, DefI);
260 for (
const auto &
E : MultiUserLookup) {
263 return &MI == E.second;
266 if (UseI ==
MBB->instr_end())
270 dbgs() <<
"Rescheduling Multi-Use Instructions Lexographically.");
272 MultiUsers[
E.second],
MBB,
276 PseudoIdempotentInstCount = PseudoIdempotentInstructions.size();
277 LLVM_DEBUG(
dbgs() <<
"Rescheduling Idempotent Instructions Lexographically.");
279 PseudoIdempotentInstructions,
MBB,
289 std::vector<MachineInstr *>
Copies;
297 if (!
MI->getOperand(0).isReg())
299 if (!
MI->getOperand(1).isReg())
302 const Register Dst =
MI->getOperand(0).getReg();
303 const Register Src =
MI->getOperand(1).getReg();
305 if (!Dst.isVirtual())
307 if (!Src.isVirtual())
314 if (!
MRI.getRegClassOrNull(Dst))
316 if (
MRI.getRegClass(Dst) !=
MRI.getRegClass(Src))
319 std::vector<MachineOperand *>
Uses;
322 for (
auto *MO :
Uses)
326 MI->eraseFromParent();
335 for (
auto &
MI : *
MBB) {
336 for (
auto &MO :
MI.operands()) {
339 if (!MO.isDef() && MO.isKill()) {
344 if (MO.isDef() && MO.isDead()) {
357 dbgs() <<
"\n\n NEW BASIC BLOCK: " <<
MBB->getName() <<
" \n\n";
358 dbgs() <<
"\n\n================================================\n\n";
371 unsigned IdempotentInstCount = 0;
375 Changed |= Renamer.renameVRegs(
MBB, BasicBlockNum);
384 dbgs() <<
"\n\n================================================\n\n");
390 static unsigned functionNum = 0;
392 if (Opts.mir_canonicalizer_nth_function != ~0U) {
393 if (Opts.mir_canonicalizer_nth_function != functionNum++)
401 std::vector<MachineBasicBlock *> RPOList =
GetRPOList(MF);
404 dbgs() <<
"\n\n NEW MACHINE FUNCTION: " << MF.
getName() <<
" \n\n";
405 dbgs() <<
"\n\n================================================\n\n";
406 dbgs() <<
"Total Basic Blocks: " << RPOList.size() <<
"\n";
409 <<
"\n\n================================================\n\n";);
414 VRegRenamer Renamer(Opts,
MRI);
415 for (
auto *
MBB : RPOList)
Expand Atomic instructions
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static bool runOnBasicBlock(MachineBasicBlock *MBB, unsigned BasicBlockNum, VRegRenamer &Renamer)
static bool rescheduleLexographically(std::vector< MachineInstr * > instructions, MachineBasicBlock *MBB, std::function< MachineBasicBlock::iterator()> getPos)
static bool doDefKillClear(MachineBasicBlock *MBB)
static bool propagateLocalCopies(MachineBasicBlock *MBB)
static bool rescheduleCanonically(unsigned &PseudoIdempotentInstCount, MachineBasicBlock *MBB)
mir Rename Register Operands static false std::vector< MachineBasicBlock * > GetRPOList(MachineFunction &MF)
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
MachineRegisterInfo & MRI
Remove Loads Into Fake Uses
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
MachineInstrBundleIterator< MachineInstr > iterator
LLVM_ABI StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
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.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Representation of each machine instruction.
const MachineBasicBlock * getParent() const
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
LLVM_ABI void dump() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Represent a constant reference to a string, i.e.
VRegRenamer - This class is used for renaming vregs in a machine basic block according to semantics o...
A raw_ostream that writes to an std::string.
This is an optimization pass for GlobalISel generic memory operations.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
LLVM_ABI char & MIRCanonicalizerID
MIRCanonicalizer - This pass canonicalizes MIR by renaming vregs according to the semantics of the in...
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
const CodeGenOptions & getCodeGenOptions(const LLVMContext &C)
Implement std::hash so that hash_code can be used in STL containers.
Function object to check whether the first component of a container supported by std::get (like std::...