Search code examples
c++compiler-constructionllvmllvm-ir

LLVM | How to implement IR code generation of Logical Not operations


Suppose I have a function like this:

// global variables
llvm::LLVMContext* context;
llvm::Module* module;
llvm::IRBuilder<>* builder;

Value* logical_not_codegen(Value* operand) {
    return builder->CreateLogicalNotOp(operand);
}

According to the docs, there is no function like CreateLogicalNotOp in IRBuilder, so my question is:

Is there anything from IRBuilder I can use to generate IR code for a logical not operation?

EDIT: BTW I think builder.CreateNot() is binary not, am I correct?


Solution

  • BTW this is how you do it:

      auto size_in_bits = rhs->getType()->getPrimitiveSizeInBits();
      if (rhs->getType()->isFloatingPointTy()) {
        return _builder->CreateFCmpOEQ(rhs, ConstantFP::get(_builder->getFloatTy(), 0.0f));
      } else if (rhs->getType()->isSingleValueType()) {
        return _builder->CreateICmpEQ(rhs, ConstantInt::get(_builder->getIntNTy((unsigned)size_in_bits), 0, false));
      }