diff --git a/llvm/tools/llvm-exegesis/lib/SnippetGenerator.cpp b/llvm/tools/llvm-exegesis/lib/SnippetGenerator.cpp --- a/llvm/tools/llvm-exegesis/lib/SnippetGenerator.cpp +++ b/llvm/tools/llvm-exegesis/lib/SnippetGenerator.cpp @@ -222,20 +222,19 @@ setRegisterOperandValue(randomElement(RandomConf.Uses), UseIB); } -static Error randomizeMCOperand(const LLVMState &State, - const Instruction &Instr, const Variable &Var, - MCOperand &AssignedValue, - const BitVector &ForbiddenRegs) { +static Expected randomizeMCOperand(const LLVMState &State, + const Instruction &Instr, + const Variable &Var, + const BitVector &ForbiddenRegs) { const Operand &Op = Instr.getPrimaryOperand(Var); if (Op.getExplicitOperandInfo().OperandType >= MCOI::OperandType::OPERAND_FIRST_TARGET) - return State.getExegesisTarget().randomizeTargetMCOperand( - Instr, Var, AssignedValue, ForbiddenRegs); + return State.getExegesisTarget().randomizeTargetMCOperand(Instr, Op, + ForbiddenRegs); switch (Op.getExplicitOperandInfo().OperandType) { case MCOI::OperandType::OPERAND_IMMEDIATE: // FIXME: explore immediate values too. - AssignedValue = MCOperand::createImm(1); - break; + return MCOperand::createImm(1); case MCOI::OperandType::OPERAND_REGISTER: { assert(Op.isReg()); auto AllowedRegs = Op.getRegisterAliasing().sourceBits(); @@ -249,13 +248,14 @@ Op.getRegisterAliasing().sourceBits())) .concat("\nforbidden:\n") .concat(debugString(State.getRegInfo(), ForbiddenRegs))); - AssignedValue = MCOperand::createReg(randomBit(AllowedRegs)); - break; + return MCOperand::createReg(randomBit(AllowedRegs)); } default: break; } - return Error::success(); + return make_error( + Twine("cannot create operand of type") + .concat(Twine(Op.getExplicitOperandInfo().OperandType))); } Error randomizeUnsetVariables(const LLVMState &State, @@ -263,10 +263,13 @@ InstructionTemplate &IT) { for (const Variable &Var : IT.getInstr().Variables) { MCOperand &AssignedValue = IT.getValueFor(Var); - if (!AssignedValue.isValid()) - if (auto Err = randomizeMCOperand(State, IT.getInstr(), Var, - AssignedValue, ForbiddenRegs)) - return Err; + if (!AssignedValue.isValid()) { + auto ErrorOr = + randomizeMCOperand(State, IT.getInstr(), Var, ForbiddenRegs); + if (!ErrorOr) + return ErrorOr.takeError(); + AssignedValue = *ErrorOr; + } } return Error::success(); } diff --git a/llvm/tools/llvm-exegesis/lib/Target.h b/llvm/tools/llvm-exegesis/lib/Target.h --- a/llvm/tools/llvm-exegesis/lib/Target.h +++ b/llvm/tools/llvm-exegesis/lib/Target.h @@ -114,13 +114,12 @@ // matter as long as it's large enough. virtual unsigned getMaxMemoryAccessSize() const { return 0; } - // Assigns a random operand of the right type to variable Var. + // Creates a random operand of the right type for operand Op. // The target is responsible for handling any operand starting from // OPERAND_FIRST_TARGET. - virtual Error randomizeTargetMCOperand(const Instruction &Instr, - const Variable &Var, - MCOperand &AssignedValue, - const BitVector &ForbiddenRegs) const { + virtual Expected + randomizeTargetMCOperand(const Instruction &Instr, const Operand &Op, + const BitVector &ForbiddenRegs) const { return make_error( "targets with target-specific operands should implement this"); } diff --git a/llvm/tools/llvm-exegesis/lib/X86/Target.cpp b/llvm/tools/llvm-exegesis/lib/X86/Target.cpp --- a/llvm/tools/llvm-exegesis/lib/X86/Target.cpp +++ b/llvm/tools/llvm-exegesis/lib/X86/Target.cpp @@ -628,9 +628,9 @@ unsigned getMaxMemoryAccessSize() const override { return 64; } - Error randomizeTargetMCOperand(const Instruction &Instr, const Variable &Var, - MCOperand &AssignedValue, - const BitVector &ForbiddenRegs) const override; + Expected + randomizeTargetMCOperand(const Instruction &Instr, const Operand &Op, + const BitVector &ForbiddenRegs) const override; void fillMemoryOperands(InstructionTemplate &IT, unsigned Reg, unsigned Offset) const override; @@ -710,15 +710,12 @@ return kLoopCounterReg; } -Error ExegesisX86Target::randomizeTargetMCOperand( - const Instruction &Instr, const Variable &Var, MCOperand &AssignedValue, +Expected ExegesisX86Target::randomizeTargetMCOperand( + const Instruction &Instr, const Operand &Op, const BitVector &ForbiddenRegs) const { - const Operand &Op = Instr.getPrimaryOperand(Var); switch (Op.getExplicitOperandInfo().OperandType) { case X86::OperandType::OPERAND_ROUNDING_CONTROL: - AssignedValue = - MCOperand::createImm(randomIndex(X86::STATIC_ROUNDING::TO_ZERO)); - return Error::success(); + return MCOperand::createImm(randomIndex(X86::STATIC_ROUNDING::TO_ZERO)); default: break; }