1218885Sdim//===-- llvm/CodeGen/AllocationOrder.cpp - Allocation Order ---------------===//
2218885Sdim//
3218885Sdim//                     The LLVM Compiler Infrastructure
4218885Sdim//
5218885Sdim// This file is distributed under the University of Illinois Open Source
6218885Sdim// License. See LICENSE.TXT for details.
7218885Sdim//
8218885Sdim//===----------------------------------------------------------------------===//
9218885Sdim//
10218885Sdim// This file implements an allocation order for virtual registers.
11218885Sdim//
12218885Sdim// The preferred allocation order for a virtual register depends on allocation
13218885Sdim// hints and target hooks. The AllocationOrder class encapsulates all of that.
14218885Sdim//
15218885Sdim//===----------------------------------------------------------------------===//
16218885Sdim
17249423Sdim#define DEBUG_TYPE "regalloc"
18218885Sdim#include "AllocationOrder.h"
19249423Sdim#include "llvm/CodeGen/MachineFunction.h"
20218885Sdim#include "llvm/CodeGen/MachineRegisterInfo.h"
21239462Sdim#include "llvm/CodeGen/RegisterClassInfo.h"
22249423Sdim#include "llvm/CodeGen/VirtRegMap.h"
23249423Sdim#include "llvm/Support/Debug.h"
24249423Sdim#include "llvm/Support/raw_ostream.h"
25218885Sdim
26218885Sdimusing namespace llvm;
27218885Sdim
28218885Sdim// Compare VirtRegMap::getRegAllocPref().
29218885SdimAllocationOrder::AllocationOrder(unsigned VirtReg,
30218885Sdim                                 const VirtRegMap &VRM,
31223017Sdim                                 const RegisterClassInfo &RegClassInfo)
32249423Sdim  : Pos(0) {
33249423Sdim  const MachineFunction &MF = VRM.getMachineFunction();
34249423Sdim  const TargetRegisterInfo *TRI = &VRM.getTargetRegInfo();
35249423Sdim  Order = RegClassInfo.getOrder(MF.getRegInfo().getRegClass(VirtReg));
36249423Sdim  TRI->getRegAllocationHints(VirtReg, Order, Hints, MF, &VRM);
37249423Sdim  rewind();
38218885Sdim
39249423Sdim  DEBUG({
40249423Sdim    if (!Hints.empty()) {
41249423Sdim      dbgs() << "hints:";
42249423Sdim      for (unsigned I = 0, E = Hints.size(); I != E; ++I)
43249423Sdim        dbgs() << ' ' << PrintReg(Hints[I], TRI);
44249423Sdim      dbgs() << '\n';
45249423Sdim    }
46249423Sdim  });
47249423Sdim#ifndef NDEBUG
48249423Sdim  for (unsigned I = 0, E = Hints.size(); I != E; ++I)
49249423Sdim    assert(std::find(Order.begin(), Order.end(), Hints[I]) != Order.end() &&
50249423Sdim           "Target hint is outside allocation order.");
51249423Sdim#endif
52218885Sdim}
53