Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 1 | //===-- SIMachineFunctionInfo.cpp - SI Machine Function Info -------===// |
| 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 | /// \file |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | |
| 11 | |
| 12 | #include "SIMachineFunctionInfo.h" |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 13 | #include "SIInstrInfo.h" |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 14 | #include "SIRegisterInfo.h" |
| 15 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Function.h" |
| 17 | #include "llvm/IR/LLVMContext.h" |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 18 | |
| 19 | #define MAX_LANES 64 |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
Juergen Ributzka | d12ccbd | 2013-11-19 00:57:56 +0000 | [diff] [blame] | 23 | |
| 24 | // Pin the vtable to this file. |
| 25 | void SIMachineFunctionInfo::anchor() {} |
| 26 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 27 | SIMachineFunctionInfo::SIMachineFunctionInfo(const MachineFunction &MF) |
Vincent Lejeune | ace6f73 | 2013-04-01 21:47:53 +0000 | [diff] [blame] | 28 | : AMDGPUMachineFunction(MF), |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 29 | PSInputAddr(0), |
Tom Stellard | b02094e | 2014-07-21 15:45:01 +0000 | [diff] [blame] | 30 | SpillTracker(), |
| 31 | NumUserSGPRs(0) { } |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 32 | |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 33 | static unsigned createLaneVGPR(MachineRegisterInfo &MRI, MachineFunction *MF) { |
| 34 | unsigned VGPR = MRI.createVirtualRegister(&AMDGPU::VReg_32RegClass); |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 35 | |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 36 | // We need to add this register as live out for the function, in order to |
| 37 | // have the live range calculated directly. |
| 38 | // |
| 39 | // When register spilling begins, we have already calculated the live |
| 40 | // live intervals for all the registers. Since we are spilling SGPRs to |
| 41 | // VGPRs, we need to update the Lane VGPR's live interval every time we |
| 42 | // spill or restore a register. |
| 43 | // |
| 44 | // Unfortunately, there is no good way to update the live interval as |
| 45 | // the TargetInstrInfo callbacks for spilling and restoring don't give |
| 46 | // us access to the live interval information. |
| 47 | // |
| 48 | // We are lucky, though, because the InlineSpiller calls |
| 49 | // LiveRangeEdit::calculateRegClassAndHint() which iterates through |
| 50 | // all the new register that have been created when restoring a register |
| 51 | // and calls LiveIntervals::getInterval(), which creates and computes |
| 52 | // the live interval for the newly created register. However, once this |
| 53 | // live intervals is created, it doesn't change and since we usually reuse |
| 54 | // the Lane VGPR multiple times, this means any uses after the first aren't |
| 55 | // added to the live interval. |
| 56 | // |
| 57 | // To work around this, we add Lane VGPRs to the functions live out list, |
| 58 | // so that we can guarantee its live range will cover all of its uses. |
| 59 | |
| 60 | for (MachineBasicBlock &MBB : *MF) { |
| 61 | if (MBB.back().getOpcode() == AMDGPU::S_ENDPGM) { |
| 62 | MBB.back().addOperand(*MF, MachineOperand::CreateReg(VGPR, false, true)); |
| 63 | return VGPR; |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
Matt Arsenault | fda9dad | 2014-07-07 18:34:42 +0000 | [diff] [blame] | 66 | |
| 67 | LLVMContext &Ctx = MF->getFunction()->getContext(); |
| 68 | Ctx.emitError("Could not find S_ENDPGM instruction."); |
| 69 | |
Tom Stellard | eba6107 | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 70 | return VGPR; |
| 71 | } |
| 72 | |
| 73 | unsigned SIMachineFunctionInfo::RegSpillTracker::reserveLanes( |
| 74 | MachineRegisterInfo &MRI, MachineFunction *MF, unsigned NumRegs) { |
| 75 | unsigned StartLane = CurrentLane; |
| 76 | CurrentLane += NumRegs; |
| 77 | if (!LaneVGPR) { |
| 78 | LaneVGPR = createLaneVGPR(MRI, MF); |
| 79 | } else { |
| 80 | if (CurrentLane >= MAX_LANES) { |
| 81 | StartLane = CurrentLane = 0; |
| 82 | LaneVGPR = createLaneVGPR(MRI, MF); |
| 83 | } |
| 84 | } |
| 85 | return StartLane; |
Tom Stellard | c149dc0 | 2013-11-27 21:23:35 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void SIMachineFunctionInfo::RegSpillTracker::addSpilledReg(unsigned FrameIndex, |
| 89 | unsigned Reg, |
| 90 | int Lane) { |
| 91 | SpilledRegisters[FrameIndex] = SpilledReg(Reg, Lane); |
| 92 | } |
| 93 | |
| 94 | const SIMachineFunctionInfo::SpilledReg& |
| 95 | SIMachineFunctionInfo::RegSpillTracker::getSpilledReg(unsigned FrameIndex) { |
| 96 | return SpilledRegisters[FrameIndex]; |
| 97 | } |