[LLVMdev] Emit a jump instruction to a place inside basicblock

Xiaoguang Wang xgwang09 at gmail.com
Mon Nov 10 08:46:49 PST 2014


On Thu, Oct 30, 2014 at 3:46 AM, serge Guelton <
serge.guelton at enst-bretagne.fr> wrote:

> On Wed, Oct 29, 2014 at 05:24:37PM -0400, Xiaoguang Wang wrote:
> > Hi all,
> >
> > I'm a beginner in LLVM. Currently, I want to implement a pass that
> generates a
> > jump table. The entry in that table is a jump to some place (may be an
> > instruction) in a basic block.
> >
> > I'm reading the JumpTable code in llvm 3.5, there is a table which
> contains
> > jump entries to functions. In AsmPrinter::doFinalization function from
> file
> > lib/CodeGen/AsmPrinter/AsmPrinter.cpp, it gets a MCSymbolRefExpr from the
> > function symbol.
> >
> > While my question is, is there a way to insert jump to place inside a
> basic
> > block?
> >
> > Thanks!
>
> ISTM you can split a basic bloc in two using ``llvm::SplitBlock'' then
> jump to the begining of the second part?
>

 Hi, I tried splitBasicBlock after the specific place in IR, and saved the
newly created BasicBlock*. This works well to create a basic block with a
LABEL to jump back. Then I modified the doFinalization method in AsmPrinter
to add a jump instruction to the LABEL. The result is it can create the
jump to the LABEL, but the LABEL just disappeared. It is like:

*The original IR before I use splitBasicBlock:*
entry:
 %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([8 x i8]*
@.str, i32 0, i32 0))
  ret i32 0

*The transformed IR code by using splitBasicBlock:*
entry:
  %call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([8 x
i8]* @.str, i32 0, i32 0))
  br label %RET_TABLE_0



RET_TABLE_0:                                      ; preds = %entry
  ret i32 0

*The final assembly when I modified AsmPrinter:doFinalization to print the
jump instruction back to LABEL:*
    calll   printf
    movl    %eax, -4(%ebp)          # 4-byte Spill
# BB#1:                                 # %.RET_TABLE_0
    movl    $0, %eax
... ...
    .align  8, 0x90
__LLVM_RET_TABLE_0:
    jmp .RET_TABLE_0

So the LLVM will optimize this branch in the pass? Can I disable this
optimization or is there other way to reserve the LABEL?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20141110/d36c0b74/attachment.html>


More information about the llvm-dev mailing list