LLVM ICmpInst производит неработающую инструкцию badref? - PullRequest
0 голосов
/ 17 июня 2020

Я пишу приведенный ниже код в проходе LLVM:

// context is retrieved from another valid instruction
Value *a = ConstantInt::get(IntegerType::get(context, 64), 1);
Value *b = ConstantInt::get(IntegerType::get(context, 64), 2);

Instruction *icmp = new ICmpInst(ICmpInst::ICMP_EQ, a, b);

icmp->print(errs());

Результат выглядит примерно так:

<badref> = icmp eq i64 1, 2

Любая причина, по которой у нас есть этот "badref" в выводе здесь ?

Спасибо.

1 Ответ

0 голосов
/ 10 июля 2020

Я не совсем понимаю, почему вы получаете badref; Я попытался набрать Builder.Insert(icmp), но badref все еще присутствовал. Вы можете использовать Builder.CreateICmpEQ, как в полном примере ниже, чтобы выполнить sh ту же задачу.

#include "llvm/ADT/APInt.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
#include <memory>

using namespace llvm;

static LLVMContext TheContext;
static IRBuilder<> Builder(TheContext);

int main() {
  static std::unique_ptr<Module> TheModule =
      std::make_unique<Module>("example.cpp", TheContext);

  FunctionType *mainType = FunctionType::get(Builder.getInt32Ty(), false);
  Function *main = Function::Create(mainType, Function::ExternalLinkage, "main",
                                    TheModule.get());

  BasicBlock *entry = BasicBlock::Create(TheContext, "entry", main);
  Builder.SetInsertPoint(entry);
  Value *a = ConstantInt::get(IntegerType::get(TheContext, 64), 1);
  Value *b = ConstantInt::get(IntegerType::get(TheContext, 64), 2);
  auto ret = Builder.CreateICmpEQ(a, b);
  /*return value for `main`*/
  Builder.CreateRet(ret);
  /*Emit the LLVM IR to the console*/
  TheModule->print(outs(), nullptr);
}
...