#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/ADT/ilist_node.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/ModuleSlotTracker.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/Support/LowLevelTypeImpl.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
using namespace llvm;
namespace {
TEST(MachineOperandTest, ChangeToTargetIndexTest) {
MachineOperand MO = MachineOperand::CreateImm(50);
ASSERT_TRUE(MO.isImm());
ASSERT_TRUE(MO.getImm() == 50);
ASSERT_FALSE(MO.isTargetIndex());
MO.ChangeToTargetIndex(74, 57, 12);
ASSERT_TRUE(MO.isTargetIndex());
ASSERT_TRUE(MO.getIndex() == 74);
ASSERT_TRUE(MO.getOffset() == 57);
ASSERT_TRUE(MO.getTargetFlags() == 12);
}
TEST(MachineOperandTest, PrintRegisterMask) {
uint32_t Dummy;
MachineOperand MO = MachineOperand::CreateRegMask(&Dummy);
ASSERT_TRUE(MO.isRegMask());
ASSERT_TRUE(MO.getRegMask() == &Dummy);
std::string str;
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "<regmask ...>");
}
TEST(MachineOperandTest, PrintSubReg) {
MachineOperand MO = MachineOperand::CreateReg(
1, false, false, false,
false, false, false,
5, false, false);
ASSERT_TRUE(MO.isReg());
ASSERT_TRUE(MO.getReg() == 1);
ASSERT_TRUE(MO.getSubReg() == 5);
std::string str;
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "$physreg1.subreg5");
}
TEST(MachineOperandTest, PrintCImm) {
LLVMContext Context;
APInt Int(128, UINT64_MAX);
++Int;
ConstantInt *CImm = ConstantInt::get(Context, Int);
MachineOperand MO = MachineOperand::CreateCImm(CImm);
ASSERT_TRUE(MO.isCImm());
ASSERT_TRUE(MO.getCImm() == CImm);
ASSERT_TRUE(MO.getCImm()->getValue() == Int);
std::string str;
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "i128 18446744073709551616");
}
TEST(MachineOperandTest, PrintSubRegIndex) {
MachineOperand MO = MachineOperand::CreateImm(3);
ASSERT_TRUE(MO.isImm());
ASSERT_TRUE(MO.getImm() == 3);
std::string str;
raw_string_ostream OS(str);
MachineOperand::printSubRegIdx(OS, MO.getImm(), nullptr);
ASSERT_TRUE(OS.str() == "%subreg.3");
}
TEST(MachineOperandTest, PrintCPI) {
MachineOperand MO = MachineOperand::CreateCPI(0, 8);
ASSERT_TRUE(MO.isCPI());
ASSERT_TRUE(MO.getIndex() == 0);
ASSERT_TRUE(MO.getOffset() == 8);
std::string str;
{
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "%const.0 + 8");
}
str.clear();
MO.setOffset(-12);
{
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "%const.0 - 12");
}
}
TEST(MachineOperandTest, PrintTargetIndexName) {
MachineOperand MO = MachineOperand::CreateTargetIndex(0, 8);
ASSERT_TRUE(MO.isTargetIndex());
ASSERT_TRUE(MO.getIndex() == 0);
ASSERT_TRUE(MO.getOffset() == 8);
std::string str;
{
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "target-index(<unknown>) + 8");
}
str.clear();
MO.setOffset(-12);
{
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "target-index(<unknown>) - 12");
}
}
TEST(MachineOperandTest, PrintJumpTableIndex) {
MachineOperand MO = MachineOperand::CreateJTI(3);
ASSERT_TRUE(MO.isJTI());
ASSERT_TRUE(MO.getIndex() == 3);
std::string str;
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "%jump-table.3");
}
TEST(MachineOperandTest, PrintExternalSymbol) {
MachineOperand MO = MachineOperand::CreateES("foo");
ASSERT_TRUE(MO.isSymbol());
ASSERT_TRUE(MO.getSymbolName() == StringRef("foo"));
std::string str;
{
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "&foo");
}
str.clear();
MO.setOffset(12);
{
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "&foo + 12");
}
str.clear();
MO.setOffset(-12);
{
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "&foo - 12");
}
}
TEST(MachineOperandTest, PrintGlobalAddress) {
LLVMContext Ctx;
Module M("MachineOperandGVTest", Ctx);
M.getOrInsertGlobal("foo", Type::getInt32Ty(Ctx));
GlobalValue *GV = M.getNamedValue("foo");
MachineOperand MO = MachineOperand::CreateGA(GV, 12);
ASSERT_TRUE(MO.isGlobal());
ASSERT_TRUE(MO.getGlobal() == GV);
ASSERT_TRUE(MO.getOffset() == 12);
std::string str;
{
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "@foo + 12");
}
str.clear();
MO.setOffset(-12);
{
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "@foo - 12");
}
}
TEST(MachineOperandTest, PrintRegisterLiveOut) {
uint32_t Mask = 0;
MachineOperand MO = MachineOperand::CreateRegLiveOut(&Mask);
ASSERT_TRUE(MO.isRegLiveOut());
ASSERT_TRUE(MO.getRegLiveOut() == &Mask);
std::string str;
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "liveout(<unknown>)");
}
TEST(MachineOperandTest, PrintMetadata) {
LLVMContext Ctx;
Module M("MachineOperandMDNodeTest", Ctx);
NamedMDNode *MD = M.getOrInsertNamedMetadata("namedmd");
ModuleSlotTracker MST(&M);
Metadata *MDS = MDString::get(Ctx, "foo");
MDNode *Node = MDNode::get(Ctx, MDS);
MD->addOperand(Node);
MachineOperand MO = MachineOperand::CreateMetadata(Node);
ASSERT_TRUE(MO.isMetadata());
ASSERT_TRUE(MO.getMetadata() == Node);
std::string str;
raw_string_ostream OS(str);
MO.print(OS, MST, LLT{}, ~0U, false, false,
false, 0, nullptr,
nullptr);
ASSERT_TRUE(OS.str() == "!0");
}
TEST(MachineOperandTest, PrintMCSymbol) {
MCAsmInfo MAI;
Triple T = Triple("unknown-unknown-unknown");
MCContext Ctx(T, &MAI, nullptr, nullptr);
MCSymbol *Sym = Ctx.getOrCreateSymbol("foo");
MachineOperand MO = MachineOperand::CreateMCSymbol(Sym);
ASSERT_TRUE(MO.isMCSymbol());
ASSERT_TRUE(MO.getMCSymbol() == Sym);
std::string str;
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "<mcsymbol foo>");
}
TEST(MachineOperandTest, PrintCFI) {
MachineOperand MO = MachineOperand::CreateCFIIndex(8);
ASSERT_TRUE(MO.isCFIIndex());
ASSERT_TRUE(MO.getCFIIndex() == 8);
std::string str;
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "<cfi directive>");
}
TEST(MachineOperandTest, PrintIntrinsicID) {
MachineOperand MO = MachineOperand::CreateIntrinsicID(Intrinsic::bswap);
ASSERT_TRUE(MO.isIntrinsicID());
ASSERT_TRUE(MO.getIntrinsicID() == Intrinsic::bswap);
std::string str;
{
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "intrinsic(@llvm.bswap)");
}
str.clear();
MO = MachineOperand::CreateIntrinsicID((Intrinsic::ID)-1);
{
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "intrinsic(4294967295)");
}
}
TEST(MachineOperandTest, PrintPredicate) {
MachineOperand MO = MachineOperand::CreatePredicate(CmpInst::ICMP_EQ);
ASSERT_TRUE(MO.isPredicate());
ASSERT_TRUE(MO.getPredicate() == CmpInst::ICMP_EQ);
std::string str;
raw_string_ostream OS(str);
MO.print(OS, nullptr, nullptr);
ASSERT_TRUE(OS.str() == "intpred(eq)");
}
TEST(MachineOperandTest, HashValue) {
char SymName1[] = "test";
char SymName2[] = "test";
MachineOperand MO1 = MachineOperand::CreateES(SymName1);
MachineOperand MO2 = MachineOperand::CreateES(SymName2);
ASSERT_NE(SymName1, SymName2);
ASSERT_EQ(hash_value(MO1), hash_value(MO2));
ASSERT_TRUE(MO1.isIdenticalTo(MO2));
}
}