]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/lib/Target/ARM/NEONMoveFix.cpp
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / 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   char NEONMoveFixPass::ID = 0;
45 }
46
47 static bool inNEONDomain(unsigned Domain, bool isA8) {
48   return (Domain & ARMII::DomainNEON) ||
49     (isA8 && (Domain & ARMII::DomainNEONA8));
50 }
51
52 bool NEONMoveFixPass::InsertMoves(MachineBasicBlock &MBB) {
53   RegMap Defs;
54   bool Modified = false;
55
56   // Walk over MBB tracking the def points of the registers.
57   MachineBasicBlock::iterator MII = MBB.begin(), E = MBB.end();
58   MachineBasicBlock::iterator NextMII;
59   for (; MII != E; MII = NextMII) {
60     NextMII = llvm::next(MII);
61     MachineInstr *MI = &*MII;
62
63     if (MI->getOpcode() == ARM::VMOVD &&
64         !TII->isPredicated(MI)) {
65       unsigned SrcReg = MI->getOperand(1).getReg();
66       // If we do not find an instruction defining the reg, this means the
67       // register should be live-in for this BB. It's always to better to use
68       // NEON reg-reg moves.
69       unsigned Domain = ARMII::DomainNEON;
70       RegMap::iterator DefMI = Defs.find(SrcReg);
71       if (DefMI != Defs.end()) {
72         Domain = DefMI->second->getDesc().TSFlags & ARMII::DomainMask;
73         // Instructions in general domain are subreg accesses.
74         // Map them to NEON reg-reg moves.
75         if (Domain == ARMII::DomainGeneral)
76           Domain = ARMII::DomainNEON;
77       }
78
79       if (inNEONDomain(Domain, isA8)) {
80         // Convert VMOVD to VORRd
81         unsigned DestReg = MI->getOperand(0).getReg();
82
83         DEBUG({errs() << "vmov convert: "; MI->dump();});
84
85         // It's safe to ignore imp-defs / imp-uses here, since:
86         //  - We're running late, no intelligent condegen passes should be run
87         //    afterwards
88         //  - The imp-defs / imp-uses are superregs only, we don't care about
89         //    them.
90         AddDefaultPred(BuildMI(MBB, *MI, MI->getDebugLoc(),
91                              TII->get(ARM::VORRd), DestReg)
92           .addReg(SrcReg).addReg(SrcReg));
93         MBB.erase(MI);
94         MachineBasicBlock::iterator I = prior(NextMII);
95         MI = &*I;
96
97         DEBUG({errs() << "        into: "; MI->dump();});
98
99         Modified = true;
100         ++NumVMovs;
101       } else {
102         assert((Domain & ARMII::DomainVFP) && "Invalid domain!");
103         // Do nothing.
104       }
105     }
106
107     // Update def information.
108     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
109       const MachineOperand& MO = MI->getOperand(i);
110       if (!MO.isReg() || !MO.isDef())
111         continue;
112       unsigned MOReg = MO.getReg();
113
114       Defs[MOReg] = MI;
115       // Catch aliases as well.
116       for (const unsigned *R = TRI->getAliasSet(MOReg); *R; ++R)
117         Defs[*R] = MI;
118     }
119   }
120
121   return Modified;
122 }
123
124 bool NEONMoveFixPass::runOnMachineFunction(MachineFunction &Fn) {
125   ARMFunctionInfo *AFI = Fn.getInfo<ARMFunctionInfo>();
126   const TargetMachine &TM = Fn.getTarget();
127
128   if (AFI->isThumb1OnlyFunction())
129     return false;
130
131   TRI = TM.getRegisterInfo();
132   TII = static_cast<const ARMBaseInstrInfo*>(TM.getInstrInfo());
133   isA8 = TM.getSubtarget<ARMSubtarget>().isCortexA8();
134
135   bool Modified = false;
136   for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
137        ++MFI) {
138     MachineBasicBlock &MBB = *MFI;
139     Modified |= InsertMoves(MBB);
140   }
141
142   return Modified;
143 }
144
145 /// createNEONMoveFixPass - Returns an instance of the NEON reg-reg moves fix
146 /// pass.
147 FunctionPass *llvm::createNEONMoveFixPass() {
148   return new NEONMoveFixPass();
149 }