In LLVM IR, I want to copy a set of Instructions a

2020-07-20 04:14发布

问题:

I want to copy these set of instructions from one part and paste to that in another part in IR

  %0 = load i32, i32* @x, align 4
  %1 = load i32, i32* @y, align 4
  %add = add nsw i32 %0, %1
  %2 = load i32, i32* @n, align 4
  %cmp = icmp slt i32 %add, %2
  %conv = zext i1 %cmp to i32

回答1:

Assuming you are using C++ API, you will just have to clone each instruction separately while fixing references between them. Something like the following:

llvm::ValueToValueMapTy vmap;

for (auto *inst: instructions_to_clone) {
  auto *new_inst = inst->clone();
  new_inst->insertBefore(insertion_pos);
  vmap[inst] = new_inst;
  llvm::RemapInstruction(new_inst, vmap,
                         RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
}


标签: llvm llvm-ir