blob: 0f32b66f41323800bca907485d96921c184062ac [file] [log] [blame]
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +00001//===-- llvm/CodeGen/AllocationOrder.cpp - Allocation Order ---------------===//
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//===----------------------------------------------------------------------===//
9//
10// This file implements an allocation order for virtual registers.
11//
12// The preferred allocation order for a virtual register depends on allocation
13// hints and target hooks. The AllocationOrder class encapsulates all of that.
14//
15//===----------------------------------------------------------------------===//
16
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000017#define DEBUG_TYPE "regalloc"
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000018#include "AllocationOrder.h"
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000019#include "llvm/CodeGen/MachineFunction.h"
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000020#include "llvm/CodeGen/MachineRegisterInfo.h"
Andrew Trick15252602012-06-06 20:29:31 +000021#include "llvm/CodeGen/RegisterClassInfo.h"
Jakob Stoklund Olesen1ead68d2012-11-28 19:13:06 +000022#include "llvm/CodeGen/VirtRegMap.h"
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000023#include "llvm/Target/TargetMachine.h"
24#include "llvm/Support/Debug.h"
25#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000026
27using namespace llvm;
28
29// Compare VirtRegMap::getRegAllocPref().
30AllocationOrder::AllocationOrder(unsigned VirtReg,
31 const VirtRegMap &VRM,
Jakob Stoklund Olesen5f2316a2011-06-03 20:34:53 +000032 const RegisterClassInfo &RegClassInfo)
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000033 : Pos(0) {
34 const MachineFunction &MF = VRM.getMachineFunction();
35 const TargetRegisterInfo *TRI = &VRM.getTargetRegInfo();
36 Order = RegClassInfo.getOrder(MF.getRegInfo().getRegClass(VirtReg));
37 TRI->getRegAllocationHints(VirtReg, Order, Hints, MF, &VRM);
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000038
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000039 DEBUG({
40 if (!Hints.empty()) {
41 dbgs() << "hints:";
42 for (unsigned I = 0, E = Hints.size(); I != E; ++I)
43 dbgs() << ' ' << PrintReg(Hints[I], TRI);
44 dbgs() << '\n';
45 }
46 });
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000047}
48
Jakob Stoklund Olesenfc29db12012-12-03 22:51:04 +000049bool AllocationOrder::isHint(unsigned PhysReg) const {
50 return std::find(Hints.begin(), Hints.end(), PhysReg) != Hints.end();
51}
52
53unsigned AllocationOrder::next() {
54 if (Pos < Hints.size())
55 return Hints[Pos++];
56 ArrayRef<MCPhysReg>::iterator I = Order.begin() + (Pos - Hints.size());
57 ArrayRef<MCPhysReg>::iterator E = Order.end();
58 while (I != E) {
59 unsigned Reg = *I++;
60 ++Pos;
61 if (!isHint(Reg))
62 return Reg;
63 }
64 return 0;
Jakob Stoklund Olesenc9672cb2010-12-10 18:36:02 +000065}