44205955d92cddf9d8740cb0be9a0766776a0359
[oota-llvm.git] / lib / Target / X86 / SSEDomainFix.cpp
1 //===- SSEDomainFix.cpp - Use proper int/float domain for SSE ---*- 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 // This file contains the SSEDomainFix pass.
11 //
12 // Some SSE instructions like mov, and, or, xor are available in different
13 // variants for different operand types. These variant instructions are
14 // equivalent, but on Nehalem and newer cpus there is extra latency
15 // transferring data between integer and floating point domains.
16 //
17 // This pass changes the variant instructions to minimize domain crossings.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #define DEBUG_TYPE "sse-domain-fix"
22 #include "X86InstrInfo.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/ADT/DepthFirstIterator.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/raw_ostream.h"
28
29 using namespace llvm;
30
31 namespace {
32 class SSEDomainFixPass : public MachineFunctionPass {
33   static char ID;
34   const X86InstrInfo *TII;
35
36   MachineFunction *MF;
37   MachineBasicBlock *MBB;
38 public:
39   SSEDomainFixPass() : MachineFunctionPass(&ID) {}
40
41   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42     AU.setPreservesAll();
43     MachineFunctionPass::getAnalysisUsage(AU);
44   }
45
46   virtual bool runOnMachineFunction(MachineFunction &MF);
47
48   virtual const char *getPassName() const {
49     return "SSE execution domain fixup";
50   }
51
52 private:
53   void enterBasicBlock(MachineBasicBlock *MBB);
54 };
55 }
56
57 char SSEDomainFixPass::ID = 0;
58
59 void SSEDomainFixPass::enterBasicBlock(MachineBasicBlock *mbb) {
60   MBB = mbb;
61   DEBUG(dbgs() << "Entering MBB " << MBB->getName() << "\n");
62 }
63
64 bool SSEDomainFixPass::runOnMachineFunction(MachineFunction &mf) {
65   MF = &mf;
66   TII = static_cast<const X86InstrInfo*>(MF->getTarget().getInstrInfo());
67
68   // If no XMM registers are used in the function, we can skip it completely.
69   bool XMMIsUsed = false;
70   for (TargetRegisterClass::const_iterator I = X86::VR128RegClass.begin(),
71          E = X86::VR128RegClass.end(); I != E; ++I)
72     if (MF->getRegInfo().isPhysRegUsed(*I)) {
73       XMMIsUsed = true;
74       break;
75     }
76   if (!XMMIsUsed) return false;
77
78   MachineBasicBlock *Entry = MF->begin();
79   SmallPtrSet<MachineBasicBlock*, 16> Visited;
80   for (df_ext_iterator<MachineBasicBlock*,
81          SmallPtrSet<MachineBasicBlock*, 16> >
82          DFI = df_ext_begin(Entry, Visited), DFE = df_ext_end(Entry, Visited);
83        DFI != DFE; ++DFI) {
84     enterBasicBlock(*DFI);
85     for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
86         ++I) {
87       MachineInstr *MI = I;
88       const unsigned *equiv = 0;
89       X86InstrInfo::SSEDomain domain = TII->GetSSEDomain(MI, equiv);
90       (void) domain;
91       DEBUG(dbgs() << "-isd"[domain] << (equiv ? "* " : "  ") << *MI);
92     }
93   }
94   return false;
95 }
96
97 FunctionPass *llvm::createSSEDomainFixPass() {
98   return new SSEDomainFixPass();
99 }