X86 floating-point passes don't modify the CFG.
[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 "X86Subtarget.h"
18 #include "llvm/Instructions.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/Passes.h"
23 #include "llvm/Target/TargetMachine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/CFG.h"
27 #include "llvm/ADT/Statistic.h"
28 using namespace llvm;
29
30 STATISTIC(NumFPKill, "Number of FP_REG_KILL instructions added");
31
32 namespace {
33   struct VISIBILITY_HIDDEN FPRegKiller : public MachineFunctionPass {
34     static char ID;
35     FPRegKiller() : MachineFunctionPass(&ID) {}
36
37     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
38       AU.setPreservesCFG();
39       AU.addPreservedID(MachineLoopInfoID);
40       AU.addPreservedID(MachineDominatorsID);
41       MachineFunctionPass::getAnalysisUsage(AU);
42     }
43
44     virtual bool runOnMachineFunction(MachineFunction &MF);
45
46     virtual const char *getPassName() const { return "X86 FP_REG_KILL inserter"; }
47   };
48   char FPRegKiller::ID = 0;
49 }
50
51 FunctionPass *llvm::createX87FPRegKillInserterPass() { return new FPRegKiller(); }
52
53 bool FPRegKiller::runOnMachineFunction(MachineFunction &MF) {
54   // If we are emitting FP stack code, scan the basic block to determine if this
55   // block defines any FP values.  If so, put an FP_REG_KILL instruction before
56   // the terminator of the block.
57
58   // Note that FP stack instructions are used in all modes for long double,
59   // so we always need to do this check.
60   // Also note that it's possible for an FP stack register to be live across
61   // an instruction that produces multiple basic blocks (SSE CMOV) so we
62   // must check all the generated basic blocks.
63
64   // Scan all of the machine instructions in these MBBs, checking for FP
65   // stores.  (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
66
67   // Fast-path: If nothing is using the x87 registers, we don't need to do
68   // any scanning.
69   MachineRegisterInfo &MRI = MF.getRegInfo();
70   if (MRI.getRegClassVirtRegs(X86::RFP80RegisterClass).empty() &&
71       MRI.getRegClassVirtRegs(X86::RFP64RegisterClass).empty() &&
72       MRI.getRegClassVirtRegs(X86::RFP32RegisterClass).empty())
73     return false;
74
75   bool Changed = false;
76   const X86Subtarget &Subtarget = MF.getTarget().getSubtarget<X86Subtarget>();
77   MachineFunction::iterator MBBI = MF.begin();
78   MachineFunction::iterator EndMBB = MF.end();
79   for (; MBBI != EndMBB; ++MBBI) {
80     MachineBasicBlock *MBB = MBBI;
81     
82     // If this block returns, ignore it.  We don't want to insert an FP_REG_KILL
83     // before the return.
84     if (!MBB->empty()) {
85       MachineBasicBlock::iterator EndI = MBB->end();
86       --EndI;
87       if (EndI->getDesc().isReturn())
88         continue;
89     }
90     
91     bool ContainsFPCode = false;
92     for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
93          !ContainsFPCode && I != E; ++I) {
94       if (I->getNumOperands() != 0 && I->getOperand(0).isReg()) {
95         const TargetRegisterClass *clas;
96         for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
97           if (I->getOperand(op).isReg() && I->getOperand(op).isDef() &&
98             TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()) &&
99               ((clas = MRI.getRegClass(I->getOperand(op).getReg())) == 
100                  X86::RFP32RegisterClass ||
101                clas == X86::RFP64RegisterClass ||
102                clas == X86::RFP80RegisterClass)) {
103             ContainsFPCode = true;
104             break;
105           }
106         }
107       }
108     }
109     // Check PHI nodes in successor blocks.  These PHI's will be lowered to have
110     // a copy of the input value in this block.  In SSE mode, we only care about
111     // 80-bit values.
112     if (!ContainsFPCode) {
113       // Final check, check LLVM BB's that are successors to the LLVM BB
114       // corresponding to BB for FP PHI nodes.
115       const BasicBlock *LLVMBB = MBB->getBasicBlock();
116       const PHINode *PN;
117       for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
118            !ContainsFPCode && SI != E; ++SI) {
119         for (BasicBlock::const_iterator II = SI->begin();
120              (PN = dyn_cast<PHINode>(II)); ++II) {
121           if (PN->getType()==Type::X86_FP80Ty ||
122               (!Subtarget.hasSSE1() && PN->getType()->isFloatingPoint()) ||
123               (!Subtarget.hasSSE2() && PN->getType()==Type::DoubleTy)) {
124             ContainsFPCode = true;
125             break;
126           }
127         }
128       }
129     }
130     // Finally, if we found any FP code, emit the FP_REG_KILL instruction.
131     if (ContainsFPCode) {
132       BuildMI(*MBB, MBBI->getFirstTerminator(), DebugLoc::getUnknownLoc(),
133               MF.getTarget().getInstrInfo()->get(X86::FP_REG_KILL));
134       ++NumFPKill;
135       Changed = true;
136     }
137   }
138
139   return Changed;
140 }