2011-09-26 00:46:08 +08:00
|
|
|
//===-- ExpandPostRAPseudos.cpp - Pseudo instruction expansion pass -------===//
|
2007-07-26 16:18:32 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2007-07-26 16:18:32 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-09-25 07:44:12 +08:00
|
|
|
//
|
2011-09-26 00:46:08 +08:00
|
|
|
// This file defines a pass that expands COPY and SUBREG_TO_REG pseudo
|
|
|
|
// instructions after register allocation.
|
2008-09-25 07:44:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-07-26 16:18:32 +08:00
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2009-08-04 04:08:18 +08:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2007-12-31 12:13:23 +08:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2017-11-08 09:01:31 +08:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 09:07:10 +08:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
2007-07-26 16:18:32 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-25 08:23:56 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-08-05 05:25:23 +08:00
|
|
|
|
2007-07-26 16:18:32 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:02:50 +08:00
|
|
|
#define DEBUG_TYPE "postrapseudos"
|
|
|
|
|
2007-07-26 16:18:32 +08:00
|
|
|
namespace {
|
2011-09-26 00:46:08 +08:00
|
|
|
struct ExpandPostRA : public MachineFunctionPass {
|
|
|
|
private:
|
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const TargetInstrInfo *TII;
|
2009-10-25 15:49:57 +08:00
|
|
|
|
2011-09-26 00:46:08 +08:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
ExpandPostRA() : MachineFunctionPass(ID) {}
|
2011-02-26 06:53:20 +08:00
|
|
|
|
2014-03-07 17:26:03 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2011-09-26 00:46:08 +08:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addPreservedID(MachineLoopInfoID);
|
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2008-09-23 04:58:04 +08:00
|
|
|
|
2011-09-26 00:46:08 +08:00
|
|
|
/// runOnMachineFunction - pass entry point
|
2014-03-07 17:26:03 +08:00
|
|
|
bool runOnMachineFunction(MachineFunction&) override;
|
2009-10-25 15:49:57 +08:00
|
|
|
|
2011-09-26 00:46:08 +08:00
|
|
|
private:
|
|
|
|
bool LowerSubregToReg(MachineInstr *MI);
|
|
|
|
bool LowerCopy(MachineInstr *MI);
|
2008-12-19 06:14:08 +08:00
|
|
|
|
2016-07-16 06:31:14 +08:00
|
|
|
void TransferImplicitOperands(MachineInstr *MI);
|
2011-09-26 00:46:08 +08:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-07-26 16:18:32 +08:00
|
|
|
|
2011-09-26 00:46:08 +08:00
|
|
|
char ExpandPostRA::ID = 0;
|
2012-02-09 05:23:13 +08:00
|
|
|
char &llvm::ExpandPostRAPseudosID = ExpandPostRA::ID;
|
2007-07-26 16:18:32 +08:00
|
|
|
|
2017-05-26 05:26:32 +08:00
|
|
|
INITIALIZE_PASS(ExpandPostRA, DEBUG_TYPE,
|
2012-02-09 05:23:13 +08:00
|
|
|
"Post-RA pseudo instruction expansion pass", false, false)
|
2007-07-26 16:18:32 +08:00
|
|
|
|
2016-07-16 06:31:14 +08:00
|
|
|
/// TransferImplicitOperands - MI is a pseudo-instruction, and the lowered
|
|
|
|
/// replacement instructions immediately precede it. Copy any implicit
|
2010-06-30 02:42:49 +08:00
|
|
|
/// operands from MI to the replacement instruction.
|
2016-07-16 06:31:14 +08:00
|
|
|
void ExpandPostRA::TransferImplicitOperands(MachineInstr *MI) {
|
2010-06-30 02:42:49 +08:00
|
|
|
MachineBasicBlock::iterator CopyMI = MI;
|
|
|
|
--CopyMI;
|
|
|
|
|
2016-07-16 06:31:14 +08:00
|
|
|
for (const MachineOperand &MO : MI->implicit_operands())
|
|
|
|
if (MO.isReg())
|
|
|
|
CopyMI->addOperand(MO);
|
2010-06-30 02:42:49 +08:00
|
|
|
}
|
|
|
|
|
2011-09-26 00:46:08 +08:00
|
|
|
bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
|
2007-08-07 00:33:56 +08:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2008-10-03 23:45:36 +08:00
|
|
|
assert((MI->getOperand(0).isReg() && MI->getOperand(0).isDef()) &&
|
|
|
|
MI->getOperand(1).isImm() &&
|
|
|
|
(MI->getOperand(2).isReg() && MI->getOperand(2).isUse()) &&
|
|
|
|
MI->getOperand(3).isImm() && "Invalid subreg_to_reg");
|
2010-06-23 06:11:07 +08:00
|
|
|
|
2008-03-16 11:12:01 +08:00
|
|
|
unsigned DstReg = MI->getOperand(0).getReg();
|
|
|
|
unsigned InsReg = MI->getOperand(2).getReg();
|
2010-06-23 06:11:07 +08:00
|
|
|
assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
|
2009-03-23 15:19:58 +08:00
|
|
|
unsigned SubIdx = MI->getOperand(3).getImm();
|
2007-08-07 00:33:56 +08:00
|
|
|
|
2008-03-16 11:12:01 +08:00
|
|
|
assert(SubIdx != 0 && "Invalid index for insert_subreg");
|
2009-10-25 15:49:57 +08:00
|
|
|
unsigned DstSubReg = TRI->getSubReg(DstReg, SubIdx);
|
2009-03-23 15:19:58 +08:00
|
|
|
|
2019-08-02 07:27:28 +08:00
|
|
|
assert(Register::isPhysicalRegister(DstReg) &&
|
2007-08-07 00:33:56 +08:00
|
|
|
"Insert destination must be in a physical register");
|
2019-08-02 07:27:28 +08:00
|
|
|
assert(Register::isPhysicalRegister(InsReg) &&
|
2007-08-07 00:33:56 +08:00
|
|
|
"Inserted value must be in a physical register");
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
|
2008-03-16 11:12:01 +08:00
|
|
|
|
2013-02-22 06:16:43 +08:00
|
|
|
if (MI->allDefsAreDead()) {
|
|
|
|
MI->setDesc(TII->get(TargetOpcode::KILL));
|
2018-10-09 08:07:34 +08:00
|
|
|
MI->RemoveOperand(3); // SubIdx
|
|
|
|
MI->RemoveOperand(1); // Imm
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);
|
2013-02-22 06:16:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-23 06:11:07 +08:00
|
|
|
if (DstSubReg == InsReg) {
|
2013-10-11 23:40:14 +08:00
|
|
|
// No need to insert an identity copy instruction.
|
2009-03-23 15:19:58 +08:00
|
|
|
// Watch out for case like this:
|
2017-12-07 18:40:31 +08:00
|
|
|
// %rax = SUBREG_TO_REG 0, killed %eax, 3
|
2017-11-29 01:15:09 +08:00
|
|
|
// We must leave %rax live.
|
2010-06-23 06:11:07 +08:00
|
|
|
if (DstReg != InsReg) {
|
|
|
|
MI->setDesc(TII->get(TargetOpcode::KILL));
|
|
|
|
MI->RemoveOperand(3); // SubIdx
|
|
|
|
MI->RemoveOperand(1); // Imm
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "subreg: replace by: " << *MI);
|
2010-06-23 06:11:07 +08:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "subreg: eliminated!");
|
2008-08-07 10:54:50 +08:00
|
|
|
} else {
|
2010-07-08 13:01:41 +08:00
|
|
|
TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
|
|
|
|
MI->getOperand(2).isKill());
|
2013-02-22 01:01:59 +08:00
|
|
|
|
2012-07-28 04:19:49 +08:00
|
|
|
// Implicitly define DstReg for subsequent uses.
|
|
|
|
MachineBasicBlock::iterator CopyMI = MI;
|
|
|
|
--CopyMI;
|
|
|
|
CopyMI->addRegisterDefined(DstReg);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);
|
2008-08-07 10:54:50 +08:00
|
|
|
}
|
2007-08-07 00:33:56 +08:00
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << '\n');
|
2008-07-18 07:49:46 +08:00
|
|
|
MBB->erase(MI);
|
2009-10-24 08:27:00 +08:00
|
|
|
return true;
|
2008-03-16 11:12:01 +08:00
|
|
|
}
|
2007-08-07 00:33:56 +08:00
|
|
|
|
2011-09-26 00:46:08 +08:00
|
|
|
bool ExpandPostRA::LowerCopy(MachineInstr *MI) {
|
2013-02-22 06:16:43 +08:00
|
|
|
|
|
|
|
if (MI->allDefsAreDead()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "dead copy: " << *MI);
|
2013-02-22 06:16:43 +08:00
|
|
|
MI->setDesc(TII->get(TargetOpcode::KILL));
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "replaced by: " << *MI);
|
2013-02-22 06:16:43 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-03 06:29:50 +08:00
|
|
|
MachineOperand &DstMO = MI->getOperand(0);
|
|
|
|
MachineOperand &SrcMO = MI->getOperand(1);
|
|
|
|
|
2017-05-12 14:32:03 +08:00
|
|
|
bool IdentityCopy = (SrcMO.getReg() == DstMO.getReg());
|
|
|
|
if (IdentityCopy || SrcMO.isUndef()) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << (IdentityCopy ? "identity copy: " : "undef copy: ")
|
|
|
|
<< *MI);
|
2010-07-03 06:29:50 +08:00
|
|
|
// No need to insert an identity copy instruction, but replace with a KILL
|
|
|
|
// if liveness is changed.
|
2013-02-22 06:16:43 +08:00
|
|
|
if (SrcMO.isUndef() || MI->getNumOperands() > 2) {
|
2010-07-03 06:29:50 +08:00
|
|
|
// We must make sure the super-register gets killed. Replace the
|
|
|
|
// instruction with KILL.
|
|
|
|
MI->setDesc(TII->get(TargetOpcode::KILL));
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "replaced by: " << *MI);
|
2010-07-03 06:29:50 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Vanilla identity copy.
|
|
|
|
MI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "real copy: " << *MI);
|
2010-07-08 13:01:41 +08:00
|
|
|
TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(),
|
|
|
|
DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill());
|
2010-07-03 06:29:50 +08:00
|
|
|
|
|
|
|
if (MI->getNumOperands() > 2)
|
2016-07-16 06:31:14 +08:00
|
|
|
TransferImplicitOperands(MI);
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG({
|
2010-07-03 06:29:50 +08:00
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
|
|
|
dbgs() << "replaced by: " << *(--dMI);
|
|
|
|
});
|
|
|
|
MI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-26 16:18:32 +08:00
|
|
|
/// runOnMachineFunction - Reduce subregister inserts and extracts to register
|
|
|
|
/// copies.
|
|
|
|
///
|
2011-09-26 00:46:08 +08:00
|
|
|
bool ExpandPostRA::runOnMachineFunction(MachineFunction &MF) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "Machine Function\n"
|
|
|
|
<< "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
|
|
|
|
<< "********** Function: " << MF.getName() << '\n');
|
2014-08-05 10:39:49 +08:00
|
|
|
TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
TII = MF.getSubtarget().getInstrInfo();
|
2007-07-26 16:18:32 +08:00
|
|
|
|
2009-08-23 04:23:49 +08:00
|
|
|
bool MadeChange = false;
|
2007-07-26 16:18:32 +08:00
|
|
|
|
|
|
|
for (MachineFunction::iterator mbbi = MF.begin(), mbbe = MF.end();
|
|
|
|
mbbi != mbbe; ++mbbi) {
|
|
|
|
for (MachineBasicBlock::iterator mi = mbbi->begin(), me = mbbi->end();
|
2007-08-07 00:33:56 +08:00
|
|
|
mi != me;) {
|
2016-06-30 08:01:54 +08:00
|
|
|
MachineInstr &MI = *mi;
|
2011-09-26 03:21:35 +08:00
|
|
|
// Advance iterator here because MI may be erased.
|
|
|
|
++mi;
|
2011-10-11 04:34:28 +08:00
|
|
|
|
|
|
|
// Only expand pseudos.
|
2016-06-30 08:01:54 +08:00
|
|
|
if (!MI.isPseudo())
|
2011-10-11 04:34:28 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Give targets a chance to expand even standard pseudos.
|
|
|
|
if (TII->expandPostRAPseudo(MI)) {
|
|
|
|
MadeChange = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand standard pseudos.
|
2016-06-30 08:01:54 +08:00
|
|
|
switch (MI.getOpcode()) {
|
2011-09-26 03:21:35 +08:00
|
|
|
case TargetOpcode::SUBREG_TO_REG:
|
2016-06-30 08:01:54 +08:00
|
|
|
MadeChange |= LowerSubregToReg(&MI);
|
2011-09-26 03:21:35 +08:00
|
|
|
break;
|
|
|
|
case TargetOpcode::COPY:
|
2016-06-30 08:01:54 +08:00
|
|
|
MadeChange |= LowerCopy(&MI);
|
2011-09-26 03:21:35 +08:00
|
|
|
break;
|
|
|
|
case TargetOpcode::DBG_VALUE:
|
|
|
|
continue;
|
|
|
|
case TargetOpcode::INSERT_SUBREG:
|
|
|
|
case TargetOpcode::EXTRACT_SUBREG:
|
|
|
|
llvm_unreachable("Sub-register pseudos should have been eliminated.");
|
2007-07-26 16:18:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|