Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 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" |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/CFG.h" |
| 26 | #include "llvm/ADT/Statistic.h" |
| 27 | using namespace llvm; |
| 28 | |
| 29 | STATISTIC(NumFPKill, "Number of FP_REG_KILL instructions added"); |
| 30 | |
| 31 | namespace { |
Nick Lewycky | 492d06e | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 32 | struct FPRegKiller : public MachineFunctionPass { |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 33 | static char ID; |
| 34 | FPRegKiller() : MachineFunctionPass(&ID) {} |
| 35 | |
| 36 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 15468d7 | 2009-08-01 00:26:16 +0000 | [diff] [blame] | 37 | AU.setPreservesCFG(); |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 38 | AU.addPreservedID(MachineLoopInfoID); |
| 39 | AU.addPreservedID(MachineDominatorsID); |
| 40 | MachineFunctionPass::getAnalysisUsage(AU); |
| 41 | } |
| 42 | |
| 43 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 44 | |
Chris Lattner | 413b45b | 2010-05-21 17:49:07 +0000 | [diff] [blame] | 45 | virtual const char *getPassName() const { |
| 46 | return "X86 FP_REG_KILL inserter"; |
| 47 | } |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 48 | }; |
| 49 | char FPRegKiller::ID = 0; |
| 50 | } |
| 51 | |
Chris Lattner | 413b45b | 2010-05-21 17:49:07 +0000 | [diff] [blame] | 52 | FunctionPass *llvm::createX87FPRegKillInserterPass() { |
| 53 | return new FPRegKiller(); |
| 54 | } |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 55 | |
Chris Lattner | a92785d | 2010-05-21 17:57:03 +0000 | [diff] [blame] | 56 | /// ContainsFPStackCode - Return true if the specific MBB has floating point |
| 57 | /// stack code, and thus needs an FP_REG_KILL. |
| 58 | static bool ContainsFPStackCode(MachineBasicBlock *MBB, unsigned SSELevel, |
| 59 | MachineRegisterInfo &MRI) { |
Chris Lattner | 9562119 | 2010-05-21 18:01:24 +0000 | [diff] [blame^] | 60 | // Scan the block, looking for instructions that define fp stack vregs. |
Chris Lattner | a92785d | 2010-05-21 17:57:03 +0000 | [diff] [blame] | 61 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
| 62 | I != E; ++I) { |
Chris Lattner | 9562119 | 2010-05-21 18:01:24 +0000 | [diff] [blame^] | 63 | 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 Lattner | a92785d | 2010-05-21 17:57:03 +0000 | [diff] [blame] | 79 | } |
| 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 Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 105 | bool 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 Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 127 | const X86Subtarget &Subtarget = MF.getTarget().getSubtarget<X86Subtarget>(); |
Chris Lattner | a92785d | 2010-05-21 17:57:03 +0000 | [diff] [blame] | 128 | unsigned SSELevel = 0; |
| 129 | if (Subtarget.hasSSE2()) |
| 130 | SSELevel = 2; |
| 131 | else if (Subtarget.hasSSE1()) |
| 132 | SSELevel = 1; |
| 133 | |
| 134 | bool Changed = false; |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 135 | 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 Lattner | a92785d | 2010-05-21 17:57:03 +0000 | [diff] [blame] | 149 | // If we find any FP stack code, emit the FP_REG_KILL instruction. |
| 150 | if (ContainsFPStackCode(MBB, SSELevel, MRI)) { |
Chris Lattner | d2c680b | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 151 | BuildMI(*MBB, MBBI->getFirstTerminator(), DebugLoc(), |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 152 | MF.getTarget().getInstrInfo()->get(X86::FP_REG_KILL)); |
| 153 | ++NumFPKill; |
| 154 | Changed = true; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return Changed; |
| 159 | } |