40 #define DEBUG_TYPE "aarch64-copyelim"
42 STATISTIC(NumCopiesRemoved,
"Number of copies removed.");
60 RegImm(
MCPhysReg Reg, int32_t Imm) : Reg(Reg), Imm(Imm) {}
73 return "AArch64 Redundant Copy Elimination";
80 "AArch64 redundant copy elimination pass",
false,
false)
87 ClobberedRegs.setBitsNotInMask(MO.getRegMask());
93 unsigned Reg = MO.getReg();
100 ClobberedRegs.set(*AI);
110 AArch64RedundantCopyElimination::knownRegValInBlock(
117 if (((Opc == AArch64::CBZW || Opc == AArch64::CBZX) &&
119 ((Opc == AArch64::CBNZW || Opc == AArch64::CBNZX) &&
126 if (Opc != AArch64::Bcc)
142 "Conditional branch not in predecessor block!");
143 if (CondBr == PredMBB->
begin())
158 switch (PredI.getOpcode()) {
163 case AArch64::ADDSWri:
164 case AArch64::ADDSXri:
168 case AArch64::SUBSWri:
169 case AArch64::SUBSXri: {
170 MCPhysReg SrcReg = PredI.getOperand(1).getReg();
173 if (!PredI.getOperand(2).isImm())
182 int32_t KnownImm = PredI.getOperand(2).getImm();
183 int32_t Shift = PredI.getOperand(3).getImm();
186 KnownImm = -KnownImm;
188 return RegImm(SrcReg, KnownImm);
193 if (PredI.definesRegister(AArch64::NZCV))
211 if (CondBr == PredMBB->
end())
222 bool SeenFirstUse =
false;
231 if (KnownRegImm ==
None)
241 for (
auto PredI = Itr;; --PredI) {
242 if (FirstUse == PredI)
245 if (PredI->isCopy()) {
246 MCPhysReg CopyDstReg = PredI->getOperand(0).getReg();
247 MCPhysReg CopySrcReg = PredI->getOperand(1).getReg();
248 for (
auto &KnownReg : KnownRegs) {
253 if (CopySrcReg == KnownReg.Reg && !
ClobberedRegs[CopyDstReg]) {
254 KnownRegs.push_back(RegImm(CopyDstReg, KnownReg.Imm));
261 if (CopyDstReg == KnownReg.Reg && !
ClobberedRegs[CopySrcReg]) {
262 KnownRegs.push_back(RegImm(CopySrcReg, KnownReg.Imm));
271 if (PredI == PredMBB->
begin())
276 if (
all_of(KnownRegs, [&](RegImm KnownReg) {
283 }
while (Itr != PredMBB->
begin() && Itr->isTerminator());
286 if (KnownRegs.empty())
289 bool Changed =
false;
297 bool RemovedMI =
false;
298 bool IsCopy = MI->
isCopy();
300 if (IsCopy || IsMoveImm) {
304 if (!
MRI->isReserved(DefReg) &&
305 ((IsCopy && (SrcReg == AArch64::XZR || SrcReg == AArch64::WZR)) ||
307 for (RegImm &KnownReg : KnownRegs) {
308 if (KnownReg.Reg != DefReg &&
309 !TRI->isSuperRegister(DefReg, KnownReg.Reg))
313 if (IsCopy && KnownReg.Imm != 0)
319 if (KnownReg.Imm != SrcImm)
326 return !O.isDead() && O.isReg() && O.isDef() &&
327 O.getReg() != CmpReg;
333 DEBUG(
dbgs() <<
"Remove redundant Copy : " << *MI);
335 DEBUG(
dbgs() <<
"Remove redundant Move : " << *MI);
341 UsedKnownRegs.
insert(KnownReg.Reg);
353 for (
unsigned RI = 0; RI < KnownRegs.size();)
355 std::swap(KnownRegs[RI], KnownRegs[KnownRegs.size() - 1]);
356 KnownRegs.pop_back();
364 if (KnownRegs.empty())
379 DEBUG(
dbgs() <<
"Clearing kill flags.\n\tFirstUse: " << *FirstUse
380 <<
"\tLastChange: " << *LastChange);
389 bool AArch64RedundantCopyElimination::runOnMachineFunction(
400 bool Changed =
false;
402 Changed |= optimizeCopy(&MBB);
407 return new AArch64RedundantCopyElimination();
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
unsigned succ_size() const
void push_back(const T &Elt)
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
STATISTIC(NumFunctions,"Total number of functions")
MachineBasicBlock * getMBB() const
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly...
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
void initializeAArch64RedundantCopyEliminationPass(PassRegistry &)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Reg
All possible values of the reg field in the ModR/M byte.
bool insert(const value_type &X)
Insert a new element into the SetVector.
iterator getLastNonDebugInstr()
Returns an iterator to the last non-debug instruction in the basic block, or end().
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
const MachineBasicBlock * getParent() const
INITIALIZE_PASS(AArch64RedundantCopyElimination,"aarch64-copyelim","AArch64 redundant copy elimination pass", false, false) static void trackRegDefs(const MachineInstr &MI
Remember what registers the specified instruction modifies.
void addLiveIn(MCPhysReg PhysReg, LaneBitmask LaneMask=LaneBitmask::getAll())
Adds the specified register as a live in.
unsigned const MachineRegisterInfo * MRI
const MachineOperand & getOperand(unsigned i) const
MCRegAliasIterator enumerates all registers aliasing Reg.
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
FunctionPass class - This class is used to implement most global optimizations.
FunctionPass * createAArch64RedundantCopyEliminationPass()
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
pred_iterator pred_begin()
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
BitVector & ClobberedRegs
A SetVector that performs no allocations if smaller than a certain size.
MachineOperand class - Representation of each machine instruction operand.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
iterator_range< mop_iterator > implicit_operands()
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
reverse_self_iterator getReverseIterator()
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
bool isLiveIn(MCPhysReg Reg, LaneBitmask LaneMask=LaneBitmask::getAll()) const
Return true if the specified register is in the live in set.
unsigned getReg() const
getReg - Returns the register number.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
virtual const TargetRegisterInfo * getRegisterInfo() const
getRegisterInfo - If register information is available, return it.
StringRef - Represent a constant reference to a string, i.e.
bool isMoveImmediate(QueryType Type=IgnoreBundle) const
Return true if this instruction is a move immediate (including conditional moves) instruction...
unsigned pred_size() const
bool modifiesRegister(unsigned Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr modifies (fully define or partially define) the specified register...
Properties which a MachineFunction may have at a given point in time.