blob: 34f45fd8cc294f3a194ba5c0dd3025f37489a8a5 [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) {
60
61 for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
62 I != E; ++I) {
63 if (I->getNumOperands() != 0 && I->getOperand(0).isReg()) {
64 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) {
65 if (I->getOperand(op).isReg() && I->getOperand(op).isDef() &&
66 TargetRegisterInfo::isVirtualRegister(I->getOperand(op).getReg())) {
67 const TargetRegisterClass *RegClass =
68 MRI.getRegClass(I->getOperand(op).getReg());
69
70 if (RegClass == X86::RFP32RegisterClass ||
71 RegClass == X86::RFP64RegisterClass ||
72 RegClass == X86::RFP80RegisterClass)
73 return true;
74 }
75 }
76 }
77 }
78
79 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
80 // a copy of the input value in this block. In SSE mode, we only care about
81 // 80-bit values.
82
83 // Final check, check LLVM BB's that are successors to the LLVM BB
84 // corresponding to BB for FP PHI nodes.
85 const BasicBlock *LLVMBB = MBB->getBasicBlock();
86 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
87 SI != E; ++SI) {
88 const PHINode *PN;
89 for (BasicBlock::const_iterator II = SI->begin();
90 (PN = dyn_cast<PHINode>(II)); ++II) {
91 if (PN->getType()->isX86_FP80Ty() ||
92 (SSELevel == 0 && PN->getType()->isFloatingPointTy()) ||
93 (SSELevel < 2 && PN->getType()->isDoubleTy())) {
94 return true;
95 }
96 }
97 }
98
99 return false;
100}
101
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000102bool FPRegKiller::runOnMachineFunction(MachineFunction &MF) {
103 // If we are emitting FP stack code, scan the basic block to determine if this
104 // block defines any FP values. If so, put an FP_REG_KILL instruction before
105 // the terminator of the block.
106
107 // Note that FP stack instructions are used in all modes for long double,
108 // so we always need to do this check.
109 // Also note that it's possible for an FP stack register to be live across
110 // an instruction that produces multiple basic blocks (SSE CMOV) so we
111 // must check all the generated basic blocks.
112
113 // Scan all of the machine instructions in these MBBs, checking for FP
114 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
115
116 // Fast-path: If nothing is using the x87 registers, we don't need to do
117 // any scanning.
118 MachineRegisterInfo &MRI = MF.getRegInfo();
119 if (MRI.getRegClassVirtRegs(X86::RFP80RegisterClass).empty() &&
120 MRI.getRegClassVirtRegs(X86::RFP64RegisterClass).empty() &&
121 MRI.getRegClassVirtRegs(X86::RFP32RegisterClass).empty())
122 return false;
123
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000124 const X86Subtarget &Subtarget = MF.getTarget().getSubtarget<X86Subtarget>();
Chris Lattnera92785d2010-05-21 17:57:03 +0000125 unsigned SSELevel = 0;
126 if (Subtarget.hasSSE2())
127 SSELevel = 2;
128 else if (Subtarget.hasSSE1())
129 SSELevel = 1;
130
131 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.
147 if (ContainsFPStackCode(MBB, SSELevel, 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}