Using BuildMI to insert Intel MPX instruction BNDCU failed

Hello everyone, I’m a newbie of llvm. I’m trying to insert Intel MPX instruction BNDCU with BuildMI. I add my machinefunctionpass at addPreEmitPass2.
Here is the code of insertion:

BuildMI(MBB, MI, DL, TII->get(X86::BNDCU64rr)).addReg(X86::BND2, RegState::Define).addReg(X86::R10);

And here is to stack track when I compiler program with modified llc:

/home/shenyouren/workspace/build-llvm/bin/llc -o obj/simple.s obj/simple-opt.bc
llc: /home/shenyouren/workspace/llvm/include/llvm/ADT/SmallVector.h:154: const T& llvm::SmallVectorTemplateCommon<T, >::operator[](llvm::SmallVectorTemplateCommon<T, >::size_type) const [with T = llvm::MCOperand; = void; llvm::SmallVectorTemplateCommon<T, >::const_reference = const llvm::MCOperand&; llvm::SmallVectorTemplateCommon<T, >::size_type = long unsigned int]: Assertion `idx < size()’ failed.
Stack dump:
0. Program arguments: /home/shenyouren/workspace/build-llvm/bin/llc -o obj/simple.s obj/simple-opt.bc

  1. Running pass ‘Function Pass Manager’ on module ‘obj/simple-opt.bc’.
  2. Running pass ‘X86 Assembly Printer’ on function ‘@main
    #0 0x0000000003879bdf llvm::sys::PrintStackTrace(llvm::raw_ostream&) /home/shenyouren/workspace/llvm/lib/Support/Unix/Signals.inc:398:0
    #1 0x0000000003879c72 PrintStackTraceSignalHandler(void*) /home/shenyouren/workspace/llvm/lib/Support/Unix/Signals.inc:462:0
    #2 0x0000000003877e5b llvm::sys::RunSignalHandlers() /home/shenyouren/workspace/llvm/lib/Support/Signals.cpp:49:0
    #3 0x0000000003879454 SignalHandler(int) /home/shenyouren/workspace/llvm/lib/Support/Unix/Signals.inc:252:0
    #4 0x00007fcc8f4c9390 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x11390)
    #5 0x00007fcc8e67e428 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x35428)
    #6 0x00007fcc8e68002a abort (/lib/x86_64-linux-gnu/libc.so.6+0x3702a)
    #7 0x00007fcc8e676bd7 (/lib/x86_64-linux-gnu/libc.so.6+0x2dbd7)
    #8 0x00007fcc8e676c82 (/lib/x86_64-linux-gnu/libc.so.6+0x2dc82)
    #9 0x00000000016b63bb llvm::SmallVectorTemplateCommon<llvm::MCOperand, void>::operator[](unsigned long) const /home/shenyouren/workspace/llvm/include/llvm/ADT/SmallVector.h:154:0
    #10 0x00000000016b63bb llvm::MCInst::getOperand(unsigned int) const /home/shenyouren/workspace/llvm/include/llvm/MC/MCInst.h:182:0
    #11 0x00000000026695fd llvm::X86ATTInstPrinter::printOperand(llvm::MCInst const*, unsigned int, llvm::raw_ostream&) /home/shenyouren/workspace/llvm/lib/Target/X86/InstPrinter/X86ATTInstPrinter.cpp:185:0
    #12 0x0000000002664c06 llvm::X86ATTInstPrinter::printInstruction(llvm::MCInst const*, llvm::raw_ostream&) /home/shenyouren/workspace/build-llvm/lib/Target/X86/X86GenAsmWriter.inc:48204:0
    #13 0x0000000002668edc llvm::X86ATTInstPrinter::printInst(llvm::MCInst const*, llvm::raw_ostream&, llvm::StringRef, llvm::MCSubtargetInfo const&) /home/shenyouren/workspace/llvm/lib/Target/X86/InstPrinter/X86ATTInstPrinter.cpp:89:0
    #14 0x00000000031d5811 llvm::MCTargetStreamer::prettyPrintAsm(llvm::MCInstPrinter&, llvm::raw_ostream&, llvm::MCInst const&, llvm::MCSubtargetInfo const&) /home/shenyouren/workspace/llvm/lib/MC/MCStreamer.cpp:856:0
    #15 0x00000000031690e2 (anonymous namespace)::MCAsmStreamer::EmitInstruction(llvm::MCInst const&, llvm::MCSubtargetInfo const&, bool) /home/shenyouren/workspace/llvm/lib/MC/MCAsmStreamer.cpp:1752:0
    #16 0x0000000002355fdf llvm::X86AsmPrinter::EmitAndCountInstruction(llvm::MCInst&) /home/shenyouren/workspace/llvm/lib/Target/X86/X86MCInstLower.cpp:107:0
    #17 0x0000000002360ae9 llvm::X86AsmPrinter::EmitInstruction(llvm::MachineInstr const*) /home/shenyouren/workspace/llvm/lib/Target/X86/X86MCInstLower.cpp:2604:0
    #18 0x000000000292e629 llvm::AsmPrinter::EmitFunctionBody() /home/shenyouren/workspace/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp:1086:0
    #19 0x0000000002352908 llvm::X86AsmPrinter::runOnMachineFunction(llvm::MachineFunction&) /home/shenyouren/workspace/llvm/lib/Target/X86/X86AsmPrinter.cpp:79:0
    #20 0x0000000002bc6afa llvm::MachineFunctionPass::runOnFunction(llvm::Function&) /home/shenyouren/workspace/llvm/lib/CodeGen/MachineFunctionPass.cpp:62:0
    #21 0x000000000307eeac llvm::FPPassManager::runOnFunction(llvm::Function&) /home/shenyouren/workspace/llvm/lib/IR/LegacyPassManager.cpp:1520:0
    #22 0x000000000307f045 llvm::FPPassManager::runOnModule(llvm::Module&) /home/shenyouren/workspace/llvm/lib/IR/LegacyPassManager.cpp:1541:0
    #23 0x000000000307f3c0 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/shenyouren/workspace/llvm/lib/IR/LegacyPassManager.cpp:1597:0
    #24 0x000000000307fad5 llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/shenyouren/workspace/llvm/lib/IR/LegacyPassManager.cpp:1700:0
    #25 0x000000000307fce1 llvm::legacy::PassManager::run(llvm::Module&) /home/shenyouren/workspace/llvm/lib/IR/LegacyPassManager.cpp:1732:0
    #26 0x00000000013e9361 compileModule(char**, llvm::LLVMContext&) /home/shenyouren/workspace/llvm/tools/llc/llc.cpp:575:0
    #27 0x00000000013e7a6d main /home/shenyouren/workspace/llvm/tools/llc/llc.cpp:345:0
    #28 0x00007fcc8e669830 __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x20830)
    #29 0x00000000013e5779 _start (/home/shenyouren/workspace/build-llvm/bin/llc+0x13e5779)
    Makefile:25: recipe for target ‘obj/simple.s’ failed

I have checked X86ATTInstPrinter::printInstruction(llvm::MCInst const*, llvm::raw_ostream&) /home/shenyouren/workspace/build-llvm/lib/Target/X86/X86GenAsmWriter.inc:48204, and it will invoke printOperand(MI, 2, O); And I guess the BNDCU only have 2 operand, that’s why it failed.
But I don’t know how to fix it.
Is there anyone know how to solve this problem? I really appreciate for any help!

BNDCU shouldn’t be printing more than 2 operands.

On trunk right now, it uses case 4 for the first fragment (operand 1). Case 1 for the second fragment (a comma and a space). And fragment 2 for the third fragment (operand 0).

Are you using trunk or an earlier version?

Also shouldn’t, BND2 be a use not a def. The instruction takes a BND register and a GPR as inputs. It’s output is to signal #BR if the check fails.

Hi, Craig,

Thanks for your reply, that’s really helpful. I reviewed my code and find it’s actually the error of an Add insertion.
It’s so obsure for me to construct an instruction with BuildMI. Although there is a little description on code generator[1]. But the example is not enough for a starter to learn how to add operands. I have learned that using llvm-mc can tell u how to construct a instruction. like below:

echo “add r10, r11” | llvm-mc -x86-asm-syntax=intel -output-asm-variant=1 -show-inst

.text
add r10, r11 # <MCInst #206 ADD64rr

>

I believe put some words about how the construct instruction at [1] will be really helpful.

what’s the meaning of members in llvm:RegStates? like Define, Undefine, Implicit, Dead or use, what’s their present at target codes?

I really appreciate for all the help and reply.
Thank you very much.
ref:
[1]. https://llvm.org/docs/CodeGenerator.html#id23