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