blob: 8cbde9f0186c6e9c9c7357f356d8c0254c20d29a [file] [log] [blame]
Clement Courbetd939f6d2018-09-13 07:40:53 +00001//===-- SnippetGenerator.cpp ------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Clement Courbetd939f6d2018-09-13 07:40:53 +00006//
7//===----------------------------------------------------------------------===//
8
9#include <array>
10#include <string>
11
12#include "Assembler.h"
13#include "MCInstrDescView.h"
14#include "SnippetGenerator.h"
Roman Lebedev404bdb12019-04-06 14:16:26 +000015#include "Target.h"
Clement Courbetd939f6d2018-09-13 07:40:53 +000016#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
Fangrui Song32401af2018-10-22 17:10:47 +000023namespace llvm {
Clement Courbetd939f6d2018-09-13 07:40:53 +000024namespace exegesis {
25
Guillaume Chateletfcbb6f32018-10-17 11:37:28 +000026std::vector<CodeTemplate> getSingleton(CodeTemplate &&CT) {
Guillaume Chatelet296a8622018-10-15 09:09:19 +000027 std::vector<CodeTemplate> Result;
28 Result.push_back(std::move(CT));
29 return Result;
30}
31
Clement Courbetd939f6d2018-09-13 07:40:53 +000032SnippetGeneratorFailure::SnippetGeneratorFailure(const llvm::Twine &S)
33 : llvm::StringError(S, llvm::inconvertibleErrorCode()) {}
34
Guillaume Chateletee9c2a172018-10-10 14:22:48 +000035SnippetGenerator::SnippetGenerator(const LLVMState &State) : State(State) {}
Clement Courbetd939f6d2018-09-13 07:40:53 +000036
37SnippetGenerator::~SnippetGenerator() = default;
38
39llvm::Expected<std::vector<BenchmarkCode>>
Guillaume Chatelet9b592382018-10-10 14:57:32 +000040SnippetGenerator::generateConfigurations(const Instruction &Instr) const {
Guillaume Chatelet296a8622018-10-15 09:09:19 +000041 if (auto E = generateCodeTemplates(Instr)) {
Guillaume Chateletee9c2a172018-10-10 14:22:48 +000042 const auto &RATC = State.getRATC();
Clement Courbetd939f6d2018-09-13 07:40:53 +000043 std::vector<BenchmarkCode> Output;
Guillaume Chatelet296a8622018-10-15 09:09:19 +000044 for (CodeTemplate &CT : E.get()) {
45 const llvm::BitVector &ForbiddenRegs =
46 CT.ScratchSpacePointerInReg
47 ? RATC.getRegister(CT.ScratchSpacePointerInReg).aliasedBits()
48 : RATC.emptyRegisters();
49 // TODO: Generate as many BenchmarkCode as needed.
50 {
51 BenchmarkCode BC;
52 BC.Info = CT.Info;
53 for (InstructionTemplate &IT : CT.Instructions) {
Roman Lebedev404bdb12019-04-06 14:16:26 +000054 randomizeUnsetVariables(State.getExegesisTarget(), ForbiddenRegs, IT);
Guillaume Chatelet296a8622018-10-15 09:09:19 +000055 BC.Instructions.push_back(IT.build());
56 }
57 if (CT.ScratchSpacePointerInReg)
58 BC.LiveIns.push_back(CT.ScratchSpacePointerInReg);
59 BC.RegisterInitialValues =
Clement Courbet0d79aaf2018-11-08 12:09:45 +000060 computeRegisterInitialValues(CT.Instructions);
Guillaume Chatelet296a8622018-10-15 09:09:19 +000061 Output.push_back(std::move(BC));
Clement Courbetd939f6d2018-09-13 07:40:53 +000062 }
Clement Courbetd939f6d2018-09-13 07:40:53 +000063 }
64 return Output;
65 } else
66 return E.takeError();
67}
68
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000069std::vector<RegisterValue> SnippetGenerator::computeRegisterInitialValues(
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000070 const std::vector<InstructionTemplate> &Instructions) const {
Clement Courbetd939f6d2018-09-13 07:40:53 +000071 // Collect all register uses and create an assignment for each of them.
72 // Ignore memory operands which are handled separately.
73 // Loop invariant: DefinedRegs[i] is true iif it has been set at least once
74 // before the current instruction.
Guillaume Chateletee9c2a172018-10-10 14:22:48 +000075 llvm::BitVector DefinedRegs = State.getRATC().emptyRegisters();
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000076 std::vector<RegisterValue> RIV;
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000077 for (const InstructionTemplate &IT : Instructions) {
Clement Courbetd939f6d2018-09-13 07:40:53 +000078 // Returns the register that this Operand sets or uses, or 0 if this is not
79 // a register.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000080 const auto GetOpReg = [&IT](const Operand &Op) -> unsigned {
Guillaume Chatelet09c28392018-10-09 08:59:10 +000081 if (Op.isMemory())
Clement Courbetd939f6d2018-09-13 07:40:53 +000082 return 0;
Guillaume Chatelet09c28392018-10-09 08:59:10 +000083 if (Op.isImplicitReg())
84 return Op.getImplicitReg();
85 if (Op.isExplicit() && IT.getValueFor(Op).isReg())
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000086 return IT.getValueFor(Op).getReg();
Clement Courbetd939f6d2018-09-13 07:40:53 +000087 return 0;
88 };
89 // Collect used registers that have never been def'ed.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000090 for (const Operand &Op : IT.Instr.Operands) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +000091 if (Op.isUse()) {
Clement Courbetd939f6d2018-09-13 07:40:53 +000092 const unsigned Reg = GetOpReg(Op);
93 if (Reg > 0 && !DefinedRegs.test(Reg)) {
Clement Courbet54c2fa12018-11-08 12:37:56 +000094 RIV.push_back(RegisterValue::zero(Reg));
Clement Courbetd939f6d2018-09-13 07:40:53 +000095 DefinedRegs.set(Reg);
96 }
97 }
98 }
99 // Mark defs as having been def'ed.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000100 for (const Operand &Op : IT.Instr.Operands) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000101 if (Op.isDef()) {
Clement Courbetd939f6d2018-09-13 07:40:53 +0000102 const unsigned Reg = GetOpReg(Op);
103 if (Reg > 0)
104 DefinedRegs.set(Reg);
105 }
106 }
107 }
Guillaume Chateletc96a97b2018-09-20 12:22:18 +0000108 return RIV;
Clement Courbetd939f6d2018-09-13 07:40:53 +0000109}
110
Guillaume Chatelet296a8622018-10-15 09:09:19 +0000111llvm::Expected<std::vector<CodeTemplate>>
112generateSelfAliasingCodeTemplates(const Instruction &Instr) {
Clement Courbetd939f6d2018-09-13 07:40:53 +0000113 const AliasingConfigurations SelfAliasing(Instr, Instr);
Guillaume Chatelet296a8622018-10-15 09:09:19 +0000114 if (SelfAliasing.empty())
Clement Courbetd939f6d2018-09-13 07:40:53 +0000115 return llvm::make_error<SnippetGeneratorFailure>("empty self aliasing");
Guillaume Chatelet296a8622018-10-15 09:09:19 +0000116 std::vector<CodeTemplate> Result;
117 Result.emplace_back();
118 CodeTemplate &CT = Result.back();
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000119 InstructionTemplate IT(Instr);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000120 if (SelfAliasing.hasImplicitAliasing()) {
121 CT.Info = "implicit Self cycles, picking random values.";
122 } else {
123 CT.Info = "explicit self cycles, selecting one aliasing Conf.";
124 // This is a self aliasing instruction so defs and uses are from the same
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000125 // instance, hence twice IT in the following call.
126 setRandomAliasing(SelfAliasing, IT, IT);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000127 }
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000128 CT.Instructions.push_back(std::move(IT));
Guillaume Chateleta3849492018-10-15 09:21:21 +0000129 return std::move(Result);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000130}
131
Guillaume Chatelet296a8622018-10-15 09:09:19 +0000132llvm::Expected<std::vector<CodeTemplate>>
133generateUnconstrainedCodeTemplates(const Instruction &Instr,
134 llvm::StringRef Msg) {
135 std::vector<CodeTemplate> Result;
136 Result.emplace_back();
137 CodeTemplate &CT = Result.back();
Clement Courbetd939f6d2018-09-13 07:40:53 +0000138 CT.Info = llvm::formatv("{0}, repeating an unconstrained assignment", Msg);
139 CT.Instructions.emplace_back(Instr);
Guillaume Chateleta3849492018-10-15 09:21:21 +0000140 return std::move(Result);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000141}
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000142
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000143std::mt19937 &randomGenerator() {
144 static std::random_device RandomDevice;
145 static std::mt19937 RandomGenerator(RandomDevice());
146 return RandomGenerator;
147}
148
149static size_t randomIndex(size_t Size) {
150 assert(Size > 0);
151 std::uniform_int_distribution<> Distribution(0, Size - 1);
152 return Distribution(randomGenerator());
153}
154
155template <typename C>
156static auto randomElement(const C &Container) -> decltype(Container[0]) {
157 return Container[randomIndex(Container.size())];
158}
159
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000160static void setRegisterOperandValue(const RegisterOperandAssignment &ROV,
161 InstructionTemplate &IB) {
162 assert(ROV.Op);
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000163 if (ROV.Op->isExplicit()) {
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000164 auto &AssignedValue = IB.getValueFor(*ROV.Op);
165 if (AssignedValue.isValid()) {
166 assert(AssignedValue.isReg() && AssignedValue.getReg() == ROV.Reg);
167 return;
168 }
169 AssignedValue = llvm::MCOperand::createReg(ROV.Reg);
170 } else {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000171 assert(ROV.Op->isImplicitReg());
172 assert(ROV.Reg == ROV.Op->getImplicitReg());
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000173 }
174}
175
176size_t randomBit(const llvm::BitVector &Vector) {
177 assert(Vector.any());
178 auto Itr = Vector.set_bits_begin();
179 for (size_t I = randomIndex(Vector.count()); I != 0; --I)
180 ++Itr;
181 return *Itr;
182}
183
184void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations,
185 InstructionTemplate &DefIB, InstructionTemplate &UseIB) {
186 assert(!AliasingConfigurations.empty());
187 assert(!AliasingConfigurations.hasImplicitAliasing());
188 const auto &RandomConf = randomElement(AliasingConfigurations.Configurations);
189 setRegisterOperandValue(randomElement(RandomConf.Defs), DefIB);
190 setRegisterOperandValue(randomElement(RandomConf.Uses), UseIB);
191}
192
Roman Lebedev404bdb12019-04-06 14:16:26 +0000193void randomizeUnsetVariables(const ExegesisTarget &Target,
194 const llvm::BitVector &ForbiddenRegs,
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000195 InstructionTemplate &IT) {
196 for (const Variable &Var : IT.Instr.Variables) {
197 llvm::MCOperand &AssignedValue = IT.getValueFor(Var);
198 if (!AssignedValue.isValid())
Roman Lebedev404bdb12019-04-06 14:16:26 +0000199 Target.randomizeMCOperand(IT.Instr, Var, AssignedValue, ForbiddenRegs);
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000200 }
201}
202
Clement Courbetd939f6d2018-09-13 07:40:53 +0000203} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000204} // namespace llvm