Put Darwin-specific code inside an __APPLE__ ifdef.
[oota-llvm.git] / lib / Target / ARM / NEONMoveFix.cpp
1 //===-- NEONMoveFix.cpp - Convert vfp reg-reg moves into neon ---*- C++ -*-===//
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 #define DEBUG_TYPE "neon-mov-fix"
11 #include "ARM.h"
12 #include "ARMMachineFunctionInfo.h"
13 #include "ARMInstrInfo.h"
14 #include "llvm/CodeGen/MachineInstr.h"
15 #include "llvm/CodeGen/MachineInstrBuilder.h"
16 #include "llvm/CodeGen/MachineFunctionPass.h"
17 #include "llvm/ADT/Statistic.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
21
22 STATISTIC(NumVMovs, "Number of reg-reg moves converted");
23
24 namespace {
25   struct NEONMoveFixPass : public MachineFunctionPass {
26     static char ID;
27     NEONMoveFixPass() : MachineFunctionPass(ID) {}
28
29     virtual bool runOnMachineFunction(MachineFunction &Fn);
30
31     virtual const char *getPassName() const {
32       return "NEON reg-reg move conversion";
33     }
34
35   private:
36     const TargetRegisterInfo *TRI;
37     const ARMBaseInstrInfo *TII;
38     bool isA8;
39
40     typedef DenseMap<unsigned, const MachineInstr*> RegMap;
41
42     bool InsertMoves(MachineBasicBlock &MBB);
43
44     void TransferImpOps(MachineInstr &Old, MachineInstr &New);
45   };
46   char NEONMoveFixPass::ID = 0;
47 }
48
49 static bool inNEONDomain(unsigned Domain, bool isA8) {
50   return (Domain & ARMII::DomainNEON) ||
51     (isA8 && (Domain & ARMII::DomainNEONA8));
52 }
53
54 /// Transfer implicit kill and def operands from Old to New.
55 void NEONMoveFixPass::TransferImpOps(MachineInstr &Old, MachineInstr &New) {
56   for (unsigned i = 0, e = Old.getNumOperands(); i != e; ++i) {
57     MachineOperand &MO = Old.getOperand(i);
58     if (!MO.isReg() || !MO.isImplicit())
59       continue;
60     New.addOperand(MO);
61   }
62 }
63
64 bool NEONMoveFixPass::InsertMoves(MachineBasicBlock &MBB) {
65   RegMap Defs;
66   bool Modified = false;
67
68   // Walk over MBB tracking the def points of the registers.
69   MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
70   MachineBasicBlock::iterator NextMII;
71   for (; MII != E; MII = NextMII) {
72     NextMII = llvm::next(MII);
73     MachineInstr *MI = &*MII;
74
75     if (MI->getOpcode() == ARM::VMOVD &&
76         !TII->isPredicated(MI)) {
77       unsigned SrcReg = MI->getOperand(1).getReg();
78       // If we do not find an instruction defining the reg, this means the
79       // register should be live-in for this BB. It's always to better to use
80       // NEON reg-reg moves.
81       unsigned Domain = ARMII::DomainNEON;
82       RegMap::iterator DefMI = Defs.find(SrcReg);
83       if (DefMI != Defs.end()) {
84         Domain = DefMI->second->getDesc().TSFlags & ARMII::DomainMask;
85         // Instructions in general domain are subreg accesses.
86         // Map them to NEON reg-reg moves.
87         if (Domain == ARMII::DomainGeneral)
88           Domain = ARMII::DomainNEON;
89       }
90
91       if (inNEONDomain(Domain, isA8)) {
92         // Convert VMOVD to VORRd
93         unsigned DestReg = MI->getOperand(0).getReg();
94
95         DEBUG({errs() << "vmov convert: "; MI->dump();});
96
97         // We need to preserve imp-defs / imp-uses here. Following passes may
98         // use the register scavenger to update liveness.
99         MachineInstr *NewMI =
100           AddDefaultPred(BuildMI(MBB, *MI, MI->getDebugLoc(),
101                                  TII->get(ARM::VORRd), DestReg)
102                          .addReg(SrcReg).addReg(SrcReg));
103         TransferImpOps(*MI, *NewMI);
104         MBB.erase(MI);
105         MI = NewMI;
106
107         DEBUG({errs() << "        into: "; MI->dump();});
108
109         Modified = true;
110         ++NumVMovs;
111       } else {
112         assert((Domain & ARMII::DomainVFP) && "Invalid domain!");
113         // Do nothing.
114       }
115     }
116
117     // Update def information.
118     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
119       const MachineOperand& MO = MI->getOperand(i);
120       if (!MO.isReg() || !MO.isDef())
121         continue;
122       unsigned MOReg = MO.getReg();
123
124       Defs[MOReg] = MI;
125       // Catch aliases as well.
126       for (const unsigned *R = TRI->getAliasSet(MOReg); *R; ++R)
127         Defs[*R] = MI;
128     }
129   }
130
131   return Modified;
132 }
133
134 bool NEONMoveFixPass::runOnMachineFunction(MachineFunction &Fn) {
135   ARMFunctionInfo *AFI = Fn.getInfo<ARMFunctionInfo>();
136   const TargetMachine &TM = Fn.getTarget();
137
138   if (AFI->isThumb1OnlyFunction())
139     return false;
140
141   TRI = TM.getRegisterInfo();
142   TII = static_cast<const ARMBaseInstrInfo*>(TM.getInstrInfo());
143   isA8 = TM.getSubtarget<ARMSubtarget>().isCortexA8();
144
145   bool Modified = false;
146   for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
147        ++MFI) {
148     MachineBasicBlock &MBB = *MFI;
149     Modified |= InsertMoves(MBB);
150   }
151
152   return Modified;
153 }
154
155 /// createNEONMoveFixPass - Returns an instance of the NEON reg-reg moves fix
156 /// pass.
157 FunctionPass *llvm::createNEONMoveFixPass() {
158   return new NEONMoveFixPass();
159 }