blob: cd93589d34d051a7c0034d11a5df93b708307f67 [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 {
Clement Courbet8ef97e12019-09-27 08:04:10 +000041 llvm::BitVector ForbiddenRegs = State.getRATC().reservedRegisters();
42
43 // If the instruction has memory registers, prevent the generator from
44 // using the scratch register and its aliasing registers.
45 if (Instr.hasMemoryOperands()) {
46 const auto &ET = State.getExegesisTarget();
47 unsigned ScratchSpacePointerInReg =
48 ET.getScratchMemoryRegister(State.getTargetMachine().getTargetTriple());
49 if (ScratchSpacePointerInReg == 0)
50 return llvm::make_error<BenchmarkFailure>(
51 "Infeasible : target does not support memory instructions");
52 const auto &ScratchRegAliases =
53 State.getRATC().getRegister(ScratchSpacePointerInReg).aliasedBits();
54 // If the instruction implicitly writes to ScratchSpacePointerInReg , abort.
55 // FIXME: We could make a copy of the scratch register.
56 for (const auto &Op : Instr.Operands) {
57 if (Op.isDef() && Op.isImplicitReg() &&
58 ScratchRegAliases.test(Op.getImplicitReg()))
59 return llvm::make_error<BenchmarkFailure>(
60 "Infeasible : memory instruction uses scratch memory register");
61 }
62 ForbiddenRegs |= ScratchRegAliases;
63 }
64
65 if (auto E = generateCodeTemplates(Instr, ForbiddenRegs)) {
Clement Courbetd939f6d2018-09-13 07:40:53 +000066 std::vector<BenchmarkCode> Output;
Guillaume Chatelet296a8622018-10-15 09:09:19 +000067 for (CodeTemplate &CT : E.get()) {
Guillaume Chatelet296a8622018-10-15 09:09:19 +000068 // TODO: Generate as many BenchmarkCode as needed.
69 {
70 BenchmarkCode BC;
71 BC.Info = CT.Info;
72 for (InstructionTemplate &IT : CT.Instructions) {
Roman Lebedev404bdb12019-04-06 14:16:26 +000073 randomizeUnsetVariables(State.getExegesisTarget(), ForbiddenRegs, IT);
Guillaume Chatelet296a8622018-10-15 09:09:19 +000074 BC.Instructions.push_back(IT.build());
75 }
76 if (CT.ScratchSpacePointerInReg)
77 BC.LiveIns.push_back(CT.ScratchSpacePointerInReg);
78 BC.RegisterInitialValues =
Clement Courbet0d79aaf2018-11-08 12:09:45 +000079 computeRegisterInitialValues(CT.Instructions);
Guillaume Chatelet296a8622018-10-15 09:09:19 +000080 Output.push_back(std::move(BC));
Clement Courbetd939f6d2018-09-13 07:40:53 +000081 }
Clement Courbetd939f6d2018-09-13 07:40:53 +000082 }
83 return Output;
84 } else
85 return E.takeError();
86}
87
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000088std::vector<RegisterValue> SnippetGenerator::computeRegisterInitialValues(
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000089 const std::vector<InstructionTemplate> &Instructions) const {
Clement Courbetd939f6d2018-09-13 07:40:53 +000090 // Collect all register uses and create an assignment for each of them.
91 // Ignore memory operands which are handled separately.
92 // Loop invariant: DefinedRegs[i] is true iif it has been set at least once
93 // before the current instruction.
Guillaume Chateletee9c2a172018-10-10 14:22:48 +000094 llvm::BitVector DefinedRegs = State.getRATC().emptyRegisters();
Guillaume Chateletc96a97b2018-09-20 12:22:18 +000095 std::vector<RegisterValue> RIV;
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000096 for (const InstructionTemplate &IT : Instructions) {
Clement Courbetd939f6d2018-09-13 07:40:53 +000097 // Returns the register that this Operand sets or uses, or 0 if this is not
98 // a register.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +000099 const auto GetOpReg = [&IT](const Operand &Op) -> unsigned {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000100 if (Op.isMemory())
Clement Courbetd939f6d2018-09-13 07:40:53 +0000101 return 0;
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000102 if (Op.isImplicitReg())
103 return Op.getImplicitReg();
104 if (Op.isExplicit() && IT.getValueFor(Op).isReg())
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000105 return IT.getValueFor(Op).getReg();
Clement Courbetd939f6d2018-09-13 07:40:53 +0000106 return 0;
107 };
108 // Collect used registers that have never been def'ed.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000109 for (const Operand &Op : IT.Instr.Operands) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000110 if (Op.isUse()) {
Clement Courbetd939f6d2018-09-13 07:40:53 +0000111 const unsigned Reg = GetOpReg(Op);
112 if (Reg > 0 && !DefinedRegs.test(Reg)) {
Clement Courbet54c2fa12018-11-08 12:37:56 +0000113 RIV.push_back(RegisterValue::zero(Reg));
Clement Courbetd939f6d2018-09-13 07:40:53 +0000114 DefinedRegs.set(Reg);
115 }
116 }
117 }
118 // Mark defs as having been def'ed.
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000119 for (const Operand &Op : IT.Instr.Operands) {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000120 if (Op.isDef()) {
Clement Courbetd939f6d2018-09-13 07:40:53 +0000121 const unsigned Reg = GetOpReg(Op);
122 if (Reg > 0)
123 DefinedRegs.set(Reg);
124 }
125 }
126 }
Guillaume Chateletc96a97b2018-09-20 12:22:18 +0000127 return RIV;
Clement Courbetd939f6d2018-09-13 07:40:53 +0000128}
129
Guillaume Chatelet296a8622018-10-15 09:09:19 +0000130llvm::Expected<std::vector<CodeTemplate>>
131generateSelfAliasingCodeTemplates(const Instruction &Instr) {
Clement Courbetd939f6d2018-09-13 07:40:53 +0000132 const AliasingConfigurations SelfAliasing(Instr, Instr);
Guillaume Chatelet296a8622018-10-15 09:09:19 +0000133 if (SelfAliasing.empty())
Clement Courbetd939f6d2018-09-13 07:40:53 +0000134 return llvm::make_error<SnippetGeneratorFailure>("empty self aliasing");
Guillaume Chatelet296a8622018-10-15 09:09:19 +0000135 std::vector<CodeTemplate> Result;
136 Result.emplace_back();
137 CodeTemplate &CT = Result.back();
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000138 InstructionTemplate IT(Instr);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000139 if (SelfAliasing.hasImplicitAliasing()) {
140 CT.Info = "implicit Self cycles, picking random values.";
141 } else {
142 CT.Info = "explicit self cycles, selecting one aliasing Conf.";
143 // This is a self aliasing instruction so defs and uses are from the same
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000144 // instance, hence twice IT in the following call.
145 setRandomAliasing(SelfAliasing, IT, IT);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000146 }
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000147 CT.Instructions.push_back(std::move(IT));
Guillaume Chateleta3849492018-10-15 09:21:21 +0000148 return std::move(Result);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000149}
150
Guillaume Chatelet296a8622018-10-15 09:09:19 +0000151llvm::Expected<std::vector<CodeTemplate>>
152generateUnconstrainedCodeTemplates(const Instruction &Instr,
153 llvm::StringRef Msg) {
154 std::vector<CodeTemplate> Result;
155 Result.emplace_back();
156 CodeTemplate &CT = Result.back();
Clement Courbetd939f6d2018-09-13 07:40:53 +0000157 CT.Info = llvm::formatv("{0}, repeating an unconstrained assignment", Msg);
158 CT.Instructions.emplace_back(Instr);
Guillaume Chateleta3849492018-10-15 09:21:21 +0000159 return std::move(Result);
Clement Courbetd939f6d2018-09-13 07:40:53 +0000160}
Guillaume Chatelet70ac0192018-09-27 09:23:04 +0000161
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000162std::mt19937 &randomGenerator() {
163 static std::random_device RandomDevice;
164 static std::mt19937 RandomGenerator(RandomDevice());
165 return RandomGenerator;
166}
167
Roman Lebedeva8223582019-04-08 10:11:00 +0000168size_t randomIndex(size_t Max) {
169 std::uniform_int_distribution<> Distribution(0, Max);
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000170 return Distribution(randomGenerator());
171}
172
173template <typename C>
174static auto randomElement(const C &Container) -> decltype(Container[0]) {
Roman Lebedeva8223582019-04-08 10:11:00 +0000175 assert(!Container.empty() &&
176 "Can't pick a random element from an empty container)");
177 return Container[randomIndex(Container.size() - 1)];
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000178}
179
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000180static void setRegisterOperandValue(const RegisterOperandAssignment &ROV,
181 InstructionTemplate &IB) {
182 assert(ROV.Op);
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000183 if (ROV.Op->isExplicit()) {
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000184 auto &AssignedValue = IB.getValueFor(*ROV.Op);
185 if (AssignedValue.isValid()) {
186 assert(AssignedValue.isReg() && AssignedValue.getReg() == ROV.Reg);
187 return;
188 }
189 AssignedValue = llvm::MCOperand::createReg(ROV.Reg);
190 } else {
Guillaume Chatelet09c28392018-10-09 08:59:10 +0000191 assert(ROV.Op->isImplicitReg());
192 assert(ROV.Reg == ROV.Op->getImplicitReg());
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000193 }
194}
195
196size_t randomBit(const llvm::BitVector &Vector) {
197 assert(Vector.any());
198 auto Itr = Vector.set_bits_begin();
Roman Lebedeva8223582019-04-08 10:11:00 +0000199 for (size_t I = randomIndex(Vector.count() - 1); I != 0; --I)
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000200 ++Itr;
201 return *Itr;
202}
203
204void setRandomAliasing(const AliasingConfigurations &AliasingConfigurations,
205 InstructionTemplate &DefIB, InstructionTemplate &UseIB) {
206 assert(!AliasingConfigurations.empty());
207 assert(!AliasingConfigurations.hasImplicitAliasing());
208 const auto &RandomConf = randomElement(AliasingConfigurations.Configurations);
209 setRegisterOperandValue(randomElement(RandomConf.Defs), DefIB);
210 setRegisterOperandValue(randomElement(RandomConf.Uses), UseIB);
211}
212
Roman Lebedev404bdb12019-04-06 14:16:26 +0000213void randomizeUnsetVariables(const ExegesisTarget &Target,
214 const llvm::BitVector &ForbiddenRegs,
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000215 InstructionTemplate &IT) {
216 for (const Variable &Var : IT.Instr.Variables) {
217 llvm::MCOperand &AssignedValue = IT.getValueFor(Var);
218 if (!AssignedValue.isValid())
Roman Lebedev404bdb12019-04-06 14:16:26 +0000219 Target.randomizeMCOperand(IT.Instr, Var, AssignedValue, ForbiddenRegs);
Guillaume Chatelet415b2fb2018-10-01 12:19:10 +0000220 }
221}
222
Clement Courbetd939f6d2018-09-13 07:40:53 +0000223} // namespace exegesis
Fangrui Song32401af2018-10-22 17:10:47 +0000224} // namespace llvm