1//===-- LiveRangeUtils.h - Live Range modification utilities ----*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8/// \file
9/// This file contains helper functions to modify live ranges.
10///
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_LIB_CODEGEN_LIVERANGEUTILS_H
14#define LLVM_LIB_CODEGEN_LIVERANGEUTILS_H
15
16#include "llvm/CodeGen/LiveInterval.h"
17
18namespace llvm {
19
20/// Helper function that distributes live range value numbers and the
21/// corresponding segments of a primary live range \p LR to a list of newly
22/// created live ranges \p SplitLRs. \p VNIClasses maps each value number in \p
23/// LR to 0 meaning it should stay or to 1..N meaning it should go to a specific
24/// live range in the \p SplitLRs array.
25template<typename LiveRangeT, typename EqClassesT>
26static void DistributeRange(LiveRangeT &LR, LiveRangeT *SplitLRs[],
27 EqClassesT VNIClasses) {
28 // Move segments to new intervals.
29 typename LiveRangeT::iterator J = LR.begin(), E = LR.end();
30 while (J != E && VNIClasses[J->valno->id] == 0)
31 ++J;
32 for (typename LiveRangeT::iterator I = J; I != E; ++I) {
33 if (unsigned eq = VNIClasses[I->valno->id]) {
34 assert((SplitLRs[eq-1]->empty() || SplitLRs[eq-1]->expiredAt(I->start)) &&
35 "New intervals should be empty");
36 SplitLRs[eq-1]->segments.push_back(*I);
37 } else
38 *J++ = *I;
39 }
40 LR.segments.erase(J, E);
41
42 // Transfer VNInfos to their new owners and renumber them.
43 unsigned j = 0, e = LR.getNumValNums();
44 while (j != e && VNIClasses[j] == 0)
45 ++j;
46 for (unsigned i = j; i != e; ++i) {
47 VNInfo *VNI = LR.getValNumInfo(i);
48 if (unsigned eq = VNIClasses[i]) {
49 VNI->id = SplitLRs[eq-1]->getNumValNums();
50 SplitLRs[eq-1]->valnos.push_back(VNI);
51 } else {
52 VNI->id = j;
53 LR.valnos[j++] = VNI;
54 }
55 }
56 LR.valnos.resize(j);
57}
58
59} // End llvm namespace
60
61#endif
62