How to prevent an instruction to be executed more than once?

Hi LLVM-devs,

I am looking for a way to prevent an instruction to be executed more than
once, or duplicated .. I know that by default everything can be duplicated,
but is there a work-around?

The structure of the code is
BB1 BB2
   > >
   > >
   > >
   >----------|
        >
        v
       BB3

where the first instruction in BB3 is inserting an inline assembly which
contains a newly defined label:

            call void asm "BB4_label:\0A", ""()

I want to find a way to have this instruction executed only once. The
problem is that in the optimisation steps, LLVM duplicates BB3 and
concatenates it with BB1 and with BB2, therefore the label is defined twice,
which gives an error.
        
Is it possible to do this, without having to modify the back-end of LLVM ?

Thanks,
Alexandra

Hello-

Doing control-flow (jumps and/or labels) in inline asm is a really bad idea. What are you trying to achieve?

Alistair

I need to mark the beginning of some regions of code, that I want to patch at
runtime.
In this case, I want to mark the beginning of BB4 and then to patch the
jumps, because I want to temporarily change the flow at runtime and then to
restore the previous version.

In order to patch, I need to know the exact structure of the generated code.
So, I might have a BasicBlock like:

BB4:
call void asm "label_BB4:\0A", ""()
call void asm "jmp label_BB5\0A", ""()
br label %BB5

and in the generated code the br label %BB5 will actually never be reached.
But I introduce the jmp in inline asm in hexa code with .byte in order to
control if it is a long or a short jump and to know how many bytes to patch.

I tried to keep it short, please let me know if it is not clear enough.
Mainly, what I need to know statically is the beginning of the code
contained in the BasicBlock BB4 and the size of jump, in the generated code.

Alexandra

I need to mark the beginning of some regions of code, that I want to patch at
runtime.
In this case, I want to mark the beginning of BB4 and then to patch the
jumps, because I want to temporarily change the flow at runtime and then to
restore the previous version.

In order to patch, I need to know the exact structure of the generated code.
So, I might have a BasicBlock like:

BB4:
call void asm "label_BB4:\0A", ""()
call void asm "jmp label_BB5\0A", ""()
br label %BB5

You can't prevent various optimizations from making multiple copies of asm's. Also, branches from one asm to another, or into or out of an asm, do not work, in general. All of this is the same as gcc.

I'm not sure if a branch within the same asm is powerful enough to do what you want, but the way to avoid the duplicate-label problem in that case is to use local labels, "jmp 0f" etc. See gcc description of asm for examples.

As I have seen in the CodeGen phase there is the tail duplication step which
duplicates all my inlined asm. However, tail duplication checks for each
machine instruction if the description allows duplication or not.

/// isNotDuplicable - Return true if this instruction cannot be safely
00363 /// duplicated. For example, if the instruction has a unique labels
attached
00364 /// to it, duplicating it would cause multiple definition errors.
00365 bool isNotDuplicable() const {
00366 return Flags & (1 << TID::NotDuplicable);
00367 }

Is there a way to mark my instructions as not duplicable and to propagate
this description till the machine instruction level? What is the "unique
label" attached to an instruction?

Thanks.

Alexandra.