blob: f7a76d88ccf719f21ad98ba70b3af05de25bc0c0 [file] [log] [blame]
Clement Courbetd939f6d2018-09-13 07:40:53 +00001//===-- SnippetGenerator.cpp ------------------------------------*- 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
10#include <array>
11#include <string>
12
13#include "Assembler.h"
14#include "MCInstrDescView.h"
15#include "SnippetGenerator.h"
16#include "llvm/ADT/StringExtras.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/Twine.h"
19#include "llvm/Support/FileSystem.h"
20#include "llvm/Support/FormatVariadic.h"
21#include "llvm/Support/Program.h"
22
23namespace exegesis {
24
25SnippetGeneratorFailure::SnippetGeneratorFailure(const llvm::Twine &S)
26 : llvm::StringError(S, llvm::inconvertibleErrorCode()) {}
27
Guillaume Chateletee9c2a172018-10-10 14:22:48 +000028SnippetGenerator::SnippetGenerator(const LLVMState &State) : State(State) {}
Clement Courbetd939f6d2018-09-13 07:40:53 +000029
30SnippetGenerator::~SnippetGenerator() = default;
31
32llvm::Expected<std::vector<BenchmarkCode>>
Guillaume Chatelet9b592382018-10-10 14:57:32 +000033SnippetGenerator::generateConfigurations(const Instruction &Instr) const {
34 if (auto E = generateCodeTemplate(Instr)) {
Clement Courbetd939f6d2018-09-13 07:40:53 +000035 CodeTemplate &CT = E.get();
Guillaume Chateletee9c2a172018-10-10 14:22:48 +000036 const auto &RATC = State.getRATC();
Guillaume Chateletc6268f32018-10-01 11:46:06 +000037 const llvm::BitVector &ForbiddenRegs =
38 CT.ScratchSpacePointerInReg
39 ? RATC.getRegister(CT.ScratchSpacePointerInReg).aliasedBits()
40 : RATC.emptyRegisters();
Clement Courbetd939f6d2018-09-13 07:40:53 +000041 std::vector<BenchmarkCode> Output;
42 // TODO: Generate as many BenchmarkCode as needed.
43 {
44 BenchmarkCode BC;
45 BC.Info = CT.Info;
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000046 for (InstructionTemplate &IT : CT.Instructions) {
Guillaume Chateletc6268f32018-10-01 11:46:06 +000047 randomizeUnsetVariables(ForbiddenRegs, IT);
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000048 BC.Instructions.push_back(IT.build());
Clement Courbetd939f6d2018-09-13 07:40:53 +000049 }
50 if (CT.ScratchSpacePointerInReg)
51 BC.LiveIns.push_back(CT.ScratchSpacePointerInReg);
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000052 BC.RegisterInitialValues = computeRegisterInitialValues(CT.Instructions);
Clement Courbetd939f6d2018-09-13 07:40:53 +000053 Output.push_back(std::move(BC));
54 }
55 return Output;
56 } else
57 return E.takeError();
58}
59
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000060std::vector<RegisterValue> SnippetGenerator::computeRegisterInitialValues(
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000061 const std::vector<InstructionTemplate> &Instructions) const {
Clement Courbetd939f6d2018-09-13 07:40:53 +000062 // Collect all register uses and create an assignment for each of them.
63 // Ignore memory operands which are handled separately.
64 // Loop invariant: DefinedRegs[i] is true iif it has been set at least once
65 // before the current instruction.
Guillaume Chateletee9c2a172018-10-10 14:22:48 +000066 llvm::BitVector DefinedRegs = State.getRATC().emptyRegisters();
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000067 std::vector<RegisterValue> RIV;
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000068 for (const InstructionTemplate &IT : Instructions) {
Clement Courbetd939f6d2018-09-13 07:40:53 +000069 // Returns the register that this Operand sets or uses, or 0 if this is not
70 // a register.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000071 const auto GetOpReg = [&IT](const Operand &Op) -> unsigned {
Guillaume Chatelet09c28392018-10-09 08:59:10 +000072 if (Op.isMemory())
Clement Courbetd939f6d2018-09-13 07:40:53 +000073 return 0;
Guillaume Chatelet09c28392018-10-09 08:59:10 +000074 if (Op.isImplicitReg())
75 return Op.getImplicitReg();
76 if (Op.isExplicit() && IT.getValueFor(Op).isReg())
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000077 return IT.getValueFor(Op).getReg();
Clement Courbetd939f6d2018-09-13 07:40:53 +000078 return 0;
79 };
80 // Collect used registers that have never been def'ed.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000081 for (const Operand &Op : IT.Instr.Operands) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +000082 if (Op.isUse()) {
Clement Courbetd939f6d2018-09-13 07:40:53 +000083 const unsigned Reg = GetOpReg(Op);
84 if (Reg > 0 && !DefinedRegs.test(Reg)) {
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000085 RIV.push_back(RegisterValue{Reg, llvm::APInt()});
Clement Courbetd939f6d2018-09-13 07:40:53 +000086 DefinedRegs.set(Reg);
87 }
88 }
89 }
90 // Mark defs as having been def'ed.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000091 for (const Operand &Op : IT.Instr.Operands) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +000092 if (Op.isDef()) {
Clement Courbetd939f6d2018-09-13 07:40:53 +000093 const unsigned Reg = GetOpReg(Op);
94 if (Reg > 0)
95 DefinedRegs.set(Reg);
96 }
97 }
98 }
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000099 return RIV;
Clement Courbetd939f6d2018-09-13 07:40:53 +0000100}
101
102llvm::Expected<CodeTemplate> SnippetGenerator::generateSelfAliasingCodeTemplate(
103 const Instruction &Instr) const {
104 const AliasingConfigurations SelfAliasing(Instr, Instr);
105 if (SelfAliasing.empty()) {
106 return llvm::make_error<SnippetGeneratorFailure>("empty self aliasing");
107 }
108 CodeTemplate CT;
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000109 InstructionTemplate IT(Instr);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000110 if (SelfAliasing.hasImplicitAliasing()) {
111 CT.Info = "implicit Self cycles, picking random values.";
112 } else {
113 CT.Info = "explicit self cycles, selecting one aliasing Conf.";
114 // This is a self aliasing instruction so defs and uses are from the same
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000115 // instance, hence twice IT in the following call.
116 setRandomAliasing(SelfAliasing, IT, IT);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000117 }
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000118 CT.Instructions.push_back(std::move(IT));
Clement Courbetd939f6d2018-09-13 07:40:53 +0000119 return std::move(CT);
120}
121
122llvm::Expected<CodeTemplate>
123SnippetGenerator::generateUnconstrainedCodeTemplate(const Instruction &Instr,
124 llvm::StringRef Msg) const {
125 CodeTemplate CT;
126 CT.Info = llvm::formatv("{0}, repeating an unconstrained assignment", Msg);
127 CT.Instructions.emplace_back(Instr);
128 return std::move(CT);
129}
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000130
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000131std::mt19937 &randomGenerator() {
132 static std::random_device RandomDevice;
133 static std::mt19937 RandomGenerator(RandomDevice());
134 return RandomGenerator;
135}
136
137static size_t randomIndex(size_t Size) {
138 assert(Size > 0);
139 std::uniform_int_distribution<> Distribution(0, Size - 1);
140 return Distribution(randomGenerator());
141}
142
143template <typename C>
144static auto randomElement(const C &Container) -> decltype(Container[0]) {
145 return Container[randomIndex(Container.size())];
146}
147
148static void randomize(const Instruction &Instr, const Variable &Var,
149 llvm::MCOperand &AssignedValue,
150 const llvm::BitVector &ForbiddenRegs) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000151 const Operand &Op = Instr.getPrimaryOperand(Var);
152 switch (Op.getExplicitOperandInfo().OperandType) {
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000153 case llvm::MCOI::OperandType::OPERAND_IMMEDIATE:
154 // FIXME: explore immediate values too.
155 AssignedValue = llvm::MCOperand::createImm(1);
156 break;
157 case llvm::MCOI::OperandType::OPERAND_REGISTER: {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000158 assert(Op.isReg());
159 auto AllowedRegs = Op.getRegisterAliasing().sourceBits();
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000160 assert(AllowedRegs.size() == ForbiddenRegs.size());
161 for (auto I : ForbiddenRegs.set_bits())
162 AllowedRegs.reset(I);
163 AssignedValue = llvm::MCOperand::createReg(randomBit(AllowedRegs));
164 break;
165 }
166 default:
167 break;
168 }
169}
170
171static void setRegisterOperandValue(const RegisterOperandAssignment &ROV,
172 InstructionTemplate &IB) {
173 assert(ROV.Op);
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000174 if (ROV.Op->isExplicit()) {
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000175 auto &AssignedValue = IB.getValueFor(*ROV.Op);
176 if (AssignedValue.isValid()) {
177 assert(AssignedValue.isReg() && AssignedValue.getReg() == ROV.Reg);
178 return;
179 }
180 AssignedValue = llvm::MCOperand::createReg(ROV.Reg);
181 } else {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000182 assert(ROV.Op->isImplicitReg());
183 assert(ROV.Reg == ROV.Op->getImplicitReg());
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000184 }
185}
186
187size_t randomBit(const llvm::BitVector &Vector) {
188 assert(Vector.any());
189 auto Itr = Vector.set_bits_begin();
190 for (size_t I = randomIndex(Vector.count()); I != 0; --I)
191 ++Itr;
192 return *Itr;
193}
194
195void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations,
196 InstructionTemplate &DefIB, InstructionTemplate &UseIB) {
197 assert(!AliasingConfigurations.empty());
198 assert(!AliasingConfigurations.hasImplicitAliasing());
199 const auto &RandomConf = randomElement(AliasingConfigurations.Configurations);
200 setRegisterOperandValue(randomElement(RandomConf.Defs), DefIB);
201 setRegisterOperandValue(randomElement(RandomConf.Uses), UseIB);
202}
203
204void randomizeUnsetVariables(const llvm::BitVector &ForbiddenRegs,
205 InstructionTemplate &IT) {
206 for (const Variable &Var : IT.Instr.Variables) {
207 llvm::MCOperand &AssignedValue = IT.getValueFor(Var);
208 if (!AssignedValue.isValid())
209 randomize(IT.Instr, Var, AssignedValue, ForbiddenRegs);
210 }
211}
212
Clement Courbetd939f6d2018-09-13 07:40:53 +0000213} // namespace exegesis