blob: 97a43f2b95de2cffd7a18eb58f5022583275cd71 [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"
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"
Dan Gohmanfd6722c2008-11-12 22:55:05 +000025#include "llvm/Support/CFG.h"
26#include "llvm/ADT/Statistic.h"
27using namespace llvm;
28
29STATISTIC(NumFPKill, "Number of FP_REG_KILL instructions added");
30
31namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000032 struct FPRegKiller : public MachineFunctionPass {
Dan Gohmanfd6722c2008-11-12 22:55:05 +000033 static char ID;
34 FPRegKiller() : MachineFunctionPass(&ID) {}
35
36 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Dan Gohman15468d72009-08-01 00:26:16 +000037 AU.setPreservesCFG();
Dan Gohmanfd6722c2008-11-12 22:55:05 +000038 AU.addPreservedID(MachineLoopInfoID);
39 AU.addPreservedID(MachineDominatorsID);
40 MachineFunctionPass::getAnalysisUsage(AU);
41 }
42
43 virtual bool runOnMachineFunction(MachineFunction &MF);
44
Chris Lattner413b45b2010-05-21 17:49:07 +000045 virtual const char *getPassName() const {
46 return "X86 FP_REG_KILL inserter";
47 }
Dan Gohmanfd6722c2008-11-12 22:55:05 +000048 };
49 char FPRegKiller::ID = 0;
50}
51
Chris Lattner413b45b2010-05-21 17:49:07 +000052FunctionPass *llvm::createX87FPRegKillInserterPass() {
53 return new FPRegKiller();
54}
Dan Gohmanfd6722c2008-11-12 22:55:05 +000055
Chris Lattnera92785d2010-05-21 17:57:03 +000056/// ContainsFPStackCode - Return true if the specific MBB has floating point
57/// stack code, and thus needs an FP_REG_KILL.
58static bool ContainsFPStackCode(MachineBasicBlock *MBB, unsigned SSELevel,
59 MachineRegisterInfo &MRI) {
Chris Lattner95621192010-05-21 18:01:24 +000060 // Scan the block, looking for instructions that define fp stack vregs.
Chris Lattnera92785d2010-05-21 17:57:03 +000061 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
62 I != E; ++I) {
Chris Lattner95621192010-05-21 18:01:24 +000063 if (I->getNumOperands() == 0 || !I->getOperand(0).isReg())
64 continue;
65
66 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
67 if (!I->getOperand(op).isReg() || !I->getOperand(op).isDef() ||
68 !TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg()))
69 continue;
70
71 const TargetRegisterClass *RegClass =
72 MRI.getRegClass(I->getOperand(op).getReg());
73
74 switch (RegClass->getID())
75 case X86::RFP32RegClassID:
76 case X86::RFP64RegClassID:
77 case X86::RFP80RegClassID:
78 return true;
Chris Lattnera92785d2010-05-21 17:57:03 +000079 }
80 }
81
82 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
83 // a copy of the input value in this block. In SSE mode, we only care about
84 // 80-bit values.
85
86 // Final check, check LLVM BB's that are successors to the LLVM BB
87 // corresponding to BB for FP PHI nodes.
88 const BasicBlock *LLVMBB = MBB->getBasicBlock();
89 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
90 SI != E; ++SI) {
91 const PHINode *PN;
92 for (BasicBlock::const_iterator II = SI->begin();
93 (PN = dyn_cast<PHINode>(II)); ++II) {
94 if (PN->getType()->isX86_FP80Ty() ||
95 (SSELevel == 0 && PN->getType()->isFloatingPointTy()) ||
96 (SSELevel < 2 && PN->getType()->isDoubleTy())) {
97 return true;
98 }
99 }
100 }
101
102 return false;
103}
104
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000105bool FPRegKiller::runOnMachineFunction(MachineFunction &MF) {
106 // If we are emitting FP stack code, scan the basic block to determine if this
107 // block defines any FP values. If so, put an FP_REG_KILL instruction before
108 // the terminator of the block.
109
110 // Note that FP stack instructions are used in all modes for long double,
111 // so we always need to do this check.
112 // Also note that it's possible for an FP stack register to be live across
113 // an instruction that produces multiple basic blocks (SSE CMOV) so we
114 // must check all the generated basic blocks.
115
116 // Scan all of the machine instructions in these MBBs, checking for FP
117 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
118
119 // Fast-path: If nothing is using the x87 registers, we don't need to do
120 // any scanning.
121 MachineRegisterInfo &MRI = MF.getRegInfo();
122 if (MRI.getRegClassVirtRegs(X86::RFP80RegisterClass).empty() &&
123 MRI.getRegClassVirtRegs(X86::RFP64RegisterClass).empty() &&
124 MRI.getRegClassVirtRegs(X86::RFP32RegisterClass).empty())
125 return false;
126
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000127 const X86Subtarget &Subtarget = MF.getTarget().getSubtarget<X86Subtarget>();
Chris Lattnera92785d2010-05-21 17:57:03 +0000128 unsigned SSELevel = 0;
129 if (Subtarget.hasSSE2())
130 SSELevel = 2;
131 else if (Subtarget.hasSSE1())
132 SSELevel = 1;
133
134 bool Changed = false;
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000135 MachineFunction::iterator MBBI = MF.begin();
136 MachineFunction::iterator EndMBB = MF.end();
137 for (; MBBI != EndMBB; ++MBBI) {
138 MachineBasicBlock *MBB = MBBI;
139
140 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
141 // before the return.
142 if (!MBB->empty()) {
143 MachineBasicBlock::iterator EndI = MBB->end();
144 --EndI;
145 if (EndI->getDesc().isReturn())
146 continue;
147 }
148
Chris Lattnera92785d2010-05-21 17:57:03 +0000149 // If we find any FP stack code, emit the FP_REG_KILL instruction.
150 if (ContainsFPStackCode(MBB, SSELevel, MRI)) {
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000151 BuildMI(*MBB, MBBI->getFirstTerminator(), DebugLoc(),
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000152 MF.getTarget().getInstrInfo()->get(X86::FP_REG_KILL));
153 ++NumFPKill;
154 Changed = true;
155 }
156 }
157
158 return Changed;
159}