blob: 71687884f4dd1ac8220911de0926b07686e21fe1 [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
Quentin Colombetab8c21f2016-04-08 17:19:10 +000013#include "llvm/ADT/PostOrderIterator.h"
Quentin Colombet8e8e85c2016-04-05 19:06:01 +000014#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
Quentin Colombet40ad5732016-04-07 18:19:27 +000015#include "llvm/CodeGen/GlobalISel/RegisterBank.h"
16#include "llvm/CodeGen/MachineRegisterInfo.h"
Quentin Colombete16f5612016-04-07 23:53:55 +000017#include "llvm/Support/Debug.h"
Quentin Colombet40ad5732016-04-07 18:19:27 +000018#include "llvm/Target/TargetSubtargetInfo.h"
Quentin Colombet8e8e85c2016-04-05 19:06:01 +000019
20#define DEBUG_TYPE "regbankselect"
21
22using namespace llvm;
23
24char RegBankSelect::ID = 0;
25INITIALIZE_PASS(RegBankSelect, "regbankselect",
26 "Assign register bank of generic virtual registers",
27 false, false);
28
Quentin Colombet40ad5732016-04-07 18:19:27 +000029RegBankSelect::RegBankSelect()
30 : MachineFunctionPass(ID), RBI(nullptr), MRI(nullptr) {
Quentin Colombet8e8e85c2016-04-05 19:06:01 +000031 initializeRegBankSelectPass(*PassRegistry::getPassRegistry());
32}
33
Quentin Colombet40ad5732016-04-07 18:19:27 +000034void RegBankSelect::init(MachineFunction &MF) {
35 RBI = MF.getSubtarget().getRegBankInfo();
36 assert(RBI && "Cannot work without RegisterBankInfo");
37 MRI = &MF.getRegInfo();
Quentin Colombetaac71a42016-04-07 21:32:23 +000038 TRI = MF.getSubtarget().getRegisterInfo();
Quentin Colombet40ad5732016-04-07 18:19:27 +000039 MIRBuilder.setMF(MF);
40}
41
42bool RegBankSelect::assignmentMatch(
43 unsigned Reg, const RegisterBankInfo::ValueMapping &ValMapping) const {
44 // Each part of a break down needs to end up in a different register.
45 // In other word, Reg assignement does not match.
46 if (ValMapping.BreakDown.size() > 1)
47 return false;
48
Quentin Colombet6d6d6af2016-04-08 16:48:16 +000049 const RegisterBank *CurRegBank = RBI->getRegBank(Reg, *MRI, *TRI);
50 const RegisterBank *DesiredRegBrank = ValMapping.BreakDown[0].RegBank;
51 DEBUG(dbgs() << "Does assignment already match: ";
52 if (CurRegBank) dbgs() << *CurRegBank; else dbgs() << "none";
53 dbgs() << " against ";
54 assert(DesiredRegBrank && "The mapping must be valid");
55 dbgs() << *DesiredRegBrank << '\n';);
56 return CurRegBank == DesiredRegBrank;
Quentin Colombet40ad5732016-04-07 18:19:27 +000057}
58
59unsigned
60RegBankSelect::repairReg(unsigned Reg,
61 const RegisterBankInfo::ValueMapping &ValMapping) {
62 assert(ValMapping.BreakDown.size() == 1 &&
63 "Support for complex break down not supported yet");
64 const RegisterBankInfo::PartialMapping &PartialMap = ValMapping.BreakDown[0];
65 assert(PartialMap.Mask.getBitWidth() == MRI->getSize(Reg) &&
66 "Repairing other than copy not implemented yet");
67 unsigned NewReg =
68 MRI->createGenericVirtualRegister(PartialMap.Mask.getBitWidth());
69 (void)MIRBuilder.buildInstr(TargetOpcode::COPY, NewReg, Reg);
Quentin Colombete16f5612016-04-07 23:53:55 +000070 DEBUG(dbgs() << "Repair: " << PrintReg(Reg) << " with: "
71 << PrintReg(NewReg) << '\n');
Quentin Colombet40ad5732016-04-07 18:19:27 +000072 return NewReg;
73}
74
75void RegBankSelect::assignInstr(MachineInstr &MI) {
Quentin Colombete16f5612016-04-07 23:53:55 +000076 DEBUG(dbgs() << "Assign: " << MI);
Quentin Colombet40ad5732016-04-07 18:19:27 +000077 const RegisterBankInfo::InstructionMapping DefaultMapping =
78 RBI->getInstrMapping(MI);
79 // Make sure the mapping is valid for MI.
80 DefaultMapping.verify(MI);
Quentin Colombete16f5612016-04-07 23:53:55 +000081
82 DEBUG(dbgs() << "Mapping: " << DefaultMapping << '\n');
83
Quentin Colombet40ad5732016-04-07 18:19:27 +000084 // Set the insertion point before MI.
85 // This is where we are going to insert the repairing code if any.
86 MIRBuilder.setInstr(MI, /*Before*/ true);
87
88 // For now, do not look for alternative mappings.
89 // Alternative mapping may require to rewrite MI and we do not support
90 // that yet.
91 // Walk the operands and assign then to the chosen mapping, possibly with
92 // the insertion of repair code for uses.
93 for (unsigned OpIdx = 0, EndIdx = MI.getNumOperands(); OpIdx != EndIdx;
94 ++OpIdx) {
95 MachineOperand &MO = MI.getOperand(OpIdx);
96 // Nothing to be done for non-register operands.
97 if (!MO.isReg())
98 continue;
99 unsigned Reg = MO.getReg();
100 if (!Reg)
101 continue;
102
103 const RegisterBankInfo::ValueMapping &ValMapping =
104 DefaultMapping.getOperandMapping(OpIdx);
105 // If Reg is already properly mapped, move on.
106 if (assignmentMatch(Reg, ValMapping))
107 continue;
108
109 // For uses, we may need to create a new temporary.
110 // Indeed, if Reg is already assigned a register bank, at this
111 // point, we know it is different from the one defined by the
112 // chosen mapping, we need to adjust for that.
113 assert(ValMapping.BreakDown.size() == 1 &&
114 "Support for complex break down not supported yet");
115 if (!MO.isDef() && MRI->getRegClassOrRegBank(Reg)) {
116 // For phis, we need to change the insertion point to the end of
117 // the related predecessor block.
118 assert(!MI.isPHI() && "PHI support not implemented yet");
119 Reg = repairReg(Reg, ValMapping);
120 }
121 // If we end up here, MO should be free of encoding constraints,
122 // i.e., we do not have to constrained the RegBank of Reg to
123 // the requirement of the operands.
124 // If that is not the case, this means the code was broken before
125 // hands because we should have found that the assignment match.
126 // This will not hold when we will consider alternative mappings.
Quentin Colombet6d6d6af2016-04-08 16:48:16 +0000127 DEBUG(dbgs() << "Assign: " << *ValMapping.BreakDown[0].RegBank << " to "
128 << PrintReg(Reg) << '\n');
Quentin Colombetab8c21f2016-04-08 17:19:10 +0000129 // For a definition, we may be changing the register bank silently
130 // for all the uses here.
131 // Although this will be correct when we do a RPO traversal of the
132 // basic block, because the only uses that could be affected are
133 // PHIs (i.e., copies), this may not be the best solution
134 // according to the cost model.
Quentin Colombet40ad5732016-04-07 18:19:27 +0000135 MRI->setRegBank(Reg, *ValMapping.BreakDown[0].RegBank);
136 MO.setReg(Reg);
137 }
Quentin Colombete16f5612016-04-07 23:53:55 +0000138 DEBUG(dbgs() << "Assigned: " << MI);
Quentin Colombet40ad5732016-04-07 18:19:27 +0000139}
140
Quentin Colombet8e8e85c2016-04-05 19:06:01 +0000141bool RegBankSelect::runOnMachineFunction(MachineFunction &MF) {
Quentin Colombete16f5612016-04-07 23:53:55 +0000142 DEBUG(dbgs() << "Assign register banks for: " << MF.getName() << '\n');
Quentin Colombet40ad5732016-04-07 18:19:27 +0000143 init(MF);
144 // Walk the function and assign register banks to all operands.
Quentin Colombetab8c21f2016-04-08 17:19:10 +0000145 // Use a RPOT to make sure all registers are assigned before we choose
146 // the best mapping of the current instruction.
147 ReversePostOrderTraversal<MachineFunction*> RPOT(&MF);
148 for (MachineBasicBlock *MBB : RPOT)
149 for (MachineInstr &MI : *MBB)
Quentin Colombet40ad5732016-04-07 18:19:27 +0000150 assignInstr(MI);
Quentin Colombet8e8e85c2016-04-05 19:06:01 +0000151 return false;
152}