I use "builder->CreateSub" and "builder->CreateAdd" to generate LLVM-IR for subtraction and addition.
left = this->builder->CreateAlloca(llvm::Type::getInt32Ty(*this->llvm_context), nullptr, std::string("a1"));
right = this->builder->CreateAlloca(llvm::Type::getInt32Ty(*this->llvm_context), nullptr, std::string("b2"));
builder->CreateSub(left, right, "sub");
builder->CreateAdd(left, right, "add");
However, the generated IR (shown below) cannot be compiled/interpreted by LLVM (lli)
%a1 = alloca i32, align 4
store i32 100, ptr %a1, align 4
%b2 = alloca i32, align 4
store i32 10, ptr %b2, align 4
%add = add ptr %a1, %b2
%sub = sub ptr %c3, i32 2
%sub1 = sub ptr %add, %sub
Which gives error (on ptr):
lli: lli: test.ll:14:14: error: invalid operand type for instruction
%add = add ptr %a1, %b2
As pointed out by Nick Lewycky, we need load instruction to read memory from stack where alloca vars are stored. I added CreateLoad for left and right variables above.
builder->CreateLoad(llvm::Type::getInt32Ty(*this->llvm_context), alloca);