30 changes: 30 additions & 0 deletions llvm/unittests/CodeGen/MachineInstrTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "llvm/ADT/Triple.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineMemOperand.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/TargetFrameLowering.h"
Expand All @@ -25,6 +26,7 @@
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"

using namespace llvm;
Expand Down Expand Up @@ -442,6 +444,34 @@ TEST(MachineInstrDebugValue, AddDebugValueOperand) {
}
}

MATCHER_P(HasMIMetadata, MIMD, "") {
return arg->getDebugLoc() == MIMD.getDL() &&
arg->getPCSections() == MIMD.getPCSections();
}

TEST(MachineInstrBuilder, BuildMI) {
LLVMContext Ctx;
MDNode *PCS = MDNode::getDistinct(Ctx, None);
MDNode *DI = MDNode::getDistinct(Ctx, None);
DebugLoc DL(DI);
MIMetadata MIMD(DL, PCS);
EXPECT_EQ(MIMD.getDL(), DL);
EXPECT_EQ(MIMD.getPCSections(), PCS);
// Check common BuildMI() overloads propagate MIMetadata.
Module Mod("Module", Ctx);
auto MF = createMachineFunction(Ctx, Mod);
auto MBB = MF->CreateMachineBasicBlock();
MCInstrDesc MCID = {0, 0, 0, 0, 0, 0, 0, nullptr, nullptr, nullptr};
EXPECT_THAT(BuildMI(*MF, MIMD, MCID), HasMIMetadata(MIMD));
EXPECT_THAT(BuildMI(*MF, MIMD, MCID), HasMIMetadata(MIMD));
EXPECT_THAT(BuildMI(*MBB, MBB->end(), MIMD, MCID), HasMIMetadata(MIMD));
EXPECT_THAT(BuildMI(*MBB, MBB->end(), MIMD, MCID), HasMIMetadata(MIMD));
EXPECT_THAT(BuildMI(*MBB, MBB->instr_end(), MIMD, MCID), HasMIMetadata(MIMD));
EXPECT_THAT(BuildMI(*MBB, *MBB->begin(), MIMD, MCID), HasMIMetadata(MIMD));
EXPECT_THAT(BuildMI(*MBB, &*MBB->begin(), MIMD, MCID), HasMIMetadata(MIMD));
EXPECT_THAT(BuildMI(MBB, MIMD, MCID), HasMIMetadata(MIMD));
}

static_assert(std::is_trivially_copyable<MCOperand>::value,
"trivially copyable");

Expand Down