1 //===-- SIFixSGPRLiveRanges.cpp - Fix SGPR live ranges ----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// SALU instructions ignore control flow, so we need to modify the live ranges
12 /// of the registers they define.
14 /// The strategy is to view the entire program as if it were a single basic
15 /// block and calculate the intervals accordingly. We implement this
16 /// by walking this list of segments for each LiveRange and setting the
17 /// end of each segment equal to the start of the segment that immediately
21 #include "SIRegisterInfo.h"
22 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/Support/Debug.h"
26 #include "llvm/Target/TargetMachine.h"
30 #define DEBUG_TYPE "si-fix-sgpr-live-ranges"
34 class SIFixSGPRLiveRanges : public MachineFunctionPass {
39 SIFixSGPRLiveRanges() : MachineFunctionPass(ID) {
40 initializeSIFixSGPRLiveRangesPass(*PassRegistry::getPassRegistry());
43 virtual bool runOnMachineFunction(MachineFunction &MF) override;
45 virtual const char *getPassName() const override {
46 return "SI Fix SGPR live ranges";
49 virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
50 AU.addRequired<LiveIntervals>();
51 AU.addPreserved<LiveIntervals>();
52 AU.addPreserved<SlotIndexes>();
54 MachineFunctionPass::getAnalysisUsage(AU);
58 } // End anonymous namespace.
60 INITIALIZE_PASS_BEGIN(SIFixSGPRLiveRanges, DEBUG_TYPE,
61 "SI Fix SGPR Live Ranges", false, false)
62 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
63 INITIALIZE_PASS_END(SIFixSGPRLiveRanges, DEBUG_TYPE,
64 "SI Fix SGPR Live Ranges", false, false)
66 char SIFixSGPRLiveRanges::ID = 0;
68 char &llvm::SIFixSGPRLiveRangesID = SIFixSGPRLiveRanges::ID;
70 FunctionPass *llvm::createSIFixSGPRLiveRangesPass() {
71 return new SIFixSGPRLiveRanges();
74 bool SIFixSGPRLiveRanges::runOnMachineFunction(MachineFunction &MF) {
75 MachineRegisterInfo &MRI = MF.getRegInfo();
76 const SIRegisterInfo *TRI = static_cast<const SIRegisterInfo *>(
77 MF.getTarget().getRegisterInfo());
78 LiveIntervals *LIS = &getAnalysis<LiveIntervals>();
80 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
83 MachineBasicBlock &MBB = *BI;
84 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
86 MachineInstr &MI = *I;
87 MachineOperand *ExecUse = MI.findRegisterUseOperand(AMDGPU::EXEC);
91 for (const MachineOperand &Def : MI.operands()) {
92 if (!Def.isReg() || !Def.isDef() ||!TargetRegisterInfo::isVirtualRegister(Def.getReg()))
95 const TargetRegisterClass *RC = MRI.getRegClass(Def.getReg());
97 if (!TRI->isSGPRClass(RC))
99 LiveInterval &LI = LIS->getInterval(Def.getReg());
100 for (unsigned i = 0, e = LI.size() - 1; i != e; ++i) {
101 LiveRange::Segment &Seg = LI.segments[i];
102 LiveRange::Segment &Next = LI.segments[i + 1];
103 Seg.end = Next.start;