LLVM IR--如何将数组存储转换为memcpy?
LLVM IR包含数组作为基类型,因此IR中的“存储”指令将获取数组对象并将其存储到内存指针 我正在编译到C环境,所以我需要将“存储”指令转换为对memcpy的调用。我曾尝试使用IRBuilder使这项工作更容易,但我一直在研究如何获取对象的地址 我编写的函数如下所示:LLVM IR--如何将数组存储转换为memcpy?,c,llvm,memcpy,C,Llvm,Memcpy,LLVM IR包含数组作为基类型,因此IR中的“存储”指令将获取数组对象并将其存储到内存指针 我正在编译到C环境,所以我需要将“存储”指令转换为对memcpy的调用。我曾尝试使用IRBuilder使这项工作更容易,但我一直在研究如何获取对象的地址 我编写的函数如下所示: bool convert_array_store_to_memcpy(llvm::StoreInst *instruction) { llvm::Type *value_type = instruction->ge
bool convert_array_store_to_memcpy(llvm::StoreInst *instruction)
{
llvm::Type *value_type = instruction->getValueOperand()->getType();
if (!value_type->isArrayTy())
return false;
/* set up IRBuilder and get the pieces of the store */
llvm::IRBuilder<> Builder(llvm::getGlobalContext());
Builder.SetInsertPoint(instruction);
llvm::Value *destination = instruction->getPointerOperand();
llvm::Value *source = instruction->getValueOperand();
/* get the number of bytes by getting the size of the array (elements*element-size) */
llvm::ArrayType *array_type = cast<ArrayType>(value_type);
uint64_t element_count = array_type->getNumElements();
llvm::Type *element_type = array_type->getElementType();
DataLayout *targetData = new DataLayout(mod);
uint64_t element_size = targetData->getTypeAllocSize(element_type);
uint64_t size = element_count*element_size;
/* PROBLEM: I am trying to take the address of the start of the array */
llvm::Type *i32_type = llvm::IntegerType::getInt32Ty(llvm::getGlobalContext());
llvm::Constant *constant_int = llvm::ConstantInt::get(i32_type, 0, true);
Value *indexList[1] = {constant_int};
/* NEW PROBLEM:indexList seems to be the wrong type or contain the wrong type of thing */
llvm::Value *pointer_to_source = Builder.CreateGEP(source, ArrayRef<Value*>(indexList, 1));
unsigned alignment = instruction->getAlignment();
if (!array_type)
fprintf(stderr, "ERROR!\n");
/* insert the memcpy */
llvm::CallInst *memcpy_call = Builder.CreateMemCpy(destination,
pointer_to_source,
size,
alignment,
instruction->isVolatile());
/* erase the store */
instruction->eraseFromParent();
return true;
} /* convert_array_store_to_memcpy */
bool convert_array_store_to_memcpy(llvm::StoreInst*指令)
{
llvm::Type*value_Type=指令->GetValueOperator()->getType();
如果(!value\u type->isArray())
返回false;
/*设置iBuilder并获取商店的各个部分*/
llvm::IRBuilder生成器(llvm::getGlobalContext());
Builder.设置插入点(说明);
llvm::Value*destination=instruction->getPointerOperand();
llvm::Value*source=指令->GetValueOperator();
/*通过获取数组的大小(元素*元素大小)获取字节数*/
llvm::ArrayType*array_type=cast获取指向数组doBuilder.CreateGEP(源,ArrayRef(ConstantInt::get(i32_type,0),ConstantInt::get(i32_type,0));
获取指向数组doBuilder.CreateGEP(源,ArrayRef(ConstantInt::get(i32_type,0),ConstantInt::get)的指针(i32_类型,0));