blob: d9c69f5f912325a564fd8ba69d80276a77cf1679 [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
Chris Lattner120b4d02010-05-21 18:02:42 +000074 switch (RegClass->getID()) {
75 default: break;
Chris Lattner95621192010-05-21 18:01:24 +000076 case X86::RFP32RegClassID:
77 case X86::RFP64RegClassID:
78 case X86::RFP80RegClassID:
Chris Lattner120b4d02010-05-21 18:02:42 +000079 return true;
80 }
Chris Lattnera92785d2010-05-21 17:57:03 +000081 }
82 }
83
84 // Check PHI nodes in successor blocks. These PHI's will be lowered to have
85 // a copy of the input value in this block. In SSE mode, we only care about
86 // 80-bit values.
87
88 // Final check, check LLVM BB's that are successors to the LLVM BB
89 // corresponding to BB for FP PHI nodes.
90 const BasicBlock *LLVMBB = MBB->getBasicBlock();
91 for (succ_const_iterator SI = succ_begin(LLVMBB), E = succ_end(LLVMBB);
92 SI != E; ++SI) {
93 const PHINode *PN;
94 for (BasicBlock::const_iterator II = SI->begin();
95 (PN = dyn_cast<PHINode>(II)); ++II) {
96 if (PN->getType()->isX86_FP80Ty() ||
97 (SSELevel == 0 && PN->getType()->isFloatingPointTy()) ||
98 (SSELevel < 2 && PN->getType()->isDoubleTy())) {
99 return true;
100 }
101 }
102 }
103
104 return false;
105}
106
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000107bool FPRegKiller::runOnMachineFunction(MachineFunction &MF) {
108 // If we are emitting FP stack code, scan the basic block to determine if this
109 // block defines any FP values. If so, put an FP_REG_KILL instruction before
110 // the terminator of the block.
111
112 // Note that FP stack instructions are used in all modes for long double,
113 // so we always need to do this check.
114 // Also note that it's possible for an FP stack register to be live across
115 // an instruction that produces multiple basic blocks (SSE CMOV) so we
116 // must check all the generated basic blocks.
117
118 // Scan all of the machine instructions in these MBBs, checking for FP
119 // stores. (RFP32 and RFP64 will not exist in SSE mode, but RFP80 might.)
120
121 // Fast-path: If nothing is using the x87 registers, we don't need to do
122 // any scanning.
123 MachineRegisterInfo &MRI = MF.getRegInfo();
124 if (MRI.getRegClassVirtRegs(X86::RFP80RegisterClass).empty() &&
125 MRI.getRegClassVirtRegs(X86::RFP64RegisterClass).empty() &&
126 MRI.getRegClassVirtRegs(X86::RFP32RegisterClass).empty())
127 return false;
128
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000129 const X86Subtarget &Subtarget = MF.getTarget().getSubtarget<X86Subtarget>();
Chris Lattnera92785d2010-05-21 17:57:03 +0000130 unsigned SSELevel = 0;
131 if (Subtarget.hasSSE2())
132 SSELevel = 2;
133 else if (Subtarget.hasSSE1())
134 SSELevel = 1;
135
136 bool Changed = false;
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000137 MachineFunction::iterator MBBI = MF.begin();
138 MachineFunction::iterator EndMBB = MF.end();
139 for (; MBBI != EndMBB; ++MBBI) {
140 MachineBasicBlock *MBB = MBBI;
141
142 // If this block returns, ignore it. We don't want to insert an FP_REG_KILL
143 // before the return.
144 if (!MBB->empty()) {
145 MachineBasicBlock::iterator EndI = MBB->end();
146 --EndI;
147 if (EndI->getDesc().isReturn())
148 continue;
149 }
150
Chris Lattnera92785d2010-05-21 17:57:03 +0000151 // If we find any FP stack code, emit the FP_REG_KILL instruction.
152 if (ContainsFPStackCode(MBB, SSELevel, MRI)) {
Chris Lattnerd2c680b2010-04-02 20:16:16 +0000153 BuildMI(*MBB, MBBI->getFirstTerminator(), DebugLoc(),
Dan Gohmanfd6722c2008-11-12 22:55:05 +0000154 MF.getTarget().getInstrInfo()->get(X86::FP_REG_KILL));
155 ++NumFPKill;
156 Changed = true;
157 }
158 }
159
160 return Changed;
161}