aboutsummaryrefslogtreecommitdiff
path: root/lib/CodeGen/AllocationOrder.cpp
blob: a8ee2b6357c3677c530d9cebda82a27697f82b1f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//===-- llvm/CodeGen/AllocationOrder.cpp - Allocation Order ---------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements an allocation order for virtual registers.
//
// The preferred allocation order for a virtual register depends on allocation
// hints and target hooks. The AllocationOrder class encapsulates all of that.
//
//===----------------------------------------------------------------------===//

#include "AllocationOrder.h"
#include "RegisterClassInfo.h"
#include "VirtRegMap.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"

using namespace llvm;

// Compare VirtRegMap::getRegAllocPref().
AllocationOrder::AllocationOrder(unsigned VirtReg,
                                 const VirtRegMap &VRM,
                                 const RegisterClassInfo &RegClassInfo)
  : Begin(0), End(0), Pos(0), RCI(RegClassInfo), OwnedBegin(false) {
  const TargetRegisterClass *RC = VRM.getRegInfo().getRegClass(VirtReg);
  std::pair<unsigned, unsigned> HintPair =
    VRM.getRegInfo().getRegAllocationHint(VirtReg);

  // HintPair.second is a register, phys or virt.
  Hint = HintPair.second;

  // Translate to physreg, or 0 if not assigned yet.
  if (TargetRegisterInfo::isVirtualRegister(Hint))
    Hint = VRM.getPhys(Hint);

  // The first hint pair component indicates a target-specific hint.
  if (HintPair.first) {
    const TargetRegisterInfo &TRI = VRM.getTargetRegInfo();
    // The remaining allocation order may depend on the hint.
    const unsigned *B, *E;
    tie(B, E) = TRI.getAllocationOrder(RC, HintPair.first, Hint,
                                       VRM.getMachineFunction());

    // Empty allocation order?
    if (B == E)
      return;

    // Copy the allocation order with reserved registers removed.
    OwnedBegin = true;
    unsigned *P = new unsigned[E - B];
    Begin = P;
    for (; B != E; ++B)
      if (!RCI.isReserved(*B))
        *P++ = *B;
    End = P;

    // Target-dependent hints require resolution.
    Hint = TRI.ResolveRegAllocHint(HintPair.first, Hint,
                                   VRM.getMachineFunction());
  } else {
    // If there is no hint or just a normal hint, use the cached allocation
    // order from RegisterClassInfo.
    ArrayRef<unsigned> O = RCI.getOrder(RC);
    Begin = O.begin();
    End = O.end();
  }

  // The hint must be a valid physreg for allocation.
  if (Hint && (!TargetRegisterInfo::isPhysicalRegister(Hint) ||
               !RC->contains(Hint) || RCI.isReserved(Hint)))
    Hint = 0;
}

AllocationOrder::~AllocationOrder() {
  if (OwnedBegin)
    delete [] Begin;
}