blob: 747683dcc412805f88adba2ab189aa6f8114aee1 [file] [log] [blame]
Dan Gohmanfd6722c2008-11-12 22:55:05 +00001//===-- 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"
Dan Gohmanfd6722c2008-11-12 22:55:05 +000017#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"
Dan Gohmanfd6722c2008-11-12 22:55:05 +000024#include "llvm/Support/CFG.h"
25#include "llvm/ADT/Statistic.h"
26using namespace llvm;
27
28STATISTIC(NumFPKill, "Number of FP_REG_KILL instructions added");
29
30namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000031 struct FPRegKiller : public MachineFunctionPass {
Dan Gohmanfd6722c2008-11-12 22:55:05 +000032 static char ID;
33 FPRegKiller() : MachineFunctionPass(&ID) {}
34
35 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman15468d72009-08-01 00:26:16 +000036 AU.setPreservesCFG();
Dan Gohmanfd6722c2008-11-12 22:55:05 +000037 AU.addPreservedID(MachineLoopInfoID);
38 AU.addPreservedID(MachineDominatorsID);
39 MachineFunctionPass::getAnalysisUsage(AU);
40 }
41
42 virtual bool runOnMachineFunction(MachineFunction &MF);
43
Chris Lattner413b45b2010-05-21 17:49:07 +000044 virtual const char *getPassName() const {
45 return "X86 FP_REG_KILL inserter";
46 }
Dan Gohmanfd6722c2008-11-12 22:55:05 +000047 };
48 char FPRegKiller::ID = 0;
49}
50
Chris Lattner413b45b2010-05-21 17:49:07 +000051FunctionPass *llvm::createX87FPRegKillInserterPass() {
52 return new FPRegKiller();
53}
Dan Gohmanfd6722c2008-11-12 22:55:05 +000054
Chris Lattner6c1fdb72010-05-21 18:17:54 +000055/// isFPStackVReg - Return true if the specified vreg is from a fp stack
56/// register class.
57static 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
Chris Lattnera92785d2010-05-21 17:57:03 +000071/// ContainsFPStackCode - Return true if the specific MBB has floating point
72/// stack code, and thus needs an FP_REG_KILL.
Chris Lattner6c1fdb72010-05-21 18:17:54 +000073static bool ContainsFPStackCode(MachineBasicBlock *MBB,
74 const MachineRegisterInfo &MRI) {
Chris Lattner95621192010-05-21 18:01:24 +000075 // Scan the block, looking for instructions that define fp stack vregs.
Chris Lattnera92785d2010-05-21 17:57:03 +000076 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
77 I != E; ++I) {
Chris Lattner95621192010-05-21 18:01:24 +000078 if (I->getNumOperands() == 0 || !I->getOperand(0).isReg())
79 continue;
80
81 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
Chris Lattner6c1fdb72010-05-21 18:17:54 +000082 if (!I->getOperand(op).isReg() || !I->getOperand(op).isDef())
Chris Lattner95621192010-05-21 18:01:24 +000083 continue;
84
Chris Lattner6c1fdb72010-05-21 18:17:54 +000085 if (isFPStackVReg(I->getOperand(op).getReg(), MRI))
Chris Lattner120b4d02010-05-21 18:02:42 +000086 return true;
Chris Lattnera92785d2010-05-21 17:57:03 +000087 }
88 }
89
90 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
Chris Lattner6c1fdb72010-05-21 18:17:54 +000091 // 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))
Chris Lattnera92785d2010-05-21 17:57:03 +0000102 return true;
Chris Lattnera92785d2010-05-21 17:57:03 +0000103 }
104 }
105
106 return false;
107}
108
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000109bool 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.
Chris Lattner6c1fdb72010-05-21 18:17:54 +0000125 const MachineRegisterInfo &MRI = MF.getRegInfo();
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000126 if (MRI.getRegClassVirtRegs(X86::RFP80RegisterClass).empty() &&
127 MRI.getRegClassVirtRegs(X86::RFP64RegisterClass).empty() &&
128 MRI.getRegClassVirtRegs(X86::RFP32RegisterClass).empty())
129 return false;
130
Chris Lattnera92785d2010-05-21 17:57:03 +0000131 bool Changed = false;
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000132 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
Chris Lattnera92785d2010-05-21 17:57:03 +0000146 // If we find any FP stack code, emit the FP_REG_KILL instruction.
Chris Lattner6c1fdb72010-05-21 18:17:54 +0000147 if (ContainsFPStackCode(MBB, MRI)) {
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000148 BuildMI(*MBB, MBBI->getFirstTerminator(), DebugLoc(),
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000149 MF.getTarget().getInstrInfo()->get(X86::FP_REG_KILL));
150 ++NumFPKill;
151 Changed = true;
152 }
153 }
154
155 return Changed;
156}