On Jan 11, 2007, at 7:14 AM, Chris Lattner wrote:> On Wed, 10 Jan 2007, Nicolas Geoffray wrote: >> I was looking for an indirect branch instruction in llvm, which would >> not take a BasicBlock as argument, but a value. Reid told me on >> IRC that >> there is no such instruction in llvm. >> >> Is this deliberate? Or did you never face the need of this >> instruction >> yet? > > The switch instruction serves the same functional role. In llvm- > gcc, the > GCC "Address of label" and "indirect goto" extensions are compiled > into a > switch instruction.I think the question is how to branch to a location of which you do not have a label. Presumably to avoid a stack overflow when you know that you only want to return the result of an indirect call. - Eric> > -Chris > > -- > http://nondot.org/sabre/ > http://llvm.org/ > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Hi Chris, Hi Eric, Eric van Riet Paap wrote:> On Jan 11, 2007, at 7:14 AM, Chris Lattner wrote: > > >> On Wed, 10 Jan 2007, Nicolas Geoffray wrote: >> >>> I was looking for an indirect branch instruction in llvm, which would >>> not take a BasicBlock as argument, but a value. Reid told me on >>> IRC that >>> there is no such instruction in llvm. >>> >>> Is this deliberate? Or did you never face the need of this >>> instruction >>> yet? >>> >> The switch instruction serves the same functional role. In llvm- >> gcc, the >> GCC "Address of label" and "indirect goto" extensions are compiled >> into a >> switch instruction. >> > > I think the question is how to branch to a location of which you do > not have a label. >Yes, that was the original question. But actually, for my purpose, Chris' solution is functional enough. However, I looked into llvm-gcc code and the switch instruction switches for all possible labels defined in the method. Does LLVM is smart enough to realize that it switches on the address of a label, therefore only has to generate a jmp in x86 or a {mtctr, bctr} couple in powerpc? Nicolas
On Thu, 11 Jan 2007, Nicolas Geoffray wrote:>> I think the question is how to branch to a location of which you do >> not have a label. >> > Yes, that was the original question. But actually, for my purpose, > Chris' solution is functional enough. However, I looked into llvm-gcc > code and the switch instruction switches for all possible labels defined > in the method. Does LLVM is smart enough to realize that it switches on > the address of a label, therefore only has to generate a jmp in x86 or a > {mtctr, bctr} couple in powerpc?I'm not sure what you mean. We do reasonable optimization of switch statements, but I'm sure there are cases we miss. If so, please let us know, so we can add them to lib/Target/README.txt -Chris -- http://nondot.org/sabre/ http://llvm.org/
On Thu, 11 Jan 2007, Eric van Riet Paap wrote:>> The switch instruction serves the same functional role. In llvm- gcc, >> the GCC "Address of label" and "indirect goto" extensions are compiled >> into a switch instruction. > > I think the question is how to branch to a location of which you do not > have a label.I'm not sure what you mean. The compiler needs to have an accurate CFG to be successful with dataflow analysis (i.e. not miscompile your code).> Presumably to avoid a stack overflow when you know that > you only want to return the result of an indirect call.It sounds like you just want tail call elimination? Jumping to the address of an indirect call is a great way to get a crash, except in the most trivial cases. -Chris -- http://nondot.org/sabre/ http://llvm.org/
Chris Lattner wrote:> On Thu, 11 Jan 2007, Eric van Riet Paap wrote: > > >>> The switch instruction serves the same functional role. In llvm- gcc, >>> the GCC "Address of label" and "indirect goto" extensions are compiled >>> into a switch instruction. >>> >> I think the question is how to branch to a location of which you do not >> have a label. >> > > I'm not sure what you mean. The compiler needs to have an accurate CFG to > be successful with dataflow analysis (i.e. not miscompile your code). > >It would be to the programer responsability that there are no "phis" with an indirect branch. So there is no dataflow analysis needed in such case.>> Presumably to avoid a stack overflow when you know that >> you only want to return the result of an indirect call. >> > > It sounds like you just want tail call elimination? Jumping to the > address of an indirect call is a great way to get a crash, except in the > most trivial cases. > >Again, it would be the programer responsabilty to not jump to an invalid address. But maybe this is undesirable for llvm (and I would understand, it's better to have a compiler that can certify most things won't go wrong).> -Chris > >Nicolas