blob: e5822b114324a5338f2891916f69786952e4fd1d [file] [log] [blame]
Jun Bum Limb389d9b2016-02-16 20:02:39 +00001//=- AArch64RedundantCopyElimination.cpp - Remove useless copy for AArch64 -=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
Chad Rosier9b2b4c92017-07-23 16:38:08 +00008// This pass removes unnecessary copies/moves in BBs based on a dominating
9// condition.
10//
11// We handle three cases:
12// 1. For BBs that are targets of CBZ/CBNZ instructions, we know the value of
13// the CBZ/CBNZ source register is zero on the taken/not-taken path. For
14// instance, the copy instruction in the code below can be removed because
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000015// the CBZW jumps to %bb.2 when w0 is zero.
Chad Rosier9b2b4c92017-07-23 16:38:08 +000016//
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000017// %bb.1:
Chad Rosier9b2b4c92017-07-23 16:38:08 +000018// cbz w0, .LBB0_2
19// .LBB0_2:
20// mov w0, wzr ; <-- redundant
21//
22// 2. If the flag setting instruction defines a register other than WZR/XZR, we
23// can remove a zero copy in some cases.
24//
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000025// %bb.0:
Chad Rosier9b2b4c92017-07-23 16:38:08 +000026// subs w0, w1, w2
27// str w0, [x1]
28// b.ne .LBB0_2
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000029// %bb.1:
Chad Rosier9b2b4c92017-07-23 16:38:08 +000030// mov w0, wzr ; <-- redundant
31// str w0, [x2]
32// .LBB0_2
33//
34// 3. Finally, if the flag setting instruction is a comparison against a
35// constant (i.e., ADDS[W|X]ri, SUBS[W|X]ri), we can remove a mov immediate
36// in some cases.
37//
Francis Visoiu Mistrih25528d62017-12-04 17:18:51 +000038// %bb.0:
Chad Rosier9b2b4c92017-07-23 16:38:08 +000039// subs xzr, x0, #1
Chad Rosierea25eca2017-03-02 20:48:11 +000040// b.eq .LBB0_1
41// .LBB0_1:
Chad Rosier9b2b4c92017-07-23 16:38:08 +000042// orr x0, xzr, #0x1 ; <-- redundant
Jun Bum Limb389d9b2016-02-16 20:02:39 +000043//
Chad Rosierea25eca2017-03-02 20:48:11 +000044// This pass should be run after register allocation.
Jun Bum Limb389d9b2016-02-16 20:02:39 +000045//
46// FIXME: This could also be extended to check the whole dominance subtree below
47// the comparison if the compile time regression is acceptable.
48//
Chad Rosier9b2b4c92017-07-23 16:38:08 +000049// FIXME: Add support for handling CCMP instructions.
50// FIXME: If the known register value is zero, we should be able to rewrite uses
51// to use WZR/XZR directly in some cases.
Jun Bum Limb389d9b2016-02-16 20:02:39 +000052//===----------------------------------------------------------------------===//
Jun Bum Limb389d9b2016-02-16 20:02:39 +000053#include "AArch64.h"
Chad Rosierea25eca2017-03-02 20:48:11 +000054#include "llvm/ADT/Optional.h"
Jun Bum Limb389d9b2016-02-16 20:02:39 +000055#include "llvm/ADT/SetVector.h"
56#include "llvm/ADT/Statistic.h"
57#include "llvm/ADT/iterator_range.h"
58#include "llvm/CodeGen/MachineFunctionPass.h"
59#include "llvm/CodeGen/MachineRegisterInfo.h"
60#include "llvm/Support/Debug.h"
61
62using namespace llvm;
63
64#define DEBUG_TYPE "aarch64-copyelim"
65
66STATISTIC(NumCopiesRemoved, "Number of copies removed.");
67
Jun Bum Limb389d9b2016-02-16 20:02:39 +000068namespace {
69class AArch64RedundantCopyElimination : public MachineFunctionPass {
70 const MachineRegisterInfo *MRI;
71 const TargetRegisterInfo *TRI;
Chad Rosier9b2b4c92017-07-23 16:38:08 +000072
73 // DomBBClobberedRegs is used when computing known values in the dominating
74 // BB.
75 BitVector DomBBClobberedRegs;
76
77 // OptBBClobberedRegs is used when optimizing away redundant copies/moves.
78 BitVector OptBBClobberedRegs;
Jun Bum Limb389d9b2016-02-16 20:02:39 +000079
80public:
81 static char ID;
Diana Picus850043b2016-08-01 05:56:57 +000082 AArch64RedundantCopyElimination() : MachineFunctionPass(ID) {
83 initializeAArch64RedundantCopyEliminationPass(
84 *PassRegistry::getPassRegistry());
85 }
Chad Rosierea25eca2017-03-02 20:48:11 +000086
87 struct RegImm {
88 MCPhysReg Reg;
89 int32_t Imm;
90 RegImm(MCPhysReg Reg, int32_t Imm) : Reg(Reg), Imm(Imm) {}
91 };
92
Chad Rosier9b2b4c92017-07-23 16:38:08 +000093 bool knownRegValInBlock(MachineInstr &CondBr, MachineBasicBlock *MBB,
94 SmallVectorImpl<RegImm> &KnownRegs,
95 MachineBasicBlock::iterator &FirstUse);
96 bool optimizeBlock(MachineBasicBlock *MBB);
Jun Bum Limb389d9b2016-02-16 20:02:39 +000097 bool runOnMachineFunction(MachineFunction &MF) override;
Derek Schuff1dbf7a52016-04-04 17:09:25 +000098 MachineFunctionProperties getRequiredProperties() const override {
99 return MachineFunctionProperties().set(
Matthias Braun1eb47362016-08-25 01:27:13 +0000100 MachineFunctionProperties::Property::NoVRegs);
Derek Schuff1dbf7a52016-04-04 17:09:25 +0000101 }
Mehdi Amini117296c2016-10-01 02:56:57 +0000102 StringRef getPassName() const override {
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000103 return "AArch64 Redundant Copy Elimination";
104 }
105};
106char AArch64RedundantCopyElimination::ID = 0;
107}
108
109INITIALIZE_PASS(AArch64RedundantCopyElimination, "aarch64-copyelim",
110 "AArch64 redundant copy elimination pass", false, false)
111
Geoff Berry6bb79152017-02-22 19:10:45 +0000112/// Remember what registers the specified instruction modifies.
113static void trackRegDefs(const MachineInstr &MI, BitVector &ClobberedRegs,
114 const TargetRegisterInfo *TRI) {
115 for (const MachineOperand &MO : MI.operands()) {
116 if (MO.isRegMask()) {
117 ClobberedRegs.setBitsNotInMask(MO.getRegMask());
118 continue;
119 }
120
121 if (!MO.isReg())
122 continue;
123 unsigned Reg = MO.getReg();
124 if (!Reg)
125 continue;
126 if (!MO.isDef())
127 continue;
128
129 for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI)
130 ClobberedRegs.set(*AI);
131 }
132}
133
Chad Rosierea25eca2017-03-02 20:48:11 +0000134/// It's possible to determine the value of a register based on a dominating
135/// condition. To do so, this function checks to see if the basic block \p MBB
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000136/// is the target of a conditional branch \p CondBr with an equality comparison.
137/// If the branch is a CBZ/CBNZ, we know the value of its source operand is zero
138/// in \p MBB for some cases. Otherwise, we find and inspect the NZCV setting
139/// instruction (e.g., SUBS, ADDS). If this instruction defines a register
140/// other than WZR/XZR, we know the value of the destination register is zero in
141/// \p MMB for some cases. In addition, if the NZCV setting instruction is
142/// comparing against a constant we know the other source register is equal to
143/// the constant in \p MBB for some cases. If we find any constant values, push
144/// a physical register and constant value pair onto the KnownRegs vector and
145/// return true. Otherwise, return false if no known values were found.
146bool AArch64RedundantCopyElimination::knownRegValInBlock(
Chad Rosierea25eca2017-03-02 20:48:11 +0000147 MachineInstr &CondBr, MachineBasicBlock *MBB,
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000148 SmallVectorImpl<RegImm> &KnownRegs, MachineBasicBlock::iterator &FirstUse) {
Chad Rosierea25eca2017-03-02 20:48:11 +0000149 unsigned Opc = CondBr.getOpcode();
150
151 // Check if the current basic block is the target block to which the
152 // CBZ/CBNZ instruction jumps when its Wt/Xt is zero.
153 if (((Opc == AArch64::CBZW || Opc == AArch64::CBZX) &&
154 MBB == CondBr.getOperand(1).getMBB()) ||
155 ((Opc == AArch64::CBNZW || Opc == AArch64::CBNZX) &&
156 MBB != CondBr.getOperand(1).getMBB())) {
157 FirstUse = CondBr;
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000158 KnownRegs.push_back(RegImm(CondBr.getOperand(0).getReg(), 0));
159 return true;
Chad Rosierea25eca2017-03-02 20:48:11 +0000160 }
161
162 // Otherwise, must be a conditional branch.
163 if (Opc != AArch64::Bcc)
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000164 return false;
Chad Rosierea25eca2017-03-02 20:48:11 +0000165
166 // Must be an equality check (i.e., == or !=).
167 AArch64CC::CondCode CC = (AArch64CC::CondCode)CondBr.getOperand(0).getImm();
168 if (CC != AArch64CC::EQ && CC != AArch64CC::NE)
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000169 return false;
Chad Rosierea25eca2017-03-02 20:48:11 +0000170
171 MachineBasicBlock *BrTarget = CondBr.getOperand(1).getMBB();
172 if ((CC == AArch64CC::EQ && BrTarget != MBB) ||
173 (CC == AArch64CC::NE && BrTarget == MBB))
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000174 return false;
Chad Rosierea25eca2017-03-02 20:48:11 +0000175
176 // Stop if we get to the beginning of PredMBB.
177 MachineBasicBlock *PredMBB = *MBB->pred_begin();
178 assert(PredMBB == CondBr.getParent() &&
179 "Conditional branch not in predecessor block!");
180 if (CondBr == PredMBB->begin())
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000181 return false;
Chad Rosierea25eca2017-03-02 20:48:11 +0000182
183 // Registers clobbered in PredMBB between CondBr instruction and current
184 // instruction being checked in loop.
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000185 DomBBClobberedRegs.reset();
Chad Rosierea25eca2017-03-02 20:48:11 +0000186
187 // Find compare instruction that sets NZCV used by CondBr.
188 MachineBasicBlock::reverse_iterator RIt = CondBr.getReverseIterator();
189 for (MachineInstr &PredI : make_range(std::next(RIt), PredMBB->rend())) {
190
Chad Rosier9a70c7c2017-03-06 21:20:00 +0000191 bool IsCMN = false;
Chad Rosierea25eca2017-03-02 20:48:11 +0000192 switch (PredI.getOpcode()) {
193 default:
194 break;
195
Chad Rosier9a70c7c2017-03-06 21:20:00 +0000196 // CMN is an alias for ADDS with a dead destination register.
197 case AArch64::ADDSWri:
198 case AArch64::ADDSXri:
199 IsCMN = true;
Simon Pilgrim8b4dc532017-07-07 13:03:28 +0000200 LLVM_FALLTHROUGH;
Chad Rosierea25eca2017-03-02 20:48:11 +0000201 // CMP is an alias for SUBS with a dead destination register.
202 case AArch64::SUBSWri:
203 case AArch64::SUBSXri: {
Tim Northover350a87e2017-10-17 21:43:52 +0000204 // Sometimes the first operand is a FrameIndex. Bail if tht happens.
205 if (!PredI.getOperand(1).isReg())
206 return false;
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000207 MCPhysReg DstReg = PredI.getOperand(0).getReg();
Chad Rosier9a70c7c2017-03-06 21:20:00 +0000208 MCPhysReg SrcReg = PredI.getOperand(1).getReg();
209
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000210 bool Res = false;
211 // If we're comparing against a non-symbolic immediate and the source
212 // register of the compare is not modified (including a self-clobbering
213 // compare) between the compare and conditional branch we known the value
214 // of the 1st source operand.
215 if (PredI.getOperand(2).isImm() && !DomBBClobberedRegs[SrcReg] &&
216 SrcReg != DstReg) {
217 // We've found the instruction that sets NZCV.
218 int32_t KnownImm = PredI.getOperand(2).getImm();
219 int32_t Shift = PredI.getOperand(3).getImm();
220 KnownImm <<= Shift;
221 if (IsCMN)
222 KnownImm = -KnownImm;
223 FirstUse = PredI;
224 KnownRegs.push_back(RegImm(SrcReg, KnownImm));
225 Res = true;
226 }
Chad Rosierea25eca2017-03-02 20:48:11 +0000227
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000228 // If this instructions defines something other than WZR/XZR, we know it's
229 // result is zero in some cases.
230 if (DstReg == AArch64::WZR || DstReg == AArch64::XZR)
231 return Res;
Chad Rosierea25eca2017-03-02 20:48:11 +0000232
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000233 // The destination register must not be modified between the NZCV setting
234 // instruction and the conditional branch.
235 if (DomBBClobberedRegs[DstReg])
236 return Res;
237
Chad Rosierea25eca2017-03-02 20:48:11 +0000238 FirstUse = PredI;
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000239 KnownRegs.push_back(RegImm(DstReg, 0));
240 return true;
241 }
242
243 // Look for NZCV setting instructions that define something other than
244 // WZR/XZR.
245 case AArch64::ADCSWr:
246 case AArch64::ADCSXr:
247 case AArch64::ADDSWrr:
248 case AArch64::ADDSWrs:
249 case AArch64::ADDSWrx:
250 case AArch64::ADDSXrr:
251 case AArch64::ADDSXrs:
252 case AArch64::ADDSXrx:
253 case AArch64::ADDSXrx64:
254 case AArch64::ANDSWri:
255 case AArch64::ANDSWrr:
256 case AArch64::ANDSWrs:
257 case AArch64::ANDSXri:
258 case AArch64::ANDSXrr:
259 case AArch64::ANDSXrs:
260 case AArch64::BICSWrr:
261 case AArch64::BICSWrs:
262 case AArch64::BICSXrs:
263 case AArch64::BICSXrr:
264 case AArch64::SBCSWr:
265 case AArch64::SBCSXr:
266 case AArch64::SUBSWrr:
267 case AArch64::SUBSWrs:
268 case AArch64::SUBSWrx:
269 case AArch64::SUBSXrr:
270 case AArch64::SUBSXrs:
271 case AArch64::SUBSXrx:
272 case AArch64::SUBSXrx64: {
273 MCPhysReg DstReg = PredI.getOperand(0).getReg();
274 if (DstReg == AArch64::WZR || DstReg == AArch64::XZR)
275 return false;
276
277 // The destination register of the NZCV setting instruction must not be
278 // modified before the conditional branch.
279 if (DomBBClobberedRegs[DstReg])
280 return false;
281
282 // We've found the instruction that sets NZCV whose DstReg == 0.
283 FirstUse = PredI;
284 KnownRegs.push_back(RegImm(DstReg, 0));
285 return true;
Chad Rosierea25eca2017-03-02 20:48:11 +0000286 }
287 }
288
289 // Bail if we see an instruction that defines NZCV that we don't handle.
290 if (PredI.definesRegister(AArch64::NZCV))
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000291 return false;
292
293 // Track clobbered registers.
294 trackRegDefs(PredI, DomBBClobberedRegs, TRI);
Chad Rosierea25eca2017-03-02 20:48:11 +0000295 }
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000296 return false;
Chad Rosierea25eca2017-03-02 20:48:11 +0000297}
298
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000299bool AArch64RedundantCopyElimination::optimizeBlock(MachineBasicBlock *MBB) {
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000300 // Check if the current basic block has a single predecessor.
301 if (MBB->pred_size() != 1)
302 return false;
303
Chad Rosier072e70b2017-01-25 15:56:59 +0000304 // Check if the predecessor has two successors, implying the block ends in a
305 // conditional branch.
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000306 MachineBasicBlock *PredMBB = *MBB->pred_begin();
Chad Rosier072e70b2017-01-25 15:56:59 +0000307 if (PredMBB->succ_size() != 2)
308 return false;
309
Chad Rosierea25eca2017-03-02 20:48:11 +0000310 MachineBasicBlock::iterator CondBr = PredMBB->getLastNonDebugInstr();
311 if (CondBr == PredMBB->end())
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000312 return false;
313
Geoff Berry6bb79152017-02-22 19:10:45 +0000314 // Keep track of the earliest point in the PredMBB block where kill markers
315 // need to be removed if a COPY is removed.
316 MachineBasicBlock::iterator FirstUse;
Chad Rosierea25eca2017-03-02 20:48:11 +0000317 // After calling knownRegValInBlock, FirstUse will either point to a CBZ/CBNZ
318 // or a compare (i.e., SUBS). In the latter case, we must take care when
319 // updating FirstUse when scanning for COPY instructions. In particular, if
320 // there's a COPY in between the compare and branch the COPY should not
321 // update FirstUse.
322 bool SeenFirstUse = false;
323 // Registers that contain a known value at the start of MBB.
324 SmallVector<RegImm, 4> KnownRegs;
325
326 MachineBasicBlock::iterator Itr = std::next(CondBr);
Tim Northover3f228562016-02-17 21:16:53 +0000327 do {
Chad Rosierea25eca2017-03-02 20:48:11 +0000328 --Itr;
329
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000330 if (!knownRegValInBlock(*Itr, MBB, KnownRegs, FirstUse))
Geoff Berry6bb79152017-02-22 19:10:45 +0000331 continue;
332
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000333 // Reset the clobber list.
334 OptBBClobberedRegs.reset();
Chad Rosierea25eca2017-03-02 20:48:11 +0000335
336 // Look backward in PredMBB for COPYs from the known reg to find other
337 // registers that are known to be a constant value.
338 for (auto PredI = Itr;; --PredI) {
339 if (FirstUse == PredI)
340 SeenFirstUse = true;
341
Geoff Berry6bb79152017-02-22 19:10:45 +0000342 if (PredI->isCopy()) {
343 MCPhysReg CopyDstReg = PredI->getOperand(0).getReg();
344 MCPhysReg CopySrcReg = PredI->getOperand(1).getReg();
Chad Rosierea25eca2017-03-02 20:48:11 +0000345 for (auto &KnownReg : KnownRegs) {
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000346 if (OptBBClobberedRegs[KnownReg.Reg])
Geoff Berry6bb79152017-02-22 19:10:45 +0000347 continue;
348 // If we have X = COPY Y, and Y is known to be zero, then now X is
349 // known to be zero.
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000350 if (CopySrcReg == KnownReg.Reg && !OptBBClobberedRegs[CopyDstReg]) {
Chad Rosierea25eca2017-03-02 20:48:11 +0000351 KnownRegs.push_back(RegImm(CopyDstReg, KnownReg.Imm));
352 if (SeenFirstUse)
353 FirstUse = PredI;
Geoff Berry6bb79152017-02-22 19:10:45 +0000354 break;
355 }
356 // If we have X = COPY Y, and X is known to be zero, then now Y is
357 // known to be zero.
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000358 if (CopyDstReg == KnownReg.Reg && !OptBBClobberedRegs[CopySrcReg]) {
Chad Rosierea25eca2017-03-02 20:48:11 +0000359 KnownRegs.push_back(RegImm(CopySrcReg, KnownReg.Imm));
360 if (SeenFirstUse)
361 FirstUse = PredI;
Geoff Berry6bb79152017-02-22 19:10:45 +0000362 break;
363 }
364 }
365 }
366
367 // Stop if we get to the beginning of PredMBB.
368 if (PredI == PredMBB->begin())
369 break;
370
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000371 trackRegDefs(*PredI, OptBBClobberedRegs, TRI);
Geoff Berry6bb79152017-02-22 19:10:45 +0000372 // Stop if all of the known-zero regs have been clobbered.
Chad Rosierea25eca2017-03-02 20:48:11 +0000373 if (all_of(KnownRegs, [&](RegImm KnownReg) {
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000374 return OptBBClobberedRegs[KnownReg.Reg];
Geoff Berry6bb79152017-02-22 19:10:45 +0000375 }))
376 break;
377 }
378 break;
379
Chad Rosierea25eca2017-03-02 20:48:11 +0000380 } while (Itr != PredMBB->begin() && Itr->isTerminator());
Tim Northover3f228562016-02-17 21:16:53 +0000381
Chad Rosierea25eca2017-03-02 20:48:11 +0000382 // We've not found a registers with a known value, time to bail out.
383 if (KnownRegs.empty())
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000384 return false;
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000385
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000386 bool Changed = false;
Chad Rosierea25eca2017-03-02 20:48:11 +0000387 // UsedKnownRegs is the set of KnownRegs that have had uses added to MBB.
388 SmallSetVector<unsigned, 4> UsedKnownRegs;
Tim Northover3f228562016-02-17 21:16:53 +0000389 MachineBasicBlock::iterator LastChange = MBB->begin();
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000390 // Remove redundant copy/move instructions unless KnownReg is modified.
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000391 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;) {
392 MachineInstr *MI = &*I;
393 ++I;
Chad Rosierea25eca2017-03-02 20:48:11 +0000394 bool RemovedMI = false;
395 bool IsCopy = MI->isCopy();
396 bool IsMoveImm = MI->isMoveImmediate();
397 if (IsCopy || IsMoveImm) {
Geoff Berry6bb79152017-02-22 19:10:45 +0000398 MCPhysReg DefReg = MI->getOperand(0).getReg();
Chad Rosierea25eca2017-03-02 20:48:11 +0000399 MCPhysReg SrcReg = IsCopy ? MI->getOperand(1).getReg() : 0;
400 int64_t SrcImm = IsMoveImm ? MI->getOperand(1).getImm() : 0;
401 if (!MRI->isReserved(DefReg) &&
402 ((IsCopy && (SrcReg == AArch64::XZR || SrcReg == AArch64::WZR)) ||
403 IsMoveImm)) {
404 for (RegImm &KnownReg : KnownRegs) {
405 if (KnownReg.Reg != DefReg &&
406 !TRI->isSuperRegister(DefReg, KnownReg.Reg))
407 continue;
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000408
Chad Rosierea25eca2017-03-02 20:48:11 +0000409 // For a copy, the known value must be a zero.
410 if (IsCopy && KnownReg.Imm != 0)
411 continue;
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000412
Chad Rosierea25eca2017-03-02 20:48:11 +0000413 if (IsMoveImm) {
414 // For a move immediate, the known immediate must match the source
415 // immediate.
416 if (KnownReg.Imm != SrcImm)
417 continue;
418
419 // Don't remove a move immediate that implicitly defines the upper
420 // bits when only the lower 32 bits are known.
421 MCPhysReg CmpReg = KnownReg.Reg;
422 if (any_of(MI->implicit_operands(), [CmpReg](MachineOperand &O) {
423 return !O.isDead() && O.isReg() && O.isDef() &&
424 O.getReg() != CmpReg;
425 }))
426 continue;
Geoff Berry6bb79152017-02-22 19:10:45 +0000427 }
Chad Rosierea25eca2017-03-02 20:48:11 +0000428
429 if (IsCopy)
430 DEBUG(dbgs() << "Remove redundant Copy : " << *MI);
431 else
432 DEBUG(dbgs() << "Remove redundant Move : " << *MI);
433
434 MI->eraseFromParent();
435 Changed = true;
436 LastChange = I;
437 NumCopiesRemoved++;
438 UsedKnownRegs.insert(KnownReg.Reg);
439 RemovedMI = true;
440 break;
Geoff Berry6bb79152017-02-22 19:10:45 +0000441 }
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000442 }
443 }
444
Chad Rosierea25eca2017-03-02 20:48:11 +0000445 // Skip to the next instruction if we removed the COPY/MovImm.
446 if (RemovedMI)
Geoff Berry6bb79152017-02-22 19:10:45 +0000447 continue;
448
Chad Rosierea25eca2017-03-02 20:48:11 +0000449 // Remove any regs the MI clobbers from the KnownConstRegs set.
450 for (unsigned RI = 0; RI < KnownRegs.size();)
451 if (MI->modifiesRegister(KnownRegs[RI].Reg, TRI)) {
452 std::swap(KnownRegs[RI], KnownRegs[KnownRegs.size() - 1]);
453 KnownRegs.pop_back();
Geoff Berry6bb79152017-02-22 19:10:45 +0000454 // Don't increment RI since we need to now check the swapped-in
Chad Rosierea25eca2017-03-02 20:48:11 +0000455 // KnownRegs[RI].
Geoff Berry6bb79152017-02-22 19:10:45 +0000456 } else {
457 ++RI;
458 }
459
Chad Rosierea25eca2017-03-02 20:48:11 +0000460 // Continue until the KnownRegs set is empty.
461 if (KnownRegs.empty())
Tim Northover3f228562016-02-17 21:16:53 +0000462 break;
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000463 }
Tim Northover3f228562016-02-17 21:16:53 +0000464
465 if (!Changed)
466 return false;
467
Geoff Berry6bb79152017-02-22 19:10:45 +0000468 // Add newly used regs to the block's live-in list if they aren't there
469 // already.
Chad Rosierea25eca2017-03-02 20:48:11 +0000470 for (MCPhysReg KnownReg : UsedKnownRegs)
471 if (!MBB->isLiveIn(KnownReg))
472 MBB->addLiveIn(KnownReg);
Tim Northover3f228562016-02-17 21:16:53 +0000473
Geoff Berry6bb79152017-02-22 19:10:45 +0000474 // Clear kills in the range where changes were made. This is conservative,
475 // but should be okay since kill markers are being phased out.
Chad Rosierea25eca2017-03-02 20:48:11 +0000476 DEBUG(dbgs() << "Clearing kill flags.\n\tFirstUse: " << *FirstUse
477 << "\tLastChange: " << *LastChange);
Geoff Berry6bb79152017-02-22 19:10:45 +0000478 for (MachineInstr &MMI : make_range(FirstUse, PredMBB->end()))
479 MMI.clearKillInfo();
Duncan P. N. Exon Smith84c2da42016-08-18 17:58:09 +0000480 for (MachineInstr &MMI : make_range(MBB->begin(), LastChange))
Geoff Berry6bb79152017-02-22 19:10:45 +0000481 MMI.clearKillInfo();
Tim Northover7687bce2016-02-17 23:07:04 +0000482
Tim Northover3f228562016-02-17 21:16:53 +0000483 return true;
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000484}
485
486bool AArch64RedundantCopyElimination::runOnMachineFunction(
487 MachineFunction &MF) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000488 if (skipFunction(MF.getFunction()))
Andrew Kaylor1ac98bb2016-04-25 21:58:52 +0000489 return false;
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000490 TRI = MF.getSubtarget().getRegisterInfo();
491 MRI = &MF.getRegInfo();
Chad Rosierea25eca2017-03-02 20:48:11 +0000492
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000493 // Resize the clobber register bitfield trackers. We do this once per
494 // function.
495 DomBBClobberedRegs.resize(TRI->getNumRegs());
496 OptBBClobberedRegs.resize(TRI->getNumRegs());
Chad Rosierea25eca2017-03-02 20:48:11 +0000497
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000498 bool Changed = false;
499 for (MachineBasicBlock &MBB : MF)
Chad Rosier9b2b4c92017-07-23 16:38:08 +0000500 Changed |= optimizeBlock(&MBB);
Jun Bum Limb389d9b2016-02-16 20:02:39 +0000501 return Changed;
502}
503
504FunctionPass *llvm::createAArch64RedundantCopyEliminationPass() {
505 return new AArch64RedundantCopyElimination();
506}