Fix for codegen bug that could cause illegal cmn instruction generation
[oota-llvm.git] / lib / Target / ARM64 / ARM64DeadRegisterDefinitionsPass.cpp
1 //===-- ARM64DeadRegisterDefinitions.cpp - Replace dead defs w/ zero reg --===//
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 // When allowed by the instruction, replace a dead definition of a GPR with
10 // the zero register. This makes the code a bit friendlier towards the
11 // hardware's register renamer.
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "arm64-dead-defs"
15 #include "ARM64.h"
16 #include "ARM64RegisterInfo.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstr.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
23 using namespace llvm;
24
25 STATISTIC(NumDeadDefsReplaced, "Number of dead definitions replaced");
26
27 namespace {
28 class ARM64DeadRegisterDefinitions : public MachineFunctionPass {
29 private:
30   const TargetRegisterInfo *TRI;
31   bool implicitlyDefinesSubReg(unsigned Reg, const MachineInstr *MI);
32   bool processMachineBasicBlock(MachineBasicBlock *MBB);
33   bool usesFrameIndex(const MachineInstr *MI);
34 public:
35   static char ID; // Pass identification, replacement for typeid.
36   explicit ARM64DeadRegisterDefinitions() : MachineFunctionPass(ID) {}
37
38   virtual bool runOnMachineFunction(MachineFunction &F);
39
40   const char *getPassName() const { return "Dead register definitions"; }
41
42   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43     AU.setPreservesCFG();
44     MachineFunctionPass::getAnalysisUsage(AU);
45   }
46 };
47 char ARM64DeadRegisterDefinitions::ID = 0;
48 } // end anonymous namespace
49
50 bool ARM64DeadRegisterDefinitions::implicitlyDefinesSubReg(
51                                                        unsigned Reg,
52                                                        const MachineInstr *MI) {
53   for (const MachineOperand &MO : MI->implicit_operands())
54     if (MO.isReg() && MO.isDef())
55       if (TRI->isSubRegister(Reg, MO.getReg()))
56         return true;
57   return false;
58 }
59
60 bool ARM64DeadRegisterDefinitions::usesFrameIndex(const MachineInstr *MI) {
61   for (int I = MI->getDesc().getNumDefs(), E = MI->getNumOperands(); I != E; ++I) {
62     if (MI->getOperand(I).isFI())
63       return true;
64   }
65   return false;
66 }
67
68 bool
69 ARM64DeadRegisterDefinitions::processMachineBasicBlock(MachineBasicBlock *MBB) {
70   bool Changed = false;
71   for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
72        ++I) {
73     MachineInstr *MI = I;
74     if (usesFrameIndex(MI)) {
75       // We need to skip this instruction because while it appears to have a
76       // dead def it uses a frame index which might expand into a multi
77       // instruction sequence during EPI
78       DEBUG(dbgs() << "    Ignoring, operand is frame index\n");
79       continue;
80     }
81     for (int i = 0, e = MI->getDesc().getNumDefs(); i != e; ++i) {
82       MachineOperand &MO = MI->getOperand(i);
83       if (MO.isReg() && MO.isDead() && MO.isDef()) {
84         assert(!MO.isImplicit() && "Unexpected implicit def!");
85         DEBUG(dbgs() << "  Dead def operand #" << i << " in:\n    ";
86               MI->print(dbgs()));
87         // Be careful not to change the register if it's a tied operand.
88         if (MI->isRegTiedToUseOperand(i)) {
89           DEBUG(dbgs() << "    Ignoring, def is tied operand.\n");
90           continue;
91         }
92         // Don't change the register if there's an implicit def of a subreg.
93         if (implicitlyDefinesSubReg(MO.getReg(), MI)) {
94           DEBUG(dbgs() << "    Ignoring, implicitly defines subregister.\n");
95           continue;
96         }
97         // Make sure the instruction take a register class that contains
98         // the zero register and replace it if so.
99         unsigned NewReg;
100         switch (MI->getDesc().OpInfo[i].RegClass) {
101         default:
102           DEBUG(dbgs() << "    Ignoring, register is not a GPR.\n");
103           continue;
104         case ARM64::GPR32RegClassID:
105           NewReg = ARM64::WZR;
106           break;
107         case ARM64::GPR64RegClassID:
108           NewReg = ARM64::XZR;
109           break;
110         }
111         DEBUG(dbgs() << "    Replacing with zero register. New:\n      ");
112         MO.setReg(NewReg);
113         DEBUG(MI->print(dbgs()));
114         ++NumDeadDefsReplaced;
115       }
116     }
117   }
118   return Changed;
119 }
120
121 // Scan the function for instructions that have a dead definition of a
122 // register. Replace that register with the zero register when possible.
123 bool ARM64DeadRegisterDefinitions::runOnMachineFunction(MachineFunction &mf) {
124   MachineFunction *MF = &mf;
125   TRI = MF->getTarget().getRegisterInfo();
126   bool Changed = false;
127   DEBUG(dbgs() << "***** ARM64DeadRegisterDefinitions *****\n");
128
129   for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I)
130     if (processMachineBasicBlock(I))
131       Changed = true;
132   return Changed;
133 }
134
135 FunctionPass *llvm::createARM64DeadRegisterDefinitions() {
136   return new ARM64DeadRegisterDefinitions();
137 }