2011-09-25 16:46:08 +00:00
|
|
|
//===-- ExpandPostRAPseudos.cpp - Pseudo instruction expansion pass -------===//
|
2007-07-26 08:18:32 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00: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 08:18:32 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2008-09-24 23:44:12 +00:00
|
|
|
//
|
2011-09-25 16:46:08 +00:00
|
|
|
// This file defines a pass that expands COPY and SUBREG_TO_REG pseudo
|
|
|
|
// instructions after register allocation.
|
2008-09-24 23:44:12 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-07-26 08:18:32 +00:00
|
|
|
|
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2009-08-03 20:08:18 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstrBuilder.h"
|
2007-12-31 04:13:23 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
2017-06-06 11:49:48 +00:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2017-11-08 01:01:31 +00:00
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
2017-11-17 01:07:10 +00:00
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-13 13:15:01 -08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2007-07-26 08:18:32 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-25 00:23:56 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-08-04 21:25:23 +00:00
|
|
|
|
2007-07-26 08:18:32 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:02:50 +00:00
|
|
|
#define DEBUG_TYPE "postrapseudos"
|
|
|
|
|
2007-07-26 08:18:32 +00:00
|
|
|
namespace {
|
2011-09-25 16:46:08 +00:00
|
|
|
struct ExpandPostRA : public MachineFunctionPass {
|
|
|
|
private:
|
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
const TargetInstrInfo *TII;
|
2009-10-25 07:49:57 +00:00
|
|
|
|
2011-09-25 16:46:08 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
ExpandPostRA() : MachineFunctionPass(ID) {}
|
2011-02-25 22:53:20 +00:00
|
|
|
|
2014-03-07 09:26:03 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2011-09-25 16:46:08 +00:00
|
|
|
AU.setPreservesCFG();
|
|
|
|
AU.addPreservedID(MachineLoopInfoID);
|
|
|
|
AU.addPreservedID(MachineDominatorsID);
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
2008-09-22 20:58:04 +00:00
|
|
|
|
2011-09-25 16:46:08 +00:00
|
|
|
/// runOnMachineFunction - pass entry point
|
2014-03-07 09:26:03 +00:00
|
|
|
bool runOnMachineFunction(MachineFunction&) override;
|
2009-10-25 07:49:57 +00:00
|
|
|
|
2011-09-25 16:46:08 +00:00
|
|
|
private:
|
|
|
|
bool LowerSubregToReg(MachineInstr *MI);
|
|
|
|
bool LowerCopy(MachineInstr *MI);
|
2008-12-18 22:14:08 +00:00
|
|
|
|
2016-07-15 22:31:14 +00:00
|
|
|
void TransferImplicitOperands(MachineInstr *MI);
|
2011-09-25 16:46:08 +00:00
|
|
|
};
|
|
|
|
} // end anonymous namespace
|
2007-07-26 08:18:32 +00:00
|
|
|
|
2011-09-25 16:46:08 +00:00
|
|
|
char ExpandPostRA::ID = 0;
|
2012-02-08 21:23:13 +00:00
|
|
|
char &llvm::ExpandPostRAPseudosID = ExpandPostRA::ID;
|
2007-07-26 08:18:32 +00:00
|
|
|
|
2017-05-25 21:26:32 +00:00
|
|
|
INITIALIZE_PASS(ExpandPostRA, DEBUG_TYPE,
|
2012-02-08 21:23:13 +00:00
|
|
|
"Post-RA pseudo instruction expansion pass", false, false)
|
2007-07-26 08:18:32 +00:00
|
|
|
|
2016-07-15 22:31:14 +00:00
|
|
|
/// TransferImplicitOperands - MI is a pseudo-instruction, and the lowered
|
|
|
|
/// replacement instructions immediately precede it. Copy any implicit
|
2010-06-29 18:42:49 +00:00
|
|
|
/// operands from MI to the replacement instruction.
|
2016-07-15 22:31:14 +00:00
|
|
|
void ExpandPostRA::TransferImplicitOperands(MachineInstr *MI) {
|
2010-06-29 18:42:49 +00:00
|
|
|
MachineBasicBlock::iterator CopyMI = MI;
|
|
|
|
--CopyMI;
|
|
|
|
|
2016-07-15 22:31:14 +00:00
|
|
|
for (const MachineOperand &MO : MI->implicit_operands())
|
|
|
|
if (MO.isReg())
|
|
|
|
CopyMI->addOperand(MO);
|
2010-06-29 18:42:49 +00:00
|
|
|
}
|
|
|
|
|
2011-09-25 16:46:08 +00:00
|
|
|
bool ExpandPostRA::LowerSubregToReg(MachineInstr *MI) {
|
2007-08-06 16:33:56 +00:00
|
|
|
MachineBasicBlock *MBB = MI->getParent();
|
2008-10-03 15:45:36 +00: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-22 22:11:07 +00:00
|
|
|
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-15 19:22:08 +00:00
|
|
|
Register DstReg = MI->getOperand(0).getReg();
|
|
|
|
Register InsReg = MI->getOperand(2).getReg();
|
2010-06-22 22:11:07 +00:00
|
|
|
assert(!MI->getOperand(2).getSubReg() && "SubIdx on physreg?");
|
2009-03-23 07:19:58 +00:00
|
|
|
unsigned SubIdx = MI->getOperand(3).getImm();
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2008-03-16 03:12:01 +00:00
|
|
|
assert(SubIdx != 0 && "Invalid index for insert_subreg");
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-15 19:22:08 +00:00
|
|
|
Register DstSubReg = TRI->getSubReg(DstReg, SubIdx);
|
2009-03-23 07:19:58 +00:00
|
|
|
|
2019-08-01 23:27:28 +00:00
|
|
|
assert(Register::isPhysicalRegister(DstReg) &&
|
2007-08-06 16:33:56 +00:00
|
|
|
"Insert destination must be in a physical register");
|
2019-08-01 23:27:28 +00:00
|
|
|
assert(Register::isPhysicalRegister(InsReg) &&
|
2007-08-06 16:33:56 +00:00
|
|
|
"Inserted value must be in a physical register");
|
|
|
|
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "subreg: CONVERTING: " << *MI);
|
2008-03-16 03:12:01 +00:00
|
|
|
|
2013-02-21 22:16:43 +00:00
|
|
|
if (MI->allDefsAreDead()) {
|
|
|
|
MI->setDesc(TII->get(TargetOpcode::KILL));
|
2018-10-09 00:07:34 +00:00
|
|
|
MI->RemoveOperand(3); // SubIdx
|
|
|
|
MI->RemoveOperand(1); // Imm
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "subreg: replaced by: " << *MI);
|
2013-02-21 22:16:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-06-22 22:11:07 +00:00
|
|
|
if (DstSubReg == InsReg) {
|
2013-10-11 15:40:14 +00:00
|
|
|
// No need to insert an identity copy instruction.
|
2009-03-23 07:19:58 +00:00
|
|
|
// Watch out for case like this:
|
2017-12-07 10:40:31 +00:00
|
|
|
// %rax = SUBREG_TO_REG 0, killed %eax, 3
|
2017-11-28 17:15:09 +00:00
|
|
|
// We must leave %rax live.
|
2010-06-22 22:11:07 +00:00
|
|
|
if (DstReg != InsReg) {
|
|
|
|
MI->setDesc(TII->get(TargetOpcode::KILL));
|
|
|
|
MI->RemoveOperand(3); // SubIdx
|
|
|
|
MI->RemoveOperand(1); // Imm
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "subreg: replace by: " << *MI);
|
2010-06-22 22:11:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "subreg: eliminated!");
|
2008-08-07 02:54:50 +00:00
|
|
|
} else {
|
2010-07-08 05:01:41 +00:00
|
|
|
TII->copyPhysReg(*MBB, MI, MI->getDebugLoc(), DstSubReg, InsReg,
|
|
|
|
MI->getOperand(2).isKill());
|
2013-02-21 17:01:59 +00:00
|
|
|
|
2012-07-27 20:19:49 +00:00
|
|
|
// Implicitly define DstReg for subsequent uses.
|
|
|
|
MachineBasicBlock::iterator CopyMI = MI;
|
|
|
|
--CopyMI;
|
|
|
|
CopyMI->addRegisterDefined(DstReg);
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "subreg: " << *CopyMI);
|
2008-08-07 02:54:50 +00:00
|
|
|
}
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << '\n');
|
2008-07-17 23:49:46 +00:00
|
|
|
MBB->erase(MI);
|
2009-10-24 00:27:00 +00:00
|
|
|
return true;
|
2008-03-16 03:12:01 +00:00
|
|
|
}
|
2007-08-06 16:33:56 +00:00
|
|
|
|
2011-09-25 16:46:08 +00:00
|
|
|
bool ExpandPostRA::LowerCopy(MachineInstr *MI) {
|
2013-02-21 22:16:43 +00:00
|
|
|
|
|
|
|
if (MI->allDefsAreDead()) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "dead copy: " << *MI);
|
2013-02-21 22:16:43 +00:00
|
|
|
MI->setDesc(TII->get(TargetOpcode::KILL));
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "replaced by: " << *MI);
|
2013-02-21 22:16:43 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2010-07-02 22:29:50 +00:00
|
|
|
MachineOperand &DstMO = MI->getOperand(0);
|
|
|
|
MachineOperand &SrcMO = MI->getOperand(1);
|
|
|
|
|
2017-05-12 06:32:03 +00:00
|
|
|
bool IdentityCopy = (SrcMO.getReg() == DstMO.getReg());
|
|
|
|
if (IdentityCopy || SrcMO.isUndef()) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << (IdentityCopy ? "identity copy: " : "undef copy: ")
|
|
|
|
<< *MI);
|
2010-07-02 22:29:50 +00:00
|
|
|
// No need to insert an identity copy instruction, but replace with a KILL
|
|
|
|
// if liveness is changed.
|
2013-02-21 22:16:43 +00:00
|
|
|
if (SrcMO.isUndef() || MI->getNumOperands() > 2) {
|
2010-07-02 22:29:50 +00:00
|
|
|
// We must make sure the super-register gets killed. Replace the
|
|
|
|
// instruction with KILL.
|
|
|
|
MI->setDesc(TII->get(TargetOpcode::KILL));
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "replaced by: " << *MI);
|
2010-07-02 22:29:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Vanilla identity copy.
|
|
|
|
MI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "real copy: " << *MI);
|
2010-07-08 05:01:41 +00:00
|
|
|
TII->copyPhysReg(*MI->getParent(), MI, MI->getDebugLoc(),
|
|
|
|
DstMO.getReg(), SrcMO.getReg(), SrcMO.isKill());
|
2010-07-02 22:29:50 +00:00
|
|
|
|
|
|
|
if (MI->getNumOperands() > 2)
|
2016-07-15 22:31:14 +00:00
|
|
|
TransferImplicitOperands(MI);
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG({
|
2010-07-02 22:29:50 +00:00
|
|
|
MachineBasicBlock::iterator dMI = MI;
|
|
|
|
dbgs() << "replaced by: " << *(--dMI);
|
|
|
|
});
|
|
|
|
MI->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-26 08:18:32 +00:00
|
|
|
/// runOnMachineFunction - Reduce subregister inserts and extracts to register
|
|
|
|
/// copies.
|
|
|
|
///
|
2011-09-25 16:46:08 +00:00
|
|
|
bool ExpandPostRA::runOnMachineFunction(MachineFunction &MF) {
|
2018-05-14 12:53:11 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Machine Function\n"
|
|
|
|
<< "********** EXPANDING POST-RA PSEUDO INSTRS **********\n"
|
|
|
|
<< "********** Function: " << MF.getName() << '\n');
|
2014-08-05 02:39:49 +00:00
|
|
|
TRI = MF.getSubtarget().getRegisterInfo();
|
|
|
|
TII = MF.getSubtarget().getInstrInfo();
|
2007-07-26 08:18:32 +00:00
|
|
|
|
2009-08-22 20:23:49 +00:00
|
|
|
bool MadeChange = false;
|
2007-07-26 08:18:32 +00:00
|
|
|
|
2021-02-12 23:44:33 -08:00
|
|
|
for (MachineBasicBlock &MBB : MF) {
|
2021-11-15 21:28:46 -08:00
|
|
|
for (MachineInstr &MI : llvm::make_early_inc_range(MBB)) {
|
2011-10-10 20:34:28 +00:00
|
|
|
// Only expand pseudos.
|
2016-06-30 00:01:54 +00:00
|
|
|
if (!MI.isPseudo())
|
2011-10-10 20:34:28 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// Give targets a chance to expand even standard pseudos.
|
|
|
|
if (TII->expandPostRAPseudo(MI)) {
|
|
|
|
MadeChange = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expand standard pseudos.
|
2016-06-30 00:01:54 +00:00
|
|
|
switch (MI.getOpcode()) {
|
2011-09-25 19:21:35 +00:00
|
|
|
case TargetOpcode::SUBREG_TO_REG:
|
2016-06-30 00:01:54 +00:00
|
|
|
MadeChange |= LowerSubregToReg(&MI);
|
2011-09-25 19:21:35 +00:00
|
|
|
break;
|
|
|
|
case TargetOpcode::COPY:
|
2016-06-30 00:01:54 +00:00
|
|
|
MadeChange |= LowerCopy(&MI);
|
2011-09-25 19:21:35 +00: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 08:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return MadeChange;
|
|
|
|
}
|