Previous commit message should refer to 104308.
[oota-llvm.git] / lib / Target / X86 / X86FloatingPointRegKill.cpp
1 //===-- X86FloatingPoint.cpp - FP_REG_KILL inserter -----------------------===//
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 defines the pass which inserts FP_REG_KILL instructions.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "x86-codegen"
15 #include "X86.h"
16 #include "X86InstrInfo.h"
17 #include "llvm/Instructions.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/Passes.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/CFG.h"
25 #include "llvm/ADT/Statistic.h"
26 using namespace llvm;
27
28 STATISTIC(NumFPKill, "Number of FP_REG_KILL instructions added");
29
30 namespace {
31   struct FPRegKiller : public MachineFunctionPass {
32     static char ID;
33     FPRegKiller() : MachineFunctionPass(&ID) {}
34
35     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
36       AU.setPreservesCFG();
37       AU.addPreservedID(MachineLoopInfoID);
38       AU.addPreservedID(MachineDominatorsID);
39       MachineFunctionPass::getAnalysisUsage(AU);
40     }
41
42     virtual bool runOnMachineFunction(MachineFunction &MF);
43
44     virtual const char *getPassName() const {
45       return "X86 FP_REG_KILL inserter";
46     }
47   };
48   char FPRegKiller::ID = 0;
49 }
50
51 FunctionPass *llvm::createX87FPRegKillInserterPass() {
52   return new FPRegKiller();
53 }
54
55 /// isFPStackVReg - Return true if the specified vreg is from a fp stack
56 /// register class.
57 static bool isFPStackVReg(unsigned RegNo, const MachineRegisterInfo &MRI) {
58   if (!TargetRegisterInfo::isVirtualRegister(RegNo))
59     return false;
60   
61   switch (MRI.getRegClass(RegNo)->getID()) {
62   default: return false;
63   case X86::RFP32RegClassID:
64   case X86::RFP64RegClassID:
65   case X86::RFP80RegClassID:
66   return true;
67   }
68 }
69
70
71 /// ContainsFPStackCode - Return true if the specific MBB has floating point
72 /// stack code, and thus needs an FP_REG_KILL.
73 static bool ContainsFPStackCode(MachineBasicBlock *MBB,
74                                 const MachineRegisterInfo &MRI) {
75   // Scan the block, looking for instructions that define fp stack vregs.
76   for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
77        I != E; ++I) {
78     if (I->getNumOperands() == 0 || !I->getOperand(0).isReg())
79       continue;
80     
81     for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
82       if (!I->getOperand(op).isReg() || !I->getOperand(op).isDef())
83         continue;
84       
85       if (isFPStackVReg(I->getOperand(op).getReg(), MRI))
86         return true;
87     }
88   }
89   
90   // Check PHI nodes in successor blocks.  These PHI's will be lowered to have
91   // a copy of the input value in this block, which is a definition of the
92   // value.
93   for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
94        E = MBB->succ_end(); SI != E; ++ SI) {
95     MachineBasicBlock *SuccBB = *SI;
96     for (MachineBasicBlock::iterator I = SuccBB->begin(), E = SuccBB->end();
97          I != E; ++I) {
98       // All PHI nodes are at the top of the block.
99       if (!I->isPHI()) break;
100       
101       if (isFPStackVReg(I->getOperand(0).getReg(), MRI))
102         return true;
103     }
104   }
105   
106   return false;
107 }                                 
108
109 bool FPRegKiller::runOnMachineFunction(MachineFunction &MF) {
110   // If we are emitting FP stack code, scan the basic block to determine if this
111   // block defines any FP values.  If so, put an FP_REG_KILL instruction before
112   // the terminator of the block.
113
114   // Note that FP stack instructions are used in all modes for long double,
115   // so we always need to do this check.
116   // Also note that it's possible for an FP stack register to be live across
117   // an instruction that produces multiple basic blocks (SSE CMOV) so we
118   // must check all the generated basic blocks.
119
120   // Scan all of the machine instructions in these MBBs, checking for FP
121   // stores.  (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
122
123   // Fast-path: If nothing is using the x87 registers, we don't need to do
124   // any scanning.
125   const MachineRegisterInfo &MRI = MF.getRegInfo();
126   if (MRI.getRegClassVirtRegs(X86::RFP80RegisterClass).empty() &&
127       MRI.getRegClassVirtRegs(X86::RFP64RegisterClass).empty() &&
128       MRI.getRegClassVirtRegs(X86::RFP32RegisterClass).empty())
129     return false;
130
131   bool Changed = false;
132   MachineFunction::iterator MBBI = MF.begin();
133   MachineFunction::iterator EndMBB = MF.end();
134   for (; MBBI != EndMBB; ++MBBI) {
135     MachineBasicBlock *MBB = MBBI;
136     
137     // If this block returns, ignore it.  We don't want to insert an FP_REG_KILL
138     // before the return.
139     if (!MBB->empty()) {
140       MachineBasicBlock::iterator EndI = MBB->end();
141       --EndI;
142       if (EndI->getDesc().isReturn())
143         continue;
144     }
145     
146     // If we find any FP stack code, emit the FP_REG_KILL instruction.
147     if (ContainsFPStackCode(MBB, MRI)) {
148       BuildMI(*MBB, MBBI->getFirstTerminator(), DebugLoc(),
149               MF.getTarget().getInstrInfo()->get(X86::FP_REG_KILL));
150       ++NumFPKill;
151       Changed = true;
152     }
153   }
154
155   return Changed;
156 }