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" |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 17 | #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 Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CFG.h" |
| 25 | #include "llvm/ADT/Statistic.h" |
| 26 | using namespace llvm; |
| 27 | |
| 28 | STATISTIC(NumFPKill, "Number of FP_REG_KILL instructions added"); |
| 29 | |
| 30 | namespace { |
Nick Lewycky | 492d06e | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 31 | struct FPRegKiller : public MachineFunctionPass { |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 32 | static char ID; |
| 33 | FPRegKiller() : MachineFunctionPass(&ID) {} |
| 34 | |
| 35 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 15468d7 | 2009-08-01 00:26:16 +0000 | [diff] [blame] | 36 | AU.setPreservesCFG(); |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 37 | AU.addPreservedID(MachineLoopInfoID); |
| 38 | AU.addPreservedID(MachineDominatorsID); |
| 39 | MachineFunctionPass::getAnalysisUsage(AU); |
| 40 | } |
| 41 | |
| 42 | virtual bool runOnMachineFunction(MachineFunction &MF); |
| 43 | |
Chris Lattner | 413b45b | 2010-05-21 17:49:07 +0000 | [diff] [blame] | 44 | virtual const char *getPassName() const { |
| 45 | return "X86 FP_REG_KILL inserter"; |
| 46 | } |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 47 | }; |
| 48 | char FPRegKiller::ID = 0; |
| 49 | } |
| 50 | |
Chris Lattner | 413b45b | 2010-05-21 17:49:07 +0000 | [diff] [blame] | 51 | FunctionPass *llvm::createX87FPRegKillInserterPass() { |
| 52 | return new FPRegKiller(); |
| 53 | } |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 6c1fdb7 | 2010-05-21 18:17:54 +0000 | [diff] [blame] | 55 | /// isFPStackVReg - Return true if the specified vreg is from a fp stack |
| 56 | /// register class. |
| 57 | static 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 Lattner | a92785d | 2010-05-21 17:57:03 +0000 | [diff] [blame] | 71 | /// ContainsFPStackCode - Return true if the specific MBB has floating point |
| 72 | /// stack code, and thus needs an FP_REG_KILL. |
Chris Lattner | 6c1fdb7 | 2010-05-21 18:17:54 +0000 | [diff] [blame] | 73 | static bool ContainsFPStackCode(MachineBasicBlock *MBB, |
| 74 | const MachineRegisterInfo &MRI) { |
Chris Lattner | 9562119 | 2010-05-21 18:01:24 +0000 | [diff] [blame] | 75 | // Scan the block, looking for instructions that define fp stack vregs. |
Chris Lattner | a92785d | 2010-05-21 17:57:03 +0000 | [diff] [blame] | 76 | for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); |
| 77 | I != E; ++I) { |
Chris Lattner | 9562119 | 2010-05-21 18:01:24 +0000 | [diff] [blame] | 78 | if (I->getNumOperands() == 0 || !I->getOperand(0).isReg()) |
| 79 | continue; |
| 80 | |
| 81 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) { |
Chris Lattner | 6c1fdb7 | 2010-05-21 18:17:54 +0000 | [diff] [blame] | 82 | if (!I->getOperand(op).isReg() || !I->getOperand(op).isDef()) |
Chris Lattner | 9562119 | 2010-05-21 18:01:24 +0000 | [diff] [blame] | 83 | continue; |
| 84 | |
Chris Lattner | 6c1fdb7 | 2010-05-21 18:17:54 +0000 | [diff] [blame] | 85 | if (isFPStackVReg(I->getOperand(op).getReg(), MRI)) |
Chris Lattner | 120b4d0 | 2010-05-21 18:02:42 +0000 | [diff] [blame] | 86 | return true; |
Chris Lattner | a92785d | 2010-05-21 17:57:03 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | // Check PHI nodes in successor blocks. These PHI's will be lowered to have |
Chris Lattner | 6c1fdb7 | 2010-05-21 18:17:54 +0000 | [diff] [blame] | 91 | // 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 Lattner | a92785d | 2010-05-21 17:57:03 +0000 | [diff] [blame] | 102 | return true; |
Chris Lattner | a92785d | 2010-05-21 17:57:03 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | |
| 106 | return false; |
| 107 | } |
| 108 | |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 109 | bool 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 Lattner | 6c1fdb7 | 2010-05-21 18:17:54 +0000 | [diff] [blame] | 125 | const MachineRegisterInfo &MRI = MF.getRegInfo(); |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 126 | if (MRI.getRegClassVirtRegs(X86::RFP80RegisterClass).empty() && |
| 127 | MRI.getRegClassVirtRegs(X86::RFP64RegisterClass).empty() && |
| 128 | MRI.getRegClassVirtRegs(X86::RFP32RegisterClass).empty()) |
| 129 | return false; |
| 130 | |
Chris Lattner | a92785d | 2010-05-21 17:57:03 +0000 | [diff] [blame] | 131 | bool Changed = false; |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 132 | 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 Lattner | a92785d | 2010-05-21 17:57:03 +0000 | [diff] [blame] | 146 | // If we find any FP stack code, emit the FP_REG_KILL instruction. |
Chris Lattner | 6c1fdb7 | 2010-05-21 18:17:54 +0000 | [diff] [blame] | 147 | if (ContainsFPStackCode(MBB, MRI)) { |
Chris Lattner | d2c680b | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 148 | BuildMI(*MBB, MBBI->getFirstTerminator(), DebugLoc(), |
Dan Gohman | fd6722c | 2008-11-12 22:55:05 +0000 | [diff] [blame] | 149 | MF.getTarget().getInstrInfo()->get(X86::FP_REG_KILL)); |
| 150 | ++NumFPKill; |
| 151 | Changed = true; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | return Changed; |
| 156 | } |