45 #define DEBUG_TYPE "mir-canonicalizer" 50 cl::desc(
"Function number to canonicalize."));
60 return "Rename register operands in a canonical ordering.";
78 "Rename Register Operands Canonically",
false,
false)
87 std::vector<MachineBasicBlock *> RPOList;
88 for (
auto MBB : RPOT) {
89 RPOList.push_back(MBB);
100 bool Changed =
false;
101 using StringInstrPair = std::pair<std::string, MachineInstr *>;
102 std::vector<StringInstrPair> StringInstrMap;
104 for (
auto *II : instructions) {
111 const size_t i = S.find(
"=");
112 StringInstrMap.push_back({(i == std::string::npos) ? S : S.substr(i), II});
116 [](
const StringInstrPair &a,
const StringInstrPair &b) ->
bool {
117 return (a.first < b.first);
120 for (
auto &II : StringInstrMap) {
123 dbgs() <<
"Splicing ";
125 dbgs() <<
" right before: ";
130 MBB->
splice(getPos(), MBB, II.second);
139 bool Changed =
false;
144 for (
auto &CurMI : *
MI.getParent()) {
154 std::vector<MachineInstr *> Instructions;
155 for (
auto &
MI : *MBB) {
156 Instructions.push_back(&
MI);
159 std::map<MachineInstr *, std::vector<MachineInstr *>> MultiUsers;
160 std::map<unsigned, MachineInstr *> MultiUserLookup;
161 unsigned UseToBringDefCloserToCount = 0;
162 std::vector<MachineInstr *> PseudoIdempotentInstructions;
163 std::vector<unsigned> PhysRegDefs;
164 for (
auto *II : Instructions) {
165 for (
unsigned i = 1; i < II->getNumOperands(); i++) {
176 PhysRegDefs.push_back(MO.
getReg());
180 for (
auto *II : Instructions) {
181 if (II->getNumOperands() == 0)
183 if (II->mayLoadOrStore())
192 bool IsPseudoIdempotent =
true;
193 for (
unsigned i = 1; i < II->getNumOperands(); i++) {
195 if (II->getOperand(i).isImm()) {
199 if (II->getOperand(i).isReg()) {
201 if (
llvm::find(PhysRegDefs, II->getOperand(i).getReg()) ==
207 IsPseudoIdempotent =
false;
211 if (IsPseudoIdempotent) {
212 PseudoIdempotentInstructions.push_back(II);
219 unsigned Distance = ~0U;
225 const unsigned DefLoc = getInstrIdx(*Def);
226 const unsigned UseLoc = getInstrIdx(*UseInst);
227 const unsigned Delta = (UseLoc - DefLoc);
231 if (DefLoc >= UseLoc)
234 if (Delta < Distance) {
236 UseToBringDefCloserTo = UseInst;
237 MultiUserLookup[UseToBringDefCloserToCount++] = UseToBringDefCloserTo;
241 const auto BBE = MBB->instr_end();
245 for (
auto BBI = MBB->instr_begin(); BBI != BBE; ++BBI) {
247 if (DefI != BBE && UseI != BBE)
255 if (&*BBI == UseToBringDefCloserTo) {
261 if (DefI == BBE || UseI == BBE)
265 dbgs() <<
"Splicing ";
267 dbgs() <<
" right before: ";
271 MultiUsers[UseToBringDefCloserTo].push_back(Def);
273 MBB->splice(UseI, MBB, DefI);
277 for (
const auto &
E : MultiUserLookup) {
283 if (UseI == MBB->instr_end())
287 dbgs() <<
"Rescheduling Multi-Use Instructions Lexographically.";);
289 MultiUsers[
E.second], MBB,
293 PseudoIdempotentInstCount = PseudoIdempotentInstructions.size();
295 dbgs() <<
"Rescheduling Idempotent Instructions Lexographically.";);
297 PseudoIdempotentInstructions, MBB,
304 bool Changed =
false;
307 std::vector<MachineInstr *>
Copies;
310 Copies.push_back(&
MI);
315 if (!
MI->getOperand(0).isReg())
317 if (!
MI->getOperand(1).isReg())
320 const Register Dst =
MI->getOperand(0).getReg();
321 const Register Src =
MI->getOperand(1).getReg();
337 std::vector<MachineOperand *> Uses;
339 Uses.push_back(&*UI);
340 for (
auto *MO : Uses)
344 MI->eraseFromParent();
351 bool Changed =
false;
353 for (
auto &
MI : *MBB) {
354 for (
auto &MO :
MI.operands()) {
357 if (!MO.isDef() && MO.isKill()) {
362 if (MO.isDef() && MO.isDead()) {
375 dbgs() <<
"\n\n NEW BASIC BLOCK: " << MBB->
getName() <<
" \n\n";
376 dbgs() <<
"\n\n================================================\n\n";
379 bool Changed =
false;
389 unsigned IdempotentInstCount = 0;
393 Changed |= Renamer.
renameVRegs(MBB, BasicBlockNum);
402 dbgs() <<
"\n\n================================================\n\n");
408 static unsigned functionNum = 0;
418 std::vector<MachineBasicBlock *> RPOList =
GetRPOList(MF);
421 dbgs() <<
"\n\n NEW MACHINE FUNCTION: " << MF.
getName() <<
" \n\n";
422 dbgs() <<
"\n\n================================================\n\n";
423 dbgs() <<
"Total Basic Blocks: " << RPOList.size() <<
"\n";
426 <<
"\n\n================================================\n\n";);
429 bool Changed =
false;
432 for (
auto MBB : RPOList)
char & MIRCanonicalizerID
MIRCanonicalizer - This pass canonicalizes MIR by renaming vregs according to the semantics of the in...
This class represents lattice values for constants.
iterator_range< use_nodbg_iterator > use_nodbg_operands(unsigned Reg) const
static bool rescheduleLexographically(std::vector< MachineInstr *> instructions, MachineBasicBlock *MBB, std::function< MachineBasicBlock::iterator()> getPos)
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
static use_iterator use_end()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
mir Rename Register Operands Canonically
mir Rename Register Operands
static bool doDefKillClear(MachineBasicBlock *MBB)
static cl::opt< unsigned > CanonicalizeFunctionNumber("canon-nth-function", cl::Hidden, cl::init(~0u), cl::value_desc("N"), cl::desc("Function number to canonicalize."))
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
initializer< Ty > init(const Ty &Val)
unsigned const MachineRegisterInfo * MRI
StringRef getName() const
Return the name of the corresponding LLVM basic block, or an empty string.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
mir Rename Register Operands static false std::vector< MachineBasicBlock * > GetRPOList(MachineFunction &MF)
static bool runOnBasicBlock(MachineBasicBlock *MBB, unsigned BasicBlockNum, VRegRenamer &Renamer)
Represent the analysis usage information of a pass.
static bool rescheduleCanonically(unsigned &PseudoIdempotentInstCount, MachineBasicBlock *MBB)
auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range))
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly...
auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
void sort(IteratorTy Start, IteratorTy End)
MachineOperand class - Representation of each machine instruction operand.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
static bool propagateLocalCopies(MachineBasicBlock *MBB)
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
INITIALIZE_PASS_BEGIN(MIRCanonicalizer, "mir-canonicalizer", "Rename Register Operands Canonically", false, false) INITIALIZE_PASS_END(MIRCanonicalizer
bool renameVRegs(MachineBasicBlock *MBB, unsigned BBNum)
Same as the above, but sets a BBNum depending on BB traversal that will be used as prefix for the vre...
VRegRenamer - This class is used for renaming vregs in a machine basic block according to semantics o...
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
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 '...
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
use_iterator use_begin(unsigned RegNo) const
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const TargetRegisterClass * getRegClassOrNull(unsigned Reg) const
Return the register class of Reg, or null if Reg has not been assigned a register class yet...
A raw_ostream that writes to an std::string.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
inst_range instructions(Function *F)
Register getReg() const
getReg - Returns the register number.
Wrapper class representing virtual and physical registers.