1//===- MachineCopyPropagation.cpp - Machine Copy Propagation Pass ---------===//
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//
8//===----------------------------------------------------------------------===//
9//
10// This is an extremely simple MachineInstr-level copy propagation pass.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "codegen-cp"
15#include "llvm/CodeGen/Passes.h"
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/SetVector.h"
18#include "llvm/ADT/SmallVector.h"
19#include "llvm/ADT/Statistic.h"
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/Pass.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/ErrorHandling.h"
26#include "llvm/Support/raw_ostream.h"
27#include "llvm/Target/TargetInstrInfo.h"
28#include "llvm/Target/TargetRegisterInfo.h"
29using namespace llvm;
30
31STATISTIC(NumDeletes, "Number of dead copies deleted");
32
33namespace {
34  class MachineCopyPropagation : public MachineFunctionPass {
35    const TargetRegisterInfo *TRI;
36    const TargetInstrInfo *TII;
37    MachineRegisterInfo *MRI;
38
39  public:
40    static char ID; // Pass identification, replacement for typeid
41    MachineCopyPropagation() : MachineFunctionPass(ID) {
42     initializeMachineCopyPropagationPass(*PassRegistry::getPassRegistry());
43    }
44
45    virtual bool runOnMachineFunction(MachineFunction &MF);
46
47  private:
48    typedef SmallVector<unsigned, 4> DestList;
49    typedef DenseMap<unsigned, DestList> SourceMap;
50
51    void SourceNoLongerAvailable(unsigned Reg,
52                                 SourceMap &SrcMap,
53                                 DenseMap<unsigned, MachineInstr*> &AvailCopyMap);
54    bool CopyPropagateBlock(MachineBasicBlock &MBB);
55    void removeCopy(MachineInstr *MI);
56  };
57}
58char MachineCopyPropagation::ID = 0;
59char &llvm::MachineCopyPropagationID = MachineCopyPropagation::ID;
60
61INITIALIZE_PASS(MachineCopyPropagation, "machine-cp",
62                "Machine Copy Propagation Pass", false, false)
63
64void
65MachineCopyPropagation::SourceNoLongerAvailable(unsigned Reg,
66                              SourceMap &SrcMap,
67                              DenseMap<unsigned, MachineInstr*> &AvailCopyMap) {
68  for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
69    SourceMap::iterator SI = SrcMap.find(*AI);
70    if (SI != SrcMap.end()) {
71      const DestList& Defs = SI->second;
72      for (DestList::const_iterator I = Defs.begin(), E = Defs.end();
73           I != E; ++I) {
74        unsigned MappedDef = *I;
75        // Source of copy is no longer available for propagation.
76        if (AvailCopyMap.erase(MappedDef)) {
77          for (MCSubRegIterator SR(MappedDef, TRI); SR.isValid(); ++SR)
78            AvailCopyMap.erase(*SR);
79        }
80      }
81    }
82  }
83}
84
85static bool NoInterveningSideEffect(const MachineInstr *CopyMI,
86                                    const MachineInstr *MI) {
87  const MachineBasicBlock *MBB = CopyMI->getParent();
88  if (MI->getParent() != MBB)
89    return false;
90  MachineBasicBlock::const_iterator I = CopyMI;
91  MachineBasicBlock::const_iterator E = MBB->end();
92  MachineBasicBlock::const_iterator E2 = MI;
93
94  ++I;
95  while (I != E && I != E2) {
96    if (I->hasUnmodeledSideEffects() || I->isCall() ||
97        I->isTerminator())
98      return false;
99    ++I;
100  }
101  return true;
102}
103
104/// isNopCopy - Return true if the specified copy is really a nop. That is
105/// if the source of the copy is the same of the definition of the copy that
106/// supplied the source. If the source of the copy is a sub-register than it
107/// must check the sub-indices match. e.g.
108/// ecx = mov eax
109/// al  = mov cl
110/// But not
111/// ecx = mov eax
112/// al  = mov ch
113static bool isNopCopy(MachineInstr *CopyMI, unsigned Def, unsigned Src,
114                      const TargetRegisterInfo *TRI) {
115  unsigned SrcSrc = CopyMI->getOperand(1).getReg();
116  if (Def == SrcSrc)
117    return true;
118  if (TRI->isSubRegister(SrcSrc, Def)) {
119    unsigned SrcDef = CopyMI->getOperand(0).getReg();
120    unsigned SubIdx = TRI->getSubRegIndex(SrcSrc, Def);
121    if (!SubIdx)
122      return false;
123    return SubIdx == TRI->getSubRegIndex(SrcDef, Src);
124  }
125
126  return false;
127}
128
129// Remove MI from the function because it has been determined it is dead.
130// Turn it into a noop KILL instruction if it has super-register liveness
131// adjustments.
132void MachineCopyPropagation::removeCopy(MachineInstr *MI) {
133  if (MI->getNumOperands() == 2)
134    MI->eraseFromParent();
135  else
136    MI->setDesc(TII->get(TargetOpcode::KILL));
137}
138
139bool MachineCopyPropagation::CopyPropagateBlock(MachineBasicBlock &MBB) {
140  SmallSetVector<MachineInstr*, 8> MaybeDeadCopies;  // Candidates for deletion
141  DenseMap<unsigned, MachineInstr*> AvailCopyMap;    // Def -> available copies map
142  DenseMap<unsigned, MachineInstr*> CopyMap;         // Def -> copies map
143  SourceMap SrcMap; // Src -> Def map
144
145  bool Changed = false;
146  for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E; ) {
147    MachineInstr *MI = &*I;
148    ++I;
149
150    if (MI->isCopy()) {
151      unsigned Def = MI->getOperand(0).getReg();
152      unsigned Src = MI->getOperand(1).getReg();
153
154      if (TargetRegisterInfo::isVirtualRegister(Def) ||
155          TargetRegisterInfo::isVirtualRegister(Src))
156        report_fatal_error("MachineCopyPropagation should be run after"
157                           " register allocation!");
158
159      DenseMap<unsigned, MachineInstr*>::iterator CI = AvailCopyMap.find(Src);
160      if (CI != AvailCopyMap.end()) {
161        MachineInstr *CopyMI = CI->second;
162        if (!MRI->isReserved(Def) &&
163            (!MRI->isReserved(Src) || NoInterveningSideEffect(CopyMI, MI)) &&
164            isNopCopy(CopyMI, Def, Src, TRI)) {
165          // The two copies cancel out and the source of the first copy
166          // hasn't been overridden, eliminate the second one. e.g.
167          //  %ECX<def> = COPY %EAX<kill>
168          //  ... nothing clobbered EAX.
169          //  %EAX<def> = COPY %ECX
170          // =>
171          //  %ECX<def> = COPY %EAX
172          //
173          // Also avoid eliminating a copy from reserved registers unless the
174          // definition is proven not clobbered. e.g.
175          // %RSP<def> = COPY %RAX
176          // CALL
177          // %RAX<def> = COPY %RSP
178
179          // Clear any kills of Def between CopyMI and MI. This extends the
180          // live range.
181          for (MachineBasicBlock::iterator I = CopyMI, E = MI; I != E; ++I)
182            I->clearRegisterKills(Def, TRI);
183
184          removeCopy(MI);
185          Changed = true;
186          ++NumDeletes;
187          continue;
188        }
189      }
190
191      // If Src is defined by a previous copy, it cannot be eliminated.
192      for (MCRegAliasIterator AI(Src, TRI, true); AI.isValid(); ++AI) {
193        CI = CopyMap.find(*AI);
194        if (CI != CopyMap.end())
195          MaybeDeadCopies.remove(CI->second);
196      }
197
198      // Copy is now a candidate for deletion.
199      MaybeDeadCopies.insert(MI);
200
201      // If 'Src' is previously source of another copy, then this earlier copy's
202      // source is no longer available. e.g.
203      // %xmm9<def> = copy %xmm2
204      // ...
205      // %xmm2<def> = copy %xmm0
206      // ...
207      // %xmm2<def> = copy %xmm9
208      SourceNoLongerAvailable(Def, SrcMap, AvailCopyMap);
209
210      // Remember Def is defined by the copy.
211      // ... Make sure to clear the def maps of aliases first.
212      for (MCRegAliasIterator AI(Def, TRI, false); AI.isValid(); ++AI) {
213        CopyMap.erase(*AI);
214        AvailCopyMap.erase(*AI);
215      }
216      for (MCSubRegIterator SR(Def, TRI, /*IncludeSelf=*/true); SR.isValid();
217           ++SR) {
218        CopyMap[*SR] = MI;
219        AvailCopyMap[*SR] = MI;
220      }
221
222      // Remember source that's copied to Def. Once it's clobbered, then
223      // it's no longer available for copy propagation.
224      if (std::find(SrcMap[Src].begin(), SrcMap[Src].end(), Def) ==
225          SrcMap[Src].end()) {
226        SrcMap[Src].push_back(Def);
227      }
228
229      continue;
230    }
231
232    // Not a copy.
233    SmallVector<unsigned, 2> Defs;
234    int RegMaskOpNum = -1;
235    for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
236      MachineOperand &MO = MI->getOperand(i);
237      if (MO.isRegMask())
238        RegMaskOpNum = i;
239      if (!MO.isReg())
240        continue;
241      unsigned Reg = MO.getReg();
242      if (!Reg)
243        continue;
244
245      if (TargetRegisterInfo::isVirtualRegister(Reg))
246        report_fatal_error("MachineCopyPropagation should be run after"
247                           " register allocation!");
248
249      if (MO.isDef()) {
250        Defs.push_back(Reg);
251        continue;
252      }
253
254      // If 'Reg' is defined by a copy, the copy is no longer a candidate
255      // for elimination.
256      for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
257        DenseMap<unsigned, MachineInstr*>::iterator CI = CopyMap.find(*AI);
258        if (CI != CopyMap.end())
259          MaybeDeadCopies.remove(CI->second);
260      }
261    }
262
263    // The instruction has a register mask operand which means that it clobbers
264    // a large set of registers.  It is possible to use the register mask to
265    // prune the available copies, but treat it like a basic block boundary for
266    // now.
267    if (RegMaskOpNum >= 0) {
268      // Erase any MaybeDeadCopies whose destination register is clobbered.
269      const MachineOperand &MaskMO = MI->getOperand(RegMaskOpNum);
270      for (SmallSetVector<MachineInstr*, 8>::iterator
271           DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end();
272           DI != DE; ++DI) {
273        unsigned Reg = (*DI)->getOperand(0).getReg();
274        if (MRI->isReserved(Reg) || !MaskMO.clobbersPhysReg(Reg))
275          continue;
276        removeCopy(*DI);
277        Changed = true;
278        ++NumDeletes;
279      }
280
281      // Clear all data structures as if we were beginning a new basic block.
282      MaybeDeadCopies.clear();
283      AvailCopyMap.clear();
284      CopyMap.clear();
285      SrcMap.clear();
286      continue;
287    }
288
289    for (unsigned i = 0, e = Defs.size(); i != e; ++i) {
290      unsigned Reg = Defs[i];
291
292      // No longer defined by a copy.
293      for (MCRegAliasIterator AI(Reg, TRI, true); AI.isValid(); ++AI) {
294        CopyMap.erase(*AI);
295        AvailCopyMap.erase(*AI);
296      }
297
298      // If 'Reg' is previously source of a copy, it is no longer available for
299      // copy propagation.
300      SourceNoLongerAvailable(Reg, SrcMap, AvailCopyMap);
301    }
302  }
303
304  // If MBB doesn't have successors, delete the copies whose defs are not used.
305  // If MBB does have successors, then conservative assume the defs are live-out
306  // since we don't want to trust live-in lists.
307  if (MBB.succ_empty()) {
308    for (SmallSetVector<MachineInstr*, 8>::iterator
309           DI = MaybeDeadCopies.begin(), DE = MaybeDeadCopies.end();
310         DI != DE; ++DI) {
311      if (!MRI->isReserved((*DI)->getOperand(0).getReg())) {
312        removeCopy(*DI);
313        Changed = true;
314        ++NumDeletes;
315      }
316    }
317  }
318
319  return Changed;
320}
321
322bool MachineCopyPropagation::runOnMachineFunction(MachineFunction &MF) {
323  bool Changed = false;
324
325  TRI = MF.getTarget().getRegisterInfo();
326  TII = MF.getTarget().getInstrInfo();
327  MRI = &MF.getRegInfo();
328
329  for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
330    Changed |= CopyPropagateBlock(*I);
331
332  return Changed;
333}
334