LLVM inserts internal Cos function

I am trying to insert a call to the cos () intrinsic function in an LLVM pass. My code in FunctionPass:

std::vector<Type *> arg_type;
arg_type.push_back(Type::getFloatTy(getGlobalContext()));
Function *fun = Intrinsic::getDeclaration(F.getParent(), Intrinsic::cos, arg_type);
CallInst* callInst = CallInst::Create(fun, args, Twine("cos"), (Instruction *)&I);

When I leave the last line generated by IR, this is:

define i32 @main() nounwind uwtable {
entry:
...
}

declare float @llvm.cos.f32(float) nounwind readonly

but with CallInst enabled, all I get is:

0  opt                0x000000000094f4bf
1  opt                0x000000000094f9c9
2  libpthread.so.0    0x00007fb92b652cb0
3  opt                0x00000000008be244 llvm::Instruction::Instruction(llvm::Type*, unsigned int, llvm::Use*, unsigned int, llvm::Instruction*) + 148
4  LLVMObfuscation.so 0x00007fb92a66c52f
5  LLVMObfuscation.so 0x00007fb92a66c9a5
6  LLVMObfuscation.so 0x00007fb92a66df21
7  opt                0x00000000008e921f llvm::FPPassManager::runOnFunction(llvm::Function&) + 591
8  opt                0x00000000008e9293 llvm::FPPassManager::runOnModule(llvm::Module&) + 51
9  opt                0x00000000008e8f34 llvm::MPPassManager::runOnModule(llvm::Module&) + 532
10 opt                0x00000000008ea4fb llvm::PassManagerImpl::run(llvm::Module&) + 171
11 opt                0x0000000000496208 main + 4104
12 libc.so.6          0x00007fb92a89376d __libc_start_main + 237
13 opt                0x000000000049cfe5

What else do I need to do? I think that I do not need to define this function in the module.

the arguments assigned to the function are defined as follows:

std::vector<Value *> args;
args.push_back(fp);

where fp is pre-inserted Instructions:

Instruction *fp = BinaryOperator::Create(Instruction::FSub, ...

The variable I am is inst_iterator, but I do it outside for the loop.

Thank.

+3
source share
1 answer

I advise you to use IRBuilder. This simplifies the generation of IR in the LLVM pass. In your case, you can use it like this:

std::vector<Type *> arg_type;
arg_type.push_back(Type::getFloatTy(getGlobalContext()));
Function *fun = Intrinsic::getDeclaration(F.getParent(), Intrinsic::cos, arg_type);
IRBuilder<> Builder(&I);
Builder.CreateCall(fun, args);
+3

Source: https://habr.com/ru/post/1569240/


All Articles