在LLVM中的两个块之间插入一个块 [英] Inserting a block between two blocks in LLVM

查看:69
本文介绍了在LLVM中的两个块之间插入一个块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在LLVM的两个基本块之间插入一个块.因此,例如,如果一个基本块A跳到了基本块B,我想在它们之间插入一个基本块C,以便A跳到C而C跳到B.我该怎么做?我确实有一个基本想法,我需要更改基本块A的终止指令,以便将目标B替换为C,但是如何继续在它们之间添加新的基本块C?

I want to insert a block in between two basic blocks in LLVM. So for example, if a basic block A was jumping to basic block B, I want to insert a basic block C in between them such that A jumps to C and C jumps to B. How can I do that? I do have the basic idea that I need to change the terminating instruction of Basic Block A, such that the target B is replaced by C, but how do I go on adding the new basic block C in between?

推荐答案

是的,您需要更改(或替换)基本块A的终止指令-例如,如果它是分支,则可以使用 BranchInst:: setSuccessor().然后,您创建基本块C,并确保其终止指令跳到B,这将使其处于中间.

Yes, you need to change (or replace) the terminating instruction of basic block A - for example, if it's a branch, you can use BranchInst::setSuccessor(). You then create basic block C and make sure that its terminating instruction jumps to B, which will make it in-between.

您需要做的就是更改终结者的目标-您不需要重新排列内存中的块顺序或类似的内容.

All you need to do is to change the terminators' targets - you don't need to rearrange the block order in the memory or anything like that.

但是,您必须意识到,您需要担心两个特殊的说明-phi节点和着陆垫.

However, you must be aware that there are two special instructions you need to worry about - phi nodes and landing pads.

  • Phi节点仅指该块的前身.这意味着,如果在A和B之间插入C,则必须通过删除它们或使它们引用C而不是A来修复B中的所有phi节点.

  • Phi nodes only refer to the block's immediate predecessor. That means that if you insert C between A and B, you must fix all the phi nodes in B by either removing them or making them refer to C instead of A.

如果B是一个着陆板块(包含一个着陆板指令),则直接从调用指令的展开目标直接跳入该块是合法的.如果从A到B的跳跃是通过展开目标进行的,则除非在将C本身放入平台并从B移除平台的情况下,否则就不能在两者之间添加基本块.

If B is a landingpad block (contains a landingpad instruction), it is only legal to jump into it directly from the unwind target of an invoke instruction. If the jump from A to B is through the unwind target, you can't add a basic block in-between unless you make C itself into a landingpad and remove the landingpad from B.

这篇关于在LLVM中的两个块之间插入一个块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆