blob: f5c9370805fd279e3cc3abf3d6b2105f3affbce1 [file] [log] [blame]
Quentin Colombet8e8e85c2016-04-05 19:06:01 +00001//===- llvm/CodeGen/GlobalISel/RegBankSelect.cpp - RegBankSelect -*- C++ -*-==//
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/// \file
10/// This file implements the RegBankSelect class.
11//===----------------------------------------------------------------------===//
12
13#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Quentin Colombet40ad5732016-04-07 18:19:27 +000014#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
15#include "llvm/CodeGen/MachineRegisterInfo.h"
Quentin Colombete16f5612016-04-07 23:53:55 +000016#include "llvm/Support/Debug.h"
Quentin Colombet40ad5732016-04-07 18:19:27 +000017#include "llvm/Target/TargetSubtargetInfo.h"
Quentin Colombet8e8e85c2016-04-05 19:06:01 +000018
19#define DEBUG_TYPE "regbankselect"
20
21using namespace llvm;
22
23char RegBankSelect::ID = 0;
24INITIALIZE_PASS(RegBankSelect, "regbankselect",
25 "Assign register bank of generic virtual registers",
26 false, false);
27
Quentin Colombet40ad5732016-04-07 18:19:27 +000028RegBankSelect::RegBankSelect()
29 : MachineFunctionPass(ID), RBI(nullptr), MRI(nullptr) {
Quentin Colombet8e8e85c2016-04-05 19:06:01 +000030 initializeRegBankSelectPass(*PassRegistry::getPassRegistry());
31}
32
Quentin Colombet40ad5732016-04-07 18:19:27 +000033void RegBankSelect::init(MachineFunction &MF) {
34 RBI = MF.getSubtarget().getRegBankInfo();
35 assert(RBI && "Cannot work without RegisterBankInfo");
36 MRI = &MF.getRegInfo();
Quentin Colombetaac71a42016-04-07 21:32:23 +000037 TRI = MF.getSubtarget().getRegisterInfo();
Quentin Colombet40ad5732016-04-07 18:19:27 +000038 MIRBuilder.setMF(MF);
39}
40
41bool RegBankSelect::assignmentMatch(
42 unsigned Reg, const RegisterBankInfo::ValueMapping &ValMapping) const {
43 // Each part of a break down needs to end up in a different register.
44 // In other word, Reg assignement does not match.
45 if (ValMapping.BreakDown.size() > 1)
46 return false;
47
Quentin Colombetaac71a42016-04-07 21:32:23 +000048 return RBI->getRegBank(Reg, *MRI, *TRI) == ValMapping.BreakDown[0].RegBank;
Quentin Colombet40ad5732016-04-07 18:19:27 +000049}
50
51unsigned
52RegBankSelect::repairReg(unsigned Reg,
53 const RegisterBankInfo::ValueMapping &ValMapping) {
54 assert(ValMapping.BreakDown.size() == 1 &&
55 "Support for complex break down not supported yet");
56 const RegisterBankInfo::PartialMapping &PartialMap = ValMapping.BreakDown[0];
57 assert(PartialMap.Mask.getBitWidth() == MRI->getSize(Reg) &&
58 "Repairing other than copy not implemented yet");
59 unsigned NewReg =
60 MRI->createGenericVirtualRegister(PartialMap.Mask.getBitWidth());
61 (void)MIRBuilder.buildInstr(TargetOpcode::COPY, NewReg, Reg);
Quentin Colombete16f5612016-04-07 23:53:55 +000062 DEBUG(dbgs() << "Repair: " << PrintReg(Reg) << " with: "
63 << PrintReg(NewReg) << '\n');
Quentin Colombet40ad5732016-04-07 18:19:27 +000064 return NewReg;
65}
66
67void RegBankSelect::assignInstr(MachineInstr &MI) {
Quentin Colombete16f5612016-04-07 23:53:55 +000068 DEBUG(dbgs() << "Assign: " << MI);
Quentin Colombet40ad5732016-04-07 18:19:27 +000069 const RegisterBankInfo::InstructionMapping DefaultMapping =
70 RBI->getInstrMapping(MI);
71 // Make sure the mapping is valid for MI.
72 DefaultMapping.verify(MI);
Quentin Colombete16f5612016-04-07 23:53:55 +000073
74 DEBUG(dbgs() << "Mapping: " << DefaultMapping << '\n');
75
Quentin Colombet40ad5732016-04-07 18:19:27 +000076 // Set the insertion point before MI.
77 // This is where we are going to insert the repairing code if any.
78 MIRBuilder.setInstr(MI, /*Before*/ true);
79
80 // For now, do not look for alternative mappings.
81 // Alternative mapping may require to rewrite MI and we do not support
82 // that yet.
83 // Walk the operands and assign then to the chosen mapping, possibly with
84 // the insertion of repair code for uses.
85 for (unsigned OpIdx = 0, EndIdx = MI.getNumOperands(); OpIdx != EndIdx;
86 ++OpIdx) {
87 MachineOperand &MO = MI.getOperand(OpIdx);
88 // Nothing to be done for non-register operands.
89 if (!MO.isReg())
90 continue;
91 unsigned Reg = MO.getReg();
92 if (!Reg)
93 continue;
94
95 const RegisterBankInfo::ValueMapping &ValMapping =
96 DefaultMapping.getOperandMapping(OpIdx);
97 // If Reg is already properly mapped, move on.
98 if (assignmentMatch(Reg, ValMapping))
99 continue;
100
101 // For uses, we may need to create a new temporary.
102 // Indeed, if Reg is already assigned a register bank, at this
103 // point, we know it is different from the one defined by the
104 // chosen mapping, we need to adjust for that.
105 assert(ValMapping.BreakDown.size() == 1 &&
106 "Support for complex break down not supported yet");
107 if (!MO.isDef() && MRI->getRegClassOrRegBank(Reg)) {
108 // For phis, we need to change the insertion point to the end of
109 // the related predecessor block.
110 assert(!MI.isPHI() && "PHI support not implemented yet");
111 Reg = repairReg(Reg, ValMapping);
112 }
113 // If we end up here, MO should be free of encoding constraints,
114 // i.e., we do not have to constrained the RegBank of Reg to
115 // the requirement of the operands.
116 // If that is not the case, this means the code was broken before
117 // hands because we should have found that the assignment match.
118 // This will not hold when we will consider alternative mappings.
119 MRI->setRegBank(Reg, *ValMapping.BreakDown[0].RegBank);
120 MO.setReg(Reg);
121 }
Quentin Colombete16f5612016-04-07 23:53:55 +0000122 DEBUG(dbgs() << "Assigned: " << MI);
Quentin Colombet40ad5732016-04-07 18:19:27 +0000123}
124
Quentin Colombet8e8e85c2016-04-05 19:06:01 +0000125bool RegBankSelect::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombete16f5612016-04-07 23:53:55 +0000126 DEBUG(dbgs() << "Assign register banks for: " << MF.getName() << '\n');
Quentin Colombet40ad5732016-04-07 18:19:27 +0000127 init(MF);
128 // Walk the function and assign register banks to all operands.
129 for (MachineBasicBlock &MBB : MF)
130 for (MachineInstr &MI : MBB)
131 assignInstr(MI);
Quentin Colombet8e8e85c2016-04-05 19:06:01 +0000132 return false;
133}